|
| 1 | +/** |
| 2 | + * The session registry's own behavior: a session's `ttlMinutes` lifetime and |
| 3 | + * `listSessions` filters. Each test loads a fresh registry module, so it |
| 4 | + * starts empty and its cleanup interval runs on this test's fake clock. |
| 5 | + */ |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 7 | +import type { SessionEvent } from '../types'; |
| 8 | + |
| 9 | +type Registry = typeof import('../registry')['SessionRegistry']; |
| 10 | + |
| 11 | +const A = 'https://a.example'; |
| 12 | +const B = 'https://b.example'; |
| 13 | +const START = new Date('2026-12-04T12:00:00Z'); |
| 14 | + |
| 15 | +let registry: Registry; |
| 16 | + |
| 17 | +beforeEach(async () => { |
| 18 | + vi.useFakeTimers(); |
| 19 | + vi.setSystemTime(START); |
| 20 | + vi.resetModules(); |
| 21 | + ({ SessionRegistry: registry } = await import('../registry')); |
| 22 | +}); |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + vi.useRealTimers(); |
| 26 | +}); |
| 27 | + |
| 28 | +function create(origin: string, ttlMinutes?: number): string { |
| 29 | + const result = registry.createExplicitSession(origin, { |
| 30 | + capabilities: { llm: {} }, |
| 31 | + ...(ttlMinutes !== undefined ? { limits: { ttlMinutes } } : {}), |
| 32 | + }); |
| 33 | + expect(result.success).toBe(true); |
| 34 | + return result.sessionId!; |
| 35 | +} |
| 36 | + |
| 37 | +describe('a session with ttlMinutes', () => { |
| 38 | + it('is usable until its lifetime runs out, then refused as expired and listed as terminated', () => { |
| 39 | + const id = create(A, 5); |
| 40 | + |
| 41 | + vi.setSystemTime(START.getTime() + 5 * 60_000 - 1); |
| 42 | + expect(registry.getValidatedSession(id, A).status).toBe('active'); |
| 43 | + |
| 44 | + vi.setSystemTime(START.getTime() + 5 * 60_000 + 1); |
| 45 | + expect(() => registry.getValidatedSession(id, A)).toThrow('Session has expired'); |
| 46 | + expect(registry.listSessions({ origin: A })).toEqual([ |
| 47 | + expect.objectContaining({ sessionId: id, status: 'terminated' }), |
| 48 | + ]); |
| 49 | + // Once terminated, a later lookup says so rather than expiring it again. |
| 50 | + expect(() => registry.getValidatedSession(id, A)).toThrow('Session has been terminated'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('is terminated by the periodic cleanup even if nothing looks it up', () => { |
| 54 | + const id = create(A, 5); |
| 55 | + const events: SessionEvent[] = []; |
| 56 | + registry.subscribe((e) => events.push(e)); |
| 57 | + |
| 58 | + // Cleanup runs every 10 minutes; the session expired at 5. |
| 59 | + vi.advanceTimersByTime(10 * 60_000); |
| 60 | + |
| 61 | + expect(registry.getSession(id)?.status).toBe('terminated'); |
| 62 | + expect(events).toContainEqual({ type: 'session_terminated', sessionId: id, origin: A }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('without ttlMinutes has no expiry', () => { |
| 66 | + const id = create(A); |
| 67 | + expect(registry.getSession(id)?.capabilities.limits?.expiresAt).toBeUndefined(); |
| 68 | + |
| 69 | + vi.setSystemTime(START.getTime() + 24 * 60 * 60_000); |
| 70 | + expect(registry.getValidatedSession(id, A).status).toBe('active'); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('listSessions', () => { |
| 75 | + it('filters by origin, status and activeOnly, and lists the most recently active first', () => { |
| 76 | + const a1 = create(A); |
| 77 | + vi.advanceTimersByTime(1_000); |
| 78 | + const a2 = create(A); |
| 79 | + vi.advanceTimersByTime(1_000); |
| 80 | + const b1 = create(B); |
| 81 | + registry.terminateSession(a1, A); |
| 82 | + vi.advanceTimersByTime(1_000); |
| 83 | + registry.touchSession(a1); |
| 84 | + |
| 85 | + const ids = (opts?: Parameters<Registry['listSessions']>[0]) => |
| 86 | + registry.listSessions(opts).map((s) => s.sessionId); |
| 87 | + |
| 88 | + expect(ids()).toEqual([a1, b1, a2]); |
| 89 | + expect(ids({ origin: A })).toEqual([a1, a2]); |
| 90 | + expect(ids({ origin: B })).toEqual([b1]); |
| 91 | + expect(ids({ origin: 'https://none.example' })).toEqual([]); |
| 92 | + expect(ids({ status: 'terminated' })).toEqual([a1]); |
| 93 | + expect(ids({ status: 'active' })).toEqual([b1, a2]); |
| 94 | + expect(ids({ activeOnly: true })).toEqual([b1, a2]); |
| 95 | + expect(ids({ origin: A, activeOnly: true })).toEqual([a2]); |
| 96 | + // 'explicit' is the only session type the registry produces. |
| 97 | + expect(ids({ type: 'explicit' })).toEqual([a1, b1, a2]); |
| 98 | + // The type arrives untyped from runtime messages; an unknown type matches nothing. |
| 99 | + expect(ids({ type: 'implicit' as never })).toEqual([]); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns summaries, not the sessions themselves', () => { |
| 103 | + create(A); |
| 104 | + const [summary] = registry.listSessions(); |
| 105 | + expect(Object.keys(summary).sort()).toEqual( |
| 106 | + ['capabilities', 'createdAt', 'lastActiveAt', 'name', 'origin', 'sessionId', 'status', 'type', 'usage'].sort(), |
| 107 | + ); |
| 108 | + expect(summary.capabilities).toEqual({ hasLLM: true, hasBrowserAccess: false }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments