-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdiscoverSharedNodes.test.ts
More file actions
117 lines (108 loc) · 3.74 KB
/
Copy pathdiscoverSharedNodes.test.ts
File metadata and controls
117 lines (108 loc) · 3.74 KB
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
import { describe, expect, it } from "vitest";
import {
ridToSpaceUriAndLocalId,
spaceUriAndLocalIdToRid,
} from "@repo/database/lib/rid";
import { assembleDiscoveredNodes } from "~/utils/discoverSharedNodes";
const ROAM_URL = "https://roamresearch.com/#/app/MAPLab";
const OBSIDIAN_URL = "obsidian:9a8b7c6d5e4f3210";
const spaceMeta: Map<
number,
{ url: string; name: string | null; platform: string | null }
> = new Map([
[1, { url: ROAM_URL, name: "MAP Lab", platform: "Roam" }],
[2, { url: OBSIDIAN_URL, name: "Field Vault", platform: "Obsidian" }],
]);
const row = (
space_id: number,
source_local_id: string,
variant: string,
text: string | null,
last_modified: string | null,
) => ({ space_id, source_local_id, variant, text, last_modified });
describe("assembleDiscoveredNodes", () => {
it("merges direct + full into one node, preferring direct for the title", () => {
const result = assembleDiscoveredNodes({
contentRows: [
row(1, "abc", "direct", "Sleep improves memory", "2026-06-12T10:00:00"),
row(
1,
"abc",
"full",
"# Sleep improves memory\n\nbody",
"2026-06-12T12:00:00",
),
],
spaceMetaById: spaceMeta,
importedRids: new Set(),
});
expect(result).toHaveLength(1);
const node = result[0]!;
expect(node.title).toBe("Sleep improves memory");
expect(node.sourceApp).toBe("roam");
expect(node.sourceSpaceId).toBe(ROAM_URL);
expect(node.sourceSpaceName).toBe("MAP Lab");
expect(node.sourceNodeId).toBe("abc");
expect(node.alreadyImported).toBe(false);
expect(node.sourceModifiedAt).toBe(
new Date("2026-06-12T12:00:00Z").toISOString(),
);
expect(node.sourceNodeRid).toBe(spaceUriAndLocalIdToRid(ROAM_URL, "abc"));
expect(ridToSpaceUriAndLocalId(node.sourceNodeRid)).toEqual({
spaceUri: ROAM_URL,
sourceLocalId: "abc",
});
});
it("skips title-only nodes that have no full variant (not actually shared)", () => {
const result = assembleDiscoveredNodes({
contentRows: [
row(1, "title-only", "direct", "Just a title", "2026-06-12T10:00:00"),
],
spaceMetaById: spaceMeta,
importedRids: new Set(),
});
expect(result).toHaveLength(0);
});
it("skips nodes whose source space metadata is missing", () => {
const result = assembleDiscoveredNodes({
contentRows: [
row(99, "x", "direct", "Title", "2026-06-12T10:00:00"),
row(99, "x", "full", "body", "2026-06-12T10:00:00"),
],
spaceMetaById: spaceMeta,
importedRids: new Set(),
});
expect(result).toHaveLength(0);
});
it("marks already-imported nodes by source RID", () => {
const contentRows = [
row(2, "node-1", "direct", "Field note", "2026-06-12T10:00:00"),
row(2, "node-1", "full", "body", "2026-06-12T10:00:00"),
];
const rid = spaceUriAndLocalIdToRid(OBSIDIAN_URL, "node-1");
const result = assembleDiscoveredNodes({
contentRows,
spaceMetaById: spaceMeta,
importedRids: new Set([rid]),
});
expect(result).toHaveLength(1);
expect(result[0]!.sourceApp).toBe("obsidian");
expect(result[0]!.alreadyImported).toBe(true);
});
it("sorts nodes by source space name", () => {
const result = assembleDiscoveredNodes({
contentRows: [
row(2, "o1", "direct", "Obsidian node", "2026-06-12T10:00:00"),
row(2, "o1", "full", "body", "2026-06-12T10:00:00"),
row(1, "r1", "direct", "Roam node", "2026-06-12T10:00:00"),
row(1, "r1", "full", "body", "2026-06-12T10:00:00"),
],
spaceMetaById: spaceMeta,
importedRids: new Set(),
});
expect(result.map((n) => n.sourceSpaceName)).toEqual([
"Field Vault",
"MAP Lab",
]);
});
});