diff --git a/src/commands/doctor-memory-search.test.ts b/src/commands/doctor-memory-search.test.ts index 9db631a6fe1..10c58132060 100644 --- a/src/commands/doctor-memory-search.test.ts +++ b/src/commands/doctor-memory-search.test.ts @@ -31,6 +31,19 @@ import { detectLegacyWorkspaceDirs } from "./doctor-workspace.js"; describe("noteMemorySearchHealth", () => { const cfg = {} as OpenClawConfig; + async function expectNoWarningWithConfiguredRemoteApiKey(provider: string) { + resolveMemorySearchConfig.mockReturnValue({ + provider, + local: {}, + remote: { apiKey: "from-config" }, + }); + + await noteMemorySearchHealth(cfg); + + expect(note).not.toHaveBeenCalled(); + expect(resolveApiKeyForProvider).not.toHaveBeenCalled(); + } + beforeEach(() => { note.mockReset(); resolveDefaultAgentId.mockClear(); @@ -40,29 +53,11 @@ describe("noteMemorySearchHealth", () => { }); it("does not warn when remote apiKey is configured for explicit provider", async () => { - resolveMemorySearchConfig.mockReturnValue({ - provider: "openai", - local: {}, - remote: { apiKey: "from-config" }, - }); - - await noteMemorySearchHealth(cfg); - - expect(note).not.toHaveBeenCalled(); - expect(resolveApiKeyForProvider).not.toHaveBeenCalled(); + await expectNoWarningWithConfiguredRemoteApiKey("openai"); }); it("does not warn in auto mode when remote apiKey is configured", async () => { - resolveMemorySearchConfig.mockReturnValue({ - provider: "auto", - local: {}, - remote: { apiKey: "from-config" }, - }); - - await noteMemorySearchHealth(cfg); - - expect(note).not.toHaveBeenCalled(); - expect(resolveApiKeyForProvider).not.toHaveBeenCalled(); + await expectNoWarningWithConfiguredRemoteApiKey("auto"); }); it("resolves provider auth from the default agent directory", async () => { diff --git a/src/commands/doctor-state-migrations.e2e.test.ts b/src/commands/doctor-state-migrations.e2e.test.ts index 0deb332e9a4..d00fc6628d7 100644 --- a/src/commands/doctor-state-migrations.e2e.test.ts +++ b/src/commands/doctor-state-migrations.e2e.test.ts @@ -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; + transcripts?: Record; +}) { + 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//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"), diff --git a/src/commands/health.command.coverage.e2e.test.ts b/src/commands/health.command.coverage.e2e.test.ts index 794f9adb586..bb29d4906c8 100644 --- a/src/commands/health.command.coverage.e2e.test.ts +++ b/src/commands/health.command.coverage.e2e.test.ts @@ -8,6 +8,13 @@ import { healthCommand } from "./health.js"; const callGatewayMock = vi.fn(); const logWebSelfIdMock = vi.fn(); +function createRecentSessionRows(now = Date.now()) { + return [ + { key: "main", updatedAt: now - 60_000, age: 60_000 }, + { key: "foo", updatedAt: null, age: null }, + ]; +} + vi.mock("../gateway/call.js", () => ({ callGateway: (...args: unknown[]) => callGatewayMock(...args), })); @@ -56,6 +63,7 @@ describe("healthCommand (coverage)", () => { }); it("prints the rich text summary when linked and configured", async () => { + const recent = createRecentSessionRows(); callGatewayMock.mockResolvedValueOnce({ ok: true, ts: Date.now(), @@ -104,20 +112,14 @@ describe("healthCommand (coverage)", () => { sessions: { path: "/tmp/sessions.json", count: 2, - recent: [ - { key: "main", updatedAt: Date.now() - 60_000, age: 60_000 }, - { key: "foo", updatedAt: null, age: null }, - ], + recent, }, }, ], sessions: { path: "/tmp/sessions.json", count: 2, - recent: [ - { key: "main", updatedAt: Date.now() - 60_000, age: 60_000 }, - { key: "foo", updatedAt: null, age: null }, - ], + recent, }, } satisfies HealthSummary); diff --git a/src/commands/health.snapshot.e2e.test.ts b/src/commands/health.snapshot.e2e.test.ts index 5732dfb80df..e06b7ed39fe 100644 --- a/src/commands/health.snapshot.e2e.test.ts +++ b/src/commands/health.snapshot.e2e.test.ts @@ -72,6 +72,33 @@ function stubTelegramFetchOk(calls: string[]) { ); } +async function runSuccessfulTelegramProbe( + config: Record, + options?: { clearTokenEnv?: boolean }, +) { + testConfig = config; + testStore = {}; + vi.stubEnv("DISCORD_BOT_TOKEN", ""); + if (options?.clearTokenEnv) { + vi.stubEnv("TELEGRAM_BOT_TOKEN", ""); + } + + const calls: string[] = []; + stubTelegramFetchOk(calls); + + const snap = await getHealthSnapshot({ timeoutMs: 25 }); + const telegram = snap.channels.telegram as { + configured?: boolean; + probe?: { + ok?: boolean; + bot?: { username?: string }; + webhook?: { url?: string }; + }; + }; + + return { calls, telegram }; +} + describe("getHealthSnapshot", () => { beforeEach(async () => { setActivePluginRegistry( @@ -112,22 +139,9 @@ describe("getHealthSnapshot", () => { }); it("probes telegram getMe + webhook info when configured", async () => { - testConfig = { channels: { telegram: { botToken: "t-1" } } }; - testStore = {}; - vi.stubEnv("DISCORD_BOT_TOKEN", ""); - - const calls: string[] = []; - stubTelegramFetchOk(calls); - - const snap = await getHealthSnapshot({ timeoutMs: 25 }); - const telegram = snap.channels.telegram as { - configured?: boolean; - probe?: { - ok?: boolean; - bot?: { username?: string }; - webhook?: { url?: string }; - }; - }; + const { calls, telegram } = await runSuccessfulTelegramProbe({ + channels: { telegram: { botToken: "t-1" } }, + }); expect(telegram.configured).toBe(true); expect(telegram.probe?.ok).toBe(true); expect(telegram.probe?.bot?.username).toBe("bot"); @@ -140,18 +154,10 @@ describe("getHealthSnapshot", () => { const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-health-")); const tokenFile = path.join(tmpDir, "telegram-token"); fs.writeFileSync(tokenFile, "t-file\n", "utf-8"); - testConfig = { channels: { telegram: { tokenFile } } }; - testStore = {}; - vi.stubEnv("TELEGRAM_BOT_TOKEN", ""); - - const calls: string[] = []; - stubTelegramFetchOk(calls); - - const snap = await getHealthSnapshot({ timeoutMs: 25 }); - const telegram = snap.channels.telegram as { - configured?: boolean; - probe?: { ok?: boolean }; - }; + const { calls, telegram } = await runSuccessfulTelegramProbe( + { channels: { telegram: { tokenFile } } }, + { clearTokenEnv: true }, + ); expect(telegram.configured).toBe(true); expect(telegram.probe?.ok).toBe(true); expect(calls.some((c) => c.includes("bott-file/getMe"))).toBe(true); diff --git a/src/commands/sandbox.e2e.test.ts b/src/commands/sandbox.e2e.test.ts index 83900c7f55e..c266587fdf1 100644 --- a/src/commands/sandbox.e2e.test.ts +++ b/src/commands/sandbox.e2e.test.ts @@ -250,6 +250,13 @@ describe("sandboxRecreateCommand", () => { }); describe("confirmation flow", () => { + async function runCancelledConfirmation(confirmResult: boolean | symbol) { + mocks.listSandboxContainers.mockResolvedValue([createContainer()]); + mocks.clackConfirm.mockResolvedValue(confirmResult); + + await sandboxRecreateCommand({ all: true, browser: false, force: false }, runtime as never); + } + it("should require confirmation without --force", async () => { mocks.listSandboxContainers.mockResolvedValue([createContainer()]); mocks.clackConfirm.mockResolvedValue(true); @@ -261,20 +268,14 @@ describe("sandboxRecreateCommand", () => { }); it("should cancel when user declines", async () => { - mocks.listSandboxContainers.mockResolvedValue([createContainer()]); - mocks.clackConfirm.mockResolvedValue(false); - - await sandboxRecreateCommand({ all: true, browser: false, force: false }, runtime as never); + await runCancelledConfirmation(false); expect(runtime.log).toHaveBeenCalledWith("Cancelled."); expect(mocks.removeSandboxContainer).not.toHaveBeenCalled(); }); it("should cancel on clack cancel symbol", async () => { - mocks.listSandboxContainers.mockResolvedValue([createContainer()]); - mocks.clackConfirm.mockResolvedValue(Symbol.for("clack:cancel")); - - await sandboxRecreateCommand({ all: true, browser: false, force: false }, runtime as never); + await runCancelledConfirmation(Symbol.for("clack:cancel")); expect(runtime.log).toHaveBeenCalledWith("Cancelled."); expect(mocks.removeSandboxContainer).not.toHaveBeenCalled();