/**
 * Sessions 路由单元测试
 */
import { describe, it, expect } from 'vitest';
describe('sessions route validation', () => {
    it('requires agentId for recovery', () => {
        const validateRecoverRequest = (body) => {
            if (!body.agentId)
                return { valid: false, error: 'agentId is required' };
            return { valid: true };
        };
        expect(validateRecoverRequest({}).valid).toBe(false);
        expect(validateRecoverRequest({ agentId: 'agent-123' }).valid).toBe(true);
    });
    it('uses provided workspacePath when available', () => {
        const resolveWorkspacePath = (provided, persisted) => {
            return provided || persisted || null;
        };
        expect(resolveWorkspacePath('/provided/path', '/persisted/path')).toBe('/provided/path');
        expect(resolveWorkspacePath(undefined, '/persisted/path')).toBe('/persisted/path');
        expect(resolveWorkspacePath(undefined, undefined)).toBe(null);
    });
    it('resolves command with fallback chain', () => {
        const resolveCommand = (provided, persisted) => {
            return provided || persisted || 'claude';
        };
        expect(resolveCommand('custom-cmd', 'persisted-cmd')).toBe('custom-cmd');
        expect(resolveCommand(undefined, 'persisted-cmd')).toBe('persisted-cmd');
        expect(resolveCommand(undefined, undefined)).toBe('claude');
    });
});
describe('session persistence', () => {
    it('transitions from running to stopped on exit', () => {
        const sessions = [
            { sessionId: 'session-1', status: 'running' },
            { sessionId: 'session-2', status: 'running' },
        ];
        const updated = sessions.map(s => s.sessionId === 'session-1' ? { ...s, status: 'stopped' } : s);
        expect(updated[0].status).toBe('stopped');
        expect(updated[1].status).toBe('running');
    });
    it('filters out active sessions from persisted list', () => {
        const active = [{ sessionId: 'session-1' }];
        const persisted = [{ sessionId: 'session-1' }, { sessionId: 'session-2' }];
        const activeIds = new Set(active.map(s => s.sessionId));
        const result = persisted.filter(s => !activeIds.has(s.sessionId));
        expect(result.length).toBe(1);
        expect(result[0].sessionId).toBe('session-2');
    });
});
describe('terminal configuration', () => {
    it('selects correct shell for platform', () => {
        const getShellConfig = (platform) => {
            if (platform === 'win32')
                return { shell: 'powershell.exe', args: ['-NoLogo'] };
            return { shell: 'bash', args: [] };
        };
        expect(getShellConfig('win32').shell).toBe('powershell.exe');
        expect(getShellConfig('linux').shell).toBe('bash');
    });
    it('generates correct terminal environment', () => {
        const buildTerminalEnv = (agentId, baseEnv) => ({
            ...baseEnv,
            AWS_AGENT_ID: agentId,
        });
        const env = buildTerminalEnv('agent-123', { PATH: '/usr/bin' });
        expect(env.AWS_AGENT_ID).toBe('agent-123');
        expect(env.PATH).toBe('/usr/bin');
    });
});
