|
| 1 | +import { apiClient } from "../../lib/apiClient.js"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 4 | +import { formatAxiosError } from "../../lib/error.js"; |
| 5 | +import { getBrowserStackAuth } from "../../lib/get-auth.js"; |
| 6 | +import { BrowserStackConfig } from "../../lib/types.js"; |
| 7 | +import { getTMBaseURL } from "../../lib/tm-base-url.js"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Schema for fetching a single test plan by identifier, including its linked test runs. |
| 11 | + */ |
| 12 | +export const GetTestPlanSchema = z.object({ |
| 13 | + project_identifier: z |
| 14 | + .string() |
| 15 | + .describe( |
| 16 | + "Identifier of the project (starts with PR- followed by a number).", |
| 17 | + ), |
| 18 | + test_plan_identifier: z |
| 19 | + .string() |
| 20 | + .describe( |
| 21 | + "Identifier of the test plan (starts with TP- followed by a number).", |
| 22 | + ), |
| 23 | +}); |
| 24 | + |
| 25 | +export type GetTestPlanArgs = z.infer<typeof GetTestPlanSchema>; |
| 26 | + |
| 27 | +interface TestPlan { |
| 28 | + identifier: string; |
| 29 | + name: string; |
| 30 | + active_state: string; |
| 31 | + description: string | null; |
| 32 | + project_id: string; |
| 33 | + start_date: string | null; |
| 34 | + end_date: string | null; |
| 35 | + created_at: string; |
| 36 | + test_runs_count?: { active: number; closed: number }; |
| 37 | + test_runs?: Array<{ identifier: string; name: string }>; |
| 38 | + links?: Record<string, string>; |
| 39 | +} |
| 40 | + |
| 41 | +interface LinkedTestRun { |
| 42 | + identifier: string; |
| 43 | + name: string; |
| 44 | + run_state: string; |
| 45 | + active_state: string; |
| 46 | + assignee?: string | null; |
| 47 | + description?: string | null; |
| 48 | + created_at: string; |
| 49 | + project_id: string; |
| 50 | + test_cases_count: number; |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Fetches a test plan by identifier and its linked test runs, returning a unified view |
| 55 | + * suitable for generating documentation (metadata + linked runs + status summary + case count). |
| 56 | + */ |
| 57 | +export async function getTestPlan( |
| 58 | + args: GetTestPlanArgs, |
| 59 | + config: BrowserStackConfig, |
| 60 | +): Promise<CallToolResult> { |
| 61 | + try { |
| 62 | + const tmBaseUrl = await getTMBaseURL(config); |
| 63 | + const projectId = encodeURIComponent(args.project_identifier); |
| 64 | + const planId = encodeURIComponent(args.test_plan_identifier); |
| 65 | + |
| 66 | + const authString = getBrowserStackAuth(config); |
| 67 | + const [username, password] = authString.split(":"); |
| 68 | + const authHeader = |
| 69 | + "Basic " + Buffer.from(`${username}:${password}`).toString("base64"); |
| 70 | + |
| 71 | + const planResp = await apiClient.get({ |
| 72 | + url: `${tmBaseUrl}/api/v2/projects/${projectId}/test-plans/${planId}`, |
| 73 | + headers: { Authorization: authHeader }, |
| 74 | + }); |
| 75 | + |
| 76 | + if (!planResp.data?.success) { |
| 77 | + return { |
| 78 | + content: [ |
| 79 | + { |
| 80 | + type: "text", |
| 81 | + text: `Failed to fetch test plan: ${JSON.stringify(planResp.data)}`, |
| 82 | + }, |
| 83 | + ], |
| 84 | + isError: true, |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + const plan: TestPlan = planResp.data.test_plan; |
| 89 | + |
| 90 | + const runsResp = await apiClient.get({ |
| 91 | + url: `${tmBaseUrl}/api/v2/projects/${projectId}/test-plans/${planId}/test-runs`, |
| 92 | + headers: { Authorization: authHeader }, |
| 93 | + }); |
| 94 | + |
| 95 | + const runs: LinkedTestRun[] = runsResp.data?.success |
| 96 | + ? (runsResp.data.test_runs ?? []) |
| 97 | + : []; |
| 98 | + |
| 99 | + const statusSummary: Record<string, number> = {}; |
| 100 | + let totalCases = 0; |
| 101 | + for (const run of runs) { |
| 102 | + statusSummary[run.run_state] = (statusSummary[run.run_state] ?? 0) + 1; |
| 103 | + totalCases += run.test_cases_count ?? 0; |
| 104 | + } |
| 105 | + |
| 106 | + const header = [ |
| 107 | + `Test Plan ${plan.identifier}: ${plan.name}`, |
| 108 | + `Status: ${plan.active_state}`, |
| 109 | + plan.description ? `Description: ${plan.description}` : null, |
| 110 | + plan.start_date || plan.end_date |
| 111 | + ? `Dates: ${plan.start_date ?? "—"} → ${plan.end_date ?? "—"}` |
| 112 | + : null, |
| 113 | + `Linked runs: ${runs.length} (plan counts — active ${plan.test_runs_count?.active ?? 0} / closed ${plan.test_runs_count?.closed ?? 0})`, |
| 114 | + `Total test cases across runs: ${totalCases}`, |
| 115 | + Object.keys(statusSummary).length > 0 |
| 116 | + ? `Run-state breakdown: ${Object.entries(statusSummary) |
| 117 | + .map(([s, n]) => `${s}=${n}`) |
| 118 | + .join(", ")}` |
| 119 | + : null, |
| 120 | + ] |
| 121 | + .filter(Boolean) |
| 122 | + .join("\n"); |
| 123 | + |
| 124 | + const runsBlock = runs.length |
| 125 | + ? "\n\nLinked test runs:\n" + |
| 126 | + runs |
| 127 | + .map( |
| 128 | + (r) => |
| 129 | + `• ${r.identifier}: ${r.name} [${r.run_state}] — ${r.test_cases_count} case(s)${r.assignee ? ` (assignee: ${r.assignee})` : ""}`, |
| 130 | + ) |
| 131 | + .join("\n") |
| 132 | + : "\n\nNo test runs linked to this plan."; |
| 133 | + |
| 134 | + return { |
| 135 | + content: [ |
| 136 | + { type: "text", text: header + runsBlock }, |
| 137 | + { |
| 138 | + type: "text", |
| 139 | + text: JSON.stringify( |
| 140 | + { |
| 141 | + test_plan: plan, |
| 142 | + linked_test_runs: runs, |
| 143 | + status_summary: statusSummary, |
| 144 | + total_test_cases: totalCases, |
| 145 | + }, |
| 146 | + null, |
| 147 | + 2, |
| 148 | + ), |
| 149 | + }, |
| 150 | + ], |
| 151 | + }; |
| 152 | + } catch (err) { |
| 153 | + return formatAxiosError(err, "Failed to fetch test plan"); |
| 154 | + } |
| 155 | +} |
0 commit comments