import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';

const workflowPath = new URL('../.gitea/workflows/quality.yml', import.meta.url);
const diffCheckPath = new URL('../scripts/check_diff.sh', import.meta.url);
const pythonRequirementsPath = new URL('../requirements-test.txt', import.meta.url);

test('Gitea CI gates pull requests and main with the reproducible quality suite', async () => {
  const workflow = await readFile(workflowPath, 'utf8');

  assert.match(workflow, /^name: Quality gates$/m);
  assert.match(workflow, /^on:\n  push:\n    branches: \[main\]\n  pull_request:\n    branches: \[main\]$/m);
  assert.match(workflow, /^permissions:\n  contents: read$/m);
  assert.match(workflow, /^    timeout-minutes: 15$/m);
  assert.match(workflow, /fetch-depth: 0/);
  assert.match(workflow, /npm ci/);
  assert.match(workflow, /python3 -m pip install --break-system-packages -r requirements-test\.txt/);
  assert.match(workflow, /npm test/);
  assert.match(workflow, /npm run test:ui/);
  assert.match(workflow, /npm run test:photo/);
  assert.match(workflow, /npm run test:sleek/);
  assert.match(workflow, /npm audit --audit-level=high/);
  assert.match(workflow, /npm run check:syntax/);
  assert.match(workflow, /npm run check:diff/);
});

test('CI pins the Python image dependency required by the full unit suite', async () => {
  const requirements = await readFile(pythonRequirementsPath, 'utf8');

  assert.match(requirements, /^Pillow==12\.3\.0$/m);
});

test('diff hygiene checks only the pull request or latest commit range', async () => {
  const script = await readFile(diffCheckPath, 'utf8');

  assert.match(script, /origin\/\$GITHUB_BASE_REF\.\.\.HEAD/);
  assert.match(script, /HEAD\^\.\.HEAD/);
  assert.doesNotMatch(script, /hash-object -t tree/);
});
