Add multiplayer rooms and Vercel analytics
This commit is contained in:
283
scripts/test-multiplayer-sql.mjs
Normal file
283
scripts/test-multiplayer-sql.mjs
Normal file
@@ -0,0 +1,283 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { readFile, readdir } from "node:fs/promises";
|
||||
import { PGlite } from "../.data/sql-verification/node_modules/@electric-sql/pglite/dist/index.js";
|
||||
|
||||
// Isolated PostgreSQL: fixtures never enter production rooms or leaderboards.
|
||||
const db = new PGlite();
|
||||
try {
|
||||
await db.exec(`create role anon; create role authenticated; create role service_role;
|
||||
create schema auth; create table auth.users(id uuid primary key);
|
||||
create function auth.uid() returns uuid language sql as $$ select null::uuid $$;
|
||||
create schema storage; create table storage.buckets(id text primary key,name text,public boolean,file_size_limit bigint);`);
|
||||
for (const file of (await readdir("supabase/migrations"))
|
||||
.filter((file) => file.endsWith(".sql"))
|
||||
.sort())
|
||||
await db.exec(await readFile(`supabase/migrations/${file}`, "utf8"));
|
||||
const account = (
|
||||
await db.query(
|
||||
"insert into auth.users values(gen_random_uuid()) returning id",
|
||||
)
|
||||
).rows[0].id;
|
||||
const dataset = (
|
||||
await db.query(
|
||||
`insert into public.datasets(owner_id,name,source,bounds,state,panorama_count)
|
||||
values($1,'Multiplayer fixture','plugin','{"minX":0,"minZ":0,"maxX":64,"maxZ":64}','published',5) returning id`,
|
||||
[account],
|
||||
)
|
||||
).rows[0].id;
|
||||
const manifest = {
|
||||
name: "Multiplayer fixture",
|
||||
bounds: { minX: 0, minZ: 0, maxX: 64, maxZ: 64 },
|
||||
viewpoints: Array.from({ length: 5 }, (_, i) => ({
|
||||
id: `point${i}`,
|
||||
x: 10 + i,
|
||||
z: 10 + i,
|
||||
y: 64,
|
||||
})),
|
||||
};
|
||||
const revision = (
|
||||
await db.query(
|
||||
"insert into public.revisions(dataset_id,asset_root,manifest) values($1,'fixture',$2) returning id",
|
||||
[dataset, JSON.stringify(manifest)],
|
||||
)
|
||||
).rows[0].id;
|
||||
await db.query("update public.datasets set revision_id=$1 where id=$2", [
|
||||
revision,
|
||||
dataset,
|
||||
]);
|
||||
const secret = (n) => String(n).padStart(64, "0");
|
||||
async function create(cap = 2) {
|
||||
return (
|
||||
await db.query(
|
||||
"select public.create_multiplayer_room($1,'hard','Host',$2,$3) as id",
|
||||
[dataset, cap, secret(1)],
|
||||
)
|
||||
).rows[0].id;
|
||||
}
|
||||
async function act(room, who, type = "view", payload = {}) {
|
||||
return (
|
||||
await db.query("select public.multiplayer_action($1,$2,$3,$4) as room", [
|
||||
room,
|
||||
secret(who),
|
||||
type,
|
||||
JSON.stringify(payload),
|
||||
])
|
||||
).rows[0].room;
|
||||
}
|
||||
for (const cap of [1, 17, null])
|
||||
await assert.rejects(create(cap), /Choose between 2 and 16 players/);
|
||||
const room = await create();
|
||||
let view = await act(room, 1);
|
||||
const host = view.you;
|
||||
assert.equal(view.playerLimit, 2);
|
||||
assert.equal(view.players.length, 1);
|
||||
assert.equal(view.rounds.length, 5);
|
||||
assert.ok(view.rounds.every((turn) => turn.results.length === 0));
|
||||
const outsider = await act(room, 3);
|
||||
assert.equal(outsider.you, null);
|
||||
assert.deepEqual(outsider.players, []);
|
||||
assert.deepEqual(outsider.rounds, []);
|
||||
assert.ok(!JSON.stringify(view).includes('"viewpoint"'));
|
||||
assert.ok(!JSON.stringify(view).includes("access_hash"));
|
||||
await assert.rejects(act(room, 1, "start"), /Invite at least one friend/);
|
||||
await assert.rejects(act(room, 3, "start"), /membership permission/);
|
||||
const joined = await act(room, 2, "join", { name: "Friend" });
|
||||
const friend = joined.you;
|
||||
assert.equal(
|
||||
(await act(room, 2, "join", { name: "Changed name" })).you,
|
||||
friend,
|
||||
);
|
||||
await assert.rejects(act(room, 3, "join", { name: "Extra" }), /room is full/);
|
||||
await assert.rejects(act(room, 2, "start"), /owner permission/);
|
||||
view = await act(room, 1, "start");
|
||||
assert.equal(view.status, "loading");
|
||||
await assert.rejects(
|
||||
act(room, 3, "join", { name: "Late" }),
|
||||
/already started/,
|
||||
);
|
||||
await assert.rejects(
|
||||
act(room, 1, "guess", { round: 0, x: 10, z: 10 }),
|
||||
/Round is not active/,
|
||||
);
|
||||
assert.equal((await act(room, 1, "ready", { round: 0 })).status, "loading");
|
||||
view = await act(room, 2, "ready", { round: 0 });
|
||||
assert.equal(view.status, "playing");
|
||||
const deadline = view.rounds[0].deadline;
|
||||
assert.equal(
|
||||
(await act(room, 1, "ready", { round: 0 })).rounds[0].deadline,
|
||||
deadline,
|
||||
);
|
||||
await assert.rejects(
|
||||
act(room, 1, "guess", { round: 0, x: 65, z: 0 }),
|
||||
/Guess outside map/,
|
||||
);
|
||||
const location = (
|
||||
await db.query(
|
||||
"select viewpoint from public.multiplayer_rounds where room_id=$1 and round_number=0",
|
||||
[room],
|
||||
)
|
||||
).rows[0].viewpoint;
|
||||
view = await act(room, 1, "guess", {
|
||||
round: 0,
|
||||
x: location.x,
|
||||
z: location.z,
|
||||
});
|
||||
assert.equal(view.status, "playing");
|
||||
assert.deepEqual(view.rounds[0].results, []);
|
||||
assert.ok(
|
||||
view.players.every((player) => player.total === 0),
|
||||
"scores concealed until everyone finishes",
|
||||
);
|
||||
assert.deepEqual(view.myGuess, { x: location.x, z: location.z });
|
||||
await act(room, 1, "guess", { round: 0, x: 0, z: 0 });
|
||||
assert.equal(
|
||||
(
|
||||
await db.query(
|
||||
"select count(*)::integer as count from public.multiplayer_guesses where room_id=$1 and player_id=$2",
|
||||
[room, host],
|
||||
)
|
||||
).rows[0].count,
|
||||
1,
|
||||
);
|
||||
view = await act(room, 2, "guess", { round: 0, x: 0, z: 0 });
|
||||
assert.equal(view.status, "results");
|
||||
assert.equal(
|
||||
view.rounds[0].results.find((result) => result.playerId === host).score,
|
||||
5000,
|
||||
);
|
||||
assert.ok(
|
||||
view.rounds[0].results.find((result) => result.playerId === friend).score <
|
||||
5000,
|
||||
);
|
||||
await assert.rejects(act(room, 2, "next", { round: 0 }), /owner permission/);
|
||||
assert.equal(
|
||||
(await act(room, 1, "guess", { round: 0, x: 0, z: 0 })).status,
|
||||
"results",
|
||||
"retry cannot alter a submitted guess",
|
||||
);
|
||||
for (let round = 1; round < 5; round++) {
|
||||
await act(room, 1, "next", { round: round - 1 });
|
||||
await act(room, 1, "ready", { round });
|
||||
await act(room, 2, "ready", { round });
|
||||
const point = (
|
||||
await db.query(
|
||||
"select viewpoint from public.multiplayer_rounds where room_id=$1 and round_number=$2",
|
||||
[room, round],
|
||||
)
|
||||
).rows[0].viewpoint;
|
||||
await act(room, 1, "guess", { round, x: point.x, z: point.z });
|
||||
if (round === 2) {
|
||||
await db.query(
|
||||
"update public.multiplayer_rounds set deadline=clock_timestamp()-interval '1 second' where room_id=$1 and round_number=$2",
|
||||
[room, round],
|
||||
);
|
||||
view = await act(room, 2, "guess", { round, x: point.x, z: point.z });
|
||||
assert.equal(
|
||||
view.rounds[round].results.find((result) => result.playerId === friend)
|
||||
.score,
|
||||
0,
|
||||
);
|
||||
assert.equal(
|
||||
view.rounds[round].results.find((result) => result.playerId === friend)
|
||||
.timedOut,
|
||||
true,
|
||||
);
|
||||
} else view = await act(room, 2, "guess", { round, x: 0, z: 0 });
|
||||
}
|
||||
assert.equal(view.status, "finished");
|
||||
assert.equal(view.players[0].id, host);
|
||||
assert.equal(view.players[0].total, 25000);
|
||||
assert.equal(view.players[0].rank, 1);
|
||||
assert.equal(view.players[1].rank, 2);
|
||||
assert.equal(view.rounds.flatMap((turn) => turn.results).length, 10);
|
||||
|
||||
const capacity = await create(16);
|
||||
for (let n = 2; n <= 16; n++)
|
||||
await act(capacity, n, "join", { name: `Player ${n}` });
|
||||
await assert.rejects(
|
||||
act(capacity, 17, "join", { name: "Overflow" }),
|
||||
/room is full/,
|
||||
);
|
||||
assert.equal((await act(capacity, 1)).playerCount, 16);
|
||||
const removed = (await act(capacity, 16)).you;
|
||||
await act(capacity, 1, "kick", { playerId: removed });
|
||||
assert.equal((await act(capacity, 16)).you, null);
|
||||
await act(capacity, 17, "join", { name: "Replacement" });
|
||||
await act(capacity, 1, "leave");
|
||||
assert.equal(
|
||||
(await act(capacity, 2)).hostId,
|
||||
(await act(capacity, 2)).you,
|
||||
"ownership transfers when host leaves",
|
||||
);
|
||||
await assert.rejects(
|
||||
act(capacity, 18, "join", { name: "player 2" }),
|
||||
/name is already taken/,
|
||||
);
|
||||
|
||||
const stalled = await create();
|
||||
await act(stalled, 2, "join", { name: "Offline" });
|
||||
await act(stalled, 1, "start");
|
||||
await act(stalled, 1, "ready", { round: 0 });
|
||||
await db.query(
|
||||
"update public.multiplayer_rooms set transition_at=clock_timestamp()-interval '1 second' where id=$1",
|
||||
[stalled],
|
||||
);
|
||||
assert.equal(
|
||||
(await act(stalled, 1)).status,
|
||||
"playing",
|
||||
"offline loader cannot block the match",
|
||||
);
|
||||
await db.query(
|
||||
"update public.multiplayer_rounds set deadline=clock_timestamp()-interval '1 second' where room_id=$1 and round_number=0",
|
||||
[stalled],
|
||||
);
|
||||
view = await act(stalled, 1);
|
||||
assert.equal(view.status, "results");
|
||||
assert.ok(
|
||||
view.players.every((player) => player.rank === 1),
|
||||
"equal totals share a rank",
|
||||
);
|
||||
await db.query(
|
||||
"update public.multiplayer_rooms set transition_at=clock_timestamp()-interval '1 second' where id=$1",
|
||||
[stalled],
|
||||
);
|
||||
assert.equal(
|
||||
(await act(stalled, 1)).round,
|
||||
1,
|
||||
"rounds progress without host clicks",
|
||||
);
|
||||
await act(stalled, 2, "leave");
|
||||
await act(stalled, 1, "ready", { round: 1 });
|
||||
const point = (
|
||||
await db.query(
|
||||
"select viewpoint from public.multiplayer_rounds where room_id=$1 and round_number=1",
|
||||
[stalled],
|
||||
)
|
||||
).rows[0].viewpoint;
|
||||
view = await act(stalled, 1, "guess", { round: 1, x: point.x, z: point.z });
|
||||
assert.equal(view.status, "results", "departed players cannot block scoring");
|
||||
await act(stalled, 1, "close");
|
||||
assert.equal((await act(stalled, 1)).status, "closed");
|
||||
|
||||
for (const role of ["anon", "authenticated"]) {
|
||||
await db.exec(`set role ${role}`);
|
||||
for (const table of [
|
||||
"multiplayer_rooms",
|
||||
"multiplayer_players",
|
||||
"multiplayer_rounds",
|
||||
"multiplayer_guesses",
|
||||
])
|
||||
await assert.rejects(
|
||||
db.query(`select * from public.${table}`),
|
||||
/permission denied/,
|
||||
);
|
||||
await assert.rejects(act(room, 1), /permission denied/);
|
||||
await assert.rejects(create(), /permission denied/);
|
||||
await db.exec("reset role");
|
||||
}
|
||||
console.log(
|
||||
"Multiplayer SQL checks passed: 16-player cap, owner permissions, private answers, synchronized rounds, deadlines, retries, ties, departure, and client-role isolation.",
|
||||
);
|
||||
} finally {
|
||||
await db.close();
|
||||
}
|
||||
Reference in New Issue
Block a user