|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { SessionWatcher } from '../watcher.js'; |
| 3 | +import type { SessionData } from '../types.js'; |
| 4 | + |
| 5 | +function mockSession(overrides: Partial<SessionData> = {}): SessionData { |
| 6 | + return { |
| 7 | + id: 'test-id', |
| 8 | + filePath: '/tmp/test.jsonl', |
| 9 | + project: 'default-project', |
| 10 | + source: 'claude-code', |
| 11 | + model: 'claude-sonnet-4-20250514', |
| 12 | + cwd: '/tmp', |
| 13 | + startedAt: new Date(), |
| 14 | + isActive: false, |
| 15 | + tokens: { input: 0, output: 0, cacheCreation: 0, cacheRead: 0 }, |
| 16 | + compactions: 0, |
| 17 | + toolCalls: {}, |
| 18 | + agentSpawns: 0, |
| 19 | + skillInvocations: [], |
| 20 | + subagents: [], |
| 21 | + agentDescriptions: [], |
| 22 | + ...overrides, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +function createWatcherWithSessions( |
| 27 | + sessions: SessionData[] |
| 28 | +): SessionWatcher { |
| 29 | + const watcher = new SessionWatcher(['/tmp'], false); |
| 30 | + const map = (watcher as any).sessions as Map<string, SessionData>; |
| 31 | + for (const s of sessions) { |
| 32 | + map.set(s.id, s); |
| 33 | + } |
| 34 | + return watcher; |
| 35 | +} |
| 36 | + |
| 37 | +describe('SessionWatcher.getProjects', () => { |
| 38 | + it('returns unique sorted project names from loaded sessions', () => { |
| 39 | + const watcher = createWatcherWithSessions([ |
| 40 | + mockSession({ id: 's1', project: 'proj-b' }), |
| 41 | + mockSession({ id: 's2', project: 'proj-a' }), |
| 42 | + mockSession({ id: 's3', project: 'proj-b' }), |
| 43 | + ]); |
| 44 | + |
| 45 | + expect(watcher.getProjects()).toEqual(['proj-a', 'proj-b']); |
| 46 | + }); |
| 47 | + |
| 48 | + it('returns empty array when no sessions loaded', () => { |
| 49 | + const watcher = new SessionWatcher(['/tmp'], false); |
| 50 | + expect(watcher.getProjects()).toEqual([]); |
| 51 | + }); |
| 52 | + |
| 53 | + it('handles sessions with empty/undefined project', () => { |
| 54 | + const watcher = createWatcherWithSessions([ |
| 55 | + mockSession({ id: 's1', project: '' }), |
| 56 | + mockSession({ id: 's2', project: 'proj-a' }), |
| 57 | + ]); |
| 58 | + |
| 59 | + // Empty string is falsy, so getProjects() skips it |
| 60 | + expect(watcher.getProjects()).toEqual(['proj-a']); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('SessionWatcher.getSessions with projectFilter', () => { |
| 65 | + it('returns all sessions when no projectFilter is given (backward compat)', () => { |
| 66 | + const now = new Date(); |
| 67 | + const watcher = createWatcherWithSessions([ |
| 68 | + mockSession({ id: 's1', project: 'proj-a', startedAt: now }), |
| 69 | + mockSession({ id: 's2', project: 'proj-b', startedAt: now }), |
| 70 | + ]); |
| 71 | + |
| 72 | + expect(watcher.getSessions(false)).toHaveLength(2); |
| 73 | + }); |
| 74 | + |
| 75 | + it('filters sessions by project name', () => { |
| 76 | + const now = new Date(); |
| 77 | + const watcher = createWatcherWithSessions([ |
| 78 | + mockSession({ id: 's1', project: 'proj-a', startedAt: now }), |
| 79 | + mockSession({ id: 's2', project: 'proj-b', startedAt: now }), |
| 80 | + mockSession({ id: 's3', project: 'proj-a', startedAt: now }), |
| 81 | + ]); |
| 82 | + |
| 83 | + const filtered = watcher.getSessions(false, 'proj-a'); |
| 84 | + expect(filtered).toHaveLength(2); |
| 85 | + expect(filtered.every(s => s.project === 'proj-a')).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('combines activeOnly and projectFilter', () => { |
| 89 | + const now = new Date(); |
| 90 | + const watcher = createWatcherWithSessions([ |
| 91 | + mockSession({ id: 's1', project: 'proj-a', isActive: true, startedAt: now }), |
| 92 | + mockSession({ id: 's2', project: 'proj-a', isActive: false, startedAt: now }), |
| 93 | + mockSession({ id: 's3', project: 'proj-b', isActive: true, startedAt: now }), |
| 94 | + ]); |
| 95 | + |
| 96 | + const filtered = watcher.getSessions(true, 'proj-a'); |
| 97 | + expect(filtered).toHaveLength(1); |
| 98 | + expect(filtered[0].id).toBe('s1'); |
| 99 | + }); |
| 100 | + |
| 101 | + it('returns empty when projectFilter matches no sessions', () => { |
| 102 | + const watcher = createWatcherWithSessions([ |
| 103 | + mockSession({ id: 's1', project: 'proj-a', startedAt: new Date() }), |
| 104 | + ]); |
| 105 | + |
| 106 | + const filtered = watcher.getSessions(false, 'nonexistent'); |
| 107 | + expect(filtered).toHaveLength(0); |
| 108 | + }); |
| 109 | + |
| 110 | + it('sorts sessions by startedAt descending', () => { |
| 111 | + const t1 = new Date('2026-03-10T10:00:00Z'); |
| 112 | + const t2 = new Date('2026-03-11T10:00:00Z'); |
| 113 | + const t3 = new Date('2026-03-12T10:00:00Z'); |
| 114 | + const watcher = createWatcherWithSessions([ |
| 115 | + mockSession({ id: 's1', project: 'proj-a', startedAt: t1 }), |
| 116 | + mockSession({ id: 's2', project: 'proj-a', startedAt: t3 }), |
| 117 | + mockSession({ id: 's3', project: 'proj-a', startedAt: t2 }), |
| 118 | + ]); |
| 119 | + |
| 120 | + const result = watcher.getSessions(false, 'proj-a'); |
| 121 | + expect(result.map(s => s.id)).toEqual(['s2', 's3', 's1']); |
| 122 | + }); |
| 123 | +}); |
0 commit comments