|
1 | 1 | import { assert, it, describe } from "@effect/vitest"; |
2 | 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | +import * as Duration from "effect/Duration"; |
3 | 4 | import * as Effect from "effect/Effect"; |
4 | 5 | import * as Layer from "effect/Layer"; |
| 6 | +import { TestClock } from "effect/testing"; |
5 | 7 | import { ChildProcessSpawner } from "effect/unstable/process"; |
6 | 8 |
|
7 | 9 | import * as VcsProcess from "./VcsProcess.ts"; |
@@ -93,6 +95,54 @@ describe("VcsDriverRegistry", () => { |
93 | 95 | }).pipe(Effect.provide(layer)); |
94 | 96 | }); |
95 | 97 |
|
| 98 | + it.effect("keeps repository detection cached for minutes, not seconds", () => { |
| 99 | + const calls: VcsProcess.VcsProcessInput[] = []; |
| 100 | + const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe( |
| 101 | + Layer.provide(NodeServices.layer), |
| 102 | + Layer.provide( |
| 103 | + Layer.mock(VcsProjectConfig.VcsProjectConfig)({ |
| 104 | + resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"), |
| 105 | + }), |
| 106 | + ), |
| 107 | + Layer.provide( |
| 108 | + Layer.mock(VcsProcess.VcsProcess)({ |
| 109 | + run: (input) => |
| 110 | + Effect.sync(() => { |
| 111 | + calls.push(input); |
| 112 | + const command = normalizeGitArgs(input.args).join(" "); |
| 113 | + if (command === "rev-parse --is-inside-work-tree") { |
| 114 | + return processOutput("true\n"); |
| 115 | + } |
| 116 | + if (command === "rev-parse --show-toplevel") { |
| 117 | + return processOutput("/repo\n"); |
| 118 | + } |
| 119 | + if (command === "rev-parse --git-common-dir") { |
| 120 | + return processOutput("/repo/.git\n"); |
| 121 | + } |
| 122 | + return processOutput(""); |
| 123 | + }), |
| 124 | + }), |
| 125 | + ), |
| 126 | + ); |
| 127 | + |
| 128 | + return Effect.gen(function* () { |
| 129 | + const registry = yield* VcsDriverRegistry.VcsDriverRegistry; |
| 130 | + yield* registry.resolve({ cwd: "/repo", requestedKind: "git" }); |
| 131 | + const callsAfterFirstResolve = calls.length; |
| 132 | + assert.isAbove(callsAfterFirstResolve, 0); |
| 133 | + |
| 134 | + // Detection used to expire after two seconds, so every later VCS call |
| 135 | + // spawned git again for the same checkout. |
| 136 | + yield* TestClock.adjust(Duration.minutes(1)); |
| 137 | + yield* registry.resolve({ cwd: "/repo", requestedKind: "git" }); |
| 138 | + assert.strictEqual(calls.length, callsAfterFirstResolve); |
| 139 | + |
| 140 | + yield* TestClock.adjust(Duration.minutes(6)); |
| 141 | + yield* registry.resolve({ cwd: "/repo", requestedKind: "git" }); |
| 142 | + assert.isAbove(calls.length, callsAfterFirstResolve); |
| 143 | + }).pipe(Effect.provide(Layer.merge(TestClock.layer(), layer))); |
| 144 | + }); |
| 145 | + |
96 | 146 | it.effect("detects a repository created after a negative lookup", () => { |
97 | 147 | let insideWorkTreeChecks = 0; |
98 | 148 | const layer = Layer.effect(VcsDriverRegistry.VcsDriverRegistry, VcsDriverRegistry.make).pipe( |
|
0 commit comments