import { describe, test, expect } from 'bun:test';
import { SubtleCryptoProvider } from '@shade/crypto-web';
import { ValidationError } from '@shade/core';
import {
  deriveStreamKey,
  deriveLaneKey,
  generateStreamId,
  generateStreamSecret,
} from '../src/index.js';

const crypto = new SubtleCryptoProvider();

function hex(bytes: Uint8Array): string {
  return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
}

describe('deriveStreamKey', () => {
  test('produces 32-byte output', async () => {
    const secret = generateStreamSecret(crypto);
    const id = generateStreamId(crypto);
    const key = await deriveStreamKey(crypto, secret, id);
    expect(key.length).toBe(32);
  });

  test('is deterministic for the same inputs', async () => {
    const secret = new Uint8Array(32).fill(7);
    const id = new Uint8Array(16).fill(3);
    const a = await deriveStreamKey(crypto, secret, id);
    const b = await deriveStreamKey(crypto, secret, id);
    expect(hex(a)).toBe(hex(b));
  });

  test('changes with streamSecret', async () => {
    const id = new Uint8Array(16).fill(1);
    const a = await deriveStreamKey(crypto, new Uint8Array(32).fill(1), id);
    const b = await deriveStreamKey(crypto, new Uint8Array(32).fill(2), id);
    expect(hex(a)).not.toBe(hex(b));
  });

  test('changes with streamId', async () => {
    const secret = new Uint8Array(32).fill(9);
    const a = await deriveStreamKey(crypto, secret, new Uint8Array(16).fill(1));
    const b = await deriveStreamKey(crypto, secret, new Uint8Array(16).fill(2));
    expect(hex(a)).not.toBe(hex(b));
  });

  test('rejects wrong-length streamSecret', async () => {
    const id = new Uint8Array(16);
    await expect(deriveStreamKey(crypto, new Uint8Array(31), id)).rejects.toThrow(ValidationError);
    await expect(deriveStreamKey(crypto, new Uint8Array(33), id)).rejects.toThrow(ValidationError);
  });

  test('rejects wrong-length streamId', async () => {
    const secret = new Uint8Array(32);
    await expect(deriveStreamKey(crypto, secret, new Uint8Array(15))).rejects.toThrow(ValidationError);
    await expect(deriveStreamKey(crypto, secret, new Uint8Array(17))).rejects.toThrow(ValidationError);
  });
});

describe('deriveLaneKey', () => {
  test('produces 32-byte output', async () => {
    const streamKey = new Uint8Array(32).fill(5);
    const id = new Uint8Array(16).fill(2);
    const laneKey = await deriveLaneKey(crypto, streamKey, id, 0);
    expect(laneKey.length).toBe(32);
  });

  test('is deterministic for the same (streamKey, streamId, laneId)', async () => {
    const streamKey = new Uint8Array(32).fill(5);
    const id = new Uint8Array(16).fill(2);
    const a = await deriveLaneKey(crypto, streamKey, id, 7);
    const b = await deriveLaneKey(crypto, streamKey, id, 7);
    expect(hex(a)).toBe(hex(b));
  });

  test('different laneId yields different lane keys', async () => {
    const streamKey = new Uint8Array(32).fill(5);
    const id = new Uint8Array(16).fill(2);
    const a = await deriveLaneKey(crypto, streamKey, id, 0);
    const b = await deriveLaneKey(crypto, streamKey, id, 1);
    expect(hex(a)).not.toBe(hex(b));
  });

  test('different streamKey yields different lane keys', async () => {
    const id = new Uint8Array(16).fill(2);
    const a = await deriveLaneKey(crypto, new Uint8Array(32).fill(5), id, 0);
    const b = await deriveLaneKey(crypto, new Uint8Array(32).fill(6), id, 0);
    expect(hex(a)).not.toBe(hex(b));
  });

  test('rejects laneId outside u32 range', async () => {
    const streamKey = new Uint8Array(32);
    const id = new Uint8Array(16);
    await expect(deriveLaneKey(crypto, streamKey, id, -1)).rejects.toThrow(ValidationError);
    await expect(deriveLaneKey(crypto, streamKey, id, 0x1_0000_0000)).rejects.toThrow(
      ValidationError,
    );
    await expect(deriveLaneKey(crypto, streamKey, id, 1.5)).rejects.toThrow(ValidationError);
  });

  test('full pipeline: streamSecret → streamKey → laneKey is deterministic across both sides', async () => {
    const secret = new Uint8Array(32).fill(0xab);
    const id = new Uint8Array(16).fill(0xcd);
    const senderStreamKey = await deriveStreamKey(crypto, secret, id);
    const senderLaneKey = await deriveLaneKey(crypto, senderStreamKey, id, 3);
    const receiverStreamKey = await deriveStreamKey(crypto, secret, id);
    const receiverLaneKey = await deriveLaneKey(crypto, receiverStreamKey, id, 3);
    expect(hex(senderLaneKey)).toBe(hex(receiverLaneKey));
  });
});
