|
| 1 | +import fs from "node:fs"; |
| 2 | +import { expect, it, vi } from "vitest"; |
| 3 | +import { resolveInternalSessionEffectsIdentity } from "../../config/sessions/internal-session-key.js"; |
| 4 | +import * as sessionAccessor from "../../config/sessions/session-accessor.js"; |
| 5 | +import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
| 6 | +import { |
| 7 | + isOpenClawAgentDatabaseOpen, |
| 8 | + resolveIncognitoOpenClawAgentSqlitePath, |
| 9 | +} from "../../state/openclaw-agent-db.js"; |
| 10 | +import { withOpenClawTestState } from "../../test-utils/openclaw-test-state.js"; |
| 11 | +import { resolveSession, resolveSessionKeyForRequestCore } from "./session.js"; |
| 12 | + |
| 13 | +it.each(["work", "dashboard:incognito-work"])( |
| 14 | + "resolves the exact %s session without enumerating unrelated rows", |
| 15 | + async (key) => { |
| 16 | + await withOpenClawTestState({ label: "command-exact-session" }, async (state) => { |
| 17 | + const storePath = state.path("sessions.sqlite"); |
| 18 | + const sessionKey = `agent:main:${key}`; |
| 19 | + const incognito = key.startsWith("dashboard:incognito-"); |
| 20 | + const cfg = { |
| 21 | + agents: { defaults: {} }, |
| 22 | + session: { store: storePath, reset: { mode: "idle", idleMinutes: 60 } }, |
| 23 | + } satisfies OpenClawConfig; |
| 24 | + const entry = { |
| 25 | + sessionId: "selected-session", |
| 26 | + updatedAt: Date.now(), |
| 27 | + sessionStartedAt: Date.now(), |
| 28 | + lastInteractionAt: Date.now(), |
| 29 | + thinkingLevel: "high", |
| 30 | + modelOverride: "gpt-5.6-luna", |
| 31 | + providerOverride: "openai", |
| 32 | + lifecycleRevision: "selected-revision", |
| 33 | + skillsSnapshot: { prompt: "selected prompt", skills: [] }, |
| 34 | + ...(incognito ? { incognito: true as const } : {}), |
| 35 | + }; |
| 36 | + await sessionAccessor.replaceSessionEntry({ sessionKey, storePath }, entry); |
| 37 | + const persisted = sessionAccessor.loadExactSessionEntryReadOnly({ |
| 38 | + sessionKey, |
| 39 | + storePath, |
| 40 | + })?.entry; |
| 41 | + expect(persisted).toMatchObject({ |
| 42 | + thinkingLevel: "high", |
| 43 | + modelOverride: "gpt-5.6-luna", |
| 44 | + providerOverride: "openai", |
| 45 | + lifecycleRevision: expect.any(String), |
| 46 | + }); |
| 47 | + if (!incognito) { |
| 48 | + await sessionAccessor.replaceSessionEntry( |
| 49 | + { sessionKey: "agent:main:unrelated", storePath }, |
| 50 | + { sessionId: "unrelated-session", updatedAt: Date.now() }, |
| 51 | + ); |
| 52 | + } |
| 53 | + const list = vi.spyOn(sessionAccessor, "listSessionEntriesCore"); |
| 54 | + try { |
| 55 | + const resolved = resolveSession({ cfg, sessionKey }); |
| 56 | + expect(resolved).toMatchObject({ |
| 57 | + sessionId: entry.sessionId, |
| 58 | + sessionKey, |
| 59 | + isNewSession: false, |
| 60 | + persistedThinking: "high", |
| 61 | + sessionEntry: persisted, |
| 62 | + }); |
| 63 | + if (!resolved.sessionEntry?.skillsSnapshot) { |
| 64 | + throw new Error("expected the selected session's skills"); |
| 65 | + } |
| 66 | + resolved.sessionEntry.skillsSnapshot.prompt = "caller-owned edit"; |
| 67 | + expect( |
| 68 | + sessionAccessor.loadExactSessionEntryReadOnly({ sessionKey, storePath })?.entry |
| 69 | + .skillsSnapshot?.prompt, |
| 70 | + ).toBe("selected prompt"); |
| 71 | + expect(list).not.toHaveBeenCalled(); |
| 72 | + if (incognito) { |
| 73 | + expect(fs.existsSync(storePath)).toBe(false); |
| 74 | + } |
| 75 | + } finally { |
| 76 | + list.mockRestore(); |
| 77 | + } |
| 78 | + }); |
| 79 | + }, |
| 80 | +); |
| 81 | + |
| 82 | +it("does not provision a missing incognito lookup or select a hidden run-owned entry", async () => { |
| 83 | + await withOpenClawTestState({ label: "command-private-session" }, async (state) => { |
| 84 | + const storePath = state.path("sessions.sqlite"); |
| 85 | + const cfg = { agents: { defaults: {} }, session: { store: storePath } }; |
| 86 | + const incognitoPath = resolveIncognitoOpenClawAgentSqlitePath({ agentId: "main" }); |
| 87 | + expect( |
| 88 | + resolveSessionKeyForRequestCore({ cfg, sessionKey: "agent:main:dashboard:incognito-missing" }) |
| 89 | + .sessionEntry, |
| 90 | + ).toBeUndefined(); |
| 91 | + expect(isOpenClawAgentDatabaseOpen(incognitoPath)).toBe(false); |
| 92 | + expect(fs.existsSync(storePath)).toBe(false); |
| 93 | + |
| 94 | + const hidden = resolveInternalSessionEffectsIdentity({ agentId: "main", runId: "hidden-run" }); |
| 95 | + await sessionAccessor.replaceSessionEntry( |
| 96 | + { sessionKey: hidden.sessionKey, storePath }, |
| 97 | + { sessionId: hidden.sessionId, updatedAt: Date.now() }, |
| 98 | + ); |
| 99 | + expect( |
| 100 | + resolveSessionKeyForRequestCore({ cfg, sessionKey: hidden.sessionKey }).sessionEntry, |
| 101 | + ).toBeUndefined(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments