|
| 1 | +import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js"; |
| 2 | +import type { |
| 3 | + ServerNotification, |
| 4 | + ServerRequest, |
| 5 | +} from "@modelcontextprotocol/sdk/types.js"; |
| 6 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 7 | +import { CreateTestCycleWebLink } from "../../../../../zephyr/tool/test-cycle/create-web-link"; |
| 8 | + |
| 9 | +describe("CreateTestCycleWebLink", () => { |
| 10 | + let mockClient: any; |
| 11 | + let instance: CreateTestCycleWebLink; |
| 12 | + |
| 13 | + const EXTRA_REQUEST_HANDLER: RequestHandlerExtra< |
| 14 | + ServerRequest, |
| 15 | + ServerNotification |
| 16 | + > = { |
| 17 | + signal: AbortSignal.timeout(5000), |
| 18 | + requestId: "", |
| 19 | + sendNotification: (_notification) => { |
| 20 | + throw new Error("Function not implemented."); |
| 21 | + }, |
| 22 | + sendRequest: (_request, _resultSchema, _options?) => { |
| 23 | + throw new Error("Function not implemented."); |
| 24 | + }, |
| 25 | + }; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + mockClient = { |
| 29 | + getApiClient: vi.fn().mockReturnValue({ |
| 30 | + post: vi.fn(), |
| 31 | + }), |
| 32 | + }; |
| 33 | + instance = new CreateTestCycleWebLink(mockClient as any); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should set specification correctly", () => { |
| 37 | + expect(instance.specification.title).toBe("Create Test Cycle Web Link"); |
| 38 | + expect(instance.specification.summary).toBe( |
| 39 | + "Create a new Web Link for a Test Cycle in Zephyr", |
| 40 | + ); |
| 41 | + expect(instance.specification.readOnly).toBe(false); |
| 42 | + expect(instance.specification.idempotent).toBe(false); |
| 43 | + expect(instance.specification.inputSchema).toBeDefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should call apiClient.post with correct params (Test Cycle Key) and return created web link information", async () => { |
| 47 | + const responseMock = { |
| 48 | + id: 53, |
| 49 | + self: "https://<api-base-url>/weblinks/53", |
| 50 | + }; |
| 51 | + |
| 52 | + mockClient.getApiClient().post.mockResolvedValueOnce(responseMock); |
| 53 | + |
| 54 | + const args = { |
| 55 | + testCycleIdOrKey: "SA-R1", |
| 56 | + url: "https://example.com", |
| 57 | + description: "Link to documentation", |
| 58 | + }; |
| 59 | + |
| 60 | + const result = await instance.handle(args, EXTRA_REQUEST_HANDLER); |
| 61 | + |
| 62 | + expect(mockClient.getApiClient().post).toHaveBeenCalledWith( |
| 63 | + "/testcycles/SA-R1/links/weblinks", |
| 64 | + { |
| 65 | + url: args.url, |
| 66 | + description: args.description, |
| 67 | + }, |
| 68 | + ); |
| 69 | + |
| 70 | + expect(result.structuredContent).toBe(responseMock); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should call apiClient.post with correct params (Test Cycle Id) and return created web link information", async () => { |
| 74 | + const responseMock = { |
| 75 | + id: 53, |
| 76 | + self: "https://<api-base-url>/weblinks/53", |
| 77 | + }; |
| 78 | + |
| 79 | + mockClient.getApiClient().post.mockResolvedValueOnce(responseMock); |
| 80 | + |
| 81 | + const args = { |
| 82 | + testCycleIdOrKey: "10001", |
| 83 | + url: "https://example.com", |
| 84 | + description: "Link to documentation", |
| 85 | + }; |
| 86 | + |
| 87 | + const result = await instance.handle(args, EXTRA_REQUEST_HANDLER); |
| 88 | + |
| 89 | + expect(mockClient.getApiClient().post).toHaveBeenCalledWith( |
| 90 | + "/testcycles/10001/links/weblinks", |
| 91 | + { |
| 92 | + url: args.url, |
| 93 | + description: args.description, |
| 94 | + }, |
| 95 | + ); |
| 96 | + |
| 97 | + expect(result.structuredContent).toBe(responseMock); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should ignore extra parameters not in the schema", async () => { |
| 101 | + const responseMock = { |
| 102 | + id: 54, |
| 103 | + self: "https://<api-base-url>/weblinks/54", |
| 104 | + }; |
| 105 | + |
| 106 | + mockClient.getApiClient().post.mockResolvedValueOnce(responseMock); |
| 107 | + |
| 108 | + const args = { |
| 109 | + testCycleIdOrKey: "SA-R1", |
| 110 | + url: "https://example.com", |
| 111 | + description: "Link to documentation", |
| 112 | + extraField: "should be ignored", |
| 113 | + }; |
| 114 | + |
| 115 | + const result = await instance.handle(args, EXTRA_REQUEST_HANDLER); |
| 116 | + |
| 117 | + expect(mockClient.getApiClient().post).toHaveBeenCalledWith( |
| 118 | + "/testcycles/SA-R1/links/weblinks", |
| 119 | + { |
| 120 | + url: args.url, |
| 121 | + description: args.description, |
| 122 | + }, |
| 123 | + ); |
| 124 | + |
| 125 | + expect(result.structuredContent).toBe(responseMock); |
| 126 | + }); |
| 127 | + |
| 128 | + it("should handle apiClient.post throwing error", async () => { |
| 129 | + mockClient |
| 130 | + .getApiClient() |
| 131 | + .post.mockRejectedValueOnce(new Error("API error")); |
| 132 | + |
| 133 | + const args = { |
| 134 | + testCycleIdOrKey: "SA-R1", |
| 135 | + url: "https://example.com", |
| 136 | + description: "Link to documentation", |
| 137 | + }; |
| 138 | + |
| 139 | + await expect(instance.handle(args, EXTRA_REQUEST_HANDLER)).rejects.toThrow( |
| 140 | + "API error", |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + it("should throw validation error if url is missing", async () => { |
| 145 | + const args = { |
| 146 | + testCycleIdOrKey: "SA-R1", |
| 147 | + description: "Link to documentation", |
| 148 | + }; |
| 149 | + |
| 150 | + await expect( |
| 151 | + instance.handle(args, EXTRA_REQUEST_HANDLER), |
| 152 | + ).rejects.toThrow(); |
| 153 | + }); |
| 154 | + |
| 155 | + it("should throw validation error if testCycleIdOrKey is missing", async () => { |
| 156 | + const args = { |
| 157 | + url: "https://example.com", |
| 158 | + description: "Link to documentation", |
| 159 | + }; |
| 160 | + |
| 161 | + await expect( |
| 162 | + instance.handle(args, EXTRA_REQUEST_HANDLER), |
| 163 | + ).rejects.toThrow(); |
| 164 | + }); |
| 165 | +}); |
0 commit comments