import { describe, expect, it } from "vitest"; import { editPreview, foldConversation, foldEdits, likeness, parseEdit, } from "@shared/edits"; const MIN = 60_000; let nextId = 1; const msg = ( body: string, min: number, opts: { out?: boolean; sender?: string } = {}, ) => ({ id: nextId++, body, ts: 1_700_000_000_000 + min * MIN, direction: (opts.out ? "out" : "in") as "in" | "out", contact: "5550001111", ...(opts.sender ? { sender: opts.sender } : {}), }); describe("parseEdit", () => { it("reads the new text", () => { expect(parseEdit("Edited to “see you at 9:30”")).toBe("see you at 9:30"); expect(parseEdit('Edited to "ok"')).toBe("ok"); expect(parseEdit("Editado a “nos vemos”")).toBe("nos vemos"); expect(parseEdit("Edited the plan")).toBeNull(); expect(editPreview("Edited to “see you”")).toBe("✏️ see you"); }); it("likeness", () => { expect(likeness("abc", "abc")).toBe(1); expect(likeness("see you at 9", "see you at 9:30")).toBeGreaterThan(0.7); }); }); describe("foldEdits", () => { it("applies a small edit and keeps the history", () => { const a = msg("see you at 9", 0); const b = msg("sounds good", 1, { out: true }); const e = msg("Edited to “see you at 9:30”", 3); const { shown, versions } = foldEdits([a, b, e]); expect(shown.map((m) => m.body)).toEqual([ "see you at 9:30", "sounds good", ]); expect(versions.get(a.id)?.map((v) => v.body)).toEqual([ "see you at 9", "see you at 9:30", ]); }); it("chains edits and picks the most alike of several", () => { const a = msg("grab milk", 0); const b = msg("and the dog food from the shelf", 1); const e1 = msg("Edited to “and the cat food from the shelf”", 2); const e2 = msg("Edited to “and the cat food from the top shelf”", 4); const { shown, versions } = foldEdits([a, b, e1, e2]); expect(shown.map((m) => m.body)).toEqual([ "grab milk", "and the cat food from the top shelf", ]); expect(versions.get(b.id)?.length).toBe(3); }); it("takes a big change when theirs is the only message in the window", () => { const a = msg("yes", 0); const e = msg("Edited to “actually no, sorry”", 2); expect(foldEdits([a, e]).shown.map((m) => m.body)).toEqual([ "actually no, sorry", ]); }); it("leaves it when it can't tell", () => { const a = msg("pizza tonight?", 0); const b = msg("or tacos", 1); const e = msg("Edited to “never mind, I am busy”", 2); expect(foldEdits([a, b, e]).shown).toHaveLength(3); // someone else's message is never the one edited const c = msg("hello there", 0, { out: true }); const f = msg("Edited to “hello there!”", 1); expect(foldEdits([c, f]).shown).toHaveLength(2); }); it("reactions to the old text still land", () => { const a = msg("see you at 9", 0); const r = msg("Loved “see you at 9”", 1, { out: true }); const e = msg("Edited to “see you at 9:30”", 2); const { shown, reactions } = foldConversation([a, r, e]); expect(shown.map((m) => m.body)).toEqual(["see you at 9:30"]); expect(reactions.get(a.id)?.[0].emoji).toBe("❤️"); }); });