import { describe, expect, it } from "vitest"; import { locateDriftSegment } from "./locateSegment"; const DRAFT = "第一段没问题。\n\n他乘坐迈巴赫扬长而去,留下一地尘土。\n\n结尾。"; const PARAS = DRAFT.split(/\n{2,}/); describe("locateDriftSegment", () => { it("locates by content anchor, ignoring a mismatched idx", () => { // idx 指向第 0 段,但 text 是第 2 段——内容锚优先,定位到真实原文。 const located = locateDriftSegment(PARAS, { idx: 0, text: "他乘坐迈巴赫扬长而去,留下一地尘土。", }); expect(located).toBe("他乘坐迈巴赫扬长而去,留下一地尘土。"); }); it("trims the anchor before matching", () => { const located = locateDriftSegment(PARAS, { idx: 99, text: " 他乘坐迈巴赫扬长而去,留下一地尘土。 ", }); expect(located).toBe("他乘坐迈巴赫扬长而去,留下一地尘土。"); }); it("returns null when the anchor text is not found (#9 guard — no silent no-op)", () => { // 自带原文但终稿里已被改动——不回退到可能错位的 idx,判定为定位失败。 expect( locateDriftSegment(PARAS, { idx: 1, text: "他乘坐保时捷离开了。" }), ).toBeNull(); }); it("falls back to positional idx when no text anchor (legacy data)", () => { expect(locateDriftSegment(PARAS, { idx: 1, text: "" })).toBe( "他乘坐迈巴赫扬长而去,留下一地尘土。", ); }); it("returns null when no anchor and idx is out of range (#9 guard)", () => { expect(locateDriftSegment(PARAS, { idx: 9, text: "" })).toBeNull(); }); it("returns null when no anchor and the positional paragraph is blank", () => { expect(locateDriftSegment(["", " "], { idx: 0, text: "" })).toBeNull(); }); });