Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/good-job/__tests__/core.test.ts

Raw
import { describe, expect, it } from "vitest";
import {
	analysisPrompt,
	feedbackId,
	feedbackIdFromSeed,
	feedbackTrigger,
	parseLearning,
} from "../core.js";

describe("good-job core", () => {
	it("matches praise and problem prefixes with optional details", () => {
		expect(feedbackTrigger("gj")).toEqual({
			kind: "praise",
			feedback: "gj",
		});
		expect(feedbackTrigger("good job! excellent scope control")).toEqual({
			kind: "praise",
			feedback: "excellent scope control",
		});
		expect(feedbackTrigger("wtf you deleted my db")).toEqual({
			kind: "problem",
			feedback: "you deleted my db",
		});
		for (const other of ["great job", "object.gj", "/gj list", "", "awtf"])
			expect(feedbackTrigger(other), other).toBeUndefined();
	});

	it("creates short kind-prefixed Crockford IDs", () => {
		const bytes = Uint8Array.from([0, 1, 2, 3, 4, 5, 6]);
		expect(feedbackId("praise", bytes)).toMatch(/^gj-[0-9a-hjkmnp-tv-z]{10}$/);
		expect(feedbackId("problem", bytes)).toMatch(
			/^wtf-[0-9a-hjkmnp-tv-z]{10}$/,
		);
		expect(feedbackIdFromSeed("problem", "same")).toBe(
			feedbackIdFromSeed("problem", "same"),
		);
	});

	it("parses and bounds structured learnings", () => {
		const learning = parseLearning(`\`\`\`json
{
  "slug": "Shared Route First!",
  "summary": "  Fixed the shared route  ",
  "evidence": ["All tests passed", "Caller uses one path"],
  "behaviors": ["Inspected callers first"],
  "candidate_rule": "Fix shared routes before leaf callers."
}
\`\`\``);
		expect(learning).toEqual({
			slug: "shared-route-first",
			summary: "Fixed the shared route",
			evidence: ["All tests passed", "Caller uses one path"],
			behaviors: ["Inspected callers first"],
			candidateRule: "Fix shared routes before leaf callers.",
		});
	});

	it("rejects incomplete model output", () => {
		expect(() => parseLearning('{"summary":"done","evidence":[]}')).toThrow(
			"missing required learning fields",
		);
	});

	it("asks for evidence-backed reusable behavior and a memorable slug", () => {
		const praise = analysisPrompt("praise", "gj");
		const problem = analysisPrompt("problem", "Ignored the requested scope");
		expect(praise).toContain("conversation preceding");
		expect(praise).toContain("kebab-case");
		expect(problem).toContain("prevent recurrence");
		expect(problem).toContain("Do not reproduce credentials");
	});
});