import { describe, it, expect } from 'vitest';
import { ByteRingBuffer } from './ring-buffer.js';

describe('ByteRingBuffer', () => {
  it('append within cap stores all bytes', () => {
    const b = new ByteRingBuffer(10);
    b.append(Buffer.from('abc'));
    b.append(Buffer.from('de'));
    expect(b.bytes).toBe(5);
    expect(b.concat().toString()).toBe('abcde');
  });

  it('append over cap drops oldest bytes', () => {
    const b = new ByteRingBuffer(5);
    b.append(Buffer.from('abcde'));
    b.append(Buffer.from('fg'));
    expect(b.bytes).toBe(5);
    expect(b.concat().toString()).toBe('cdefg');
  });

  it('single append larger than cap stores only the tail', () => {
    const b = new ByteRingBuffer(3);
    b.append(Buffer.from('abcdef'));
    expect(b.bytes).toBe(3);
    expect(b.concat().toString()).toBe('def');
  });

  it('tail(n) returns only the last n bytes', () => {
    const b = new ByteRingBuffer(10);
    b.append(Buffer.from('abcdefgh'));
    expect(b.tail(3).toString()).toBe('fgh');
    expect(b.tail(100).toString()).toBe('abcdefgh');
  });

  it('clear empties the buffer', () => {
    const b = new ByteRingBuffer(10);
    b.append(Buffer.from('xyz'));
    b.clear();
    expect(b.bytes).toBe(0);
    expect(b.concat().length).toBe(0);
  });
});
