import { describe, test, expect } from 'bun:test';
import { ValidationError } from '@shade/core';
import { buildChunkNonce, buildChunkAad, MAX_SEQ, STREAM_NONCE_BYTES } from '../src/index.js';

describe('buildChunkNonce', () => {
  test('produces 12-byte output', () => {
    expect(buildChunkNonce(0, 0).length).toBe(STREAM_NONCE_BYTES);
  });

  test('encodes laneId as u32 BE in bytes [0..4)', () => {
    const n = buildChunkNonce(0x01020304, 0);
    expect(n[0]).toBe(0x01);
    expect(n[1]).toBe(0x02);
    expect(n[2]).toBe(0x03);
    expect(n[3]).toBe(0x04);
  });

  test('encodes seq as u64 BE in bytes [4..12)', () => {
    const n = buildChunkNonce(0, 0x0102030405060708n);
    expect(n[4]).toBe(0x01);
    expect(n[5]).toBe(0x02);
    expect(n[6]).toBe(0x03);
    expect(n[7]).toBe(0x04);
    expect(n[8]).toBe(0x05);
    expect(n[9]).toBe(0x06);
    expect(n[10]).toBe(0x07);
    expect(n[11]).toBe(0x08);
  });

  test('different (laneId, seq) yields different nonces', () => {
    const seen = new Set<string>();
    for (let lane = 0; lane < 4; lane++) {
      for (let seq = 0; seq < 100; seq++) {
        const n = buildChunkNonce(lane, seq);
        const key = Array.from(n).join(',');
        expect(seen.has(key)).toBe(false);
        seen.add(key);
      }
    }
  });

  test('accepts both number and bigint seq', () => {
    const a = buildChunkNonce(1, 42);
    const b = buildChunkNonce(1, 42n);
    expect(a).toEqual(b);
  });

  test('handles MAX_SEQ', () => {
    const n = buildChunkNonce(0, MAX_SEQ);
    for (let i = 4; i < 12; i++) expect(n[i]).toBe(0xff);
  });

  test('rejects out-of-range laneId', () => {
    expect(() => buildChunkNonce(-1, 0)).toThrow(ValidationError);
    expect(() => buildChunkNonce(0x1_0000_0000, 0)).toThrow(ValidationError);
    expect(() => buildChunkNonce(1.5, 0)).toThrow(ValidationError);
  });

  test('rejects out-of-range seq', () => {
    expect(() => buildChunkNonce(0, -1)).toThrow(ValidationError);
    expect(() => buildChunkNonce(0, MAX_SEQ + 1n)).toThrow(ValidationError);
  });
});

describe('buildChunkAad', () => {
  test('produces 29-byte output (16+4+8+1)', () => {
    const id = new Uint8Array(16);
    expect(buildChunkAad(id, 0, 0, false).length).toBe(29);
  });

  test('embeds streamId, laneId, seq, isLast in canonical layout', () => {
    const id = new Uint8Array(16).fill(0xaa);
    const aad = buildChunkAad(id, 0x01020304, 0x05060708090a0b0cn, true);
    for (let i = 0; i < 16; i++) expect(aad[i]).toBe(0xaa);
    expect(aad[16]).toBe(0x01);
    expect(aad[17]).toBe(0x02);
    expect(aad[18]).toBe(0x03);
    expect(aad[19]).toBe(0x04);
    expect(aad[20]).toBe(0x05);
    expect(aad[27]).toBe(0x0c);
    expect(aad[28]).toBe(0x01);
  });

  test('isLast=false sets last byte to 0x00', () => {
    const id = new Uint8Array(16);
    const aad = buildChunkAad(id, 0, 0, false);
    expect(aad[28]).toBe(0x00);
  });

  test('rejects wrong-length streamId', () => {
    expect(() => buildChunkAad(new Uint8Array(15), 0, 0, false)).toThrow(ValidationError);
    expect(() => buildChunkAad(new Uint8Array(17), 0, 0, false)).toThrow(ValidationError);
  });

  test('rejects out-of-range laneId / seq', () => {
    const id = new Uint8Array(16);
    expect(() => buildChunkAad(id, -1, 0, false)).toThrow(ValidationError);
    expect(() => buildChunkAad(id, 0, -1, false)).toThrow(ValidationError);
  });
});
