import { describe, expect, it } from "vitest";

import { applyConflictFix, hasApplicableFix } from "./applyFix";

describe("hasApplicableFix", () => {
  it("requires non-empty original and string replacement", () => {
    expect(hasApplicableFix("迈巴赫", "奔驰")).toBe(true);
    expect(hasApplicableFix("删我", "")).toBe(true); // 空串=删除，仍可应用
    expect(hasApplicableFix(null, "奔驰")).toBe(false);
    expect(hasApplicableFix("", "奔驰")).toBe(false);
    expect(hasApplicableFix("迈巴赫", null)).toBe(false);
    expect(hasApplicableFix(undefined, undefined)).toBe(false);
  });
});

describe("applyConflictFix", () => {
  it("replaces the first occurrence of original with replacement", () => {
    const out = applyConflictFix(
      "他乘坐迈巴赫扬长而去，迈巴赫的尾灯。",
      "迈巴赫",
      "奔驰",
    );
    expect(out.status).toBe("applied");
    expect(out.text).toBe("他乘坐奔驰扬长而去，迈巴赫的尾灯。");
  });

  it("returns not-found and leaves text untouched when original is absent (drift)", () => {
    const out = applyConflictFix("作者已经手改过的终稿。", "迈巴赫", "奔驰");
    expect(out.status).toBe("not-found");
    expect(out.text).toBe("作者已经手改过的终稿。");
  });

  it("returns no-patch when conflict carries no applicable patch", () => {
    const out = applyConflictFix("正文", null, null);
    expect(out.status).toBe("no-patch");
    expect(out.text).toBe("正文");
  });

  it("does not mutate the input string", () => {
    const original = "迈巴赫尾灯";
    applyConflictFix(original, "迈巴赫", "奔驰");
    expect(original).toBe("迈巴赫尾灯");
  });
});
