#!/usr/bin/env node
import assert from 'assert';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { createAutoTestPipelineTool } from './src/tools/autotest-pipeline.js';

const workDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vlcode-autotest-state-'));
const vlDir = path.join(workDir, '.vl-code');
fs.mkdirSync(vlDir, { recursive: true });

fs.writeFileSync(path.join(vlDir, 'autotest-cases.json'), JSON.stringify({
  testPlan: { pages: [{ path: '/TaskList' }] },
  testCases: [
    {
      id: 'WF_001',
      name: 'Smoke',
      priority: 'P0',
      page: '/TaskList',
      steps: [{ action: 'navigate', selector: 'https://example.test/app' }],
    },
  ],
}, null, 2));

fs.writeFileSync(path.join(vlDir, 'autotest-results.json'), JSON.stringify({
  runResults: [
    {
      caseId: 'WF_001',
      pass: false,
      status: 'soft_pass',
      steps: [{ index: 0, action: 'navigate', selector: 'https://example.test/app', status: 'soft_pass' }],
    },
  ],
  evaluations: [
    {
      caseId: 'WF_001',
      name: 'Smoke',
      evaluation: {
        pass: false,
        softPass: true,
        reason: 'Soft pass: 1 steps skipped (selectors not found in DOM)',
        evidence: '⚠️ Step 1 navigate "https://example.test/app": soft-pass (not found)',
      },
    },
  ],
  timestamp: '2026-03-14T15:40:00.000Z',
}, null, 2));

const tool = createAutoTestPipelineTool(
  {
    workDir,
    llmProvider: 'cli',
    cliAvailable: true,
    model: 'claude-opus-4-6',
  },
  {
    getAllFiles() { return []; },
  }
);

const status = await tool.execute({ action: 'status' });
assert.equal(status.testCases, 1, 'status should hydrate persisted test cases');
assert.equal(status.runResults, 1, 'status should hydrate persisted run results');
assert.equal(status.passed, 0);
assert.equal(status.softPassed, 1);
assert.equal(status.failed, 0);
assert.match(status.result, /1 test cases, 1 results/);

const report = await tool.execute({ action: 'report' });
assert.match(report.result, /Soft-Passed/);
assert.match(report.result, /WF_001/);

fs.rmSync(workDir, { recursive: true, force: true });

console.log('\n── AutoTest State Hydration ──');
console.log('PASS test-autotest-state-hydration.js');
