The best way to control Claude Code, Codex and more from any computer or phone

main
ts 180 lines 6.72 KB
Raw
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { SourceControlProviderKind } from "@t3tools/contracts";
3 import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
4 import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
5 import { processOutput } from "@t3tools/source-control-testing/TestSourceControlHost";
6 import { assert, it, vi } from "@effect/vitest";
7 import * as Effect from "effect/Effect";
8 import * as Layer from "effect/Layer";
9 import * as Option from "effect/Option";
10 import { ChildProcessSpawner } from "effect/process";
11
12 import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
13 import * as AzureDevOpsSourceControlProvider from "./AzureDevOpsSourceControlProvider.ts";
14
15 const { parseAuth } = AzureDevOpsSourceControlProvider.discovery;
16
17 it("reads the signed-in Azure account and says when az is signed out", () => {
18 assert.deepStrictEqual(parseAuth(processOutput("azure-user@example.com\n")), {
19 status: "authenticated",
20 account: Option.some("azure-user@example.com"),
21 host: Option.some("dev.azure.com"),
22 detail: Option.none(),
23 });
24 assert.strictEqual(
25 parseAuth(
26 processOutput("", {
27 stderr: "Please run 'az login' to setup account.\n",
28 exitCode: ChildProcessSpawner.ExitCode(1),
29 }),
30 ).status,
31 "unauthenticated",
32 );
33 assert.strictEqual(parseAuth(processOutput("\n")).status, "unknown");
34 });
35
36 it.effect("names the remote's repository when reading the default branch", () => {
37 const run = vi.fn<SourceControlHost.SourceControlHost["Service"]["process"]["run"]>(() =>
38 Effect.succeed(
39 processOutput(
40 JSON.stringify({
41 name: "My Repo",
42 webUrl: "https://dev.azure.com/acme/project/_git/My%20Repo",
43 remoteUrl: "https://dev.azure.com/acme/project/_git/My%20Repo",
44 sshUrl: "git@ssh.dev.azure.com:v3/acme/project/My%20Repo",
45 defaultBranch: "refs/heads/master",
46 }),
47 ),
48 ),
49 );
50 const support = Layer.mergeAll(
51 TestSourceControlHost.layer({ process: { run } }),
52 NodeServices.layer,
53 );
54 return Effect.gen(function* () {
55 const provider = yield* AzureDevOpsSourceControlProvider.make;
56 for (const [remoteUrl, repository, scoped] of [
57 ["https://acme@dev.azure.com/acme/project/_git/My%20Repo", "My Repo", true],
58 ["https://dev.azure.com/acme/project/_git/My%20Repo?view=x#readme", "My Repo", false],
59 ["https://acme.visualstudio.com/DefaultCollection/project/_git/My%20Repo/", "My Repo", false],
60 ["git@ssh.dev.azure.com:v3/acme/project/My%20Repo", "My Repo", true],
61 ["ssh://git@ssh.dev.azure.com/v3/acme/project/My%20Repo", "My Repo", true],
62 ["https://dev.azure.com/acme/project/_git/service.git", "service.git", true],
63 ["https://dev.azure.com/acme/project/_git/-repo", "-repo", true],
64 ["https://dev.azure.com/acme/project/_git/%40.env", null, false],
65 ] as const) {
66 run.mockClear();
67 const branch = yield* provider.getDefaultBranch({
68 cwd: "/repo",
69 context: {
70 provider: {
71 kind: SourceControlProviderKind.make("azure-devops"),
72 name: "Azure DevOps",
73 baseUrl: "https://dev.azure.com",
74 },
75 remoteName: "origin",
76 remoteUrl,
77 },
78 });
79 if (repository === null) {
80 assert.strictEqual(branch, null, remoteUrl);
81 assert.strictEqual(run.mock.calls.length, 0, remoteUrl);
82 continue;
83 }
84 assert.strictEqual(branch, "master", remoteUrl);
85 const args = [...(run.mock.calls.find(([input]) => input.command !== "git")?.[0].args ?? [])];
86 assert.include(args, `--repository=${repository}`, remoteUrl);
87 // The selected remote names the project, not the checkout's primary remote.
88 if (scoped) assert.include(args, "--project=project", remoteUrl);
89 }
90 }).pipe(Effect.provide(AzureDevOpsCli.layer.pipe(Layer.provideMerge(support))));
91 });
92
93 it.effect(
94 "looks up a branch's pull requests a full page at a time, past forks' same-named branches",
95 () =>
96 Effect.gen(function* () {
97 const provider = yield* AzureDevOpsSourceControlProvider.make;
98 assert.deepStrictEqual(
99 provider.headBranchProbe?.({
100 headSelectors: ["octocat:feature", "feature"],
101 state: "open",
102 }),
103 { headSelectors: ["feature"], limit: 100 },
104 );
105 }).pipe(
106 Effect.provide(
107 Layer.mergeAll(
108 Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}),
109 TestSourceControlHost.layer(),
110 ),
111 ),
112 ),
113 );
114
115 it.effect.each([
116 [
117 "creates a missing branch from the fetched head",
118 [],
119 ["fetchRemoteBranch:upstream/feature/x->feature/x"],
120 ],
121 ["fetches without resetting an existing branch", ["feature/x"], ["fetchRemoteTrackingBranch"]],
122 ] as const)("checks a pull request out with git and %s", ([, localBranches, fetches]) => {
123 const calls: Array<string> = [];
124 return Effect.gen(function* () {
125 const provider = yield* AzureDevOpsSourceControlProvider.make;
126 yield* provider.checkoutChangeRequest({
127 cwd: "/repo",
128 reference: "42",
129 context: {
130 provider: {
131 kind: SourceControlProviderKind.make("azure-devops"),
132 name: "Azure DevOps",
133 baseUrl: "https://dev.azure.com",
134 },
135 remoteName: "upstream",
136 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
137 },
138 });
139 assert.deepStrictEqual(calls, [...fetches, "switchRef:feature/x"]);
140 }).pipe(
141 Effect.provide(
142 Layer.mergeAll(
143 Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({
144 getPullRequest: () =>
145 Effect.succeed({
146 number: 42,
147 title: "Change",
148 url: "https://dev.azure.com/acme/project/_git/repo/pullrequest/42",
149 baseRefName: "main",
150 headRefName: "feature/x",
151 state: "open",
152 isCrossRepository: false,
153 closedAt: null,
154 mergedAt: null,
155 updatedAt: Option.none(),
156 }),
157 }),
158 TestSourceControlHost.layer({
159 git: {
160 listLocalBranchNames: () => Effect.succeed([...localBranches]),
161 fetchRemoteBranch: (input) => {
162 calls.push(
163 `fetchRemoteBranch:${input.remoteName}/${input.remoteBranch}->${input.localBranch}`,
164 );
165 return Effect.void;
166 },
167 fetchRemoteTrackingBranch: () => {
168 calls.push("fetchRemoteTrackingBranch");
169 return Effect.void;
170 },
171 switchRef: (input) => {
172 calls.push(`switchRef:${input.refName}`);
173 return Effect.succeed({ refName: input.refName });
174 },
175 },
176 }),
177 ),
178 ),
179 );
180 });