|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | + |
| 3 | +vi.mock("../../utils/logger.js", () => ({ |
| 4 | + logger: { |
| 5 | + debug: vi.fn(), |
| 6 | + info: vi.fn(), |
| 7 | + warn: vi.fn(), |
| 8 | + error: vi.fn(), |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +import { registerPrompts } from "../index.js"; |
| 13 | + |
| 14 | +type PromptHandler = (params: Record<string, string | undefined>) => { |
| 15 | + messages: Array<{ |
| 16 | + role: string; |
| 17 | + content: { type: string; text: string }; |
| 18 | + }>; |
| 19 | +}; |
| 20 | + |
| 21 | +function createMockServer() { |
| 22 | + const prompts = new Map<string, PromptHandler>(); |
| 23 | + return { |
| 24 | + prompt: vi.fn( |
| 25 | + ( |
| 26 | + name: string, |
| 27 | + _description: string, |
| 28 | + _schema: unknown, |
| 29 | + handler: PromptHandler |
| 30 | + ) => { |
| 31 | + prompts.set(name, handler); |
| 32 | + } |
| 33 | + ), |
| 34 | + prompts, |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +describe("prompts", () => { |
| 39 | + let server: ReturnType<typeof createMockServer>; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + vi.clearAllMocks(); |
| 43 | + server = createMockServer(); |
| 44 | + registerPrompts(server as unknown as Parameters<typeof registerPrompts>[0]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("registers all 4 prompts", () => { |
| 48 | + expect(server.prompts.size).toBe(4); |
| 49 | + expect(server.prompts.has("deploy-stack")).toBe(true); |
| 50 | + expect(server.prompts.has("troubleshoot-container")).toBe(true); |
| 51 | + expect(server.prompts.has("security-audit")).toBe(true); |
| 52 | + expect(server.prompts.has("cleanup-environment")).toBe(true); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("deploy-stack", () => { |
| 56 | + it("returns messages with correct tool names", () => { |
| 57 | + const handler = server.prompts.get("deploy-stack")!; |
| 58 | + const result = handler({ environmentId: "env-1" }); |
| 59 | + |
| 60 | + expect(result.messages).toHaveLength(1); |
| 61 | + expect(result.messages[0].role).toBe("user"); |
| 62 | + expect(result.messages[0].content.type).toBe("text"); |
| 63 | + |
| 64 | + const text = result.messages[0].content.text; |
| 65 | + expect(text).toContain("environment env-1"); |
| 66 | + expect(text).toContain("arcane_environment_get"); |
| 67 | + expect(text).toContain("arcane_project_create"); |
| 68 | + expect(text).toContain("arcane_project_up"); |
| 69 | + expect(text).toContain("arcane_container_list"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("includes compose path when provided", () => { |
| 73 | + const handler = server.prompts.get("deploy-stack")!; |
| 74 | + const result = handler({ |
| 75 | + environmentId: "env-1", |
| 76 | + composePath: "/opt/docker-compose.yml", |
| 77 | + }); |
| 78 | + |
| 79 | + const text = result.messages[0].content.text; |
| 80 | + expect(text).toContain("/opt/docker-compose.yml"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("asks for compose content when no path given", () => { |
| 84 | + const handler = server.prompts.get("deploy-stack")!; |
| 85 | + const result = handler({ environmentId: "env-1" }); |
| 86 | + |
| 87 | + const text = result.messages[0].content.text; |
| 88 | + expect(text).toContain("Ask the user for the compose file content"); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe("security-audit", () => { |
| 93 | + it("references vulnerability tools", () => { |
| 94 | + const handler = server.prompts.get("security-audit")!; |
| 95 | + const result = handler({ environmentId: "env-1" }); |
| 96 | + |
| 97 | + const text = result.messages[0].content.text; |
| 98 | + expect(text).toContain("arcane_vulnerability_get_scanner_status"); |
| 99 | + expect(text).toContain("arcane_vulnerability_get_environment_summary"); |
| 100 | + expect(text).toContain("arcane_vulnerability_list_all"); |
| 101 | + expect(text).toContain("arcane_vulnerability_list"); |
| 102 | + expect(text).toContain("arcane_image_update_check_all"); |
| 103 | + expect(text).toContain("arcane_port_list"); |
| 104 | + expect(text).toContain("arcane_network_list"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("returns proper message structure", () => { |
| 108 | + const handler = server.prompts.get("security-audit")!; |
| 109 | + const result = handler({ environmentId: "env-1" }); |
| 110 | + |
| 111 | + expect(result.messages).toHaveLength(1); |
| 112 | + expect(result.messages[0]).toHaveProperty("role", "user"); |
| 113 | + expect(result.messages[0].content).toHaveProperty("type", "text"); |
| 114 | + expect(typeof result.messages[0].content.text).toBe("string"); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe("troubleshoot-container", () => { |
| 119 | + it("returns diagnostic workflow with correct tools", () => { |
| 120 | + const handler = server.prompts.get("troubleshoot-container")!; |
| 121 | + const result = handler({ |
| 122 | + environmentId: "env-1", |
| 123 | + containerId: "my-app", |
| 124 | + }); |
| 125 | + |
| 126 | + const text = result.messages[0].content.text; |
| 127 | + expect(text).toContain("my-app"); |
| 128 | + expect(text).toContain("arcane_container_get"); |
| 129 | + expect(text).toContain("arcane_dashboard_get_action_items"); |
| 130 | + expect(text).toContain("arcane_image_update_check_by_id"); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe("cleanup-environment", () => { |
| 135 | + it("returns cleanup workflow with correct tools", () => { |
| 136 | + const handler = server.prompts.get("cleanup-environment")!; |
| 137 | + const result = handler({ environmentId: "env-1" }); |
| 138 | + |
| 139 | + const text = result.messages[0].content.text; |
| 140 | + expect(text).toContain("arcane_dashboard_get"); |
| 141 | + expect(text).toContain("arcane_container_list"); |
| 142 | + expect(text).toContain("arcane_image_prune"); |
| 143 | + expect(text).toContain("arcane_volume_prune"); |
| 144 | + expect(text).toContain("arcane_network_prune"); |
| 145 | + }); |
| 146 | + |
| 147 | + it("returns proper message structure", () => { |
| 148 | + const handler = server.prompts.get("cleanup-environment")!; |
| 149 | + const result = handler({ environmentId: "env-1" }); |
| 150 | + |
| 151 | + expect(result.messages).toHaveLength(1); |
| 152 | + expect(result.messages[0].role).toBe("user"); |
| 153 | + expect(result.messages[0].content.type).toBe("text"); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments