|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { BUILTIN_PROMPTS, type PromptsConfig } from "perplexity-user-mcp/prompts-config"; |
| 3 | +import { |
| 4 | + deletePromptHandler, |
| 5 | + resetPromptHandler, |
| 6 | + savePromptHandler, |
| 7 | +} from "../src/webview/prompts-handler.js"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Pure handlers for prompts:save / delete / reset, extracted from |
| 11 | + * DashboardProvider. The merge/upsert/read/write primitives are covered by the |
| 12 | + * mcp-server prompts-config tests; here we verify the extension-side validation |
| 13 | + * and dispatch. The postActionResult/postPromptsState wiring is exercised by the |
| 14 | + * smoke checklist in docs/smoke-tests.md. |
| 15 | + */ |
| 16 | +function makeDeps(initial: PromptsConfig = { overrides: {}, custom: [] }) { |
| 17 | + let config: PromptsConfig = initial; |
| 18 | + let writes = 0; |
| 19 | + return { |
| 20 | + deps: { |
| 21 | + read: () => config, |
| 22 | + write: (c: PromptsConfig) => { |
| 23 | + config = c; |
| 24 | + writes += 1; |
| 25 | + }, |
| 26 | + }, |
| 27 | + current: () => config, |
| 28 | + writeCount: () => writes, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +const validCustom = { |
| 33 | + name: "my-cmd", |
| 34 | + description: "mine", |
| 35 | + arguments: [{ name: "q", description: "the query", required: true }], |
| 36 | + template: "Search: {q}", |
| 37 | +}; |
| 38 | + |
| 39 | +describe("savePromptHandler", () => { |
| 40 | + it("persists a valid custom prompt", () => { |
| 41 | + const h = makeDeps(); |
| 42 | + const out = savePromptHandler(validCustom, h.deps); |
| 43 | + expect(out.ok).toBe(true); |
| 44 | + expect(h.current().custom.map((c) => c.name)).toEqual(["my-cmd"]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("rejects an invalid name without writing", () => { |
| 48 | + const h = makeDeps(); |
| 49 | + const out = savePromptHandler({ ...validCustom, name: "Bad Name" }, h.deps); |
| 50 | + expect(out).toEqual({ ok: false, error: expect.stringContaining("Invalid command name") }); |
| 51 | + expect(h.writeCount()).toBe(0); |
| 52 | + }); |
| 53 | + |
| 54 | + it("rejects an empty template without writing", () => { |
| 55 | + const h = makeDeps(); |
| 56 | + const out = savePromptHandler({ ...validCustom, template: " " }, h.deps); |
| 57 | + expect(out).toEqual({ ok: false, error: expect.stringContaining("Template cannot be empty") }); |
| 58 | + expect(h.writeCount()).toBe(0); |
| 59 | + }); |
| 60 | + |
| 61 | + it("writes an override (not a custom entry) for a built-in name", () => { |
| 62 | + const builtin = BUILTIN_PROMPTS[0].name; |
| 63 | + const h = makeDeps(); |
| 64 | + const out = savePromptHandler({ ...validCustom, name: builtin }, h.deps); |
| 65 | + expect(out.ok).toBe(true); |
| 66 | + expect(h.current().overrides[builtin]).toBeDefined(); |
| 67 | + expect(h.current().custom).toHaveLength(0); |
| 68 | + }); |
| 69 | + |
| 70 | + it("drops arguments without names and coerces required", () => { |
| 71 | + const h = makeDeps(); |
| 72 | + savePromptHandler( |
| 73 | + { |
| 74 | + name: "argy", |
| 75 | + description: "", |
| 76 | + arguments: [ |
| 77 | + { name: "keep", description: "", required: 1 as unknown as boolean }, |
| 78 | + { name: "", description: "nameless", required: true }, |
| 79 | + ], |
| 80 | + template: "{keep}", |
| 81 | + }, |
| 82 | + h.deps, |
| 83 | + ); |
| 84 | + const saved = h.current().custom[0]; |
| 85 | + expect(saved.arguments).toEqual([{ name: "keep", description: "", required: true }]); |
| 86 | + }); |
| 87 | + |
| 88 | + it("rejects a missing payload", () => { |
| 89 | + const h = makeDeps(); |
| 90 | + expect(savePromptHandler(undefined, h.deps).ok).toBe(false); |
| 91 | + expect(h.writeCount()).toBe(0); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +describe("deletePromptHandler", () => { |
| 96 | + it("removes the named custom prompt", () => { |
| 97 | + const h = makeDeps({ overrides: {}, custom: [{ name: "a", description: "", arguments: [], template: "x" }] }); |
| 98 | + const out = deletePromptHandler("a", h.deps); |
| 99 | + expect(out.ok).toBe(true); |
| 100 | + expect(h.current().custom).toHaveLength(0); |
| 101 | + }); |
| 102 | + |
| 103 | + it("rejects an empty name", () => { |
| 104 | + const h = makeDeps(); |
| 105 | + expect(deletePromptHandler("", h.deps).ok).toBe(false); |
| 106 | + expect(h.writeCount()).toBe(0); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe("resetPromptHandler", () => { |
| 111 | + it("clears a built-in override", () => { |
| 112 | + const builtin = BUILTIN_PROMPTS[0].name; |
| 113 | + const h = makeDeps({ overrides: { [builtin]: { name: builtin, description: "x", arguments: [], template: "y" } }, custom: [] }); |
| 114 | + const out = resetPromptHandler(builtin, h.deps); |
| 115 | + expect(out.ok).toBe(true); |
| 116 | + expect(h.current().overrides[builtin]).toBeUndefined(); |
| 117 | + }); |
| 118 | + |
| 119 | + it("is a no-op (still ok) when there is no override", () => { |
| 120 | + const h = makeDeps(); |
| 121 | + expect(resetPromptHandler(BUILTIN_PROMPTS[0].name, h.deps).ok).toBe(true); |
| 122 | + }); |
| 123 | +}); |
0 commit comments