|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { |
| 5 | + mergeHistoryOverSnapshot, |
| 6 | + messageSnapshotKey, |
| 7 | + readMessageSnapshot, |
| 8 | + removeMessageSnapshotsForRelay, |
| 9 | + writeMessageSnapshot, |
| 10 | +} from "./messageSnapshot.ts"; |
| 11 | + |
| 12 | +if (typeof globalThis.window === "undefined") { |
| 13 | + const storage = new Map(); |
| 14 | + globalThis.window = { |
| 15 | + localStorage: { |
| 16 | + getItem: (key) => storage.get(key) ?? null, |
| 17 | + setItem: (key, value) => storage.set(key, value), |
| 18 | + removeItem: (key) => storage.delete(key), |
| 19 | + key: (index) => [...storage.keys()][index] ?? null, |
| 20 | + get length() { |
| 21 | + return storage.size; |
| 22 | + }, |
| 23 | + }, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +function makeEvent(overrides = {}) { |
| 28 | + return { |
| 29 | + id: `event-${Math.random().toString(36).slice(2)}`, |
| 30 | + pubkey: "pubkey-1", |
| 31 | + created_at: 1_700_000_000, |
| 32 | + kind: 9, |
| 33 | + tags: [["h", "chan-1"]], |
| 34 | + content: "hello", |
| 35 | + sig: "sig", |
| 36 | + ...overrides, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +const RELAY = "wss://relay.example.com"; |
| 41 | + |
| 42 | +function clearRelay(relayUrl = RELAY) { |
| 43 | + removeMessageSnapshotsForRelay(relayUrl); |
| 44 | +} |
| 45 | + |
| 46 | +test("messageSnapshotKey: normalizes trailing slash and case", () => { |
| 47 | + assert.equal( |
| 48 | + messageSnapshotKey("WSS://Relay.Example.com/", "chan-1"), |
| 49 | + messageSnapshotKey("wss://relay.example.com", "chan-1"), |
| 50 | + ); |
| 51 | +}); |
| 52 | + |
| 53 | +test("read after write returns the persisted events", () => { |
| 54 | + clearRelay(); |
| 55 | + const events = [makeEvent({ id: "a" }), makeEvent({ id: "b" })]; |
| 56 | + writeMessageSnapshot(RELAY, "chan-1", events); |
| 57 | + assert.deepEqual(readMessageSnapshot(RELAY, "chan-1"), events); |
| 58 | +}); |
| 59 | + |
| 60 | +test("read for an unknown channel returns null", () => { |
| 61 | + clearRelay(); |
| 62 | + assert.equal(readMessageSnapshot(RELAY, "chan-never"), null); |
| 63 | +}); |
| 64 | + |
| 65 | +test("read returns null for malformed JSON", () => { |
| 66 | + window.localStorage.setItem( |
| 67 | + messageSnapshotKey(RELAY, "chan-bad"), |
| 68 | + "not-json{{{", |
| 69 | + ); |
| 70 | + assert.equal(readMessageSnapshot(RELAY, "chan-bad"), null); |
| 71 | +}); |
| 72 | + |
| 73 | +test("read returns null for a wrong-version payload", () => { |
| 74 | + window.localStorage.setItem( |
| 75 | + messageSnapshotKey(RELAY, "chan-v2"), |
| 76 | + JSON.stringify({ version: 2, updatedAt: 1, events: [makeEvent()] }), |
| 77 | + ); |
| 78 | + assert.equal(readMessageSnapshot(RELAY, "chan-v2"), null); |
| 79 | +}); |
| 80 | + |
| 81 | +test("pending optimistic events are not persisted", () => { |
| 82 | + clearRelay(); |
| 83 | + const settled = makeEvent({ id: "settled" }); |
| 84 | + writeMessageSnapshot(RELAY, "chan-1", [ |
| 85 | + settled, |
| 86 | + makeEvent({ id: "optimistic", pending: true }), |
| 87 | + ]); |
| 88 | + assert.deepEqual(readMessageSnapshot(RELAY, "chan-1"), [settled]); |
| 89 | +}); |
| 90 | + |
| 91 | +test("write with only pending events persists nothing", () => { |
| 92 | + clearRelay(); |
| 93 | + writeMessageSnapshot(RELAY, "chan-1", [makeEvent({ pending: true })]); |
| 94 | + assert.equal(readMessageSnapshot(RELAY, "chan-1"), null); |
| 95 | +}); |
| 96 | + |
| 97 | +test("snapshot keeps only the newest slice of a long timeline", () => { |
| 98 | + clearRelay(); |
| 99 | + const events = Array.from({ length: 200 }, (_, i) => |
| 100 | + makeEvent({ id: `event-${i}`, created_at: 1_700_000_000 + i }), |
| 101 | + ); |
| 102 | + writeMessageSnapshot(RELAY, "chan-1", events); |
| 103 | + const persisted = readMessageSnapshot(RELAY, "chan-1"); |
| 104 | + assert.equal(persisted.length, 80); |
| 105 | + assert.equal(persisted[persisted.length - 1].id, "event-199"); |
| 106 | + assert.equal(persisted[0].id, "event-120"); |
| 107 | +}); |
| 108 | + |
| 109 | +test("per-relay channel cap evicts the least recently written snapshot", () => { |
| 110 | + clearRelay(); |
| 111 | + for (let i = 0; i < 21; i++) { |
| 112 | + writeMessageSnapshot(RELAY, `chan-${i}`, [makeEvent({ id: `e-${i}` })]); |
| 113 | + } |
| 114 | + // chan-0 was written first (oldest updatedAt tie broken by insertion) — |
| 115 | + // with 21 channels, at least one of the earliest must be evicted and the |
| 116 | + // newest retained. |
| 117 | + assert.notEqual(readMessageSnapshot(RELAY, "chan-20"), null); |
| 118 | + const retained = Array.from({ length: 21 }, (_, i) => |
| 119 | + readMessageSnapshot(RELAY, `chan-${i}`), |
| 120 | + ).filter((snapshot) => snapshot !== null); |
| 121 | + assert.equal(retained.length, 20); |
| 122 | +}); |
| 123 | + |
| 124 | +test("remove clears every snapshot for that relay only", () => { |
| 125 | + clearRelay(); |
| 126 | + clearRelay("wss://other.example.com"); |
| 127 | + writeMessageSnapshot(RELAY, "chan-1", [makeEvent({ id: "keep-other" })]); |
| 128 | + writeMessageSnapshot("wss://other.example.com", "chan-1", [ |
| 129 | + makeEvent({ id: "other" }), |
| 130 | + ]); |
| 131 | + removeMessageSnapshotsForRelay(RELAY); |
| 132 | + assert.equal(readMessageSnapshot(RELAY, "chan-1"), null); |
| 133 | + assert.notEqual( |
| 134 | + readMessageSnapshot("wss://other.example.com", "chan-1"), |
| 135 | + null, |
| 136 | + ); |
| 137 | +}); |
| 138 | + |
| 139 | +test("write is tolerant of storage failures", () => { |
| 140 | + const original = window.localStorage.setItem; |
| 141 | + window.localStorage.setItem = () => { |
| 142 | + throw new Error("quota exceeded"); |
| 143 | + }; |
| 144 | + try { |
| 145 | + assert.doesNotThrow(() => |
| 146 | + writeMessageSnapshot(RELAY, "chan-1", [makeEvent()]), |
| 147 | + ); |
| 148 | + } finally { |
| 149 | + window.localStorage.setItem = original; |
| 150 | + } |
| 151 | +}); |
| 152 | + |
| 153 | +test("cold snapshot load: merge keeps snapshot-only rows and widens aux backfill to them", () => { |
| 154 | + const snapshotOnly = makeEvent({ id: "ghost", created_at: 1_700_000_000 }); |
| 155 | + const fresh = makeEvent({ id: "fresh", created_at: 1_700_000_100 }); |
| 156 | + const { merged, auxBackfillWindow } = mergeHistoryOverSnapshot({ |
| 157 | + cached: undefined, |
| 158 | + snapshot: [snapshotOnly], |
| 159 | + history: [fresh], |
| 160 | + }); |
| 161 | + assert.deepEqual( |
| 162 | + merged.map((event) => event.id), |
| 163 | + ["ghost", "fresh"], |
| 164 | + ); |
| 165 | + assert.ok(auxBackfillWindow.some((event) => event.id === "ghost")); |
| 166 | + assert.ok(auxBackfillWindow.some((event) => event.id === "fresh")); |
| 167 | +}); |
| 168 | + |
| 169 | +test("warm load: aux backfill stays scoped to the fresh window", () => { |
| 170 | + const cached = makeEvent({ id: "cached", created_at: 1_700_000_000 }); |
| 171 | + const fresh = makeEvent({ id: "fresh", created_at: 1_700_000_100 }); |
| 172 | + const { merged, auxBackfillWindow } = mergeHistoryOverSnapshot({ |
| 173 | + cached: [cached], |
| 174 | + snapshot: [makeEvent({ id: "stale-snapshot" })], |
| 175 | + history: [fresh], |
| 176 | + }); |
| 177 | + assert.ok(merged.some((event) => event.id === "cached")); |
| 178 | + assert.deepEqual( |
| 179 | + auxBackfillWindow.map((event) => event.id), |
| 180 | + ["fresh"], |
| 181 | + ); |
| 182 | +}); |
| 183 | + |
| 184 | +test("cold load without a snapshot backfills the fresh window only", () => { |
| 185 | + const fresh = makeEvent({ id: "fresh" }); |
| 186 | + const { merged, auxBackfillWindow } = mergeHistoryOverSnapshot({ |
| 187 | + cached: undefined, |
| 188 | + snapshot: null, |
| 189 | + history: [fresh], |
| 190 | + }); |
| 191 | + assert.deepEqual( |
| 192 | + merged.map((event) => event.id), |
| 193 | + ["fresh"], |
| 194 | + ); |
| 195 | + assert.deepEqual( |
| 196 | + auxBackfillWindow.map((event) => event.id), |
| 197 | + ["fresh"], |
| 198 | + ); |
| 199 | +}); |
0 commit comments