|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { DISCOURSE_GRAPH_PROP_NAME } from "~/utils/createReifiedBlock"; |
| 3 | +import { |
| 4 | + findImportedNodeUidBySourceRid, |
| 5 | + getImportedSourceRids, |
| 6 | + IMPORTED_FROM_PROP_KEY, |
| 7 | + parseImportedSourceIdentity, |
| 8 | + readImportedSourceIdentity, |
| 9 | + writeImportedSourceIdentity, |
| 10 | +} from "~/utils/importedSourceIdentity"; |
| 11 | +import type { json } from "~/utils/getBlockProps"; |
| 12 | + |
| 13 | +const SOURCE_NODE_RID = "orn:obsidian.note:vault-a/node-1"; |
| 14 | +const SOURCE_MODIFIED_AT = "2026-06-14T15:00:00.000Z"; |
| 15 | +const PAGE_UID = "page-uid"; |
| 16 | + |
| 17 | +const propsByUid = new Map<string, Record<string, json>>(); |
| 18 | +const query = vi.fn(); |
| 19 | + |
| 20 | +const setRoamAlphaApi = (): void => { |
| 21 | + (globalThis as { window: unknown }).window = { |
| 22 | + roamAlphaAPI: { |
| 23 | + data: { |
| 24 | + async: { q: query }, |
| 25 | + block: { |
| 26 | + update: vi.fn( |
| 27 | + ({ |
| 28 | + block, |
| 29 | + }: { |
| 30 | + block: { props: Record<string, json>; uid: string }; |
| 31 | + }) => { |
| 32 | + propsByUid.set(block.uid, block.props); |
| 33 | + }, |
| 34 | + ), |
| 35 | + }, |
| 36 | + }, |
| 37 | + pull: (_pattern: string, [, uid]: [string, string]) => ({ |
| 38 | + ":block/props": propsByUid.get(uid) ?? {}, |
| 39 | + }), |
| 40 | + }, |
| 41 | + }; |
| 42 | +}; |
| 43 | + |
| 44 | +beforeEach(() => { |
| 45 | + propsByUid.clear(); |
| 46 | + query.mockReset(); |
| 47 | + setRoamAlphaApi(); |
| 48 | +}); |
| 49 | + |
| 50 | +describe("imported source identity metadata", () => { |
| 51 | + it("reads the source RID without depending on display metadata", () => { |
| 52 | + const props = { |
| 53 | + [DISCOURSE_GRAPH_PROP_NAME]: { |
| 54 | + [IMPORTED_FROM_PROP_KEY]: { |
| 55 | + sourceModifiedAt: SOURCE_MODIFIED_AT, |
| 56 | + sourceNodeRid: SOURCE_NODE_RID, |
| 57 | + sourceTitle: "Legacy title that may change", |
| 58 | + }, |
| 59 | + }, |
| 60 | + }; |
| 61 | + |
| 62 | + expect(parseImportedSourceIdentity(props)).toEqual({ |
| 63 | + sourceModifiedAt: SOURCE_MODIFIED_AT, |
| 64 | + sourceNodeRid: SOURCE_NODE_RID, |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it("returns undefined for missing or malformed source identity", () => { |
| 69 | + expect(parseImportedSourceIdentity({})).toBeUndefined(); |
| 70 | + expect( |
| 71 | + parseImportedSourceIdentity({ |
| 72 | + [DISCOURSE_GRAPH_PROP_NAME]: { |
| 73 | + [IMPORTED_FROM_PROP_KEY]: { sourceNodeRid: 123 }, |
| 74 | + }, |
| 75 | + }), |
| 76 | + ).toBeUndefined(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("writes the source RID and modified time while preserving sibling metadata", () => { |
| 80 | + propsByUid.set(PAGE_UID, { |
| 81 | + [DISCOURSE_GRAPH_PROP_NAME]: { |
| 82 | + "relation-migration": { relationUid: 1718000000000 }, |
| 83 | + }, |
| 84 | + "other-extension": { enabled: true }, |
| 85 | + }); |
| 86 | + |
| 87 | + writeImportedSourceIdentity({ |
| 88 | + pageUid: PAGE_UID, |
| 89 | + sourceModifiedAt: SOURCE_MODIFIED_AT, |
| 90 | + sourceNodeRid: SOURCE_NODE_RID, |
| 91 | + }); |
| 92 | + |
| 93 | + expect(readImportedSourceIdentity(PAGE_UID)).toEqual({ |
| 94 | + sourceModifiedAt: SOURCE_MODIFIED_AT, |
| 95 | + sourceNodeRid: SOURCE_NODE_RID, |
| 96 | + }); |
| 97 | + expect(propsByUid.get(PAGE_UID)).toEqual({ |
| 98 | + [DISCOURSE_GRAPH_PROP_NAME]: { |
| 99 | + "relation-migration": { relationUid: 1718000000000 }, |
| 100 | + [IMPORTED_FROM_PROP_KEY]: { |
| 101 | + sourceModifiedAt: SOURCE_MODIFIED_AT, |
| 102 | + sourceNodeRid: SOURCE_NODE_RID, |
| 103 | + }, |
| 104 | + }, |
| 105 | + "other-extension": { enabled: true }, |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe("imported source identity lookup", () => { |
| 111 | + it("returns the stored RID set used for duplicate prevention", async () => { |
| 112 | + query.mockResolvedValue([SOURCE_NODE_RID, 123, null]); |
| 113 | + |
| 114 | + await expect(getImportedSourceRids()).resolves.toEqual( |
| 115 | + new Set([SOURCE_NODE_RID]), |
| 116 | + ); |
| 117 | + expect(query).toHaveBeenCalledOnce(); |
| 118 | + expect(query.mock.calls[0]?.[0]).toContain(":sourceNodeRid"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("finds the imported Roam page by source RID", async () => { |
| 122 | + query.mockResolvedValue([[PAGE_UID]]); |
| 123 | + |
| 124 | + await expect(findImportedNodeUidBySourceRid(SOURCE_NODE_RID)).resolves.toBe( |
| 125 | + PAGE_UID, |
| 126 | + ); |
| 127 | + expect(query).toHaveBeenCalledWith( |
| 128 | + expect.stringContaining(":sourceNodeRid"), |
| 129 | + SOURCE_NODE_RID, |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it("returns null when the source RID has not been imported", async () => { |
| 134 | + query.mockResolvedValue([]); |
| 135 | + |
| 136 | + await expect( |
| 137 | + findImportedNodeUidBySourceRid(SOURCE_NODE_RID), |
| 138 | + ).resolves.toBeNull(); |
| 139 | + }); |
| 140 | +}); |
0 commit comments