|
| 1 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 2 | +import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; |
| 3 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 5 | +import { registerWorkoutPrompts } from "./workouts.js"; |
| 6 | + |
| 7 | +describe("workout prompts", () => { |
| 8 | + let client: Client; |
| 9 | + let server: McpServer; |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + server = new McpServer({ name: "prompt-test-server", version: "1.0.0" }); |
| 13 | + registerWorkoutPrompts(server); |
| 14 | + |
| 15 | + client = new Client({ name: "prompt-test-client", version: "1.0.0" }); |
| 16 | + const [clientTransport, serverTransport] = |
| 17 | + InMemoryTransport.createLinkedPair(); |
| 18 | + await Promise.all([ |
| 19 | + server.connect(serverTransport), |
| 20 | + client.connect(clientTransport), |
| 21 | + ]); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(async () => { |
| 25 | + await Promise.all([client.close(), server.close()]); |
| 26 | + }); |
| 27 | + |
| 28 | + it("lists both prompts with discoverable metadata and argument schemas", async () => { |
| 29 | + const result = await client.listPrompts(); |
| 30 | + |
| 31 | + expect(result.prompts).toHaveLength(2); |
| 32 | + expect(result.prompts).toEqual( |
| 33 | + expect.arrayContaining([ |
| 34 | + expect.objectContaining({ |
| 35 | + name: "analyze-workout-progress", |
| 36 | + title: "Analyze Workout Progress", |
| 37 | + description: expect.stringContaining("workout"), |
| 38 | + arguments: expect.arrayContaining([ |
| 39 | + expect.objectContaining({ |
| 40 | + name: "weeks", |
| 41 | + required: false, |
| 42 | + }), |
| 43 | + ]), |
| 44 | + }), |
| 45 | + expect.objectContaining({ |
| 46 | + name: "create-workout-from-routine", |
| 47 | + title: "Create Workout From Routine", |
| 48 | + description: expect.stringContaining("routine"), |
| 49 | + arguments: expect.arrayContaining([ |
| 50 | + expect.objectContaining({ name: "routineId", required: true }), |
| 51 | + expect.objectContaining({ name: "startTime", required: true }), |
| 52 | + ]), |
| 53 | + }), |
| 54 | + ]), |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it("coerces a string week count in prompts/get", async () => { |
| 59 | + const result = await client.getPrompt({ |
| 60 | + name: "analyze-workout-progress", |
| 61 | + arguments: { weeks: "6" }, |
| 62 | + }); |
| 63 | + |
| 64 | + expect(result.messages).toEqual([ |
| 65 | + expect.objectContaining({ |
| 66 | + role: "user", |
| 67 | + content: expect.objectContaining({ |
| 68 | + type: "text", |
| 69 | + text: expect.stringContaining("last 6 weeks"), |
| 70 | + }), |
| 71 | + }), |
| 72 | + ]); |
| 73 | + expect(result.messages[0]?.content).toEqual( |
| 74 | + expect.objectContaining({ |
| 75 | + text: expect.stringContaining("pageSize=10"), |
| 76 | + }), |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it("uses the default week count with explicit empty arguments", async () => { |
| 81 | + const result = await client.getPrompt({ |
| 82 | + name: "analyze-workout-progress", |
| 83 | + arguments: {}, |
| 84 | + }); |
| 85 | + |
| 86 | + expect(result.messages[0]?.content).toEqual( |
| 87 | + expect.objectContaining({ |
| 88 | + text: expect.stringContaining("last 4 weeks"), |
| 89 | + }), |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it("rejects omitting the entire arguments object at the SDK boundary", async () => { |
| 94 | + await expect( |
| 95 | + client.getPrompt({ name: "analyze-workout-progress" }), |
| 96 | + ).rejects.toThrow(/arguments/i); |
| 97 | + }); |
| 98 | + |
| 99 | + it.each(["0", "13", "2.5", "not-a-number"])( |
| 100 | + "rejects invalid week value %s", |
| 101 | + async (weeks) => { |
| 102 | + await expect( |
| 103 | + client.getPrompt({ |
| 104 | + name: "analyze-workout-progress", |
| 105 | + arguments: { weeks }, |
| 106 | + }), |
| 107 | + ).rejects.toThrow(); |
| 108 | + }, |
| 109 | + ); |
| 110 | + |
| 111 | + it("returns routine-to-workout guidance without inventing completion data", async () => { |
| 112 | + const result = await client.getPrompt({ |
| 113 | + name: "create-workout-from-routine", |
| 114 | + arguments: { |
| 115 | + routineId: "routine-123", |
| 116 | + startTime: "2026-07-10T08:00:00Z", |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(result.messages).toHaveLength(1); |
| 121 | + expect(result.messages[0]).toEqual( |
| 122 | + expect.objectContaining({ |
| 123 | + role: "user", |
| 124 | + content: expect.objectContaining({ |
| 125 | + type: "text", |
| 126 | + text: expect.stringMatching( |
| 127 | + /get-routine[\s\S]*restSeconds[\s\S]*repRange[\s\S]*endTime[\s\S]*Never invent/, |
| 128 | + ), |
| 129 | + }), |
| 130 | + }), |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it("rejects an invalid workout start timestamp", async () => { |
| 135 | + await expect( |
| 136 | + client.getPrompt({ |
| 137 | + name: "create-workout-from-routine", |
| 138 | + arguments: { |
| 139 | + routineId: "routine-123", |
| 140 | + startTime: "2026-07-10T08:00:00+00:00", |
| 141 | + }, |
| 142 | + }), |
| 143 | + ).rejects.toThrow(); |
| 144 | + }); |
| 145 | +}); |
0 commit comments