// GitRemote.localPath (pure) and GitRemote.sameLocal over an in-memory volume with symbolic links: // real-path equality, the lexical fallback for paths that do not exist, relative paths against a base, // and the failure for a resolution that is not NotFound. import { assert, describe, it } from "@effect/vitest"; import { MemoryFileSystem } from "@effected/memfs"; import { Effect, Option } from "effect"; import type { GitRemoteForm } from "../src/GitRemote.js"; import { GitRemote } from "../src/GitRemote.js"; const parsed = (raw: string): GitRemoteForm => Option.getOrThrow(GitRemote.parse(raw)); const local = (raw: string): Option.Option => GitRemote.localPath(parsed(raw)); describe("GitRemote.localPath", () => { it("is a path form's path verbatim, relative or absolute", () => { assert.deepStrictEqual(local("/srv/r.git"), Option.some("/srv/r.git")); assert.deepStrictEqual(local("../sibling.git"), Option.some("../sibling.git")); assert.deepStrictEqual(local("./a:b"), Option.some("./a:b")); }); it("is a file URL's path, percent-decoded, its host ignored as git ignores it", () => { assert.deepStrictEqual(local("file:///srv/r.git"), Option.some("/srv/r.git")); assert.deepStrictEqual(local("file:///srv/a%20b.git"), Option.some("/srv/a b.git")); assert.deepStrictEqual(local("file:///srv/%C3%A9.git"), Option.some("/srv/é.git")); // A `%` not followed by two hex digits stays, as git's url_decode keeps it. assert.deepStrictEqual(local("file:///srv/100%/r%2"), Option.some("/srv/100%/r%2")); assert.deepStrictEqual(local("file://localhost/srv/r.git"), Option.some("/srv/r.git")); assert.deepStrictEqual(local("file://anything/srv/r.git"), Option.some("/srv/r.git")); }); it("is none for every remote that is not local", () => { for (const raw of [ "https://github.com/o/n.git", "git@github.com:o/n.git", "ssh://host/srv/r.git", // git reads FILE:// as a remote helper's name, not the file scheme. "FILE:///srv/r.git", "file://host", "persistent::/srv/r.git", ]) { assert.isTrue(Option.isNone(local(raw)), raw); } }); }); const volume = MemoryFileSystem.layerWithPath({ "/srv/r.git/HEAD": "ref: refs/heads/main\n", "/srv/other.git/HEAD": "ref: refs/heads/main\n", "/work/app/.keep": "", "/link": MemoryFileSystem.symlink("/srv"), "/srv/alias.git": MemoryFileSystem.symlink("r.git"), "/file": "not a directory", }); const same = (a: string, b: string, base?: string) => GitRemote.sameLocal(parsed(a), parsed(b), base === undefined ? undefined : { base }); describe("GitRemote.sameLocal", () => { it.effect("is true for one directory however it is spelled or linked", () => Effect.gen(function* () { assert.isTrue(yield* same("/srv/r.git", "/srv/r.git/")); assert.isTrue(yield* same("/srv/r.git", "file:///srv/r.git")); assert.isTrue(yield* same("/srv/r.git", "/link/r.git")); assert.isTrue(yield* same("file:///link/r.git", "/srv/alias.git")); assert.isTrue(yield* same("/srv/./r.git", "/srv/other.git/../r.git")); }).pipe(Effect.provide(volume)), ); it.effect("is false for two directories, even where the spelling is close", () => Effect.gen(function* () { assert.isFalse(yield* same("/srv/r.git", "/srv/other.git")); assert.isFalse(yield* same("/link/r.git", "/srv/other.git")); // No `.git` suffix probing: a directory, not a repository, is compared. assert.isFalse(yield* same("/srv/r", "/srv/r.git")); }).pipe(Effect.provide(volume)), ); it.effect("resolves a relative path against base", () => Effect.gen(function* () { assert.isTrue(yield* same("../../srv/r.git", "/srv/r.git", "/work/app")); assert.isTrue(yield* same("../../link/r.git", "file:///srv/alias.git", "/work/app/")); assert.isFalse(yield* same("../r.git", "/srv/r.git", "/work/app")); }).pipe(Effect.provide(volume)), ); it.effect("falls back to the nearest existing ancestor's real path for a path that does not exist", () => Effect.gen(function* () { // Not created yet: the link in the existing prefix is still followed. assert.isTrue(yield* same("/link/new.git", "/srv/new.git")); assert.isTrue(yield* same("/link/new.git/", "file:///srv/new.git")); // The missing rest is joined lexically. assert.isTrue(yield* same("/srv/missing/../new.git", "/srv/new.git")); assert.isFalse(yield* same("/link/new.git", "/srv/newer.git")); // Nothing on disk at all: purely lexical. assert.isTrue(yield* same("/nowhere/a/../r.git", "/nowhere/r.git")); }).pipe(Effect.provide(volume)), ); it.effect("is false without touching the filesystem when either remote is not local", () => Effect.gen(function* () { assert.isFalse(yield* same("/srv/r.git", "https://github.com/o/n.git")); assert.isFalse(yield* same("git@github.com:o/n.git", "git@github.com:o/n.git")); // The volume dies on any realPath, so reaching the filesystem would fail this test. }).pipe(Effect.provide(MemoryFileSystem.layerWithPath({}, { faults: { realPath: () => Effect.die("read") } }))), ); it.effect("fails with the PlatformError of a resolution that is not NotFound", () => Effect.gen(function* () { const error = yield* Effect.flip(same("/file/r.git", "/srv/r.git")); assert.strictEqual(error._tag, "PlatformError"); assert.notStrictEqual(error.reason._tag, "NotFound"); }).pipe(Effect.provide(volume)), ); it.effect("is false where equals is false for every local remote, and true for the same directory", () => Effect.gen(function* () { const remote = parsed("/srv/r.git"); assert.isFalse(GitRemote.equals(remote, remote)); assert.isTrue(yield* GitRemote.sameLocal(remote, remote)); }).pipe(Effect.provide(volume)), ); });