From e07cc3e3aeca4232a8b3e0e2251977ab3000f4ca Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 2 May 2026 01:57:03 -0700 Subject: [PATCH] Use effect file system and node services in VCS tests - Replace ad hoc temp-dir handling with scoped FileSystem APIs - Provide NodeServices in VCS driver and status broadcaster tests - Normalize CWDs through FileSystem realpath --- apps/server/src/vcs/JjVcsDriver.test.ts | 128 +++++++------- apps/server/src/vcs/JjVcsDriver.ts | 48 +++--- apps/server/src/vcs/VcsDriverRegistry.test.ts | 4 + .../src/vcs/VcsStatusBroadcaster.test.ts | 161 +++++++++--------- apps/server/src/vcs/VcsStatusBroadcaster.ts | 23 +-- 5 files changed, 183 insertions(+), 181 deletions(-) diff --git a/apps/server/src/vcs/JjVcsDriver.test.ts b/apps/server/src/vcs/JjVcsDriver.test.ts index ec038e2ec7..83f8fe0cc2 100644 --- a/apps/server/src/vcs/JjVcsDriver.test.ts +++ b/apps/server/src/vcs/JjVcsDriver.test.ts @@ -1,5 +1,3 @@ -import { spawnSync } from "node:child_process"; - import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it } from "@effect/vitest"; import { Effect, FileSystem, Layer, Path, PlatformError } from "effect"; @@ -28,8 +26,6 @@ const processOutput = (stdout: string, exitCode = 0, stderr = ""): VcsProcessOut stderrTruncated: false, }); -const hasJj = spawnSync("jj", ["--version"], { stdio: "ignore" }).status === 0; - const runJj = (cwd: string, args: ReadonlyArray) => Effect.gen(function* () { const process = yield* VcsProcess; @@ -44,34 +40,28 @@ const runJj = (cwd: string, args: ReadonlyArray) => type JjContractError = PlatformError.PlatformError | VcsError; -if (hasJj) { - runVcsDriverContractSuite({ - name: "JJ", - kind: "jj", - layer: JjContractLayer, - fixture: { - createRepo: (cwd) => runJj(cwd, ["git", "init", "--colocate"]), - writeFile: (cwd, relativePath, contents) => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - const absolutePath = path.join(cwd, relativePath); - yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true }); - yield* fileSystem.writeFileString(absolutePath, contents); - }), - ignorePath: (cwd, pattern) => - Effect.gen(function* () { - const fileSystem = yield* FileSystem.FileSystem; - const path = yield* Path.Path; - yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), `${pattern}\n`); - }), - }, - }); -} else { - it("skips the JJ VCS driver contract when jj is not installed", () => { - assert.isFalse(hasJj); - }); -} +runVcsDriverContractSuite({ + name: "JJ", + kind: "jj", + layer: JjContractLayer, + fixture: { + createRepo: (cwd) => runJj(cwd, ["git", "init", "--colocate"]), + writeFile: (cwd, relativePath, contents) => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const absolutePath = path.join(cwd, relativePath); + yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true }); + yield* fileSystem.writeFileString(absolutePath, contents); + }), + ignorePath: (cwd, pattern) => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), `${pattern}\n`); + }), + }, +}); describe("JjVcsDriver", () => { it.effect("detects repository identity with jj root", () => { @@ -87,13 +77,16 @@ describe("JjVcsDriver", () => { assert.deepStrictEqual(commandCalls(calls), [["jj", "--no-pager", "root"]]); }).pipe( Effect.provide( - Layer.mock(VcsProcess)({ - run: (input) => - Effect.sync(() => { - calls.push(input); - return processOutput("/repo\n"); - }), - }), + Layer.mergeAll( + Layer.mock(VcsProcess)({ + run: (input) => + Effect.sync(() => { + calls.push(input); + return processOutput("/repo\n"); + }), + }), + NodeServices.layer, + ), ), ); }); @@ -109,13 +102,16 @@ describe("JjVcsDriver", () => { assert.deepStrictEqual(observedInput?.args, ["--no-pager", "file", "list"]); }).pipe( Effect.provide( - Layer.mock(VcsProcess)({ - run: (input) => - Effect.sync(() => { - observedInput = input; - return processOutput("README.md\nsrc/index.ts\n"); - }), - }), + Layer.mergeAll( + Layer.mock(VcsProcess)({ + run: (input) => + Effect.sync(() => { + observedInput = input; + return processOutput("README.md\nsrc/index.ts\n"); + }), + }), + NodeServices.layer, + ), ), ); }); @@ -144,16 +140,19 @@ describe("JjVcsDriver", () => { assert.equal(calls[1]?.stdin, "keep.ts\0debug.log\0src/index.ts\0"); }).pipe( Effect.provide( - Layer.mock(VcsProcess)({ - run: (input) => - Effect.sync(() => { - calls.push(input); - if (input.command === "git" && input.args.includes("check-ignore")) { - return processOutput("debug.log\0"); - } - return processOutput(""); - }), - }), + Layer.mergeAll( + Layer.mock(VcsProcess)({ + run: (input) => + Effect.sync(() => { + calls.push(input); + if (input.command === "git" && input.args.includes("check-ignore")) { + return processOutput("debug.log\0"); + } + return processOutput(""); + }), + }), + NodeServices.layer, + ), ), ); }); @@ -178,13 +177,16 @@ describe("JjVcsDriver", () => { }); }).pipe( Effect.provide( - Layer.mock(VcsProcess)({ - run: (input) => - Effect.sync(() => { - observedEnv = input.env; - return processOutput(""); - }), - }), + Layer.mergeAll( + Layer.mock(VcsProcess)({ + run: (input) => + Effect.sync(() => { + observedEnv = input.env; + return processOutput(""); + }), + }), + NodeServices.layer, + ), ), ); }); diff --git a/apps/server/src/vcs/JjVcsDriver.ts b/apps/server/src/vcs/JjVcsDriver.ts index f51460c842..052ea36b5f 100644 --- a/apps/server/src/vcs/JjVcsDriver.ts +++ b/apps/server/src/vcs/JjVcsDriver.ts @@ -1,8 +1,4 @@ -import { mkdtemp, rm } from "node:fs/promises"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; - -import { Effect, Layer } from "effect"; +import { Effect, FileSystem, Layer } from "effect"; import { VcsOutputDecodeError, VcsProcessExitError } from "@t3tools/contracts"; import { VcsDriver, type VcsDriverShape } from "./VcsDriver.ts"; @@ -117,7 +113,7 @@ const gitCommand = ( options?: Parameters[5], ) => processCommand(process, "git", operation, cwd, args, options); -function tempDirError(operation: string, cwd: string, detail: string, cause: unknown) { +function fileSystemError(operation: string, cwd: string, detail: string, cause: unknown) { return new VcsOutputDecodeError({ operation, command: "git check-ignore", @@ -127,17 +123,18 @@ function tempDirError(operation: string, cwd: string, detail: string, cause: unk }); } -const makeTempGitDir = (operation: string, cwd: string) => - Effect.tryPromise({ - try: () => mkdtemp(join(tmpdir(), "t3-jj-check-ignore-")), - catch: (cause) => tempDirError(operation, cwd, "failed to create temp git dir", cause), - }); - -const removeTempGitDir = (gitDir: string) => - Effect.promise(() => rm(gitDir, { force: true, recursive: true })).pipe(Effect.ignore); +const makeScopedTempGitDir = (fileSystem: FileSystem.FileSystem, operation: string, cwd: string) => + fileSystem + .makeTempDirectoryScoped({ prefix: "t3-jj-check-ignore-" }) + .pipe( + Effect.mapError((cause) => + fileSystemError(operation, cwd, "failed to create temp git dir", cause), + ), + ); export const makeVcsDriverShape = Effect.fn("makeJjVcsDriverShape")(function* () { const process = yield* VcsProcess; + const fileSystem = yield* FileSystem.FileSystem; const capabilities = { kind: "jj" as const, supportsWorktrees: true, @@ -229,15 +226,19 @@ export const makeVcsDriverShape = Effect.fn("makeJjVcsDriverShape")(function* () const operation = "JjVcsDriver.filterIgnoredPaths"; const ignoredPaths = new Set(); - yield* Effect.acquireUseRelease( - makeTempGitDir(operation, cwd), - Effect.fn("JjVcsDriver.filterIgnoredPaths.withTempGitDir")(function* (gitDir) { - const initResult = yield* gitCommand(process, operation, cwd, [ - "--git-dir", - gitDir, - "init", - "--bare", - ]); + + yield* Effect.scoped( + Effect.gen(function* () { + const gitDir = yield* makeScopedTempGitDir(fileSystem, operation, cwd); + const initResult = yield* gitCommand( + process, + operation, + cwd, + ["--git-dir", gitDir, "init", "--bare"], + { + allowNonZeroExit: true, + }, + ); if (initResult.exitCode !== 0) { return yield* new VcsProcessExitError({ operation, @@ -290,7 +291,6 @@ export const makeVcsDriverShape = Effect.fn("makeJjVcsDriverShape")(function* () } } }), - removeTempGitDir, ); if (ignoredPaths.size === 0) { diff --git a/apps/server/src/vcs/VcsDriverRegistry.test.ts b/apps/server/src/vcs/VcsDriverRegistry.test.ts index 724bbc4f85..baf7bbd0c7 100644 --- a/apps/server/src/vcs/VcsDriverRegistry.test.ts +++ b/apps/server/src/vcs/VcsDriverRegistry.test.ts @@ -1,3 +1,4 @@ +import * as NodeServices from "@effect/platform-node/NodeServices"; import { assert, it } from "@effect/vitest"; import { Effect, Layer } from "effect"; import { ChildProcessSpawner } from "effect/unstable/process"; @@ -46,6 +47,7 @@ describe("VcsDriverRegistry", () => { }), }), ), + Layer.provide(NodeServices.layer), ); return Effect.gen(function* () { @@ -87,6 +89,7 @@ describe("VcsDriverRegistry", () => { }), }), ), + Layer.provide(NodeServices.layer), ); return Effect.gen(function* () { @@ -123,6 +126,7 @@ describe("VcsDriverRegistry", () => { }), }), ), + Layer.provide(NodeServices.layer), ); return Effect.gen(function* () { diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts index 5c2ad2a370..474014eedf 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.test.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.test.ts @@ -50,29 +50,30 @@ function makeTestLayer(state: { localInvalidationCalls: number; remoteInvalidationCalls: number; }) { - return VcsStatusBroadcasterLayer.pipe( - Layer.provide( - Layer.mock(GitWorkflowService)({ - localStatus: () => - Effect.sync(() => { - state.localStatusCalls += 1; - return state.currentLocalStatus; - }), - remoteStatus: () => - Effect.sync(() => { - state.remoteStatusCalls += 1; - return state.currentRemoteStatus; - }), - invalidateLocalStatus: () => - Effect.sync(() => { - state.localInvalidationCalls += 1; - }), - invalidateRemoteStatus: () => - Effect.sync(() => { - state.remoteInvalidationCalls += 1; - }), + const gitWorkflowLayer = Layer.mock(GitWorkflowService)({ + localStatus: () => + Effect.sync(() => { + state.localStatusCalls += 1; + return state.currentLocalStatus; }), - ), + remoteStatus: () => + Effect.sync(() => { + state.remoteStatusCalls += 1; + return state.currentRemoteStatus; + }), + invalidateLocalStatus: () => + Effect.sync(() => { + state.localInvalidationCalls += 1; + }), + invalidateRemoteStatus: () => + Effect.sync(() => { + state.remoteInvalidationCalls += 1; + }), + }); + + return VcsStatusBroadcasterLayer.pipe( + Layer.provideMerge(NodeServices.layer), + Layer.provide(gitWorkflowLayer), ); } @@ -189,31 +190,31 @@ describe("VcsStatusBroadcaster", () => { localInvalidationCalls: 0, remoteInvalidationCalls: 0, }; + const gitWorkflowLayer = Layer.mock(GitWorkflowService)({ + localStatus: (input) => + Effect.sync(() => { + seenCwds.push(input.cwd); + state.localStatusCalls += 1; + return state.currentLocalStatus; + }), + remoteStatus: (input) => + Effect.sync(() => { + seenCwds.push(input.cwd); + state.remoteStatusCalls += 1; + return state.currentRemoteStatus; + }), + invalidateLocalStatus: () => + Effect.sync(() => { + state.localInvalidationCalls += 1; + }), + invalidateRemoteStatus: () => + Effect.sync(() => { + state.remoteInvalidationCalls += 1; + }), + } satisfies Partial); const testLayer = VcsStatusBroadcasterLayer.pipe( - Layer.provide( - Layer.mock(GitWorkflowService)({ - localStatus: (input) => - Effect.sync(() => { - seenCwds.push(input.cwd); - state.localStatusCalls += 1; - return state.currentLocalStatus; - }), - remoteStatus: (input) => - Effect.sync(() => { - seenCwds.push(input.cwd); - state.remoteStatusCalls += 1; - return state.currentRemoteStatus; - }), - invalidateLocalStatus: () => - Effect.sync(() => { - state.localInvalidationCalls += 1; - }), - invalidateRemoteStatus: () => - Effect.sync(() => { - state.remoteInvalidationCalls += 1; - }), - } satisfies Partial), - ), + Layer.provideMerge(NodeServices.layer), + Layer.provide(gitWorkflowLayer), ); return Effect.gen(function* () { @@ -236,7 +237,7 @@ describe("VcsStatusBroadcaster", () => { assert.deepStrictEqual(seenCwds, [realPath, realPath]); assert.equal(state.localStatusCalls, 1); assert.equal(state.remoteStatusCalls, 1); - }).pipe(Effect.provide(Layer.mergeAll(testLayer, NodeServices.layer))); + }).pipe(Effect.provide(testLayer)); }); it.effect("streams a local snapshot first and remote updates later", () => { @@ -290,40 +291,40 @@ describe("VcsStatusBroadcaster", () => { }; let remoteInterruptedDeferred: Deferred.Deferred | null = null; let remoteStartedDeferred: Deferred.Deferred | null = null; + const gitWorkflowLayer = Layer.mock(GitWorkflowService)({ + localStatus: () => + Effect.sync(() => { + state.localStatusCalls += 1; + return state.currentLocalStatus; + }), + remoteStatus: () => + Effect.sync(() => { + state.remoteStatusCalls += 1; + }).pipe( + Effect.andThen( + remoteStartedDeferred + ? Deferred.succeed(remoteStartedDeferred, undefined).pipe(Effect.ignore) + : Effect.void, + ), + Effect.andThen(Effect.never as Effect.Effect), + Effect.onInterrupt(() => + remoteInterruptedDeferred + ? Deferred.succeed(remoteInterruptedDeferred, undefined).pipe(Effect.ignore) + : Effect.void, + ), + ), + invalidateLocalStatus: () => + Effect.sync(() => { + state.localInvalidationCalls += 1; + }), + invalidateRemoteStatus: () => + Effect.sync(() => { + state.remoteInvalidationCalls += 1; + }), + } satisfies Partial); const testLayer = VcsStatusBroadcasterLayer.pipe( - Layer.provide( - Layer.mock(GitWorkflowService)({ - localStatus: () => - Effect.sync(() => { - state.localStatusCalls += 1; - return state.currentLocalStatus; - }), - remoteStatus: () => - Effect.sync(() => { - state.remoteStatusCalls += 1; - }).pipe( - Effect.andThen( - remoteStartedDeferred - ? Deferred.succeed(remoteStartedDeferred, undefined).pipe(Effect.ignore) - : Effect.void, - ), - Effect.andThen(Effect.never as Effect.Effect), - Effect.onInterrupt(() => - remoteInterruptedDeferred - ? Deferred.succeed(remoteInterruptedDeferred, undefined).pipe(Effect.ignore) - : Effect.void, - ), - ), - invalidateLocalStatus: () => - Effect.sync(() => { - state.localInvalidationCalls += 1; - }), - invalidateRemoteStatus: () => - Effect.sync(() => { - state.remoteInvalidationCalls += 1; - }), - } satisfies Partial), - ), + Layer.provideMerge(NodeServices.layer), + Layer.provide(gitWorkflowLayer), ); return Effect.gen(function* () { diff --git a/apps/server/src/vcs/VcsStatusBroadcaster.ts b/apps/server/src/vcs/VcsStatusBroadcaster.ts index 028d3f1a1d..b6901e851f 100644 --- a/apps/server/src/vcs/VcsStatusBroadcaster.ts +++ b/apps/server/src/vcs/VcsStatusBroadcaster.ts @@ -1,9 +1,8 @@ -import { realpathSync } from "node:fs"; - import { Duration, Effect, Exit, + FileSystem, Fiber, Layer, PubSub, @@ -69,18 +68,11 @@ function fingerprintStatusPart(status: unknown): string { return JSON.stringify(status); } -function normalizeCwd(cwd: string): string { - try { - return realpathSync.native(cwd); - } catch { - return cwd; - } -} - export const layer = Layer.effect( VcsStatusBroadcaster, Effect.gen(function* () { const workflow = yield* GitWorkflowService; + const fileSystem = yield* FileSystem.FileSystem; const changesPubSub = yield* Effect.acquireRelease( PubSub.unbounded(), (pubsub) => PubSub.shutdown(pubsub), @@ -91,6 +83,9 @@ export const layer = Layer.effect( const cacheRef = yield* Ref.make(new Map()); const pollersRef = yield* SynchronizedRef.make(new Map()); + const normalizeCwd = (cwd: string) => + fileSystem.realPath(cwd).pipe(Effect.catch(() => Effect.succeed(cwd))); + const getCachedStatus = Effect.fn("VcsStatusBroadcaster.getCachedStatus")(function* ( cwd: string, ) { @@ -198,7 +193,7 @@ export const layer = Layer.effect( const getStatus: VcsStatusBroadcasterShape["getStatus"] = Effect.fn( "VcsStatusBroadcaster.getStatus", )(function* (input) { - const cwd = normalizeCwd(input.cwd); + const cwd = yield* normalizeCwd(input.cwd); const [local, remote] = yield* Effect.all([ getOrLoadLocalStatus(cwd), getOrLoadRemoteStatus(cwd), @@ -209,7 +204,7 @@ export const layer = Layer.effect( const refreshLocalStatus: VcsStatusBroadcasterShape["refreshLocalStatus"] = Effect.fn( "VcsStatusBroadcaster.refreshLocalStatus", )(function* (rawCwd) { - const cwd = normalizeCwd(rawCwd); + const cwd = yield* normalizeCwd(rawCwd); yield* workflow.invalidateLocalStatus(cwd); const local = yield* workflow.localStatus({ cwd }); return yield* updateCachedLocalStatus(cwd, local, { publish: true }); @@ -226,7 +221,7 @@ export const layer = Layer.effect( const refreshStatus: VcsStatusBroadcasterShape["refreshStatus"] = Effect.fn( "VcsStatusBroadcaster.refreshStatus", )(function* (rawCwd) { - const cwd = normalizeCwd(rawCwd); + const cwd = yield* normalizeCwd(rawCwd); const [local, remote] = yield* Effect.all([ refreshLocalStatus(cwd), refreshRemoteStatus(cwd), @@ -312,7 +307,7 @@ export const layer = Layer.effect( const streamStatus: VcsStatusBroadcasterShape["streamStatus"] = (input) => Stream.unwrap( Effect.gen(function* () { - const cwd = normalizeCwd(input.cwd); + const cwd = yield* normalizeCwd(input.cwd); const subscription = yield* PubSub.subscribe(changesPubSub); const initialLocal = yield* getOrLoadLocalStatus(cwd); const initialRemote = (yield* getCachedStatus(cwd))?.remote?.value ?? null;