|
| 1 | +import { z } from "zod"; |
| 2 | +import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 3 | +import axios from "axios"; |
| 4 | +import config from "../../config.js"; |
| 5 | +import { formatAxiosError } from "../../lib/error.js"; |
| 6 | +import { |
| 7 | + projectIdentifierToId, |
| 8 | + testCaseIdentifierToDetails, |
| 9 | +} from "./TCG-utils/api.js"; |
| 10 | +import { pollLCAStatus } from "./poll-lca-status.js"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Schema for creating LCA steps for a test case |
| 14 | + */ |
| 15 | +export const CreateLCAStepsSchema = z.object({ |
| 16 | + project_identifier: z |
| 17 | + .string() |
| 18 | + .describe("ID of the project (Starts with 'PR-')"), |
| 19 | + test_case_identifier: z |
| 20 | + .string() |
| 21 | + .describe("Identifier of the test case (e.g., 'TC-12345')"), |
| 22 | + base_url: z.string().describe("Base URL for the test (e.g., 'google.com')"), |
| 23 | + credentials: z |
| 24 | + .object({ |
| 25 | + username: z.string().describe("Username for authentication"), |
| 26 | + password: z.string().describe("Password for authentication"), |
| 27 | + }) |
| 28 | + .optional() |
| 29 | + .describe( |
| 30 | + "Optional credentials for authentication. Extract from the test case details if provided in it. This is required for the test cases which require authentication.", |
| 31 | + ), |
| 32 | + local_enabled: z |
| 33 | + .boolean() |
| 34 | + .optional() |
| 35 | + .default(false) |
| 36 | + .describe("Whether local testing is enabled"), |
| 37 | + test_name: z.string().describe("Name of the test"), |
| 38 | + test_case_details: z |
| 39 | + .object({ |
| 40 | + name: z.string().describe("Name of the test case"), |
| 41 | + description: z.string().describe("Description of the test case"), |
| 42 | + preconditions: z.string().describe("Preconditions for the test case"), |
| 43 | + test_case_steps: z |
| 44 | + .array( |
| 45 | + z.object({ |
| 46 | + step: z.string().describe("Test step description"), |
| 47 | + result: z.string().describe("Expected result"), |
| 48 | + }), |
| 49 | + ) |
| 50 | + .describe("Array of test case steps with expected results"), |
| 51 | + }) |
| 52 | + .describe("Test case details including steps"), |
| 53 | + wait_for_completion: z |
| 54 | + .boolean() |
| 55 | + .optional() |
| 56 | + .default(true) |
| 57 | + .describe("Whether to wait for LCA build completion (default: true)"), |
| 58 | +}); |
| 59 | + |
| 60 | +export type CreateLCAStepsArgs = z.infer<typeof CreateLCAStepsSchema>; |
| 61 | + |
| 62 | +/** |
| 63 | + * Creates LCA (Low Code Automation) steps for a test case in BrowserStack Test Management |
| 64 | + */ |
| 65 | +export async function createLCASteps( |
| 66 | + args: CreateLCAStepsArgs, |
| 67 | + context: any, |
| 68 | +): Promise<CallToolResult> { |
| 69 | + try { |
| 70 | + // Get the project ID from identifier |
| 71 | + const projectId = await projectIdentifierToId(args.project_identifier); |
| 72 | + |
| 73 | + // Get the test case ID and folder ID from identifier |
| 74 | + const { testCaseId, folderId } = await testCaseIdentifierToDetails( |
| 75 | + projectId, |
| 76 | + args.test_case_identifier, |
| 77 | + ); |
| 78 | + |
| 79 | + const url = `https://test-management.browserstack.com/api/v1/projects/${projectId}/test-cases/${testCaseId}/lcnc`; |
| 80 | + |
| 81 | + const payload = { |
| 82 | + base_url: args.base_url, |
| 83 | + credentials: args.credentials, |
| 84 | + local_enabled: args.local_enabled, |
| 85 | + test_name: args.test_name, |
| 86 | + test_case_details: args.test_case_details, |
| 87 | + version: "v2", |
| 88 | + webhook_path: `https://test-management.browserstack.com/api/v1/projects/${projectId}/test-cases/${testCaseId}/webhooks/lcnc`, |
| 89 | + }; |
| 90 | + |
| 91 | + const response = await axios.post(url, payload, { |
| 92 | + headers: { |
| 93 | + "API-TOKEN": `${config.browserstackUsername}:${config.browserstackAccessKey}`, |
| 94 | + accept: "application/json, text/plain, */*", |
| 95 | + "Content-Type": "application/json", |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + if (response.status >= 200 && response.status < 300) { |
| 100 | + // Check if user wants to wait for completion |
| 101 | + if (!args.wait_for_completion) { |
| 102 | + return { |
| 103 | + content: [ |
| 104 | + { |
| 105 | + type: "text", |
| 106 | + text: `LCA steps creation initiated for test case ${args.test_case_identifier} (ID: ${testCaseId})`, |
| 107 | + }, |
| 108 | + { |
| 109 | + type: "text", |
| 110 | + text: "LCA build started. Check the BrowserStack Lowcode Automation UI for completion status.", |
| 111 | + }, |
| 112 | + ], |
| 113 | + }; |
| 114 | + } |
| 115 | + |
| 116 | + // Start polling for LCA build completion |
| 117 | + try { |
| 118 | + const max_wait_minutes = 10; // Maximum wait time in minutes |
| 119 | + const maxWaitMs = max_wait_minutes * 60 * 1000; |
| 120 | + const lcaResult = await pollLCAStatus( |
| 121 | + projectId, |
| 122 | + folderId, |
| 123 | + testCaseId, |
| 124 | + context, |
| 125 | + maxWaitMs, // max wait time |
| 126 | + 2 * 60 * 1000, // 2 minutes initial wait |
| 127 | + 10 * 1000, // 10 seconds interval |
| 128 | + ); |
| 129 | + |
| 130 | + if (lcaResult && lcaResult.status === "done") { |
| 131 | + return { |
| 132 | + content: [ |
| 133 | + { |
| 134 | + type: "text", |
| 135 | + text: `Successfully created LCA steps for test case ${args.test_case_identifier} (ID: ${testCaseId})`, |
| 136 | + }, |
| 137 | + { |
| 138 | + type: "text", |
| 139 | + text: `LCA build completed! Resource URL: ${lcaResult.resource_path}`, |
| 140 | + }, |
| 141 | + ], |
| 142 | + }; |
| 143 | + } else { |
| 144 | + return { |
| 145 | + content: [ |
| 146 | + { |
| 147 | + type: "text", |
| 148 | + text: `LCA steps creation initiated for test case ${args.test_case_identifier} (ID: ${testCaseId})`, |
| 149 | + }, |
| 150 | + { |
| 151 | + type: "text", |
| 152 | + text: `Warning: LCA build did not complete within ${max_wait_minutes} minutes. You can check the status later in the BrowserStack Test Management UI.`, |
| 153 | + }, |
| 154 | + ], |
| 155 | + }; |
| 156 | + } |
| 157 | + } catch (pollError) { |
| 158 | + console.error("Error during LCA polling:", pollError); |
| 159 | + return { |
| 160 | + content: [ |
| 161 | + { |
| 162 | + type: "text", |
| 163 | + text: `LCA steps creation initiated for test case ${args.test_case_identifier} (ID: ${testCaseId})`, |
| 164 | + }, |
| 165 | + { |
| 166 | + type: "text", |
| 167 | + text: "Warning: Error occurred while polling for LCA build completion. Check the BrowserStack Test Management UI for status.", |
| 168 | + }, |
| 169 | + ], |
| 170 | + }; |
| 171 | + } |
| 172 | + } else { |
| 173 | + throw new Error(`Unexpected response: ${JSON.stringify(response.data)}`); |
| 174 | + } |
| 175 | + } catch (error) { |
| 176 | + // Add more specific error handling |
| 177 | + if (error instanceof Error) { |
| 178 | + if (error.message.includes("not found")) { |
| 179 | + return { |
| 180 | + content: [ |
| 181 | + { |
| 182 | + type: "text", |
| 183 | + text: `Error: ${error.message}. Please verify that the project identifier "${args.project_identifier}" and test case identifier "${args.test_case_identifier}" are correct.`, |
| 184 | + isError: true, |
| 185 | + }, |
| 186 | + ], |
| 187 | + isError: true, |
| 188 | + }; |
| 189 | + } |
| 190 | + } |
| 191 | + return formatAxiosError(error, "Failed to create LCA steps"); |
| 192 | + } |
| 193 | +} |
0 commit comments