Files
cinny/src/app/state/sessions.test.ts
T
jared 9f4516c6a8 test: add suites for state/sessions, recentSearches, upload (+17)
Via subagent, all verified against real behavior:
- state/sessions (5): fallback-session round-trip across the four cinny_* keys,
  missing-key → undefined for each required key, removeFallbackSession clears all.
- state/recentSearches (6): addRecentSearch prepend, case-sensitive dedupe +
  move-to-front, trim, ignore empty/whitespace, cap at 10.
- state/upload (6): the createUploadAtom reducer driven through a real jotai
  store — idle→loading→progress(gated)→success/error, file ref preserved.

No bugs found.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 14:29:36 -04:00

75 lines
2.6 KiB
TypeScript

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<string, string> => {
const store = new Map<string, string>();
(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);
});