-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathupdate-testcase.ts
More file actions
203 lines (181 loc) · 6.02 KB
/
update-testcase.ts
File metadata and controls
203 lines (181 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { apiClient } from "../../lib/apiClient.js";
import { z } from "zod";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { formatAxiosError } from "../../lib/error.js";
import { projectIdentifierToId } from "./TCG-utils/api.js";
import { BrowserStackConfig } from "../../lib/types.js";
import { getTMBaseURL } from "../../lib/tm-base-url.js";
import { getBrowserStackAuth } from "../../lib/get-auth.js";
import logger from "../../logger.js";
export interface TestCaseUpdateRequest {
project_identifier: string;
test_case_identifier: string;
name?: string;
description?: string;
preconditions?: string;
test_case_steps?: Array<{
step: string;
result: string;
}>;
test_case_bdd?: { feature: string; scenario: string; background?: string };
}
export const UpdateTestCaseSchema = z.object({
project_identifier: z
.string()
.describe(
"The ID of the BrowserStack project containing the test case to update.",
),
test_case_identifier: z
.string()
.describe(
"The ID of the test case to update. This can be found using the listTestCases tool.",
),
name: z.string().optional().describe("Updated name of the test case."),
description: z
.string()
.optional()
.describe("Updated brief description of the test case."),
preconditions: z
.string()
.optional()
.describe("Updated preconditions for the test case."),
test_case_steps: z
.array(
z.object({
step: z.string().describe("The action to perform in this step."),
result: z.string().describe("The expected result of this step."),
}),
)
.optional()
.describe("Updated list of test case steps with expected results."),
test_case_bdd: z
.object({
feature: z.string().describe("Updated feature description for BDD test cases."),
scenario: z.string().describe("Updated Gherkin scenario with Given/When/Then steps."),
background: z.string().optional().describe("Updated background steps for BDD test cases."),
})
.optional()
.describe("BDD/Gherkin template fields. Mutually exclusive with test_case_steps."),
});
/**
* Updates an existing test case in BrowserStack Test Management.
*/
export async function updateTestCase(
params: TestCaseUpdateRequest,
config: BrowserStackConfig,
): Promise<CallToolResult> {
const authString = getBrowserStackAuth(config);
const [username, password] = authString.split(":");
// Build the request body with only the fields to update
const testCaseBody: any = {};
if (params.name !== undefined) {
testCaseBody.name = params.name;
}
if (params.description !== undefined) {
testCaseBody.description = params.description;
}
if (params.preconditions !== undefined) {
testCaseBody.preconditions = params.preconditions;
}
if (params.test_case_steps !== undefined) {
testCaseBody.steps = params.test_case_steps;
}
if (params.test_case_bdd !== undefined) {
testCaseBody.template = "test_case_bdd";
testCaseBody.feature = params.test_case_bdd.feature;
testCaseBody.scenario = params.test_case_bdd.scenario;
if (params.test_case_bdd.background) testCaseBody.background = params.test_case_bdd.background;
}
const body = { test_case: testCaseBody };
try {
const tmBaseUrl = await getTMBaseURL(config);
const response = await apiClient.patch({
url: `${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
params.project_identifier,
)}/test-cases/${encodeURIComponent(params.test_case_identifier)}`,
headers: {
"Content-Type": "application/json",
Authorization:
"Basic " + Buffer.from(`${username}:${password}`).toString("base64"),
},
body,
});
const { data } = response.data;
if (!data.success) {
return {
content: [
{
type: "text",
text: `Failed to update test case: ${JSON.stringify(
response.data,
)}`,
},
],
isError: true,
};
}
const tc = data.test_case;
// Convert project identifier to project ID for dashboard URL
const projectId = await projectIdentifierToId(
params.project_identifier,
config,
);
return {
content: [
{
type: "text",
text: `Test case successfully updated:
**Test Case Details:**
- **ID**: ${tc.identifier}
- **Name**: ${tc.title}
- **Description**: ${tc.description || "N/A"}
- **Case Type**: ${tc.case_type}
- **Priority**: ${tc.priority}
- **Status**: ${tc.status}
**View on BrowserStack Dashboard:**
https://test-management.browserstack.com/projects/${projectId}/folders/${tc.folder_id}/test-cases/${tc.identifier}
The test case has been updated successfully and is now available in your BrowserStack Test Management project.`,
},
],
};
} catch (err: any) {
logger.error("Failed to update test case: %s", err);
logger.error(
"Error details:",
JSON.stringify(err.response?.data || err.message),
);
if (err.response?.status === 404) {
return {
content: [
{
type: "text",
text: `Test case not found. Please verify the project_identifier ("${params.project_identifier}") and test_case_identifier ("${params.test_case_identifier}") are correct. Make sure to use actual values, not placeholders like "your_project_id".
Error details: ${JSON.stringify(err.response?.data || err.message)}`,
},
],
isError: true,
};
}
if (err.response?.status === 403) {
return {
content: [
{
type: "text",
text: "Access denied. You don't have permission to update this test case.",
},
],
isError: true,
};
}
const errorMessage = formatAxiosError(err, "Failed to update test case");
return {
content: [
{
type: "text",
text: `Failed to update test case: ${errorMessage}. Please verify your credentials and try again.`,
},
],
isError: true,
};
}
}