Files
cli/tests/generator.test.ts
Mauricio Siu c073a037b1 feat: integrate Vitest for testing and update workflows
- Added Vitest as a testing framework in package.json and configured test scripts.
- Created new test files for CLI and client functionalities, ensuring comprehensive coverage.
- Updated GitHub Actions workflows to include testing steps and streamlined release process.
- Removed the deprecated onRelease workflow to consolidate CI/CD processes.
2026-04-15 21:00:48 -06:00

64 lines
2.1 KiB
TypeScript

import * as fs from "node:fs";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(__dirname, "..");
describe("generator", () => {
const specPath = path.join(ROOT, "openapi.json");
const generatedPath = path.join(ROOT, "src", "generated", "commands.ts");
it("openapi.json should exist", () => {
expect(fs.existsSync(specPath)).toBe(true);
});
it("generated commands file should exist", () => {
expect(fs.existsSync(generatedPath)).toBe(true);
});
it("generated file should export registerGeneratedCommands", () => {
const content = fs.readFileSync(generatedPath, "utf8");
expect(content).toContain("export function registerGeneratedCommands");
});
it("generated file should import from client", () => {
const content = fs.readFileSync(generatedPath, "utf8");
expect(content).toContain('from "../client.js"');
});
it("generated file should contain command groups from the spec", () => {
const spec = JSON.parse(fs.readFileSync(specPath, "utf8"));
const content = fs.readFileSync(generatedPath, "utf8");
// Only check groups that have a dot-separated action (group.action)
const groups = new Set<string>();
for (const p of Object.keys(spec.paths)) {
const clean = p.replace(/^\//, "");
const [group, ...rest] = clean.split(".");
if (group && rest.length > 0) groups.add(group);
}
for (const group of groups) {
expect(content).toContain(`"${group}.`);
}
});
it("number of apiPost/apiGet calls should match valid endpoints", () => {
const spec = JSON.parse(fs.readFileSync(specPath, "utf8"));
const content = fs.readFileSync(generatedPath, "utf8");
// Count only endpoints with group.action pattern
let validEndpoints = 0;
for (const p of Object.keys(spec.paths)) {
const clean = p.replace(/^\//, "");
const [group, ...rest] = clean.split(".");
if (group && rest.length > 0) validEndpoints++;
}
const apiCalls = (content.match(/await api(Post|Get)\(/g) || []).length;
expect(apiCalls).toBe(validEndpoints);
});
});