#!/usr/bin/env tsx

/**
 * Script to verify S1 pack tests execution time
 *
 * This script runs the S1 pack tests (Git parsing and Path operations)
 * and verifies they complete within the 2-second requirement.
 */

import { spawn } from "node:child_process";

process.stdout.write("🔍 Verifying S1 Pack Tests Execution Time...");
process.stdout.write("=============================================\n");

const startTime = Date.now();

const vitest = spawn(
	"npx",
	[
		"vitest",
		"run",
		"test/unit/git.unit.test.ts",
		"test/unit/path.unit.test.ts",
		"--reporter=verbose",
	],
	{
		cwd: "/Users/user1/WebstormProjects/vreko-site/apps/vscode",
		stdio: "pipe",
	},
);

let _output = "";

vitest.stdout.on("data", (data) => {
	_output += data.toString();
	process.stdout.write(data);
});

vitest.stderr.on("data", (data) => {
	_output += data.toString();
	process.stderr.write(data);
});

vitest.on("close", (code) => {
	const endTime = Date.now();
	const duration = endTime - startTime;

	process.stdout.write("\n=============================================");
	process.stdout.write(`⏱️  Test Execution Time: ${duration}ms`);
	process.stdout.write(`🎯 Requirement: < 2000ms`);

	if (duration < 2000) {
		process.stdout.write("✅ S1 Pack Tests PASSED time requirement!");
		process.stdout.write(`   Time saved: ${2000 - duration}ms`);
	} else {
		process.stdout.write("❌ S1 Pack Tests FAILED time requirement!");
		process.stdout.write(`   Time exceeded by: ${duration - 2000}ms`);
	}

	process.stdout.write("=============================================\n");

	if (code === 0) {
		process.stdout.write("✅ All tests passed!");
		process.exit(0);
	} else {
		process.stdout.write("❌ Some tests failed!");
		process.exit(1);
	}
});
