import assert from 'assert';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { buildWorkspaceFingerprint } from './src/tools/autotest-pipeline.js';

const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'vlcode-fingerprint-'));
const workDir = path.join(tmpRoot, 'Project');
fs.mkdirSync(path.join(workDir, 'Apps'), { recursive: true });
fs.mkdirSync(path.join(workDir, '.vl-code'), { recursive: true });

const appFile = path.join(workDir, 'Apps', 'AppMain.vl');
const metaFile = path.join(workDir, '.vl-code', 'ProjectMeta.json');
const runFile = path.join(workDir, '.vl-code', 'autotest-results.json');

fs.writeFileSync(appFile, 'App AppMain {}');
fs.writeFileSync(metaFile, '{"apps":["AppMain"]}');
fs.writeFileSync(runFile, '{"runResults":[]}');

const projectContext = {
  getAllFiles() {
    return [
      { path: 'Apps/AppMain.vl', fullPath: appFile, isVL: true },
      { path: '.vl-code/ProjectMeta.json', fullPath: metaFile, isVL: false },
      { path: '.vl-code/autotest-results.json', fullPath: runFile, isVL: false },
    ];
  },
};

const before = buildWorkspaceFingerprint(projectContext);
fs.writeFileSync(runFile, '{"runResults":[{"id":"case-1"}]}');
const afterVolatile = buildWorkspaceFingerprint(projectContext);
assert.strictEqual(afterVolatile, before, 'volatile autotest outputs should not affect fingerprint');

fs.writeFileSync(appFile, 'App AppMain { Text "changed" }');
const afterSourceChange = buildWorkspaceFingerprint(projectContext);
assert.notStrictEqual(afterSourceChange, before, 'source changes must affect fingerprint');

console.log('PASS test-autotest-workspace-fingerprint');
