test(FN-953): fix flaky sequential processing test with fake timers

- Replace real setTimeout delays with fake timers in sequential processing test
- Eliminates race conditions and timing-dependent test failures
- Improves test reliability and reduces test execution time
This commit is contained in:
gsxdsm
2026-04-04 21:32:32 -07:00
parent 20acbf7b60
commit 2e51bd7eff

View File

@@ -3509,29 +3509,41 @@ describe("POST /github/issues/batch-import", () => {
}, 15000); // Increase timeout for multiple retries }, 15000); // Increase timeout for multiple retries
it("processes issues sequentially (not parallel)", async () => { it("processes issues sequentially (not parallel)", async () => {
const callTimes: number[] = []; vi.useFakeTimers();
fetchSpy.mockImplementation(() => {
callTimes.push(Date.now());
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve(mockGitHubIssue(callTimes.length)),
} as Response);
});
await REQUEST( try {
buildApp(), const callTimes: number[] = [];
"POST", fetchSpy.mockImplementation(() => {
"/api/github/issues/batch-import", callTimes.push(Date.now());
JSON.stringify({ owner: "owner", repo: "repo", issueNumbers: [1, 2, 3], delayMs: 50 }), return Promise.resolve({
{ "Content-Type": "application/json" } ok: true,
); status: 200,
json: () => Promise.resolve(mockGitHubIssue(callTimes.length)),
} as Response);
});
// Verify sequential processing by checking call timing // Start the request without awaiting (fake timers block real delays)
expect(callTimes).toHaveLength(3); const requestPromise = REQUEST(
// Each call should be at least 40ms after the previous (allowing for small timing variations) buildApp(),
for (let i = 1; i < callTimes.length; i++) { "POST",
expect(callTimes[i] - callTimes[i - 1]).toBeGreaterThanOrEqual(40); "/api/github/issues/batch-import",
JSON.stringify({ owner: "owner", repo: "repo", issueNumbers: [1, 2, 3], delayMs: 50 }),
{ "Content-Type": "application/json" }
);
// Advance fake time to resolve all sequential delays (3 issues × 50ms)
await vi.advanceTimersByTimeAsync(500);
await requestPromise;
// Verify sequential processing with deterministic timing
expect(callTimes).toHaveLength(3);
// With fake timers, each call should be exactly 50ms apart
for (let i = 1; i < callTimes.length; i++) {
expect(callTimes[i] - callTimes[i - 1]).toBe(50);
}
} finally {
vi.useRealTimers();
} }
}); });