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

import { buildCompileRequest } from './src/server/routes/compile.js';

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

console.log('\n── Compile Request Shape ──');

const workDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vlcode-compile-route-'));
fs.mkdirSync(path.join(workDir, 'Apps'), { recursive: true });
fs.writeFileSync(path.join(workDir, 'Apps', 'Demo.vx'), '// VL_VERSION:3.5\n<App-Demo>\n</App-Demo>\n', 'utf-8');

const { parseUrl, body } = buildCompileRequest(workDir, {
  targetGid: 1653,
  action: 'parsePjt',
  download: true,
});

assert(parseUrl.endsWith('/edtfn/parsevl'), 'compile should target ZIP upload endpoint');
assert(body.targetGid === 1653, 'compile request should preserve target gid');
assert(typeof body.file === 'string' && body.file.startsWith('data:application/zip;base64,'), 'compile request should package local files as ZIP data URL');
assert(body.action === 'parsePjt', 'compile request should keep the action');

console.log('PASS test-compile-request-shape.js');
