/**
 * Testes do utilitário de captura de canvas (M9.3).
 *
 * Valida apenas lógica independente de DOM (parsing de data URL,
 * estimativas). As funções que dependem de `document` e
 * `HTMLCanvasElement` (canvasToDataURL, captureCanvasImage, downloadImage)
 * são exercitadas apenas no browser real, validadas por tipagem estática.
 */

import { describe, it, expect } from 'vitest';
import { estimateDataUrlSizeKB } from '../canvas-capture';

describe('M9.3 — Estimativa de tamanho de data URL', () => {
  it('Data URL vazia retorna 0', () => {
    expect(estimateDataUrlSizeKB('')).toBe(0);
  });

  it('Data URL sem vírgula retorna 0', () => {
    expect(estimateDataUrlSizeKB('data:image/png;base64')).toBe(0);
  });

  it('Tamanho aproximado coerente com base64 (~75% do base64 / 1024)', () => {
    const base64 = 'A'.repeat(1000);
    const url = `data:image/png;base64,${base64}`;
    const expected = Math.round((1000 * 3) / 4 / 1024);
    expect(estimateDataUrlSizeKB(url)).toBe(expected);
  });

  it('4 KB de base64 → ~3 KB de binário', () => {
    const base64 = 'A'.repeat(4096);
    const url = `data:image/png;base64,${base64}`;
    expect(estimateDataUrlSizeKB(url)).toBe(3);
  });

  it('100 KB de base64 → ~75 KB', () => {
    const base64 = 'A'.repeat(102_400);
    const url = `data:image/png;base64,${base64}`;
    expect(estimateDataUrlSizeKB(url)).toBe(75);
  });
});

describe('M9.3 — Constantes e tipos de saída', () => {
  it('Formato PNG não usa qualidade', () => {
    const url = 'data:image/png;base64,AAAA';
    expect(url.startsWith('data:image/png')).toBe(true);
  });

  it('Formato JPEG usa mime type correto', () => {
    const url = 'data:image/jpeg;base64,AAAA';
    expect(url.startsWith('data:image/jpeg')).toBe(true);
  });

  it('Formato WebP suportado', () => {
    const url = 'data:image/webp;base64,AAAA';
    expect(url.startsWith('data:image/webp')).toBe(true);
  });
});

describe('M9.3 — Sanity do módulo', () => {
  it('Exporta função principal captureCanvasImage', async () => {
    const mod = await import('../canvas-capture');
    expect(typeof mod.captureCanvasImage).toBe('function');
  });

  it('Exporta canvasToDataURL', async () => {
    const mod = await import('../canvas-capture');
    expect(typeof mod.canvasToDataURL).toBe('function');
  });

  it('Exporta downloadImage', async () => {
    const mod = await import('../canvas-capture');
    expect(typeof mod.downloadImage).toBe('function');
  });

  it('Exporta dataURLtoBlob', async () => {
    const mod = await import('../canvas-capture');
    expect(typeof mod.dataURLtoBlob).toBe('function');
  });

  it('Exporta estimateDataUrlSizeKB', async () => {
    const mod = await import('../canvas-capture');
    expect(typeof mod.estimateDataUrlSizeKB).toBe('function');
  });
});