Open History to see the last commit
1import { assert, describe, it, vi } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4
5import * as GitManager from "./GitManager.ts";
6import * as GitWorkflowService from "./GitWorkflowService.ts";
7import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
8import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
9
10function makeLayer(input: { readonly detect: VcsDriverRegistry.VcsDriverRegistryShape["detect"] }) {
11 return GitWorkflowService.layer.pipe(
12 Layer.provide(
13 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
14 detect: input.detect,
15 }),
16 ),
17 Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})),
18 Layer.provide(Layer.mock(GitManager.GitManager)({})),
19 );
20}
21
22describe("GitWorkflowService", () => {
23 it.effect("returns an empty local status when no VCS repository is detected", () =>
24 Effect.gen(function* () {
25 const workflow = yield* GitWorkflowService.GitWorkflowService;
26 const status = yield* workflow.localStatus({ cwd: "/not-a-repo" });
27
28 assert.deepStrictEqual(status, {
29 isRepo: false,
30 hasPrimaryRemote: false,
31 isDefaultRef: false,
32 refName: null,
33 hasWorkingTreeChanges: false,
34 workingTree: {
35 files: [],
36 insertions: 0,
37 deletions: 0,
38 },
39 });
40 }).pipe(
41 Effect.provide(
42 makeLayer({
43 detect: () => Effect.succeed(null),
44 }),
45 ),
46 ),
47 );
48
49 it.effect("returns an empty full status when no VCS repository is detected", () =>
50 Effect.gen(function* () {
51 const workflow = yield* GitWorkflowService.GitWorkflowService;
52 const status = yield* workflow.status({ cwd: "/not-a-repo" });
53
54 assert.deepStrictEqual(status, {
55 isRepo: false,
56 hasPrimaryRemote: false,
57 isDefaultRef: false,
58 refName: null,
59 hasWorkingTreeChanges: false,
60 workingTree: {
61 files: [],
62 insertions: 0,
63 deletions: 0,
64 },
65 hasUpstream: false,
66 aheadCount: 0,
67 behindCount: 0,
68 aheadOfDefaultCount: 0,
69 pr: null,
70 });
71 }).pipe(
72 Effect.provide(
73 makeLayer({
74 detect: () => Effect.succeed(null),
75 }),
76 ),
77 ),
78 );
79
80 it.effect("does not call GitManager status methods when no VCS repository is detected", () => {
81 const localStatus = vi.fn();
82 const remoteStatus = vi.fn();
83 const status = vi.fn();
84
85 const testLayer = GitWorkflowService.layer.pipe(
86 Layer.provide(
87 Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
88 detect: () => Effect.succeed(null),
89 }),
90 ),
91 Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)({})),
92 Layer.provide(
93 Layer.mock(GitManager.GitManager)({
94 localStatus,
95 remoteStatus,
96 status,
97 }),
98 ),
99 );
100
101 return Effect.gen(function* () {
102 const workflow = yield* GitWorkflowService.GitWorkflowService;
103 yield* workflow.localStatus({ cwd: "/not-a-repo" });
104 yield* workflow.remoteStatus({ cwd: "/not-a-repo" });
105 yield* workflow.status({ cwd: "/not-a-repo" });
106
107 assert.equal(localStatus.mock.calls.length, 0);
108 assert.equal(remoteStatus.mock.calls.length, 0);
109 assert.equal(status.mock.calls.length, 0);
110 }).pipe(Effect.provide(testLayer));
111 });
112
113 it.effect("returns an empty ref list when no VCS repository is detected", () =>
114 Effect.gen(function* () {
115 const workflow = yield* GitWorkflowService.GitWorkflowService;
116 const refs = yield* workflow.listRefs({ cwd: "/not-a-repo" });
117
118 assert.deepStrictEqual(refs, {
119 refs: [],
120 isRepo: false,
121 hasPrimaryRemote: false,
122 nextCursor: null,
123 totalCount: 0,
124 });
125 }).pipe(
126 Effect.provide(
127 makeLayer({
128 detect: () => Effect.succeed(null),
129 }),
130 ),
131 ),
132 );
133});