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

GitWorkflowService.test.ts

223 lines6.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 GitManager from "./GitManager.ts";
10import * as GitWorkflowService from "./GitWorkflowService.ts";
11import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
12import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
13​
14function makeLayer(input: {
15 readonly detect: VcsDriverRegistry.VcsDriverRegistry["Service"]["detect"];
16}) {
17 return GitWorkflowService.layer.pipe(
18 Layer.provide(
19 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
20 detect: input.detect,
21 }),
22 ),
23 Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})),
24 Layer.provide(Layer.mock(GitManager.GitManager)({})),
25 );
27​
28describe("GitWorkflowService", () => {
29 it.effect("reports a non-Git VCS repository as not a Git repository", () =>
30 Effect.gen(function* () {
31 const workflow = yield* GitWorkflowService.GitWorkflowService;
32 const isRepository = yield* workflow.isRepository("/jj-repo");
33​
34 assert.equal(isRepository, false);
35 }).pipe(
36 Effect.provide(
37 makeLayer({
38 detect: () =>
39 Effect.succeed({
40 kind: "jj",
41 repository: {
42 kind: "jj",
43 rootPath: "/jj-repo",
44 metadataPath: "/jj-repo/.jj",
45 freshness: {
46 source: "live-local",
47 observedAt: DateTime.makeUnsafe("2026-01-01T00:00:00.000Z"),
48 expiresAt: Option.none(),
49 },
50 },
51 driver: {} as VcsDriverRegistry.VcsDriverHandle["driver"],
52 }),
53 }),
54 ),
55 ),
56 );
57​
58 it.effect("returns an empty local status when no VCS repository is detected", () =>
59 Effect.gen(function* () {
60 const workflow = yield* GitWorkflowService.GitWorkflowService;
61 const status = yield* workflow.localStatus({ cwd: "/not-a-repo" });
62​
63 assert.deepStrictEqual(status, {
64 isRepo: false,
65 hasPrimaryRemote: false,
66 isDefaultRef: false,
67 refName: null,
68 hasWorkingTreeChanges: false,
69 workingTree: {
70 files: [],
71 insertions: 0,
72 deletions: 0,
73 },
74 });
75 }).pipe(
76 Effect.provide(
77 makeLayer({
78 detect: () => Effect.succeed(null),
79 }),
80 ),
81 ),
82 );
83​
84 it.effect("returns an empty full status when no VCS repository is detected", () =>
85 Effect.gen(function* () {
86 const workflow = yield* GitWorkflowService.GitWorkflowService;
87 const status = yield* workflow.status({ cwd: "/not-a-repo" });
88​
89 assert.deepStrictEqual(status, {
90 isRepo: false,
91 hasPrimaryRemote: false,
92 isDefaultRef: false,
93 refName: null,
94 hasWorkingTreeChanges: false,
95 workingTree: {
96 files: [],
97 insertions: 0,
98 deletions: 0,
99 },
100 hasUpstream: false,
101 aheadCount: 0,
102 behindCount: 0,
103 aheadOfDefaultCount: 0,
104 pr: null,
105 });
106 }).pipe(
107 Effect.provide(
108 makeLayer({
109 detect: () => Effect.succeed(null),
110 }),
111 ),
112 ),
113 );
114​
115 it.effect("does not call GitManager status methods when no VCS repository is detected", () => {
116 const localStatus = vi.fn();
117 const remoteStatus = vi.fn();
118 const status = vi.fn();
119​
120 const testLayer = GitWorkflowService.layer.pipe(
121 Layer.provide(
122 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
123 detect: () => Effect.succeed(null),
124 }),
125 ),
126 Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})),
127 Layer.provide(
128 Layer.mock(GitManager.GitManager)({
129 localStatus,
130 remoteStatus,
131 status,
132 }),
133 ),
134 );
135​
136 return Effect.gen(function* () {
137 const workflow = yield* GitWorkflowService.GitWorkflowService;
138 yield* workflow.localStatus({ cwd: "/not-a-repo" });
139 yield* workflow.remoteStatus({ cwd: "/not-a-repo" });
140 yield* workflow.status({ cwd: "/not-a-repo" });
141​
142 assert.equal(localStatus.mock.calls.length, 0);
143 assert.equal(remoteStatus.mock.calls.length, 0);
144 assert.equal(status.mock.calls.length, 0);
145 }).pipe(Effect.provide(testLayer));
146 });
147​
148 it.effect("returns an empty ref list when no VCS repository is detected", () =>
149 Effect.gen(function* () {
150 const workflow = yield* GitWorkflowService.GitWorkflowService;
151 const refs = yield* workflow.listRefs({ cwd: "/not-a-repo" });
152​
153 assert.deepStrictEqual(refs, {
154 refs: [],
155 isRepo: false,
156 hasPrimaryRemote: false,
157 nextCursor: null,
158 totalCount: 0,
159 });
160 }).pipe(
161 Effect.provide(
162 makeLayer({
163 detect: () => Effect.succeed(null),
164 }),
165 ),
166 ),
167 );
168​
169 it.effect("structures workflow detection failures without exposing upstream details", () => {
170 const cause = new VcsRepositoryDetectionError({
171 operation: "VcsDriverRegistry.detect",
172 cwd: "/repo",
173 detail: "upstream detail must stay in the cause chain",
174 });
175​
176 return Effect.gen(function* () {
177 const workflow = yield* GitWorkflowService.GitWorkflowService;
178 const error = yield* workflow.status({ cwd: "/repo" }).pipe(Effect.flip);
179​
180 expect(error).toMatchObject({
181 _tag: "GitManagerError",
182 operation: "GitWorkflowService.status",
183 cwd: "/repo",
184 detail: "Failed to detect a VCS repository for this Git workflow.",
185 });
186 expect(error.message).not.toContain(cause.detail);
187 }).pipe(
188 Effect.provide(
189 makeLayer({
190 detect: () => Effect.fail(cause),
191 }),
192 ),
193 );
194 });
195​
196 it.effect("structures command detection failures without exposing upstream details", () => {
197 const cause = new VcsRepositoryDetectionError({
198 operation: "VcsDriverRegistry.detect",
199 cwd: "/repo",
200 detail: "upstream command detail must stay in the cause chain",
201 });
202​
203 return Effect.gen(function* () {
204 const workflow = yield* GitWorkflowService.GitWorkflowService;
205 const error = yield* workflow.listRefs({ cwd: "/repo" }).pipe(Effect.flip);
206​
207 expect(error).toMatchObject({
208 _tag: "GitCommandError",
209 operation: "GitWorkflowService.listRefs",
210 command: "vcs-route",
211 cwd: "/repo",
212 detail: "Failed to detect a VCS repository for this Git command.",
213 });
214 expect(error.message).not.toContain(cause.detail);
215 }).pipe(
216 Effect.provide(
217 makeLayer({
218 detect: () => Effect.fail(cause),
219 }),
220 ),
221 );
222 });
223});