import { describe, it, expect, vi } from 'vitest';
import { StreamParser } from '../stream-parser.js';
describe('StreamParser', () => {
    it('should emit chunks as they arrive', () => {
        const onChunk = vi.fn();
        const onDone = vi.fn();
        const parser = new StreamParser({ onChunk, onDone });
        parser.write('Hello ');
        parser.write('world');
        parser.end();
        expect(onChunk).toHaveBeenCalledTimes(2);
        expect(onChunk).toHaveBeenNthCalledWith(1, 'Hello ');
        expect(onChunk).toHaveBeenNthCalledWith(2, 'world');
        expect(onDone).toHaveBeenCalledOnce();
        expect(onDone).toHaveBeenCalledWith('Hello world');
    });
    it('should handle empty writes', () => {
        const onChunk = vi.fn();
        const onDone = vi.fn();
        const parser = new StreamParser({ onChunk, onDone });
        parser.write('');
        parser.write('text');
        parser.end();
        expect(onChunk).toHaveBeenCalledTimes(1);
        expect(onChunk).toHaveBeenCalledWith('text');
        expect(onDone).toHaveBeenCalledWith('text');
    });
    it('should handle Buffer input', () => {
        const onChunk = vi.fn();
        const onDone = vi.fn();
        const parser = new StreamParser({ onChunk, onDone });
        parser.write(Buffer.from('Hello'));
        parser.end();
        expect(onChunk).toHaveBeenCalledWith('Hello');
        expect(onDone).toHaveBeenCalledWith('Hello');
    });
    it('should accumulate full response for done callback', () => {
        const onChunk = vi.fn();
        const onDone = vi.fn();
        const parser = new StreamParser({ onChunk, onDone });
        parser.write('Part 1. ');
        parser.write('Part 2. ');
        parser.write('Part 3.');
        parser.end();
        expect(onDone).toHaveBeenCalledWith('Part 1. Part 2. Part 3.');
    });
    it('should handle end without any writes', () => {
        const onChunk = vi.fn();
        const onDone = vi.fn();
        const parser = new StreamParser({ onChunk, onDone });
        parser.end();
        expect(onChunk).not.toHaveBeenCalled();
        expect(onDone).toHaveBeenCalledWith('');
    });
    it('should ignore writes after end', () => {
        const onChunk = vi.fn();
        const onDone = vi.fn();
        const parser = new StreamParser({ onChunk, onDone });
        parser.write('before');
        parser.end();
        parser.write('after');
        expect(onChunk).toHaveBeenCalledTimes(1);
        expect(onDone).toHaveBeenCalledOnce();
    });
});
//# sourceMappingURL=stream-parser.test.js.map