1
13import { describe, it, expect } from "vitest";
14import { blockPlainText } from "@/lib/ai";
15import { wordDiff, reconstructOld, reconstructNew } from "@factory/editor";
16
17describe("blockPlainText (the diff's OLD side)", () => {
18 it("reads {text} (the canonical content model)", () => {
19 expect(blockPlainText({ text: "Hello world" })).toBe("Hello world");
20 });
21 it("passes a raw string through (legacy/inline path)", () => {
22 expect(blockPlainText("just a string")).toBe("just a string");
23 });
24 it("falls back to {title}", () => {
25 expect(blockPlainText({ title: "Page title" })).toBe("Page title");
26 });
27 it("prefers {text} over {title}", () => {
28 expect(blockPlainText({ text: "body", title: "head" })).toBe("body");
29 });
30 it("returns '' for null/undefined/non-object", () => {
31 expect(blockPlainText(null)).toBe("");
32 expect(blockPlainText(undefined)).toBe("");
33 expect(blockPlainText(42)).toBe("");
34 expect(blockPlainText({ text: 42 })).toBe("");
35 });
36});
37
38describe("wordDiff (the diff renderer's input)", () => {
39 it("marks unchanged text as `same`", () => {
40 expect(wordDiff("hello world", "hello world")).toEqual([{ type: "same", text: "hello world" }]);
41 });
42 it("produces add/del segments that reconstruct BOTH sides", () => {
43 const old = "the quick brown fox";
44 const neu = "the slow brown fox";
45 const segs = wordDiff(old, neu);
46
47 expect(segs.some((s) => s.type === "del")).toBe(true);
48 expect(segs.some((s) => s.type === "add")).toBe(true);
49
50
51 expect(reconstructOld(segs)).toBe(old);
52 expect(reconstructNew(segs)).toBe(neu);
53 });
54 it("treats a full replacement as all-del + all-add", () => {
55 const segs = wordDiff("alpha beta", "gamma delta");
56 expect(reconstructOld(segs)).toBe("alpha beta");
57 expect(reconstructNew(segs)).toBe("gamma delta");
58 expect(segs.some((s) => s.type === "del")).toBe(true);
59 expect(segs.some((s) => s.type === "add")).toBe(true);
60 });
61 it("returns [] for an empty old text", () => {
62 expect(wordDiff("", "new")).toEqual([{ type: "add", text: "new" }]);
63 });
64});
65