import { NestFactory } from '@nestjs/core';
import { AppModule } from '../../app.module';
import { SmokeTestService } from '../../domains/operations/smoke-tests/smoke-test.service';

async function main() {
  const app = await NestFactory.createApplicationContext(AppModule, {
    logger: ['error', 'warn'],
  });
  try {
    const smoke = app.get(SmokeTestService);
    smoke.assertAllowed();
    const summary = await smoke.run();
    console.log(
      `Smoke: pass=${summary.passed} fail=${summary.failed} skip=${summary.skipped} total=${summary.total} (${summary.durationMs}ms)`,
    );
    if (summary.failed > 0) {
      for (const r of summary.results.filter((x) => x.status === 'FAIL')) {
        console.error(`FAIL ${r.id} ${r.name}: ${r.error ?? r.message ?? ''}`);
      }
      process.exit(1);
    }
  } finally {
    await app.close();
  }
}

main().catch((err) => {
  console.error(err);
  process.exit(1);
});
