-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathget-testcase.ts
More file actions
123 lines (108 loc) · 3.2 KB
/
get-testcase.ts
File metadata and controls
123 lines (108 loc) · 3.2 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
import { apiClient } from "../../lib/apiClient.js";
import { z } from "zod";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import { formatAxiosError } from "../../lib/error.js";
import { getBrowserStackAuth } from "../../lib/get-auth.js";
import { BrowserStackConfig } from "../../lib/types.js";
import { getTMBaseURL } from "../../lib/tm-base-url.js";
/**
* Schema for getting a specific test case by ID.
*/
export const GetTestCaseSchema = z.object({
project_identifier: z
.string()
.describe(
"Identifier of the project to fetch the test case from. This id starts with a PR- and is followed by a number.",
),
test_case_id: z
.string()
.describe(
"Identifier of the test case to fetch (e.g., TC-16667 or 2). Multiple IDs can be provided separated by commas.",
),
});
export type GetTestCaseArgs = z.infer<typeof GetTestCaseSchema>;
/**
* Calls BrowserStack Test Management to get a specific test case by ID.
*/
export async function getTestCase(
args: GetTestCaseArgs,
config: BrowserStackConfig,
): Promise<CallToolResult> {
try {
const tmBaseUrl = await getTMBaseURL(config);
const url = `${tmBaseUrl}/api/v2/projects/${encodeURIComponent(
args.project_identifier,
)}/test-cases?id=${encodeURIComponent(args.test_case_id)}`;
const authString = getBrowserStackAuth(config);
const [username, password] = authString.split(":");
const resp = await apiClient.get({
url,
headers: {
Authorization:
"Basic " + Buffer.from(`${username}:${password}`).toString("base64"),
},
});
const resp_data = resp.data;
if (!resp_data.success) {
return {
content: [
{
type: "text",
text: `Failed to get test case: ${JSON.stringify(resp_data)}`,
},
],
isError: true,
};
}
const { test_cases } = resp_data;
if (!test_cases || test_cases.length === 0) {
return {
content: [
{
type: "text",
text: `No test case found with ID: ${args.test_case_id}`,
},
],
isError: true,
};
}
const tc = test_cases[0];
// Format steps if present
let stepsText = "";
if (tc.steps && tc.steps.length > 0) {
stepsText = tc.steps
.map(
(step: any, index: number) =>
`${index + 1}. ${step.step}\n Result: ${step.result}`,
)
.join("\n\n");
}
const summary = `Test Case: ${tc.identifier}
Title: ${tc.title}
Type: ${tc.case_type}
Priority: ${tc.priority}
Status: ${tc.status}
Automation: ${tc.automation_status}
Owner: ${tc.owner || "Unassigned"}
Description:
${tc.description || "N/A"}
Preconditions:
${tc.preconditions || "N/A"}
Steps:${stepsText ? "\n" + stepsText : " None"}
URL: https://test-management.browserstack.com/projects/${args.project_identifier}/folders/${tc.folder_id}/test-cases/${tc.identifier}`;
return {
content: [
{
type: "text",
text: summary,
},
{
type: "text",
text: JSON.stringify(tc, null, 2),
},
],
};
} catch (err) {
return formatAxiosError(err, "Failed to get test case");
}
}