/**
 * Session Output 服务单元测试
 */
import { afterEach, describe, expect, it } from 'vitest';
import { appendSessionOutput, sessions } from './session-output.js';
const createSession = (overrides = {}) => ({
    agentId: 'agent-1',
    workspacePath: 'D:/workspace',
    outputBuffer: '',
    flushTimer: null,
    flushInFlight: null,
    seq: 0,
    ...overrides,
});
afterEach(() => {
    sessions.clear();
});
describe('session-output service', () => {
    it('manages sessions map correctly', () => {
        sessions.set('session-1', createSession());
        expect(sessions.size).toBe(1);
        expect(sessions.has('session-1')).toBe(true);
        const session = sessions.get('session-1');
        if (session)
            session.outputBuffer = 'test output';
        expect(sessions.get('session-1')?.outputBuffer).toBe('test output');
        sessions.delete('session-1');
        expect(sessions.size).toBe(0);
    });
    it('finds session by agentId', () => {
        sessions.set('session-1', createSession({ agentId: 'agent-1' }));
        sessions.set('session-2', createSession({ agentId: 'agent-2' }));
        const findByAgentId = (agentId) => {
            for (const [id, session] of sessions) {
                if (session.agentId === agentId)
                    return id;
            }
            return null;
        };
        expect(findByAgentId('agent-1')).toBe('session-1');
        expect(findByAgentId('agent-3')).toBe(null);
    });
    it('accumulates output correctly', () => {
        sessions.set('session-1', createSession());
        appendSessionOutput('session-1', 'line1\n');
        appendSessionOutput('session-1', 'line2\n');
        expect(sessions.get('session-1')?.outputBuffer).toBe('line1\nline2\n');
    });
    it('bounds pending SDK output so callback failures cannot grow memory without limit', () => {
        sessions.set('session-bounded', createSession());
        appendSessionOutput('session-bounded', 'a'.repeat(70 * 1024));
        const output = sessions.get('session-bounded')?.outputBuffer ?? '';
        expect(output.length).toBeLessThanOrEqual(64 * 1024);
        expect(output).toContain('AgentsWorkStudio: omitted');
        expect(output.endsWith('a')).toBe(true);
    });
    it('increments sequence correctly', () => {
        const session = createSession();
        sessions.set('session-1', session);
        session.seq += 1;
        expect(session.seq).toBe(1);
        session.seq += 1;
        expect(session.seq).toBe(2);
    });
    it('builds correct status payload', () => {
        const buildStatusPayload = (agentId, sessionId, status) => ({
            agentId,
            sessionId,
            status,
        });
        expect(buildStatusPayload('agent-1', 'session-1', 'running')).toEqual({
            agentId: 'agent-1',
            sessionId: 'session-1',
            status: 'running',
        });
        expect(buildStatusPayload('agent-1', null, 'stopped')).toEqual({
            agentId: 'agent-1',
            sessionId: null,
            status: 'stopped',
        });
    });
});
