Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import * as Schema from "effect/Schema";
3
4import {
5 VcsCreateWorktreeInput,
6 GitPreparePullRequestThreadInput,
7 GitPreparePullRequestThreadResult,
8 GitRunStackedActionResult,
9 GitRunStackedActionInput,
10 GitResolvePullRequestResult,
11} from "./git.ts";
12
13const decodeCreateWorktreeInput = Schema.decodeUnknownSync(VcsCreateWorktreeInput);
14const decodePreparePullRequestThreadInput = Schema.decodeUnknownSync(
15 GitPreparePullRequestThreadInput,
16);
17const decodePreparePullRequestThreadResult = Schema.decodeUnknownSync(
18 GitPreparePullRequestThreadResult,
19);
20const decodeRunStackedActionInput = Schema.decodeUnknownSync(GitRunStackedActionInput);
21const decodeRunStackedActionResult = Schema.decodeUnknownSync(GitRunStackedActionResult);
22const decodeResolvePullRequestResult = Schema.decodeUnknownSync(GitResolvePullRequestResult);
23
24describe("VcsCreateWorktreeInput", () => {
25 it("accepts omitted newRefName for existing-refName worktrees", () => {
26 const parsed = decodeCreateWorktreeInput({
27 cwd: "/repo",
28 refName: "feature/existing",
29 path: "/tmp/worktree",
30 });
31
32 expect(parsed.newRefName).toBeUndefined();
33 expect(parsed.refName).toBe("feature/existing");
34 });
35
36 it("accepts baseRefName metadata for a new worktree ref", () => {
37 const parsed = decodeCreateWorktreeInput({
38 cwd: "/repo",
39 refName: "0123456789abcdef",
40 newRefName: "feature/new",
41 baseRefName: "origin/main",
42 path: "/tmp/worktree",
43 });
44
45 expect(parsed.baseRefName).toBe("origin/main");
46 });
47
48 it("accepts an absolute rootDir on either platform", () => {
49 expect(
50 decodeCreateWorktreeInput({
51 cwd: "/repo",
52 refName: "feature/new",
53 path: null,
54 rootDir: "/custom/worktrees",
55 }).rootDir,
56 ).toBe("/custom/worktrees");
57
58 expect(
59 decodeCreateWorktreeInput({
60 cwd: "C:\\repo",
61 refName: "feature/new",
62 path: null,
63 rootDir: "C:\\worktrees",
64 }).rootDir,
65 ).toBe("C:\\worktrees");
66 });
67
68 it("rejects a relative rootDir that Git would resolve against the repository", () => {
69 for (const rootDir of ["../outside", "worktrees", "./worktrees", "~/worktrees"]) {
70 expect(() =>
71 decodeCreateWorktreeInput({
72 cwd: "/repo",
73 refName: "feature/new",
74 path: null,
75 rootDir,
76 }),
77 ).toThrow();
78 }
79 });
80});
81
82describe("GitPreparePullRequestThreadInput", () => {
83 it("accepts pull request references and mode", () => {
84 const parsed = decodePreparePullRequestThreadInput({
85 cwd: "/repo",
86 reference: "#42",
87 mode: "worktree",
88 });
89
90 expect(parsed.reference).toBe("#42");
91 expect(parsed.mode).toBe("worktree");
92 });
93});
94
95describe("GitPreparePullRequestThreadResult", () => {
96 it("defaults legacy responses to the pull request head", () => {
97 const parsed = decodePreparePullRequestThreadResult({
98 pullRequest: {
99 number: 42,
100 title: "PR threads",
101 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
102 baseBranch: "main",
103 headBranch: "feature/pr-threads",
104 state: "open",
105 },
106 branch: "feature/pr-threads",
107 worktreePath: "/tmp/pr-threads",
108 });
109
110 expect(parsed.isOnPullRequestHead).toBe(true);
111 });
112
113 it("preserves an explicit stale pull request checkout result", () => {
114 const parsed = decodePreparePullRequestThreadResult({
115 pullRequest: {
116 number: 42,
117 title: "PR threads",
118 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
119 baseBranch: "main",
120 headBranch: "feature/pr-threads",
121 state: "open",
122 },
123 branch: "feature/pr-threads",
124 worktreePath: "/tmp/pr-threads",
125 isOnPullRequestHead: false,
126 });
127
128 expect(parsed.isOnPullRequestHead).toBe(false);
129 });
130});
131
132describe("GitResolvePullRequestResult", () => {
133 it("decodes resolved pull request metadata", () => {
134 const parsed = decodeResolvePullRequestResult({
135 pullRequest: {
136 number: 42,
137 title: "PR threads",
138 url: "https://github.com/pingdotgg/codething-mvp/pull/42",
139 baseBranch: "main",
140 headBranch: "feature/pr-threads",
141 state: "open",
142 },
143 });
144
145 expect(parsed.pullRequest.number).toBe(42);
146 expect(parsed.pullRequest.headBranch).toBe("feature/pr-threads");
147 });
148});
149
150describe("GitRunStackedActionInput", () => {
151 it("accepts explicit stacked actions and requires a client-provided actionId", () => {
152 const parsed = decodeRunStackedActionInput({
153 actionId: "action-1",
154 cwd: "/repo",
155 action: "create_pr",
156 });
157
158 expect(parsed.actionId).toBe("action-1");
159 expect(parsed.action).toBe("create_pr");
160 });
161});
162
163describe("GitRunStackedActionResult", () => {
164 it("decodes a server-authored completion toast", () => {
165 const parsed = decodeRunStackedActionResult({
166 action: "commit_push",
167 branch: {
168 status: "created",
169 name: "feature/server-owned-toast",
170 },
171 commit: {
172 status: "created",
173 commitSha: "89abcdef01234567",
174 subject: "feat: move toast state into git manager",
175 },
176 push: {
177 status: "pushed",
178 branch: "feature/server-owned-toast",
179 upstreamBranch: "origin/feature/server-owned-toast",
180 },
181 pr: {
182 status: "skipped_not_requested",
183 },
184 toast: {
185 title: "Pushed 89abcde to origin/feature/server-owned-toast",
186 description: "feat: move toast state into git manager",
187 cta: {
188 kind: "run_action",
189 label: "Create PR",
190 action: {
191 kind: "create_pr",
192 },
193 },
194 },
195 });
196
197 expect(parsed.toast.cta.kind).toBe("run_action");
198 if (parsed.toast.cta.kind === "run_action") {
199 expect(parsed.toast.cta.action.kind).toBe("create_pr");
200 }
201 });
202});