|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + buildWorkspaceReadyComment, |
| 4 | + buildWorkspaceReadyMetadata, |
| 5 | + buildWorkspaceReadyPresentation, |
| 6 | + type RealizedExecutionWorkspace, |
| 7 | + type RuntimeServiceRef, |
| 8 | +} from "./workspace-runtime.js"; |
| 9 | + |
| 10 | +function workspace( |
| 11 | + overrides: Partial<RealizedExecutionWorkspace> = {}, |
| 12 | +): RealizedExecutionWorkspace { |
| 13 | + return { |
| 14 | + baseCwd: "/repo", |
| 15 | + source: "project_primary", |
| 16 | + projectId: "project-id", |
| 17 | + workspaceId: "project-workspace-id", |
| 18 | + repoUrl: null, |
| 19 | + repoRef: "main", |
| 20 | + strategy: "git_worktree", |
| 21 | + cwd: "/repo/.paperclip/worktrees/PAP-16051", |
| 22 | + branchName: "PAP-16051-workspace-ready-notice", |
| 23 | + worktreePath: "/repo/.paperclip/worktrees/PAP-16051", |
| 24 | + warnings: [], |
| 25 | + created: true, |
| 26 | + ...overrides, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +function runtimeService( |
| 31 | + overrides: Partial<RuntimeServiceRef> = {}, |
| 32 | +): RuntimeServiceRef { |
| 33 | + return { |
| 34 | + id: "service-id", |
| 35 | + companyId: "company-id", |
| 36 | + projectId: "project-id", |
| 37 | + projectWorkspaceId: "project-workspace-id", |
| 38 | + executionWorkspaceId: "execution-workspace-id", |
| 39 | + issueId: "issue-id", |
| 40 | + serviceName: "web", |
| 41 | + status: "running", |
| 42 | + lifecycle: "ephemeral", |
| 43 | + scopeType: "run", |
| 44 | + scopeId: "run-id", |
| 45 | + reuseKey: null, |
| 46 | + command: "pnpm dev", |
| 47 | + cwd: "/repo/.paperclip/worktrees/PAP-16051", |
| 48 | + port: 3100, |
| 49 | + url: "http://localhost:3100", |
| 50 | + provider: "local_process", |
| 51 | + providerRef: null, |
| 52 | + ownerAgentId: "agent-id", |
| 53 | + startedByRunId: "run-id", |
| 54 | + lastUsedAt: "2026-08-01T00:00:00.000Z", |
| 55 | + startedAt: "2026-08-01T00:00:00.000Z", |
| 56 | + stoppedAt: null, |
| 57 | + stopPolicy: null, |
| 58 | + healthStatus: "healthy", |
| 59 | + reused: false, |
| 60 | + ...overrides, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +describe("workspace-ready comment builders", () => { |
| 65 | + it("uses a compact info notice that is collapsed when the workspace has no warnings", () => { |
| 66 | + const input = { workspace: workspace(), runtimeServices: [] }; |
| 67 | + |
| 68 | + expect(buildWorkspaceReadyPresentation(input)).toEqual({ |
| 69 | + kind: "system_notice", |
| 70 | + tone: "info", |
| 71 | + title: "Workspace ready · PAP-16051-workspace-ready-notice", |
| 72 | + density: "compact", |
| 73 | + detailsDefaultOpen: false, |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it("uses a warning notice that is expanded when warnings are present", () => { |
| 78 | + const input = { |
| 79 | + workspace: workspace({ warnings: ["The worktree was restored from a stale reference."] }), |
| 80 | + runtimeServices: [], |
| 81 | + }; |
| 82 | + |
| 83 | + expect(buildWorkspaceReadyPresentation(input)).toMatchObject({ |
| 84 | + tone: "warning", |
| 85 | + detailsDefaultOpen: true, |
| 86 | + }); |
| 87 | + expect(buildWorkspaceReadyMetadata(input).sections.at(-1)).toEqual({ |
| 88 | + title: "Warnings", |
| 89 | + rows: [{ type: "text", text: "The worktree was restored from a stale reference." }], |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("truncates the presentation title to 160 characters", () => { |
| 94 | + const presentation = buildWorkspaceReadyPresentation({ |
| 95 | + workspace: workspace({ branchName: "b".repeat(200) }), |
| 96 | + runtimeServices: [], |
| 97 | + }); |
| 98 | + |
| 99 | + expect(presentation.title).toHaveLength(160); |
| 100 | + expect(presentation.title).toBe(`${`Workspace ready · ${"b".repeat(200)}`.slice(0, 159)}…`); |
| 101 | + }); |
| 102 | + |
| 103 | + it("falls back to the workspace strategy when no branch is available", () => { |
| 104 | + const presentation = buildWorkspaceReadyPresentation({ |
| 105 | + workspace: workspace({ branchName: null, strategy: "project_primary" }), |
| 106 | + runtimeServices: [], |
| 107 | + }); |
| 108 | + |
| 109 | + expect(presentation.title).toBe("Workspace ready · project_primary"); |
| 110 | + }); |
| 111 | + |
| 112 | + it("builds structured workspace and service sections without an empty warnings section", () => { |
| 113 | + const input = { |
| 114 | + workspace: workspace(), |
| 115 | + runtimeServices: [ |
| 116 | + runtimeService(), |
| 117 | + runtimeService({ |
| 118 | + id: "worker-service-id", |
| 119 | + serviceName: "worker", |
| 120 | + url: null, |
| 121 | + reused: true, |
| 122 | + }), |
| 123 | + ], |
| 124 | + }; |
| 125 | + |
| 126 | + expect(buildWorkspaceReadyMetadata(input)).toEqual({ |
| 127 | + version: 1, |
| 128 | + sections: [ |
| 129 | + { |
| 130 | + title: "Workspace", |
| 131 | + rows: [ |
| 132 | + { type: "key_value", label: "Strategy", value: "git_worktree" }, |
| 133 | + { type: "key_value", label: "Branch", value: "PAP-16051-workspace-ready-notice" }, |
| 134 | + { type: "key_value", label: "CWD", value: "/repo/.paperclip/worktrees/PAP-16051" }, |
| 135 | + ], |
| 136 | + }, |
| 137 | + { |
| 138 | + title: "Services", |
| 139 | + rows: [ |
| 140 | + { type: "key_value", label: "web", value: "http://localhost:3100" }, |
| 141 | + { type: "key_value", label: "worker", value: "running (reused)" }, |
| 142 | + ], |
| 143 | + }, |
| 144 | + ], |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it("includes a distinct worktree row and preserves the existing markdown body", () => { |
| 149 | + const input = { |
| 150 | + workspace: workspace({ |
| 151 | + cwd: "/repo/runtime", |
| 152 | + worktreePath: "/repo/.paperclip/worktrees/PAP-16051", |
| 153 | + warnings: ["Warning text"], |
| 154 | + }), |
| 155 | + runtimeServices: [runtimeService({ reused: true })], |
| 156 | + }; |
| 157 | + |
| 158 | + expect(buildWorkspaceReadyMetadata(input).sections[0]).toEqual({ |
| 159 | + title: "Workspace", |
| 160 | + rows: [ |
| 161 | + { type: "key_value", label: "Strategy", value: "git_worktree" }, |
| 162 | + { type: "key_value", label: "Branch", value: "PAP-16051-workspace-ready-notice" }, |
| 163 | + { type: "key_value", label: "CWD", value: "/repo/runtime" }, |
| 164 | + { type: "key_value", label: "Worktree", value: "/repo/.paperclip/worktrees/PAP-16051" }, |
| 165 | + ], |
| 166 | + }); |
| 167 | + expect(buildWorkspaceReadyComment(input)).toBe([ |
| 168 | + "## Workspace Ready", |
| 169 | + "", |
| 170 | + "- Strategy: `git_worktree`", |
| 171 | + "- Branch: `PAP-16051-workspace-ready-notice`", |
| 172 | + "- CWD: `/repo/runtime`", |
| 173 | + "- Worktree: `/repo/.paperclip/worktrees/PAP-16051`", |
| 174 | + "- Warning: Warning text", |
| 175 | + "- Service: web: http://localhost:3100 (reused)", |
| 176 | + ].join("\n")); |
| 177 | + }); |
| 178 | +}); |
0 commit comments