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

AzureDevOpsSourceControlProvider.test.ts

181 lines6.5 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { SourceControlProviderKind } from "@t3tools/contracts";
3import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
4import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
5import { processOutput } from "@t3tools/source-control-testing/TestSourceControlHost";
6import { assert, it, vi } from "@effect/vitest";
7import * as Effect from "effect/Effect";
8import * as Layer from "effect/Layer";
9import * as Option from "effect/Option";
10import { ChildProcessSpawner } from "effect/process";
11​
12import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
13import * as AzureDevOpsSourceControlProvider from "./AzureDevOpsSourceControlProvider.ts";
14​
15const { parseAuth } = AzureDevOpsSourceControlProvider.discovery;
16​
17it("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​
36it.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] of [
57 ["https://acme@dev.azure.com/acme/project/_git/My%20Repo", "My Repo"],
58 ["https://dev.azure.com/acme/project/_git/My%20Repo?view=x#readme", "My Repo"],
59 ["https://acme.visualstudio.com/DefaultCollection/project/_git/My%20Repo/", "My Repo"],
60 ["git@ssh.dev.azure.com:v3/acme/project/My%20Repo", "My Repo"],
61 ["ssh://git@ssh.dev.azure.com/v3/acme/project/My%20Repo", "My Repo"],
62 ["https://dev.azure.com/acme/project/_git/service.git", "service.git"],
63 ["https://dev.azure.com/acme/project/_git/-repo", "-repo"],
64 ["https://dev.azure.com/acme/project/_git/%40.env", null],
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 assert.include(
86 [...(run.mock.calls[0]?.[0].args ?? [])],
87 `--repository=${repository}`,
88 remoteUrl,
89 );
90 }
91 }).pipe(Effect.provide(AzureDevOpsCli.layer.pipe(Layer.provideMerge(support))));
92});
93​
94it.effect(
95 "looks up a branch's pull requests a full page at a time, past forks' same-named branches",
96 () =>
97 Effect.gen(function* () {
98 const provider = yield* AzureDevOpsSourceControlProvider.make;
99 assert.deepStrictEqual(
100 provider.headBranchProbe?.({
101 headSelectors: ["octocat:feature", "feature"],
102 state: "open",
103 }),
104 { headSelectors: ["feature"], limit: 100 },
105 );
106 }).pipe(
107 Effect.provide(
108 Layer.mergeAll(
109 Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}),
110 TestSourceControlHost.layer(),
111 ),
112 ),
113 ),
114);
115​
116it.effect.each([
117 [
118 "creates a missing branch from the fetched head",
119 [],
120 ["fetchRemoteBranch:upstream/feature/x->feature/x"],
121 ],
122 ["fetches without resetting an existing branch", ["feature/x"], ["fetchRemoteTrackingBranch"]],
123] as const)("checks a pull request out with git and %s", ([, localBranches, fetches]) => {
124 const calls: Array<string> = [];
125 return Effect.gen(function* () {
126 const provider = yield* AzureDevOpsSourceControlProvider.make;
127 yield* provider.checkoutChangeRequest({
128 cwd: "/repo",
129 reference: "42",
130 context: {
131 provider: {
132 kind: SourceControlProviderKind.make("azure-devops"),
133 name: "Azure DevOps",
134 baseUrl: "https://dev.azure.com",
135 },
136 remoteName: "upstream",
137 remoteUrl: "https://dev.azure.com/acme/project/_git/repo",
138 },
139 });
140 assert.deepStrictEqual(calls, [...fetches, "switchRef:feature/x"]);
141 }).pipe(
142 Effect.provide(
143 Layer.mergeAll(
144 Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({
145 getPullRequest: () =>
146 Effect.succeed({
147 number: 42,
148 title: "Change",
149 url: "https://dev.azure.com/acme/project/_git/repo/pullrequest/42",
150 baseRefName: "main",
151 headRefName: "feature/x",
152 state: "open",
153 isCrossRepository: false,
154 closedAt: null,
155 mergedAt: null,
156 updatedAt: Option.none(),
157 }),
158 }),
159 TestSourceControlHost.layer({
160 git: {
161 listLocalBranchNames: () => Effect.succeed([...localBranches]),
162 fetchRemoteBranch: (input) => {
163 calls.push(
164 `fetchRemoteBranch:${input.remoteName}/${input.remoteBranch}->${input.localBranch}`,
165 );
166 return Effect.void;
167 },
168 fetchRemoteTrackingBranch: () => {
169 calls.push("fetchRemoteTrackingBranch");
170 return Effect.void;
171 },
172 switchRef: (input) => {
173 calls.push(`switchRef:${input.refName}`);
174 return Effect.succeed({ refName: input.refName });
175 },
176 },
177 }),
178 ),
179 ),
180 );
181});