import { expect, it } from "bun:test"; import { diff } from "../diff"; import { applyExtendedPatch, applyPatchToString } from "../extended"; it("adds a string", () => { expect( applyPatchToString("hello", { op: "add", path: [5], value: " world" }) ).toEqual("hello world"); }); it("removes a string", () => { expect( applyPatchToString("hello world", { op: "remove", path: [5] }) ).toEqual("helloworld"); }); it("removes a string with a length", () => { expect( applyPatchToString("hello world", { op: "remove", path: [5], length: 6 }) ).toEqual("hello"); }); it("replaces a string", () => { expect( applyPatchToString("hello world", { op: "replace", path: [6], length: 5, value: "foo", }) ).toEqual("hello foo"); }); it("applies patch to nested string", () => { const original = { foo: "hello" }; const updated = { foo: "hello world" }; expect( applyExtendedPatch(original, [ { op: "add", path: ["foo", 5], value: " world" }, ]) ).toEqual(updated); }); it("diffs strings", () => { const original = "hello world"; const updated = "hello foo"; const [patches, inversePatches] = diff(original, updated); expect(patches).toEqual([ { op: "replace", path: [6], length: 1, value: "f" }, { op: "replace", path: [8], length: 3, value: "o" }, ]); const result = applyExtendedPatch(original, patches); expect(result).toEqual(updated); const inverseResult = applyExtendedPatch(updated, inversePatches); expect(inverseResult).toEqual(original); });