Skip to content

Commit 0c2a338

Browse files
committed
enhance Percy integration support
1 parent a2ed32e commit 0c2a338

File tree

6 files changed

+53
-31
lines changed

6 files changed

+53
-31
lines changed

src/tools/add-percy-snapshots.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async function updateTestsWithPercyCommands(args: {
1515
if (!filePaths) {
1616
throw new Error(`No test files found in memory for UUID: ${uuid}`);
1717
}
18+
1819
if (index < 0 || index >= filePaths.length) {
1920
throw new Error(
2021
`Invalid index: ${index}. There are ${filePaths.length} files for UUID: ${uuid}`,

src/tools/percy-snapshot-utils/utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function updateFileAndStep(
1818
type: "text",
1919
text: `Step 1 : You need to add percy snapshot commands in some key test cases in the file ${file} use the following instructions: \n${instruction}`,
2020
});
21-
21+
2222
content.push({
2323
type: "text",
2424
text: `Step 2 : Confirm that Percy snapshot commands have been added at all key points of visual change in the file ${file}.`,
@@ -30,5 +30,13 @@ export async function updateFileAndStep(
3030
text: `Step 3 : Call the tool updateTestFileWithInstructions with index as ${nextIndex} out of ${total}`,
3131
});
3232
}
33+
34+
if (nextIndex === total) {
35+
content.push({
36+
type: "text",
37+
text: `Step 4: Percy snapshot commands have been added to all files. You can now run the Percy build using the above command.`,
38+
});
39+
}
40+
3341
return content;
3442
}

src/tools/sdk-utils/common/constants.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ export const RUN_ON_BROWSERSTACK_DESCRIPTION =
88
"Set up and run automated web-based tests on BrowserStack using the BrowserStack SDK. Use this tool for functional or integration test setup on BrowserStack only. For any visual testing or Percy integration, use the dedicated Percy setup tool. Example prompts: run this test on browserstack; set up this project for browserstack.";
99

1010
export const SIMULATE_PERCY_CHANGE_DESCRIPTION =
11-
"This tool helps new users simulate a Percy visual change by guiding them to make a visible UI modification, rerun Percy, and verify that the change is detected.";
11+
"This tool helps new users simulate a Percy visual change by guiding them to make a visible UI modification, rerun Percy, and verify that the change is detected. Only Percy web is supported";
1212

1313
export const PERCY_REPLACE_REGEX =
14-
/MANDATORY: Aggressive, Exhaustive Percy Snapshot Integration[\s\S]*?Iteratively update every relevant test file in the directory and all subdirectories, adding Percy integration to each, one file at a time, until all are complete\./;
14+
/Invoke listTestFiles\(\) with the provided directories[\s\S]*?- DO NOT STOP until you add commands in all the files or you reach end of the files\./;
1515

1616
export const PERCY_SNAPSHOT_INSTRUCTION = `
1717
Invoke listTestFiles() with the provided directories from user to gather all test files in memory and obtain the generated UUID ---STEP---
18-
Process files in STRICT sequential order using tool addPercySnapshotCommands() with below instructions as param:
18+
Process files in STRICT sequential order using tool addPercySnapshotCommands() with below instructions:
1919
- Start with index 0
2020
- Then index 1
2121
- Then index 2
@@ -78,6 +78,8 @@ STEP 4: Run a second Percy build.
7878
7979
STEP 5: Compare the two Percy builds to see the detected visual difference.
8080
81+
STEP 6: Now ask user if they want to setup percy for full project coverage? If yes, call the tool "setupPercyVisualTesting" tool to enable complete coverage for the entire project.
82+
8183
CONSTRAINTS:
8284
- Do NOT run any builds until explicitly instructed in the steps.
8385
- Do NOT add new snapshot names—only use existing ones.

src/tools/sdk-utils/common/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,21 @@ Percy Mode: ${context.percyMode ?? "automate"}
108108
SDK Version: ${context.sdkVersion ?? "N/A"}
109109
Please open an issue on GitHub if the problem persists.`;
110110
}
111+
112+
export function percyUnsupportedResult(
113+
integrationType: PercyIntegrationTypeEnum,
114+
supportCheck?: { errorMessage?: string },
115+
): CallToolResult {
116+
const defaultMessage = `Percy ${integrationType} integration is not supported for this configuration.`;
117+
118+
return {
119+
content: [
120+
{
121+
type: "text",
122+
text: supportCheck?.errorMessage || defaultMessage,
123+
},
124+
],
125+
isError: true,
126+
shouldSkipFormatting: true,
127+
};
128+
}

src/tools/sdk-utils/handler.ts

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import {
22
SetUpPercySchema,
33
RunTestsOnBrowserStackSchema,
44
} from "./common/schema.js";
5-
import { getBootstrapFailedMessage } from "./common/utils.js";
5+
import {
6+
getBootstrapFailedMessage,
7+
percyUnsupportedResult,
8+
} from "./common/utils.js";
69
import { formatToolResult } from "./common/utils.js";
710
import { BrowserStackConfig } from "../../lib/types.js";
811
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
@@ -61,27 +64,23 @@ export async function setUpPercyHandler(
6164
folderPaths: input.folderPaths || [],
6265
};
6366

67+
// Check for Percy Web integration support
6468
if (input.integrationType === PercyIntegrationTypeEnum.WEB) {
6569
const supportCheck = checkPercyIntegrationSupport(percyInput);
6670
if (!supportCheck.supported) {
67-
return {
68-
content: [
69-
{
70-
type: "text",
71-
text:
72-
supportCheck.errorMessage ||
73-
"Percy Web integration is not supported for this configuration.",
74-
},
75-
],
76-
isError: true,
77-
shouldSkipFormatting: true,
78-
};
71+
return percyUnsupportedResult(
72+
PercyIntegrationTypeEnum.WEB,
73+
supportCheck,
74+
);
7975
}
76+
77+
// Fetch the Percy token
8078
const percyToken = await fetchPercyToken(
8179
input.projectName,
8280
authorization,
8381
{ type: PercyIntegrationTypeEnum.WEB },
8482
);
83+
8584
const result = runPercyWeb(percyInput, percyToken);
8685
return await formatToolResult(result);
8786
} else if (input.integrationType === PercyIntegrationTypeEnum.AUTOMATE) {
@@ -115,18 +114,10 @@ export async function setUpPercyHandler(
115114
integrationType: PercyIntegrationTypeEnum.AUTOMATE,
116115
});
117116
if (!supportCheck.supported) {
118-
return {
119-
content: [
120-
{
121-
type: "text",
122-
text:
123-
supportCheck.errorMessage ||
124-
"Percy Automate integration is not supported for this configuration.",
125-
},
126-
],
127-
isError: true,
128-
shouldSkipFormatting: true,
129-
};
117+
return percyUnsupportedResult(
118+
PercyIntegrationTypeEnum.AUTOMATE,
119+
supportCheck,
120+
);
130121
}
131122
// SDK setup instructions (for Automate, without Percy)
132123
const sdkInput = {
@@ -148,6 +139,7 @@ export async function setUpPercyHandler(
148139
percyInput,
149140
percyToken,
150141
);
142+
151143
// Combine steps: warning, SDK steps, Percy Automate steps
152144
const steps = [
153145
{
@@ -159,6 +151,8 @@ export async function setUpPercyHandler(
159151
...(sdkResult.steps || []),
160152
...(percyAutomateResult.steps || []),
161153
];
154+
155+
// Combine all steps into the final result
162156
return await formatToolResult({
163157
...percyAutomateResult,
164158
steps,

src/tools/sdk-utils/percy-web/constants.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,6 @@ class PercyPlaywrightExample
538538
\`\`\`
539539
`;
540540

541-
542541
export const pythonInstructions = `
543542
Install Percy dependencies
544543
- Install Percy CLI:
@@ -979,4 +978,4 @@ Run Percy with your tests
979978
npx percy exec -- dotnet test
980979
981980
${percyReviewSnapshotsStep}
982-
`;
981+
`;

0 commit comments

Comments
 (0)