Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitHubSourceControlProvider.test.ts

341 lines8.9 KB

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.GitHubCliShape>) {
28 return GitHubSourceControlProvider.make().pipe(
29 Effect.provide(Layer.mock(GitHubCli.GitHubCli)(github)),
30 );
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("uses gh json listing for non-open change request state queries", () =>
72 Effect.gen(function* () {
73 let executeArgs: ReadonlyArray<string> = [];
74 const provider = yield* makeProvider({
75 execute: (input) => {
76 executeArgs = input.args;
77 return Effect.succeed(
78 processResult(
79 JSON.stringify([
80 {
81 number: 7,
82 title: "Merged work",
83 url: "https://github.com/pingdotgg/t3code/pull/7",
84 baseRefName: "main",
85 headRefName: "feature/merged",
86 state: "merged",
87 updatedAt: "2026-01-02T00:00:00.000Z",
88 },
89 ]),
90 ),
91 );
92 },
93 });
94​
95 const changeRequests = yield* provider.listChangeRequests({
96 cwd: "/repo",
97 headSelector: "feature/merged",
98 state: "all",
99 limit: 10,
100 });
101​
102 assert.deepStrictEqual(executeArgs, [
103 "pr",
104 "list",
105 "--head",
106 "feature/merged",
107 "--state",
108 "all",
109 "--limit",
110 "10",
111 "--json",
112 "number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",
113 ]);
114 assert.strictEqual(changeRequests[0]?.provider, "github");
115 assert.strictEqual(changeRequests[0]?.state, "merged");
116 assert.deepStrictEqual(
117 changeRequests[0]?.updatedAt,
118 Option.some(DateTime.makeUnsafe("2026-01-02T00:00:00.000Z")),
119 );
120 }),
121);
122​
123it.effect("treats empty non-open change request listing output as no results", () =>
124 Effect.gen(function* () {
125 const provider = yield* makeProvider({
126 execute: () => Effect.succeed(processResult("")),
127 });
128​
129 const changeRequests = yield* provider.listChangeRequests({
130 cwd: "/repo",
131 headSelector: "feature/empty",
132 state: "all",
133 limit: 10,
134 });
135​
136 assert.deepStrictEqual(changeRequests, []);
137 }),
138);
139​
140it.effect("creates GitHub PRs through provider-neutral input names", () =>
141 Effect.gen(function* () {
142 let createInput: Parameters<GitHubCli.GitHubCliShape["createPullRequest"]>[0] | null = null;
143 const provider = yield* makeProvider({
144 createPullRequest: (input) => {
145 createInput = input;
146 return Effect.void;
147 },
148 });
149​
150 yield* provider.createChangeRequest({
151 cwd: "/repo",
152 baseRefName: "main",
153 headSelector: "owner:feature/provider",
154 title: "Provider PR",
155 bodyFile: "/tmp/body.md",
156 });
157​
158 assert.deepStrictEqual(createInput, {
159 cwd: "/repo",
160 baseBranch: "main",
161 headSelector: "owner:feature/provider",
162 title: "Provider PR",
163 bodyFile: "/tmp/body.md",
164 });
165 }),
166);
167​
168it("accepts active authenticated GitHub accounts when another account fails", () => {
169 const auth = GitHubSourceControlProvider.discovery.parseAuth(
170 processResult(
171 JSON.stringify({
172 hosts: {
173 "github.com": [
174 {
175 state: "success",
176 active: true,
177 host: "github.com",
178 login: "active-user",
179 tokenSource: "keyring",
180 gitProtocol: "ssh",
181 },
182 {
183 state: "error",
184 active: false,
185 host: "github.com",
186 login: "stale-user",
187 tokenSource: "keyring",
188 gitProtocol: "ssh",
189 error: "The token in keyring is invalid.",
190 },
191 ],
192 },
193 }),
194 ),
195 );
196​
197 assert.deepStrictEqual(
198 {
199 status: auth.status,
200 account: auth.account,
201 host: auth.host,
202 },
203 {
204 status: "authenticated",
205 account: Option.some("active-user"),
206 host: Option.some("github.com"),
207 },
208 );
209});
210​
211it("parses GitHub auth JSON from stdout when stderr has warnings", () => {
212 const auth = GitHubSourceControlProvider.discovery.parseAuth(
213 processResult(
214 JSON.stringify({
215 hosts: {
216 "github.com": [
217 {
218 state: "success",
219 active: true,
220 host: "github.com",
221 login: "active-user",
222 tokenSource: "keyring",
223 gitProtocol: "ssh",
224 },
225 ],
226 },
227 }),
228 { stderr: "warning: ignored diagnostic from gh\n" },
229 ),
230 );
231​
232 assert.deepStrictEqual(
233 {
234 status: auth.status,
235 account: auth.account,
236 host: auth.host,
237 },
238 {
239 status: "authenticated",
240 account: Option.some("active-user"),
241 host: Option.some("github.com"),
242 },
243 );
244});
245​
246it("parses GitHub auth status accounts by host and active state", () => {
247 assert.deepStrictEqual(
248 parseGitHubAuthStatus(
249 JSON.stringify({
250 hosts: {
251 "github.com": [
252 {
253 state: "success",
254 active: true,
255 host: "github.com",
256 login: "active-user",
257 tokenSource: "keyring",
258 gitProtocol: "ssh",
259 },
260 {
261 state: "error",
262 active: false,
263 host: "github.com",
264 login: "stale-user",
265 tokenSource: "keyring",
266 gitProtocol: "ssh",
267 },
268 ],
269 "github.example.test": [
270 {
271 state: "success",
272 active: false,
273 host: "github.example.test",
274 login: "enterprise-user",
275 tokenSource: "keyring",
276 gitProtocol: "ssh",
277 },
278 ],
279 },
280 }),
281 ).accounts,
282 [
283 {
284 host: "github.com",
285 account: "active-user",
286 authenticated: true,
287 active: true,
288 error: null,
289 },
290 {
291 host: "github.com",
292 account: "stale-user",
293 authenticated: false,
294 active: false,
295 error: null,
296 },
297 {
298 host: "github.example.test",
299 account: "enterprise-user",
300 authenticated: true,
301 active: false,
302 error: null,
303 },
304 ],
305 );
306});
307​
308it("reports unauthenticated when GitHub JSON has accounts but none are valid", () => {
309 const auth = GitHubSourceControlProvider.discovery.parseAuth(
310 processResult(
311 JSON.stringify({
312 hosts: {
313 "github.com": [
314 {
315 state: "error",
316 active: true,
317 host: "github.com",
318 login: "stale-user",
319 tokenSource: "keyring",
320 gitProtocol: "ssh",
321 error: "The token in keyring is invalid.",
322 },
323 ],
324 },
325 }),
326 ),
327 );
328​
329 assert.deepStrictEqual(
330 {
331 status: auth.status,
332 host: auth.host,
333 detail: auth.detail,
334 },
335 {
336 status: "unauthenticated",
337 host: Option.some("github.com"),
338 detail: Option.some("The token in keyring is invalid."),
339 },
340 );
341});