All files / server/routes test-reset.routes.ts

69.23% Statements 9/13
75% Branches 6/8
100% Functions 2/2
66.66% Lines 8/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                                                        5x   1x 1x 1x 1x                     1x   1x     1x                        
import type { Express } from "express";
 
interface TestResetApi {
  stopAllRunnersAndNotify: () => Promise<{
    cleanedUpCount: number;
    cleanedTestRunIds: string[];
  }>;
}
 
interface TestResetLogger {
  info(message: string): void;
  warn(message: string): void;
  error(message: string): void;
}
 
interface TestResetRouteOptions {
  isTest: boolean;
  enabled: boolean;
  getSimulationApi: () => TestResetApi | null;
  logger: TestResetLogger;
}
 
export function registerTestResetRoute(
  app: Express,
  options: TestResetRouteOptions,
): void {
  // Requiring both conditions prevents a lone production environment flag
  // from exposing a destructive, process-global endpoint.
  if (!options.isTest || !options.enabled) return;
 
  app.post("/api/test-reset", async (_req, res) => {
    try {
      const simulationApi = options.getSimulationApi();
      Iif (!simulationApi) {
        options.logger.warn("/api/test-reset called before WS module initialized");
        return res.json({
          status: "reset",
          message: "No active runners",
          cleanedTestRunIds: [],
          timestamp: new Date().toISOString(),
        });
      }
 
      const { cleanedUpCount, cleanedTestRunIds } =
        await simulationApi.stopAllRunnersAndNotify();
 
      options.logger.info(
        `[Test Reset] Cleaned up ${cleanedUpCount} client runner(s). TestRunIds: ${cleanedTestRunIds.join(", ") || "none"}`,
      );
      res.json({
        status: "reset",
        message: `Backend reset complete. Cleaned up ${cleanedUpCount} runner(s).`,
        cleanedTestRunIds,
        timestamp: new Date().toISOString(),
      });
    } catch (error) {
      options.logger.error(`[Test Reset] Error during reset: ${error}`);
      res.status(500).json({ error: "Reset failed", message: String(error) });
    }
  });
}