-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathcreate-testcase.ts
More file actions
232 lines (216 loc) · 6.68 KB
/
Copy pathcreate-testcase.ts
File metadata and controls
232 lines (216 loc) · 6.68 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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";
interface TestCaseStep {
step: string;
result: string;
}
interface IssueTracker {
name: string;
host: string;
}
export interface TestCaseCreateRequest {
project_identifier: string;
folder_id: string;
name: string;
description?: string;
owner?: string;
preconditions?: string;
test_case_steps: TestCaseStep[];
test_case_bdd?: { feature: string; scenario: string; background?: string };
issues?: string[];
issue_tracker?: IssueTracker;
tags?: string[];
custom_fields?: Record<string, string>;
automation_status?: string;
}
export interface TestCaseResponse {
data: {
success: boolean;
test_case: {
case_type: string;
priority: string;
status: string;
folder_id: number;
issues: Array<{
jira_id: string;
issue_type: string;
}>;
tags: string[];
template: string;
description: string;
preconditions: string;
title: string;
identifier: string;
automation_status: string;
owner: string;
steps: TestCaseStep[];
custom_fields: Array<{
name: string;
value: string;
}>;
};
};
}
export const CreateTestCaseSchema = z.object({
project_identifier: z
.string()
.describe(
"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.",
),
folder_id: z
.string()
.describe(
"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.",
),
name: z.string().describe("Name of the test case."),
description: z
.string()
.optional()
.describe("Brief description of the test case."),
owner: z
.string()
.email()
.describe("Email of the test case owner.")
.optional(),
preconditions: z
.string()
.optional()
.describe("Any preconditions (HTML allowed)."),
test_case_steps: z
.array(
z.object({
step: z.string().describe("Action to perform in this step."),
result: z.string().describe("Expected result of this step."),
}),
)
.describe("List of steps and expected results."),
test_case_bdd: z
.object({
feature: z.string().describe("Feature description for BDD/Gherkin test cases."),
scenario: z.string().describe("Gherkin scenario with Given/When/Then steps."),
background: z.string().optional().describe("Optional background steps shared across scenarios."),
})
.optional()
.describe("BDD/Gherkin template fields. Mutually exclusive with test_case_steps."),
issues: z
.array(z.string())
.optional()
.describe(
"List of the linked Jira, Asana or Azure issues ID's. This should be strictly in array format not the string of json.",
),
issue_tracker: z
.object({
name: z
.string()
.describe(
"Issue tracker name, For example, use jira for Jira, azure for Azure DevOps, or asana for Asana.",
),
host: z.string().url().describe("Base URL of the issue tracker."),
})
.optional(),
tags: z
.array(z.string())
.optional()
.describe(
"Tags to attach to the test case. This should be strictly in array format not the string of json",
),
custom_fields: z
.record(z.string(), z.string())
.optional()
.describe("Map of custom field names to values."),
automation_status: z
.string()
.optional()
.describe(
"Automation status of the test case. Common values include 'not_automated', 'automated', 'automation_not_required'.",
),
});
export function sanitizeArgs(args: any) {
const cleaned = { ...args };
if (cleaned.description === null) delete cleaned.description;
if (cleaned.owner === null) delete cleaned.owner;
if (cleaned.preconditions === null) delete cleaned.preconditions;
if (cleaned.automation_status === null) delete cleaned.automation_status;
if (cleaned.issue_tracker) {
if (
cleaned.issue_tracker.name === undefined ||
cleaned.issue_tracker.host === undefined
) {
delete cleaned.issue_tracker;
}
}
return cleaned;
}
import { getBrowserStackAuth } from "../../lib/get-auth.js";
export async function createTestCase(
params: TestCaseCreateRequest,
config: BrowserStackConfig,
): Promise<CallToolResult> {
const { test_case_bdd, ...rest } = params;
const testCase: Record<string, any> = { ...rest };
if (test_case_bdd) {
testCase.template = "test_case_bdd";
testCase.feature = test_case_bdd.feature;
testCase.scenario = test_case_bdd.scenario;
if (test_case_bdd.background) testCase.background = test_case_bdd.background;
}
const body = { test_case: testCase };
const authString = getBrowserStackAuth(config);
const [username, password] = authString.split(":");
try {
const tmBaseUrl = await getTMBaseURL(config);
const response = await apiClient.post({
url: `${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
params.project_identifier,
)}/folders/${encodeURIComponent(params.folder_id)}/test-cases`,
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 create test case: ${JSON.stringify(
response.data,
)}`,
},
],
isError: true,
};
}
const tc = data.test_case;
const projectId = await projectIdentifierToId(
params.project_identifier,
config,
);
return {
content: [
{
type: "text",
text: `Test case successfully created:
- Identifier: ${tc.identifier}
- Title: ${tc.title}
You can view it here: ${tmBaseUrl}/projects/${projectId}/folder/search?q=${tc.identifier}`,
},
{
type: "text",
text: JSON.stringify(tc, null, 2),
},
],
};
} catch (err) {
// Delegate to our centralized Axios error formatter
return formatAxiosError(err, "Failed to create test case");
}
}