Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import { ChildProcessSpawner } from "effect/unstable/process";
7import { VcsProcessSpawnError } from "@piku/contracts";
8
9import * as ServerConfig from "../config.ts";
10import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
11import * as VcsProcess from "../vcs/VcsProcess.ts";
12import * as GitHubCli from "./GitHubCli.ts";
13import * as SourceControlDiscovery from "./SourceControlDiscovery.ts";
14import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
15
16const sourceControlProviderRegistryTestLayer = (input: {
17 readonly process: Partial<VcsProcess.VcsProcess["Service"]>;
18}) =>
19 SourceControlProviderRegistry.layer.pipe(
20 Layer.provide(
21 Layer.mergeAll(
22 ServerConfig.layerTest(process.cwd(), {
23 prefix: "piku-source-control-registry-test-",
24 }).pipe(Layer.provide(NodeServices.layer)),
25 Layer.mock(GitHubCli.GitHubCli)({}),
26 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({}),
27 Layer.mock(VcsProcess.VcsProcess)(input.process),
28 ),
29 ),
30 );
31
32const processOutput = (
33 stdout: string,
34 options?: {
35 readonly stderr?: string;
36 readonly exitCode?: ChildProcessSpawner.ExitCode;
37 },
38): VcsProcess.VcsProcessOutput => ({
39 exitCode: options?.exitCode ?? ChildProcessSpawner.ExitCode(0),
40 stdout,
41 stderr: options?.stderr ?? "",
42 stdoutTruncated: false,
43 stderrTruncated: false,
44});
45
46it.effect("reports implemented tools separately from locally available executables", () => {
47 const processMock = {
48 run: (input: VcsProcess.VcsProcessInput) => {
49 if (input.command === "git") {
50 return Effect.succeed(processOutput("git version 2.51.0\n"));
51 }
52 if (input.command === "gh" && input.args[0] === "--version") {
53 return Effect.succeed(processOutput("gh version 2.83.0\n"));
54 }
55 if (input.command === "gh" && input.args.join(" ") === "auth status --json hosts") {
56 return Effect.succeed(
57 processOutput(
58 JSON.stringify({
59 hosts: {
60 "github.com": [
61 {
62 state: "success",
63 active: true,
64 host: "github.com",
65 login: "juliusmarminge",
66 tokenSource: "keyring",
67 gitProtocol: "ssh",
68 },
69 ],
70 },
71 }),
72 ),
73 );
74 }
75 return Effect.fail(
76 new VcsProcessSpawnError({
77 operation: input.operation,
78 command: input.command,
79 cwd: input.cwd,
80 cause: new Error(`${input.command} not found`),
81 }),
82 );
83 },
84 } satisfies Partial<VcsProcess.VcsProcess["Service"]>;
85 const testLayer = SourceControlDiscovery.layer.pipe(
86 Layer.provide(
87 ServerConfig.layerTest(process.cwd(), {
88 prefix: "piku-source-control-discovery-",
89 }),
90 ),
91 Layer.provide(Layer.mock(VcsProcess.VcsProcess)(processMock)),
92 Layer.provide(
93 sourceControlProviderRegistryTestLayer({
94 process: processMock,
95 }),
96 ),
97 Layer.provideMerge(NodeServices.layer),
98 );
99
100 return Effect.gen(function* () {
101 const discovery = yield* SourceControlDiscovery.SourceControlDiscovery;
102 const result = yield* discovery.discover;
103
104 assert.deepStrictEqual(
105 result.versionControlSystems.map((item) => ({
106 kind: item.kind,
107 implemented: item.implemented,
108 status: item.status,
109 })),
110 [
111 { kind: "git", implemented: true, status: "available" },
112 { kind: "jj", implemented: false, status: "missing" },
113 ],
114 );
115 assert.deepStrictEqual(
116 result.sourceControlProviders.map((item) => ({
117 kind: item.kind,
118 status: item.status,
119 auth: item.auth.status,
120 account: item.auth.account,
121 })),
122 [
123 {
124 kind: "github",
125 status: "available",
126 auth: "authenticated",
127 account: Option.some("juliusmarminge"),
128 },
129 ],
130 );
131 }).pipe(Effect.provide(testLayer));
132});
133
134it.effect("probes provider authentication without exposing token details", () => {
135 const processMock = {
136 run: (input: VcsProcess.VcsProcessInput) => {
137 if (input.args[0] === "--version") {
138 return Effect.succeed(processOutput(`${input.command} version test\n`));
139 }
140 if (input.command === "gh" && input.args.join(" ") === "auth status --json hosts") {
141 return Effect.succeed(
142 processOutput(
143 JSON.stringify({
144 hosts: {
145 "github.com": [
146 {
147 state: "success",
148 active: true,
149 host: "github.com",
150 login: "octocat",
151 tokenSource: "keyring",
152 gitProtocol: "ssh",
153 },
154 ],
155 },
156 }),
157 ),
158 );
159 }
160 return Effect.fail(
161 new VcsProcessSpawnError({
162 operation: input.operation,
163 command: input.command,
164 cwd: input.cwd,
165 cause: new Error(`${input.command} not found`),
166 }),
167 );
168 },
169 } satisfies Partial<VcsProcess.VcsProcess["Service"]>;
170 const testLayer = SourceControlDiscovery.layer.pipe(
171 Layer.provide(
172 ServerConfig.layerTest(process.cwd(), {
173 prefix: "piku-source-control-auth-discovery-",
174 }),
175 ),
176 Layer.provide(Layer.mock(VcsProcess.VcsProcess)(processMock)),
177 Layer.provide(
178 sourceControlProviderRegistryTestLayer({
179 process: processMock,
180 }),
181 ),
182 Layer.provideMerge(NodeServices.layer),
183 );
184
185 return Effect.gen(function* () {
186 const discovery = yield* SourceControlDiscovery.SourceControlDiscovery;
187 const result = yield* discovery.discover;
188
189 assert.deepStrictEqual(
190 result.sourceControlProviders.map((item) => ({
191 kind: item.kind,
192 auth: item.auth.status,
193 account: item.auth.account,
194 detail: item.auth.detail,
195 })),
196 [
197 {
198 kind: "github",
199 auth: "authenticated",
200 account: Option.some("octocat"),
201 detail: Option.none(),
202 },
203 ],
204 );
205 }).pipe(Effect.provide(testLayer));
206});