sailfork / t3code-github / apps/ server/ src/ vcs/ GitVcsDriverCore.test.ts

// @effect-diagnostics nodeBuiltinImport:off - realpathSync.native resolves Windows 8.3 short names, which the Effect realPath does not.
import * as NodeFS from "node:fs";
import * as NodePath from "node:path";
import * as NodeURL from "node:url";
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { assert, it, describe } from "@effect/vitest";
import * as Cause from "effect/Cause";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as FileSystem from "effect/FileSystem";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Queue from "effect/Queue";
import * as PlatformError from "effect/PlatformError";
import * as Ref from "effect/Ref";
import * as Result from "effect/Result";
import * as Scope from "effect/Scope";
import * as Schema from "effect/Schema";
import * as Sink from "effect/Sink";
import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
import { ChildProcess, ChildProcessSpawner, CommandLine } from "effect/process";

import {
  GitCommandError,
  ReviewDiffPreviewInput,
  VcsRef,
  VcsListRefsResult,
  type ReviewDiffFileContentsInput,
  type ReviewDiffPreviewResult,
  type WorktreeSubmodules,
} from "@t3tools/contracts";
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
import * as ServerConfig from "../config.ts";
import {
  makeGitVcsDriverCore,
  parseGitCheckoutProgressLine,
  splitNullSeparatedGitStdoutPaths,
  windowsLongPathConfigEnv,
} from "./GitVcsDriverCore.ts";
import * as GitVcsDriver from "./GitVcsDriver.ts";
import { gitConfigEnv } from "./VcsProcess.ts";

const encodeGitCommandError = Schema.encodeEffect(Schema.fromJsonString(GitCommandError));
const decodeVcsRef = Schema.decodeUnknownSync(VcsRef);
const decodeVcsListRefsResult = Schema.decodeUnknownSync(VcsListRefsResult);

const layerServerConfig = ServerConfig.layerTest(process.cwd(), {
  prefix: "t3-git-vcs-driver-test-",
});
const layerTest = GitVcsDriver.layer.pipe(
  Layer.provideMerge(layerServerConfig),
  Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
);

const makeNonRepositoryHandle = () =>
  ChildProcessSpawner.makeHandle({
    pid: ChildProcessSpawner.ProcessId(1),
    exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(128)),
    isRunning: Effect.succeed(false),
    kill: () => Effect.void,
    unref: Effect.succeed(Effect.void),
    stdin: Sink.drain,
    stdout: Stream.empty,
    stderr: Stream.encodeText(Stream.make("fatal: not a git repository")),
    all: Stream.empty,
    getInputFd: () => Sink.drain,
    getOutputFd: () => Stream.empty,
  });

const makeSuccessfulHandle = (stdout: string) =>
  ChildProcessSpawner.makeHandle({
    pid: ChildProcessSpawner.ProcessId(1),
    exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
    isRunning: Effect.succeed(false),
    kill: () => Effect.void,
    unref: Effect.succeed(Effect.void),
    stdin: Sink.drain,
    stdout: Stream.encodeText(Stream.make(stdout)),
    stderr: Stream.empty,
    all: Stream.empty,
    getInputFd: () => Sink.drain,
    getOutputFd: () => Stream.empty,
  });

const makeFailedHandle = (stderr: string) =>
  ChildProcessSpawner.makeHandle({
    ...makeNonRepositoryHandle(),
    exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
    stderr: Stream.encodeText(Stream.make(stderr)),
  });

// Releases the lock as soon as a matching git command fails on it, before the driver retries.
const makeDriverReleasingLock = (
  matches: (args: ReadonlyArray<string>) => boolean,
  lockPath: string,
) =>
  Effect.gen(function* () {
    const failures = yield* Ref.make(0);
    const fileSystem = yield* FileSystem.FileSystem;
    const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
    const spawner = ChildProcessSpawner.make((command) =>
      Effect.gen(function* () {
        const handle = yield* delegate.spawn(command);
        if (!ChildProcess.isStandardCommand(command) || !matches(command.args.map(String)))
          return handle;
        return ChildProcessSpawner.makeHandle({
          ...handle,
          exitCode: handle.exitCode.pipe(
            Effect.tap((exitCode) =>
              exitCode === 0
                ? Effect.void
                : Ref.update(failures, (count) => count + 1).pipe(
                    Effect.andThen(fileSystem.remove(lockPath, { force: true })),
                    Effect.orDie,
                  ),
            ),
          ),
        });
      }),
    );
    const driver = yield* makeGitVcsDriverCore().pipe(
      Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
      Effect.provide(layerServerConfig),
    );
    return { driver, failures };
  });

it.effect("runs background Git in its own lane so user commands never queue behind it", () =>
  Effect.gen(function* () {
    const gate = yield* Deferred.make<void>();
    const starts = yield* Queue.unbounded<string>();
    const spawner = ChildProcessSpawner.make((command) =>
      Effect.gen(function* () {
        yield* Queue.offer(
          starts,
          ChildProcess.isStandardCommand(command) ? String(command.args[0]) : "",
        );
        return ChildProcessSpawner.makeHandle({
          ...makeSuccessfulHandle("ok"),
          exitCode: Deferred.await(gate).pipe(Effect.as(ChildProcessSpawner.ExitCode(0))),
        });
      }),
    );
    const driver = yield* makeGitVcsDriverCore().pipe(
      Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
      Effect.provide(layerServerConfig),
    );
    const run = (args: ReadonlyArray<string>) =>
      driver.execute({ operation: "test.gitLane", cwd: "/repo", args: [...args] });
    const background = yield* Effect.forEach(
      Array.from({ length: 12 }),
      () => run(["background"]),
      {
        concurrency: "unbounded",
      },
    ).pipe(Effect.provideService(GitVcsDriver.BackgroundGitWork, true), Effect.forkChild);
    const foreground = yield* Effect.forEach(Array.from({ length: 4 }), () => run(["foreground"]), {
      concurrency: "unbounded",
    }).pipe(Effect.forkChild);

    yield* TestClock.adjust("2 seconds");
    const started = yield* Queue.takeAll(starts);
    assert.deepStrictEqual([...started].toSorted(), [
      "background",
      "background",
      "foreground",
      "foreground",
      "foreground",
      "foreground",
    ]);
    yield* Deferred.succeed(gate, undefined);
    assert.equal((yield* Fiber.join(background)).length, 12);
    assert.equal((yield* Fiber.join(foreground)).length, 4);
  }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),
);

describe("Git output collection", () => {
  // Git writes in arbitrary chunk sizes; one byte per chunk splits every
  // multibyte character across reads.
  const byteChunks = (text: string) =>
    Stream.fromIterable(Array.from(new TextEncoder().encode(text), (byte) => Uint8Array.of(byte)));
  const makeDriver = (output: { stdout?: string; stderr?: string }) =>
    makeGitVcsDriverCore().pipe(
      Effect.provideService(
        ChildProcessSpawner.ChildProcessSpawner,
        ChildProcessSpawner.make(() =>
          Effect.succeed(
            ChildProcessSpawner.makeHandle({
              ...makeSuccessfulHandle(""),
              stdout: byteChunks(output.stdout ?? ""),
              stderr: byteChunks(output.stderr ?? ""),
            }),
          ),
        ),
      ),
    );

  it.effect("returns the same split UTF-8 and NUL output with and without line callbacks", () =>
    Effect.gen(function* () {
      const stdout = "a\\b\0雪\nlast ";
      const stderr = "Receiving é\r\nUpdating 雪\rdone";
      const driver = yield* makeDriver({ stdout, stderr });
      const input = { operation: "test.chunkedGitOutput", cwd: "/repo", args: ["status"] };
      const expected = {
        exitCode: 0,
        stdout,
        stderr,
        stdoutTruncated: false,
        stderrTruncated: false,
      };

      assert.deepEqual(yield* driver.execute(input), expected);

      const stdoutLines: string[] = [];
      const stderrLines: string[] = [];
      const withCallbacks = yield* driver.execute({
        ...input,
        progress: {
          onStdoutLine: (line) => Effect.sync(() => void stdoutLines.push(line)),
          onStderrLine: (line) => Effect.sync(() => void stderrLines.push(line)),
        },
      });
      assert.deepEqual(withCallbacks, expected);
      assert.deepEqual(stdoutLines, ["a\\b\0雪", "last "]);
      assert.deepEqual(stderrLines, ["Receiving é", "Updating 雪", "done"]);
    }).pipe(
      Effect.provide(
        layerServerConfig.pipe(
          Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
        ),
      ),
    ),
  );

  it.effect("caps output without line callbacks", () =>
    Effect.gen(function* () {
      const driver = yield* makeDriver({ stderr: "é雪\rdone" });
      const input = {
        operation: "test.cappedGitOutput",
        cwd: "/repo",
        args: ["status"],
        maxOutputBytes: 3,
      };

      const truncated = yield* driver.execute({ ...input, appendTruncationMarker: true });
      assert.equal(truncated.stderr, "é\uFFFD");
      assert.isTrue(truncated.stderrTruncated);

      const error = yield* driver.execute(input).pipe(Effect.flip);
      assert.equal(error.outputLength, 4);
      assert.include(error.detail, "exceeded 3 bytes");
    }).pipe(
      Effect.provide(
        layerServerConfig.pipe(
          Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
        ),
      ),
    ),
  );
});

const makeTmpDir = (
  prefix = "git-vcs-driver-test-",
): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
  Effect.gen(function* () {
    const fileSystem = yield* FileSystem.FileSystem;
    return yield* fileSystem.makeTempDirectoryScoped({ prefix });
  });

const writeTextFile = (
  cwd: string,
  relativePath: string,
  contents: string,
): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem | Path.Path> =>
  Effect.gen(function* () {
    const fileSystem = yield* FileSystem.FileSystem;
    const pathService = yield* Path.Path;
    const filePath = pathService.join(cwd, relativePath);
    yield* fileSystem.makeDirectory(pathService.dirname(filePath), { recursive: true });
    yield* fileSystem.writeFileString(filePath, contents);
  });

const makeReviewDiffFileContentsInput = (
  cwd: string,
  overrides: Partial<Omit<ReviewDiffFileContentsInput, "cwd">> = {},
): ReviewDiffFileContentsInput => ({
  cwd,
  sourceKind: "working-tree",
  changeType: "change",
  baseRef: "HEAD",
  headRef: null,
  oldPath: "README.md",
  newPath: "README.md",
  ...overrides,
});

const git = (
  cwd: string,
  args: ReadonlyArray<string>,
  env?: NodeJS.ProcessEnv,
): Effect.Effect<string, GitCommandError, GitVcsDriver.GitVcsDriver> =>
  Effect.gen(function* () {
    const driver = yield* GitVcsDriver.GitVcsDriver;
    const result = yield* driver.execute({
      operation: "GitVcsDriver.test.git",
      cwd,
      args,
      ...(env ? { env } : {}),
      timeoutMs: 10_000,
    });
    return result.stdout.trim();
  });

// Git blocks file submodule transports (CVE-2022-39253) despite repo config; local fixtures need env permission.
const withGitFileProtocol = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
  Effect.flatMap(HostProcess.Environment, (env) =>
    effect.pipe(
      Effect.provideService(HostProcess.Environment, { ...env, GIT_ALLOW_PROTOCOL: "file" }),
    ),
  );

const initRepoWithCommit = (
  cwd: string,
): Effect.Effect<
  { readonly initialBranch: string },
  GitCommandError | PlatformError.PlatformError,
  GitVcsDriver.GitVcsDriver | FileSystem.FileSystem | Path.Path
> =>
  Effect.gen(function* () {
    const driver = yield* GitVcsDriver.GitVcsDriver;
    yield* driver.initRepo({ cwd });
    yield* git(cwd, ["config", "user.email", "test@test.com"]);
    yield* git(cwd, ["config", "user.name", "Test"]);
    yield* writeTextFile(cwd, "README.md", "# test\n");
    yield* git(cwd, ["add", "."]);
    yield* git(cwd, ["commit", "-m", "initial commit"]);
    const initialBranch = yield* git(cwd, ["branch", "--show-current"]);
    return { initialBranch };
  });

it.effect.each(["root", "nested", "worktree"] as const)(
  "skips clean filters while the %s index is locked and resumes after unlock",
  (location) =>
    Effect.gen(function* () {
      const driver = yield* GitVcsDriver.GitVcsDriver;
      const fs = yield* FileSystem.FileSystem;
      const path = yield* Path.Path;
      const repository = yield* makeTmpDir();
      yield* initRepoWithCommit(repository);
      const cwd = location === "worktree" ? yield* makeTmpDir() : repository;
      if (location === "worktree") {
        yield* git(repository, ["worktree", "add", "--detach", cwd]);
      }
      yield* git(cwd, ["config", "filter.probe.clean", "echo clean >> .filter-runs; cat"]);
      yield* writeTextFile(cwd, ".gitattributes", "asset.bin filter=probe\n");
      yield* writeTextFile(cwd, ".gitignore", ".filter-runs\n");
      yield* writeTextFile(cwd, "asset.bin", "original\n");
      yield* git(cwd, ["add", "."]);
      yield* git(cwd, ["commit", "-m", "filtered asset"]);
      NodeFS.utimesSync(path.join(cwd, "asset.bin"), 1, 1);
      const runsPath = path.join(cwd, ".filter-runs");
      yield* fs.remove(runsPath, { force: true });
      const indexPath = yield* git(cwd, ["rev-parse", "--git-path", "index"]);
      const lockPath = `${path.resolve(cwd, indexPath)}.lock`;
      yield* fs.writeFileString(lockPath, "");
      const statusCwd = location === "nested" ? path.join(cwd, "nested") : cwd;
      yield* fs.makeDirectory(statusCwd, { recursive: true });

      for (let poll = 0; poll < 3; poll++) {
        const result = yield* driver.statusDetailsLocal(statusCwd).pipe(Effect.result);
        assert.isTrue(Result.isFailure(result));
        if (Result.isFailure(result)) assert.include(result.failure.detail, "index is locked");
      }
      assert.isFalse(yield* fs.exists(runsPath));
      assert.isTrue(yield* fs.exists(lockPath));

      yield* fs.remove(lockPath);
      const status = yield* driver.statusDetailsLocal(statusCwd);
      assert.isFalse(status.hasWorkingTreeChanges);
      assert.include(yield* fs.readFileString(runsPath), "clean");
    }).pipe(Effect.provide(layerTest)),
);

it.effect.each(["origin", "github"] as const)(
  "recognizes %s as the primary remote after a driver mutation",
  (remoteName) =>
    Effect.gen(function* () {
      const driver = yield* GitVcsDriver.GitVcsDriver;
      const cwd = yield* makeTmpDir();
      const remote = yield* makeTmpDir("git-vcs-driver-remote-");
      yield* initRepoWithCommit(cwd);
      yield* git(remote, ["init", "--bare"]);

      assert.equal((yield* driver.statusDetailsLocal(cwd)).hasOriginRemote, false);
      assert.equal((yield* driver.listRefs({ cwd })).hasPrimaryRemote, false);

      yield* driver.ensureRemote({ cwd, preferredName: remoteName, url: remote });

      assert.equal((yield* driver.statusDetailsLocal(cwd)).hasOriginRemote, true);
      assert.equal((yield* driver.listRefs({ cwd })).hasPrimaryRemote, true);
    }).pipe(Effect.provide(layerTest)),
);

it.effect.each([
  { hookExitCode: 0, commonDirFailure: "none" },
  { hookExitCode: 1, commonDirFailure: "none" },
  { hookExitCode: 0, commonDirFailure: "error" },
  { hookExitCode: 1, commonDirFailure: "error" },
  { hookExitCode: 0, commonDirFailure: "non-repository" },
  { hookExitCode: 1, commonDirFailure: "non-repository" },
])(
  "invalidates sibling worktree refs and status after checkout hook exit $hookExitCode with common-dir failure $commonDirFailure",
  ({ hookExitCode, commonDirFailure }) =>
    Effect.scoped(
      Effect.gen(function* () {
        const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
        const spawnedArgs: Array<ReadonlyArray<string>> = [];
        let invalidationFailure = "none";
        const spawner = ChildProcessSpawner.make((command) =>
          Effect.gen(function* () {
            if (!ChildProcess.isStandardCommand(command)) {
              return yield* Effect.die("expected a standard Git command");
            }
            spawnedArgs.push(command.args.map(String));
            if (invalidationFailure !== "none" && command.args.includes("--git-common-dir")) {
              if (invalidationFailure === "non-repository") return makeNonRepositoryHandle();
              return ChildProcessSpawner.makeHandle({
                ...makeNonRepositoryHandle(),
                stderr: Stream.encodeText(Stream.make("fatal: could not read repository metadata")),
              });
            }
            return yield* delegate.spawn(command);
          }),
        );
        const driver = yield* makeGitVcsDriverCore().pipe(
          Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
        );
        const cwd = yield* makeTmpDir();
        const { initialBranch } = yield* initRepoWithCommit(cwd);
        const path = yield* Path.Path;
        const fs = yield* FileSystem.FileSystem;
        const siblingPath = path.join(yield* makeTmpDir("git-worktrees-"), "sibling");
        yield* git(cwd, ["worktree", "add", "-b", "cache-sibling", siblingPath, "HEAD"]);

        const before = yield* driver.listRefs({ cwd: siblingPath, refresh: true });
        assert.isTrue(before.refs.find((ref) => ref.name === "cache-sibling")?.current);
        const statusBefore = yield* driver.statusDetailsLocal(siblingPath);
        assert.isFalse(statusBefore.hasOriginRemote);
        assert.isFalse(statusBefore.isDefaultBranch);
        yield* driver.listRefs({ cwd, refresh: true });
        yield* git(cwd, ["checkout", "-b", "creator-current"]);
        yield* writeTextFile(
          cwd,
          ".git/hooks/post-checkout",
          "#!/bin/sh\n" +
            "git remote add origin https://example.invalid/repo.git || exit 2\n" +
            "git update-ref refs/remotes/origin/cache-sibling HEAD || exit 2\n" +
            "git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/cache-sibling || exit 2\n" +
            `exit ${hookExitCode}\n`,
        );
        yield* fs.chmod(path.join(cwd, ".git/hooks/post-checkout"), 0o755);
        const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "created");
        spawnedArgs.length = 0;
        invalidationFailure = commonDirFailure;

        const created = yield* driver
          .createWorktree({
            cwd,
            path: worktreePath,
            refName: initialBranch,
            newRefName: "feature/cache-created",
          })
          .pipe(Effect.result);

        assert.equal(Result.isSuccess(created), hookExitCode === 0);
        if (Result.isFailure(created)) {
          assert.equal(created.failure.operation, "GitVcsDriver.createWorktree");
          assert.equal(created.failure.detail, "git worktree add failed");
          assert.equal(created.failure.exitCode, hookExitCode);
        }
        const metadataQueries = spawnedArgs.filter((args) => args.includes("--git-common-dir"));
        assert.deepStrictEqual(metadataQueries, [["rev-parse", "--git-common-dir"]]);
        assert.isFalse(spawnedArgs.some((args) => args.includes("symbolic-ref")));
        invalidationFailure = "none";

        spawnedArgs.length = 0;
        const creatorStatus = yield* driver.statusDetailsLocal(cwd);
        assert.equal(creatorStatus.branch, "creator-current");
        assert.isTrue(spawnedArgs.some((args) => args.includes("--git-common-dir")));
        const siblingRefs = yield* driver.listRefs({ cwd: siblingPath });
        const createdRef = siblingRefs.refs.find((ref) => ref.name === "feature/cache-created");
        assert.isDefined(createdRef?.worktreePath);
        assert.equal(
          NodeFS.realpathSync.native(createdRef!.worktreePath!),
          NodeFS.realpathSync.native(worktreePath),
        );
        assert.isTrue(siblingRefs.hasPrimaryRemote);
        assert.isTrue(siblingRefs.refs.find((ref) => ref.name === "cache-sibling")?.current);
        const statusAfter = yield* driver.statusDetailsLocal(siblingPath);
        assert.isTrue(statusAfter.hasOriginRemote);
        assert.isTrue(statusAfter.isDefaultBranch);
        const creatorRefs = yield* driver.listRefs({ cwd });
        assert.isTrue(creatorRefs.refs.find((ref) => ref.name === "creator-current")?.current);
        assert.isFalse(creatorRefs.refs.find((ref) => ref.name === initialBranch)?.current);
      }),
    ).pipe(Effect.provide(layerTest)),
);

it.effect.each(["branch", "detached", "nested", "aliased-root", "aliased-nested"] as const)(
  "discovers only the common directory during worktree creation from %s",
  (location) =>
    Effect.scoped(
      Effect.gen(function* () {
        const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
        const spawnedArgs: Array<ReadonlyArray<string>> = [];
        const spawner = ChildProcessSpawner.make((command) => {
          if (ChildProcess.isStandardCommand(command)) spawnedArgs.push(command.args.map(String));
          return delegate.spawn(command);
        });
        const driver = yield* makeGitVcsDriverCore().pipe(
          Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
        );
        const cwd = yield* makeTmpDir();
        const { initialBranch } = yield* initRepoWithCommit(cwd);
        const path = yield* Path.Path;
        if (location === "detached") yield* git(cwd, ["checkout", "--detach"]);
        const isNested = location === "nested" || location === "aliased-nested";
        const physicalCwd = isNested ? path.join(cwd, "nested") : cwd;
        const fs = yield* FileSystem.FileSystem;
        yield* fs.makeDirectory(physicalCwd, { recursive: true });
        const isAliased = location === "aliased-root" || location === "aliased-nested";
        const creationCwd = isAliased
          ? path.join(yield* makeTmpDir("git-alias-"), "link")
          : physicalCwd;
        if (isAliased) NodeFS.symlinkSync(physicalCwd, creationCwd, "junction");
        const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "cold");

        yield* driver.createWorktree({
          cwd: creationCwd,
          path: worktreePath,
          refName: initialBranch,
          newRefName: "feature/cold",
        });

        assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1);
        assert.isFalse(spawnedArgs.some((args) => args.includes("--show-toplevel")));
        assert.isFalse(spawnedArgs.some((args) => args.includes("symbolic-ref")));
        spawnedArgs.length = 0;
        const originalStatus = yield* driver.statusDetailsLocal(creationCwd);
        assert.equal(originalStatus.branch, location === "detached" ? null : initialBranch);
        const originalRefs = yield* driver.listRefs({ cwd: creationCwd });
        assert.equal(originalRefs.isAtRepositoryRoot, !isNested);
        assert.deepStrictEqual(
          originalRefs.refs.filter((ref) => ref.current).map((ref) => ref.name),
          location === "detached" ? [] : [initialBranch],
        );
        assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1);
        const refs = yield* driver.listRefs({ cwd: worktreePath });
        assert.isTrue(refs.refs.find((ref) => ref.name === "feature/cold")?.current);
        assert.equal(
          yield* git(worktreePath, ["rev-parse", "HEAD"]),
          yield* git(cwd, ["rev-parse", "HEAD"]),
        );
      }),
    ).pipe(Effect.provide(layerTest)),
);

it.effect.each(["bare", "unborn"] as const)(
  "invalidates a %s repository after worktree creation",
  (state) =>
    Effect.scoped(
      Effect.gen(function* () {
        const source = yield* makeTmpDir();
        const cwd = yield* makeTmpDir("git-discovery-fallback-");
        if (state === "bare") {
          yield* initRepoWithCommit(source);
          yield* git(source, ["clone", "--bare", source, cwd]);
        } else {
          yield* git(cwd, ["init"]);
        }
        const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
        const spawnedArgs: Array<ReadonlyArray<string>> = [];
        const spawner = ChildProcessSpawner.make((command) => {
          if (ChildProcess.isStandardCommand(command)) spawnedArgs.push(command.args.map(String));
          return delegate.spawn(
            state === "bare" ? ChildProcess.setEnv(command, { GIT_DIR: cwd }) : command,
          );
        });
        const driver = yield* makeGitVcsDriverCore().pipe(
          Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
        );
        const path = yield* Path.Path;
        const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "fallback");
        const result = yield* driver
          .createWorktree({
            cwd,
            path: worktreePath,
            refName: "HEAD",
            newRefName: "feature/fallback",
          })
          .pipe(Effect.result);
        assert.equal(Result.isSuccess(result), state === "bare");
        if (Result.isFailure(result)) {
          assert.equal(result.failure.operation, "GitVcsDriver.createWorktree.createBranch");
          assert.equal(result.failure.detail, "git worktree add failed");
        }
        assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1);
        assert.isFalse(spawnedArgs.some((args) => args[0] === "symbolic-ref"));
        spawnedArgs.length = 0;
        const refs = yield* driver.listRefs({ cwd });
        assert.isTrue(refs.isRepo);
        if (state === "bare") {
          assert.isTrue(refs.refs.some((ref) => ref.name === "feature/fallback"));
        } else {
          assert.isEmpty(refs.refs);
        }
        assert.equal(spawnedArgs.filter((args) => args.includes("--git-common-dir")).length, 1);
      }),
    ).pipe(Effect.provide(layerTest)),
);

it.effect.each(["on a branch", "detached", "unborn"] as const)(
  "reads only the repository root for review requests that ignore the branch when %s",
  (state) =>
    Effect.scoped(
      Effect.gen(function* () {
        const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
        const spawnedArgs: Array<ReadonlyArray<string>> = [];
        const spawner = ChildProcessSpawner.make((command) => {
          if (ChildProcess.isStandardCommand(command)) spawnedArgs.push(command.args.map(String));
          return delegate.spawn(command);
        });
        const driver = yield* makeGitVcsDriverCore().pipe(
          Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
        );
        const discoveries = () => {
          const commands = spawnedArgs.filter(
            (args) =>
              args.includes("--show-toplevel") ||
              args.includes("--git-common-dir") ||
              (args.includes("symbolic-ref") && args.includes("HEAD")),
          );
          spawnedArgs.length = 0;
          return commands.length;
        };
        const cwd = yield* makeTmpDir();
        if (state === "unborn") {
          yield* git(cwd, ["init", "-b", "main"]);
        } else {
          yield* initRepoWithCommit(cwd);
          yield* writeTextFile(cwd, "committed.txt", "committed\n");
          yield* git(cwd, ["add", "."]);
          yield* git(cwd, ["commit", "-m", "add file"]);
          if (state === "detached") yield* git(cwd, ["checkout", "--detach"]);
        }
        yield* writeTextFile(cwd, "new.txt", "new\n");
        spawnedArgs.length = 0;

        const workingTree = yield* driver.getReviewDiffPreview({
          cwd,
          file: { path: "new.txt", previousPath: null, sourceKind: "working-tree" },
        });
        assert.equal(discoveries(), 1);
        assert.deepStrictEqual(
          workingTree.sources
            .find((source) => source.kind === "working-tree")!
            .files!.map((file) => file.path),
          ["new.txt"],
        );

        if (state !== "unborn") {
          const commitSha = yield* git(cwd, ["rev-parse", "HEAD"]);
          spawnedArgs.length = 0;
          const commit = yield* driver.getReviewDiffPreview({
            cwd,
            commitSha,
            file: { path: "committed.txt", previousPath: null, sourceKind: "commit" },
          });
          assert.equal(discoveries(), 1);
          assert.include(commit.sources[0]!.diff, "+committed");
        }

        const general = yield* driver.getReviewDiffPreview({ cwd });
        assert.equal(discoveries(), 3);
        assert.equal(
          general.sources.find((source) => source.kind === "branch-range")!.headRef,
          state === "detached" ? "HEAD" : yield* git(cwd, ["branch", "--show-current"]),
        );
      }),
    ).pipe(Effect.provide(layerTest)),
);

it.effect.each([
  { kind: "working-tree", request: { sourceKind: "working-tree" } },
  { kind: "commit", request: { sourceKind: "commit", commitSha: "HEAD" } },
] as const)("keeps $kind review requests safe without a worktree root", ({ request }) =>
  Effect.scoped(
    Effect.gen(function* () {
      const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
      const bareRoot = yield* makeTmpDir("git-review-bare-");
      const spawner = ChildProcessSpawner.make((command) =>
        delegate.spawn(
          ChildProcess.isStandardCommand(command) && command.options.cwd === bareRoot
            ? ChildProcess.setEnv(command, { GIT_DIR: bareRoot })
            : command,
        ),
      );
      const driver = yield* makeGitVcsDriverCore().pipe(
        Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
      );
      const path = yield* Path.Path;
      const file = { path: "README.md", previousPath: null, sourceKind: request.sourceKind };
      const input = (cwd: string) => ({
        cwd,
        file,
        ...("commitSha" in request ? { commitSha: request.commitSha } : {}),
      });
      const source = yield* makeTmpDir();
      yield* initRepoWithCommit(source);
      yield* git(source, ["clone", "--bare", source, bareRoot]);
      const nonRepository = yield* makeTmpDir();
      for (const cwd of [bareRoot, nonRepository, path.join(nonRepository, "missing")]) {
        assert.isEmpty((yield* driver.getReviewDiffPreview(input(cwd))).sources);
      }

      const brokenConfig = yield* makeTmpDir();
      yield* initRepoWithCommit(brokenConfig);
      yield* writeTextFile(brokenConfig, ".git/config", "[broken");
      const error = yield* driver.getReviewDiffPreview(input(brokenConfig)).pipe(Effect.flip);
      assert.equal(error.operation, "GitVcsDriver.resolveRepositoryPaths.commonDir");
    }),
  ).pipe(Effect.provide(layerTest)),
);

it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
  "preserves embedded newlines in repository paths after worktree creation",
  () =>
    Effect.scoped(
      Effect.gen(function* () {
        const path = yield* Path.Path;
        const fs = yield* FileSystem.FileSystem;
        const cwd = path.join(yield* makeTmpDir(), "repo\nwith-newline");
        yield* fs.makeDirectory(cwd);
        const { initialBranch } = yield* initRepoWithCommit(cwd);
        const driver = yield* GitVcsDriver.GitVcsDriver;
        const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "newline");
        yield* driver.createWorktree({
          cwd,
          path: worktreePath,
          refName: initialBranch,
          newRefName: "feature/newline",
        });
        const status = yield* driver.statusDetailsLocal(cwd);
        assert.equal(status.branch, initialBranch);
        const refs = yield* driver.listRefs({ cwd });
        assert.isTrue(refs.refs.find((ref) => ref.name === initialBranch)?.current);
        assert.isTrue(refs.refs.some((ref) => ref.name === "feature/newline"));
      }),
    ).pipe(Effect.provide(layerTest)),
);

it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
  "preserves a trailing carriage return in the repository root after creation through an alias",
  () =>
    Effect.scoped(
      Effect.gen(function* () {
        const path = yield* Path.Path;
        const fs = yield* FileSystem.FileSystem;
        const sandbox = yield* makeTmpDir();
        const repository = path.join(sandbox, "repo\r");
        const cwd = path.join(sandbox, "alias");
        yield* fs.makeDirectory(repository);
        const { initialBranch } = yield* initRepoWithCommit(repository);
        NodeFS.symlinkSync(repository, cwd, "junction");
        const driver = yield* GitVcsDriver.GitVcsDriver;
        yield* driver.createWorktree({
          cwd,
          path: path.join(yield* makeTmpDir("git-worktrees-"), "created"),
          refName: initialBranch,
          newRefName: "feature/carriage-return",
        });
        const status = yield* driver.statusDetailsLocal(cwd);
        assert.equal(status.workingTree.rootPath, NodeFS.realpathSync.native(repository));
        assert.equal(status.branch, initialBranch);
        const refs = yield* driver.listRefs({ cwd });
        assert.isTrue(refs.isAtRepositoryRoot);
        assert.isTrue(refs.refs.some((ref) => ref.name === "feature/carriage-return"));
      }),
    ).pipe(Effect.provide(layerTest)),
);

it.effect(
  "reads fresh metadata when another mutation completes during common-directory discovery",
  () =>
    Effect.scoped(
      Effect.gen(function* () {
        const cwd = yield* makeTmpDir();
        yield* initRepoWithCommit(cwd);
        const commit = yield* git(cwd, ["rev-parse", "HEAD"]);
        const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
        const discoveryComplete = yield* Deferred.make<void>();
        const releaseDiscovery = yield* Deferred.make<void>();
        let holdDiscovery = false;
        const spawner = ChildProcessSpawner.make((command) =>
          Effect.gen(function* () {
            const handle = yield* delegate.spawn(command);
            if (
              !holdDiscovery ||
              !ChildProcess.isStandardCommand(command) ||
              !command.args.includes("--git-common-dir")
            )
              return handle;
            holdDiscovery = false;
            const output = yield* Stream.runCollect(handle.stdout);
            yield* handle.exitCode;
            yield* Deferred.succeed(discoveryComplete, undefined);
            yield* Deferred.await(releaseDiscovery);
            return ChildProcessSpawner.makeHandle({
              ...handle,
              stdout: Stream.fromIterable(output),
            });
          }),
        );
        const driver = yield* makeGitVcsDriverCore().pipe(
          Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
        );
        yield* driver.listRefs({ cwd, refresh: true });
        const path = yield* Path.Path;
        const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "concurrent");
        holdDiscovery = true;
        const creation = yield* driver
          .createWorktree({
            cwd,
            path: worktreePath,
            refName: "HEAD",
            newRefName: "feature/concurrent",
          })
          .pipe(Effect.forkChild({ startImmediately: true }));
        yield* Effect.gen(function* () {
          yield* Deferred.await(discoveryComplete);
          yield* driver.switchRef({ cwd, refName: commit });
        }).pipe(Effect.ensuring(Deferred.succeed(releaseDiscovery, undefined)));
        const created = yield* Fiber.join(creation);
        assert.equal(created.worktree.path, worktreePath);
        const refs = yield* driver.listRefs({ cwd });
        assert.isEmpty(refs.refs.filter((ref) => ref.current));
        assert.isTrue(refs.refs.some((ref) => ref.name === "feature/concurrent"));
        assert.equal((yield* driver.statusDetailsLocal(cwd)).branch, null);
      }),
    ).pipe(Effect.provide(layerTest)),
);

it.effect("invalidates cached metadata after interrupted worktree creation", () =>
  Effect.scoped(
    Effect.gen(function* () {
      const cwd = yield* makeTmpDir();
      yield* initRepoWithCommit(cwd);
      const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
      const discoveryComplete = yield* Deferred.make<void>();
      const releaseDiscovery = yield* Deferred.make<void>();
      let holdDiscovery = false;
      const spawner = ChildProcessSpawner.make((command) =>
        Effect.gen(function* () {
          const handle = yield* delegate.spawn(command);
          if (
            !holdDiscovery ||
            !ChildProcess.isStandardCommand(command) ||
            !command.args.includes("--git-common-dir")
          )
            return handle;
          holdDiscovery = false;
          const output = yield* Stream.runCollect(handle.stdout);
          yield* handle.exitCode;
          yield* Deferred.succeed(discoveryComplete, undefined);
          yield* Deferred.await(releaseDiscovery);
          return ChildProcessSpawner.makeHandle({ ...handle, stdout: Stream.fromIterable(output) });
        }),
      );
      const driver = yield* makeGitVcsDriverCore().pipe(
        Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
      );
      yield* driver.listRefs({ cwd, refresh: true });
      yield* driver.statusDetailsLocal(cwd);
      yield* git(cwd, ["checkout", "-b", "creator-current"]);
      const path = yield* Path.Path;
      const worktreePath = path.join(yield* makeTmpDir("git-worktrees-"), "interrupted");
      holdDiscovery = true;
      const creation = yield* driver
        .createWorktree({
          cwd,
          path: worktreePath,
          refName: "HEAD",
          newRefName: "feature/interrupted",
        })
        .pipe(Effect.forkChild({ startImmediately: true }));
      const cancellation = yield* Effect.gen(function* () {
        yield* Deferred.await(discoveryComplete);
        return yield* Fiber.interrupt(creation).pipe(Effect.forkChild({ startImmediately: true }));
      }).pipe(Effect.ensuring(Deferred.succeed(releaseDiscovery, undefined)));
      yield* Fiber.join(cancellation);
      const exit = yield* Fiber.await(creation);
      assert.isTrue(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause));
      const refs = yield* driver.listRefs({ cwd });
      assert.isTrue(refs.refs.find((ref) => ref.name === "creator-current")?.current);
      const interruptedRef = refs.refs.find((ref) => ref.name === "feature/interrupted");
      assert.isDefined(interruptedRef?.worktreePath);
      assert.equal(
        NodeFS.realpathSync.native(interruptedRef!.worktreePath!),
        NodeFS.realpathSync.native(worktreePath),
      );
      assert.equal((yield* driver.statusDetailsLocal(cwd)).branch, "creator-current");
      const nextPath = path.join(yield* makeTmpDir("git-worktrees-"), "after-interruption");
      yield* driver.createWorktree({
        cwd,
        path: nextPath,
        refName: "HEAD",
        newRefName: "feature/after",
      });
      assert.isTrue(
        (yield* driver.listRefs({ cwd })).refs.some((ref) => ref.name === "feature/after"),
      );
    }),
  ).pipe(Effect.provide(layerTest)),
);

describe("pullCurrentBranch", () => {
  // A fast-forward that prints over 1 MB takes thousands of files, so the real pull's output is
  // padded past the cap instead.
  const withPaddedPull = (
    stream: "stdout" | "stderr",
    onPull?: (args: ReadonlyArray<string>) => void,
  ) =>
    Effect.gen(function* () {
      const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
      const padding = new Uint8Array(1_000_001).fill(0x20);
      const paddingSpawner = ChildProcessSpawner.make((command) =>
        Effect.gen(function* () {
          const handle = yield* delegate.spawn(command);
          const isPull =
            ChildProcess.isStandardCommand(command) &&
            command.args.includes("pull") &&
            command.args.includes("--ff-only");
          if (isPull) onPull?.(command.args.map(String));
          return isPull
            ? ChildProcessSpawner.makeHandle({
                ...handle,
                [stream]: Stream.concat(handle[stream], Stream.make(padding)),
              })
            : handle;
        }),
      );
      return yield* makeGitVcsDriverCore().pipe(
        Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, paddingSpawner),
      );
    });

  const cloneBehindRemote = Effect.gen(function* () {
    const cwd = yield* makeTmpDir();
    const remote = yield* makeTmpDir("git-vcs-driver-remote-");
    const updater = yield* makeTmpDir("git-vcs-driver-updater-");
    const { initialBranch } = yield* initRepoWithCommit(cwd);
    yield* git(remote, ["init", "--bare"]);
    yield* git(cwd, ["remote", "add", "origin", remote]);
    yield* git(cwd, ["push", "-u", "origin", initialBranch]);
    yield* git(updater, ["clone", remote, "."]);
    yield* git(updater, ["config", "user.email", "test@test.com"]);
    yield* git(updater, ["config", "user.name", "Test"]);
    yield* writeTextFile(updater, "remote.txt", "remote\n");
    yield* git(updater, ["add", "remote.txt"]);
    yield* git(updater, ["commit", "-m", "remote commit"]);
    yield* git(updater, ["push", "origin", initialBranch]);
    return {
      cwd,
      remote,
      updater,
      initialBranch,
      remoteHead: yield* git(updater, ["rev-parse", "HEAD"]),
    };
  });

  it.effect("still fetches other branches on the first pull", () =>
    Effect.gen(function* () {
      const driver = yield* GitVcsDriver.GitVcsDriver;
      const { cwd, updater, remoteHead } = yield* cloneBehindRemote;
      yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]);

      const result = yield* driver.pullCurrentBranch(cwd);

      assert.equal(result.status, "pulled");
      assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead);
      assert.equal(yield* git(cwd, ["rev-parse", "refs/remotes/origin/other"]), remoteHead);
    }).pipe(Effect.provide(layerTest)),
  );

  it.effect.each([false, true])(
    "pulls past an unrelated lock with custom fetch mapping=%s",
    (customMapping) =>
      Effect.gen(function* () {
        const driver = yield* GitVcsDriver.GitVcsDriver;
        const fs = yield* FileSystem.FileSystem;
        const { cwd, updater, initialBranch, remoteHead } = yield* cloneBehindRemote;
        yield* git(cwd, ["tag", initialBranch]);
        yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]);
        const remoteName = customMapping ? "team/upstream" : "origin";
        const refPrefix = customMapping
          ? `refs/remotes/${remoteName}/mapped`
          : "refs/remotes/origin";
        if (customMapping) {
          yield* git(cwd, ["remote", "rename", "origin", remoteName]);
          yield* git(cwd, ["config", `remote.${remoteName}.fetch`, `+refs/heads/*:${refPrefix}/*`]);
          yield* git(cwd, ["update-ref", `${refPrefix}/${initialBranch}`, "HEAD"]);
        }
        const lock = `.git/${refPrefix}/other.lock`;
        yield* writeTextFile(cwd, lock, "");

        const result = yield* driver.pullCurrentBranch(cwd);

        assert.equal(result.status, "pulled");
        assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead);
        assert.equal(yield* git(cwd, ["rev-parse", `${refPrefix}/${initialBranch}`]), remoteHead);
        assert.isTrue(yield* fs.exists(`${cwd}/${lock}`));
        assert.equal(yield* fs.readFileString(`${cwd}/${lock}`), "");
      }).pipe(Effect.provide(layerTest)),
  );

  it.effect("reports an upstream lock after the fallback without removing it", () =>
    Effect.gen(function* () {
      const driver = yield* GitVcsDriver.GitVcsDriver;
      const fs = yield* FileSystem.FileSystem;
      const { cwd, initialBranch } = yield* cloneBehindRemote;
      const before = yield* git(cwd, ["rev-parse", "HEAD"]);
      const lock = `.git/refs/remotes/origin/${initialBranch}.lock`;
      yield* writeTextFile(cwd, lock, "");

      const error = yield* Effect.flip(driver.pullCurrentBranch(cwd));

      assert.include(error.detail, "could not update a local reference");
      assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), before);
      assert.isTrue(yield* fs.exists(`${cwd}/${lock}`));
    }).pipe(Effect.provide(layerTest)),
  );

  it.effect.each(["branch", "HEAD"] as const)(
    "does not retry a failed fetch if another operation moved %s",
    (moved) =>
      Effect.gen(function* () {
        const fixtureDriver = yield* GitVcsDriver.GitVcsDriver;
        const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
        const { cwd, updater } = yield* cloneBehindRemote;
        yield* git(updater, ["push", "origin", "HEAD:refs/heads/other"]);
        yield* writeTextFile(cwd, ".git/refs/remotes/origin/other.lock", "");
        let pulls = 0;
        const spawner = ChildProcessSpawner.make((command) =>
          Effect.gen(function* () {
            const handle = yield* delegate.spawn(command);
            if (!ChildProcess.isStandardCommand(command) || !command.args.includes("pull")) {
              return handle;
            }
            pulls += 1;
            return ChildProcessSpawner.makeHandle({
              ...handle,
              exitCode: handle.exitCode.pipe(
                Effect.tap(() =>
                  git(
                    cwd,
                    moved === "branch"
                      ? ["checkout", "-b", "moved"]
                      : ["commit", "--allow-empty", "-m", "concurrent commit"],
                  ).pipe(
                    Effect.provideService(GitVcsDriver.GitVcsDriver, fixtureDriver),
                    Effect.orDie,
                  ),
                ),
              ),
            });
          }),
        );
        const driver = yield* makeGitVcsDriverCore().pipe(
          Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
        );

        const error = yield* Effect.flip(driver.pullCurrentBranch(cwd));

        assert.include(error.detail, "could not update a local reference");
        assert.equal(pulls, 1);
      }).pipe(Effect.scoped, Effect.provide(layerServerConfig.pipe(Layer.provideMerge(layerTest)))),
  );

  it.effect.each(["stdout", "stderr"] as const)(
    "reports a fast-forward whose %s passes the cap as pulled",
    (stream) =>
      Effect.scoped(
        Effect.gen(function* () {
          const driver = yield* withPaddedPull(stream);
          yield* Effect.gen(function* () {
            const { cwd, remoteHead } = yield* cloneBehindRemote;

            const result = yield* driver.pullCurrentBranch(cwd);

            assert.equal(result.status, "pulled");
            assert.equal(yield* git(cwd, ["rev-parse", "HEAD"]), remoteHead);
          }).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, driver));
        }),
      ).pipe(
        Effect.provide(
          layerServerConfig.pipe(
            Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
          ),
        ),
      ),
  );

  it.effect("still fails a pull that Git rejects, whatever its output size", () =>
    Effect.scoped(
      Effect.gen(function* () {
        const pulls: ReadonlyArray<string>[] = [];
        const driver = yield* withPaddedPull("stderr", (args) => pulls.push(args));
        yield* Effect.gen(function* () {
          const { cwd } = yield* cloneBehindRemote;
          yield* writeTextFile(cwd, "local.txt", "local\n");
          yield* git(cwd, ["add", "local.txt"]);
          yield* git(cwd, ["commit", "-m", "local commit"]);

          const error = yield* Effect.flip(driver.pullCurrentBranch(cwd));

          assert.equal(error.operation, "GitVcsDriver.pullCurrentBranch.pull");
          assert.equal(error.exitCode, 128);
          assert.notInclude(error.detail, "exceeded");
          assert.lengthOf(pulls, 1);
        }).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, driver));
      }),
    ).pipe(
      Effect.provide(
        layerServerConfig.pipe(
          Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
        ),
      ),
    ),
  );
});

it.effect("retries an in-flight ref snapshot invalidated by a mutation", () =>
  Effect.scoped(
    Effect.gen(function* () {
      const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
      const firstWorktreeScanStarted = yield* Deferred.make<void>();
      const firstRefScanCompleted = yield* Deferred.make<void>();
      const releaseFirstWorktreeScan = yield* Deferred.make<void>();
      const delayFirstWorktreeScan = yield* Ref.make(true);
      const refScans = yield* Ref.make(0);
      const coordinatingSpawner = ChildProcessSpawner.make((command) =>
        Effect.gen(function* () {
          if (!ChildProcess.isStandardCommand(command)) {
            return yield* Effect.die("expected a standard Git command");
          }
          const isWorktreeScan =
            command.args.includes("worktree") && command.args.includes("--porcelain");
          if (isWorktreeScan && (yield* Ref.getAndSet(delayFirstWorktreeScan, false))) {
            yield* Deferred.succeed(firstWorktreeScanStarted, undefined);
            yield* Deferred.await(releaseFirstWorktreeScan);
          }
          const handle = yield* delegate.spawn(command);
          const isRefScan =
            command.args.includes("for-each-ref") &&
            command.args.includes("refs/heads") &&
            command.args.includes("refs/remotes");
          if (!isRefScan) return handle;
          const scan = yield* Ref.updateAndGet(refScans, (count) => count + 1);
          return scan === 1
            ? ChildProcessSpawner.makeHandle({
                ...handle,
                exitCode: handle.exitCode.pipe(
                  Effect.tap(() => Deferred.succeed(firstRefScanCompleted, undefined)),
                ),
              })
            : handle;
        }),
      );
      const driver = yield* makeGitVcsDriverCore().pipe(
        Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, coordinatingSpawner),
      );
      const cwd = yield* makeTmpDir();
      yield* initRepoWithCommit(cwd).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, driver));

      const inFlight = yield* driver
        .listRefs({ cwd, refresh: true, limit: 100 })
        .pipe(Effect.forkChild({ startImmediately: true }));
      yield* Deferred.await(firstWorktreeScanStarted);
      yield* Deferred.await(firstRefScanCompleted);

      yield* driver.createRef({ cwd, refName: "feature/during-refresh" });
      yield* Deferred.succeed(releaseFirstWorktreeScan, undefined);

      const refs = yield* Fiber.join(inFlight);
      assert.isTrue(refs.refs.some((ref) => ref.name === "feature/during-refresh"));
      assert.equal(yield* Ref.get(refScans), 2);
    }),
  ).pipe(
    Effect.provide(
      layerServerConfig.pipe(
        Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
      ),
    ),
  ),
);

it.effect("invalidates a ref snapshot when a mutation fails after changing Git", () =>
  Effect.scoped(
    Effect.gen(function* () {
      const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
      const partiallyFailingSpawner = ChildProcessSpawner.make((command) =>
        Effect.gen(function* () {
          if (!ChildProcess.isStandardCommand(command)) {
            return yield* Effect.die("expected a standard Git command");
          }
          if (command.args[0] === "branch" && command.args.at(-1) === "feature/partial-failure") {
            const handle = yield* delegate.spawn(command);
            yield* handle.exitCode;
            return makeNonRepositoryHandle();
          }
          return yield* delegate.spawn(command);
        }),
      );
      const driver = yield* makeGitVcsDriverCore().pipe(
        Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, partiallyFailingSpawner),
      );
      const cwd = yield* makeTmpDir();
      yield* initRepoWithCommit(cwd).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, driver));
      yield* driver.listRefs({ cwd, refresh: true });

      yield* driver.createRef({ cwd, refName: "feature/partial-failure" }).pipe(Effect.flip);

      const refs = yield* driver.listRefs({ cwd });
      assert.isTrue(refs.refs.some((ref) => ref.name === "feature/partial-failure"));
    }),
  ).pipe(
    Effect.provide(
      layerServerConfig.pipe(
        Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
      ),
    ),
  ),
);

it.effect("marks the current branch when worktree metadata is unavailable", () =>
  Effect.scoped(
    Effect.gen(function* () {
      const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
      const incompleteMetadataSpawner = ChildProcessSpawner.make((command) =>
        Effect.gen(function* () {
          if (!ChildProcess.isStandardCommand(command)) {
            return yield* Effect.die("expected a standard Git command");
          }
          const isWorktreeRoot =
            command.args.includes("rev-parse") && command.args.includes("--show-toplevel");
          const isWorktreeList =
            command.args.includes("worktree") && command.args.includes("--porcelain");
          if (isWorktreeRoot || isWorktreeList) {
            return makeNonRepositoryHandle();
          }
          return yield* delegate.spawn(command);
        }),
      );
      const driver = yield* makeGitVcsDriverCore().pipe(
        Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, incompleteMetadataSpawner),
      );
      const cwd = yield* makeTmpDir();
      const { initialBranch } = yield* initRepoWithCommit(cwd).pipe(
        Effect.provideService(GitVcsDriver.GitVcsDriver, driver),
      );

      const refs = yield* driver.listRefs({ cwd, refresh: true });

      assert.isTrue(refs.isRepo);
      assert.isTrue(refs.refs.find((ref) => ref.name === initialBranch)?.current);
    }),
  ).pipe(
    Effect.provide(
      layerServerConfig.pipe(
        Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
      ),
    ),
  ),
);

it.effect("ignores worktree metadata for directories that no longer exist", () =>
  Effect.scoped(
    Effect.gen(function* () {
      const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
      const missingWorktreePath = "/missing/deleted-worktree";
      const staleWorktreeSpawner = ChildProcessSpawner.make((command) =>
        Effect.gen(function* () {
          if (!ChildProcess.isStandardCommand(command)) {
            return yield* Effect.die("expected a standard Git command");
          }
          const isWorktreeList =
            command.args.includes("worktree") && command.args.includes("--porcelain");
          if (isWorktreeList) {
            return makeSuccessfulHandle(
              `worktree ${missingWorktreePath}\0HEAD deadbeef\0branch refs/heads/stale-worktree\0\0`,
            );
          }
          return yield* delegate.spawn(command);
        }),
      );
      const driver = yield* makeGitVcsDriverCore().pipe(
        Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, staleWorktreeSpawner),
      );
      const cwd = yield* makeTmpDir();
      yield* initRepoWithCommit(cwd).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, driver));
      yield* git(cwd, ["branch", "stale-worktree"]).pipe(
        Effect.provideService(GitVcsDriver.GitVcsDriver, driver),
      );

      const refs = yield* driver.listRefs({ cwd, refresh: true });

      assert.equal(refs.refs.find((ref) => ref.name === "stale-worktree")?.worktreePath, null);
    }),
  ).pipe(
    Effect.provide(
      layerServerConfig.pipe(
        Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
      ),
    ),
  ),
);

it.effect("reads worktrees from git that rejects worktree list -z", () =>
  Effect.scoped(
    Effect.gen(function* () {
      const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
      // Git before 2.36 has no `worktree list -z` and exits with a usage error.
      const oldGitSpawner = ChildProcessSpawner.make((command) =>
        Effect.gen(function* () {
          if (!ChildProcess.isStandardCommand(command)) {
            return yield* Effect.die("expected a standard Git command");
          }
          if (command.args.includes("worktree") && command.args.includes("-z")) {
            return ChildProcessSpawner.makeHandle({
              pid: ChildProcessSpawner.ProcessId(1),
              exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(129)),
              isRunning: Effect.succeed(false),
              kill: () => Effect.void,
              unref: Effect.succeed(Effect.void),
              stdin: Sink.drain,
              stdout: Stream.empty,
              stderr: Stream.encodeText(Stream.make("error: unknown switch `z'")),
              all: Stream.empty,
              getInputFd: () => Sink.drain,
              getOutputFd: () => Stream.empty,
            });
          }
          return yield* delegate.spawn(command);
        }),
      );
      const driver = yield* makeGitVcsDriverCore().pipe(
        Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, oldGitSpawner),
      );
      const cwd = yield* makeTmpDir();
      yield* initRepoWithCommit(cwd).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, driver));
      const worktreePath = NodeFS.realpathSync.native(yield* makeTmpDir());
      yield* git(cwd, ["worktree", "add", "-b", "feature/old-git", worktreePath]).pipe(
        Effect.provideService(GitVcsDriver.GitVcsDriver, driver),
      );

      const refs = yield* driver.listRefs({ cwd, refresh: true });
      const listedPath = refs.refs.find((ref) => ref.name === "feature/old-git")?.worktreePath;
      assert.equal(listedPath && NodeFS.realpathSync.native(listedPath), worktreePath);

      const paths = yield* driver.listWorktreePaths(cwd);
      assert.include(
        paths.map((listed) => NodeFS.realpathSync.native(listed)),
        worktreePath,
      );
    }),
  ).pipe(
    Effect.provide(
      layerServerConfig.pipe(
        Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
      ),
    ),
  ),
);

it.effect("does not report a separate git dir as the worktree of its branch", () =>
  Effect.scoped(
    Effect.gen(function* () {
      const driver = yield* GitVcsDriver.GitVcsDriver;
      const cwd = yield* makeTmpDir();
      const gitDir = yield* makeTmpDir();
      yield* git(cwd, ["init", `--separate-git-dir=${gitDir}`]);
      yield* git(cwd, ["config", "user.email", "test@test.com"]);
      yield* git(cwd, ["config", "user.name", "Test"]);
      yield* git(cwd, ["commit", "--allow-empty", "-m", "init"]);

      const refs = yield* driver.listRefs({ cwd, refresh: true });
      const worktreePath = refs.refs.find((ref) => ref.current)?.worktreePath;

      assert.equal(
        worktreePath && NodeFS.realpathSync.native(worktreePath),
        NodeFS.realpathSync.native(cwd),
      );
    }),
  ).pipe(Effect.provide(layerTest)),
);

it.effect.each([
  {
    name: "HTTPS credentials",
    stderr: "fatal: Authentication failed for",
    expected: "could not authenticate",
  },
  {
    name: "SSH credentials",
    stderr: "git@example.com: Permission denied (publickey).",
    expected: "could not authenticate",
  },
  {
    name: "disabled prompts",
    stderr: "fatal: could not read Username: terminal prompts disabled",
    expected: "could not authenticate",
  },
  {
    name: "DNS failure",
    stderr: "fatal: Could not resolve host: example.com",
    expected: "could not reach the remote",
  },
  {
    name: "connection failure",
    stderr: "ssh: connect to host example.com port 22: Connection refused",
    expected: "could not reach the remote",
  },
  {
    name: "missing remote",
    stderr: "remote: Repository not found.",
    expected: "could not access the remote repository",
  },
  {
    name: "invalid remote",
    stderr: "fatal: remote does not appear to be a git repository",
    expected: "could not access the remote repository",
  },
  {
    name: "reference lock",
    stderr: "error: cannot lock ref 'refs/remotes/origin/main': is at abc but expected def",
    expected: "could not update a local reference",
    attempts: 2,
  },
  {
    name: "lock file",
    stderr: "fatal: Unable to create '/repo/.git/FETCH_HEAD.lock': File exists.",
    expected: "could not update a local reference",
    attempts: 2,
  },
  {
    name: "reference race on Git 2.53",
    stderr: "error: fetching ref refs/remotes/origin/main failed: incorrect old value provided",
    expected: "could not update a local reference",
    attempts: 2,
  },
  {
    name: "held reference lock on Git 2.53",
    stderr: "error: fetching ref refs/remotes/origin/main failed: reference already exists",
    expected: "could not update a local reference",
    attempts: 2,
  },
  {
    name: "unrelated remote chatter",
    stderr:
      "remote: Help: authentication failed, connection refused, cannot lock ref\nremote: unrelated service error",
    expected: "git fetch origin failed",
  },
  {
    name: "HTTPS DNS failure",
    stderr:
      "fatal: unable to access 'https://example.com/repo.git/': Could not resolve host: example.com",
    expected: "could not reach the remote",
  },
  {
    name: "unknown failure",
    stderr: "fatal: unexpected remote failure",
    expected: "git fetch origin failed",
  },
] as const)("reports $name during fetch without retaining remote output", (scenario) =>
  Effect.gen(function* () {
    const secret = "secret-fetch-token";
    const stderr = `${scenario.stderr}\nhttps://user:${secret}@example.com/private?token=${secret}`;
    const attempts = yield* Ref.make(0);
    const spawner = ChildProcessSpawner.make((command) =>
      Effect.gen(function* () {
        if (!ChildProcess.isStandardCommand(command))
          return yield* Effect.die("expected Git command");
        if (command.args[0] !== "fetch") return makeNonRepositoryHandle();
        assert.deepEqual(command.args, ["fetch", "--quiet", "--", "origin"]);
        assert.equal(command.options.env?.LANGUAGE, "C");
        assert.equal(command.options.env?.GIT_TERMINAL_PROMPT, "0");
        yield* Ref.update(attempts, (count) => count + 1);
        return ChildProcessSpawner.makeHandle({
          pid: ChildProcessSpawner.ProcessId(1),
          exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(128)),
          isRunning: Effect.succeed(false),
          kill: () => Effect.void,
          unref: Effect.succeed(Effect.void),
          stdin: Sink.drain,
          stdout: Stream.encodeText(Stream.make(secret)),
          stderr: Stream.encodeText(Stream.make(stderr)),
          all: Stream.empty,
          getInputFd: () => Sink.drain,
          getOutputFd: () => Stream.empty,
        });
      }),
    );
    const driver = yield* makeGitVcsDriverCore().pipe(
      Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
    );
    const cwd = yield* makeTmpDir();
    const error = yield* driver.fetchRemote({ cwd, remoteName: "origin" }).pipe(Effect.flip);
    assert.include(error.detail, scenario.expected);
    assert.equal(error.exitCode, 128);
    assert.equal(error.stderrLength, stderr.length);
    assert.equal(error.stdoutLength, secret.length);
    assert.notInclude(error.message, secret);
    assert.notInclude(yield* encodeGitCommandError(error), secret);
    assert.notProperty(error, "stderr");
    assert.notProperty(error, "args");
    assert.equal(yield* Ref.get(attempts), "attempts" in scenario ? scenario.attempts : 1);
  }).pipe(
    Effect.provide(
      layerServerConfig.pipe(
        Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
      ),
    ),
  ),
);

it.effect("retries a fetch that lost a ref update to another Git process", () =>
  Effect.gen(function* () {
    const attempts = yield* Ref.make(0);
    const spawner = ChildProcessSpawner.make((command) =>
      Effect.gen(function* () {
        if (!ChildProcess.isStandardCommand(command))
          return yield* Effect.die("expected Git command");
        if (command.args[0] !== "fetch") return makeNonRepositoryHandle();
        const attempt = yield* Ref.updateAndGet(attempts, (count) => count + 1);
        if (attempt > 1) return makeSuccessfulHandle("");
        return ChildProcessSpawner.makeHandle({
          ...makeNonRepositoryHandle(),
          exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
          stderr: Stream.encodeText(
            Stream.make(
              "error: fetching ref refs/remotes/origin/main failed: incorrect old value provided",
            ),
          ),
        });
      }),
    );
    const driver = yield* makeGitVcsDriverCore().pipe(
      Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
    );
    const cwd = yield* makeTmpDir();
    yield* driver.fetchRemote({ cwd, remoteName: "origin", refName: "main" });
    assert.equal(yield* Ref.get(attempts), 2);
  }).pipe(
    Effect.provide(
      layerServerConfig.pipe(
        Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
      ),
    ),
  ),
);