import { describe, it } from 'bun:test'; import { expect } from 'chai'; import { Context } from '../context'; import { SessionImpl } from './session'; import { CookieSessionStore } from './session-store'; import { SessionStrategyImpl } from './session-strategy'; const options = { maxAge: 1000, sessionKey: 'cell:session' }; /** Runs fn in a request context and waits for it (Context.run does not return its promise with cls-hooked). */ function inContext(fn: () => Promise): Promise { return new Promise((resolve, reject) => { Context.run(async () => { try { Context.setCurrent(new Context({} as any, {} as any)); await fn(); resolve(); } catch (error) { reject(error); } }); }); } describe('SessionImpl', () => { it('is unchanged until a value changes', () => { const session = new SessionImpl(options) as SessionImpl & Record; expect(session.isNew).to.equal(true); expect(session.changed).to.equal(false); session.user = 'tom'; expect(session.changed).to.equal(true); }); it('a new session changes like its JSON does: undefined values do not count, a new expiry does', () => { const session = new SessionImpl(options) as SessionImpl & Record; session.nothing = undefined; session.callback = () => undefined; expect(session.changed).to.equal(false); session.expire += 1000; expect(session.changed).to.equal(true); }); it('a restored session keeps its id and notices changes inside its values', () => { const restored = new SessionImpl(options, { id: 'kept', expire: 1, maxAge: 1000, user: { name: 'tom' } }) as SessionImpl & Record; expect(restored.id).to.equal('kept'); expect(restored.isNew).to.equal(false); expect(restored.changed).to.equal(false); restored.user.name = 'jerry'; expect(restored.changed).to.equal(true); }); it('new sessions get distinct random ids', () => { const ids = new Set(Array.from({ length: 100 }, () => new SessionImpl(options).id)); expect(ids.size).to.equal(100); expect([...ids][0]).to.match(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/); }); it('serializes its values without isNew and private fields', () => { const session = new SessionImpl(options) as SessionImpl & Record; session.user = 'tom'; const json = session.toJSON(); expect(json).to.include({ user: 'tom', maxAge: 1000 }); expect(Object.keys(json)).to.not.include.members(['isNew', '_snapshot']); }); it('notices changes inside nested values', () => { const session = new SessionImpl(options, { id: 'x', user: { name: 'tom', roles: ['admin'] }, expire: 5, maxAge: 1000 }) as SessionImpl & { user: { roles: string[] } }; expect(session.changed).to.equal(false); session.user.roles.push('dev'); expect(session.changed).to.equal(true); }); it('restored from its values it is not new and unchanged', () => { const session = new SessionImpl(options, { id: 'x', user: 'tom', expire: 5, maxAge: 1000 }); expect(session.isNew).to.equal(false); expect(session.changed).to.equal(false); }); }); describe('CookieSessionStore', () => { it('stores a session compressed in a cookie and reads it back', async () => { const jar = new Map(); const store = new CookieSessionStore(); const strategy = new SessionStrategyImpl(); Object.assign(strategy, { sessionOptions: options }); Object.assign(store, { sessionOptions: options, sessionStrategy: strategy, logger: { error: () => undefined } }); await inContext(async () => { Context.setCookies({ get: (k: string) => jar.get(k), set: (k: string, v: string) => { jar.set(k, v); } } as any); const session = new SessionImpl(options) as SessionImpl & Record; session.user = '中文 user'; await store.set(session); const value = jar.get('cell:session')!; expect(value).to.match(/^[A-Za-z0-9+/]+=*$/); expect(Buffer.from(value, 'base64').subarray(0, 2)).to.deep.equal(Buffer.from([0x1f, 0x8b])); const restored = await store.get('ignored') as SessionImpl & Record; expect(restored.user).to.equal('中文 user'); expect(restored.id).to.equal(session.id); expect(restored.changed).to.equal(false); }); }); it('ignores a cookie it cannot read', async () => { const errors: unknown[] = []; const store = new CookieSessionStore(); Object.assign(store, { sessionOptions: options, sessionStrategy: new SessionStrategyImpl(), logger: { error: (e: unknown) => errors.push(e) } }); await inContext(async () => { Context.setCookies({ get: () => 'bm90IGd6aXA=' } as any); expect(await store.get('x')).to.equal(undefined); }); expect(errors).to.have.length(1); }); });