test: share pi compaction fixtures

This commit is contained in:
Peter Steinberger
2026-03-26 15:18:40 +00:00
parent d69ff3c022
commit 5e78232bc5
3 changed files with 58 additions and 92 deletions

View File

@@ -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<number> {
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}`);
}

View File

@@ -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<number> {
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;

View File

@@ -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<number> {
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;