Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import * as DateTime from "effect/DateTime";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import { ChildProcessSpawner } from "effect/unstable/process";
7
8import * as VcsProcess from "../vcs/VcsProcess.ts";
9import * as GitHubCli from "./GitHubCli.ts";
10import { parseGitHubAuthStatus } from "./gitHubAuthStatus.ts";
11import * as GitHubSourceControlProvider from "./GitHubSourceControlProvider.ts";
12
13const processResult = (
14 stdout: string,
15 options?: {
16 readonly stderr?: string;
17 readonly exitCode?: ChildProcessSpawner.ExitCode;
18 },
19): VcsProcess.VcsProcessOutput => ({
20 exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0),
21 stdout,
22 stderr: options?.stderr ?? "",
23 stdoutTruncated: false,
24 stderrTruncated: false,
25});
26
27function makeProvider(github: Partial<GitHubCli.GitHubCli["Service"]>) {
28 return GitHubSourceControlProvider.make.pipe(
29 Effect.provide(Layer.mock(GitHubCli.GitHubCli)(github)),
30 );
31}
32
33it.effect("maps GitHub PR summaries into provider-neutral change requests", () =>
34 Effect.gen(function* () {
35 const provider = yield* makeProvider({
36 getPullRequest: () =>
37 Effect.succeed({
38 number: 42,
39 title: "Add GitHub provider",
40 url: "https://github.com/pingdotgg/t3code/pull/42",
41 baseRefName: "main",
42 headRefName: "feature/source-control",
43 state: "open",
44 isCrossRepository: true,
45 headRepositoryNameWithOwner: "fork/t3code",
46 headRepositoryOwnerLogin: "fork",
47 }),
48 });
49
50 const changeRequest = yield* provider.getChangeRequest({
51 cwd: "/repo",
52 reference: "42",
53 });
54
55 assert.deepStrictEqual(changeRequest, {
56 provider: "github",
57 number: 42,
58 title: "Add GitHub provider",
59 url: "https://github.com/pingdotgg/t3code/pull/42",
60 baseRefName: "main",
61 headRefName: "feature/source-control",
62 state: "open",
63 updatedAt: Option.none(),
64 isCrossRepository: true,
65 headRepositoryNameWithOwner: "fork/t3code",
66 headRepositoryOwnerLogin: "fork",
67 });
68 }),
69);
70
71it.effect("adds safe request context while retaining GitHub CLI causes", () =>
72 Effect.gen(function* () {
73 const cause = new GitHubCli.GitHubPullRequestNotFoundError({
74 command: "gh",
75 cwd: "/repo",
76 cause: new Error("raw upstream detail that should remain in the cause"),
77 });
78 const provider = yield* makeProvider({
79 getPullRequest: () => Effect.fail(cause),
80 });
81
82 const error = yield* provider
83 .getChangeRequest({
84 cwd: "/repo",
85 reference: "https://user:secret@github.com/pingdotgg/t3code/pull/42?token=secret#diff",
86 })
87 .pipe(Effect.flip);
88
89 assert.deepStrictEqual(
90 {
91 provider: error.provider,
92 operation: error.operation,
93 command: error.command,
94 cwd: error.cwd,
95 reference: error.reference,
96 detail: error.detail,
97 },
98 {
99 provider: "github",
100 operation: "getChangeRequest",
101 command: "gh",
102 cwd: "/repo",
103 reference: "https://github.com/pingdotgg/t3code/pull/42",
104 detail: "Pull request not found. Check the PR number or URL and try again.",
105 },
106 );
107 assert.strictEqual(error.cause, cause);
108 assert.equal(error.message.includes("raw upstream detail"), false);
109 }),
110);
111
112it.effect("uses gh json listing for non-open change request state queries", () =>
113 Effect.gen(function* () {
114 let executeArgs: ReadonlyArray<string> = [];
115 const provider = yield* makeProvider({
116 execute: (input) => {
117 executeArgs = input.args;
118 return Effect.succeed(
119 processResult(
120 JSON.stringify([
121 {
122 number: 7,
123 title: "Merged work",
124 url: "https://github.com/pingdotgg/t3code/pull/7",
125 baseRefName: "main",
126 headRefName: "feature/merged",
127 state: "merged",
128 updatedAt: "2026-01-02T00:00:00.000Z",
129 },
130 ]),
131 ),
132 );
133 },
134 });
135
136 const changeRequests = yield* provider.listChangeRequests({
137 cwd: "/repo",
138 headSelector: "feature/merged",
139 state: "all",
140 limit: 10,
141 });
142
143 assert.deepStrictEqual(executeArgs, [
144 "pr",
145 "list",
146 "--head",
147 "feature/merged",
148 "--state",
149 "all",
150 "--limit",
151 "10",
152 "--json",
153 "number,title,url,baseRefName,headRefName,state,isDraft,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
154 ]);
155 assert.strictEqual(changeRequests[0]?.provider, "github");
156 assert.strictEqual(changeRequests[0]?.state, "merged");
157 assert.deepStrictEqual(
158 changeRequests[0]?.updatedAt,
159 Option.some(DateTime.makeUnsafe("2026-01-02T00:00:00.000Z")),
160 );
161 }),
162);
163
164it.effect("treats empty non-open change request listing output as no results", () =>
165 Effect.gen(function* () {
166 const provider = yield* makeProvider({
167 execute: () => Effect.succeed(processResult("")),
168 });
169
170 const changeRequests = yield* provider.listChangeRequests({
171 cwd: "/repo",
172 headSelector: "feature/empty",
173 state: "all",
174 limit: 10,
175 });
176
177 assert.deepStrictEqual(changeRequests, []);
178 }),
179);
180
181it.effect("creates GitHub PRs through provider-neutral input names", () =>
182 Effect.gen(function* () {
183 let createInput: Parameters<GitHubCli.GitHubCli["Service"]["createPullRequest"]>[0] | null =
184 null;
185 const provider = yield* makeProvider({
186 createPullRequest: (input) => {
187 createInput = input;
188 return Effect.void;
189 },
190 });
191
192 yield* provider.createChangeRequest({
193 cwd: "/repo",
194 baseRefName: "main",
195 headSelector: "owner:feature/provider",
196 title: "Provider PR",
197 bodyFile: "/tmp/body.md",
198 });
199
200 assert.deepStrictEqual(createInput, {
201 cwd: "/repo",
202 baseBranch: "main",
203 headSelector: "owner:feature/provider",
204 title: "Provider PR",
205 bodyFile: "/tmp/body.md",
206 });
207 }),
208);
209
210it("accepts active authenticated GitHub accounts when another account fails", () => {
211 const auth = GitHubSourceControlProvider.discovery.parseAuth(
212 processResult(
213 JSON.stringify({
214 hosts: {
215 "github.com": [
216 {
217 state: "success",
218 active: true,
219 host: "github.com",
220 login: "active-user",
221 tokenSource: "keyring",
222 gitProtocol: "ssh",
223 },
224 {
225 state: "error",
226 active: false,
227 host: "github.com",
228 login: "stale-user",
229 tokenSource: "keyring",
230 gitProtocol: "ssh",
231 error: "The token in keyring is invalid.",
232 },
233 ],
234 },
235 }),
236 ),
237 );
238
239 assert.deepStrictEqual(
240 {
241 status: auth.status,
242 account: auth.account,
243 host: auth.host,
244 },
245 {
246 status: "authenticated",
247 account: Option.some("active-user"),
248 host: Option.some("github.com"),
249 },
250 );
251});
252
253it("parses GitHub auth JSON from stdout when stderr has warnings", () => {
254 const auth = GitHubSourceControlProvider.discovery.parseAuth(
255 processResult(
256 JSON.stringify({
257 hosts: {
258 "github.com": [
259 {
260 state: "success",
261 active: true,
262 host: "github.com",
263 login: "active-user",
264 tokenSource: "keyring",
265 gitProtocol: "ssh",
266 },
267 ],
268 },
269 }),
270 { stderr: "warning: ignored diagnostic from gh\n" },
271 ),
272 );
273
274 assert.deepStrictEqual(
275 {
276 status: auth.status,
277 account: auth.account,
278 host: auth.host,
279 },
280 {
281 status: "authenticated",
282 account: Option.some("active-user"),
283 host: Option.some("github.com"),
284 },
285 );
286});
287
288it("parses GitHub auth status accounts by host and active state", () => {
289 assert.deepStrictEqual(
290 parseGitHubAuthStatus(
291 JSON.stringify({
292 hosts: {
293 "github.com": [
294 {
295 state: "success",
296 active: true,
297 host: "github.com",
298 login: "active-user",
299 tokenSource: "keyring",
300 gitProtocol: "ssh",
301 },
302 {
303 state: "error",
304 active: false,
305 host: "github.com",
306 login: "stale-user",
307 tokenSource: "keyring",
308 gitProtocol: "ssh",
309 },
310 ],
311 "github.example.test": [
312 {
313 state: "success",
314 active: false,
315 host: "github.example.test",
316 login: "enterprise-user",
317 tokenSource: "keyring",
318 gitProtocol: "ssh",
319 },
320 ],
321 },
322 }),
323 ).accounts,
324 [
325 {
326 host: "github.com",
327 account: "active-user",
328 authenticated: true,
329 active: true,
330 error: null,
331 },
332 {
333 host: "github.com",
334 account: "stale-user",
335 authenticated: false,
336 active: false,
337 error: null,
338 },
339 {
340 host: "github.example.test",
341 account: "enterprise-user",
342 authenticated: true,
343 active: false,
344 error: null,
345 },
346 ],
347 );
348});
349
350it("reports unauthenticated when GitHub JSON has accounts but none are valid", () => {
351 const auth = GitHubSourceControlProvider.discovery.parseAuth(
352 processResult(
353 JSON.stringify({
354 hosts: {
355 "github.com": [
356 {
357 state: "error",
358 active: true,
359 host: "github.com",
360 login: "stale-user",
361 tokenSource: "keyring",
362 gitProtocol: "ssh",
363 error: "The token in keyring is invalid.",
364 },
365 ],
366 },
367 }),
368 ),
369 );
370
371 assert.deepStrictEqual(
372 {
373 status: auth.status,
374 host: auth.host,
375 detail: auth.detail,
376 },
377 {
378 status: "unauthenticated",
379 host: Option.some("github.com"),
380 detail: Option.some("The token in keyring is invalid."),
381 },
382 );
383});
384
385it("reports an update hint instead of unauthenticated when gh predates --json", () => {
386 const auth = GitHubSourceControlProvider.discovery.parseAuth(
387 processResult("", {
388 stderr: "unknown flag: --json\n\nUsage: gh auth status [flags]\n",
389 exitCode: ChildProcessSpawner.ExitCode(1),
390 }),
391 );
392
393 assert.strictEqual(auth.status, "unknown");
394 assert.match(
395 Option.getOrElse(auth.detail, () => ""),
396 /2\.81\.0/,
397 );
398});