Files
HealthLog/src/lib/__tests__/request-cache.test.ts
T
Marc-André Bombeck 06251d7219 fix(ai): read AI switches, modules and consent afresh inside background jobs
A background job opens one wide event for its whole run, and the per-request
memo cached the operator switches, module availability, module preferences,
provider presence and consent kinds on it. The nightly status passes loop
over every candidate inside that one event, so a switch turned off mid-run
did not stop the remaining users, and the wire re-check read the same cached
answer.

memoizePerRequest takes a freshInBackground option; every input that decides
whether AI work may run sets it, so a background caller reads them on every
call while a request still reads them once.
2026-09-24 12:08:39 +02:00

28 lines
1012 B
TypeScript

import { describe, expect, it, vi } from "vitest";
import { eventStorage } from "@/lib/logging/context";
import { WideEventBuilder } from "@/lib/logging/event-builder";
import { memoizePerRequest } from "@/lib/request-cache";
function twice(kind: "http" | "background", fresh: boolean) {
const factory = vi.fn(async () => Math.random());
return eventStorage.run(new WideEventBuilder(kind), async () => {
const options = fresh ? { freshInBackground: true } : {};
await memoizePerRequest("k", factory, options);
await memoizePerRequest("k", factory, options);
return factory.mock.calls.length;
});
}
describe("memoizePerRequest", () => {
it("reads once per request", async () => {
expect(await twice("http", false)).toBe(1);
expect(await twice("http", true)).toBe(1);
});
it("reads once per background event unless the input must stay fresh", async () => {
expect(await twice("background", false)).toBe(1);
expect(await twice("background", true)).toBe(2);
});
});