/** * 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(); } } }); }, ); });