const Mocha = require("mocha");
const { execSync } = require("child_process");
const fs = require("fs");

async function runTestFile(filePath, description) {
  console.log(`Running ${description}...`);

  const mocha = new Mocha({
    reporter: "spec",
    timeout: 5000,
  });

  mocha.addFile(filePath);

  return new Promise((resolve, reject) => {
    mocha.run((failures) => {
      if (failures) {
        reject(new Error(`${description} failed with ${failures} failures`));
      } else {
        resolve();
      }
    });
  });
}

async function runTests() {
  try {
    await runTestFile("./test/env.test.js", "environment validation tests");
    console.log("✓ Environment validation passed. Running route tests...");

    await runTestFile("./test/routes.test.js", "route tests");
    console.log("✓ All tests passed!");

    const commitHash = execSync("git rev-parse HEAD").toString().trim();
    fs.writeFileSync(".last_tested_commit", commitHash + "\n");
  } catch (error) {
    console.error("Test execution failed:", error.message);
    process.exit(1);
  }
}

runTests();
