// tests/lib/profile-stats-fields.test.mjs
// SC #11 contract-test per brief design — kombinasjonen av:
//   (a) fixture-records valideres som JSONL-contracts AND
//   (b) command-prose contains field-names
// er den brief-designede gating-mekanismen. Faktisk runtime-emission av
// feltene er LLM-prose-driven og ikke testbart i node:test alene.

import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import { readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const REPO_ROOT = join(__dirname, '..', '..');

const PROFILE_FIELDS = [
  'profile',
  'phase_models',
  'parallel_agents',
  'external_research_enabled',
  'profile_source',
];

const VALID_PROFILE_SOURCES = new Set(['flag', 'env', 'default', 'inheritance']);

const COMMAND_FILES = [
  'trekbrief.md',
  'trekresearch.md',
  'trekplan.md',
  'trekexecute.md',
  'trekreview.md',
  'trekcontinue.md',
];

// (a) Fixture validates as JSONL contracts

test('SC #11(a): tests/fixtures/stats-with-profile.jsonl parses as JSONL', () => {
  const text = readFileSync(join(REPO_ROOT, 'tests', 'fixtures', 'stats-with-profile.jsonl'), 'utf-8');
  const lines = text.trim().split('\n').filter(Boolean);
  assert.equal(lines.length, 5, `expected 5 simulated stats records, got ${lines.length}`);
  for (const line of lines) {
    const record = JSON.parse(line); // throws if malformed
    assert.equal(typeof record, 'object');
    assert.ok(record.ts, 'record missing ts');
  }
});

test('SC #11(a): every fixture record contains profile + profile_source', () => {
  const text = readFileSync(join(REPO_ROOT, 'tests', 'fixtures', 'stats-with-profile.jsonl'), 'utf-8');
  const records = text.trim().split('\n').filter(Boolean).map(l => JSON.parse(l));
  for (const r of records) {
    assert.ok('profile' in r, `record missing profile: ${JSON.stringify(r)}`);
    assert.ok('profile_source' in r, `record missing profile_source: ${JSON.stringify(r)}`);
  }
});

test('SC #11(a): profile_source values are in {flag, env, default, inheritance}', () => {
  const text = readFileSync(join(REPO_ROOT, 'tests', 'fixtures', 'stats-with-profile.jsonl'), 'utf-8');
  const records = text.trim().split('\n').filter(Boolean).map(l => JSON.parse(l));
  for (const r of records) {
    assert.ok(VALID_PROFILE_SOURCES.has(r.profile_source),
      `profile_source "${r.profile_source}" not in valid set`);
  }
});

test('SC #11(a): fixture coverage — all 4 profile_source values represented', () => {
  const text = readFileSync(join(REPO_ROOT, 'tests', 'fixtures', 'stats-with-profile.jsonl'), 'utf-8');
  const records = text.trim().split('\n').filter(Boolean).map(l => JSON.parse(l));
  const seen = new Set(records.map(r => r.profile_source));
  for (const expected of VALID_PROFILE_SOURCES) {
    assert.ok(seen.has(expected),
      `fixture missing profile_source value: ${expected}; seen: ${[...seen].join(', ')}`);
  }
});

// (b) Command prose contains field-names (false-confidence kompensasjon per plan-critic Major 4)

for (const filename of COMMAND_FILES) {
  test(`SC #11(b): commands/${filename} prose mentions profile + profile_source`, () => {
    const content = readFileSync(join(REPO_ROOT, 'commands', filename), 'utf-8');
    assert.match(content, /profile_source/,
      `${filename} prose missing profile_source — Step 8 stats schema additive must be documented`);
    assert.match(content, /profile/,
      `${filename} prose missing profile — Step 8 stats schema additive must be documented`);
  });
}

test('SC #11(b): commands/trekplan.md prose mentions phase_models + parallel_agents', () => {
  const content = readFileSync(join(REPO_ROOT, 'commands', 'trekplan.md'), 'utf-8');
  assert.match(content, /phase_models/,
    'trekplan.md prose must mention phase_models (additive stats field)');
  assert.match(content, /parallel_agents/,
    'trekplan.md prose must mention parallel_agents (additive stats field)');
});

test('SC #11(b): commands/trekresearch.md prose mentions external_research_enabled', () => {
  const content = readFileSync(join(REPO_ROOT, 'commands', 'trekresearch.md'), 'utf-8');
  assert.match(content, /external_research_enabled/,
    'trekresearch.md prose must mention external_research_enabled (additive stats field)');
});
