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

GitWorkflowService.test.ts

271 lines8.7 KB

1import { assert, describe, expect, it, vi } 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";
6​
7import { VcsRepositoryDetectionError } from "@t3tools/contracts";
8​
9import * as NodeServices from "@effect/platform-node/NodeServices";
10import * as FileSystem from "effect/FileSystem";
11​
12import * as ServerConfig from "../config.ts";
13import * as VcsProcess from "../vcs/VcsProcess.ts";
14import * as GitManager from "./GitManager.ts";
15import * as GitWorkflowService from "./GitWorkflowService.ts";
16import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
17import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
18​
19function layer(input: {
20 readonly detect: VcsDriverRegistry.VcsDriverRegistry["Service"]["detect"];
21}) {
22 return GitWorkflowService.layer.pipe(
23 Layer.provide(
24 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
25 detect: input.detect,
26 }),
27 ),
28 Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})),
29 Layer.provide(Layer.mock(GitManager.GitManager)({})),
30 );
32​
33describe("GitWorkflowService", () => {
34 it.effect("reports a non-Git VCS repository as not a Git repository", () =>
35 Effect.gen(function* () {
36 const workflow = yield* GitWorkflowService.GitWorkflowService;
37 const isRepository = yield* workflow.isRepository("/jj-repo");
38​
39 assert.equal(isRepository, false);
40 }).pipe(
41 Effect.provide(
42 layer({
43 detect: () =>
44 Effect.succeed({
45 kind: "jj",
46 repository: {
47 kind: "jj",
48 rootPath: "/jj-repo",
49 metadataPath: "/jj-repo/.jj",
50 freshness: {
51 source: "live-local",
52 observedAt: DateTime.makeUnsafe("2026-01-01T00:00:00.000Z"),
53 expiresAt: Option.none(),
54 },
55 },
56 driver: {} as VcsDriverRegistry.VcsDriverHandle["driver"],
57 }),
58 }),
59 ),
60 ),
61 );
62​
63 it.effect("returns an empty local status when no VCS repository is detected", () =>
64 Effect.gen(function* () {
65 const workflow = yield* GitWorkflowService.GitWorkflowService;
66 const status = yield* workflow.localStatus({ cwd: "/not-a-repo" });
67​
68 assert.deepStrictEqual(status, {
69 isRepo: false,
70 hasPrimaryRemote: false,
71 isDefaultRef: false,
72 refName: null,
73 hasWorkingTreeChanges: false,
74 workingTree: {
75 files: [],
76 insertions: 0,
77 deletions: 0,
78 },
79 });
80 }).pipe(
81 Effect.provide(
82 layer({
83 detect: () => Effect.succeed(null),
84 }),
85 ),
86 ),
87 );
88​
89 it.effect("returns an empty full status when no VCS repository is detected", () =>
90 Effect.gen(function* () {
91 const workflow = yield* GitWorkflowService.GitWorkflowService;
92 const status = yield* workflow.status({ cwd: "/not-a-repo" });
93​
94 assert.deepStrictEqual(status, {
95 isRepo: false,
96 hasPrimaryRemote: false,
97 isDefaultRef: false,
98 refName: null,
99 hasWorkingTreeChanges: false,
100 workingTree: {
101 files: [],
102 insertions: 0,
103 deletions: 0,
104 },
105 hasUpstream: false,
106 aheadCount: 0,
107 behindCount: 0,
108 aheadOfDefaultCount: 0,
109 pr: null,
110 });
111 }).pipe(
112 Effect.provide(
113 layer({
114 detect: () => Effect.succeed(null),
115 }),
116 ),
117 ),
118 );
119​
120 it.effect("does not call GitManager status methods when no VCS repository is detected", () => {
121 const localStatus = vi.fn();
122 const remoteStatus = vi.fn();
123 const status = vi.fn();
124​
125 const layerTest = GitWorkflowService.layer.pipe(
126 Layer.provide(
127 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
128 detect: () => Effect.succeed(null),
129 }),
130 ),
131 Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})),
132 Layer.provide(
133 Layer.mock(GitManager.GitManager)({
134 localStatus,
135 remoteStatus,
136 status,
137 }),
138 ),
139 );
140​
141 return Effect.gen(function* () {
142 const workflow = yield* GitWorkflowService.GitWorkflowService;
143 yield* workflow.localStatus({ cwd: "/not-a-repo" });
144 yield* workflow.remoteStatus({ cwd: "/not-a-repo" });
145 yield* workflow.status({ cwd: "/not-a-repo" });
146​
147 assert.equal(localStatus.mock.calls.length, 0);
148 assert.equal(remoteStatus.mock.calls.length, 0);
149 assert.equal(status.mock.calls.length, 0);
150 }).pipe(Effect.provide(layerTest));
151 });
152​
153 it.effect("returns an empty ref list when no VCS repository is detected", () =>
154 Effect.gen(function* () {
155 const workflow = yield* GitWorkflowService.GitWorkflowService;
156 const refs = yield* workflow.listRefs({ cwd: "/not-a-repo" });
157​
158 assert.deepStrictEqual(refs, {
159 refs: [],
160 isRepo: false,
161 hasPrimaryRemote: false,
162 nextCursor: null,
163 totalCount: 0,
164 });
165 }).pipe(
166 Effect.provide(
167 layer({
168 detect: () => Effect.succeed(null),
169 }),
170 ),
171 ),
172 );
173​
174 it.effect("structures workflow detection failures without exposing upstream details", () => {
175 const cause = new VcsRepositoryDetectionError({
176 operation: "VcsDriverRegistry.detect",
177 cwd: "/repo",
178 detail: "upstream detail must stay in the cause chain",
179 });
180​
181 return Effect.gen(function* () {
182 const workflow = yield* GitWorkflowService.GitWorkflowService;
183 const error = yield* workflow.status({ cwd: "/repo" }).pipe(Effect.flip);
184​
185 expect(error).toMatchObject({
186 _tag: "GitManagerError",
187 operation: "GitWorkflowService.status",
188 cwd: "/repo",
189 detail: "Failed to detect a VCS repository for this Git workflow.",
190 });
191 expect(error.message).not.toContain(cause.detail);
192 }).pipe(
193 Effect.provide(
194 layer({
195 detect: () => Effect.fail(cause),
196 }),
197 ),
198 );
199 });
200​
201 it.effect("structures command detection failures without exposing upstream details", () => {
202 const cause = new VcsRepositoryDetectionError({
203 operation: "VcsDriverRegistry.detect",
204 cwd: "/repo",
205 detail: "upstream command detail must stay in the cause chain",
206 });
207​
208 return Effect.gen(function* () {
209 const workflow = yield* GitWorkflowService.GitWorkflowService;
210 const error = yield* workflow.listRefs({ cwd: "/repo" }).pipe(Effect.flip);
211​
212 expect(error).toMatchObject({
213 _tag: "GitCommandError",
214 operation: "GitWorkflowService.listRefs",
215 command: "vcs-route",
216 cwd: "/repo",
217 detail: "Failed to detect a VCS repository for this Git command.",
218 });
219 expect(error.message).not.toContain(cause.detail);
220 }).pipe(
221 Effect.provide(
222 layer({
223 detect: () => Effect.fail(cause),
224 }),
225 ),
226 );
227 });
228​
229 it.effect("finds a name among the refs of every namespace", () =>
230 Effect.gen(function* () {
231 const fileSystem = yield* FileSystem.FileSystem;
232 const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-ref-named-" });
233 const workflow = yield* GitWorkflowService.GitWorkflowService;
234 const driver = yield* GitVcsDriver.GitVcsDriver;
235 for (const args of [
236 ["init", "-b", "main"],
237 ["-c", "user.email=t@t", "-c", "user.name=t", "commit", "--allow-empty", "-m", "init"],
238 ["update-ref", "refs/remotes/upstream/feature/topic", "HEAD"],
239 ["symbolic-ref", "refs/remotes/origin/alias", "refs/heads/main"],
240 ["update-ref", "refs/mirror/origin/mirrored", "HEAD"],
241 ]) {
242 yield* driver.execute({ operation: "test.setupRepo", cwd, args });
243 }
244 const found = yield* Effect.forEach(
245 ["main", "feature/topic", "alias", "mirrored", "t3/renamed", "feature/*"],
246 (refName) => workflow.hasRefNamed({ cwd, refName }),
247 );
248 assert.deepEqual(found, [true, true, true, true, false, false]);
249 }).pipe(
250 Effect.scoped,
251 Effect.provide(
252 GitWorkflowService.layer.pipe(
253 Layer.provide(
254 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
255 resolve: () => Effect.succeed({ kind: "git" } as VcsDriverRegistry.VcsDriverHandle),
256 }),
257 ),
258 Layer.provide(Layer.mock(GitManager.GitManager)({})),
259 Layer.provideMerge(GitVcsDriver.layer),
260 Layer.provideMerge(VcsProcess.layer),
261 Layer.provideMerge(
262 ServerConfig.layerTest(process.cwd(), { prefix: "t3-ref-named-" }).pipe(
263 Layer.provide(NodeServices.layer),
264 ),
265 ),
266 Layer.provideMerge(NodeServices.layer),
267 ),
268 ),
269 ),
270 );
271});