diff --git a/src/agents/pi-embedded-subscribe.compaction-test-helpers.ts b/src/agents/pi-embedded-subscribe.compaction-test-helpers.ts new file mode 100644 index 00000000000..fa1aa50fd86 --- /dev/null +++ b/src/agents/pi-embedded-subscribe.compaction-test-helpers.ts @@ -0,0 +1,48 @@ +import fs from "node:fs/promises"; +import path from "node:path"; + +export async function seedSessionStore(params: { + storePath: string; + sessionKey: string; + compactionCount: number; + updatedAt?: number; +}) { + await fs.mkdir(path.dirname(params.storePath), { recursive: true }); + await fs.writeFile( + params.storePath, + JSON.stringify( + { + [params.sessionKey]: { + sessionId: "session-1", + updatedAt: params.updatedAt ?? 1_000, + compactionCount: params.compactionCount, + }, + }, + null, + 2, + ), + "utf-8", + ); +} + +export async function readCompactionCount(storePath: string, sessionKey: string): Promise { + const store = JSON.parse(await fs.readFile(storePath, "utf-8")) as Record< + string, + { compactionCount?: number } + >; + return store[sessionKey]?.compactionCount ?? 0; +} + +export async function waitForCompactionCount(params: { + storePath: string; + sessionKey: string; + expected: number; +}) { + for (let attempt = 0; attempt < 40; attempt += 1) { + if ((await readCompactionCount(params.storePath, params.sessionKey)) === params.expected) { + return; + } + await new Promise((resolve) => setTimeout(resolve, 10)); + } + throw new Error(`timed out waiting for compactionCount=${params.expected}`); +} diff --git a/src/agents/pi-embedded-subscribe.handlers.compaction.test.ts b/src/agents/pi-embedded-subscribe.handlers.compaction.test.ts index b6ae77ba05b..e457c0ff8dc 100644 --- a/src/agents/pi-embedded-subscribe.handlers.compaction.test.ts +++ b/src/agents/pi-embedded-subscribe.handlers.compaction.test.ts @@ -2,58 +2,17 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { describe, expect, it, vi } from "vitest"; +import { + readCompactionCount, + seedSessionStore, + waitForCompactionCount, +} from "./pi-embedded-subscribe.compaction-test-helpers.js"; import { handleAutoCompactionEnd, reconcileSessionStoreCompactionCountAfterSuccess, } from "./pi-embedded-subscribe.handlers.compaction.js"; import type { EmbeddedPiSubscribeContext } from "./pi-embedded-subscribe.handlers.types.js"; -async function seedSessionStore(params: { - storePath: string; - sessionKey: string; - compactionCount: number; - updatedAt?: number; -}) { - await fs.mkdir(path.dirname(params.storePath), { recursive: true }); - await fs.writeFile( - params.storePath, - JSON.stringify( - { - [params.sessionKey]: { - sessionId: "session-1", - updatedAt: params.updatedAt ?? 1_000, - compactionCount: params.compactionCount, - }, - }, - null, - 2, - ), - "utf-8", - ); -} - -async function readCompactionCount(storePath: string, sessionKey: string): Promise { - const store = JSON.parse(await fs.readFile(storePath, "utf-8")) as Record< - string, - { compactionCount?: number } - >; - return store[sessionKey]?.compactionCount ?? 0; -} - -async function waitForCompactionCount(params: { - storePath: string; - sessionKey: string; - expected: number; -}) { - for (let attempt = 0; attempt < 40; attempt += 1) { - if ((await readCompactionCount(params.storePath, params.sessionKey)) === params.expected) { - return; - } - await new Promise((resolve) => setTimeout(resolve, 10)); - } - throw new Error(`timed out waiting for compactionCount=${params.expected}`); -} - function createCompactionContext(params: { storePath: string; sessionKey: string; diff --git a/src/agents/pi-embedded-subscribe.handlers.lifecycle.compaction-reconcile.test.ts b/src/agents/pi-embedded-subscribe.handlers.lifecycle.compaction-reconcile.test.ts index 8b21f376b38..d04dcbf6f41 100644 --- a/src/agents/pi-embedded-subscribe.handlers.lifecycle.compaction-reconcile.test.ts +++ b/src/agents/pi-embedded-subscribe.handlers.lifecycle.compaction-reconcile.test.ts @@ -2,55 +2,14 @@ import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { describe, expect, it, vi } from "vitest"; +import { + readCompactionCount, + seedSessionStore, + waitForCompactionCount, +} from "./pi-embedded-subscribe.compaction-test-helpers.js"; import { createEmbeddedPiSessionEventHandler } from "./pi-embedded-subscribe.handlers.js"; import type { EmbeddedPiSubscribeContext } from "./pi-embedded-subscribe.handlers.types.js"; -async function seedSessionStore(params: { - storePath: string; - sessionKey: string; - compactionCount: number; - updatedAt?: number; -}) { - await fs.mkdir(path.dirname(params.storePath), { recursive: true }); - await fs.writeFile( - params.storePath, - JSON.stringify( - { - [params.sessionKey]: { - sessionId: "session-1", - updatedAt: params.updatedAt ?? 1_000, - compactionCount: params.compactionCount, - }, - }, - null, - 2, - ), - "utf-8", - ); -} - -async function readCompactionCount(storePath: string, sessionKey: string): Promise { - const store = JSON.parse(await fs.readFile(storePath, "utf-8")) as Record< - string, - { compactionCount?: number } - >; - return store[sessionKey]?.compactionCount ?? 0; -} - -async function waitForCompactionCount(params: { - storePath: string; - sessionKey: string; - expected: number; -}) { - for (let attempt = 0; attempt < 40; attempt += 1) { - if ((await readCompactionCount(params.storePath, params.sessionKey)) === params.expected) { - return; - } - await new Promise((resolve) => setTimeout(resolve, 10)); - } - throw new Error(`timed out waiting for compactionCount=${params.expected}`); -} - function createLifecycleContext(params: { storePath: string; sessionKey: string;