- add RunEventsService and emit run.started / run.finished from the scheduler - stream run events over SSE at GET /api/v1/scenarios/runs/events with a heartbeat - add useRunNotifications hook and NotificationToggle (opt-in bell in the side panel footer) - refresh runs lists when a run finishes - document the events stream and notifications Closes #12
110 lines
3.5 KiB
TypeScript
110 lines
3.5 KiB
TypeScript
import { INestApplication } from "@nestjs/common";
|
|
import { DataSource } from "typeorm";
|
|
import { buildTestApp } from "./app.harness";
|
|
import { EnvironmentEntity } from "../src/environment/environment.entity";
|
|
import {
|
|
RunEvent,
|
|
RunEventsService,
|
|
} from "../src/scenario/run-events.service";
|
|
import { ScenarioController } from "../src/scenario/scenario.controller";
|
|
import { ScenarioSchedulerService } from "../src/scenario/scenario-scheduler.service";
|
|
import { ScenarioService } from "../src/scenario/scenario.service";
|
|
|
|
describe("run events", () => {
|
|
let app: INestApplication;
|
|
let dataSource: DataSource;
|
|
let scenarioService: ScenarioService;
|
|
let scheduler: ScenarioSchedulerService;
|
|
let events: RunEvent[];
|
|
let subscription: { unsubscribe(): void };
|
|
|
|
beforeAll(async () => {
|
|
app = await buildTestApp();
|
|
dataSource = app.get(DataSource);
|
|
scenarioService = app.get(ScenarioService);
|
|
scheduler = app.get(ScenarioSchedulerService);
|
|
});
|
|
|
|
beforeEach(() => {
|
|
events = [];
|
|
subscription = app
|
|
.get(RunEventsService)
|
|
.stream()
|
|
.subscribe((e) => events.push(e));
|
|
});
|
|
|
|
afterEach(() => subscription.unsubscribe());
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
async function runScenario(name: string, codes: string[]) {
|
|
const scenario = await scenarioService.create({ name });
|
|
for (const execCode of codes) {
|
|
await scenarioService.createStep(scenario.id, { execCode });
|
|
}
|
|
const repo = dataSource.getRepository(EnvironmentEntity);
|
|
const env = await repo.save(
|
|
repo.create({ name: `env-${Math.random()}`, data: {} }),
|
|
);
|
|
const run = await scenarioService.createRun(scenario.id, env.id);
|
|
await scheduler.pickUpPendingRuns();
|
|
await scenarioService.waitForRun(scenario.id, run.id, 10_000);
|
|
// run.finished is emitted just after the status becomes terminal
|
|
await waitFor(() => events.some((e) => e.type === "run.finished"));
|
|
// give a double emit the chance to show up
|
|
await new Promise((r) => setTimeout(r, 200));
|
|
return { scenario, run };
|
|
}
|
|
|
|
async function waitFor(cond: () => boolean) {
|
|
for (let i = 0; i < 100 && !cond(); i++) {
|
|
await new Promise((r) => setTimeout(r, 50));
|
|
}
|
|
}
|
|
|
|
it("emits started then finished(pass) for a passing run", async () => {
|
|
const { scenario, run } = await runScenario("events-pass", ["return 1;"]);
|
|
|
|
expect(events.map((e) => [e.type, e.status])).toEqual([
|
|
["run.started", "in_progress"],
|
|
["run.finished", "pass"],
|
|
]);
|
|
expect(events[0]).toMatchObject({
|
|
runId: run.id,
|
|
scenarioId: scenario.id,
|
|
scenarioName: "events-pass",
|
|
});
|
|
expect(Number.isNaN(Date.parse(events[1].at))).toBe(false);
|
|
});
|
|
|
|
it("emits finished(fail) exactly once for a failing run", async () => {
|
|
await runScenario("events-fail", ["throw new Error('boom');", "return 2;"]);
|
|
|
|
expect(events.map((e) => [e.type, e.status])).toEqual([
|
|
["run.started", "in_progress"],
|
|
["run.finished", "fail"],
|
|
]);
|
|
});
|
|
|
|
it("exposes events through the SSE controller stream", () => {
|
|
const received: { type: string; data: object }[] = [];
|
|
const sub = app
|
|
.get(ScenarioController)
|
|
.streamRunEvents()
|
|
.subscribe((m) => received.push(m));
|
|
app.get(RunEventsService).emit({
|
|
type: "run.started",
|
|
runId: "r",
|
|
scenarioId: "s",
|
|
scenarioName: "n",
|
|
status: "in_progress",
|
|
at: new Date().toISOString(),
|
|
});
|
|
sub.unsubscribe();
|
|
expect(received).toHaveLength(1);
|
|
expect(received[0].type).toBe("run.started");
|
|
});
|
|
});
|