import fs from 'fs/promises';
import os from 'os';
import path from 'path';

import { WorkflowExecutor } from './src/vl/workflow-executor.js';

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

console.log('\n── Workflow File Adapter ──');

const workDir = await fs.mkdtemp(path.join(os.tmpdir(), 'vlcode-file-adapter-'));
const executor = new WorkflowExecutor({ workDir, vlVersion: '3.5' });
const file = executor._buildFileAdapter();

await file.write('/Sections/TestSection.sc', '\n\n// VL_VERSION:3.6\n<Section-TestSection>\n', 'overwrite');
const sectionContent = await fs.readFile(path.join(workDir, 'Sections', 'TestSection.sc'), 'utf-8');
assert(sectionContent.startsWith('// VL_VERSION:3.5\n'), 'section file should start with normalized VL_VERSION header');

await file.write('/Services/TestService.vs', 'SERVICE Ping();RETURN STRING\n-RETURN "ok"\n', 'overwrite');
const serviceContent = await fs.readFile(path.join(workDir, 'Services', 'TestService.vs'), 'utf-8');
assert(serviceContent.startsWith('// VL_VERSION:3.5\n'), 'service file should get a missing VL_VERSION header');

console.log('PASS test-workflow-file-adapter.js');
