Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Path from "effect/Path";
6import sharp from "sharp";
7
8import { renderBeforeAfterComparison } from "./annotate.ts";
9import {
10 buildOverlayBoxes,
11 diffAuditCaptures,
12 isPlatformNoise,
13 renderMarkdownReport,
14 type AuditCapture,
15 type RawSnapshotData,
16} from "./diff.ts";
17
18describe("mobile-audit diff", () => {
19 it("filters iOS UISwitch frames, dictation chrome, and scroll-edge clipped rows", () => {
20 const viewport = { width: 402, height: 874 };
21 expect(
22 isPlatformNoise(
23 {
24 ref: "@e1",
25 kind: "small-target",
26 role: "switch",
27 label: "Enable cups",
28 detail: { unit: "pt", width: 63, height: 28, recommendedSize: 44 },
29 },
30 "ios",
31 viewport,
32 ),
33 ).toBe(true);
34
35 expect(
36 isPlatformNoise(
37 {
38 ref: "@e2",
39 kind: "ambiguous-name",
40 role: "button",
41 label: "Dictate",
42 identifier: "dictation",
43 },
44 "ios",
45 viewport,
46 ),
47 ).toBe(true);
48
49 expect(
50 isPlatformNoise(
51 {
52 ref: "@e3",
53 kind: "small-target",
54 role: "button",
55 label: "Bottom row",
56 rect: { x: 0, y: 860, width: 402, height: 14 },
57 },
58 "ios",
59 viewport,
60 ),
61 ).toBe(true);
62
63 expect(
64 isPlatformNoise(
65 {
66 ref: "@e4",
67 kind: "small-target",
68 role: "text-field",
69 label: "Address",
70 rect: { x: 111, y: 431, width: 260, height: 23 },
71 detail: { unit: "pt", width: 260, height: 23.3, recommendedSize: 44 },
72 },
73 "ios",
74 viewport,
75 ),
76 ).toBe(false);
77 });
78
79 it("classifies fixed, regressed, and unchanged findings and locates fixed after bounds", () => {
80 const before: AuditCapture = {
81 platform: "android",
82 screen: "connections-new",
83 viewport: { width: 1080, height: 2424 },
84 pixelDensity: 2.625,
85 findings: [
86 {
87 ref: "@e1",
88 kind: "small-target",
89 role: "text-field",
90 label: "Address",
91 rect: { x: 293, y: 1055, width: 707, height: 61 },
92 detail: { unit: "dp", width: 269.3, height: 23.2, recommendedSize: 48 },
93 },
94 {
95 ref: "@e2",
96 kind: "missing-name",
97 role: "button",
98 rect: { x: 11, y: 74, width: 126, height: 126 },
99 },
100 ],
101 };
102
103 const after: AuditCapture = {
104 platform: "android",
105 screen: "connections-new",
106 viewport: { width: 1080, height: 2424 },
107 pixelDensity: 2.625,
108 findings: [
109 {
110 ref: "@e2",
111 kind: "missing-name",
112 role: "button",
113 rect: { x: 11, y: 74, width: 126, height: 126 },
114 },
115 {
116 ref: "@e9",
117 kind: "small-target",
118 role: "button",
119 label: "Scan QR code",
120 rect: { x: 100, y: 400, width: 100, height: 90 },
121 detail: { unit: "dp", width: 38.1, height: 34.3, recommendedSize: 48 },
122 },
123 ],
124 };
125
126 const afterRaw: RawSnapshotData = {
127 viewport: { width: 1080, height: 2424 },
128 androidSnapshot: { pixelDensity: 2.625 },
129 nodes: [
130 {
131 index: 10,
132 type: "android.widget.EditText",
133 label: "Address",
134 rect: { x: 293, y: 1055, width: 707, height: 126 },
135 },
136 ],
137 };
138
139 const diff = diffAuditCaptures({ before, after, afterRaw });
140 expect(diff.fixed).toHaveLength(1);
141 expect(diff.fixed[0]?.before.label).toBe("Address");
142 expect(diff.fixed[0]?.afterMeasurement).toBe("48dp ✓");
143 expect(diff.regressions).toHaveLength(1);
144 expect(diff.regressions[0]?.label).toBe("Scan QR code");
145 expect(diff.unchanged).toHaveLength(1);
146
147 const { beforeBoxes, afterBoxes } = buildOverlayBoxes(diff, 2.625);
148 expect(beforeBoxes).toHaveLength(1);
149 expect(beforeBoxes[0]?.tag).toBe("23.2dp");
150 expect(afterBoxes).toHaveLength(2);
151 expect(afterBoxes.map((b) => b.tag)).toEqual(["48dp ✓", "NEW: 34.3dp"]);
152
153 const md = renderMarkdownReport([{ ...diff, imagePath: "android-connections-new.png" }]);
154 expect(md).toContain("**1 fixed**, **1 regressions**");
155 expect(md).toContain("");
156 });
157
158 it.effect("composites a side-by-side annotated PNG for Retina 3x iOS screenshots", () =>
159 Effect.gen(function* () {
160 const fs = yield* FileSystem.FileSystem;
161 const path = yield* Path.Path;
162 const tmp = yield* fs.makeTempDirectoryScoped({ prefix: "mobile-audit-test-" });
163 const beforePng = path.join(tmp, "before.png");
164 const afterPng = path.join(tmp, "after.png");
165 const outPng = path.join(tmp, "comparison.png");
166
167 const blankPng = yield* Effect.promise(() =>
168 sharp({
169 create: {
170 width: 1206,
171 height: 2622,
172 channels: 4,
173 background: { r: 255, g: 255, b: 255, alpha: 1 },
174 },
175 })
176 .png()
177 .toBuffer(),
178 );
179 yield* fs.writeFile(beforePng, blankPng);
180 yield* fs.writeFile(afterPng, blankPng);
181
182 const result = yield* renderBeforeAfterComparison({
183 beforePng,
184 beforeBoxes: [{ x: 111, y: 431, width: 260, height: 23, tag: "23.3pt", color: "#ef4444" }],
185 beforeTitle: "BEFORE (main)",
186 beforeViewport: { width: 402, height: 874 },
187 afterPng,
188 afterBoxes: [{ x: 111, y: 431, width: 260, height: 48, tag: "48pt ✓", color: "#16a34a" }],
189 afterTitle: "AFTER (PR)",
190 afterViewport: { width: 402, height: 874 },
191 outPath: outPng,
192 });
193
194 const outBytes = yield* fs.readFile(outPng);
195 const outMeta = yield* Effect.promise(() => sharp(outBytes).metadata());
196 expect(outMeta.format).toBe("png");
197 expect(outMeta.width).toBe(result.width);
198 expect(outMeta.height).toBe(result.height);
199 expect(outMeta.width).toBe(1128);
200 }).pipe(Effect.provide(NodeServices.layer)),
201 );
202});