test(sandbox): stabilize duplicate registration test

This commit is contained in:
Guillaume ARM 2026-06-14 19:26:41 +02:00
parent af1bc0e666
commit 4cfceecb9a

View File

@ -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", {
let second: ReturnType<typeof startCraftos> | undefined;
try {
await waitFor(() => first.snapshot().includes("__SANDBOX_READY__"), 12_000, "first connected");
await waitFor(() => sandbox.registry.has("local", TRAPOS_ID), 2_000, "first registered server-side");
const secondHandle = startCraftos("gateway-client.lua", {
computerId: 9,
computerLabel: "dup",
shellArgs: [sandbox.baseUrl, "-", "idle"],
});
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(() => second.snapshot().includes("__SANDBOX_READY__"), 12_000, "second connected");
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();
}
});