Skip to content

Commit ba1c725

Browse files
authored
Merge pull request #1563 from argos-ci/variant-switch
feat: allow to switch between viewport & browser variants
2 parents 07e4cfb + 1932231 commit ba1c725

File tree

15 files changed

+641
-133
lines changed

15 files changed

+641
-133
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
22
"typescript.tsdk": "node_modules/typescript/lib",
3-
"typescript.enablePromptUseWorkspaceTsdk": true
3+
"typescript.enablePromptUseWorkspaceTsdk": true,
4+
"files.associations": {
5+
"*.css": "tailwindcss"
6+
}
47
}

apps/backend/src/graphql/__generated__/resolver-types.ts

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/backend/src/graphql/__generated__/schema.gql

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/backend/src/graphql/definitions/ScreenshotDiff.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
IScreenshotDiffResolvers,
99
IScreenshotDiffStatus,
1010
} from "../__generated__/resolver-types.js";
11+
import { getVariantKey } from "../services/variant-key.js";
1112

1213
const { gql } = gqlTag;
1314

@@ -30,8 +31,8 @@ export const typeDefs = gql`
3031
url: String
3132
"Name of the diff (either base or compare screenshot name)"
3233
name: String!
33-
"Base name of the diff, same for all retries"
34-
baseName: String!
34+
"Unique key to identify screenshot variant (browser, resolution, retries)"
35+
variantKey: String!
3536
width: Int
3637
height: Int
3738
status: ScreenshotDiffStatus!
@@ -103,25 +104,9 @@ export const resolvers: IResolvers = {
103104
return getPublicImageFileUrl(file);
104105
},
105106
name: nameResolver,
106-
baseName: async (...args) => {
107-
const [name, status] = await Promise.all([
108-
nameResolver(...args),
109-
statusResolver(...args),
110-
]);
111-
112-
if (
113-
status === IScreenshotDiffStatus.Failure ||
114-
status === IScreenshotDiffStatus.RetryFailure
115-
) {
116-
// Match ":name #num (failed).png"
117-
const match = name.match(/^(.*) #\d+ \(failed\)\.png$/);
118-
if (match && match[1]) {
119-
return match[1];
120-
}
121-
return name;
122-
}
123-
124-
return name;
107+
variantKey: async (...args) => {
108+
const name = await nameResolver(...args);
109+
return getVariantKey(name);
125110
},
126111
width: async (screenshotDiff, _args, ctx) => {
127112
if (!screenshotDiff.fileId) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { getVariantKey } from "./variant-key";
4+
5+
describe("#getVariantKey", () => {
6+
it("returns the variant key", async () => {
7+
expect(getVariantKey("chromium/role/edit/space-ui vw-375.png")).toBe(
8+
"role/edit/space-ui",
9+
);
10+
expect(getVariantKey("firefox/space-ui vw-375.png")).toBe("space-ui");
11+
expect(getVariantKey("safari/another/path vw-480.png")).toBe(
12+
"another/path",
13+
);
14+
expect(getVariantKey("chrome/role/edit/space-ui vw-720.png")).toBe(
15+
"role/edit/space-ui",
16+
);
17+
expect(getVariantKey("chrome/role/edit/space-ui.png")).toBe(
18+
"role/edit/space-ui",
19+
);
20+
});
21+
22+
it("handles failed variant keys", async () => {
23+
expect(getVariantKey("chromium/role/edit/space-ui #123 (failed).png")).toBe(
24+
"role/edit/space-ui",
25+
);
26+
expect(getVariantKey("firefox/space-ui #456 (failed).png")).toBe(
27+
"space-ui",
28+
);
29+
expect(getVariantKey("safari/another/path #789 (failed).png")).toBe(
30+
"another/path",
31+
);
32+
expect(getVariantKey("chrome/role/edit/space-ui #101 (failed).png")).toBe(
33+
"role/edit/space-ui",
34+
);
35+
});
36+
37+
it("handles variant keys without browser prefix", async () => {
38+
expect(getVariantKey("role/edit/space-ui vw-375.png")).toBe(
39+
"role/edit/space-ui",
40+
);
41+
expect(getVariantKey("space-ui vw-375.png")).toBe("space-ui");
42+
expect(getVariantKey("another/path vw-480.png")).toBe("another/path");
43+
expect(getVariantKey("role/edit/space-ui vw-720.png")).toBe(
44+
"role/edit/space-ui",
45+
);
46+
expect(getVariantKey("role/edit/space-ui.png")).toBe("role/edit/space-ui");
47+
});
48+
49+
it("handles variant keys without vw suffix", async () => {
50+
expect(getVariantKey("chromium/role/edit/space-ui.png")).toBe(
51+
"role/edit/space-ui",
52+
);
53+
expect(getVariantKey("firefox/space-ui.png")).toBe("space-ui");
54+
expect(getVariantKey("safari/another/path.png")).toBe("another/path");
55+
expect(getVariantKey("chrome/role/edit/space-ui.png")).toBe(
56+
"role/edit/space-ui",
57+
);
58+
});
59+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Generates a variant key by sanitizing the given name string.
3+
*
4+
* The function performs the following transformations:
5+
* 1. Removes the browser prefix (chromium, firefox, safari, chrome) followed by a slash.
6+
* 2. Removes any whitespace followed by "vw-" and digits, ending with ".png".
7+
* 3. Removes any occurrence of " #<digits> (failed).png".
8+
* 4. Removes the ".png" extension.
9+
*
10+
* @param name - The name string to be sanitized.
11+
* @returns The sanitized variant key.
12+
*/
13+
export function getVariantKey(name: string): string {
14+
return name
15+
.replace(/^(chromium|firefox|safari|chrome)\//, "")
16+
.replace(/\s+vw-\d+\.png$/, "")
17+
.replace(/ #\d+ \(failed\)\.png$/, "")
18+
.replace(/\.png$/, "")
19+
.trim();
20+
}

0 commit comments

Comments
 (0)