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("requires deliberate per-step model tiers and thinking levels", () => {
		const skill = fs.readFileSync(SKILL_PATH, "utf8");
		const reference = fs.readFileSync(SPEC_FORMAT_PATH, "utf8");

		expect(skill).toContain("Assign compute deliberately");
		expect(skill).toContain("Explicitly set both fields");
		for (const tier of ["`small`", "`medium`", "`big`"]) {
			expect(skill).toContain(tier);
			expect(reference).toContain(tier);
		}
		expect(reference).toContain("relative to that step's difficulty");
		expect(reference).toContain("re-evaluate each assignment");
	});

	it("matches runtime positional and structured-output semantics", () => {
		const skill = fs.readFileSync(SKILL_PATH, "utf8");
		const reference = fs.readFileSync(SPEC_FORMAT_PATH, "utf8");

		expect(reference).toContain(
			"last declared parameter absorbs all remaining tokens",
		);
		expect(reference).not.toContain("extra tokens ignored");
		expect(reference).toContain("Every step returns a JSON object");
		expect(reference).toContain("`schema` validates");
		expect(skill).toContain(
			"how tightly its structured JSON output must be constrained by a schema",
		);
	});

	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;
				const spec = parseWorkflow(parsed);
				for (const phase of spec.phases) {
					expect(phase.step.model).toMatch(/^(small|medium|big)$/);
					expect(phase.step.thinkingLevel).toMatch(
						/^(off|minimal|low|medium|high|xhigh)$/,
					);
				}
			}
		}
		// The doc must carry at least one full worked example, not only fragments.
		expect(completeSpecs).toBeGreaterThanOrEqual(1);
	});
});