|
| 1 | +import { mswServer } from "@sentry/mcp-server-mocks"; |
| 2 | +import { HttpResponse, http } from "msw"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { describe, expect, it, vi } from "vitest"; |
| 5 | +import { |
| 6 | + assertStructuredOnlyResult, |
| 7 | + getStructuredContent, |
| 8 | +} from "../../test-utils/structured-content.js"; |
| 9 | +import deleteMonitorEnvironment, { |
| 10 | + deleteMonitorEnvironmentOutputSchema, |
| 11 | +} from "./delete-monitor-environment.js"; |
| 12 | + |
| 13 | +const context = { |
| 14 | + constraints: { organizationSlug: null }, |
| 15 | + accessToken: "access-token", |
| 16 | + userId: "1", |
| 17 | +}; |
| 18 | +const params = { |
| 19 | + organizationSlug: "sentry-mcp-evals", |
| 20 | + regionUrl: null, |
| 21 | + projectSlug: "cloudflare-mcp", |
| 22 | + monitorSlug: "nightly-import", |
| 23 | + environment: "production", |
| 24 | +}; |
| 25 | +const endpoint = |
| 26 | + "https://sentry.io/api/0/projects/sentry-mcp-evals/cloudflare-mcp/monitors/nightly-import/environments/production/"; |
| 27 | + |
| 28 | +describe("delete_monitor_environment", () => { |
| 29 | + it("schedules deletion of an exact monitor environment", async () => { |
| 30 | + const result = await deleteMonitorEnvironment.handler(params, context); |
| 31 | + assertStructuredOnlyResult(result); |
| 32 | + const content = getStructuredContent(result); |
| 33 | + expect(deleteMonitorEnvironmentOutputSchema.parse(content)).toEqual( |
| 34 | + content, |
| 35 | + ); |
| 36 | + expect(content).toMatchInlineSnapshot(` |
| 37 | + { |
| 38 | + "environment": "production", |
| 39 | + "monitorSlug": "nightly-import", |
| 40 | + "projectSlug": "cloudflare-mcp", |
| 41 | + "success": true, |
| 42 | + } |
| 43 | + `); |
| 44 | + }); |
| 45 | + |
| 46 | + it("treats an absent or pending deletion environment as success", async () => { |
| 47 | + mswServer.use( |
| 48 | + http.delete(endpoint, () => new HttpResponse(null, { status: 404 })), |
| 49 | + ); |
| 50 | + const result = await deleteMonitorEnvironment.handler(params, context); |
| 51 | + expect(getStructuredContent(result)).toEqual({ |
| 52 | + success: true, |
| 53 | + monitorSlug: params.monitorSlug, |
| 54 | + projectSlug: params.projectSlug, |
| 55 | + environment: params.environment, |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it.each([403, 500])("propagates HTTP %s failures", async (status) => { |
| 60 | + mswServer.use( |
| 61 | + http.delete(endpoint, () => |
| 62 | + HttpResponse.json({ detail: "Request failed" }, { status }), |
| 63 | + ), |
| 64 | + ); |
| 65 | + await expect( |
| 66 | + deleteMonitorEnvironment.handler(params, context), |
| 67 | + ).rejects.toThrow(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("rejects a project outside the active constraint before deletion", async () => { |
| 71 | + const remove = vi.fn(() => new HttpResponse(null, { status: 202 })); |
| 72 | + mswServer.use(http.delete(endpoint, remove)); |
| 73 | + await expect( |
| 74 | + deleteMonitorEnvironment.handler(params, { |
| 75 | + ...context, |
| 76 | + constraints: { organizationSlug: null, projectSlug: "other-project" }, |
| 77 | + }), |
| 78 | + ).rejects.toThrow("outside the active project constraint"); |
| 79 | + expect(remove).not.toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + it("preserves exact environment names and encodes path delimiters", async () => { |
| 83 | + const environment = " Production #? "; |
| 84 | + const remove = vi.fn(({ request }: { request: Request }) => { |
| 85 | + expect(request.url).toBe( |
| 86 | + `${endpoint.slice(0, -"production/".length)}${encodeURIComponent(environment)}/`, |
| 87 | + ); |
| 88 | + return new HttpResponse(null, { status: 202 }); |
| 89 | + }); |
| 90 | + mswServer.use( |
| 91 | + http.delete( |
| 92 | + "https://sentry.io/api/0/projects/sentry-mcp-evals/cloudflare-mcp/monitors/nightly-import/environments/:environment/", |
| 93 | + remove, |
| 94 | + ), |
| 95 | + ); |
| 96 | + const parsed = z |
| 97 | + .object(deleteMonitorEnvironment.inputSchema) |
| 98 | + .parse({ ...params, environment }); |
| 99 | + const result = await deleteMonitorEnvironment.handler(parsed, context); |
| 100 | + expect(getStructuredContent(result)).toMatchObject({ environment }); |
| 101 | + expect(remove).toHaveBeenCalledOnce(); |
| 102 | + }); |
| 103 | + |
| 104 | + it("requires a nonempty environment", () => { |
| 105 | + expect( |
| 106 | + z |
| 107 | + .object(deleteMonitorEnvironment.inputSchema) |
| 108 | + .safeParse({ ...params, environment: "" }).success, |
| 109 | + ).toBe(false); |
| 110 | + }); |
| 111 | +}); |
0 commit comments