|
| 1 | +import axios from "axios"; |
| 2 | +import config from "../../config"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 5 | +import { formatAxiosError } from "../../lib/error"; // or correct |
| 6 | + |
| 7 | +interface TestCaseStep { |
| 8 | + step: string; |
| 9 | + result: string; |
| 10 | +} |
| 11 | + |
| 12 | +interface IssueTracker { |
| 13 | + name: string; |
| 14 | + host: string; |
| 15 | +} |
| 16 | + |
| 17 | +export interface TestCaseCreateRequest { |
| 18 | + project_identifier: string; |
| 19 | + folder_id: string; |
| 20 | + name: string; |
| 21 | + description?: string; |
| 22 | + owner?: string; |
| 23 | + preconditions?: string; |
| 24 | + test_case_steps: TestCaseStep[]; |
| 25 | + issues?: string[]; |
| 26 | + issue_tracker?: IssueTracker; |
| 27 | + tags?: string[]; |
| 28 | + custom_fields?: Record<string, string>; |
| 29 | +} |
| 30 | + |
| 31 | +export interface TestCaseResponse { |
| 32 | + data: { |
| 33 | + success: boolean; |
| 34 | + test_case: { |
| 35 | + case_type: string; |
| 36 | + priority: string; |
| 37 | + status: string; |
| 38 | + folder_id: number; |
| 39 | + issues: Array<{ |
| 40 | + jira_id: string; |
| 41 | + issue_type: string; |
| 42 | + }>; |
| 43 | + tags: string[]; |
| 44 | + template: string; |
| 45 | + description: string; |
| 46 | + preconditions: string; |
| 47 | + title: string; |
| 48 | + identifier: string; |
| 49 | + automation_status: string; |
| 50 | + owner: string; |
| 51 | + steps: TestCaseStep[]; |
| 52 | + custom_fields: Array<{ |
| 53 | + name: string; |
| 54 | + value: string; |
| 55 | + }>; |
| 56 | + }; |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +export const CreateTestCaseSchema = z.object({ |
| 61 | + project_identifier: z |
| 62 | + .string() |
| 63 | + .describe( |
| 64 | + "The ID of the BrowserStack project where the test case should be created. If no project identifier is provided, ask the user if they would like to create a new project using the createProjectOrFolder tool.", |
| 65 | + ), |
| 66 | + folder_id: z |
| 67 | + .string() |
| 68 | + .describe( |
| 69 | + "The ID of the folder within the project where the test case should be created. If not provided, ask the user if they would like to create a new folder using the createProjectOrFolder tool.", |
| 70 | + ), |
| 71 | + name: z.string().describe("Name of the test case."), |
| 72 | + description: z |
| 73 | + .string() |
| 74 | + .optional() |
| 75 | + .describe("Brief description of the test case."), |
| 76 | + owner: z |
| 77 | + .string() |
| 78 | + .email() |
| 79 | + .describe("Email of the test case owner.") |
| 80 | + .optional(), |
| 81 | + preconditions: z |
| 82 | + .string() |
| 83 | + .optional() |
| 84 | + .describe("Any preconditions (HTML allowed)."), |
| 85 | + test_case_steps: z |
| 86 | + .array( |
| 87 | + z.object({ |
| 88 | + step: z.string().describe("Action to perform in this step."), |
| 89 | + result: z.string().describe("Expected result of this step."), |
| 90 | + }), |
| 91 | + ) |
| 92 | + .describe("List of steps and expected results."), |
| 93 | + issues: z |
| 94 | + .array(z.string()) |
| 95 | + .optional() |
| 96 | + .describe( |
| 97 | + "List of the linked Jira, Asana or Azure issues ID's. This should be strictly in array format not the string of json.", |
| 98 | + ), |
| 99 | + issue_tracker: z |
| 100 | + .object({ |
| 101 | + name: z |
| 102 | + .string() |
| 103 | + .describe( |
| 104 | + "Issue tracker name, For example, use jira for Jira, azure for Azure DevOps, or asana for Asana.", |
| 105 | + ), |
| 106 | + host: z.string().url().describe("Base URL of the issue tracker."), |
| 107 | + }) |
| 108 | + .optional(), |
| 109 | + tags: z |
| 110 | + .array(z.string()) |
| 111 | + .optional() |
| 112 | + .describe( |
| 113 | + "Tags to attach to the test case. This should be strictly in array format not the string of json", |
| 114 | + ), |
| 115 | + custom_fields: z |
| 116 | + .record(z.string(), z.string()) |
| 117 | + .optional() |
| 118 | + .describe("Map of custom field names to values."), |
| 119 | +}); |
| 120 | + |
| 121 | +export function sanitizeArgs(args: any) { |
| 122 | + const cleaned = { ...args }; |
| 123 | + |
| 124 | + if (cleaned.description === null) delete cleaned.description; |
| 125 | + if (cleaned.owner === null) delete cleaned.owner; |
| 126 | + if (cleaned.preconditions === null) delete cleaned.preconditions; |
| 127 | + |
| 128 | + if (cleaned.issue_tracker) { |
| 129 | + if ( |
| 130 | + cleaned.issue_tracker.name === undefined || |
| 131 | + cleaned.issue_tracker.host === undefined |
| 132 | + ) { |
| 133 | + delete cleaned.issue_tracker; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return cleaned; |
| 138 | +} |
| 139 | + |
| 140 | +export async function createTestCase( |
| 141 | + params: TestCaseCreateRequest, |
| 142 | +): Promise<CallToolResult> { |
| 143 | + const body = { test_case: params }; |
| 144 | + |
| 145 | + try { |
| 146 | + const response = await axios.post<TestCaseResponse>( |
| 147 | + `https://test-management.browserstack.com/api/v2/projects/${encodeURIComponent( |
| 148 | + params.project_identifier, |
| 149 | + )}/folders/${encodeURIComponent(params.folder_id)}/test-cases`, |
| 150 | + body, |
| 151 | + { |
| 152 | + auth: { |
| 153 | + username: config.browserstackUsername, |
| 154 | + password: config.browserstackAccessKey, |
| 155 | + }, |
| 156 | + headers: { "Content-Type": "application/json" }, |
| 157 | + }, |
| 158 | + ); |
| 159 | + |
| 160 | + const { data } = response.data; |
| 161 | + if (!data.success) { |
| 162 | + return { |
| 163 | + content: [ |
| 164 | + { |
| 165 | + type: "text", |
| 166 | + text: `Failed to create test case: ${JSON.stringify( |
| 167 | + response.data, |
| 168 | + )}`, |
| 169 | + isError: true, |
| 170 | + }, |
| 171 | + ], |
| 172 | + isError: true, |
| 173 | + }; |
| 174 | + } |
| 175 | + |
| 176 | + const tc = data.test_case; |
| 177 | + return { |
| 178 | + content: [ |
| 179 | + { |
| 180 | + type: "text", |
| 181 | + text: `Successfully created test case ${tc.identifier}: ${tc.title}`, |
| 182 | + }, |
| 183 | + { type: "text", text: JSON.stringify(tc, null, 2) }, |
| 184 | + ], |
| 185 | + }; |
| 186 | + } catch (err) { |
| 187 | + // Delegate to our centralized Axios error formatter |
| 188 | + return formatAxiosError(err, "Failed to create test case"); |
| 189 | + } |
| 190 | +} |
0 commit comments