import assert from 'node:assert/strict';
import test from 'node:test';
import { readJson, readText, sha256 } from './helpers.ts';

interface TemplateRegistry {
  templates: Array<{ file: string; supported: boolean; importOrder: number }>;
}

interface DatasetManifest {
  files: string[];
}

test('golden dataset includes every supported setup template', () => {
  const registry = readJson<TemplateRegistry>('setup/templates/templates.json');
  const dataset = readJson<DatasetManifest>('setup/golden-dataset/dataset.json');
  const datasetFiles = new Set(dataset.files);

  for (const template of registry.templates.filter((item) => item.supported)) {
    assert.equal(datasetFiles.has(template.file), true, `${template.file} should be in golden dataset`);
  }
});

test('preview hash semantics change when CSV content changes', () => {
  const original = readText('setup/golden-dataset/customers.csv');
  const changed = original.replace('Siam Factory', 'Siam Factory Updated');

  assert.notEqual(sha256(original), sha256(changed));
});

test('golden dataset import order follows template registry ordering', () => {
  const registry = readJson<TemplateRegistry>('setup/templates/templates.json');
  const dataset = readJson<DatasetManifest>('setup/golden-dataset/dataset.json');
  const order = new Map(registry.templates.map((template) => [template.file, template.importOrder]));
  const sorted = [...dataset.files].toSorted((a, b) => (order.get(a) ?? 9999) - (order.get(b) ?? 9999));

  assert.deepEqual(dataset.files, sorted);
});
