import { test } from 'node:test'; import assert from 'node:assert/strict'; import { setFallbackSession, removeFallbackSession, getFallbackSession } from './sessions'; // The fallback-session helpers read/write specific `cinny_*` keys directly on // `localStorage`. node has none, so install a controllable in-memory mock per // case backed by a Map. const installStorage = (): Map => { const store = new Map(); (globalThis as { localStorage?: unknown }).localStorage = { getItem: (key: string) => (store.has(key) ? store.get(key) : null), setItem: (key: string, value: string) => { store.set(key, String(value)); }, removeItem: (key: string) => { store.delete(key); }, }; return store; }; test('setFallbackSession writes the cinny_* keys', () => { const store = installStorage(); setFallbackSession('token-1', 'DEVICE1', '@alice:example.org', 'https://hs.example.org'); assert.equal(store.get('cinny_access_token'), 'token-1'); assert.equal(store.get('cinny_device_id'), 'DEVICE1'); assert.equal(store.get('cinny_user_id'), '@alice:example.org'); assert.equal(store.get('cinny_hs_base_url'), 'https://hs.example.org'); }); test('getFallbackSession round-trips a full session and flags fallback stores', () => { installStorage(); setFallbackSession('token-1', 'DEVICE1', '@alice:example.org', 'https://hs.example.org'); assert.deepEqual(getFallbackSession(), { baseUrl: 'https://hs.example.org', userId: '@alice:example.org', deviceId: 'DEVICE1', accessToken: 'token-1', fallbackSdkStores: true, }); }); test('getFallbackSession returns undefined when nothing is stored', () => { installStorage(); assert.equal(getFallbackSession(), undefined); }); test('getFallbackSession returns undefined when a single key is missing', () => { // Every one of the four keys is required; missing any one yields undefined. const keys = [ 'cinny_access_token', 'cinny_device_id', 'cinny_user_id', 'cinny_hs_base_url', ] as const; keys.forEach((missing) => { installStorage(); setFallbackSession('token-1', 'DEVICE1', '@alice:example.org', 'https://hs.example.org'); localStorage.removeItem(missing); assert.equal(getFallbackSession(), undefined, `missing ${missing} should yield undefined`); }); }); test('removeFallbackSession clears all keys', () => { const store = installStorage(); setFallbackSession('token-1', 'DEVICE1', '@alice:example.org', 'https://hs.example.org'); removeFallbackSession(); assert.equal(store.size, 0); assert.equal(getFallbackSession(), undefined); });