Open History to see the last commit
1import assert from "node:assert/strict";
2import { randomUUID } from "node:crypto";
3import { pool } from "../src/server/db";
4import { bucketKeys } from "../src/shared/geometry";
5import type { Part } from "../src/shared/types";
6const base = (process.env.TEST_API_URL || process.env.VITE_API_URL!).replace(
7 /\/$/,
8 "",
9);
10async function request(token: string, path: string, body: any) {
11 const r = await fetch(`${base}/api${path}`, {
12 method: "POST",
13 headers: {
14 "Content-Type": "application/json",
15 ...(token ? { Authorization: `Bearer ${token}` } : {}),
16 },
17 body: JSON.stringify(body),
18 });
19 return { status: r.status, data: await r.json() };
20}
21const c = await pool.connect();
22try {
23 const { data: s } = await request("", "/session", {
24 name: "Lock qualification",
25 color: "#289b74",
26 }),
27 { data: w } = await request(s.token, "/worlds", {
28 title: "Lock qualification — disposable",
29 starter: "empty",
30 });
31 await request(s.token, `/worlds/${w.slug}/join`, {});
32 const p: Part = {
33 id: randomUUID(),
34 key: "brick-1x1",
35 color: "#289b74",
36 position: { x: -78, y: 0, z: -78 },
37 rotation: 0,
38 revision: 1,
39 };
40 const keys = bucketKeys([p]).map((k) => k.split(",").map(Number));
41 await c.query("BEGIN");
42 await c.query(
43 "SELECT b.x FROM spatial_buckets b JOIN unnest($2::int[],$3::int[],$4::int[]) AS k(x,y,z) USING(x,y,z) WHERE b.world=$1 ORDER BY b.x,b.y,b.z FOR UPDATE OF b",
44 [w.id, keys.map((k) => k[0]), keys.map((k) => k[1]), keys.map((k) => k[2])],
45 );
46 const independent = {
47 operationId: randomUUID(),
48 generation: 1,
49 action: "edit",
50 changes: [{ ...p, id: randomUUID(), position: { x: 72, y: 0, z: 72 } }],
51 };
52 const start = performance.now(),
53 disjoint = await request(s.token, `/worlds/${w.id}/edit`, independent);
54 assert.equal(disjoint.status, 200);
55 assert.equal(disjoint.data.accepted, true);
56 assert.ok(performance.now() - start < 2500);
57 const overlapping = {
58 operationId: randomUUID(),
59 generation: 1,
60 action: "edit",
61 changes: [p],
62 },
63 blocked = await request(s.token, `/worlds/${w.id}/edit`, overlapping);
64 assert.equal(blocked.status, 503);
65 await c.query("ROLLBACK");
66 const retried = await request(s.token, `/worlds/${w.id}/edit`, overlapping);
67 assert.equal(retried.data.accepted, true);
68 console.log(
69 "PASS a held spatial neighbourhood allows a disjoint edit to commit, blocks an intersecting edit, and accepts the identical operation after release",
70 );
71} finally {
72 await c.query("ROLLBACK").catch(() => {});
73 c.release();
74 await pool.end();
75}