Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sarangan12 committed Oct 25, 2024
1 parent 057d0c6 commit 1b94f0b
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion packages/spector/src/actions/server-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import * as path from "path";
import { logger } from "../logger.js";
import { loadScenarioMockApis } from "../scenarios-resolver.js";
import { makeServiceCall, uint8ArrayToString } from "./helper.js";
import pc from "picocolors";

const DEFAULT_BASE_URL = "http://localhost:3000";

export interface ServerTestDiagnostics {
scenario_name: string;
status: "success" | "failure";
message: any;
};

class ServerTestsGenerator {
private name: string = "";
private mockApiDefinition: MockApiDefinition;
Expand Down Expand Up @@ -184,15 +191,49 @@ export async function serverTest(scenariosPath: string, options: ServerTestOptio
}
// 2. Load all the scenarios
const scenarios = await loadScenarioMockApis(scenariosPath);
const success_diagnostics: ServerTestDiagnostics[] = [];
const failure_diagnostics: ServerTestDiagnostics[] = [];

// 3. Execute each scenario
for (const [name, scenario] of Object.entries(scenarios)) {
if (!Array.isArray(scenario.apis)) continue;
for (const api of scenario.apis) {
if (api.kind !== "MockApiDefinition") continue;
if (testCasesToRun.length === 0 || testCasesToRun.includes(name)) {
const obj: ServerTestsGenerator = new ServerTestsGenerator(name, api, baseUrl);
await obj.executeScenario();
try {
await obj.executeScenario();
success_diagnostics.push({
scenario_name: name,
status: "success",
message: "executed successfully",
});
} catch (e: any) {
failure_diagnostics.push({
scenario_name: name,
status: "failure",
message: `code = ${e.code} \n message = ${e.message} \n name = ${e.name} \n stack = ${e.stack} \n status = ${e.status}`,
});
}
}
}
}

// 4. Print diagnostics
logger.info("Server Tests Diagnostics Summary");

if (success_diagnostics.length > 0) logger.info("Success Scenarios");
success_diagnostics.forEach((diagnostic) => {
logger.info(`${pc.green("✓")} Scenario: ${diagnostic.scenario_name} - ${diagnostic.message}`);
});

if (failure_diagnostics.length > 0) logger.error("Failure Scenarios");
if (failure_diagnostics.length > 0) {
logger.error("Failed Scenario details");
failure_diagnostics.forEach((diagnostic) => {
logger.error(`${pc.red("✘")} Scenario: ${diagnostic.scenario_name}`);
logger.error(`${diagnostic.message}`);
});
process.exit(-1);
}
}

0 comments on commit 1b94f0b

Please sign in to comment.