diff --git a/tools/trap-sandbox/test-integration/duplicate.test.ts b/tools/trap-sandbox/test-integration/duplicate.test.ts index fcc17c6..5a22667 100644 --- a/tools/trap-sandbox/test-integration/duplicate.test.ts +++ b/tools/trap-sandbox/test-integration/duplicate.test.ts @@ -10,22 +10,28 @@ test("duplicate traposId is resolved newest-wins", async () => { const sandbox = await startSandbox(); const TRAPOS_ID = "9:dup"; + // Newest-wins is decided by the order hello frames reach the gateway, which is + // independent of the client-side READY print order. Spawn the two computers + // sequentially — only start `second` once `first` is registered server-side — + // so `second` is unambiguously the newer registration. const first = startCraftos("gateway-client.lua", { computerId: 9, computerLabel: "dup", shellArgs: [sandbox.baseUrl, "-", "idle", "60"], }); - const second = startCraftos("gateway-client.lua", { - computerId: 9, - computerLabel: "dup", - shellArgs: [sandbox.baseUrl, "-", "idle"], - }); + let second: ReturnType | undefined; try { await waitFor(() => first.snapshot().includes("__SANDBOX_READY__"), 12_000, "first connected"); - assert.ok(sandbox.registry.has("local", TRAPOS_ID), "first should be registered"); + await waitFor(() => sandbox.registry.has("local", TRAPOS_ID), 2_000, "first registered server-side"); - await waitFor(() => second.snapshot().includes("__SANDBOX_READY__"), 12_000, "second connected"); + const secondHandle = startCraftos("gateway-client.lua", { + computerId: 9, + computerLabel: "dup", + shellArgs: [sandbox.baseUrl, "-", "idle"], + }); + second = secondHandle; + await waitFor(() => secondHandle.snapshot().includes("__SANDBOX_READY__"), 12_000, "second connected"); // Newest-wins: still exactly one registration for the shared id. assert.equal(sandbox.registry.count(), 1, "exactly one registration after the duplicate"); assert.ok(sandbox.registry.has("local", TRAPOS_ID)); @@ -38,14 +44,14 @@ test("duplicate traposId is resolved newest-wins", async () => { throw new Error( formatFailure( error instanceof Error ? error.message : String(error), - `first:\n${first.snapshot()}\nsecond:\n${second.snapshot()}`, + `first:\n${first.snapshot()}\nsecond:\n${second?.snapshot() ?? "(not started)"}`, ), { cause: error }, ); } finally { first.abort(); - second.abort(); - await Promise.all([first.done.catch(() => undefined), second.done.catch(() => undefined)]); + second?.abort(); + await Promise.all([first.done.catch(() => undefined), second?.done.catch(() => undefined)]); await sandbox.close(); } });