|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import type { Session } from '@/types/api' |
| 3 | +import { inactiveSessionCanResume, resolveAgentSessionIdFromMetadata } from './sessionResume' |
| 4 | + |
| 5 | +function makeSession(overrides: Partial<Session> = {}): Session { |
| 6 | + return { |
| 7 | + id: 'session-1', |
| 8 | + active: false, |
| 9 | + thinking: false, |
| 10 | + activeAt: 0, |
| 11 | + updatedAt: 0, |
| 12 | + metadata: { path: '/tmp/project', host: 'localhost', flavor: 'cursor' }, |
| 13 | + ...overrides, |
| 14 | + } as Session |
| 15 | +} |
| 16 | + |
| 17 | +describe('sessionResume', () => { |
| 18 | + it('resolveAgentSessionIdFromMetadata picks the id matching the session flavor', () => { |
| 19 | + expect(resolveAgentSessionIdFromMetadata({ |
| 20 | + path: '/p', |
| 21 | + host: 'h', |
| 22 | + flavor: 'codex', |
| 23 | + codexSessionId: 'codex-1', |
| 24 | + cursorSessionId: 'cursor-1', |
| 25 | + })).toBe('codex-1') |
| 26 | + expect(resolveAgentSessionIdFromMetadata({ |
| 27 | + path: '/p', |
| 28 | + host: 'h', |
| 29 | + flavor: 'cursor', |
| 30 | + cursorSessionId: 'cursor-1', |
| 31 | + })).toBe('cursor-1') |
| 32 | + }) |
| 33 | + |
| 34 | + it('resolveAgentSessionIdFromMetadata ignores stale cross-flavor ids', () => { |
| 35 | + expect(resolveAgentSessionIdFromMetadata({ |
| 36 | + path: '/p', |
| 37 | + host: 'h', |
| 38 | + flavor: 'cursor', |
| 39 | + codexSessionId: 'codex-1', |
| 40 | + })).toBeUndefined() |
| 41 | + }) |
| 42 | + |
| 43 | + it('resolveAgentSessionIdFromMetadata defaults to claude when flavor is missing', () => { |
| 44 | + expect(resolveAgentSessionIdFromMetadata({ |
| 45 | + path: '/p', |
| 46 | + host: 'h', |
| 47 | + claudeSessionId: 'claude-1', |
| 48 | + })).toBe('claude-1') |
| 49 | + }) |
| 50 | + |
| 51 | + it('inactiveSessionCanResume is true for active sessions', () => { |
| 52 | + expect(inactiveSessionCanResume(makeSession({ active: true }), 0)).toBe(true) |
| 53 | + }) |
| 54 | + |
| 55 | + it('inactiveSessionCanResume allows fresh spawn when no agent id and no messages', () => { |
| 56 | + expect(inactiveSessionCanResume(makeSession(), 0)).toBe(true) |
| 57 | + }) |
| 58 | + |
| 59 | + it('inactiveSessionCanResume allows resume when agent id exists', () => { |
| 60 | + expect(inactiveSessionCanResume(makeSession({ |
| 61 | + metadata: { |
| 62 | + path: '/tmp/project', |
| 63 | + host: 'localhost', |
| 64 | + flavor: 'cursor', |
| 65 | + cursorSessionId: 'cursor-thread-1', |
| 66 | + }, |
| 67 | + }), 5)).toBe(true) |
| 68 | + }) |
| 69 | + |
| 70 | + it('inactiveSessionCanResume rejects inactive sessions with messages but no agent id', () => { |
| 71 | + expect(inactiveSessionCanResume(makeSession(), 3)).toBe(false) |
| 72 | + }) |
| 73 | + |
| 74 | + it('inactiveSessionCanResume rejects when stale cross-flavor agent id is present but no messages', () => { |
| 75 | + expect(inactiveSessionCanResume(makeSession({ |
| 76 | + metadata: { |
| 77 | + path: '/tmp/project', |
| 78 | + host: 'localhost', |
| 79 | + flavor: 'cursor', |
| 80 | + codexSessionId: 'stale-codex-1', |
| 81 | + }, |
| 82 | + }), 0)).toBe(true) |
| 83 | + expect(inactiveSessionCanResume(makeSession({ |
| 84 | + metadata: { |
| 85 | + path: '/tmp/project', |
| 86 | + host: 'localhost', |
| 87 | + flavor: 'cursor', |
| 88 | + codexSessionId: 'stale-codex-1', |
| 89 | + }, |
| 90 | + }), 3)).toBe(false) |
| 91 | + }) |
| 92 | + |
| 93 | + it('inactiveSessionCanResume rejects when metadata path is missing', () => { |
| 94 | + expect(inactiveSessionCanResume(makeSession({ metadata: { path: '', host: 'localhost' } }), 0)).toBe(false) |
| 95 | + }) |
| 96 | + |
| 97 | + it('inactiveSessionCanResume allows claude resume by message recovery when no claudeSessionId is stored', () => { |
| 98 | + expect(inactiveSessionCanResume(makeSession({ |
| 99 | + metadata: { path: '/tmp/project', host: 'localhost', flavor: 'claude' }, |
| 100 | + }), 3)).toBe(true) |
| 101 | + }) |
| 102 | + |
| 103 | + it('inactiveSessionCanResume allows claude recovery when flavor is missing (defaults to claude)', () => { |
| 104 | + expect(inactiveSessionCanResume(makeSession({ |
| 105 | + metadata: { path: '/tmp/project', host: 'localhost' }, |
| 106 | + }), 3)).toBe(true) |
| 107 | + }) |
| 108 | + |
| 109 | + it('inactiveSessionCanResume rejects non-claude flavors with messages but no flavor-specific id (no recovery path)', () => { |
| 110 | + expect(inactiveSessionCanResume(makeSession({ |
| 111 | + metadata: { path: '/tmp/project', host: 'localhost', flavor: 'codex' }, |
| 112 | + }), 3)).toBe(false) |
| 113 | + }) |
| 114 | +}) |
0 commit comments