|
| 1 | +/** |
| 2 | + * Tests for the inline-reference resolver (v2.5.7 fix). |
| 3 | + * |
| 4 | + * Tana stores field values that contain references or dates as empty |
| 5 | + * `<span>` tags with data attributes, e.g.: |
| 6 | + * |
| 7 | + * <span data-inlineref-node="abc123"></span> |
| 8 | + * <span data-inlineref-date="{"dateTimeString":"2026-01-26",...}"></span> |
| 9 | + * |
| 10 | + * Prior to this fix, FieldResolver ran stripHtml() on these and got back |
| 11 | + * empty strings — causing `tana_query` to return "Context": "" for reference |
| 12 | + * fields (David Delgado Vendrell's bug report against v2.5.5). |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, it, expect, beforeEach, afterEach } from "bun:test"; |
| 16 | +import { Database } from "bun:sqlite"; |
| 17 | +import { mkdtempSync, rmSync } from "fs"; |
| 18 | +import { tmpdir } from "os"; |
| 19 | +import { join } from "path"; |
| 20 | +import { |
| 21 | + resolveInlineRefs, |
| 22 | + resolveInlineRefsBatch, |
| 23 | +} from "../../src/utils/resolve-inline-refs"; |
| 24 | + |
| 25 | +function makeDb(): { db: Database; dir: string } { |
| 26 | + const dir = mkdtempSync(join(tmpdir(), "resolve-refs-")); |
| 27 | + const db = new Database(join(dir, "t.db")); |
| 28 | + db.run(`CREATE TABLE nodes (id TEXT PRIMARY KEY, name TEXT)`); |
| 29 | + return { db, dir }; |
| 30 | +} |
| 31 | + |
| 32 | +describe("resolveInlineRefs", () => { |
| 33 | + let db: Database; |
| 34 | + let dir: string; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + ({ db, dir } = makeDb()); |
| 38 | + db.run("INSERT INTO nodes (id, name) VALUES (?, ?)", ["abc123", "Pr MB2"]); |
| 39 | + db.run("INSERT INTO nodes (id, name) VALUES (?, ?)", ["xyz789", "Another Project"]); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + db.close(); |
| 44 | + rmSync(dir, { recursive: true, force: true }); |
| 45 | + }); |
| 46 | + |
| 47 | + it("resolves a pure inline node reference to the target name (regression test)", () => { |
| 48 | + const input = '<span data-inlineref-node="abc123"></span>'; |
| 49 | + expect(resolveInlineRefs(input, db)).toBe("Pr MB2"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("resolves an inline reference embedded in surrounding text", () => { |
| 53 | + const input = 'Meeting with <span data-inlineref-node="abc123"></span> today'; |
| 54 | + expect(resolveInlineRefs(input, db)).toBe("Meeting with Pr MB2 today"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("resolves multiple references in one value", () => { |
| 58 | + const input = |
| 59 | + '<span data-inlineref-node="abc123"></span>, <span data-inlineref-node="xyz789"></span>'; |
| 60 | + expect(resolveInlineRefs(input, db)).toBe("Pr MB2, Another Project"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("returns empty string when the referenced node is missing (deleted/orphaned)", () => { |
| 64 | + const input = '<span data-inlineref-node="nonexistent"></span>'; |
| 65 | + expect(resolveInlineRefs(input, db)).toBe(""); |
| 66 | + }); |
| 67 | + |
| 68 | + it("resolves inline-ref-date to its dateTimeString", () => { |
| 69 | + const input = |
| 70 | + '<span data-inlineref-date="{"dateTimeString":"2026-01-26","timezone":"UTC"}"></span>'; |
| 71 | + expect(resolveInlineRefs(input, db)).toBe("2026-01-26"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("handles plain text without any spans (no-op)", () => { |
| 75 | + expect(resolveInlineRefs("Pr MB2", db)).toBe("Pr MB2"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("strips residual HTML after reference substitution (option color spans)", () => { |
| 79 | + const input = '<span data-color="blue">DONE</span>'; |
| 80 | + expect(resolveInlineRefs(input, db)).toBe("DONE"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("trims whitespace from the final result", () => { |
| 84 | + const input = ' <span data-inlineref-node="abc123"></span> '; |
| 85 | + expect(resolveInlineRefs(input, db)).toBe("Pr MB2"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("returns empty string for completely empty input", () => { |
| 89 | + expect(resolveInlineRefs("", db)).toBe(""); |
| 90 | + }); |
| 91 | + |
| 92 | + it("handles malformed date JSON gracefully", () => { |
| 93 | + const input = '<span data-inlineref-date="not-valid-json"></span>'; |
| 94 | + expect(resolveInlineRefs(input, db)).toBe(""); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe("resolveInlineRefsBatch", () => { |
| 99 | + let db: Database; |
| 100 | + let dir: string; |
| 101 | + |
| 102 | + beforeEach(() => { |
| 103 | + ({ db, dir } = makeDb()); |
| 104 | + db.run("INSERT INTO nodes (id, name) VALUES (?, ?)", ["a", "Alpha"]); |
| 105 | + db.run("INSERT INTO nodes (id, name) VALUES (?, ?)", ["b", "Beta"]); |
| 106 | + db.run("INSERT INTO nodes (id, name) VALUES (?, ?)", ["c", "Gamma"]); |
| 107 | + }); |
| 108 | + |
| 109 | + afterEach(() => { |
| 110 | + db.close(); |
| 111 | + rmSync(dir, { recursive: true, force: true }); |
| 112 | + }); |
| 113 | + |
| 114 | + it("resolves references across many values in one pass", () => { |
| 115 | + const input = [ |
| 116 | + '<span data-inlineref-node="a"></span>', |
| 117 | + '<span data-inlineref-node="b"></span>', |
| 118 | + '<span data-inlineref-node="c"></span>', |
| 119 | + "plain text", |
| 120 | + ]; |
| 121 | + expect(resolveInlineRefsBatch(input, db)).toEqual(["Alpha", "Beta", "Gamma", "plain text"]); |
| 122 | + }); |
| 123 | + |
| 124 | + it("performs a single node lookup per unique id (batch efficiency)", () => { |
| 125 | + // Repeat the same ref many times; the lookup should deduplicate. |
| 126 | + const input = Array.from({ length: 100 }, () => '<span data-inlineref-node="a"></span>'); |
| 127 | + const results = resolveInlineRefsBatch(input, db); |
| 128 | + expect(results.every((r) => r === "Alpha")).toBe(true); |
| 129 | + }); |
| 130 | +}); |
0 commit comments