Skip to content

armgit / cogit

Public Imported from GitHub
0

Git for the intent behind agent-written code. Records what your coding agent decided and why, and links every commit to it.

18.1 KB
Raw Blame
import { execFile } from "node:child_process";
import { existsSync } from "node:fs";
import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { basename, join } from "node:path";
import { promisify } from "node:util";
import { beforeEach, describe, expect, it } from "vitest";
import {
  allowSkill,
  readPluginInstalled,
  readProjectSetup,
  readRetention,
  readSkillAllowed,
  SKILL_RULE,
  setRetention,
} from "../src/agents/claude/settings.js";
import { init } from "../src/commands/init.js";
import { cogitGitignore, ensureCogitGitignore, ensureGitignore, findGitRoot } from "../src/git.js";
import { loadConfig, loadProject, loadRegistry } from "../src/store/stores.js";
import { envFor, fakeIo, T0, tempDir } from "./helpers.js";

let dir: string;
let root: string;
let env: NodeJS.ProcessEnv;
beforeEach(async () => {
  dir = await tempDir();
  root = join(dir, "proj");
  await mkdir(root);
  env = envFor(dir);
});

const settingsFile = () => join(dir, "claude", "settings.json");
const readText = (f: string) => readFile(f, "utf8");
/** The warnings a test is about; the test repos have no git email, which init also notes. */
const warningsOf = (io: { warnings: string[] }) =>
  io.warnings.filter((w) => !w.startsWith("git has no user.email"));

describe("init", () => {
  it("leaves project settings it cannot read alone and says so", async () => {
    await mkdir(join(root, ".claude"));
    await writeFile(join(root, ".claude", "settings.json"), "{ not json");
    const io = fakeIo(env);
    await init(root, { name: "p", retention: "keep", allowSkill: false, language: "auto" }, io);
    expect(io.warnings.join("\n")).toMatch(/could not read the project's Claude Code settings/);
    expect(await readText(join(root, ".claude", "settings.json"))).toBe("{ not json");
  });

  it("refuses a directory outside any git repository, before asking anything", async () => {
    const outside = await mkdtemp(join(process.env.COGIT_TEST_NO_GIT ?? tmpdir(), "p-"));
    const io = fakeIo(env, [], true);
    await expect(init(outside, {}, io)).rejects.toThrow(/not in a git repository.*git init/);
    expect(existsSync(join(outside, ".cogit"))).toBe(false);
  });

  it("recognizes a pre-rename .codelineage project and renames it", async () => {
    await mkdir(join(root, ".codelineage"), { recursive: true });
    await writeFile(
      join(root, ".codelineage", "project.json"),
      `${JSON.stringify({ schema: 1, name: "old" })}\n`,
    );
    const io = fakeIo(env);

    await init(root, { retention: "keep" }, io);

    expect(io.lines[0]).toBe(`Already initialized: old (${root})`);
    expect(await loadProject(root)).toMatchObject({ ok: true, data: { name: "old" } });
    expect(existsSync(join(root, ".codelineage"))).toBe(false);
  });

  it("creates the project with defaults and registers it", async () => {
    const io = fakeIo(env);
    await init(root, {}, io);
    expect(await loadProject(root)).toMatchObject({ ok: true, data: { name: "proj" } });
    expect(await readdir(join(root, ".cogit"))).toContain("sessions");
    expect(await loadRegistry(env)).toMatchObject({
      ok: true,
      data: { projects: [{ path: root, name: "proj" }] },
    });
    expect((await loadConfig(env)).language).toBe("auto");
    // name, git policy and retention; the rest take their defaults
    expect(io.asked).toHaveLength(2);
    expect(await readProjectSetup(root)).toBe(true);
    expect(await readSkillAllowed(env)).toBe(true);
    expect(io.lines.at(-2)).toMatch(/^Initialized proj/);
    // No plugin list in this Claude Code config: the last line says how to install it.
    expect(io.lines.at(-1)).toContain("claude plugin install cogit --marketplace erensrtky/cogit");
    // No answer shares nothing: the root rule hides the whole folder.
    expect(await readText(join(root, ".gitignore"))).toContain("/.cogit/");
    expect(existsSync(join(root, ".cogit", ".gitignore"))).toBe(false);
    expect(io.lines).toContain(
      "Conversations are stored in .cogit/conversations/ and stay on this machine.",
    );
  });

  it("skips the install hint when Claude Code lists the plugin as installed", async () => {
    const list = join(env.CLAUDE_CONFIG_DIR ?? "", "plugins", "installed_plugins.json");
    await mkdir(join(list, ".."), { recursive: true });
    const write = (plugins: unknown) => writeFile(list, JSON.stringify({ version: 2, plugins }));

    await write({ "cogit@cogit": [{ scope: "user", version: "0.19.0" }] });
    expect(await readPluginInstalled(env)).toBe(true);
    const io = fakeIo(env);
    await init(root, {}, io);
    expect(io.lines.at(-1)).toBe(
      "Next: work with Claude Code here, then run `cogit open` to see the map.",
    );

    // Another plugin only, an empty entry, or a list of another shape.
    await write({ "other@market": [{ scope: "user" }] });
    expect(await readPluginInstalled(env)).toBe(false);
    await write({ "cogit@cogit": [] });
    expect(await readPluginInstalled(env)).toBe(false);
    await write(["cogit@cogit"]);
    expect(await readPluginInstalled(env)).toBeUndefined();
    await writeFile(list, "[1,");
    expect(await readPluginInstalled(env)).toBeUndefined();
    expect(await readPluginInstalled({ CLAUDE_CONFIG_DIR: join(dir, "nowhere") })).toBeUndefined();
  });

  it("writes the project ignore file once for a shared project", async () => {
    await mkdir(join(root, ".cogit"));
    expect(await ensureCogitGitignore(root, "share")).toBe(true);
    expect(await ensureCogitGitignore(root, "share")).toBe(false);
    expect(await readText(join(root, ".cogit", ".gitignore"))).toBe(cogitGitignore());
    // Only the marker travels through commits; the sessions have their own refs.
    expect(await readText(join(root, ".cogit", ".gitignore"))).not.toContain("sessions");
  });

  it("asks nothing when every answer is given as an option", async () => {
    await mkdir(join(root, ".git"));
    const io = fakeIo(env, [], true);
    await init(
      root,
      {
        name: "P",
        git: "share",
        retention: "keep",
        allowSkill: false,
        projectSettings: false,
        language: "Turkish",
      },
      io,
    );
    expect((await loadConfig(env)).language).toBe("Turkish");
    // Commit policy: the root file stays untouched, the project writes its own rules.
    expect(existsSync(join(root, ".gitignore"))).toBe(false);
    expect(await readText(join(root, ".cogit", ".gitignore"))).toBe(cogitGitignore());
  });

  it("asks for the git policy in a repo and writes .gitignore", async () => {
    await mkdir(join(root, ".git"));
    const io = fakeIo(env, ["ignore"]);
    await init(root, {}, io);
    expect(await readText(join(root, ".gitignore"))).toBe("# cogit\n/.cogit/\n");
    expect(io.lines).toContain("Added to .gitignore: /.cogit/");
  });

  it("raises transcript retention when asked, keeping other settings", async () => {
    await mkdir(join(dir, "claude"));
    await writeFile(settingsFile(), JSON.stringify({ model: "x" }));
    const io = fakeIo(env, ["share", "365"]);
    await init(root, {}, io);
    expect(JSON.parse(await readText(settingsFile()))).toEqual({
      model: "x",
      cleanupPeriodDays: 365,
      permissions: { allow: [SKILL_RULE] },
    });
    // two changes in one run keep a single backup holding the original
    const backups = (await readdir(join(dir, "claude"))).filter((f) =>
      f.startsWith("settings.json.cogit-bak-"),
    );
    expect(backups).toHaveLength(1);
    expect(JSON.parse(await readText(join(dir, "claude", backups[0] as string)))).toEqual({
      model: "x",
    });
    expect(io.lines.some((l) => l.includes("Set cleanupPeriodDays to 365"))).toBe(true);
  });

  it("does not ask about retention when it is already long", async () => {
    await mkdir(join(dir, "claude"));
    await writeFile(settingsFile(), JSON.stringify({ cleanupPeriodDays: 365 }));
    const io = fakeIo(env);
    await init(root, {}, io);
    expect(io.lines).toContain("Claude Code keeps transcripts for 365 days.");
    expect(io.asked).toHaveLength(1);
  });

  it("applies a retention option without asking, and skips an unchanged one", async () => {
    await init(
      root,
      {
        name: "p",
        git: "share",
        retention: 90,
        allowSkill: false,
        projectSettings: false,
        language: "auto",
      },
      fakeIo(env, [], true),
    );
    expect(await readRetention(env)).toBe(90);
    const again = join(dir, "other");
    await mkdir(again);
    const io = fakeIo(env, [], true);
    await init(
      again,
      { name: "o", git: "share", retention: 90, allowSkill: false, projectSettings: false },
      io,
    );
    expect(io.lines.some((l) => l.includes("cleanupPeriodDays"))).toBe(false);
  });

  it("applies a retention option to a project that is already set up", async () => {
    const options = {
      name: "p",
      git: "share",
      allowSkill: false,
      projectSettings: false,
      language: "auto",
    } as const;
    await init(root, { ...options, retention: "keep" }, fakeIo(env, [], true));
    await init(root, { ...options, retention: 90 }, fakeIo(env, [], true));
    expect(await readRetention(env)).toBe(90);
  });

  it("warns and moves on when Claude Code settings are unreadable", async () => {
    await mkdir(join(dir, "claude"));
    await writeFile(settingsFile(), "{oops");
    const io = fakeIo(env);
    await init(root, {}, io);
    expect(warningsOf(io)[0]).toMatch(/retention left unchanged/);
    expect(warningsOf(io)[1]).toMatch(/add Skill\(cogit:record\) to permissions.allow yourself/);
    expect(await readText(settingsFile())).toBe("{oops");
  });

  it("takes the conversation's language without asking, and the option when given", async () => {
    const io = fakeIo(env, ["share", "keep"]);
    await init(root, {}, io);
    expect((await loadConfig(env)).language).toBe("auto");
    expect(io.asked.some((q) => q.startsWith("Language"))).toBe(false);
    const second = join(dir, "second");
    await mkdir(second);
    await init(second, { language: "Turkish" }, fakeIo(env));
    expect((await loadConfig(env)).language).toBe("Turkish");
  });

  it("adds the skill rule to existing permissions, and only once", async () => {
    await mkdir(join(dir, "claude"));
    await writeFile(
      settingsFile(),
      JSON.stringify({ permissions: { allow: ["Bash(ls)"], deny: [] } }),
    );
    const io = fakeIo(env, ["share", "keep", "yes"]);
    await init(root, {}, io);
    expect(JSON.parse(await readText(settingsFile())).permissions).toEqual({
      allow: ["Bash(ls)", SKILL_RULE],
      deny: [],
    });
    expect(io.lines.some((l) => l.startsWith(`Allowed ${SKILL_RULE} in`))).toBe(true);
    const second = join(dir, "second");
    await mkdir(second);
    const again = fakeIo(env);
    await init(second, {}, again);
    expect(again.asked.some((q) => q.startsWith("Let the agent"))).toBe(false);
  });

  it("leaves permissions alone when declined with the flag", async () => {
    const io = fakeIo(env, ["share", "keep"]);
    await init(root, { allowSkill: false }, io);
    expect(await readSkillAllowed(env)).toBe(false);
    expect(io.lines).toContain(
      `Claude Code will ask before each cogit update; allow ${SKILL_RULE} to stop that.`,
    );
  });

  it("warns when permissions.allow has an unexpected shape", async () => {
    await mkdir(join(dir, "claude"));
    await writeFile(settingsFile(), JSON.stringify({ permissions: { allow: "all" } }));
    const io = fakeIo(env);
    await init(root, {}, io);
    expect(warningsOf(io)[0]).toMatch(/could not read Claude Code permissions/);
    expect(JSON.parse(await readText(settingsFile()))).toEqual({ permissions: { allow: "all" } });
  });

  it("can allow the skill for an already initialized project", async () => {
    await init(root, { allowSkill: false }, fakeIo(env));
    const io = fakeIo(env, [], true);
    await init(root, { allowSkill: true }, io);
    expect(await readSkillAllowed(env)).toBe(true);
    expect(io.lines.at(-1)).toBe(`Already initialized: proj (${root})`);
  });

  it("is idempotent for an initialized project", async () => {
    await init(root, {}, fakeIo(env));
    const io = fakeIo(env, [], true);
    await init(root, {}, io);
    expect(io.lines).toEqual([`Already initialized: proj (${root})`]);
    const reg = await loadRegistry(env);
    expect(reg.found && reg.ok && reg.data.projects).toHaveLength(1);
  });

  it("refuses a corrupt project.json", async () => {
    await mkdir(join(root, ".cogit"));
    await writeFile(join(root, ".cogit", "project.json"), "[]");
    await expect(init(root, {}, fakeIo(env))).rejects.toThrow(/not a JSON object/);
  });

  it("still initializes when the global dir is not writable", async () => {
    await writeFile(join(dir, "home"), "a file, not a dir");
    const io = fakeIo(env);
    await init(root, {}, io);
    expect(warningsOf(io).map((w) => w.split(":")[0])).toEqual([
      "could not save global config",
      "could not register the project in the global panel",
    ]);
    expect(await loadProject(root)).toMatchObject({ ok: true });
  });
});

describe("git helpers", () => {
  it("finds the repo root from a subdirectory, also for worktree .git files", async () => {
    await writeFile(join(root, ".git"), "gitdir: /elsewhere");
    const sub = join(root, "a", "b");
    await mkdir(sub, { recursive: true });
    expect(await findGitRoot(sub)).toBe(root);
    expect(await findGitRoot("/")).toBeUndefined();
  });

  it("adds the ignore line under the ignore policy and removes it when switching", async () => {
    const gi = join(root, ".gitignore");
    await writeFile(gi, "node_modules/");
    expect(await ensureGitignore(root, "share")).toEqual([]);
    expect(await readText(gi)).toBe("node_modules/");
    expect(await ensureGitignore(root, "ignore")).toEqual(["/.cogit/"]);
    expect(await ensureGitignore(root, "ignore")).toEqual([]);
    const text = await readText(gi);
    expect(text).toBe("node_modules/\n# cogit\n/.cogit/\n");
    expect(text.match(/# cogit/g)).toHaveLength(1);
    // Switching back removes only our line, never the user's.
    expect(await ensureGitignore(root, "share")).toEqual([]);
    expect(await readText(gi)).toBe("node_modules/\n# cogit\n");
    expect(basename(gi)).toBe(".gitignore");
  });

  it("installs the git hooks with the ignore policy too, for the trailer", async () => {
    const run = promisify(execFile);
    await run("git", ["-C", root, "init", "-q"]);
    await init(
      root,
      { name: "P", git: "ignore", retention: "keep", allowSkill: false, language: "auto" },
      fakeIo(env),
    );
    const hook = await readText(join(root, ".git", "hooks", "prepare-commit-msg"));
    expect(hook).toContain("hook prepare-commit-msg --dir");
    // Running init again on the ignore project keeps the hooks current.
    await rm(join(root, ".git", "hooks", "prepare-commit-msg"));
    await init(root, { allowSkill: false }, fakeIo(env));
    expect(existsSync(join(root, ".git", "hooks", "prepare-commit-msg"))).toBe(true);
  });

  it("warns when git has no email to tell this person's sessions apart by", async () => {
    const run = promisify(execFile);
    const git = (...args: string[]) => run("git", ["-C", root, ...args]);
    await git("init", "-q");
    const options = { name: "P", retention: "keep", allowSkill: false, language: "auto" } as const;
    const bare = fakeIo(env);
    await init(root, options, bare);
    expect(bare.warnings.join("\n")).toMatch(/no user\.email/);
    await rm(join(root, ".cogit"), { recursive: true });
    await git("config", "user.email", "[email protected]");
    const set = fakeIo(env);
    await init(root, options, set);
    expect(set.warnings.join("\n")).not.toMatch(/user\.email/);
  });

  it("restores the send hook when init runs again on a shared project", async () => {
    const run = promisify(execFile);
    const git = (...args: string[]) => run("git", ["-C", root, ...args]);
    await git("init", "-q");
    await git("config", "user.email", "[email protected]");
    await git("config", "user.name", "Tester");
    await init(
      root,
      { name: "P", git: "share", retention: "keep", allowSkill: false, language: "auto" },
      fakeIo(env),
    );
    const hook = join(root, ".git", "hooks", "pre-push");
    expect(existsSync(hook)).toBe(true);
    await rm(hook);
    const io = fakeIo(env);
    await init(root, { retention: "keep" }, io);
    expect(io.lines[0]).toMatch(/^Already initialized: P/);
    expect(existsSync(hook)).toBe(true);
  });

  it("warns about an unwritable root ignore file and still initializes", async () => {
    // A directory where .gitignore belongs makes the write fail.
    await mkdir(join(root, ".git"));
    await mkdir(join(root, ".gitignore"));
    const io = fakeIo(env);
    await init(
      root,
      { name: "P", git: "ignore", retention: "keep", allowSkill: false, language: "auto" },
      io,
    );
    expect(io.warnings).toEqual(
      expect.arrayContaining([expect.stringMatching(/^could not update \.gitignore: /)]),
    );
    expect(await loadProject(root)).toMatchObject({ ok: true });
  });
});

describe("setRetention", () => {
  it("rejects invalid day counts and unparsable files", async () => {
    await expect(setRetention(0, env, T0)).rejects.toThrow(/at least 1/);
    await mkdir(join(dir, "claude"));
    await writeFile(settingsFile(), "[1]");
    await expect(setRetention(90, env, T0)).rejects.toThrow(/not a valid JSON object/);
    expect(await readRetention(env)).toBeUndefined();
  });

  it("creates the settings file without a backup when there was none", async () => {
    expect(await setRetention(90, env, T0)).toEqual({ file: settingsFile() });
    expect(await readRetention(env)).toBe(90);
  });
});

describe("allowSkill", () => {
  it("treats a non-object permissions value as unreadable and refuses to change it", async () => {
    await mkdir(join(dir, "claude"));
    await writeFile(settingsFile(), JSON.stringify({ permissions: ["x"] }));
    expect(await readSkillAllowed(env)).toBeUndefined();
    await expect(allowSkill(env, T0)).rejects.toThrow(/unexpected permissions.allow/);
  });

  it("keeps the file as it is when the rule is already there", async () => {
    await allowSkill(env, T0);
    await allowSkill(env, T0);
    expect(JSON.parse(await readText(settingsFile()))).toEqual({
      permissions: { allow: [SKILL_RULE] },
    });
  });
});