|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | +import { |
| 5 | + closeOpenClawAgentDatabaseByPath, |
| 6 | + openOpenClawAgentDatabase, |
| 7 | +} from "../../src/state/openclaw-agent-db.js"; |
| 8 | +import { |
| 9 | + closeOpenClawStateDatabaseByPath, |
| 10 | + openOpenClawStateDatabase, |
| 11 | +} from "../../src/state/openclaw-state-db.js"; |
| 12 | +import { captureFullEnv, withEnvAsync } from "../../src/test-utils/env.js"; |
| 13 | +import { setupAuthTestEnv } from "./auth-wizard.js"; |
| 14 | + |
| 15 | +describe("setupAuthTestEnv", () => { |
| 16 | + it.each([ |
| 17 | + { name: "default", agentSubdir: undefined }, |
| 18 | + { name: "custom", agentSubdir: "custom-agent" }, |
| 19 | + ])("owns the $name agent directory until cleanup", async ({ agentSubdir }) => { |
| 20 | + const previousEnv = { ...process.env }; |
| 21 | + const snapshot = captureFullEnv(); |
| 22 | + const fixture = await setupAuthTestEnv("auth-wrapper-success-", { agentSubdir }); |
| 23 | + try { |
| 24 | + expect(fixture.agentDir).toBe(path.join(fixture.stateDir, agentSubdir ?? "agent")); |
| 25 | + expect((await fs.stat(fixture.agentDir)).isDirectory()).toBe(true); |
| 26 | + expect(process.env.OPENCLAW_AGENT_DIR).toBe(fixture.agentDir); |
| 27 | + expect(process.env.OPENCLAW_STATE_DIR).toBe(fixture.stateDir); |
| 28 | + expect(process.env.OPENCLAW_CONFIG_PATH).toBe(path.join(fixture.stateDir, "openclaw.json")); |
| 29 | + expect(process.env.HOME).toBe(previousEnv.HOME); |
| 30 | + await fixture.cleanup(); |
| 31 | + expect(process.env).toEqual(previousEnv); |
| 32 | + await expect(fs.stat(path.dirname(fixture.stateDir))).rejects.toMatchObject({ |
| 33 | + code: "ENOENT", |
| 34 | + }); |
| 35 | + } finally { |
| 36 | + try { |
| 37 | + await fixture.cleanup(); |
| 38 | + } finally { |
| 39 | + snapshot.restore(); |
| 40 | + } |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + it.each(["absent", "set"])( |
| 45 | + "releases state after agent mkdir fails with agent override %s", |
| 46 | + async (prior) => { |
| 47 | + await withEnvAsync( |
| 48 | + { |
| 49 | + OPENCLAW_AGENT_DIR: |
| 50 | + prior === "set" ? path.join(process.env.HOME!, "prior-agent") : undefined, |
| 51 | + }, |
| 52 | + async () => { |
| 53 | + const previousEnv = { ...process.env }; |
| 54 | + const snapshot = captureFullEnv(); |
| 55 | + const failure = new Error("agent mkdir failed"); |
| 56 | + let root: string | undefined; |
| 57 | + let shared: ReturnType<typeof openOpenClawStateDatabase> | undefined; |
| 58 | + let agent: ReturnType<typeof openOpenClawAgentDatabase> | undefined; |
| 59 | + const mkdir = fs.mkdir; |
| 60 | + const mkdirSpy = vi.spyOn(fs, "mkdir").mockImplementation(async (...args) => { |
| 61 | + if ( |
| 62 | + typeof args[0] === "string" && |
| 63 | + args[0] === process.env.OPENCLAW_AGENT_DIR && |
| 64 | + path.basename(args[0]) === "faulting-agent" |
| 65 | + ) { |
| 66 | + const stateDir = path.dirname(args[0]); |
| 67 | + root = path.dirname(stateDir); |
| 68 | + expect((await fs.stat(stateDir)).isDirectory()).toBe(true); |
| 69 | + const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir }; |
| 70 | + shared = openOpenClawStateDatabase({ env }); |
| 71 | + agent = openOpenClawAgentDatabase({ agentId: "main", env }); |
| 72 | + throw failure; |
| 73 | + } |
| 74 | + return mkdir(...args); |
| 75 | + }); |
| 76 | + try { |
| 77 | + await expect( |
| 78 | + setupAuthTestEnv("auth-wrapper-failure-", { agentSubdir: "faulting-agent" }), |
| 79 | + ).rejects.toBe(failure); |
| 80 | + expect.soft(process.env).toEqual(previousEnv); |
| 81 | + expect.soft(shared?.db.isOpen).toBe(false); |
| 82 | + expect.soft(agent?.db.isOpen).toBe(false); |
| 83 | + expect(root).toBeDefined(); |
| 84 | + await expect(fs.stat(root!)).rejects.toMatchObject({ code: "ENOENT" }); |
| 85 | + } finally { |
| 86 | + mkdirSpy.mockRestore(); |
| 87 | + try { |
| 88 | + if (agent) { |
| 89 | + closeOpenClawAgentDatabaseByPath(agent.path); |
| 90 | + } |
| 91 | + if (shared) { |
| 92 | + closeOpenClawStateDatabaseByPath(shared.path); |
| 93 | + } |
| 94 | + } finally { |
| 95 | + snapshot.restore(); |
| 96 | + if (root) { |
| 97 | + await fs.rm(root, { recursive: true, force: true }); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + }, |
| 102 | + ); |
| 103 | + }, |
| 104 | + ); |
| 105 | +}); |
0 commit comments