Files
liqa/server/test/examples.spec.ts
ars9 c1924060eb
CI / changes (pull_request) Successful in 7s
CI / client (pull_request) Skipped
CI / server (pull_request) Successful in 1m7s
CI / image (pull_request) Skipped
feat(examples): add self-test example
- add examples/ with a ready-to-import self-test: environment, credential,
  snippets and a scenario that creates, runs and checks another scenario
  through the liqa REST API and UI, then cleans up
- add test/examples.spec.ts: imports every example and parses its code
- link the examples from README, docs/scenario.md and CLAUDE.md

Closes #6
2026-10-02 15:39:02 +03:00

84 lines
2.4 KiB
TypeScript

/**
* Keeps the files in `examples/` importable: every YAML file there must pass
* `POST /import`, and all its step and snippet code must parse.
*/
import * as fs from "node:fs";
import * as path from "node:path";
import { INestApplication } from "@nestjs/common";
import request from "supertest";
import { buildTestApp } from "./app.harness";
import { ImportService } from "../src/import/import.service";
const EXAMPLES_DIR = path.resolve(__dirname, "../../examples");
function findYamlFiles(dir: string): string[] {
return fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) return findYamlFiles(full);
return /\.ya?ml$/.test(entry.name) ? [full] : [];
});
}
const AsyncFunction = Object.getPrototypeOf(async () => {})
.constructor as new (...args: string[]) => unknown;
interface ExampleItem {
kind: string;
code?: string;
steps?: { title?: string; execCode?: string }[];
}
describe("Examples", () => {
let app: INestApplication;
let importService: ImportService;
beforeAll(async () => {
app = await buildTestApp();
importService = app.get(ImportService);
});
afterAll(async () => {
await app.close();
});
const files = findYamlFiles(EXAMPLES_DIR);
it("has example files", () => {
expect(files.length).toBeGreaterThan(0);
});
describe.each(files.map((f) => [path.relative(EXAMPLES_DIR, f), f]))(
"%s",
(_name, file) => {
const yaml = fs.readFileSync(file, "utf8");
it("imports", async () => {
const res = await request(app.getHttpServer())
.post("/import")
.set("Content-Type", "application/yaml")
.send(yaml)
.expect(201);
expect(res.body.imported.length).toBeGreaterThan(0);
});
it("has code that parses", () => {
const items = importService.parseYaml(yaml) as ExampleItem[];
for (const item of items) {
// Same shape as the executor: snippets get `...args`
if (item.code != null) {
expect(
() => new AsyncFunction("context", "...args", item.code!),
).not.toThrow();
}
for (const step of item.steps ?? []) {
if (step.execCode == null) continue;
expect(
() => new AsyncFunction("context", step.execCode!),
).not.toThrow();
}
}
});
},
);
});