|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import type { ExtensionAPI, ExtensionCommandContext } from "@mariozechner/pi-coding-agent"; |
| 3 | +import { handleBlueprintCommand } from "./blueprint-command.js"; |
| 4 | +import type { StateRef, BlueprintExtensionState, Blueprint, Phase, Task } from "./types.js"; |
| 5 | + |
| 6 | +vi.mock("./storage.js", () => ({ |
| 7 | + saveBlueprint: vi.fn(), |
| 8 | + appendHistory: vi.fn(), |
| 9 | + saveIndex: vi.fn(), |
| 10 | + loadIndex: vi.fn().mockReturnValue(null), |
| 11 | +})); |
| 12 | + |
| 13 | +function makeTask(overrides: Partial<Task> & { id: string }): Task { |
| 14 | + return { |
| 15 | + title: overrides.id, description: "", status: "pending", |
| 16 | + acceptance_criteria: [], file_targets: [], dependencies: [], |
| 17 | + started_at: null, completed_at: null, session_id: null, notes: null, |
| 18 | + ...overrides, |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +function makePhase(overrides: Partial<Phase> & { id: string }): Phase { |
| 23 | + return { |
| 24 | + title: `Phase ${overrides.id}`, description: "", status: "pending", |
| 25 | + tasks: [], verification_gates: [], started_at: null, completed_at: null, |
| 26 | + ...overrides, |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +function makeBlueprint(): Blueprint { |
| 31 | + return { |
| 32 | + id: "bp-1", objective: "Test", project_id: "proj-1", status: "active", |
| 33 | + created_at: "2026-04-11T00:00:00.000Z", updated_at: "2026-04-11T00:00:00.000Z", |
| 34 | + phases: [makePhase({ id: "1", tasks: [makeTask({ id: "1.1" })] })], |
| 35 | + active_phase_id: "1", active_task_id: "1.1", |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function createMocks() { |
| 40 | + const notifications: { text: string; level: string }[] = []; |
| 41 | + const sentMessages: string[] = []; |
| 42 | + const ctx = { |
| 43 | + ui: { |
| 44 | + notify: vi.fn((text: string, level: string) => notifications.push({ text, level })), |
| 45 | + }, |
| 46 | + }; |
| 47 | + const pi = { |
| 48 | + sendUserMessage: vi.fn((text: string) => sentMessages.push(text)), |
| 49 | + }; |
| 50 | + return { ctx, pi, notifications, sentMessages }; |
| 51 | +} |
| 52 | + |
| 53 | +describe("handleBlueprintCommand", () => { |
| 54 | + let state: BlueprintExtensionState; |
| 55 | + let stateRef: StateRef; |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + state = { project: { id: "p", name: "test", root: "/tmp" }, blueprint: null, sessionId: "s" }; |
| 59 | + stateRef = { get: () => state, set: (s) => { state = s; } }; |
| 60 | + }); |
| 61 | + |
| 62 | + it("shows status when no args and no blueprint", async () => { |
| 63 | + const { ctx, pi } = createMocks(); |
| 64 | + await handleBlueprintCommand("", ctx as unknown as ExtensionCommandContext, stateRef, pi as unknown as ExtensionAPI); |
| 65 | + expect(ctx.ui.notify).toHaveBeenCalledWith(expect.stringContaining("No active blueprint"), "info"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("shows plan when no args and blueprint exists", async () => { |
| 69 | + state = { ...state, blueprint: makeBlueprint() }; |
| 70 | + const { ctx, pi } = createMocks(); |
| 71 | + await handleBlueprintCommand("", ctx as unknown as ExtensionCommandContext, stateRef, pi as unknown as ExtensionAPI); |
| 72 | + expect(ctx.ui.notify).toHaveBeenCalledWith(expect.stringContaining("Blueprint: Test"), "info"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("sends generation prompt for new objective", async () => { |
| 76 | + const { ctx, pi } = createMocks(); |
| 77 | + await handleBlueprintCommand("Add OAuth2 auth", ctx as unknown as ExtensionCommandContext, stateRef, pi as unknown as ExtensionAPI); |
| 78 | + expect(pi.sendUserMessage).toHaveBeenCalledWith( |
| 79 | + expect.stringContaining("Add OAuth2 auth"), |
| 80 | + expect.objectContaining({ deliverAs: "followUp" }), |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("warns when active blueprint exists", async () => { |
| 85 | + state = { ...state, blueprint: makeBlueprint() }; |
| 86 | + const { ctx, pi } = createMocks(); |
| 87 | + await handleBlueprintCommand("New thing", ctx as unknown as ExtensionCommandContext, stateRef, pi as unknown as ExtensionAPI); |
| 88 | + expect(ctx.ui.notify).toHaveBeenCalledWith(expect.stringContaining("already exists"), "warning"); |
| 89 | + expect(pi.sendUserMessage).not.toHaveBeenCalled(); |
| 90 | + }); |
| 91 | + |
| 92 | + it("abandons active blueprint", async () => { |
| 93 | + state = { ...state, blueprint: makeBlueprint() }; |
| 94 | + const { ctx, pi } = createMocks(); |
| 95 | + await handleBlueprintCommand("abandon", ctx as unknown as ExtensionCommandContext, stateRef, pi as unknown as ExtensionAPI); |
| 96 | + expect(ctx.ui.notify).toHaveBeenCalledWith(expect.stringContaining("abandoned"), "info"); |
| 97 | + expect(state.blueprint).toBeNull(); |
| 98 | + }); |
| 99 | +}); |
0 commit comments