repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ultra/__tests__/skill.test.ts

Raw
File outline
import * as fs from "node:fs";
import { fileURLToPath } from "node:url";
import { loadSkillsFromDir } from "@earendil-works/pi-coding-agent";
import { describe, expect, it } from "vitest";
import { parseWorkflow } from "../spec.ts";

// Root `package.json` registers ultra's skills under `pi.skills` with the
// explicit entry `extensions/ultra/skills` (alongside other skill-bundling
// extensions such as `super`). These tests scope to ultra's own `skills/`
// subtree and load it with the real SDK discovery primitive, so they track
// production behaviour for *this* extension regardless of what others bundle.
const ULTRA_SKILLS_DIR = fileURLToPath(new URL("../skills", import.meta.url));
const SKILL_PATH = fileURLToPath(
	new URL("../skills/authoring-workflows/SKILL.md", import.meta.url),
);
const SPEC_FORMAT_PATH = fileURLToPath(
	new URL(
		"../skills/authoring-workflows/references/spec-format.md",
		import.meta.url,
	),
);

describe("authoring-workflows companion skill", () => {
	it("bundles exactly one skill subdir — authoring-workflows", () => {
		// Ultra ships a single skill; this guards against an accidental empty or
		// stray skill dir sneaking into the discovered surface as ultra grows.
		const dirs = fs
			.readdirSync(ULTRA_SKILLS_DIR, { withFileTypes: true })
			.filter((e) => e.isDirectory())
			.map((e) => e.name)
			.sort();
		expect(dirs).toEqual(["authoring-workflows"]);
	});

	it("discovers exactly the authoring-workflows skill from ultra's skills dir — no diagnostics", () => {
		const { skills, diagnostics } = loadSkillsFromDir({
			dir: ULTRA_SKILLS_DIR,
			source: "extensions/ultra/skills",
		});

		expect(skills.map((s) => s.name)).toEqual(["authoring-workflows"]);
		// Valid frontmatter + a clean subtree ⇒ the loader emits nothing.
		expect(diagnostics).toEqual([]);

		const skill = skills[0];
		expect(skill.description.trim().length).toBeGreaterThan(0);
		expect(skill.description.length).toBeLessThanOrEqual(1024);
		// The description is the model's trigger surface — it must mention what it
		// routes on so "author / create / save a workflow" intent lands here.
		expect(skill.description.toLowerCase()).toContain("workflow");
		// Model-invocable (no `disable-model-invocation`), resolving to the real file.
		expect(skill.disableModelInvocation).toBe(false);
		expect(skill.filePath).toBe(SKILL_PATH);
		expect(fs.existsSync(skill.filePath)).toBe(true);
	});

	it("keeps the reference doc and the validator in lockstep: every complete JSON example parses", () => {
		// Extract every ```json fenced block from the canonical reference and run
		// each *complete* spec (one with both `name` and `phases`) through the
		// SAME validator the runtime uses. If an author copies an example verbatim
		// it must validate — and if someone edits the doc into an invalid shape,
		// this fails loudly. Fragments (lone phases/schemas) are checked only for
		// being valid JSON.
		const md = fs.readFileSync(SPEC_FORMAT_PATH, "utf8");
		const blocks = [...md.matchAll(/```json\n([\s\S]*?)```/g)].map((m) => m[1]);
		expect(blocks.length).toBeGreaterThan(0);

		let completeSpecs = 0;
		for (const block of blocks) {
			let parsed: unknown;
			expect(() => {
				parsed = JSON.parse(block);
			}).not.toThrow();

			const isCompleteSpec =
				typeof parsed === "object" &&
				parsed !== null &&
				!Array.isArray(parsed) &&
				"name" in parsed &&
				"phases" in parsed;
			if (isCompleteSpec) {
				completeSpecs += 1;
				expect(() => parseWorkflow(parsed)).not.toThrow();
			}
		}
		// The doc must carry at least one full worked example, not only fragments.
		expect(completeSpecs).toBeGreaterThanOrEqual(1);
	});
});