-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathimportedSourceIdentity.test.ts
More file actions
140 lines (123 loc) · 4.05 KB
/
Copy pathimportedSourceIdentity.test.ts
File metadata and controls
140 lines (123 loc) · 4.05 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { beforeEach, describe, expect, it, vi } from "vitest";
import { DISCOURSE_GRAPH_PROP_NAME } from "~/utils/createReifiedBlock";
import {
findImportedNodeUidBySourceRid,
getImportedSourceRids,
IMPORTED_FROM_PROP_KEY,
readImportedSourceIdentity,
writeImportedSourceIdentity,
} from "~/utils/importedSourceIdentity";
import type { json } from "~/utils/getBlockProps";
const SOURCE_NODE_RID = "orn:obsidian.note:vault-a/node-1";
const SOURCE_MODIFIED_AT = "2026-06-14T15:00:00.000Z";
const PAGE_UID = "page-uid";
const propsByUid = new Map<string, Record<string, json>>();
const query = vi.fn();
const setRoamAlphaApi = (): void => {
(globalThis as { window: unknown }).window = {
roamAlphaAPI: {
data: {
async: { q: query },
block: {
update: vi.fn(
({
block,
}: {
block: { props: Record<string, json>; uid: string };
}) => {
propsByUid.set(block.uid, block.props);
return Promise.resolve();
},
),
},
},
pull: (_pattern: string, [, uid]: [string, string]) => ({
":block/props": propsByUid.get(uid) ?? {},
}),
},
};
};
beforeEach(() => {
propsByUid.clear();
query.mockReset();
setRoamAlphaApi();
});
describe("imported source identity metadata", () => {
it("reads the source RID without depending on display metadata", () => {
propsByUid.set(PAGE_UID, {
[DISCOURSE_GRAPH_PROP_NAME]: {
[IMPORTED_FROM_PROP_KEY]: {
sourceModifiedAt: SOURCE_MODIFIED_AT,
sourceNodeRid: SOURCE_NODE_RID,
sourceTitle: "Legacy title that may change",
},
},
});
expect(readImportedSourceIdentity(PAGE_UID)).toEqual({
sourceModifiedAt: SOURCE_MODIFIED_AT,
sourceNodeRid: SOURCE_NODE_RID,
});
});
it("returns undefined for missing or malformed source identity", () => {
expect(readImportedSourceIdentity(PAGE_UID)).toBeUndefined();
propsByUid.set(PAGE_UID, {
[DISCOURSE_GRAPH_PROP_NAME]: {
[IMPORTED_FROM_PROP_KEY]: { sourceNodeRid: 123 },
},
});
expect(readImportedSourceIdentity(PAGE_UID)).toBeUndefined();
});
it("writes the source RID and modified time while preserving sibling metadata", async () => {
propsByUid.set(PAGE_UID, {
[DISCOURSE_GRAPH_PROP_NAME]: {
"relation-migration": { relationUid: 1718000000000 },
},
"other-extension": { enabled: true },
});
await writeImportedSourceIdentity({
pageUid: PAGE_UID,
sourceModifiedAt: SOURCE_MODIFIED_AT,
sourceNodeRid: SOURCE_NODE_RID,
});
expect(readImportedSourceIdentity(PAGE_UID)).toEqual({
sourceModifiedAt: SOURCE_MODIFIED_AT,
sourceNodeRid: SOURCE_NODE_RID,
});
expect(propsByUid.get(PAGE_UID)).toEqual({
[DISCOURSE_GRAPH_PROP_NAME]: {
"relation-migration": { relationUid: 1718000000000 },
[IMPORTED_FROM_PROP_KEY]: {
sourceModifiedAt: SOURCE_MODIFIED_AT,
sourceNodeRid: SOURCE_NODE_RID,
},
},
"other-extension": { enabled: true },
});
});
});
describe("imported source identity lookup", () => {
it("returns the stored RID set used for duplicate prevention", async () => {
query.mockResolvedValue([SOURCE_NODE_RID, 123, null]);
await expect(getImportedSourceRids()).resolves.toEqual(
new Set([SOURCE_NODE_RID]),
);
expect(query).toHaveBeenCalledOnce();
expect(query.mock.calls[0]?.[0]).toContain(":sourceNodeRid");
});
it("finds the imported Roam page by source RID", async () => {
query.mockResolvedValue([[PAGE_UID]]);
await expect(findImportedNodeUidBySourceRid(SOURCE_NODE_RID)).resolves.toBe(
PAGE_UID,
);
expect(query).toHaveBeenCalledWith(
expect.stringContaining(":sourceNodeRid"),
SOURCE_NODE_RID,
);
});
it("returns null when the source RID has not been imported", async () => {
query.mockResolvedValue([]);
await expect(
findImportedNodeUidBySourceRid(SOURCE_NODE_RID),
).resolves.toBeNull();
});
});