import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, readFile, rm, stat } from 'node:fs/promises';
import { spawnSync } from 'node:child_process';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

test('training ingest strips metadata, derives a pseudonymous split, and writes reviewable labels', async () => {
  const out = await mkdtemp(join(tmpdir(), 'timmy-ingest-'));
  try {
    const run = spawnSync('python3', [
      'scripts/ingest_training_photo.py',
      '--input', 'tests/fixtures/synthetic-type4.jpg',
      '--output-dir', out,
      '--subject-id', 'subject-123',
      '--consent-version', 'research-v1',
      '--bristol-type', '4',
      '--color', 'brown',
      '--quality', 'good',
      '--reviewer', 'user-confirmed',
    ], { encoding: 'utf8', env: { ...process.env, TIMMY_DATASET_SALT: 'test-only-salt' } });
    assert.equal(run.status, 0, run.stderr);
    const record = JSON.parse(run.stdout);
    assert.match(record.imageId, /^[a-f0-9]{64}$/);
    assert.ok(['train', 'validation', 'test'].includes(record.split));
    assert.equal(record.subjectKey.length, 16);
    assert.equal(record.consentVersion, 'research-v1');
    assert.deepEqual(record.labels, { bristolType: 4, color: 'brown', quality: 'good' });
    assert.equal(record.review.status, 'user-confirmed');
    assert.equal('inputPath' in record, false);
    const derived = join(out, record.relativePath);
    assert.ok((await stat(derived)).size > 100);
    const manifest = (await readFile(join(out, 'manifest.jsonl'), 'utf8')).trim().split('\n').map(JSON.parse);
    assert.deepEqual(manifest, [record]);
    const exif = spawnSync('python3', ['-c', "from PIL import Image;import sys;print(len(Image.open(sys.argv[1]).getexif()))", derived], { encoding: 'utf8' });
    assert.equal(exif.stdout.trim(), '0');
  } finally {
    await rm(out, { recursive: true, force: true });
  }
});

test('training ingest rejects records without explicit consent or human review', () => {
  const run = spawnSync('python3', [
    'scripts/ingest_training_photo.py',
    '--input', 'tests/fixtures/synthetic-type4.jpg',
    '--output-dir', '/tmp/should-not-exist-timmy',
    '--subject-id', 'subject-123',
    '--bristol-type', '4',
    '--color', 'brown',
    '--quality', 'good',
  ], { encoding: 'utf8', env: { ...process.env, TIMMY_DATASET_SALT: 'test-only-salt' } });
  assert.notEqual(run.status, 0);
  assert.match(run.stderr, /consent|reviewer/i);
});
