|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | +import { afterEach, expect, it } from "vitest"; |
| 4 | +import { useAutoCleanupTempDirTracker } from "../../../test/helpers/temp-dir.js"; |
| 5 | +import { insertRegistryWorktree } from "../../agents/worktrees/registry.js"; |
| 6 | +import { closeOpenClawAgentDatabasesForTest } from "../../state/openclaw-agent-db.js"; |
| 7 | +import { closeOpenClawStateDatabaseForTest } from "../../state/openclaw-state-db.js"; |
| 8 | +import type { OpenClawConfig } from "../types.openclaw.js"; |
| 9 | +import { loadSessionEntry, replaceSessionEntry } from "./session-accessor.js"; |
| 10 | +import { migrateManagedWorktreeCanonicalWorkspaces } from "./worktree-workspace-migration.js"; |
| 11 | + |
| 12 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 13 | + |
| 14 | +afterEach(() => { |
| 15 | + closeOpenClawAgentDatabasesForTest(); |
| 16 | + closeOpenClawStateDatabaseForTest(); |
| 17 | +}); |
| 18 | + |
| 19 | +it("backfills a nested requested workspace once instead of using the agent default", async () => { |
| 20 | + const root = tempDirs.make("openclaw-worktree-workspace-migration-"); |
| 21 | + const stateDir = path.join(root, "state"); |
| 22 | + const repoRoot = path.join(root, "repo"); |
| 23 | + const agentWorkspace = path.join(repoRoot, "agent-default"); |
| 24 | + const requestedWorkspace = path.join(repoRoot, "packages", "app"); |
| 25 | + const worktreeRoot = path.join(stateDir, "worktrees", "legacy"); |
| 26 | + const spawnedCwd = path.join(worktreeRoot, "packages", "app"); |
| 27 | + await Promise.all([ |
| 28 | + fs.mkdir(agentWorkspace, { recursive: true }), |
| 29 | + fs.mkdir(requestedWorkspace, { recursive: true }), |
| 30 | + fs.mkdir(spawnedCwd, { recursive: true }), |
| 31 | + ]); |
| 32 | + const env = { ...process.env, OPENCLAW_STATE_DIR: stateDir }; |
| 33 | + const storePath = path.join(stateDir, "agents", "main", "sessions", "sessions.json"); |
| 34 | + const sessionKey = "agent:main:dashboard:legacy-worktree"; |
| 35 | + const cfg: OpenClawConfig = { |
| 36 | + agents: { list: [{ id: "main", default: true, workspace: agentWorkspace }] }, |
| 37 | + session: { store: storePath }, |
| 38 | + }; |
| 39 | + insertRegistryWorktree(env, { |
| 40 | + id: "legacy", |
| 41 | + name: "legacy", |
| 42 | + repoFingerprint: "0123456789abcdef", |
| 43 | + repoRoot, |
| 44 | + path: worktreeRoot, |
| 45 | + branch: "openclaw/legacy", |
| 46 | + baseRef: "HEAD", |
| 47 | + ownerKind: "session", |
| 48 | + ownerId: sessionKey, |
| 49 | + createdAt: 1, |
| 50 | + lastActiveAt: 1, |
| 51 | + }); |
| 52 | + await replaceSessionEntry( |
| 53 | + { agentId: "main", env, sessionKey, storePath }, |
| 54 | + { |
| 55 | + sessionId: "legacy-session", |
| 56 | + updatedAt: 10, |
| 57 | + spawnedCwd, |
| 58 | + worktree: { id: "legacy", branch: "openclaw/legacy", repoRoot }, |
| 59 | + }, |
| 60 | + ); |
| 61 | + |
| 62 | + const runMigration = async () => |
| 63 | + await migrateManagedWorktreeCanonicalWorkspaces({ |
| 64 | + agentId: "main", |
| 65 | + cfg, |
| 66 | + env, |
| 67 | + storePath, |
| 68 | + }); |
| 69 | + |
| 70 | + await runMigration(); |
| 71 | + const first = loadSessionEntry({ agentId: "main", env, sessionKey, storePath }); |
| 72 | + expect(first?.worktree?.canonicalWorkspaceDir).toBe(requestedWorkspace); |
| 73 | + expect(first?.updatedAt).toBe(10); |
| 74 | + |
| 75 | + await runMigration(); |
| 76 | + expect(loadSessionEntry({ agentId: "main", env, sessionKey, storePath })).toEqual(first); |
| 77 | +}); |
0 commit comments