repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
pi/agent/extensions/q/__tests__/state.test.ts
RawFile outline
- payload line 6
- returnedWizardState line 27
- inserts returned answer and advances line 28
- formatFinalAnswer line 35
- raw for one question line 36
- numbered for multiple questions line 40
- undefined for incomplete answers line 44
- validationErrors line 49
- ctx line 50
- accepts matching target line 65
- detects moved leaf and missing wizard line 69
- detects wrong wizard status line 76
import { describe, expect, test } from "bun:test";
import { returnedWizardState } from "../index.js";
import { formatFinalAnswer, validationErrors } from "../session-state.js";
import type { ReturnPayload } from "../types.js";
function payload(answer: string): ReturnPayload {
return {
wizardId: "w1",
questions: [
{ question: "q1", suggestions: [] },
{ question: "q2", suggestions: [] },
],
answers: ["a1", null],
index: 1,
sourceAssistantEntryId: "assistant-entry",
questionIndex: 1,
answer,
snapshot: {
originalSessionFile: "/tmp/session.jsonl",
originalSessionId: "sid",
returnHeadId: "head",
returnEntryCount: 7,
},
};
}
describe("returnedWizardState", () => {
test("inserts returned answer and advances", () => {
const state = returnedWizardState(payload("a2"));
expect(state.answers).toEqual(["a1", "a2"]);
expect(state.index).toBe(2);
});
});
describe("formatFinalAnswer", () => {
test("raw for one question", () => {
expect(formatFinalAnswer({ wizardId: "w", questions: [{ question: "q", suggestions: [] }], answers: ["a"], index: 1 })).toBe("a");
});
test("numbered for multiple questions", () => {
expect(formatFinalAnswer(returnedWizardState(payload("a2")))).toBe("1. a1\n2. a2");
});
test("undefined for incomplete answers", () => {
expect(formatFinalAnswer(payload("a2"))).toBeUndefined();
});
});
describe("validationErrors", () => {
function ctx(overrides: { file?: string; id?: string; leaf?: string; count?: number; wizard?: unknown } = {}) {
const entries = overrides.wizard === undefined ? [
{ type: "custom", customType: "q", data: { kind: "q-wizard", wizardId: "w1", status: "waiting-fork", index: 1 } },
] : [overrides.wizard];
return {
sessionManager: {
getSessionFile: () => overrides.file ?? "/tmp/session.jsonl",
getSessionId: () => overrides.id ?? "sid",
getLeafId: () => overrides.leaf ?? "head",
getEntries: () => Array.from({ length: overrides.count ?? 7 }, (_, i) => ({ id: String(i) })),
getBranch: () => entries,
},
} as any;
}
test("accepts matching target", () => {
expect(validationErrors(ctx(), payload("a2"))).toEqual([]);
});
test("detects moved leaf and missing wizard", () => {
expect(validationErrors(ctx({ leaf: "other", wizard: { type: "custom", customType: "x", data: {} } }), payload("a2"))).toEqual([
"original leaf moved",
"pending wizard not found",
]);
});
test("detects wrong wizard status", () => {
const wizard = { type: "custom", customType: "q", data: { kind: "q-wizard", wizardId: "w1", status: "completed", index: 1 } };
expect(validationErrors(ctx({ wizard }), payload("a2"))).toContain("wizard is completed");
});
});