Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

14.7 KB
Raw Blame
// Copyright 2026 Ix Infrastructure Inc.

/**
 * diff-mode-conflict.test.ts — Mutation-verified tests for diff.ts
 *
 * Tests detectDiffModeConflict, mergeDiffOptions, and loadFileFromDisk with
 * assertions strong enough to survive adversarial mutation testing.
 *
 * Every assertion is discriminating: if you swap the conflict branches, invert
 * a condition, change an operator, or alter the error message wording, at
 * least one test goes red.
 *
 * No network access. No backend dependency. No global state mutation.
 * All temp paths use OS tmpdir. Pure unit tests.
 *
 * REVIEW ITEMS APPLIED (KageBinary style):
 * - Assert FULL message text in conflict tests (not partial substring)
 * - No duplicate tests — each test covers a unique behavioral path
 * - No weak assertions (no toBeDefined, no toBeTruthy)
 * - Every mutation is expected to cause a test failure
 * - Platform-safe temp paths throughout
 */

import { mkdtempSync, rmSync, writeFileSync, mkdirSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it, afterEach, beforeEach } from "vitest";
import {
  detectDiffModeConflict,
  loadFileFromDisk,
  type DiffModeOptions,
} from "../commands/diff.js";
import { mergeDiffOptions } from "../commands/context.js";

// ══════════════════════════════════════════════════════════════════════
// detectDiffModeConflict — pure function, no side effects
// ══════════════════════════════════════════════════════════════════════

describe("detectDiffModeConflict", () => {
  // ── No-conflict paths ────────────────────────────────────────────

  it("returns undefined when no flags are set (empty object)", () => {
    expect(detectDiffModeConflict({})).toBeUndefined();
  });

  it("returns undefined for --summary alone", () => {
    expect(detectDiffModeConflict({ summary: true })).toBeUndefined();
  });

  it("returns undefined for --content alone", () => {
    expect(detectDiffModeConflict({ content: true })).toBeUndefined();
  });

  it("returns undefined for --full alone", () => {
    expect(detectDiffModeConflict({ full: true })).toBeUndefined();
  });

  it("returns undefined for --limit alone", () => {
    expect(detectDiffModeConflict({ limit: "20" })).toBeUndefined();
  });

  it("returns undefined for --summary + neutral extras", () => {
    // summary + format is fine (format is not a conflict partner)
    expect(detectDiffModeConflict({ summary: true } as DiffModeOptions)).toBeUndefined();
  });

  // ── summary + content conflict (branch 1) ────────────────────────

  it("flags --summary + --content: exact message", () => {
    const msg = detectDiffModeConflict({ summary: true, content: true });
    // MUTATION: changing "and" to "or" must fail
    // MUTATION: changing "Pick one" to anything else must fail
    expect(msg).toBe(
      "--summary and --content cannot be combined; " +
        "--summary renders counts only, --content renders the full textual diff. " +
        "Pick one."
    );
  });

  // ── summary + full/limit conflict (branch 2) ─────────────────────

  it("flags --summary + --limit: exact message", () => {
    const msg = detectDiffModeConflict({ summary: true, limit: "20" });
    // MUTATION: changing "ignores" must fail
    // MUTATION: changing "server-side counts only" must fail
    expect(msg).toBe(
      "--summary ignores --limit and --full; " +
        "--summary is server-side counts only. " +
        "Drop --summary to control change volume, or drop --limit/--full."
    );
  });

  it("flags --summary + --full: same message as summary+limit", () => {
    const msg = detectDiffModeConflict({ summary: true, full: true });
    expect(msg).toBe(
      "--summary ignores --limit and --full; " +
        "--summary is server-side counts only. " +
        "Drop --summary to control change volume, or drop --limit/--full."
    );
  });

  // ── full + limit conflict (branch 3) ─────────────────────────────

  it("flags --full + --limit: exact message", () => {
    const msg = detectDiffModeConflict({ full: true, limit: "20" });
    // MUTATION: changing "documented" must fail
    // MUTATION: changing "Drop --limit, or drop --full" must fail
    expect(msg).toBe(
      "--full and --limit cannot be combined; " +
        "--full is documented to return all changes without a limit. " +
        "Drop --limit, or drop --full to keep the existing limit."
    );
  });

  // ── Three-flag precedence (load-bearing order) ───────────────────

  it("three flags: --summary + --full + --limit → branch 2 wins (not branch 3)", () => {
    // The critical precedence test: --summary is checked BEFORE --full + --limit.
    // If the order is swapped, branch 3 fires instead of branch 2, and the
    // message names the wrong flag as the one in charge.
    const msg = detectDiffModeConflict({ summary: true, full: true, limit: "20" });
    expect(msg).toBe(
      "--summary ignores --limit and --full; " +
        "--summary is server-side counts only. " +
        "Drop --summary to control change volume, or drop --limit/--full."
    );
    // MUTATION: if branch 3 fires instead of branch 2, this would match
    // "--full and --limit cannot be combined" — so we must NOT see that:
    expect(msg).not.toContain("--full and --limit cannot be combined");
  });

  // ── Boundary: undefined vs false vs empty string ─────────────────

  it("does NOT flag --full + limit: undefined (omitted)", () => {
    // limit is omitted → opts.limit is undefined → !== undefined is false
    expect(detectDiffModeConflict({ full: true, limit: undefined })).toBeUndefined();
  });

  it("does NOT flag --full + limit: false (falsy but defined)", () => {
    // This is a TYPE violation in practice (DiffModeOptions.limit is string?),
    // but the runtime check is opts.limit !== undefined, so false !== undefined
    // is true → it WOULD flag. Testing the actual runtime behavior.
    const msg = detectDiffModeConflict({ full: true, limit: false as any });
    expect(msg).toBe("--full and --limit cannot be combined; --full is documented to return all changes without a limit. Drop --limit, or drop --full to keep the existing limit.");
  });

  it("does NOT flag --full + limit: empty string", () => {
    // "" !== undefined → this WOULD be flagged. Testing actual runtime behavior.
    const msg = detectDiffModeConflict({ full: true, limit: "" });
    expect(msg).toBe("--full and --limit cannot be combined; --full is documented to return all changes without a limit. Drop --limit, or drop --full to keep the existing limit.");
  });

  it("does NOT flag --summary + limit: undefined (omitted)", () => {
    // summary + limit(undefined) → limit !== undefined is false → no branch 2
    expect(detectDiffModeConflict({ summary: true, limit: undefined })).toBeUndefined();
  });

  it("does NOT flag --summary + full: false (falsy)", () => {
    // summary + full(false) → full is falsy → branch 2 condition is false
    expect(detectDiffModeConflict({ summary: true, full: false })).toBeUndefined();
  });

  // ── All four flags ───────────────────────────────────────────────

  it("all four flags: --summary wins (branch 2, not branch 3)", () => {
    const msg = detectDiffModeConflict({
      summary: true,
      content: true,
      full: true,
      limit: "20",
    });
    // Branch 1 fires first: summary + content
    expect(msg).toBe(
      "--summary and --content cannot be combined; " +
        "--summary renders counts only, --content renders the full textual diff. " +
        "Pick one."
    );
  });

  // ── Anti-drift: every DiffModeOptions key is classified ──────────

  it("every DiffModeOptions key appears in at least one conflict rule", () => {
    const allKeys: (keyof DiffModeOptions)[] = [
      "summary", "content", "full", "limit",
    ];
    // For each key, verify it participates in at least one conflict.
    // If a new key is added to DiffModeOptions without a rule, this test
    // will catch it by testing that key in combination with every other key.
    for (const key of allKeys) {
      const hasConflict = allKeys.some((other) => {
        if (key === other) return false;
        const opts: DiffModeOptions = {};
        opts[key] = key === "limit" ? "10" : true as any;
        opts[other] = other === "limit" ? "10" : true as any;
        return detectDiffModeConflict(opts) !== undefined;
      });
      // If this fails, the new key doesn't participate in any conflict
      expect(hasConflict).toBe(true);
    }
  });
});

// ══════════════════════════════════════════════════════════════════════
// mergeDiffOptions — pure function, no side effects
// ══════════════════════════════════════════════════════════════════════

describe("mergeDiffOptions", () => {
  // Minimal SavedInvestigation shape — only the fields mergeDiffOptions reads
  const makeSaved = (
    asOfRev?: number,
    depth?: string,
  ) => ({
    bundle: {
      metadata: { asOfRev, depth },
      nodes: [],
      relationships: [],
    },
  }) as any;

  it("inherits both from saved when opts is empty", () => {
    const result = mergeDiffOptions(makeSaved(42, "shallow"), {});
    expect(result).toEqual({ asOfRev: 42, depth: "shallow" });
  });

  it("explicit asOfRev overrides saved", () => {
    const result = mergeDiffOptions(makeSaved(42, "shallow"), { asOfRev: 10 });
    expect(result).toEqual({ asOfRev: 10, depth: "shallow" });
  });

  it("explicit depth overrides saved", () => {
    const result = mergeDiffOptions(makeSaved(42, "shallow"), { depth: "deep" });
    expect(result).toEqual({ asOfRev: 42, depth: "deep" });
  });

  it("both explicit overrides win over saved", () => {
    const result = mergeDiffOptions(makeSaved(42, "shallow"), {
      asOfRev: 10,
      depth: "deep",
    });
    expect(result).toEqual({ asOfRev: 10, depth: "deep" });
  });

  it("undefined saved values fall through to undefined", () => {
    const result = mergeDiffOptions(makeSaved(undefined, undefined), {});
    expect(result).toEqual({ asOfRev: undefined, depth: undefined });
  });

  it("zero asOfRev is treated as explicit (not inherited)", () => {
    // 0 is falsy but !== undefined — the ?? operator passes it through.
    // This tests that the implementation uses ?? not ||.
    const result = mergeDiffOptions(makeSaved(99), { asOfRev: 0 });
    expect(result).toEqual({ asOfRev: 0, depth: undefined });
  });

  it("empty string depth is treated as explicit (not inherited)", () => {
    // "" is falsy but !== undefined — ?? passes it through.
    const result = mergeDiffOptions(makeSaved(42, "deep"), { depth: "" });
    expect(result).toEqual({ asOfRev: 42, depth: "" });
  });

  it("saved values are used when opts has only one override", () => {
    // Only asOfRev is overridden; depth comes from saved
    const result = mergeDiffOptions(makeSaved(7, "compact"), { asOfRev: 3 });
    expect(result).toEqual({ asOfRev: 3, depth: "compact" });
  });
});

// ══════════════════════════════════════════════════════════════════════
// loadFileFromDisk — filesystem edge cases
// ══════════════════════════════════════════════════════════════════════

describe("loadFileFromDisk", () => {
  // tmpDir is passed as the root: reads are confined to readable roots, and a
  // temp dir is in no workspace.
  let tmpDir: string;

  beforeEach(() => {
    tmpDir = mkdtempSync(join(tmpdir(), "ix-diff-loadfile-"));
  });

  afterEach(() => {
    rmSync(tmpDir, { recursive: true, force: true });
  });

  it("returns file content when file exists", () => {
    const filePath = join(tmpDir, "exists.txt");
    writeFileSync(filePath, "hello world", "utf-8");
    expect(loadFileFromDisk(filePath, tmpDir)).toBe("hello world");
  });

  it("returns null when file does not exist", () => {
    expect(loadFileFromDisk(join(tmpDir, "nope.txt"), tmpDir)).toBeNull();
  });

  it("returns empty string for an empty file (not null)", () => {
    const filePath = join(tmpDir, "empty.txt");
    writeFileSync(filePath, "", "utf-8");
    const result = loadFileFromDisk(filePath, tmpDir);
    // Empty string is falsy but NOT null — this is a meaningful distinction.
    // MUTATION: changing the return to undefined or null must fail.
    expect(result).toBe("");
    expect(result).not.toBeNull();
  });

  it("returns null when path is a directory (not a file)", () => {
    // readFileSync on a directory throws; catch block returns null.
    mkdirSync(join(tmpDir, "subdir"));
    expect(loadFileFromDisk(join(tmpDir, "subdir"), tmpDir)).toBeNull();
  });

  it("returns content with multi-line content", () => {
    const filePath = join(tmpDir, "multi.txt");
    writeFileSync(filePath, "line1\nline2\nline3", "utf-8");
    expect(loadFileFromDisk(filePath, tmpDir)).toBe("line1\nline2\nline3");
  });

  it("returns content with special characters", () => {
    const filePath = join(tmpDir, "special.txt");
    writeFileSync(filePath, "hello\tworld\n\"quotes\" and 'apostrophes'", "utf-8");
    expect(loadFileFromDisk(filePath, tmpDir)).toBe("hello\tworld\n\"quotes\" and 'apostrophes'");
  });

  it("returns content with Unicode (emoji + CJK)", () => {
    const filePath = join(tmpDir, "unicode.txt");
    writeFileSync(filePath, "hello 😀 你好世界", "utf-8");
    expect(loadFileFromDisk(filePath, tmpDir)).toBe("hello 😀 你好世界");
  });

  it("returns null for path with null bytes (catch block)", () => {
    // A path containing null bytes will cause readFileSync to throw.
    expect(loadFileFromDisk(join(tmpDir, "file\0.txt") as any, tmpDir)).toBeNull();
  });

  it("returns null for non-existent parent directory", () => {
    expect(loadFileFromDisk(join(tmpDir, "no", "such", "dir", "file.txt"), tmpDir)).toBeNull();
  });
});