Skip to content

Commit c29f34d

Browse files
authored
test: simplify runner fixtures and cover in-flight mock registration (#133696)
1 parent a09c7d0 commit c29f34d

3 files changed

Lines changed: 352 additions & 438 deletions

File tree

‎test/non-isolated-runner.resolve-mocks.test.ts‎

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@
33
// invalidated) twice, while every caller's pass starts at or after its call so
44
// previously queued ids are registered before the caller's fetch proceeds.
55
import { describe, expect, it } from "vitest";
6+
import { createDeferred } from "./helpers/promise.js";
67
import { drainMockerResolveMocks, serializeMockerResolveMocks } from "./non-isolated-runner.js";
78

8-
function deferred(): { promise: Promise<void>; resolve: () => void } {
9-
let resolve!: () => void;
10-
const promise = new Promise<void>((settle) => {
11-
resolve = settle;
12-
});
13-
return { promise, resolve };
14-
}
15-
169
// Mirrors BareModuleMocker.resolveMocks: snapshots the static queue's contents
1710
// at pass start, awaits its RPCs, then reassigns the static to [] so ids
1811
// pushed during the await land in the abandoned array.
@@ -34,14 +27,8 @@ class FakeMocker {
3427
this.maxConcurrentPasses = Math.max(this.maxConcurrentPasses, this.active);
3528
this.passes += 1;
3629
const snapshot = [...FakeMocker.pendingIds];
37-
if (this.passes === 1 && this.beforeFirstPass) {
38-
await this.beforeFirstPass();
39-
} else {
40-
// Simulate the parallel resolveId RPC round-trips inside one pass.
41-
await new Promise((resolve) => {
42-
setTimeout(resolve, 1);
43-
});
44-
}
30+
// Every pass must suspend like the awaited RPCs, even without a gate.
31+
await (this.passes === 1 ? this.beforeFirstPass?.() : undefined);
4532
const passError = this.nextPassError;
4633
this.nextPassError = undefined;
4734
if (passError) {
@@ -63,6 +50,16 @@ class FakeMocker {
6350
}
6451
}
6552

53+
function pauseFirstPass(mocker: FakeMocker): { started: Promise<void>; release: () => void } {
54+
const started = createDeferred();
55+
const gate = createDeferred();
56+
mocker.beforeFirstPass = () => {
57+
started.resolve();
58+
return gate.promise;
59+
};
60+
return { started: started.promise, release: gate.resolve };
61+
}
62+
6663
describe("serializeMockerResolveMocks", () => {
6764
it("serializes concurrent callers and never re-registers a drained snapshot", async () => {
6865
FakeMocker.pendingIds = ["mock-a", "mock-b"];
@@ -82,37 +79,36 @@ describe("serializeMockerResolveMocks", () => {
8279
it("registers ids queued while a pass is in flight before the later caller resolves", async () => {
8380
FakeMocker.pendingIds = ["mock-a"];
8481
const mocker = new FakeMocker();
82+
const { started, release } = pauseFirstPass(mocker);
8583
serializeMockerResolveMocks(mocker);
8684

8785
const first = mocker.resolveMocks();
86+
await started;
8887
// Upstream would abandon this push when it reassigns pendingIds to [];
8988
// the wrapper must requeue it and the second caller's own chained pass
9089
// must register it before that caller proceeds with its fetch.
9190
FakeMocker.pendingIds.push("mock-late");
9291
const second = mocker.resolveMocks();
92+
release();
9393
await second;
9494

9595
expect(mocker.processed).toEqual(["mock-a", "mock-late"]);
96+
expect(mocker.passes).toBe(2);
9697
expect(mocker.maxConcurrentPasses).toBe(1);
9798
await first;
9899
expect(FakeMocker.pendingIds).toEqual([]);
99100
});
100101

101102
it("drains ids queued during the final pass without requiring another caller", async () => {
102103
FakeMocker.pendingIds = ["mock-a"];
103-
const firstPassStarted = deferred();
104-
const releaseFirstPass = deferred();
105104
const mocker = new FakeMocker();
106-
mocker.beforeFirstPass = async () => {
107-
firstPassStarted.resolve();
108-
await releaseFirstPass.promise;
109-
};
105+
const { started, release } = pauseFirstPass(mocker);
110106
serializeMockerResolveMocks(mocker);
111107

112108
const first = mocker.resolveMocks();
113-
await firstPassStarted.promise;
109+
await started;
114110
FakeMocker.pendingIds.push("mock-late");
115-
releaseFirstPass.resolve();
111+
release();
116112
await first;
117113

118114
expect(mocker.processed).toEqual(["mock-a", "mock-late"]);
@@ -149,31 +145,30 @@ describe("serializeMockerResolveMocks", () => {
149145

150146
it("drains every queued pass before cleanup resets the mocker", async () => {
151147
FakeMocker.pendingIds = ["mock-a"];
152-
const firstPassStarted = deferred();
153-
const releaseFirstPass = deferred();
154148
const mocker = new FakeMocker();
155-
mocker.beforeFirstPass = async () => {
156-
firstPassStarted.resolve();
157-
await releaseFirstPass.promise;
158-
};
149+
const { started, release } = pauseFirstPass(mocker);
159150
serializeMockerResolveMocks(mocker);
160151

161152
const first = mocker.resolveMocks();
162-
await firstPassStarted.promise;
153+
await started;
163154
const drain = drainMockerResolveMocks(mocker);
164-
const drainState = Promise.race([drain.then(() => "settled"), Promise.resolve("pending")]);
165-
166-
expect(await drainState).toBe("pending");
167-
FakeMocker.pendingIds.push("mock-late");
168-
const second = mocker.resolveMocks();
169-
releaseFirstPass.resolve();
170-
await drain;
171-
mocker.reset();
172-
173-
expect(mocker.resetObservations).toEqual([
174-
{ active: 0, pendingIds: [], processed: ["mock-a", "mock-late"] },
175-
]);
176-
await Promise.all([first, second]);
155+
try {
156+
const drainState = Promise.race([drain.then(() => "settled"), Promise.resolve("pending")]);
157+
expect(await drainState).toBe("pending");
158+
FakeMocker.pendingIds.push("mock-late");
159+
const second = mocker.resolveMocks();
160+
release();
161+
await drain;
162+
mocker.reset();
163+
164+
expect(mocker.resetObservations).toEqual([
165+
{ active: 0, pendingIds: [], processed: ["mock-a", "mock-late"] },
166+
]);
167+
await Promise.all([first, second]);
168+
} finally {
169+
release();
170+
await drainMockerResolveMocks(mocker);
171+
}
177172
});
178173

179174
it("keeps the internal drain usable after a caller-visible rejection", async () => {

‎test/non-isolated-runner.test-api-fixtures.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe("${generation} test API consumers", () => {
9393
changes: [],
9494
});
9595
}
96-
it.each(["first test", "second test"])("keeps all three consumers usable in %s", async (phase) => {
96+
it.each(["first test", "second test"])("keeps test API consumers usable in %s", async (phase) => {
9797
await verifyConsumers(phase);
9898
});
9999
afterAll(async () => {
@@ -152,6 +152,8 @@ vi.mock(${sourcePath("cron/service/active-run-cancellation.ts")}, async () => ({
152152
...await vi.importActual(${sourcePath("cron/service/active-run-cancellation.ts")}),
153153
}));
154154
const nativeCron = await import(${sourcePath("cron/service/active-run-cancellation.ts")});
155+
// The second import replaces native metadata with a manual-mock placeholder;
156+
// the execution record must still identify the retained native generation.
155157
await import(${sourcePath("cron/service/active-run-cancellation.ts")});
156158
const native = createRequire(import.meta.url)("./native-cron.cjs");
157159
const workspace = await import(${sourcePath("agents/workspace-legacy-state.ts")});

0 commit comments

Comments
 (0)