|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { buildWorkSessionIndex, groupKeyOf } from '@/utils/sessionGrouping' |
| 3 | +import type { ActivityRecord, MCPSession } from '@/types/api' |
| 4 | + |
| 5 | +const rec = (p: Partial<ActivityRecord>): ActivityRecord => |
| 6 | + ({ status: 'success', timestamp: '2026-07-13T05:30:00Z', ...p }) as ActivityRecord |
| 7 | + |
| 8 | +// The bug this file exists for (Spec 082 SC-002). |
| 9 | +// |
| 10 | +// One opencode connection, transport session `mcp-session-…858cea`, wrote four |
| 11 | +// activity records. The first two named built-ins that did not mark the session |
| 12 | +// as worked, so they carry no work_session_id; the last two carry `ws-…000abe`. |
| 13 | +// Grouping by `work_session_id || session_id` put them in two different buckets |
| 14 | +// and the session picker showed ONE connection as TWO sessions — labelled with |
| 15 | +// the tails of two different ids, `58cea` and `00abe`. |
| 16 | +describe('work-session grouping', () => { |
| 17 | + const SID = 'mcp-session-9958d45e-015f-4c16-8d9a-74792c858cea' |
| 18 | + const WSID = 'ws-a3f505743e000abe' |
| 19 | + |
| 20 | + const opencodeRun: ActivityRecord[] = [ |
| 21 | + rec({ session_id: SID, tool_name: 'list_registries' }), // no work_session_id |
| 22 | + rec({ session_id: SID, tool_name: 'upstream_servers' }), // no work_session_id |
| 23 | + rec({ session_id: SID, work_session_id: WSID, tool_name: 'retrieve_tools' }), |
| 24 | + rec({ session_id: SID, work_session_id: WSID, tool_name: 'echo' }), |
| 25 | + ] |
| 26 | + |
| 27 | + it('folds records with no work session into their connection’s work session', () => { |
| 28 | + const index = buildWorkSessionIndex(opencodeRun, []) |
| 29 | + const keys = new Set(opencodeRun.map(a => groupKeyOf(a, index))) |
| 30 | + |
| 31 | + expect(keys).toEqual(new Set([WSID])) |
| 32 | + }) |
| 33 | + |
| 34 | + it('learns the mapping from the sessions API too, not only from sibling records', () => { |
| 35 | + // The orphaned rows on their own — the work session is only known because |
| 36 | + // /api/v1/sessions reports it for this transport session. |
| 37 | + const orphans = opencodeRun.slice(0, 2) |
| 38 | + const sessions = [{ id: SID, work_session_id: WSID } as MCPSession] |
| 39 | + |
| 40 | + const index = buildWorkSessionIndex(orphans, sessions) |
| 41 | + |
| 42 | + expect(orphans.map(a => groupKeyOf(a, index))).toEqual([WSID, WSID]) |
| 43 | + }) |
| 44 | + |
| 45 | + it('falls back to the transport session when no work session is known anywhere', () => { |
| 46 | + // Pre-082 rows, and clients whose work session was never resolved. |
| 47 | + const legacy = [rec({ session_id: 'sess-legacy', tool_name: 'echo' })] |
| 48 | + const index = buildWorkSessionIndex(legacy, []) |
| 49 | + |
| 50 | + expect(groupKeyOf(legacy[0], index)).toBe('sess-legacy') |
| 51 | + }) |
| 52 | + |
| 53 | + it('keeps genuinely different work sessions apart', () => { |
| 54 | + // Same client reconnecting into DIFFERENT work (e.g. a different project): |
| 55 | + // two transport sessions, two work sessions. These must not be merged. |
| 56 | + const rows = [ |
| 57 | + rec({ session_id: 'sess-a', work_session_id: 'ws-project-one' }), |
| 58 | + rec({ session_id: 'sess-b', work_session_id: 'ws-project-two' }), |
| 59 | + ] |
| 60 | + const index = buildWorkSessionIndex(rows, []) |
| 61 | + |
| 62 | + expect(rows.map(a => groupKeyOf(a, index))).toEqual(['ws-project-one', 'ws-project-two']) |
| 63 | + }) |
| 64 | + |
| 65 | + it('groups a work session that spans several connections (the reconnect case)', () => { |
| 66 | + const rows = [ |
| 67 | + rec({ session_id: 'sess-1', work_session_id: 'ws-same' }), |
| 68 | + rec({ session_id: 'sess-2', work_session_id: 'ws-same' }), |
| 69 | + rec({ session_id: 'sess-2' }), // orphan on the second connection |
| 70 | + ] |
| 71 | + const index = buildWorkSessionIndex(rows, []) |
| 72 | + |
| 73 | + expect(new Set(rows.map(a => groupKeyOf(a, index)))).toEqual(new Set(['ws-same'])) |
| 74 | + }) |
| 75 | + |
| 76 | + it('tolerates records with no session id at all', () => { |
| 77 | + const index = buildWorkSessionIndex([rec({ tool_name: 'echo' })], []) |
| 78 | + expect(groupKeyOf(rec({ tool_name: 'echo' }), index)).toBe('') |
| 79 | + }) |
| 80 | +}) |
0 commit comments