|
| 1 | +import { z } from "zod"; |
| 2 | +import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 3 | +import { BrowserStackConfig } from "../../../lib/types.js"; |
| 4 | +import { getBrowserStackAuth } from "../../../lib/get-auth.js"; |
| 5 | +import { |
| 6 | + getAppUploadInstruction, |
| 7 | + validateSupportforAppAutomate, |
| 8 | + SupportedFramework, |
| 9 | +} from "./utils.js"; |
| 10 | + |
| 11 | +import { |
| 12 | + getAppSDKPrefixCommand, |
| 13 | + generateAppBrowserStackYMLInstructions, |
| 14 | +} from "./index.js"; |
| 15 | + |
| 16 | +import { |
| 17 | + AppSDKSupportedLanguage, |
| 18 | + AppSDKSupportedTestingFramework, |
| 19 | + AppSDKInstruction, |
| 20 | + formatAppInstructionsWithNumbers, |
| 21 | + getAppInstructionsForProjectConfiguration, |
| 22 | + SETUP_APP_AUTOMATE_SCHEMA, |
| 23 | +} from "./index.js"; |
| 24 | + |
| 25 | +export async function setupAppAutomateHandler( |
| 26 | + rawInput: unknown, |
| 27 | + config: BrowserStackConfig, |
| 28 | +): Promise<CallToolResult> { |
| 29 | + const input = z.object(SETUP_APP_AUTOMATE_SCHEMA).parse(rawInput); |
| 30 | + const auth = getBrowserStackAuth(config); |
| 31 | + const [username, accessKey] = auth.split(":"); |
| 32 | + |
| 33 | + const instructions: AppSDKInstruction[] = []; |
| 34 | + |
| 35 | + // Use variables for all major input properties |
| 36 | + const testingFramework = |
| 37 | + input.detectedTestingFramework as AppSDKSupportedTestingFramework; |
| 38 | + const language = input.detectedLanguage as AppSDKSupportedLanguage; |
| 39 | + const platforms = (input.desiredPlatforms as string[]) ?? ["android"]; |
| 40 | + const appPath = input.appPath as string; |
| 41 | + const framework = input.detectedFramework as SupportedFramework; |
| 42 | + |
| 43 | + //Validating if supported framework or not |
| 44 | + validateSupportforAppAutomate(framework, language, testingFramework); |
| 45 | + |
| 46 | + // Step 1: Generate SDK setup command |
| 47 | + const sdkCommand = getAppSDKPrefixCommand( |
| 48 | + language, |
| 49 | + testingFramework, |
| 50 | + username, |
| 51 | + accessKey, |
| 52 | + appPath, |
| 53 | + ); |
| 54 | + |
| 55 | + if (sdkCommand) { |
| 56 | + instructions.push({ content: sdkCommand, type: "setup" }); |
| 57 | + } |
| 58 | + |
| 59 | + // Step 2: Generate browserstack.yml configuration |
| 60 | + const configInstructions = generateAppBrowserStackYMLInstructions( |
| 61 | + platforms, |
| 62 | + username, |
| 63 | + accessKey, |
| 64 | + appPath, |
| 65 | + testingFramework, |
| 66 | + ); |
| 67 | + |
| 68 | + if (configInstructions) { |
| 69 | + instructions.push({ content: configInstructions, type: "config" }); |
| 70 | + } |
| 71 | + |
| 72 | + // Step 3: Generate app upload instruction |
| 73 | + const appUploadInstruction = await getAppUploadInstruction( |
| 74 | + appPath, |
| 75 | + username, |
| 76 | + accessKey, |
| 77 | + testingFramework, |
| 78 | + ); |
| 79 | + |
| 80 | + if (appUploadInstruction) { |
| 81 | + instructions.push({ content: appUploadInstruction, type: "setup" }); |
| 82 | + } |
| 83 | + |
| 84 | + // Step 4: Generate project configuration and run instructions |
| 85 | + const projectInstructions = getAppInstructionsForProjectConfiguration( |
| 86 | + framework, |
| 87 | + testingFramework, |
| 88 | + language, |
| 89 | + ); |
| 90 | + |
| 91 | + if (projectInstructions) { |
| 92 | + instructions.push({ content: projectInstructions, type: "run" }); |
| 93 | + } |
| 94 | + |
| 95 | + const combinedInstructions = instructions |
| 96 | + .map((instruction) => instruction.content) |
| 97 | + .join("\n\n"); |
| 98 | + |
| 99 | + return { |
| 100 | + content: [ |
| 101 | + { |
| 102 | + type: "text", |
| 103 | + text: formatAppInstructionsWithNumbers(combinedInstructions), |
| 104 | + isError: false, |
| 105 | + }, |
| 106 | + ], |
| 107 | + isError: false, |
| 108 | + }; |
| 109 | +} |
0 commit comments