import fs from 'fs';
import path from 'path';

function assert(condition, message) {
  if (!condition) throw new Error(message);
}

console.log('\n── Compile-Fix Workflow Shape ──');

const workflowPath = path.join(process.cwd(), '.vl-code', 'workflows', 'compile-fix.json');
const workflow = JSON.parse(fs.readFileSync(workflowPath, 'utf-8'));

assert(workflow.version === '3.16', 'compile-fix workflow should stay on spec v3.16');
assert(workflow.registry.files.inputs.includes('.vl-code/last-compile.json'), 'compile-fix workflow should depend on the latest compile report');

const stepIds = workflow.steps.map(step => step.id);
assert(stepIds.includes('LLM_010_LoadCompileReport'), 'compile-fix workflow should load the persisted compile report first');
assert(stepIds.includes('LLM_020_PlanFixes'), 'compile-fix workflow should plan file-level fixes');
assert(stepIds.includes('LLM_031_ApplyFix'), 'compile-fix workflow should rewrite the affected files');
assert(!stepIds.includes('LLM_010_Compile'), 'compile-fix workflow should not pretend to compile internally');
assert(!stepIds.includes('LLM_040_Recompile'), 'compile-fix workflow should leave recompilation to the real compile tool');

const applyFix = workflow.steps.find(step => step.id === 'LLM_031_ApplyFix');
assert(Array.isArray(applyFix?.in?.readFiles) && applyFix.in.readFiles[0] === '=_item.file', 'file fix step should read the current file before rewriting it');
assert(applyFix.out && Object.keys(applyFix.out).includes('/{_item.file}'), 'file fix step should write back to the affected file path');

console.log('PASS test-compile-fix-workflow.js');
