mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-27 09:21:35 +07:00
test: share pi compaction fixtures
This commit is contained in:
48
src/agents/pi-embedded-subscribe.compaction-test-helpers.ts
Normal file
48
src/agents/pi-embedded-subscribe.compaction-test-helpers.ts
Normal 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}`);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user