Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as DateTime from "effect/DateTime";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7
8import { ServerConfig } from "../config.ts";
9import type * as VcsDriver from "../vcs/VcsDriver.ts";
10import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
11import * as VcsProcess from "../vcs/VcsProcess.ts";
12import * as AzureDevOpsCli from "./AzureDevOpsCli.ts";
13import * as BitbucketApi from "./BitbucketApi.ts";
14import * as GitHubCli from "./GitHubCli.ts";
15import * as GitLabCli from "./GitLabCli.ts";
16import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
17
18const TEST_EPOCH = DateTime.makeUnsafe("1970-01-01T00:00:00.000Z");
19
20function makeRegistry(input: {
21 readonly remotes: ReadonlyArray<{
22 readonly name: string;
23 readonly url: string;
24 }>;
25}) {
26 const driver = {
27 listRemotes: () =>
28 Effect.succeed({
29 remotes: input.remotes.map((remote) => ({
30 ...remote,
31 pushUrl: Option.none(),
32 isPrimary: remote.name === "origin",
33 })),
34 freshness: {
35 source: "live-local" as const,
36 observedAt: TEST_EPOCH,
37 expiresAt: Option.none(),
38 },
39 }),
40 } satisfies Partial<VcsDriver.VcsDriverShape>;
41
42 const registryLayer = Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
43 get: () => Effect.succeed(driver as unknown as VcsDriver.VcsDriverShape),
44 resolve: () =>
45 Effect.succeed({
46 kind: "git",
47 repository: {
48 kind: "git",
49 rootPath: "/repo",
50 metadataPath: null,
51 freshness: {
52 source: "live-local" as const,
53 observedAt: TEST_EPOCH,
54 expiresAt: Option.none(),
55 },
56 },
57 driver: driver as unknown as VcsDriver.VcsDriverShape,
58 }),
59 });
60
61 return SourceControlProviderRegistry.make().pipe(
62 Effect.provide(
63 Layer.mergeAll(
64 registryLayer,
65 Layer.mock(AzureDevOpsCli.AzureDevOpsCli)({}),
66 Layer.mock(BitbucketApi.BitbucketApi)({}),
67 Layer.mock(GitHubCli.GitHubCli)({}),
68 Layer.mock(GitLabCli.GitLabCli)({}),
69 Layer.mock(VcsProcess.VcsProcess)({}),
70 ServerConfig.layerTest(process.cwd(), { prefix: "t3-source-control-registry-test-" }).pipe(
71 Layer.provide(NodeServices.layer),
72 ),
73 ),
74 ),
75 );
76}
77
78it.effect("routes GitHub remotes to the GitHub provider", () =>
79 Effect.gen(function* () {
80 const registry = yield* makeRegistry({
81 remotes: [{ name: "origin", url: "git@github.com:pingdotgg/t3code.git" }],
82 });
83
84 const provider = yield* registry.resolve({ cwd: "/repo" });
85
86 assert.strictEqual(provider.kind, "github");
87 }),
88);
89
90it.effect("routes directly by provider kind for remote-first workflows", () =>
91 Effect.gen(function* () {
92 const registry = yield* makeRegistry({
93 remotes: [],
94 });
95
96 const provider = yield* registry.get("github");
97
98 assert.strictEqual(provider.kind, "github");
99 }),
100);
101
102it.effect("routes GitLab remotes to the GitLab provider", () =>
103 Effect.gen(function* () {
104 const registry = yield* makeRegistry({
105 remotes: [{ name: "origin", url: "git@gitlab.com:group/project.git" }],
106 });
107
108 const provider = yield* registry.resolve({ cwd: "/repo" });
109
110 assert.strictEqual(provider.kind, "gitlab");
111 }),
112);
113
114it.effect("routes Bitbucket remotes to the Bitbucket provider", () =>
115 Effect.gen(function* () {
116 const registry = yield* makeRegistry({
117 remotes: [{ name: "origin", url: "git@bitbucket.org:pingdotgg/t3code.git" }],
118 });
119
120 const provider = yield* registry.resolve({ cwd: "/repo" });
121
122 assert.strictEqual(provider.kind, "bitbucket");
123 }),
124);
125
126it.effect("routes Azure DevOps remotes to the Azure DevOps provider", () =>
127 Effect.gen(function* () {
128 const registry = yield* makeRegistry({
129 remotes: [{ name: "origin", url: "https://dev.azure.com/acme/project/_git/repo" }],
130 });
131
132 const provider = yield* registry.resolve({ cwd: "/repo" });
133
134 assert.strictEqual(provider.kind, "azure-devops");
135 }),
136);
137
138it.effect("falls back to a non-origin remote when origin is not configured", () =>
139 Effect.gen(function* () {
140 const registry = yield* makeRegistry({
141 remotes: [{ name: "upstream", url: "https://dev.azure.com/acme/project/_git/repo" }],
142 });
143
144 const provider = yield* registry.resolve({ cwd: "/repo" });
145
146 assert.strictEqual(provider.kind, "azure-devops");
147 }),
148);