Spaces:
Sleeping
Sleeping
File size: 6,058 Bytes
097eaf0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | import { diffMonitorJson } from "./diff";
describe("diffMonitorJson", () => {
it("returns `same` when every value matches", () => {
expect(
diffMonitorJson(
{ name: "Pro", price: "$19", tags: ["a", "b"] },
{ name: "Pro", price: "$19", tags: ["a", "b"] },
),
).toEqual({ kind: "json", status: "same" });
});
it("returns `same` regardless of object key order", () => {
expect(
diffMonitorJson({ a: 1, b: 2 }, { b: 2, a: 1 }),
).toEqual({ kind: "json", status: "same" });
});
it("reports a single change for a top-level primitive that differs", () => {
const result = diffMonitorJson({ price: "$19" }, { price: "$24" });
expect(result).toEqual({
kind: "json",
status: "changed",
json: { price: { previous: "$19", current: "$24" } },
});
});
it("recurses into nested objects so unchanged sibling fields stay quiet", () => {
// Regression: previously the whole `details` object was reported as
// changed when only `description` differed, which rendered every
// unchanged sibling field with strikethrough in the UI.
const previous = {
location: "Oslo, Norway",
details: {
direction: "forward",
description: "Clocks go forward 1 hour",
next_change_date: "",
},
};
const current = {
location: "United States",
details: {
direction: "forward",
description: "",
next_change_date: "",
},
};
const result = diffMonitorJson(previous, current);
expect(result).toEqual({
kind: "json",
status: "changed",
json: {
location: { previous: "Oslo, Norway", current: "United States" },
"details.description": {
previous: "Clocks go forward 1 hour",
current: "",
},
},
});
});
it("emits per-index paths when an array element changes", () => {
const previous = {
plans: [
{ name: "Starter", price: "$19" },
{ name: "Pro", price: "$49" },
],
};
const current = {
plans: [
{ name: "Starter", price: "$24" },
{ name: "Pro", price: "$49" },
],
};
expect(diffMonitorJson(previous, current)).toEqual({
kind: "json",
status: "changed",
json: {
"plans[0].price": { previous: "$19", current: "$24" },
},
});
});
it("reports added and removed array elements at their index", () => {
const previous = { tags: ["a", "b"] };
const current = { tags: ["a", "b", "c"] };
expect(diffMonitorJson(previous, current)).toEqual({
kind: "json",
status: "changed",
json: {
"tags[2]": { previous: undefined, current: "c" },
},
});
});
it("records the whole subtree when the value type diverges", () => {
// object → primitive should NOT recurse: there's nothing to walk in
// parallel, and the user wants to see "this whole thing became a
// string" as one entry.
const previous = { details: { foo: 1, bar: 2 } };
const current = { details: "n/a" };
expect(diffMonitorJson(previous, current)).toEqual({
kind: "json",
status: "changed",
json: {
details: { previous: { foo: 1, bar: 2 }, current: "n/a" },
},
});
});
it("treats fields that became null as a single leaf change", () => {
const previous = { contact: { email: "x@y.com" } };
const current = { contact: null };
expect(diffMonitorJson(previous, current)).toEqual({
kind: "json",
status: "changed",
json: {
contact: { previous: { email: "x@y.com" }, current: null },
},
});
});
it("ignores NFC-equivalent string differences", () => {
// "é" can be encoded as a single codepoint (U+00E9) or as e + combining
// acute accent (U+0065 U+0301). The diff should treat them as equal.
const composed = "caf\u00e9";
const decomposed = "cafe\u0301";
expect(diffMonitorJson({ name: composed }, { name: decomposed })).toEqual({
kind: "json",
status: "same",
});
});
it("ignores whitespace-only differences in string values", () => {
// Regression: LLM extractions of the same content often differ only
// in incidental whitespace (extra spaces, trailing newlines, NBSP
// from HTML rendering, etc.). Those shouldn't be reported as field
// changes — they drown out real signal.
const previous = {
headline: "Power AI agents with clean web data",
cta: "Start for free\n",
// Non-breaking space (U+00A0) instead of a regular space — common
// when scraping rendered HTML.
subhead: "The API\u00a0to scrape the web",
// BOM at the start of the string.
tagline: "\ufeffNo more brittle scrapers",
};
const current = {
headline: "Power AI agents with clean web data",
cta: "Start for free",
subhead: "The API to scrape the web",
tagline: "No more brittle scrapers",
};
expect(diffMonitorJson(previous, current)).toEqual({
kind: "json",
status: "same",
});
});
it("still detects real content changes after whitespace normalization", () => {
// Sanity check: the whitespace normalization shouldn't make every
// string look equal.
expect(
diffMonitorJson(
{ headline: "Power AI agents with clean web data" },
{ headline: "Power AI agents with structured web data" },
),
).toEqual({
kind: "json",
status: "changed",
json: {
headline: {
previous: "Power AI agents with clean web data",
current: "Power AI agents with structured web data",
},
},
});
});
it("handles undefined inputs as empty objects", () => {
expect(diffMonitorJson(undefined, undefined)).toEqual({
kind: "json",
status: "same",
});
expect(diffMonitorJson(undefined, { a: 1 })).toEqual({
kind: "json",
status: "changed",
json: { a: { previous: undefined, current: 1 } },
});
});
});
|