Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
Nick: fix diff
Nicolascabb48ea809e
diff --git a/apps/api/src/services/monitoring/diff.test.ts b/apps/api/src/services/monitoring/diff.test.tsnew file mode 100644index 000000000..a39fdbbc7--- /dev/null+++ b/apps/api/src/services/monitoring/diff.test.ts@@ -0,0 +1,151 @@+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: "[email protected]" } };+ const current = { contact: null };++ expect(diffMonitorJson(previous, current)).toEqual({+ kind: "json",+ status: "changed",+ json: {+ contact: { previous: { email: "[email protected]" }, 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("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 } },+ });+ });+});diff --git a/apps/api/src/services/monitoring/diff.ts b/apps/api/src/services/monitoring/diff.tsindex b4a719eab..30b9aa703 100644--- a/apps/api/src/services/monitoring/diff.ts+++ b/apps/api/src/services/monitoring/diff.ts@@ -158,11 +158,64 @@ function jsonValuesEqual(a: unknown, b: unknown): boolean {return false;}+function isPlainObject(value: unknown): value is Record<string, unknown> {+ return typeof value === "object" && value !== null && !Array.isArray(value);+}++function joinKey(parent: string, key: string): string {+ return parent ? `${parent}.${key}` : key;+}++function joinIndex(parent: string, index: number): string {+ return `${parent}[${index}]`;+}++/**+ * Walk two JSON subtrees in parallel and append a `{previous, current}`+ * entry to `out` for every leaf-level difference. Keys in `out` are+ * dotted/bracketed JSON paths (e.g. `plans[0].price`, `metadata.title`).+ *+ * When both values at a node are objects (or both arrays) we recurse so+ * unchanged sibling fields aren't reported as part of the diff. When the+ * types diverge (object vs primitive, array vs null, …) we record the+ * whole subtree as a single change at the current path — splitting it+ * further would be lossy/misleading.+ */+function collectJsonFieldDiffs(+ prev: unknown,+ curr: unknown,+ path: string,+ out: Record<string, { previous: unknown; current: unknown }>,+): void {+ if (jsonValuesEqual(prev, curr)) return;++ if (isPlainObject(prev) && isPlainObject(curr)) {+ const keys = new Set([...Object.keys(prev), ...Object.keys(curr)]);+ for (const key of keys) {+ collectJsonFieldDiffs(prev[key], curr[key], joinKey(path, key), out);+ }+ return;+ }++ if (Array.isArray(prev) && Array.isArray(curr)) {+ const len = Math.max(prev.length, curr.length);+ for (let i = 0; i < len; i++) {+ collectJsonFieldDiffs(prev[i], curr[i], joinIndex(path, i), out);+ }+ return;+ }++ out[path] = { previous: prev, current: curr };+}+/*** Diff two JSON snapshots (current vs previous scrape `doc.json`) for a* monitor. Returns `same` if every field is unchanged after NFC- * normalization, otherwise `changed` with a per-field {previous, current}- * map containing only the fields that differ.+ * normalization, otherwise `changed` with a per-path `{previous, current}`+ * map containing only the leaf-level fields that differ. Paths use+ * dot/bracket notation, e.g. `plans[0].price` or `metadata.title`, so a+ * single nested mutation doesn't render the entire parent object as+ * changed in the UI.*/export function diffMonitorJson(previous: Record<string, unknown> | undefined,@@ -174,9 +227,7 @@ export function diffMonitorJson(const diff: Record<string, { previous: unknown; current: unknown }> = {};for (const key of keys) {- if (!jsonValuesEqual(prev[key], curr[key])) {- diff[key] = { previous: prev[key], current: curr[key] };- }+ collectJsonFieldDiffs(prev[key], curr[key], key, diff);}if (Object.keys(diff).length === 0) {