|
| 1 | +// Subagent registry state tests cover hot read caching over the persisted SQLite snapshot. |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { |
| 4 | + clearSubagentRunsReadCacheForTest, |
| 5 | + getSubagentRunsSnapshotForRead, |
| 6 | + persistSubagentRunsToDisk, |
| 7 | +} from "./subagent-registry-state.js"; |
| 8 | +import type { SubagentRunRecord } from "./subagent-registry.types.js"; |
| 9 | + |
| 10 | +const mocks = vi.hoisted(() => ({ |
| 11 | + loadSubagentRegistryFromSqlite: vi.fn<() => Map<string, SubagentRunRecord>>(), |
| 12 | + saveSubagentRegistryToSqlite: vi.fn<(runs: Map<string, SubagentRunRecord>) => void>(), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("./subagent-registry.store.sqlite.js", () => ({ |
| 16 | + loadSubagentRegistryFromSqlite: mocks.loadSubagentRegistryFromSqlite, |
| 17 | + saveSubagentRegistryToSqlite: mocks.saveSubagentRegistryToSqlite, |
| 18 | +})); |
| 19 | + |
| 20 | +function createRun(runId: string): SubagentRunRecord { |
| 21 | + return { |
| 22 | + runId, |
| 23 | + childSessionKey: `agent:main:subagent:${runId}`, |
| 24 | + requesterSessionKey: "agent:main:main", |
| 25 | + requesterDisplayKey: "main", |
| 26 | + task: `task ${runId}`, |
| 27 | + cleanup: "keep", |
| 28 | + createdAt: 1, |
| 29 | + startedAt: 1, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +describe("subagent registry state read cache", () => { |
| 34 | + const previousReadDiskFlag = process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_DISK; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + vi.useFakeTimers(); |
| 38 | + vi.setSystemTime(1_000); |
| 39 | + process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_DISK = "1"; |
| 40 | + clearSubagentRunsReadCacheForTest(); |
| 41 | + mocks.loadSubagentRegistryFromSqlite.mockReset(); |
| 42 | + mocks.saveSubagentRegistryToSqlite.mockReset(); |
| 43 | + }); |
| 44 | + |
| 45 | + afterEach(() => { |
| 46 | + clearSubagentRunsReadCacheForTest(); |
| 47 | + if (previousReadDiskFlag === undefined) { |
| 48 | + delete process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_DISK; |
| 49 | + } else { |
| 50 | + process.env.OPENCLAW_TEST_READ_SUBAGENT_RUNS_FROM_DISK = previousReadDiskFlag; |
| 51 | + } |
| 52 | + vi.useRealTimers(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("reuses persisted snapshots for hot reads within the ttl", () => { |
| 56 | + const firstRun = createRun("run-first"); |
| 57 | + const secondRun = createRun("run-second"); |
| 58 | + mocks.loadSubagentRegistryFromSqlite |
| 59 | + .mockReturnValueOnce(new Map([[firstRun.runId, firstRun]])) |
| 60 | + .mockReturnValueOnce(new Map([[secondRun.runId, secondRun]])); |
| 61 | + |
| 62 | + expect([...getSubagentRunsSnapshotForRead(new Map()).keys()]).toEqual(["run-first"]); |
| 63 | + expect([...getSubagentRunsSnapshotForRead(new Map()).keys()]).toEqual(["run-first"]); |
| 64 | + expect(mocks.loadSubagentRegistryFromSqlite).toHaveBeenCalledTimes(1); |
| 65 | + |
| 66 | + vi.advanceTimersByTime(500); |
| 67 | + |
| 68 | + expect([...getSubagentRunsSnapshotForRead(new Map()).keys()]).toEqual(["run-second"]); |
| 69 | + expect(mocks.loadSubagentRegistryFromSqlite).toHaveBeenCalledTimes(2); |
| 70 | + }); |
| 71 | + |
| 72 | + it("refreshes the local read cache after successful writes", () => { |
| 73 | + const firstRun = createRun("run-first"); |
| 74 | + const savedRun = createRun("run-saved"); |
| 75 | + mocks.loadSubagentRegistryFromSqlite.mockReturnValue(new Map([[firstRun.runId, firstRun]])); |
| 76 | + |
| 77 | + expect([...getSubagentRunsSnapshotForRead(new Map()).keys()]).toEqual(["run-first"]); |
| 78 | + |
| 79 | + persistSubagentRunsToDisk(new Map([[savedRun.runId, savedRun]])); |
| 80 | + |
| 81 | + expect([...getSubagentRunsSnapshotForRead(new Map()).keys()]).toEqual(["run-saved"]); |
| 82 | + expect(mocks.saveSubagentRegistryToSqlite).toHaveBeenCalledOnce(); |
| 83 | + expect(mocks.loadSubagentRegistryFromSqlite).toHaveBeenCalledTimes(1); |
| 84 | + }); |
| 85 | +}); |
0 commit comments