Something went wrong. Try again.
Client side atproto account migrator in your web browser, along with services for backups and adversarial migrations. pdsmoover.com
pds atproto migrations moo cow
Something went wrong. Try again.
2.5 kB · 68 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869import {afterEach, describe, expect, it, vi} from 'vitest'import type {SessionData} from '@atproto/lex-password-session'import {createSessionCache} from './sessionCache'
const KEY = 'test:session-cache'
/** Minimal in-memory stand-in for the browser localStorage the cache talks to. */function memoryStorage(): Storage { const store = new Map<string, string>() return { getItem: (key) => store.get(key) ?? null, setItem: (key, value) => void store.set(key, value), removeItem: (key) => void store.delete(key), clear: () => store.clear(), key: (index) => [...store.keys()][index] ?? null, get length() { return store.size }, }}
function session(did: string, service: string): SessionData { return {did, service} as SessionData}
describe('createSessionCache', () => { afterEach(() => { vi.unstubAllGlobals() })
it('reads each side independently when no side is required', () => { vi.stubGlobal('localStorage', memoryStorage()) const cache = createSessionCache<'current' | 'old'>(KEY, [])
cache.saveSide('current', session('did:plc:me', 'https://new.pds'))
expect(cache.load()?.current?.service).toBe('https://new.pds') expect(cache.load()?.old).toBeUndefined() })
it('returns null and clears the entry when a required side is missing', () => { vi.stubGlobal('localStorage', memoryStorage()) const cache = createSessionCache<'old'>(KEY, ['old'])
cache.saveSide('old', session('did:plc:me', 'https://old.pds')) expect(cache.load()?.old?.service).toBe('https://old.pds')
cache.saveSide('old', null) expect(cache.load()).toBeNull() expect(localStorage.getItem(KEY)).toBeNull() })
it('keeps the remaining side when one of two is removed, then drops the key', () => { vi.stubGlobal('localStorage', memoryStorage()) const cache = createSessionCache<'current' | 'old'>(KEY, [])
cache.saveSide('current', session('did:plc:me', 'https://new.pds')) cache.saveSide('old', session('did:plc:me', 'https://old.pds')) cache.saveSide('old', null)
expect(cache.load()?.current?.service).toBe('https://new.pds') expect(cache.load()?.old).toBeUndefined() expect(localStorage.getItem(KEY)).not.toBeNull()
cache.saveSide('current', null) expect(localStorage.getItem(KEY)).toBeNull() })})