import { projectFilePath } from '@learnitive/project-storage';
import { describe, expect, it } from 'vitest';
import { buildLatexCompileResult } from '../result';

describe('R14.23.0 Biber compile results', () => {
  it('accepts a successful BusyTeX Biber compile instead of injecting the retired unsupported diagnostic', () => {
    const result = buildLatexCompileResult({
      backend: 'busytex',
      compiler: 'pdflatex',
      compileMode: 'full',
      exitCode: 0,
      pdf: new Uint8Array([37, 80, 68, 70]),
      log: 'Biber pass complete',
      durationMs: 1,
      bibliography: {
        bibtex: false,
        biberRequested: true,
        makeindex: false,
        rerun: true,
      },
    });

    expect(result.success).toBe(true);
    expect(result.diagnostics.some((item) => item.code === 'biber-not-supported')).toBe(false);
  });

  it('exposes generated files as read-only compiler artifacts without mixing them into project source', () => {
    const aux = new TextEncoder().encode('\\relax');
    const result = buildLatexCompileResult({
      backend: 'busytex',
      compiler: 'pdflatex',
      compileMode: 'full',
      rootFile: projectFilePath('chapters/main.tex'),
      exitCode: 0,
      pdf: new Uint8Array([37, 80, 68, 70]),
      synctex: new Uint8Array([1, 2, 3]),
      log: 'ok',
      durationMs: 1,
      bibliography: { bibtex: false, biberRequested: false, makeindex: false, rerun: false },
      artifacts: [{
        path: 'chapters/main.aux',
        kind: 'aux',
        mimeType: 'text/plain;charset=utf-8',
        size: aux.byteLength,
        generated: true,
        text: true,
        data: aux,
        textContent: '\\relax',
      }],
    });

    expect(result.artifacts?.map((artifact) => artifact.path)).toEqual([
      'chapters/main.pdf',
      'chapters/main.log',
      'chapters/main.synctex',
      'chapters/main.aux',
    ]);
    expect(result.artifacts?.find((artifact) => artifact.kind === 'aux')?.generated).toBe(true);
  });

  it('retains detailed real-compile performance telemetry', () => {
    const result = buildLatexCompileResult({
      backend: 'busytex',
      compiler: 'pdflatex',
      compileMode: 'full',
      exitCode: 0,
      pdf: new Uint8Array([37, 80, 68, 70]),
      log: 'ok',
      durationMs: 140,
      bibliography: { bibtex: false, biberRequested: false, makeindex: false, rerun: false },
      performance: {
        runtimeInitMs: 20,
        packageFetchMs: 10,
        bibliographyMs: 0,
        engineCompileMs: 90,
        pdfReadyMs: 20,
        totalMs: 140,
        runtimeWasWarm: false,
        packagesFetched: 2,
      },
    });

    expect(result.performance).toEqual({
      runtimeInitMs: 20,
      packageFetchMs: 10,
      bibliographyMs: 0,
      engineCompileMs: 90,
      pdfReadyMs: 20,
      totalMs: 140,
      runtimeWasWarm: false,
      packagesFetched: 2,
    });
  });

  it('normalizes a wrapper zero to failure when the native runtime reports a failed TeX process', () => {
    const result = buildLatexCompileResult({
      backend: 'busytex',
      compiler: 'pdflatex',
      compileMode: 'full',
      exitCode: 0,
      reportedSuccess: false,
      log: 'program exited (with status: 1)',
      durationMs: 1,
      bibliography: { bibtex: false, biberRequested: false, makeindex: false, rerun: false },
    });

    expect(result.success).toBe(false);
    expect(result.exitCode).toBe(1);
  });

  it('rejects a wrapper zero when the TeX log is fatal even if PDF-looking bytes were returned', () => {
    const result = buildLatexCompileResult({
      backend: 'busytex',
      compiler: 'pdflatex',
      compileMode: 'full',
      exitCode: 0,
      reportedSuccess: true,
      pdf: new Uint8Array([37, 80, 68, 70]),
      log: 'pdfTeX error: /bin/busytex (file cm-super-ts1.enc): cannot open encoding file for reading',
      durationMs: 1,
      bibliography: { bibtex: false, biberRequested: false, makeindex: false, rerun: false },
    });

    expect(result.success).toBe(false);
    expect(result.exitCode).toBe(1);
  });

  it('rejects unreadable PDF output even when the runtime and wrapper status are zero', () => {
    const result = buildLatexCompileResult({
      backend: 'busytex',
      compiler: 'pdflatex',
      compileMode: 'full',
      exitCode: 0,
      reportedSuccess: true,
      pdf: new Uint8Array([1, 2, 3, 4]),
      log: 'ok',
      durationMs: 1,
      bibliography: { bibtex: false, biberRequested: false, makeindex: false, rerun: false },
    });

    expect(result.success).toBe(false);
    expect(result.exitCode).toBe(1);
  });

});
