Skip to content

Commit 7759b86

Browse files
committed
test: seed remaining session stores with sqlite fixtures
1 parent 07bb34b commit 7759b86

35 files changed

Lines changed: 284 additions & 505 deletions

src/agents/command/attempt-execution.cli.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe("CLI attempt execution", () => {
193193
}
194194

195195
function readPersistedSessionStore(): Record<string, SessionEntry> {
196-
return readSessionStoreForTest<SessionEntry>(storePath);
196+
return readSessionStoreForTest(storePath);
197197
}
198198

199199
async function runClaudeCliAttempt(params: {

src/agents/command/session-store.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ vi.mock("../../utils/usage-format.js", () => ({
7474

7575
vi.mock("../../config/sessions.js", async () => {
7676
const pathLocal = await import("node:path");
77-
const { readSessionStoreForTest, writeSessionStoreForTestAsync } =
77+
const { readSessionStoreForTest, writeSessionStoreForTestAsync: writeSessionStoreForMockAsync } =
7878
await import("../../config/sessions/test-helpers.js");
7979
const readStore = async (storePath: string): Promise<Record<string, SessionEntry>> => {
80-
return readSessionStoreForTest<SessionEntry>(storePath);
80+
return readSessionStoreForTest(storePath);
8181
};
8282
const writeStore = async (storePath: string, store: Record<string, SessionEntry>) => {
83-
await writeSessionStoreForTestAsync(storePath, store);
83+
await writeSessionStoreForMockAsync(storePath, store);
8484
};
8585
sessionStoreMocks.updateSessionStore.mockImplementation(
8686
async <T>(
@@ -123,7 +123,7 @@ vi.mock("../../config/sessions.js", async () => {
123123
},
124124
updateSessionStore: sessionStoreMocks.updateSessionStore,
125125
loadSessionStore: (storePath: string) => {
126-
return readSessionStoreForTest<SessionEntry>(storePath);
126+
return readSessionStoreForTest(storePath);
127127
},
128128
canonicalizeAbsoluteSessionFilePath: (filePath: string) => pathLocal.resolve(filePath),
129129
rewriteSessionFileForNewSessionId: (params: {

src/agents/subagent-control.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function setSubagentControlDepsForTest(
128128
storePath: string,
129129
mutator: (store: Record<string, SessionEntry>) => Promise<T> | T,
130130
) => {
131-
const store = readSessionStoreForTest<SessionEntry>(storePath);
131+
const store = readSessionStoreForTest(storePath);
132132
const result = await mutator(store);
133133
writeSessionStoreForTest(storePath, store);
134134
return result;

src/auto-reply/reply/agent-runner-session-reset.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import os from "node:os";
44
import path from "node:path";
55
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
66
import type { SessionEntry } from "../../config/sessions.js";
7+
import { readSessionStoreForTest } from "../../config/sessions/test-helpers.js";
78
import {
89
resetReplyRunSession,
910
setAgentRunnerSessionResetTestDeps,
@@ -128,9 +129,7 @@ describe("resetReplyRunSession", () => {
128129
});
129130
expect(errorMock).toHaveBeenCalledWith("reset 00000000-0000-0000-0000-000000000123");
130131

131-
const persisted = JSON.parse(await fs.readFile(storePath, "utf8")) as {
132-
main: SessionEntry;
133-
};
132+
const persisted = readSessionStoreForTest(storePath);
134133
expect(persisted.main.sessionId).toBe(activeSessionEntry?.sessionId);
135134
expect(persisted.main.contextBudgetStatus).toBeUndefined();
136135
expect(persisted.main.fallbackNoticeReason).toBeUndefined();

src/auto-reply/reply/agent-runner.misc.runreplyagent.test.ts

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import type { OpenClawConfig } from "../../config/config.js";
1414
import * as sessionTypesModule from "../../config/sessions.js";
1515
import type { SessionEntry } from "../../config/sessions.js";
1616
import { loadSessionStore, saveSessionStore } from "../../config/sessions.js";
17+
import {
18+
readSessionStoreForTest,
19+
writeSessionStoreForTestAsync,
20+
} from "../../config/sessions/test-helpers.js";
1721
import {
1822
onInternalDiagnosticEvent,
1923
resetDiagnosticEventsForTest,
@@ -248,6 +252,16 @@ function firstMockCallArg(mock: MockCallSource, label: string): unknown {
248252
return call[0];
249253
}
250254

255+
async function seedSessionStore(params: {
256+
storePath: string;
257+
sessionKey: string;
258+
entry: Record<string, unknown>;
259+
}) {
260+
await writeSessionStoreForTestAsync(params.storePath, {
261+
[params.sessionKey]: params.entry as SessionEntry,
262+
});
263+
}
264+
251265
beforeEach(() => {
252266
vi.useRealTimers();
253267
registerCliBackendsForTest();
@@ -298,19 +312,6 @@ afterEach(() => {
298312
});
299313

300314
describe("runReplyAgent auto-compaction token update", () => {
301-
async function seedSessionStore(params: {
302-
storePath: string;
303-
sessionKey: string;
304-
entry: Record<string, unknown>;
305-
}) {
306-
await fs.mkdir(path.dirname(params.storePath), { recursive: true });
307-
await fs.writeFile(
308-
params.storePath,
309-
JSON.stringify({ [params.sessionKey]: params.entry }, null, 2),
310-
"utf-8",
311-
);
312-
}
313-
314315
function createBaseRun(params: {
315316
storePath: string;
316317
sessionEntry: Record<string, unknown>;
@@ -421,7 +422,7 @@ describe("runReplyAgent auto-compaction token update", () => {
421422
unsubscribe?.();
422423
}
423424

424-
const stored = JSON.parse(await fs.readFile(storePath, "utf-8"));
425+
const stored = readSessionStoreForTest(storePath);
425426
const usageEvent = diagnostics.find((event) => event.type === "model.usage");
426427
return { sessionKey, stored, usageEvent };
427428
}
@@ -827,17 +828,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
827828
verboseLevel: "on",
828829
};
829830

830-
await fs.writeFile(
831-
storePath,
832-
JSON.stringify(
833-
{
834-
[sessionKey]: sessionEntry,
835-
},
836-
null,
837-
2,
838-
),
839-
"utf-8",
840-
);
831+
await seedSessionStore({ storePath, sessionKey, entry: sessionEntry });
841832

842833
runEmbeddedAgentMock.mockImplementationOnce(async () => {
843834
const latest = loadSessionStore(storePath, { skipCache: true });
@@ -939,17 +930,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
939930
traceLevel: "on",
940931
};
941932

942-
await fs.writeFile(
943-
storePath,
944-
JSON.stringify(
945-
{
946-
[sessionKey]: sessionEntry,
947-
},
948-
null,
949-
2,
950-
),
951-
"utf-8",
952-
);
933+
await seedSessionStore({ storePath, sessionKey, entry: sessionEntry });
953934

954935
runEmbeddedAgentMock.mockImplementationOnce(async () => {
955936
const latest = loadSessionStore(storePath, { skipCache: true });
@@ -1050,17 +1031,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
10501031
traceLevel: "on",
10511032
};
10521033

1053-
await fs.writeFile(
1054-
storePath,
1055-
JSON.stringify(
1056-
{
1057-
[sessionKey]: sessionEntry,
1058-
},
1059-
null,
1060-
2,
1061-
),
1062-
"utf-8",
1063-
);
1034+
await seedSessionStore({ storePath, sessionKey, entry: sessionEntry });
10641035

10651036
runEmbeddedAgentMock.mockImplementationOnce(async () => {
10661037
const latest = loadSessionStore(storePath, { skipCache: true });
@@ -1163,17 +1134,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
11631134
compactionCount: 3,
11641135
};
11651136

1166-
await fs.writeFile(
1167-
storePath,
1168-
JSON.stringify(
1169-
{
1170-
[sessionKey]: sessionEntry,
1171-
},
1172-
null,
1173-
2,
1174-
),
1175-
"utf-8",
1176-
);
1137+
await seedSessionStore({ storePath, sessionKey, entry: sessionEntry });
11771138
await fs.writeFile(
11781139
sessionFile,
11791140
[
@@ -1409,7 +1370,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
14091370
traceLevel: "raw",
14101371
};
14111372

1412-
await fs.writeFile(storePath, JSON.stringify({ [sessionKey]: sessionEntry }, null, 2), "utf-8");
1373+
await seedSessionStore({ storePath, sessionKey, entry: sessionEntry });
14131374
await fs.writeFile(sessionFile, "", "utf-8");
14141375

14151376
runEmbeddedAgentMock.mockResolvedValueOnce({
@@ -1625,7 +1586,7 @@ describe("runReplyAgent Active Memory inline debug", () => {
16251586
traceLevel: "raw",
16261587
};
16271588

1628-
await fs.writeFile(storePath, JSON.stringify({ [sessionKey]: sessionEntry }, null, 2), "utf-8");
1589+
await seedSessionStore({ storePath, sessionKey, entry: sessionEntry });
16291590
await fs.writeFile(sessionFile, "", "utf-8");
16301591

16311592
runEmbeddedAgentMock.mockResolvedValueOnce({
@@ -3010,7 +2971,7 @@ describe("runReplyAgent private message_tool_only final warning (#85714)", () =>
30102971
const storePath = path.join(tmp, "sessions.json");
30112972
const sessionKey = "stranded";
30122973
const sessionEntry = { sessionId: "session", updatedAt: Date.now(), totalTokens: 1_000 };
3013-
await fs.writeFile(storePath, JSON.stringify({ [sessionKey]: sessionEntry }, null, 2), "utf-8");
2974+
await seedSessionStore({ storePath, sessionKey, entry: sessionEntry });
30142975

30152976
const finalAssistantText =
30162977
params.finalAssistantText ??

src/auto-reply/reply/session-hooks-context.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from "node:path";
55
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
66
import type { OpenClawConfig } from "../../config/config.js";
77
import type { SessionEntry } from "../../config/sessions.js";
8+
import { writeSessionStoreForTestAsync } from "../../config/sessions/test-helpers.js";
89
import type { HookRunner } from "../../plugins/hooks.js";
910
import { initSessionState } from "./session.js";
1011

@@ -70,8 +71,7 @@ async function writeStore(
7071
storePath: string,
7172
store: Record<string, SessionEntry | Record<string, unknown>>,
7273
): Promise<void> {
73-
await fs.mkdir(path.dirname(storePath), { recursive: true });
74-
await fs.writeFile(storePath, JSON.stringify(store), "utf-8");
74+
await writeSessionStoreForTestAsync(storePath, store as Record<string, SessionEntry>);
7575
}
7676

7777
async function writeTranscript(

src/auto-reply/reply/session.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ describe("initSessionState reset policy", () => {
16181618
expect(result.isNewSession).toBe(false);
16191619
expect(result.sessionId).toBe(existingSessionId);
16201620

1621-
const persisted = readSessionStoreForTest(storePath) as Record<string, SessionEntry>;
1621+
const persisted = readSessionStoreForTest(storePath);
16221622
expect(persisted[sessionKey]?.sessionId).toBe(existingSessionId);
16231623
expect(persisted[sessionKey]?.status).toBe("done");
16241624
expect(persisted[sessionKey]?.startedAt).toBe(Date.now() - 10_000);
@@ -1712,7 +1712,7 @@ describe("initSessionState reset policy", () => {
17121712
});
17131713

17141714
expect(result.isNewSession).toBe(scenario.expectNewSession);
1715-
const persisted = readSessionStoreForTest(storePath) as Record<string, SessionEntry>;
1715+
const persisted = readSessionStoreForTest(storePath);
17161716
const entry = persisted[scenario.sessionKey];
17171717
if (scenario.expectNewSession) {
17181718
expect(result.sessionId).not.toBe(existingSessionId);
@@ -4042,7 +4042,7 @@ describe("initSessionState dmScope delivery migration", () => {
40424042
});
40434043

40444044
expect(result.sessionKey).toBe("agent:main:telegram:direct:6101296751");
4045-
const persisted = readSessionStoreForTest(storePath) as Record<string, SessionEntry>;
4045+
const persisted = readSessionStoreForTest(storePath);
40464046
expect(persisted["agent:main:main"]?.sessionId).toBe("legacy-main");
40474047
expect(persisted["agent:main:main"]?.route).toBeUndefined();
40484048
expect(persisted["agent:main:main"]?.deliveryContext).toBeUndefined();
@@ -4085,7 +4085,7 @@ describe("initSessionState dmScope delivery migration", () => {
40854085
commandAuthorized: true,
40864086
});
40874087

4088-
const persisted = readSessionStoreForTest(storePath) as Record<string, SessionEntry>;
4088+
const persisted = readSessionStoreForTest(storePath);
40894089
expect(persisted["agent:main:main"]?.deliveryContext).toEqual({
40904090
channel: "telegram",
40914091
to: "1111",
@@ -4160,7 +4160,7 @@ describe("initSessionState internal channel routing preservation", () => {
41604160
accountId: "default",
41614161
});
41624162

4163-
const persisted = readSessionStoreForTest(storePath) as Record<string, SessionEntry>;
4163+
const persisted = readSessionStoreForTest(storePath);
41644164
expect(persisted[sessionKey]?.lastThreadId).toBeUndefined();
41654165
expect(persisted[sessionKey]?.deliveryContext).toEqual({
41664166
channel: "mattermost",

src/commands/agent.session.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { updateSessionStoreAfterAgentRun } from "../agents/command/session-store
88
import { resolveSession } from "../agents/command/session.js";
99
import { loadSessionStore } from "../config/sessions/store-load.js";
1010
import { clearSessionStoreCacheForTest } from "../config/sessions/store.js";
11+
import { writeSessionStoreForTest } from "../config/sessions/test-helpers.js";
1112
import { resolveSessionTranscriptFile } from "../config/sessions/transcript.js";
1213
import type { OpenClawConfig } from "../config/types.openclaw.js";
1314
import { buildOutboundSessionContext } from "../infra/outbound/session-context.js";
@@ -41,8 +42,7 @@ function writeSessionStoreSeed(
4142
storePath: string,
4243
sessions: Record<string, Record<string, unknown>>,
4344
) {
44-
fs.mkdirSync(path.dirname(storePath), { recursive: true });
45-
fs.writeFileSync(storePath, JSON.stringify(sessions));
45+
writeSessionStoreForTest(storePath, sessions);
4646
}
4747

4848
async function withCrossAgentResumeFixture(

src/commands/doctor-state-integrity.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ describe("doctor state integrity oauth dir checks", () => {
584584
await noteStateIntegrity(cfg, { confirmRuntimeRepair, note: noteMock });
585585

586586
const storePath = resolveStorePath(cfg.session?.store, { agentId: "main" });
587-
const store = readSessionStoreForTest<SessionEntry>(storePath);
587+
const store = readSessionStoreForTest(storePath);
588588
const recoveredKey = Object.keys(store).find((key) =>
589589
key.startsWith("agent:main:heartbeat-recovered-"),
590590
);
@@ -628,7 +628,7 @@ describe("doctor state integrity oauth dir checks", () => {
628628
await noteStateIntegrity(cfg, { confirmRuntimeRepair, note: noteMock });
629629

630630
const storePath = resolveStorePath(cfg.session?.store, { agentId: "main" });
631-
const store = readSessionStoreForTest<SessionEntry>(storePath);
631+
const store = readSessionStoreForTest(storePath);
632632
expect(store["agent:main:main"]?.sessionId).toBe("mixed-session");
633633
expect(Object.keys(store).filter((key) => key.includes("heartbeat-recovered"))).toEqual([]);
634634
expect(hasRepairPromptMessage(confirmRuntimeRepair, "Move heartbeat-owned main session")).toBe(

0 commit comments

Comments
 (0)