import { describe, it, expect } from 'vitest';
import {
  statusTone,
  toneClasses,
  formatActivityMeta,
  isSpreadsheetPreviewable,
  isPresentationPreviewable,
  isDocumentPreviewable,
  isOfficePreviewable,
  officePreviewKind,
  isTextPreviewable,
} from './utils';

describe('statusTone', () => {
  it('returns theme-adaptive light-dark() colors', () => {
    const t = statusTone('failed');
    expect(t.bg).toBe('light-dark(#fee2e2, rgba(239,68,68,.15))');
    expect(t.fg).toBe('light-dark(#b91c1c, #fca5a5)');
  });
  it('falls back to neutral surface/muted vars', () => {
    const t = statusTone('whatever-unknown');
    expect(t.bg).toBe('light-dark(#e2e8f0, var(--surface-2))');
    expect(t.fg).toBe('light-dark(#475569, var(--muted))');
  });
});

describe('office プレビュー判定', () => {
  it('Excel は xlsx/xlsm のみ (csv はテキスト経路なので false)', () => {
    expect(isSpreadsheetPreviewable('report.xlsx')).toBe(true);
    expect(isSpreadsheetPreviewable('macro.XLSM')).toBe(true);
    expect(isSpreadsheetPreviewable('data.csv')).toBe(false);
    expect(isSpreadsheetPreviewable('legacy.xls')).toBe(false);
  });
  it('PowerPoint は pptx/ppt', () => {
    expect(isPresentationPreviewable('deck.pptx')).toBe(true);
    expect(isPresentationPreviewable('old.PPT')).toBe(true);
    expect(isPresentationPreviewable('notes.key')).toBe(false);
  });
  it('Word は docx のみ (旧 .doc は対象外)', () => {
    expect(isDocumentPreviewable('report.docx')).toBe(true);
    expect(isDocumentPreviewable('report.DOCX')).toBe(true);
    expect(isDocumentPreviewable('report.doc')).toBe(false);
    expect(isDocumentPreviewable('report.odt')).toBe(false);
  });
  it('isOfficePreviewable は Excel/PowerPoint/Word を拾う', () => {
    expect(isOfficePreviewable('a.xlsx')).toBe(true);
    expect(isOfficePreviewable('a.pptx')).toBe(true);
    expect(isOfficePreviewable('a.docx')).toBe(true);
    expect(isOfficePreviewable('a.pdf')).toBe(false);
  });
  it('officePreviewKind は拡張子から表示種別を返す', () => {
    expect(officePreviewKind('a.xlsx')).toBe('spreadsheet');
    expect(officePreviewKind('a.xlsm')).toBe('spreadsheet');
    expect(officePreviewKind('a.pptx')).toBe('presentation');
    expect(officePreviewKind('a.ppt')).toBe('presentation');
    expect(officePreviewKind('a.docx')).toBe('document');
  });
  it('csv はテキストプレビュー側で扱う (office とは排他)', () => {
    expect(isTextPreviewable('data.csv')).toBe(true);
    expect(isOfficePreviewable('data.csv')).toBe(false);
  });
});

describe('toneClasses', () => {
  it('pairs light + dark for colored tones', () => {
    expect(toneClasses('danger')).toContain('bg-red-50');
    expect(toneClasses('danger')).toContain('dark:text-red-300');
    expect(toneClasses('success')).toContain('dark:bg-emerald-500/15');
  });
  it('neutral stays on semantic tokens (auto-themed, no dark: needed)', () => {
    expect(toneClasses('neutral')).toBe('bg-surface-2 text-slate-700 border-hairline');
  });
});

describe('formatActivityMeta (backend-primary labels)', () => {
  it('shows the physical backend as the worker with a via-proxy suffix', () => {
    expect(formatActivityMeta('aao-gateway', 'quality', 'gpu-rtx-a'))
      .toBe('worker: gpu-rtx-a (via aao-gateway) · mode: quality');
  });

  it('falls back to the plain worker name when no backend tag exists', () => {
    expect(formatActivityMeta('my-worker', null, null)).toBe('worker: my-worker');
  });

  it('ignores a backend equal to the worker id (direct worker echo)', () => {
    expect(formatActivityMeta('gpu-a', 'auto', 'gpu-a'))
      .toBe('worker: gpu-a · mode: auto');
  });
});
