|
9 | 9 | stripOrphanedAssignments, |
10 | 10 | writeChannelSectionsStore, |
11 | 11 | } from "./channelSectionsStorage.ts"; |
| 12 | +import { normalizeRelayUrl } from "@/features/profile/lib/selfProfileStorage"; |
12 | 13 |
|
13 | 14 | if (typeof globalThis.window === "undefined") { |
14 | 15 | const storage = new Map(); |
@@ -202,3 +203,125 @@ test("writeChannelSectionsStore: returns false when setItem throws", () => { |
202 | 203 | test("storageKey: returns expected format with pubkey", () => { |
203 | 204 | assert.equal(storageKey("abc123"), "buzz-channel-sections.v1:abc123"); |
204 | 205 | }); |
| 206 | + |
| 207 | +// ─── Relay-scoped key tests ─────────────────────────────────────────────────── |
| 208 | + |
| 209 | +test("storageKey: with relayUrl includes normalized+encoded relay in key", () => { |
| 210 | + const relay = "wss://relay.example.com"; |
| 211 | + const key = storageKey("pk1", relay); |
| 212 | + assert.equal( |
| 213 | + key, |
| 214 | + `buzz-channel-sections.v1:pk1:${encodeURIComponent(normalizeRelayUrl(relay))}`, |
| 215 | + ); |
| 216 | +}); |
| 217 | + |
| 218 | +test("storageKey: without relayUrl returns legacy pubkey-only key", () => { |
| 219 | + assert.equal(storageKey("pk1"), "buzz-channel-sections.v1:pk1"); |
| 220 | + assert.equal(storageKey("pk1", undefined), "buzz-channel-sections.v1:pk1"); |
| 221 | +}); |
| 222 | + |
| 223 | +test("storageKey: two different relays produce different keys for same pubkey", () => { |
| 224 | + const k1 = storageKey("pk1", "wss://relay-a.example.com"); |
| 225 | + const k2 = storageKey("pk1", "wss://relay-b.example.com"); |
| 226 | + assert.notEqual(k1, k2); |
| 227 | +}); |
| 228 | + |
| 229 | +test("storageKey: equivalent relay URLs (case + trailing slash) map to the same key", () => { |
| 230 | + const k1 = storageKey("pk1", "WSS://Relay.Example/"); |
| 231 | + const k2 = storageKey("pk1", "wss://relay.example"); |
| 232 | + assert.equal(k1, k2); |
| 233 | +}); |
| 234 | + |
| 235 | +test("readChannelSectionsStore + writeChannelSectionsStore: scoped write/read roundtrip", () => { |
| 236 | + const pubkey = "pk-relay-roundtrip"; |
| 237 | + const relay = "wss://relay.example.com"; |
| 238 | + const store = makeStore({ |
| 239 | + sections: [makeSection({ id: "s1", name: "Work", order: 0 })], |
| 240 | + assignments: { chan1: "s1" }, |
| 241 | + }); |
| 242 | + assert.equal(writeChannelSectionsStore(pubkey, store, relay), true); |
| 243 | + const result = readChannelSectionsStore(pubkey, relay); |
| 244 | + assert.deepEqual(result, store); |
| 245 | +}); |
| 246 | + |
| 247 | +test("readChannelSectionsStore: scoped key is isolated from other relay's data", () => { |
| 248 | + const pubkey = "pk-isolation"; |
| 249 | + const relayA = "wss://relay-a.example.com"; |
| 250 | + const relayB = "wss://relay-b.example.com"; |
| 251 | + const storeA = makeStore({ |
| 252 | + sections: [makeSection({ id: "sa", name: "Relay A section", order: 0 })], |
| 253 | + assignments: {}, |
| 254 | + }); |
| 255 | + writeChannelSectionsStore(pubkey, storeA, relayA); |
| 256 | + // Relay B should see empty store, not relay A's data. |
| 257 | + const resultB = readChannelSectionsStore(pubkey, relayB); |
| 258 | + assert.deepEqual(resultB, DEFAULT_STORE); |
| 259 | +}); |
| 260 | + |
| 261 | +test("readChannelSectionsStore: migrates legacy unscoped data on first scoped read", () => { |
| 262 | + const pubkey = "pk-migrate"; |
| 263 | + const relay = "wss://relay-migrate.example.com"; |
| 264 | + const legacyStore = makeStore({ |
| 265 | + sections: [makeSection({ id: "sl", name: "Legacy Section", order: 0 })], |
| 266 | + assignments: {}, |
| 267 | + }); |
| 268 | + // Write under legacy (pubkey-only) key. |
| 269 | + writeChannelSectionsStore(pubkey, legacyStore); |
| 270 | + // First scoped read should migrate and return the legacy data. |
| 271 | + const result = readChannelSectionsStore(pubkey, relay); |
| 272 | + assert.deepEqual(result, legacyStore); |
| 273 | + // Legacy key must be deleted after migration (globally one-time guarantee). |
| 274 | + const legacyKey = storageKey(pubkey); |
| 275 | + assert.equal(window.localStorage.getItem(legacyKey), null); |
| 276 | + // Subsequent scoped reads should hit the scoped key directly. |
| 277 | + const result2 = readChannelSectionsStore(pubkey, relay); |
| 278 | + assert.deepEqual(result2, legacyStore); |
| 279 | +}); |
| 280 | + |
| 281 | +test("readChannelSectionsStore: migration is globally one-time — relay B sees DEFAULT_STORE after relay A migrates", () => { |
| 282 | + const pubkey = "pk-migrate-once"; |
| 283 | + const relayA = "wss://relay-migrate-once-a.example.com"; |
| 284 | + const relayB = "wss://relay-migrate-once-b.example.com"; |
| 285 | + const legacyStore = makeStore({ |
| 286 | + sections: [makeSection({ id: "sm", name: "Migrated Section", order: 0 })], |
| 287 | + assignments: {}, |
| 288 | + }); |
| 289 | + // Write under legacy key then migrate via relay A. |
| 290 | + writeChannelSectionsStore(pubkey, legacyStore); |
| 291 | + readChannelSectionsStore(pubkey, relayA); // triggers migration, deletes legacy key |
| 292 | + // Relay B must see DEFAULT_STORE — legacy data must not bleed in. |
| 293 | + const resultB = readChannelSectionsStore(pubkey, relayB); |
| 294 | + assert.deepEqual(resultB, DEFAULT_STORE); |
| 295 | + // Relay B scoped key must not have been created. |
| 296 | + const scopedBKey = storageKey(pubkey, relayB); |
| 297 | + assert.equal(window.localStorage.getItem(scopedBKey), null); |
| 298 | +}); |
| 299 | + |
| 300 | +test("readChannelSectionsStore: migration only copies non-empty legacy stores", () => { |
| 301 | + const pubkey = "pk-migrate-empty"; |
| 302 | + const relay = "wss://relay-migrate-empty.example.com"; |
| 303 | + // Write an explicitly empty store under the legacy key. |
| 304 | + writeChannelSectionsStore(pubkey, DEFAULT_STORE); |
| 305 | + // Empty legacy store should NOT be migrated — we get DEFAULT_STORE. |
| 306 | + const result = readChannelSectionsStore(pubkey, relay); |
| 307 | + assert.deepEqual(result, DEFAULT_STORE); |
| 308 | +}); |
| 309 | + |
| 310 | +test("readChannelSectionsStore: scoped key takes precedence over legacy key after migration", () => { |
| 311 | + const pubkey = "pk-precedence"; |
| 312 | + const relay = "wss://relay-precedence.example.com"; |
| 313 | + const legacyStore = makeStore({ |
| 314 | + sections: [makeSection({ id: "sold", name: "Old Section", order: 0 })], |
| 315 | + assignments: {}, |
| 316 | + }); |
| 317 | + const newStore = makeStore({ |
| 318 | + sections: [makeSection({ id: "snew", name: "New Section", order: 0 })], |
| 319 | + assignments: {}, |
| 320 | + }); |
| 321 | + // Write legacy data then scoped data (simulates post-migration state). |
| 322 | + writeChannelSectionsStore(pubkey, legacyStore); |
| 323 | + writeChannelSectionsStore(pubkey, newStore, relay); |
| 324 | + // Scoped read must return the scoped (newer) store, not the legacy one. |
| 325 | + const result = readChannelSectionsStore(pubkey, relay); |
| 326 | + assert.deepEqual(result, newStore); |
| 327 | +}); |
0 commit comments