mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-01 18:07:00 +01:00
test: reserve Codex quorum fixture ports before credential assertions
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
22837484f4
commit
c0f1a327df
1 file changed
+33
-22
@@ -189,7 +189,7 @@ describe("managed Codex credentials", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it("prepares a fresh silent-primary fixture without taking over a foreign listener", async () => {
|
||||
it.each([[[0]], [[0, 1]]] as const)("prepares a fresh quorum fixture (%j) without taking over a foreign listener", async (occupiedIndices) => {
|
||||
const foreignFixture = await silentPrimaryQuorumFixture();
|
||||
const collision = foreignFixture.fixture;
|
||||
const collisionPort = credentialLeasePorts(collision.home)[0];
|
||||
@@ -199,9 +199,9 @@ describe("managed Codex credentials", () => {
|
||||
.mockResolvedValueOnce(collision)
|
||||
.mockImplementation(credentialFixture);
|
||||
let prepared:
|
||||
Awaited<ReturnType<typeof silentPrimaryQuorumFixture>> | undefined;
|
||||
Awaited<ReturnType<typeof occupiedQuorumFixture>> | undefined;
|
||||
try {
|
||||
prepared = await silentPrimaryQuorumFixture(nextFixture);
|
||||
prepared = await occupiedQuorumFixture(nextFixture, occupiedIndices);
|
||||
expect(nextFixture.mock.calls.length).toBeGreaterThanOrEqual(2);
|
||||
expect(nextFixture.mock.calls.length).toBeLessThanOrEqual(8);
|
||||
expect(prepared.fixture.home).not.toBe(collision.home);
|
||||
@@ -212,28 +212,30 @@ describe("managed Codex credentials", () => {
|
||||
readFile(join(collision.home, "auth.json")),
|
||||
).rejects.toMatchObject({ code: "ENOENT" });
|
||||
} finally {
|
||||
await prepared?.occupied.close();
|
||||
await Promise.all(prepared?.occupied.map((listener) => listener.close()) ?? []);
|
||||
await foreign.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("bounds silent-primary preparation and releases only its own partial reservations", async () => {
|
||||
const foreignFixture = await silentPrimaryQuorumFixture(
|
||||
it.each([[[0]], [[0, 1]]] as const)("bounds quorum preparation (%j) and releases only its own partial reservations", async (occupiedIndices) => {
|
||||
const foreignFixture = await occupiedQuorumFixture(
|
||||
credentialFixture,
|
||||
1,
|
||||
[2],
|
||||
);
|
||||
const collision = foreignFixture.fixture;
|
||||
const ports = credentialLeasePorts(collision.home);
|
||||
const foreign = foreignFixture.occupied;
|
||||
const foreign = foreignFixture.occupied[0]!;
|
||||
const nextFixture = vi.fn(async () => collision);
|
||||
let releasedPrimary: Awaited<ReturnType<typeof listenSilently>> | undefined;
|
||||
let releasedSecond: Awaited<ReturnType<typeof listenSilently>> | undefined;
|
||||
try {
|
||||
await expect(
|
||||
silentPrimaryQuorumFixture(nextFixture),
|
||||
occupiedQuorumFixture(nextFixture, occupiedIndices),
|
||||
).rejects.toMatchObject({ code: "EADDRINUSE" });
|
||||
expect(nextFixture).toHaveBeenCalledTimes(8);
|
||||
releasedPrimary = await listenSilently(ports[0]);
|
||||
await expect(listenSilently(ports[1])).rejects.toMatchObject({
|
||||
releasedSecond = await listenSilently(ports[1]);
|
||||
await expect(listenSilently(ports[2])).rejects.toMatchObject({
|
||||
code: "EADDRINUSE",
|
||||
});
|
||||
await expect(
|
||||
@@ -241,18 +243,19 @@ describe("managed Codex credentials", () => {
|
||||
).rejects.toMatchObject({ code: "ENOENT" });
|
||||
} finally {
|
||||
await releasedPrimary?.close();
|
||||
await releasedSecond?.close();
|
||||
await foreign.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("fails before auth mutation when two quorum candidates are occupied", async () => {
|
||||
const fixture = await credentialFixture();
|
||||
const destination = join(fixture.home, "auth.json");
|
||||
await writeFile(destination, '{"sentinel":true}', { mode: 0o600 });
|
||||
const ports = credentialLeasePorts(await realpath(fixture.home));
|
||||
const first = await listenSilently(ports[0]);
|
||||
const second = await listenSilently(ports[1]);
|
||||
const { fixture, occupied } = await occupiedQuorumFixture(
|
||||
credentialFixture,
|
||||
[0, 1],
|
||||
);
|
||||
try {
|
||||
const destination = join(fixture.home, "auth.json");
|
||||
await writeFile(destination, '{"sentinel":true}', { mode: 0o600 });
|
||||
await expect(
|
||||
stageManagedCodexCredential({
|
||||
agentHomeDirectory: fixture.home,
|
||||
@@ -265,7 +268,7 @@ describe("managed Codex credentials", () => {
|
||||
'{"sentinel":true}',
|
||||
);
|
||||
} finally {
|
||||
await Promise.all([first.close(), second.close()]);
|
||||
await Promise.all(occupied.map((listener) => listener.close()));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1655,6 +1658,14 @@ async function credentialFixture(): Promise<{ root: string; home: string }> {
|
||||
async function silentPrimaryQuorumFixture(
|
||||
nextFixture = credentialFixture,
|
||||
occupiedIndex: 0 | 1 = 0,
|
||||
) {
|
||||
const prepared = await occupiedQuorumFixture(nextFixture, [occupiedIndex]);
|
||||
return { fixture: prepared.fixture, occupied: prepared.occupied[0]! };
|
||||
}
|
||||
|
||||
async function occupiedQuorumFixture(
|
||||
nextFixture = credentialFixture,
|
||||
occupiedIndices: readonly (0 | 1 | 2)[] = [0],
|
||||
) {
|
||||
for (let attempt = 0; attempt < 8; attempt += 1) {
|
||||
const fixture = await nextFixture();
|
||||
@@ -1674,24 +1685,24 @@ async function silentPrimaryQuorumFixture(
|
||||
continue;
|
||||
throw error;
|
||||
}
|
||||
// Only fixture preparation may retry. Release the two free candidates
|
||||
// Only fixture preparation may retry. Release the free candidates
|
||||
// immediately before the caller stages once. A foreign bind racing this
|
||||
// handoff remains a visible production-call failure, never a hidden retry.
|
||||
const released = await Promise.allSettled(
|
||||
owned
|
||||
.filter((_, index) => index !== occupiedIndex)
|
||||
.filter((_, index) => !occupiedIndices.includes(index as 0 | 1 | 2))
|
||||
.map((listener) => listener.close()),
|
||||
);
|
||||
const releaseFailure = released.find(
|
||||
(result) => result.status === "rejected",
|
||||
);
|
||||
if (releaseFailure?.status === "rejected") {
|
||||
await owned[occupiedIndex]!.close();
|
||||
await Promise.allSettled(owned.map((listener) => listener.close()));
|
||||
throw releaseFailure.reason;
|
||||
}
|
||||
return { fixture, occupied: owned[occupiedIndex]! };
|
||||
return { fixture, occupied: occupiedIndices.map((index) => owned[index]!) };
|
||||
}
|
||||
throw new Error("Silent-primary credential fixture reservation exhausted.");
|
||||
throw new Error("Credential quorum fixture reservation exhausted.");
|
||||
}
|
||||
|
||||
function credentialLeasePorts(home: string): readonly number[] {
|
||||
|
||||
Reference in new issue
Block a user