97 lines
4.1 KiB
TypeScript
97 lines
4.1 KiB
TypeScript
import { assertEquals } from "@std/assert";
|
|
import { encode_skin } from "$/server/game/skins.ts";
|
|
import { test_game } from "./helpers.ts";
|
|
|
|
const SKIN = Deno.readFileSync("assets/sprites/skin_template.png");
|
|
// the downloads resolve on a later turn of the event loop
|
|
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
const chat = (messages: any[]) => messages.filter((m) => m.type === "chat").map((m) => m.text);
|
|
|
|
Deno.test("/skin downloads a png, tells everyone and is saved with the player", async () => {
|
|
const t = await test_game("mods");
|
|
t.downloads.set("https://example.com/skin.png", SKIN);
|
|
t.join(1, "alice");
|
|
t.join(2, "bob");
|
|
t.take(1);
|
|
t.take(2);
|
|
|
|
t.send(1, { type: "chat", text: "/skin https://example.com/skin.png" });
|
|
await settle();
|
|
|
|
const skin = encode_skin(SKIN);
|
|
const alice_id = t.game.players().find((p) => p.name === "alice")!.id;
|
|
const to_alice = t.take(1);
|
|
assertEquals(chat(to_alice), ["Downloading your skin...", "Skin changed"]);
|
|
assertEquals(to_alice.find((m) => m.type === "player_skin"), { type: "player_skin", id: alice_id, skin });
|
|
assertEquals(t.take(2).find((m) => m.type === "player_skin"), { type: "player_skin", id: alice_id, skin });
|
|
|
|
// players joining later get it with the rest of alice's info, and it's still there after a restart
|
|
const loaded = await test_game("mods", t.game.save());
|
|
loaded.join(1, "alice");
|
|
const join = loaded.join(2, "bob");
|
|
assertEquals(join.players.find((p: { name: string }) => p.name === "alice").skin, skin);
|
|
});
|
|
|
|
Deno.test("/skin <url> slim is for the model with thin arms, normal is the default", async () => {
|
|
const t = await test_game("mods");
|
|
t.downloads.set("https://example.com/alex.png", SKIN);
|
|
t.join(1, "alice");
|
|
t.take(1);
|
|
const skin_message = async (text: string) => {
|
|
t.send(1, { type: "chat", text });
|
|
await settle();
|
|
return t.take(1).find((m) => m.type === "player_skin");
|
|
};
|
|
|
|
assertEquals((await skin_message("/skin https://example.com/alex.png slim")).slim, true);
|
|
const loaded = await test_game("mods", t.game.save());
|
|
loaded.join(1, "alice");
|
|
assertEquals(loaded.join(2, "bob").players.find((p: { name: string }) => p.name === "alice").slim, true);
|
|
|
|
assertEquals((await skin_message("/skin https://example.com/alex.png normal")).slim, undefined);
|
|
assertEquals((await skin_message("/skin https://example.com/alex.png slim")).slim, true);
|
|
assertEquals((await skin_message("/skin https://example.com/alex.png")).slim, undefined);
|
|
assertEquals((await skin_message("/skin https://example.com/alex.png slim")).slim, true);
|
|
// back to the default skin is back to the normal model too
|
|
assertEquals((await skin_message("/skin")).slim, undefined);
|
|
|
|
t.send(1, { type: "chat", text: "/skin https://example.com/alex.png wide" });
|
|
assertEquals(t.take(1).some((m) => m.type === "player_skin"), false);
|
|
});
|
|
|
|
Deno.test("/skin says why a download can't be a skin, and /skin alone goes back to the default", async () => {
|
|
const t = await test_game("mods");
|
|
const wide = SKIN.slice();
|
|
new DataView(wide.buffer).setUint32(20, 32);
|
|
t.downloads.set("https://example.com/old.png", wide);
|
|
t.downloads.set("https://example.com/page.html", new TextEncoder().encode("<html></html>"));
|
|
t.join(1, "alice");
|
|
t.take(1);
|
|
|
|
const skin_messages = async (text: string) => {
|
|
t.send(1, { type: "chat", text });
|
|
await settle();
|
|
const messages = t.take(1);
|
|
return { chat: chat(messages), skins: messages.filter((m) => m.type === "player_skin") };
|
|
};
|
|
|
|
assertEquals((await skin_messages("/skin https://example.com/old.png")).chat, [
|
|
"Downloading your skin...",
|
|
"Couldn't change your skin: Skins have to be 64x64 (it's 64x32)",
|
|
]);
|
|
assertEquals(
|
|
(await skin_messages("/skin https://example.com/page.html")).chat.at(-1),
|
|
"Couldn't change your skin: That isn't a png",
|
|
);
|
|
const missing = await skin_messages("/skin https://example.com/nothing.png");
|
|
assertEquals(missing.chat.at(-1), "Couldn't change your skin: Couldn't download it");
|
|
assertEquals(missing.skins, []);
|
|
|
|
const reset = await skin_messages("/skin");
|
|
assertEquals(reset.chat, ["Your skin is back to the default"]);
|
|
assertEquals(reset.skins.length, 1);
|
|
assertEquals(reset.skins[0].skin, undefined);
|
|
});
|