perf(test): reduce sync passes in memory batch failure test

This commit is contained in:
Peter Steinberger
2026-02-14 22:08:29 +00:00
parent 97b566b8b3
commit a0ff9d9bbb

View File

@@ -265,29 +265,25 @@ describe("memory indexing with OpenAI batches", () => {
it("tracks batch failures, resets on success, and disables after repeated failures", async () => { it("tracks batch failures, resets on success, and disables after repeated failures", async () => {
const restoreTimeouts = useFastShortTimeouts(); const restoreTimeouts = useFastShortTimeouts();
const content = ["flaky", "batch"].join("\n\n");
const memoryFile = path.join(memoryDir, "2026-01-09.md"); const memoryFile = path.join(memoryDir, "2026-01-09.md");
await fs.writeFile(memoryFile, ["flaky", "batch"].join("\n\n"));
let mtimeMs = Date.now(); let mtimeMs = Date.now();
const touch = async () => { const touch = async () => {
mtimeMs += 1_000; mtimeMs += 1_000;
const date = new Date(mtimeMs); const date = new Date(mtimeMs);
await fs.utimes(memoryFile, date, date); await fs.utimes(memoryFile, date, date);
}; };
await fs.writeFile(memoryFile, content);
await touch(); await touch();
let mode: "fail" | "ok" = "fail"; let mode: "fail" | "ok" = "fail";
const { fetchMock } = createOpenAIBatchFetchMock({ const { fetchMock } = createOpenAIBatchFetchMock({
onCreateBatch: () => { onCreateBatch: () =>
if (mode === "fail") { mode === "fail"
return new Response("batch failed", { status: 400 }); ? new Response("batch failed", { status: 400 })
} : new Response(JSON.stringify({ id: "batch_1", status: "in_progress" }), {
return new Response(JSON.stringify({ id: "batch_1", status: "in_progress" }), { status: 200,
status: 200, headers: { "Content-Type": "application/json" },
headers: { "Content-Type": "application/json" }, }),
});
},
}); });
vi.stubGlobal("fetch", fetchMock); vi.stubGlobal("fetch", fetchMock);
@@ -304,17 +300,12 @@ describe("memory indexing with OpenAI batches", () => {
expect(status.batch?.enabled).toBe(true); expect(status.batch?.enabled).toBe(true);
expect(status.batch?.failures).toBe(1); expect(status.batch?.failures).toBe(1);
const markDirty = () => {
// `sync` only indexes when marked dirty (unless doing a full reindex).
(manager as unknown as { dirty: boolean }).dirty = true;
};
// Success should reset failure count. // Success should reset failure count.
embedBatch.mockClear(); embedBatch.mockClear();
mode = "ok"; mode = "ok";
await fs.writeFile(memoryFile, ["flaky", "batch", "recovery"].join("\n\n")); await fs.writeFile(memoryFile, ["flaky", "batch", "recovery"].join("\n\n"));
await touch(); await touch();
markDirty(); (manager as unknown as { dirty: boolean }).dirty = true;
await manager.sync({ reason: "test" }); await manager.sync({ reason: "test" });
status = manager.status(); status = manager.status();
expect(status.batch?.enabled).toBe(true); expect(status.batch?.enabled).toBe(true);
@@ -322,19 +313,26 @@ describe("memory indexing with OpenAI batches", () => {
expect(embedBatch).not.toHaveBeenCalled(); expect(embedBatch).not.toHaveBeenCalled();
// Two more failures after reset should disable remote batching. // Two more failures after reset should disable remote batching.
mode = "fail"; await (
await fs.writeFile(memoryFile, ["flaky", "batch", "fail-a"].join("\n\n")); manager as unknown as {
await touch(); recordBatchFailure: (params: {
markDirty(); provider: string;
await manager.sync({ reason: "test" }); message: string;
status = manager.status(); attempts?: number;
expect(status.batch?.enabled).toBe(true); forceDisable?: boolean;
expect(status.batch?.failures).toBe(1); }) => Promise<unknown>;
}
await fs.writeFile(memoryFile, ["flaky", "batch", "fail-b"].join("\n\n")); ).recordBatchFailure({ provider: "openai", message: "batch failed", attempts: 1 });
await touch(); await (
markDirty(); manager as unknown as {
await manager.sync({ reason: "test" }); recordBatchFailure: (params: {
provider: string;
message: string;
attempts?: number;
forceDisable?: boolean;
}) => Promise<unknown>;
}
).recordBatchFailure({ provider: "openai", message: "batch failed", attempts: 1 });
status = manager.status(); status = manager.status();
expect(status.batch?.enabled).toBe(false); expect(status.batch?.enabled).toBe(false);
expect(status.batch?.failures).toBeGreaterThanOrEqual(2); expect(status.batch?.failures).toBeGreaterThanOrEqual(2);
@@ -344,7 +342,7 @@ describe("memory indexing with OpenAI batches", () => {
embedBatch.mockClear(); embedBatch.mockClear();
await fs.writeFile(memoryFile, ["flaky", "batch", "fallback"].join("\n\n")); await fs.writeFile(memoryFile, ["flaky", "batch", "fallback"].join("\n\n"));
await touch(); await touch();
markDirty(); (manager as unknown as { dirty: boolean }).dirty = true;
await manager.sync({ reason: "test" }); await manager.sync({ reason: "test" });
expect(fetchMock.mock.calls.length).toBe(fetchCalls); expect(fetchMock.mock.calls.length).toBe(fetchCalls);
expect(embedBatch).toHaveBeenCalled(); expect(embedBatch).toHaveBeenCalled();