|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js"; |
| 5 | +import { resolveSessionStorePathCore } from "../config/sessions/paths.js"; |
| 6 | +import { resolveSqliteScope } from "../config/sessions/session-accessor.sqlite-scope.js"; |
| 7 | +import type { RuntimeEnv } from "../runtime.js"; |
| 8 | +import { agentExecCommand } from "./agent-exec.js"; |
| 9 | + |
| 10 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 11 | + |
| 12 | +const runtime: RuntimeEnv = { |
| 13 | + log: vi.fn(), |
| 14 | + error: vi.fn(), |
| 15 | + exit: vi.fn(), |
| 16 | +}; |
| 17 | + |
| 18 | +afterEach(() => { |
| 19 | + vi.restoreAllMocks(); |
| 20 | +}); |
| 21 | + |
| 22 | +describe("agent exec owner selection", () => { |
| 23 | + it.each([ |
| 24 | + { |
| 25 | + name: "a migrated legacy default", |
| 26 | + config: { |
| 27 | + agents: { |
| 28 | + entries: { |
| 29 | + alpha: { default: true }, |
| 30 | + beta: {}, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "the canonical system-agent owner", |
| 37 | + config: { |
| 38 | + agents: { |
| 39 | + ownership: "explicit", |
| 40 | + defaults: { systemAgent: { agentId: "alpha" } }, |
| 41 | + entries: { alpha: {}, beta: {} }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + ])("uses $name for the run and its SQLite store scope", async ({ config }) => { |
| 46 | + const configRoot = tempDirs.make("openclaw-agent-exec-default-agent-"); |
| 47 | + const configPath = path.join(configRoot, "openclaw.json"); |
| 48 | + await fs.writeFile(configPath, JSON.stringify(config), "utf8"); |
| 49 | + const runAgent = vi.fn(async (options: Record<string, unknown>) => { |
| 50 | + const requestedAgentId = typeof options.agentId === "string" ? options.agentId : "main"; |
| 51 | + const sessionId = String(options.sessionId); |
| 52 | + const storePath = resolveSessionStorePathCore(undefined, { |
| 53 | + agentId: "alpha", |
| 54 | + env: process.env, |
| 55 | + }); |
| 56 | + |
| 57 | + // Exercise the production guard that rejects keys owned by another agent. |
| 58 | + expect(() => |
| 59 | + resolveSqliteScope({ |
| 60 | + agentId: requestedAgentId, |
| 61 | + defaultAgentId: "alpha", |
| 62 | + env: process.env, |
| 63 | + sessionKey: `agent:${requestedAgentId}:explicit:${sessionId}`, |
| 64 | + storePath, |
| 65 | + }), |
| 66 | + ).not.toThrow(); |
| 67 | + expect(requestedAgentId).toBe("alpha"); |
| 68 | + return { |
| 69 | + payloads: [{ text: "done" }], |
| 70 | + meta: { durationMs: 1 }, |
| 71 | + }; |
| 72 | + }); |
| 73 | + |
| 74 | + const result = await agentExecCommand("inspect", { config: configPath }, runtime, { runAgent }); |
| 75 | + |
| 76 | + expect(result.envelope.error).toBeUndefined(); |
| 77 | + expect(result.exitCode).toBe(0); |
| 78 | + expect(runAgent).toHaveBeenCalledOnce(); |
| 79 | + }); |
| 80 | +}); |
0 commit comments