Files
burrow-server/test/items.js
T
SnipzilandClaude Opus 5.5 e05565787f feat(server): staff tools, player-made items, picture safety, chat, multiplayer games
Staff and moderation
- Invisible staff (!invisible, "xiv"): hidden from players, half see-through
  to other staff, cannot chat while hidden. New permission staff.invisible.
- Summon and goto land on the player's exact spot ("xgt" before the move).
- Custom warnings (!warn PLAYER custom TEXT), shown in the warning popup.
- Mod notes (!note, !notes; lib/modnotes.js), on the player page and the
  dashboard.
- Granular permissions: per-role overrides (setting rolePermissions) and
  per-account grants and denies, applied live to staff who are playing
  (refreshStaff re-sends "xsp").
- Item control (lib/itemcontrol.js): lock items out of the item finder
  (!lock, !unlock, !locked), take an item from a player (!take), delete a
  custom item from the game and every inventory.
- GET items/find: search any item by name or id, marked locked, for the
  dashboards' quick lock.

Player-made items (lib/playeritems.js, "xcm")
- The Item Maker: make, rename, set the worn look, delete. 5 a day, 20 in all;
  names go through the name filter.
- Admin switches: Item Maker on/off, new items wait for approval, and a
  separate switch (on by default) that holds every player-uploaded picture
  for a moderator. Review queue for items.approve (moderator and up).
- Pictures (lib/itemart.js): PNG, JPEG and GIF cleaned of metadata, BMP
  converted to PNG; 512px and 300KB at most once cleaned.
- Worn as the maker chooses: a sticker (the picture) or a texture (the
  item's shape painted with the picture).

Picture safety
- A picture waiting for approval is never served publicly: /itemart/ 404s it,
  and it is only shown through /account|/mod|/admin/api/itemart to its maker
  and to staff who review or make items.
- A picture turned away or taken off is remembered by SHA-256 and cannot be
  uploaded again.
- Three strikes: three pictures turned away stops a player's uploads until a
  moderator lets them again. Staff can stop or allow uploads by hand.
- Accounts under 24 hours old cannot upload pictures (AJ_ITEM_ART_MIN_HOURS);
  uploads capped at 10 a day.
- Optional Google Cloud Vision SafeSearch (lib/image-moderation.js) for item
  pictures and Art Studio paintings when AJ_GOOGLE_VISION_API_KEY is set;
  fails closed when the scanner is unreachable.

Chat
- Emoji: Twemoji pictures served at /emoji/<codepoints>.png and cached in
  data/emoji (lib/emoji.js).
- Smart spam filtering before the word filter (lib/chatspam.js): long runs of
  one character, repeated words and lines, huge words and emoji floods are
  tidied, and repeats within 20 seconds are dropped.
- Typing indicator: "xty" is passed on to everyone in the room who can see
  the player, at most every 2 seconds.

Games (lib/minigames.js)
- Room games can be played together: a MULTIPLAYER table gives a game a
  waiting list (Single Player / Multiplayer). Players in the same room are
  matched, taken into the game together, and each one's moves are relayed to
  the others. Gem Breaker is the first: two players, "pos", "shoot" and
  "playerLost" relayed.
- Gem Breaker's start message now lists each player's id. Without it no
  player counted as local, so even single player could not aim.

Other
- Players stay connected through the client's new 75-minute AFK limit
  (idleTimeoutMs 80 minutes).
- Jam-a-Grams carry custom items and colours; Jammer Wall avatar photos are
  validated (walls.js photoParam).
- Weak passwords refused (common words, or containing the username).
- Code-scanning fixes: every logged value goes through logText
  (lib/logtext.js) against log injection, HSTS, path-only error logs, a
  single file read in lib/names.js.

Tests: items (pictures, approval, strikes, re-upload refusal, the hidden
waiting picture, the dashboard search), staff (invisibility, goto and summon
spots, custom warnings, notes), buddygames (two-player Gem Breaker, the
typing relay), and updates for main's verified badge and spawn rooms.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 21:44:10 -04:00

510 lines
31 KiB
JavaScript

"use strict";
// end to end: items the staff make while the server runs
// (lib/customitems.js), from the moderator panel and in the game: games
// already playing are sent them, the packs have them, the shop sells them,
// and they are still there after a restart.
const assert = require("assert");
const crypto = require("crypto");
const fs = require("fs");
const os = require("os");
const path = require("path");
const zlib = require("zlib");
const harness = require("./harness.js");
const {decode} = require("../lib/amf3.js");
const cdn = require("../lib/cdn.js");
const HTTP_PORT = 18093;
const GAME_PORT = 19352;
const BASE = `http://127.0.0.1:${HTTP_PORT}`;
const TOWNSHIP = "jamaa_township.room_main#1";
const ODD_CLOTHING_SHOP = 666;
const ODD_DEN_SHOP = 396;
const {postJson} = harness;
// the authenticator app's side of 2FA (lib/totp.js), as in test/staff.js.
const STEP = 30;
const base32Decode = text => {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
const bits = text.replace(/=+$/, "").toUpperCase().split("").map(c => alphabet.indexOf(c).toString(2).padStart(5, "0")).join("");
const bytes = [];
for (let i = 0; i + 8 <= bits.length; i += 8) {
bytes.push(parseInt(bits.slice(i, i + 8), 2));
}
return Buffer.from(bytes);
};
const codeAt = (secret, step) => {
const counter = Buffer.alloc(8);
counter.writeBigUInt64BE(BigInt(step));
const mac = crypto.createHmac("sha1", base32Decode(secret)).update(counter).digest();
const offset = mac[mac.length - 1] & 0x0F;
return String((mac.readUInt32BE(offset) & 0x7FFFFFFF) % 1000000).padStart(6, "0");
};
const stepNow = () => Math.floor(Date.now() / 1000 / STEP);
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
const waitForStep = async step => {
while (stepNow() < step) {
await sleep(250);
}
};
const webSession = () => {
let cookie = "";
return async (urlPath, body) => {
const headers = {cookie};
const options = body === undefined ? {headers} : {method: "POST", headers: {...headers, "Content-Type": "application/json"}, body: JSON.stringify(body)};
const res = await fetch(`${BASE}${urlPath}`, options);
const set = res.headers.get("set-cookie");
if (set) {
cookie = set.split(";")[0];
}
return {status: res.status, data: await res.json().catch(() => ({}))};
};
};
const say = (client, roomId, text) =>
client.send(`<msg t='sys'><body action='pubMsg' r='${roomId}'><txt><![CDATA[${text}%0]]></txt></body></msg>`);
// `onLogin(client)` runs before the room is joined, for what the server
// sends with the login (joining skips everything else on the way).
const arrive = async (username, password, totp, onLogin = async () => {}) => {
const res = await postJson(`${BASE}/authenticate`, {username, password, ...(totp ? {totp} : {})});
const auth = await res.json();
assert.strictEqual(res.status, 200, `${username} could not sign in: ${JSON.stringify(auth)}`);
const client = new harness.GameClient(GAME_PORT);
await client.connect();
const login = await client.logIn(username, auth.auth_token);
assert.strictEqual(login.status, "1");
await onLogin(client);
client.xt("rj", 1, TOWNSHIP, 1, 0, 0);
const rj = await client.untilXt("rj");
await client.untilXt("rp");
return {client, roomId: rj[4], token: auth.auth_token};
};
// a pack as the clients get it.
const pack = async id => Object.values(decode(zlib.inflateRawSync(Buffer.from(await (await fetch(`${BASE}/${cdn.hashedPath(`defPacks/${id}`)}`)).arrayBuffer()))));
const strings = async () => decode(zlib.inflateRawSync(Buffer.from(await (await fetch(`${BASE}/${cdn.hashedPath("defPacks/10230")}`)).arrayBuffer())));
const byId = (rows, id) => rows.find(row => String(row.id) === String(id));
// "xdi" [.., .., kind, rows as JSON], the next one of `kind`.
const untilDefs = async (client, kind) => {
for (;;) {
const fields = await client.untilXt("xdi");
if (fields[2] === kind) {
return JSON.parse(fields[3]);
}
}
};
const nextBox = async client => (await client.untilXt("xtl"))[2];
const shopList = async (client, roomId, shop) => {
client.xt("gl", roomId, shop);
return (await client.untilXt("gl")).slice(9);
};
const people = {Dev: "KettlePass21", Hal: "KettlePass22", Pat: "KettlePass23", Lou: "KettlePass24"};
const secret = {};
let usedStep = 0;
let made = {};
const first = async () => {
const registerJson = body => fetch(`${BASE}/api/register`, {method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify({avatarType: 1, agreed: true, ...body})});
for (const [username, password] of Object.entries(people)) {
assert.strictEqual((await registerJson({username, password})).status, 200);
}
const admin = webSession();
assert.strictEqual((await admin("/admin/api/setup", {password: "AdminPass99!"})).status, 200);
assert.strictEqual((await admin("/admin/api/login", {password: "AdminPass99!"})).status, 200);
for (const username of ["Dev", "Hal"]) {
const page = webSession();
assert.strictEqual((await page("/account/api/login", {username, password: people[username]})).status, 200);
secret[username] = (await page("/account/api/act", {action: "twoStepStart", current: people[username]})).data.setup.secret;
usedStep = Math.max(usedStep, stepNow());
assert.ok((await page("/account/api/act", {action: "twoStepConfirm", code: codeAt(secret[username], stepNow())})).data.ok);
}
assert.match((await admin("/admin/api/account", {username: "Dev", action: "role", role: "developer"})).data.ok, /developer/);
assert.match((await admin("/admin/api/account", {username: "Hal", action: "role", role: "helper"})).data.ok, /helper/);
// a game started before anything is made
const pat = await arrive("Pat", people.Pat);
// --- the panel: a developer makes items, and sees no players
usedStep++;
await waitForStep(usedStep);
const dev = webSession();
assert.strictEqual((await dev("/mod/api/login", {username: "Dev", password: people.Dev, code: codeAt(secret.Dev, usedStep)})).status, 200);
const me = (await dev("/mod/api/me")).data;
assert.strictEqual(me.role, "developer");
assert.ok(me.permissions.includes("items.create") && !me.permissions.includes("player.view"));
for (const page of ["/mod/api/players", "/mod/api/chat", "/mod/api/reports", "/mod/api/overview"]) {
assert.strictEqual((await dev(page)).status, 403, page);
}
assert.deepStrictEqual((await dev("/mod/api/items")).data.items, []);
// something to make them from: a clothing piece with several colours, and the bean bag chair
const clothing = await pack(1000);
const base = clothing.find(row => row.type === "1" && String(row.colors).split(",").length >= 3);
const looked = (await dev(`/mod/api/items/base?kind=clothing&id=${base.id}`)).data;
assert.strictEqual(looked.art, Number(base.layerDefId));
assert.strictEqual(looked.colors.length, String(base.colors).split(",").length);
const bodyPart = clothing.find(row => row.type === "0");
assert.strictEqual((await dev(`/mod/api/items/base?kind=clothing&id=${bodyPart.id}`)).status, 404);
// bad requests
const make = fields => dev("/mod/api/items", {action: "create", kind: "clothing", base: base.id, name: "Test Crown", cost: 25, ...fields});
assert.match((await make({name: ""})).data.error, /name/);
assert.match((await make({name: "<b>hi</b>"})).data.error, /name/);
assert.match((await make({cost: -1})).data.error, /price/);
assert.match((await make({base: bodyPart.id})).data.error, /body part/);
assert.match((await make({base: 999999})).data.error, /No clothing/);
assert.match((await make({colors: []})).data.error, /colours/);
// made: Pat's game is told at once
const colours = looked.colors.slice(0, 2);
const crown = await make({colors: colours});
assert.match(crown.data.ok, /^Test Crown \(clothing 50000\) made\.$/);
const [row] = await untilDefs(pat.client, "clothing");
assert.deepStrictEqual([row.id, row.name, row.layerDefId, row.layerId, row.colors, row.titleStrId],
["50000", "Test Crown", base.layerDefId, base.layerId, colours.join(","), "9100000"]);
// the packs a new game fetches have it, and its name
const crownRow = byId(await pack(1000), 50000);
assert.strictEqual(crownRow.layerDefId, base.layerDefId);
assert.strictEqual(crownRow.avatarUseFlags, "9007199254740991"); // any animal, like all clothing here
assert.strictEqual((await strings())[9100000], "Test Crown");
// first in the shop of oddities, and Pat can buy it in a colour it has
assert.strictEqual((await shopList(pat.client, pat.roomId, ODD_CLOTHING_SHOP))[0], "50000");
pat.client.xt("ib", pat.roomId, ODD_CLOTHING_SHOP, 1, 50000, 2, 0);
assert.strictEqual((await pat.client.untilXt("ib"))[2], "0"); // a third colour it was not given
pat.client.xt("ib", pat.roomId, ODD_CLOTHING_SHOP, 1, 50000, 1, 0);
const bought = await pat.client.untilXt("il");
assert.ok(bought.includes("50000"));
assert.strictEqual((await pat.client.untilXt("ib"))[2], "1");
// a den item, from the bean bag chair
const chair = await dev("/mod/api/items", {action: "create", kind: "den", base: 1, name: "Test Chair", cost: 10});
assert.match(chair.data.ok, /^Test Chair \(den item 50000\) made\.$/);
const [chairRow] = await untilDefs(pat.client, "den");
assert.deepStrictEqual([chairRow.id, chairRow.abbrName, chairRow.name, chairRow.nameStrId], ["50000", "BeanBagChair", "Test Chair", "9200000"]);
assert.strictEqual((await shopList(pat.client, pat.roomId, ODD_DEN_SHOP))[0], "50000");
assert.strictEqual(byId(await pack(1030), 50000).abbrName, "BeanBagChair");
// the item finder has them
const found = await (await fetch(`${BASE}/api/search?kind=all&q=50000`)).json();
assert.deepStrictEqual(found.results.filter(entry => entry.id === 50000).map(entry => `${entry.kind} ${entry.name}`).sort(), ["clothing Test Crown", "den Test Chair"]);
// changed: renamed and taken off sale, and Pat is told the new name
assert.match((await dev("/mod/api/items", {action: "update", kind: "clothing", id: 50000, name: "Gold Crown", inShop: false})).data.ok, /Gold Crown .* changed/);
assert.strictEqual((await untilDefs(pat.client, "clothing"))[0].name, "Gold Crown");
assert.ok(!(await shopList(pat.client, pat.roomId, ODD_CLOTHING_SHOP)).includes("50000"));
assert.strictEqual((await strings())[9100000], "Gold Crown");
// --- in the game: !item, for whoever may make items
usedStep++;
await waitForStep(usedStep);
// a game logging in is sent every item made so far
let atLogin = null;
const devGame = await arrive("Dev", people.Dev, codeAt(secret.Dev, usedStep), async client => {
atLogin = await untilDefs(client, "clothing");
});
assert.deepStrictEqual(atLogin.map(entry => entry.name), ["Gold Crown"]);
say(devGame.client, devGame.roomId, `!item clothing ${base.id} Quick Hat`);
assert.strictEqual(await nextBox(devGame.client), "Made Quick Hat, clothing 50001. It is in the item finder now.");
assert.strictEqual((await untilDefs(pat.client, "clothing"))[0].name, "Quick Hat");
say(devGame.client, devGame.roomId, "!item list");
assert.match(await nextBox(devGame.client), /^Latest items made: 50001 Quick Hat \(clothing\); 50000 Test Chair \(den\); 50000 Gold Crown \(clothing, off sale\)$/);
say(devGame.client, devGame.roomId, "!item hat 12 x");
assert.match(await nextBox(devGame.client), /^Use: !item/);
// a helper may not: it is chat (another account: the same step will do)
const hal = await arrive("Hal", people.Hal, codeAt(secret.Hal, usedStep));
say(hal.client, hal.roomId, `!item clothing ${base.id} Nope`);
for (;;) {
const line = await pat.client.untilXt("uc");
if (line[3] === `!item clothing ${base.id} Nope`) {
break;
}
}
assert.strictEqual((await dev("/mod/api/items")).data.items.length, 3);
// --- locked out of the item finder (lib/itemcontrol.js): players do not
// find it or take it; the owner here, and staff who may lock, still do
const searchAs = async (q, who) => (await (await fetch(`${BASE}/api/search?kind=clothing&q=${q}${who ? `&username=${who.name}&token=${who.token}` : ""}`)).json()).results;
// (as the item finder page and the client send it: no Origin of another site)
const giveAs = (body, token) => fetch(`${BASE}/api/give`, {method: "POST", headers: {"Content-Type": "application/json"},
body: JSON.stringify({...body, ...(token ? {token} : {})})});
const patAs = {name: "Pat", token: pat.token};
assert.ok((await searchAs(50001, patAs)).some(entry => entry.id === 50001));
assert.match((await dev("/mod/api/items", {action: "lock", kind: "clothing", id: 50001})).data.ok, /^Quick Hat \(clothing 50001\) is locked out of the item finder\.$/);
assert.match((await dev("/mod/api/items", {action: "lock", kind: "clothing", id: 50001})).data.error, /already locked/);
assert.ok(!(await searchAs(50001, patAs)).some(entry => entry.id === 50001));
const refused = await giveAs({username: "Pat", kind: "clothing", id: 50001}, pat.token);
assert.deepStrictEqual([refused.status, (await refused.json()).error], [403, "That item is locked out of the item finder."]);
assert.deepStrictEqual((await searchAs(50001)).filter(entry => entry.id === 50001).map(entry => [entry.name, entry.locked]), [["Quick Hat (locked)", true]]);
assert.strictEqual((await searchAs(50001, {name: "Dev", token: devGame.token})).find(entry => entry.id === 50001).name, "Quick Hat (locked)");
assert.deepStrictEqual((await dev("/mod/api/items")).data.locked, [{kind: "clothing", id: 50001, name: "Quick Hat"}]);
// the dashboards' lock finds it by name, marked locked, with the count
const findLock = (await dev("/mod/api/items/find?q=quick%20hat&kind=clothing")).data;
assert.deepStrictEqual([findLock.lockedCount, findLock.results.find(entry => entry.id === 50001)],
[1, {kind: "clothing", id: 50001, cost: findLock.results.find(entry => entry.id === 50001).cost, name: "Quick Hat", locked: true, custom: true}]);
say(devGame.client, devGame.roomId, "!locked");
assert.strictEqual(await nextBox(devGame.client), "Locked out of the item finder: clothing 50001 Quick Hat");
// --- taken from a player: every one of it, their game told
assert.strictEqual((await giveAs({username: "Pat", kind: "clothing", id: 50001, colorIndex: 0})).status, 200); // the owner, here
assert.strictEqual((await giveAs({username: "Pat", kind: "clothing", id: 50001, colorIndex: 0})).status, 200);
say(devGame.client, devGame.roomId, "!take Pat clothing 50001");
assert.strictEqual(await nextBox(devGame.client), "Took 2 of Quick Hat (clothing 50001) from Pat.");
for (;;) {
const changed = await pat.client.untilXt("il");
if (changed[2] === "2" && changed[8] === "2") { // two removed
break;
}
}
say(devGame.client, devGame.roomId, "!take Pat clothing 50001");
assert.strictEqual(await nextBox(devGame.client), "Pat has no Quick Hat (clothing 50001).");
assert.strictEqual((await giveAs({username: "Pat", kind: "den", id: 50000})).status, 200);
assert.match((await dev("/mod/api/items", {action: "take", username: "Pat", kind: "den", id: 50000})).data.ok, /^Took 1 of Test Chair \(den item 50000\) from Pat\.$/);
assert.match((await dev("/mod/api/items", {action: "unlock", kind: "clothing", id: 50001})).data.ok, /back in the item finder/);
assert.ok((await searchAs(50001, patAs)).some(entry => entry.id === 50001));
// --- a custom item deleted: out of the game and every inventory
say(devGame.client, devGame.roomId, `!item clothing ${base.id} Doomed Hat`);
assert.strictEqual(await nextBox(devGame.client), "Made Doomed Hat, clothing 50002. It is in the item finder now.");
assert.strictEqual((await giveAs({username: "Pat", kind: "clothing", id: 50002, colorIndex: 0})).status, 200);
assert.match((await dev("/mod/api/items", {action: "delete", kind: "clothing", id: 50002})).data.ok, /^Deleted Doomed Hat \(clothing 50002\), and took 1 from players\.$/);
assert.ok(!(await searchAs(50002)).some(entry => entry.id === 50002));
assert.strictEqual((await giveAs({username: "Pat", kind: "clothing", id: 50002, colorIndex: 0})).status, 400);
assert.ok(!byId(await pack(1000), 50002));
assert.strictEqual((await dev("/mod/api/items")).data.items.length, 3);
// --- a player's own items (lib/playeritems.js, the client's Item Maker):
// free, given to them, found by them only, at most five a day
const maker = async (...params) => {
pat.client.xt("xcm", pat.roomId, ...params);
const answer = await pat.client.untilXt("xcm");
return {ok: answer[2] === "1", said: answer[3], today: Number(answer[4]), inAll: Number(answer[5]), mine: JSON.parse(answer[6])};
};
assert.deepStrictEqual(await maker("info"), {ok: true, said: "", today: 5, inAll: 20, mine: []});
const own = await maker("make", "clothing", base.id, "Pat Hat");
assert.deepStrictEqual([own.ok, own.said, own.today, own.inAll], [true, "Made Pat Hat. It is in your clothing now.", 4, 19]);
const patHat = own.mine[0];
assert.deepStrictEqual([patHat.kind, patHat.name, patHat.base], ["clothing", "Pat Hat", Number(base.id)]);
assert.ok((await searchAs(patHat.id, patAs)).some(entry => entry.id === patHat.id));
const halAs = {name: "Hal", token: hal.token};
assert.ok(!(await searchAs(patHat.id, halAs)).some(entry => entry.id === patHat.id));
assert.strictEqual((await giveAs({username: "Hal", kind: "clothing", id: patHat.id, colorIndex: 0}, hal.token)).status, 403);
assert.ok((await searchAs(patHat.id, {name: "Dev", token: devGame.token})).some(entry => entry.id === patHat.id)); // staff who make items
assert.match((await maker("make", "clothing", base.id, "Admin Hat")).said, /not allowed/);
assert.match((await maker("make", "clothing", bodyPart.id, "Body")).said, /cannot be made into one of your own/);
// the base list offers only what can be copied
assert.ok(!(await (await fetch(`${BASE}/api/search?base=1&kind=clothing&q=${bodyPart.id}`)).json()).results.some(entry => entry.id === bodyPart.id));
for (let count = 2; count <= 5; count++) {
assert.ok((await maker("make", "den", 1, `Pat Chair ${count}`)).ok);
}
const full = await maker("make", "den", 1, "One Too Many");
assert.deepStrictEqual([full.ok, full.today], [false, 0]);
assert.match(full.said, /made 5 items today/);
// --- its own picture (lib/itemart.js): checked and cleaned, served, and
// every game told; only by its maker or staff who make items
const crc32 = buf => {
let c = ~0;
for (const byte of buf) {
c ^= byte;
for (let k = 0; k < 8; k++) {
c = (c >>> 1) ^ (0xEDB88320 & -(c & 1));
}
}
return (~c) >>> 0;
};
const chunk = (type, data) => {
const head = Buffer.alloc(8);
head.writeUInt32BE(data.length);
head.write(type, 4, "latin1");
const crc = Buffer.alloc(4);
crc.writeUInt32BE(crc32(Buffer.concat([head.subarray(4), data])));
return Buffer.concat([head, data, crc]);
};
const png = (side, extra = []) => {
const ihdr = Buffer.alloc(13);
ihdr.writeUInt32BE(side, 0);
ihdr.writeUInt32BE(side, 4);
ihdr[8] = 8;
ihdr[9] = 2;
const raw = Buffer.alloc((side * 3 + 1) * side, 0x7F);
return Buffer.concat([Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]), chunk("IHDR", ihdr), ...extra,
chunk("IDAT", zlib.deflateSync(raw)), chunk("IEND", Buffer.alloc(0))]);
};
const upload = (who, entry, body, extra = "") => fetch(`${BASE}/api/itemart?kind=${entry.kind}&id=${entry.id}&token=${who.token}${extra}`,
{method: "POST", headers: {"Content-Type": "application/octet-stream"}, body}).then(response => response.json());
const hatEntry = (await maker("info")).mine.find(entry => entry.name === "Pat Hat");
// by default a player's picture waits for a moderator, even with items
// let in at once: nobody else is shown it, not even by its address
assert.match((await upload(pat, hatEntry, png(30))).ok, /waiting for a moderator/);
assert.strictEqual((await maker("info")).mine.find(entry => entry.kind === hatEntry.kind && entry.id === hatEntry.id).art, "");
assert.strictEqual((await fetch(`${BASE}/itemart/clothing/${hatEntry.id}.png`)).status, 404);
const pendingUrl = (await admin("/admin/api/review")).data.waiting.find(entry => entry.id === hatEntry.id).artUrl;
assert.match(pendingUrl, /^\/admin\/api\/itemart\?kind=clothing&id=\d+&v=p[0-9a-f]{12}$/);
assert.strictEqual((await admin(pendingUrl)).status, 200);
assert.strictEqual((await fetch(`${BASE}${pendingUrl}`)).status, 404); // signed in to nothing
assert.match((await admin("/admin/api/review", {kind: "clothing", id: hatEntry.id, approve: true})).data.ok, /picture is shown now/);
assert.strictEqual((await fetch(`${BASE}/itemart/clothing/${hatEntry.id}.png`)).status, 200);
while (!(await untilDefs(pat.client, "clothing")).some(row => row.art)) {
// every game is told of it waiting (no picture), then of it shown
}
// the rest of these, shown at once (the admin's switch)
assert.strictEqual((await admin("/admin/api/item-switches", {artApproval: false})).status, 200);
assert.strictEqual((await admin("/admin/api/review")).data.artApproval, false);
assert.match((await upload(pat, hatEntry, png(32, [chunk("tEXt", Buffer.from("Author\x00Pat at home"))]))).ok, /new picture/);
const told = (await untilDefs(pat.client, "clothing")).map(row => [row.id, row.art]);
const sameItem = entry => entry.kind === hatEntry.kind && entry.id === hatEntry.id;
const withArt = (await maker("info")).mine.find(sameItem);
assert.match(withArt.art, /^p[0-9a-f]{12}$/);
assert.deepStrictEqual(told, [[String(hatEntry.id), withArt.art]]);
const served = await fetch(`${BASE}/itemart/clothing/${hatEntry.id}.png?v=${withArt.art}`);
assert.deepStrictEqual([served.status, served.headers.get("content-type"), served.headers.get("x-content-type-options")], [200, "image/png", "nosniff"]);
const bytes = Buffer.from(await served.arrayBuffer());
assert.ok(!bytes.includes(Buffer.from("tEXt")) && !bytes.includes(Buffer.from("Pat at home")), "the text chunk was left in");
assert.match((await upload(pat, hatEntry, Buffer.from("<html><script>alert(1)</script></html>"))).error, /not a PNG, JPEG, GIF or BMP/);
// a GIF is kept a GIF (without its comment); a BMP is made a PNG
const gif = Buffer.concat([Buffer.from("GIF89a"), Buffer.from([16, 0, 16, 0, 0x80, 0, 0]), Buffer.alloc(6, 0x55),
Buffer.from([0x21, 0xFE, 4]), Buffer.from("Bob!"), Buffer.from([0]), Buffer.from([0x2C, 0, 0, 0, 0, 16, 0, 16, 0, 0, 2, 3, 1, 2, 3, 0, 0x3B])]);
assert.match((await upload(pat, hatEntry, gif)).ok, /new picture/);
const gifArt = (await maker("info")).mine.find(entry => entry.kind === hatEntry.kind && entry.id === hatEntry.id).art;
const gifServed = await fetch(`${BASE}/itemart/clothing/${hatEntry.id}.png?v=${gifArt}`);
assert.strictEqual(gifServed.headers.get("content-type"), "image/gif");
assert.ok(!Buffer.from(await gifServed.arrayBuffer()).includes(Buffer.from("Bob!")), "the GIF comment was left in");
const bmp = Buffer.alloc(54 + 32 * 3 * 32);
bmp.write("BM");
bmp.writeUInt32LE(bmp.length, 2);
bmp.writeUInt32LE(54, 10);
bmp.writeUInt32LE(40, 14);
bmp.writeInt32LE(32, 18);
bmp.writeInt32LE(32, 22);
bmp.writeUInt16LE(1, 26);
bmp.writeUInt16LE(24, 28);
assert.match((await upload(pat, hatEntry, bmp)).ok, /new picture/);
const bmpArt = (await maker("info")).mine.find(entry => entry.kind === hatEntry.kind && entry.id === hatEntry.id).art;
assert.strictEqual((await fetch(`${BASE}/itemart/clothing/${hatEntry.id}.png?v=${bmpArt}`)).headers.get("content-type"), "image/png");
assert.match((await upload(pat, hatEntry, png(600))).error, /512 pixels/);
assert.match((await upload(hal, hatEntry, png(16))).error, /not one of your items/);
assert.match((await fetch(`${BASE}/api/itemart?kind=clothing&id=${hatEntry.id}`, {method: "POST", body: png(16)}).then(r => r.json())).error, /Sign in/);
// a JPEG keeps its picture and loses its EXIF (where a photo was taken)
const segment = (marker, data) => Buffer.concat([Buffer.from([0xFF, marker, (data.length + 2) >> 8, (data.length + 2) & 0xFF]), data]);
const jpeg = Buffer.concat([Buffer.from([0xFF, 0xD8]), segment(0xE0, Buffer.from("JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00", "latin1")),
segment(0xE1, Buffer.from("Exif\x00\x00GPS 51.5N 0.12W", "latin1")), segment(0xC0, Buffer.from([8, 0, 20, 0, 30, 1, 1, 0x11, 0])),
segment(0xDA, Buffer.from([1, 1, 0, 0, 0x3F, 0])), Buffer.from([0x12, 0x34, 0xFF, 0xD9])]);
assert.match((await upload(pat, hatEntry, jpeg)).ok, /new picture/);
const jpegArt = (await maker("info")).mine.find(sameItem).art;
const jpegBytes = Buffer.from(await (await fetch(`${BASE}/itemart/clothing/${hatEntry.id}.png?v=${jpegArt}`)).arrayBuffer());
assert.ok(jpegBytes.includes(Buffer.from("JFIF")) && !jpegBytes.includes(Buffer.from("GPS")), "the EXIF was left in");
assert.match((await upload(pat, hatEntry, Buffer.alloc(0), "&remove=1")).ok, /taken away/);
assert.strictEqual((await fetch(`${BASE}/itemart/clothing/${hatEntry.id}.png`)).status, 404);
assert.match((await upload(pat, hatEntry, png(24))).ok, /new picture/); // deleted with the item, below
assert.match((await maker("rename", "clothing", hatEntry.id, "Party Hat")).said, /^Renamed it Party Hat\.$/);
// how it is worn: a sticker by default, a texture when its maker says so, every game told
pat.client.xt("xcm", pat.roomId, "look", "clothing", hatEntry.id, "texture");
assert.strictEqual((await untilDefs(pat.client, "clothing"))[0].artMode, "texture");
assert.strictEqual((await pat.client.untilXt("xcm"))[2], "1"); // its own answer, after every game was told
const wornAs = await maker("info");
assert.strictEqual(wornAs.mine.find(entry => entry.kind === "clothing" && entry.id === hatEntry.id).artMode, "texture");
assert.match((await maker("look", "clothing", hatEntry.id, "melted")).said, /sticker or texture/);
// deleting: their own only, and gone from everyone
assert.match((await maker("delete", "clothing", 50001)).said, /not one of your items/);
for (const entry of full.mine) {
assert.match((await maker("delete", entry.kind, entry.id)).said, /^Deleted /);
}
assert.deepStrictEqual((await maker("info")).mine, []);
assert.strictEqual((await fetch(`${BASE}/itemart/clothing/${hatEntry.id}.png`)).status, 404);
assert.strictEqual((await dev("/mod/api/items")).data.items.length, 3);
// --- the admin's switches: what players make waits for a moderator first,
// out of the game till then; or the Item Maker is off
assert.strictEqual((await admin("/admin/api/item-switches", {enabled: true, approval: true})).status, 200);
const asked = await maker("make", "clothing", base.id, "Waiting Hat");
assert.match(asked.said, /waiting for a moderator/);
const waitingHat = asked.mine.find(entry => entry.name === "Waiting Hat");
assert.strictEqual(waitingHat.waiting, "item");
assert.ok(!(await searchAs(waitingHat.id)).some(entry => entry.id === waitingHat.id)); // not in the game yet
const queue = (await admin("/admin/api/review")).data;
assert.deepStrictEqual([queue.enabled, queue.approval, queue.waiting.map(entry => `${entry.name} ${entry.waiting} ${entry.maker}`)],
[true, true, ["Waiting Hat item Pat"]]);
assert.strictEqual((await dev("/mod/api/review")).status, 403); // a developer approves nothing
assert.match((await admin("/admin/api/review", {kind: "clothing", id: waitingHat.id, approve: true})).data.ok, /in the game, and Pat has one/);
for (;;) {
const changed = await pat.client.untilXt("il");
if (changed[2] === "2" && changed[10] === String(waitingHat.id)) { // given to Pat
break;
}
}
assert.ok((await searchAs(waitingHat.id, patAs)).some(entry => entry.id === waitingHat.id));
// a new picture waits too, and is not shown till then
assert.match((await upload(pat, waitingHat, png(20))).ok, /waiting for a moderator/);
assert.strictEqual((await maker("info")).mine.find(entry => entry.id === waitingHat.id && entry.kind === "clothing").waiting, "picture");
assert.match((await admin("/admin/api/review", {kind: "clothing", id: waitingHat.id, approve: false})).data.ok,
/picture was turned away\. Pat has had 1 of 3 pictures turned away\.$/);
assert.strictEqual((await admin("/admin/api/review")).data.waiting.length, 0);
// the same picture cannot come back; after three turned away, uploads stop
assert.match((await upload(pat, waitingHat, png(20))).error, /turned that picture away before/);
assert.match((await upload(pat, waitingHat, png(21))).ok, /waiting/);
assert.match((await admin("/admin/api/review", {kind: "clothing", id: waitingHat.id, approve: false})).data.ok, /2 of 3/);
assert.match((await upload(pat, waitingHat, png(22))).ok, /waiting/);
assert.match((await admin("/admin/api/review", {action: "takePicture", kind: "clothing", id: waitingHat.id})).data.ok,
/taken away\. That is 3 for Pat: their picture uploads are stopped\.$/);
assert.match((await upload(pat, waitingHat, png(23))).error, /turned off picture uploads/);
assert.deepStrictEqual((await admin("/admin/api/review")).data.stopped, ["Pat"]);
assert.match((await admin("/admin/api/review", {action: "uploads", username: "Pat", blocked: false})).data.ok, /Pat can upload pictures again/);
assert.match((await upload(pat, waitingHat, png(23))).ok, /waiting/);
assert.match((await admin("/admin/api/review", {kind: "clothing", id: waitingHat.id, approve: true})).data.ok, /shown now/);
// off: nothing is made
assert.strictEqual((await admin("/admin/api/item-switches", {enabled: false, approval: false})).status, 200);
assert.match((await maker("make", "clothing", base.id, "No Hat")).said, /turned off/);
assert.strictEqual((await admin("/admin/api/item-switches", {enabled: true})).status, 200);
assert.match((await maker("delete", "clothing", waitingHat.id)).said, /^Deleted /);
// written down under the maker's name
const log = (await admin("/admin/api/moderators")).data.log.map(entry => `${entry.by} ${entry.action} ${entry.detail}`);
for (const line of ["Dev item made clothing 50000 Test Crown", "Dev item made den 50000 Test Chair", "Dev item changed clothing 50000 Gold Crown",
"Dev item made (in game) clothing 50001 Quick Hat", "Dev item locked clothing 50001", "Dev item taken (in game) clothing 50001 x2",
"Dev item taken den 50000 x1", "Dev item deleted clothing 50002"]) {
assert.ok(log.includes(line), `missing from the log: ${line}\n${log.join("\n")}`);
}
made = {base};
for (const {client} of [pat, devGame, hal]) {
client.socket.destroy();
}
};
// the same database, a new server: everything is still there.
const second = async () => {
const atLogin = {};
const lou = await arrive("Lou", people.Lou, null, async client => {
atLogin.clothing = await untilDefs(client, "clothing");
atLogin.den = await untilDefs(client, "den");
});
assert.deepStrictEqual(atLogin.clothing.map(entry => `${entry.id} ${entry.name}`), ["50000 Gold Crown", "50001 Quick Hat"]);
assert.deepStrictEqual(atLogin.den.map(entry => entry.name), ["Test Chair"]);
assert.strictEqual(byId(await pack(1000), 50001).layerDefId, made.base.layerDefId);
assert.strictEqual((await strings())[9100001], "Quick Hat");
const shop = await shopList(lou.client, lou.roomId, ODD_CLOTHING_SHOP);
assert.ok(shop.includes("50001") && !shop.includes("50000"));
lou.client.socket.destroy();
};
(async () => {
const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "aj-test-"));
try {
await harness.withServer("items test", HTTP_PORT, GAME_PORT, first, {dataDir});
await harness.withServer("items test (after a restart)", HTTP_PORT, GAME_PORT, second, {dataDir});
}
finally {
fs.rmSync(dataDir, {recursive: true, force: true, maxRetries: 10, retryDelay: 200});
}
})().catch(err => {
console.error(err);
process.exit(1);
});