refactor(test): consolidate doctor health and sandbox fixtures

This commit is contained in:
Peter Steinberger
2026-02-16 16:48:46 +00:00
parent 2d8edf85ad
commit 0d51869c3c
5 changed files with 133 additions and 109 deletions

View File

@@ -35,6 +35,20 @@ function writeJson5(filePath: string, value: unknown) {
fs.writeFileSync(filePath, JSON.stringify(value, null, 2), "utf-8");
}
function writeLegacySessionsFixture(params: {
root: string;
sessions: Record<string, { sessionId: string; updatedAt: number }>;
transcripts?: Record<string, string>;
}) {
const legacySessionsDir = path.join(params.root, "sessions");
fs.mkdirSync(legacySessionsDir, { recursive: true });
writeJson5(path.join(legacySessionsDir, "sessions.json"), params.sessions);
for (const [fileName, content] of Object.entries(params.transcripts ?? {})) {
fs.writeFileSync(path.join(legacySessionsDir, fileName), content, "utf-8");
}
return legacySessionsDir;
}
async function detectAndRunMigrations(params: {
root: string;
cfg: OpenClawConfig;
@@ -93,6 +107,21 @@ async function runStateDirMigration(root: string, env = {} as NodeJS.ProcessEnv)
});
}
async function runAutoMigrateLegacyStateWithLog(params: {
root: string;
cfg: OpenClawConfig;
now?: () => number;
}) {
const log = { info: vi.fn(), warn: vi.fn() };
const result = await autoMigrateLegacyState({
cfg: params.cfg,
env: { OPENCLAW_STATE_DIR: params.root } as NodeJS.ProcessEnv,
log,
now: params.now,
});
return { result, log };
}
function expectTargetAlreadyExistsWarning(result: StateDirMigrationResult, targetDir: string) {
expect(result.migrated).toBe(false);
expect(result.warnings).toEqual([
@@ -104,18 +133,20 @@ describe("doctor legacy state migrations", () => {
it("migrates legacy sessions into agents/<id>/sessions", async () => {
const root = await makeTempRoot();
const cfg: OpenClawConfig = {};
const legacySessionsDir = path.join(root, "sessions");
fs.mkdirSync(legacySessionsDir, { recursive: true });
writeJson5(path.join(legacySessionsDir, "sessions.json"), {
"+1555": { sessionId: "a", updatedAt: 10 },
"+1666": { sessionId: "b", updatedAt: 20 },
"slack:channel:C123": { sessionId: "c", updatedAt: 30 },
"group:abc": { sessionId: "d", updatedAt: 40 },
"subagent:xyz": { sessionId: "e", updatedAt: 50 },
const legacySessionsDir = writeLegacySessionsFixture({
root,
sessions: {
"+1555": { sessionId: "a", updatedAt: 10 },
"+1666": { sessionId: "b", updatedAt: 20 },
"slack:channel:C123": { sessionId: "c", updatedAt: 30 },
"group:abc": { sessionId: "d", updatedAt: 40 },
"subagent:xyz": { sessionId: "e", updatedAt: 50 },
},
transcripts: {
"a.jsonl": "a",
"b.jsonl": "b",
},
});
fs.writeFileSync(path.join(legacySessionsDir, "a.jsonl"), "a", "utf-8");
fs.writeFileSync(path.join(legacySessionsDir, "b.jsonl"), "b", "utf-8");
const detected = await detectLegacyStateMigrations({
cfg,
@@ -177,13 +208,7 @@ describe("doctor legacy state migrations", () => {
fs.mkdirSync(legacyAgentDir, { recursive: true });
fs.writeFileSync(path.join(legacyAgentDir, "auth.json"), "{}", "utf-8");
const log = { info: vi.fn(), warn: vi.fn() };
const result = await autoMigrateLegacyState({
cfg,
env: { OPENCLAW_STATE_DIR: root } as NodeJS.ProcessEnv,
log,
});
const { result, log } = await runAutoMigrateLegacyStateWithLog({ root, cfg });
const targetAgentDir = path.join(root, "agents", "main", "agent");
expect(fs.existsSync(path.join(targetAgentDir, "auth.json"))).toBe(true);
@@ -194,20 +219,19 @@ describe("doctor legacy state migrations", () => {
it("auto-migrates legacy sessions on startup", async () => {
const root = await makeTempRoot();
const cfg: OpenClawConfig = {};
const legacySessionsDir = path.join(root, "sessions");
fs.mkdirSync(legacySessionsDir, { recursive: true });
writeJson5(path.join(legacySessionsDir, "sessions.json"), {
"+1555": { sessionId: "a", updatedAt: 10 },
const legacySessionsDir = writeLegacySessionsFixture({
root,
sessions: {
"+1555": { sessionId: "a", updatedAt: 10 },
},
transcripts: {
"a.jsonl": "a",
},
});
fs.writeFileSync(path.join(legacySessionsDir, "a.jsonl"), "a", "utf-8");
const log = { info: vi.fn(), warn: vi.fn() };
const result = await autoMigrateLegacyState({
const { result, log } = await runAutoMigrateLegacyStateWithLog({
root,
cfg,
env: { OPENCLAW_STATE_DIR: root } as NodeJS.ProcessEnv,
log,
now: () => 123,
});
@@ -295,10 +319,11 @@ describe("doctor legacy state migrations", () => {
const cfg: OpenClawConfig = {
agents: { list: [{ id: "alpha", default: true }] },
};
const legacySessionsDir = path.join(root, "sessions");
fs.mkdirSync(legacySessionsDir, { recursive: true });
writeJson5(path.join(legacySessionsDir, "sessions.json"), {
"+1555": { sessionId: "a", updatedAt: 10 },
writeLegacySessionsFixture({
root,
sessions: {
"+1555": { sessionId: "a", updatedAt: 10 },
},
});
const targetDir = path.join(root, "agents", "alpha", "sessions");
@@ -314,11 +339,12 @@ describe("doctor legacy state migrations", () => {
it("honors session.mainKey when seeding the direct-chat bucket", async () => {
const root = await makeTempRoot();
const cfg: OpenClawConfig = { session: { mainKey: "work" } };
const legacySessionsDir = path.join(root, "sessions");
fs.mkdirSync(legacySessionsDir, { recursive: true });
writeJson5(path.join(legacySessionsDir, "sessions.json"), {
"+1555": { sessionId: "a", updatedAt: 10 },
"+1666": { sessionId: "b", updatedAt: 20 },
writeLegacySessionsFixture({
root,
sessions: {
"+1555": { sessionId: "a", updatedAt: 10 },
"+1666": { sessionId: "b", updatedAt: 20 },
},
});
const targetDir = path.join(root, "agents", "main", "sessions");
@@ -396,13 +422,7 @@ describe("doctor legacy state migrations", () => {
main: { sessionId: "legacy", updatedAt: 10 },
});
const log = { info: vi.fn(), warn: vi.fn() };
const result = await autoMigrateLegacyState({
cfg,
env: { OPENCLAW_STATE_DIR: root } as NodeJS.ProcessEnv,
log,
});
const { result, log } = await runAutoMigrateLegacyStateWithLog({ root, cfg });
const store = JSON.parse(
fs.readFileSync(path.join(targetDir, "sessions.json"), "utf-8"),