repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/good-job/__tests__/core.test.ts
Rawimport { describe, expect, it } from "vitest";
import {
feedbackId,
feedbackIdFromSeed,
feedbackTrigger,
learningSlug,
parseLearning,
} from "../core.js";
describe("good-job core", () => {
it.each([
[" GJ ", { kind: "praise", feedback: "GJ" }],
[
"good job?!\tStay in scope",
{ kind: "praise", feedback: "Stay in scope" },
],
["WTF?! stop", { kind: "problem", feedback: "stop" }],
["wtf!!!", { kind: "problem", feedback: "wtf" }],
] as const)("recognizes explicit feedback %j", (text, expected) => {
expect(feedbackTrigger(text)).toEqual(expected);
});
it.each(["great job", "object.gj", "/gj list", "", "awtf"])(
"ignores non-feedback input %j",
(text) => expect(feedbackTrigger(text)).toBeUndefined(),
);
it("normalizes slugs and caps them at six words and 48 characters", () => {
expect(learningSlug(" Café / Shared Route! ")).toBe("cafe-shared-route");
expect(learningSlug("one two three four five six seven")).toBe(
"one-two-three-four-five-six",
);
expect(learningSlug("!!!")).toBe("learning");
expect(learningSlug("a".repeat(60))).toHaveLength(48);
});
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.each([
{ candidate_rule: null },
{ candidateRule: null },
{ candidate_rule: null, candidateRule: "ignored legacy alias" },
])("accepts an explicit absent candidate rule: %j", (candidate) => {
expect(
parseLearning(
JSON.stringify({
slug: "limited-evidence",
summary: "Evidence does not justify a rule",
evidence: ["One observation"],
behaviors: ["Inspected the observation"],
...candidate,
}),
).candidateRule,
).toBeNull();
});
it("cleans and bounds untrusted learning fields", () => {
const learning = parseLearning(
JSON.stringify({
summary: ` ${"s".repeat(200)}\nsummary `,
evidence: [
" observed\n outcome ",
...Array.from({ length: 10 }, (_, index) => `extra ${index}`),
],
behaviors: [" inspected\t callers "],
candidate_rule: null,
}),
);
expect(learning.slug).toBe("s".repeat(48));
expect(learning.summary).toHaveLength(180);
expect(learning.evidence).toHaveLength(8);
expect(learning.evidence.every((item) => item.trim().length > 0)).toBe(
true,
);
expect(learning.evidence[0]).toBe("observed outcome");
expect(learning.behaviors).toEqual(["inspected callers"]);
expect(learning.candidateRule).toBeNull();
});
});