@geonosis/visual-diff 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +159 -0
- package/bin/geonosis-visual-diff.mjs +4 -0
- package/dist/chunk-AZ4XL3H5.js +171 -0
- package/dist/cli.js +134 -0
- package/dist/index.js +38 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# `@geonosis/visual-diff`
|
|
2
|
+
|
|
3
|
+
Pixel comparison, reporting and baseline management for visual regression. The union of the two
|
|
4
|
+
forks dielime and during.day grew separately, measured in
|
|
5
|
+
[`docs/visual-diff-inventory-2026-08-30.md`](../../docs/visual-diff-inventory-2026-08-30.md) and
|
|
6
|
+
proved equivalent to both in [`proofs/022-W2-parity`](../../proofs/022-W2-parity/README.md).
|
|
7
|
+
|
|
8
|
+
**Capture is not in here.** Nothing in this package launches a browser, sets a viewport or takes a
|
|
9
|
+
screenshot — that belongs to the repo that knows what its pages are. Dependencies are `pixelmatch`
|
|
10
|
+
and `pngjs`, and nothing else.
|
|
11
|
+
|
|
12
|
+
## The threshold, which is the whole contract
|
|
13
|
+
|
|
14
|
+
`threshold` is pixelmatch's per-pixel colour tolerance, 0–1, and smaller is stricter.
|
|
15
|
+
|
|
16
|
+
- **`threshold: 0` is strict per-pixel equality.** A single pixel whose blue channel moved by one
|
|
17
|
+
unit is counted. This is what a parity claim — "these two renders are the same" — has to be
|
|
18
|
+
measured at, and it is what during.day's `CLAUDE.md` ARTICLE VI requires of a mode-parity claim.
|
|
19
|
+
- **`threshold: 0.1` (the default) tolerates anti-aliasing.** It absorbs the sub-pixel noise two
|
|
20
|
+
renders of one layout produce on different machines, and it is pixelmatch's own default.
|
|
21
|
+
|
|
22
|
+
Both halves are pinned by a test that plants exactly one pixel one channel off and watches it count
|
|
23
|
+
at `0` and vanish at `0.1` (`src/compare.test.ts`, "the threshold"). The same contract is shown on
|
|
24
|
+
real frames in the proof: during.day's `226-selection` control pair differs by 24 pixels at `0` and
|
|
25
|
+
by none at `0.1`.
|
|
26
|
+
|
|
27
|
+
Anti-aliased pixels are excluded by pixelmatch's own slope detector regardless of threshold; pass
|
|
28
|
+
`includeAA: true` to count them.
|
|
29
|
+
|
|
30
|
+
`mismatchRatio` is a **different knob**: the fraction of a frame allowed to differ before the frame
|
|
31
|
+
fails, default `0.005`. `threshold` decides whether ONE pixel counts; `mismatchRatio` decides how
|
|
32
|
+
many counted pixels a frame may carry. Do not conflate them.
|
|
33
|
+
|
|
34
|
+
## The library
|
|
35
|
+
|
|
36
|
+
### `compare` and `formatReport` — files in, a verdict out
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { compare, formatReport } from '@geonosis/visual-diff'
|
|
40
|
+
|
|
41
|
+
const result = await compare({
|
|
42
|
+
image1: 'actual.png',
|
|
43
|
+
image2: 'expected.png',
|
|
44
|
+
options: { threshold: 0, outputPath: 'DIFF.png' },
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
console.log(formatReport(result))
|
|
48
|
+
console.log(result.identical, result.differentPixels, result.diffPercent, result.verdict)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`diffPercent` is a **percentage** (0–100). `verdict` bands it: `IDENTICAL` at exactly 0,
|
|
52
|
+
`NEAR_IDENTICAL` under 0.01 %, `SIMILAR` under 0.1 %, `MINOR_DIFF` under 1 %, `SIGNIFICANT_DIFF`
|
|
53
|
+
at or above. `outputPath` writes the rendered diff PNG, creating its directory.
|
|
54
|
+
|
|
55
|
+
`compare` **throws** on a missing input (`Image not found: <path>`) and on a size mismatch
|
|
56
|
+
(`Dimension mismatch: Image1 is WxH, Image2 is WxH`). Two different sizes means the question was
|
|
57
|
+
wrong, not the answer.
|
|
58
|
+
|
|
59
|
+
### `comparePng` — buffers in, an outcome out
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
import { comparePng, isWithinBudget } from '@geonosis/visual-diff'
|
|
63
|
+
|
|
64
|
+
const result = comparePng(baselineBuffer, actualBuffer, { threshold: 0.1 })
|
|
65
|
+
if (!isWithinBudget(result, 0.005)) fail(result.mismatch, result.ratio)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Synchronous, no fs. A size mismatch is **reported** here rather than thrown —
|
|
69
|
+
`dimensionsMatch: false`, `ratio: 1`, `diff: null` — so a 75-frame run gets an outcome per frame
|
|
70
|
+
instead of ending on frame 3. `ratio` is a fraction (0–1), not a percentage.
|
|
71
|
+
|
|
72
|
+
### `reconcileFrame` and `describeOutcome` — the baseline store
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { describeOutcome, reconcileFrame } from '@geonosis/visual-diff'
|
|
76
|
+
|
|
77
|
+
const outcome = reconcileFrame('home-xl.png', screenshot, {
|
|
78
|
+
baselineDir: 'e2e/visual/baselines',
|
|
79
|
+
artifactsDir: 'test-results/visual-artifacts',
|
|
80
|
+
}, { update: process.env['VISUAL_UPDATE'] === '1' })
|
|
81
|
+
|
|
82
|
+
expect(outcome.status, describeOutcome(outcome)).toBe('pass')
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Status is `created` or `updated` in update mode; otherwise `missing-baseline` (never a silent pass),
|
|
86
|
+
`pass` or `fail`. A failure leaves `<name>.actual.png` and `<name>.diff.png` under `artifactsDir`.
|
|
87
|
+
|
|
88
|
+
### `baselineName`, `breakpointsFor`, `DEFAULT_BREAKPOINTS`
|
|
89
|
+
|
|
90
|
+
`baselineName('home', 'xl') === 'home-xl.png'`. `breakpointsFor(target)` returns the target's own
|
|
91
|
+
grid or a fallback — `DEFAULT_BREAKPOINTS` (375/640/768/1024/1280 at 1600) unless you pass your own
|
|
92
|
+
as the second argument. **There is no list of pages here.** What a repo captures is that repo's
|
|
93
|
+
data (law 6); keep your `VISUAL_TARGETS` in your app and pass the `VisualTarget` shape through.
|
|
94
|
+
|
|
95
|
+
## The bin
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
geonosis-visual-diff compare <actual> <expected> [--threshold <0..1>] [--out <path>] [--include-aa]
|
|
99
|
+
geonosis-visual-diff baseline <name> <actual> [--dir <path>] [--artifacts <path>] [--update]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
| Exit | Means |
|
|
103
|
+
|---|---|
|
|
104
|
+
| `0` | the frames are equal, or the baseline was recorded |
|
|
105
|
+
| `1` | they differ — a real answer |
|
|
106
|
+
| `2` | refused: a missing file, two different sizes, a `--threshold` that is not a number in 0..1, an unknown command |
|
|
107
|
+
|
|
108
|
+
`baseline` keeps one PNG per name under `--dir` (default `./visual-baselines`), so one name is one
|
|
109
|
+
surface-at-one-theme-at-one-breakpoint. Without `--update` a differing or absent baseline exits 1
|
|
110
|
+
and prints the baseline's path; with `--update` it is written.
|
|
111
|
+
|
|
112
|
+
The name is taken **whole and never sanitised**, which is what makes `home-xl@die-lime` usable as a
|
|
113
|
+
per-theme name — pinned by a test planted against an `@`-stripping regression. It matters because a
|
|
114
|
+
store keyed by surface alone overwrites one brand's frame with the next one's and then reports
|
|
115
|
+
`pass`, which would require two brands to render identically. The recipe for a themed kit's walk is
|
|
116
|
+
in [`docs/themekit-visual-and-switcher.md`](../../docs/themekit-visual-and-switcher.md).
|
|
117
|
+
|
|
118
|
+
A tool that answers "different" when it could not read the file teaches you to distrust
|
|
119
|
+
"different" — hence the third code.
|
|
120
|
+
|
|
121
|
+
## Migrating
|
|
122
|
+
|
|
123
|
+
**Status, by D-027:** a package is done when every consumer depends on the PUBLISHED version and has
|
|
124
|
+
deleted its copy. The extraction is complete and proved; **neither consumer has adopted yet**. What
|
|
125
|
+
follows is the instruction for that work, not a record of it.
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
### during.day — five lines
|
|
129
|
+
|
|
130
|
+
1. `bun remove @during/visual-diff` and delete `packages/visual-diff`; add `@geonosis/visual-diff`.
|
|
131
|
+
2. Change the specifier in `apps/web/scripts/visual-diff.ts` and in both
|
|
132
|
+
`__tests__/e2e/.../tests.ts` files. **No call and no assertion changes** — `compare` and
|
|
133
|
+
`formatReport` keep their exact semantics, proved field by field.
|
|
134
|
+
3. Drop `compareUrls`: it has no caller in the repo and it is the only reason `playwright` was a
|
|
135
|
+
dependency of a comparison package. `scripts/visual-diff.ts` already does its own capture.
|
|
136
|
+
4. Keep `scripts/visual-diff.ts`'s own `process.exit` policy if you want `MINOR_DIFF` to pass — the
|
|
137
|
+
new bin is stricter (any difference exits 1) and is a separate tool, not a replacement.
|
|
138
|
+
5. ARTICLE VI still holds word for word: `compare` + `formatReport`, `threshold: 0` for a parity
|
|
139
|
+
claim, `0.1` for anti-aliasing.
|
|
140
|
+
|
|
141
|
+
### dielime — five lines
|
|
142
|
+
|
|
143
|
+
1. Delete `packages/visual-diff`; add `@geonosis/visual-diff` to `apps/web`.
|
|
144
|
+
2. Move `VISUAL_TARGETS` (and `manifest.test.ts`) into `apps/web/e2e/visual/manifest.ts` — 38
|
|
145
|
+
storefront routes are dielime's data, not the kit's — importing the `VisualTarget` and
|
|
146
|
+
`Breakpoint` types from the kit.
|
|
147
|
+
3. Change the specifier in `apps/web/e2e/visual/visual-regression.spec.ts`; `reconcileFrame`,
|
|
148
|
+
`describeOutcome`, `baselineName`, `breakpointsFor` and `BaselineDirs` are unchanged, and
|
|
149
|
+
`baseline.test.ts` + `compare.test.ts` pass against the kit unmodified.
|
|
150
|
+
4. `BREAKPOINTS` → `DEFAULT_BREAKPOINTS`, which is the same five widths at the same height.
|
|
151
|
+
5. `CompareOptions.pixelThreshold` → `threshold` and `DEFAULT_PIXEL_THRESHOLD` →
|
|
152
|
+
`DEFAULT_THRESHOLD`. No caller passes either. The `NO BASELINE` message no longer names
|
|
153
|
+
`VISUAL_UPDATE=1`; it names the baseline path instead. Nothing asserts on it.
|
|
154
|
+
|
|
155
|
+
## Known hazard
|
|
156
|
+
|
|
157
|
+
`formatReport` and the `analysis` strings call `Number.toLocaleString()`, inherited verbatim from
|
|
158
|
+
during.day. On a machine with a different default locale the thousands separator differs. This was
|
|
159
|
+
preserved rather than fixed so that the parity proof means what it says; it wants its own ticket.
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// src/compare.ts
|
|
2
|
+
import pixelmatch from "pixelmatch";
|
|
3
|
+
import { PNG } from "pngjs";
|
|
4
|
+
var DEFAULT_THRESHOLD = 0.1;
|
|
5
|
+
var DEFAULT_MISMATCH_RATIO = 5e-3;
|
|
6
|
+
function comparePng(baseline, actual, options = {}) {
|
|
7
|
+
const before = PNG.sync.read(baseline);
|
|
8
|
+
const after = PNG.sync.read(actual);
|
|
9
|
+
const total = before.width * before.height;
|
|
10
|
+
const dimensions = {
|
|
11
|
+
actualHeight: after.height,
|
|
12
|
+
actualWidth: after.width,
|
|
13
|
+
height: before.height,
|
|
14
|
+
total,
|
|
15
|
+
width: before.width
|
|
16
|
+
};
|
|
17
|
+
if (before.width !== after.width || before.height !== after.height) {
|
|
18
|
+
return { ...dimensions, diff: null, dimensionsMatch: false, mismatch: 0, ratio: 1 };
|
|
19
|
+
}
|
|
20
|
+
const rendered = new PNG({ height: before.height, width: before.width });
|
|
21
|
+
const mismatch = pixelmatch(before.data, after.data, rendered.data, before.width, before.height, {
|
|
22
|
+
includeAA: options.includeAA ?? false,
|
|
23
|
+
threshold: options.threshold ?? DEFAULT_THRESHOLD
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
...dimensions,
|
|
27
|
+
diff: PNG.sync.write(rendered),
|
|
28
|
+
dimensionsMatch: true,
|
|
29
|
+
mismatch,
|
|
30
|
+
ratio: mismatch / total
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function isWithinBudget(result, mismatchRatio = DEFAULT_MISMATCH_RATIO) {
|
|
34
|
+
return result.dimensionsMatch && result.ratio <= mismatchRatio;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/baseline.ts
|
|
38
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
39
|
+
import { dirname, join } from "path";
|
|
40
|
+
function writeUnder(path, content) {
|
|
41
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
42
|
+
writeFileSync(path, content);
|
|
43
|
+
}
|
|
44
|
+
function reconcileFrame(name, actual, dirs, options = {}) {
|
|
45
|
+
const baselinePath = join(dirs.baselineDir, name);
|
|
46
|
+
const stored = existsSync(baselinePath);
|
|
47
|
+
if (options.update) {
|
|
48
|
+
writeUnder(baselinePath, actual);
|
|
49
|
+
return { baselinePath, name, result: null, status: stored ? "updated" : "created" };
|
|
50
|
+
}
|
|
51
|
+
if (!stored) {
|
|
52
|
+
return { baselinePath, name, result: null, status: "missing-baseline" };
|
|
53
|
+
}
|
|
54
|
+
const result = comparePng(readFileSync(baselinePath), actual, options);
|
|
55
|
+
const passed = isWithinBudget(result, options.mismatchRatio ?? DEFAULT_MISMATCH_RATIO);
|
|
56
|
+
if (!passed) {
|
|
57
|
+
const stem = name.replace(/\.png$/, "");
|
|
58
|
+
writeUnder(join(dirs.artifactsDir, `${stem}.actual.png`), actual);
|
|
59
|
+
if (result.diff) writeFileSync(join(dirs.artifactsDir, `${stem}.diff.png`), result.diff);
|
|
60
|
+
}
|
|
61
|
+
return { baselinePath, name, result, status: passed ? "pass" : "fail" };
|
|
62
|
+
}
|
|
63
|
+
function describeOutcome(outcome) {
|
|
64
|
+
if (outcome.status === "missing-baseline") {
|
|
65
|
+
return `${outcome.name}: NO BASELINE at ${outcome.baselinePath} \u2014 run again in update mode to record it.`;
|
|
66
|
+
}
|
|
67
|
+
if (outcome.result === null) {
|
|
68
|
+
return `${outcome.name}: ${outcome.status}`;
|
|
69
|
+
}
|
|
70
|
+
if (!outcome.result.dimensionsMatch) {
|
|
71
|
+
const { actualHeight, actualWidth, height, width } = outcome.result;
|
|
72
|
+
return `${outcome.name}: DIMENSION MISMATCH (baseline ${width}\xD7${height}, actual ${actualWidth}\xD7${actualHeight}) \u2014 re-record the baseline.`;
|
|
73
|
+
}
|
|
74
|
+
const percent = (outcome.result.ratio * 100).toFixed(3);
|
|
75
|
+
return `${outcome.name}: ${outcome.status} \u2014 ${outcome.result.mismatch}/${outcome.result.total} px (${percent}%)`;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/report.ts
|
|
79
|
+
import { existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync2, writeFileSync as writeFileSync2 } from "fs";
|
|
80
|
+
import { dirname as dirname2 } from "path";
|
|
81
|
+
function verdictFor(diffPercent) {
|
|
82
|
+
if (diffPercent === 0) return "IDENTICAL";
|
|
83
|
+
if (diffPercent < 0.01) return "NEAR_IDENTICAL";
|
|
84
|
+
if (diffPercent < 0.1) return "SIMILAR";
|
|
85
|
+
if (diffPercent < 1) return "MINOR_DIFF";
|
|
86
|
+
return "SIGNIFICANT_DIFF";
|
|
87
|
+
}
|
|
88
|
+
function analysisFor(verdict, diffPercent, differentPixels) {
|
|
89
|
+
const pixels = differentPixels.toLocaleString();
|
|
90
|
+
switch (verdict) {
|
|
91
|
+
case "IDENTICAL":
|
|
92
|
+
return "Images are 100% pixel-perfect identical";
|
|
93
|
+
case "NEAR_IDENTICAL":
|
|
94
|
+
return `Nearly identical (${diffPercent.toFixed(4)}% diff - likely anti-aliasing or subpixel rendering)`;
|
|
95
|
+
case "SIMILAR":
|
|
96
|
+
return `Very similar (${diffPercent.toFixed(4)}% diff - minor visual differences)`;
|
|
97
|
+
case "MINOR_DIFF":
|
|
98
|
+
return `Minor differences detected (${diffPercent.toFixed(2)}% diff - ${pixels} pixels)`;
|
|
99
|
+
case "SIGNIFICANT_DIFF":
|
|
100
|
+
return `Significant differences detected (${diffPercent.toFixed(2)}% diff - ${pixels} pixels)`;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function readFrame(path) {
|
|
104
|
+
if (!existsSync2(path)) throw new Error(`Image not found: ${path}`);
|
|
105
|
+
return readFileSync2(path);
|
|
106
|
+
}
|
|
107
|
+
async function compare({ image1, image2, options = {} }) {
|
|
108
|
+
const result = comparePng(readFrame(image1), readFrame(image2), options);
|
|
109
|
+
if (!result.dimensionsMatch) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`Dimension mismatch: Image1 is ${result.width}x${result.height}, Image2 is ${result.actualWidth}x${result.actualHeight}`
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
const diffPercent = result.mismatch / result.total * 100;
|
|
115
|
+
const verdict = verdictFor(diffPercent);
|
|
116
|
+
let diffImagePath = null;
|
|
117
|
+
if (options.outputPath && result.diff) {
|
|
118
|
+
mkdirSync2(dirname2(options.outputPath), { recursive: true });
|
|
119
|
+
writeFileSync2(options.outputPath, result.diff);
|
|
120
|
+
diffImagePath = options.outputPath;
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
analysis: analysisFor(verdict, diffPercent, result.mismatch),
|
|
124
|
+
diffImagePath,
|
|
125
|
+
diffPercent,
|
|
126
|
+
diffPercentFormatted: `${diffPercent.toFixed(4)}%`,
|
|
127
|
+
differentPixels: result.mismatch,
|
|
128
|
+
dimensions: { height: result.height, width: result.width },
|
|
129
|
+
identical: result.mismatch === 0,
|
|
130
|
+
totalPixels: result.total,
|
|
131
|
+
verdict
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
var ICONS = {
|
|
135
|
+
IDENTICAL: "\u2705",
|
|
136
|
+
MINOR_DIFF: "\u26A0\uFE0F",
|
|
137
|
+
NEAR_IDENTICAL: "\u2705",
|
|
138
|
+
SIGNIFICANT_DIFF: "\u274C",
|
|
139
|
+
SIMILAR: "\u26A0\uFE0F"
|
|
140
|
+
};
|
|
141
|
+
function formatReport(result) {
|
|
142
|
+
const rule = "=".repeat(50);
|
|
143
|
+
return `
|
|
144
|
+
${rule}
|
|
145
|
+
VISUAL DIFF REPORT
|
|
146
|
+
${rule}
|
|
147
|
+
|
|
148
|
+
${ICONS[result.verdict]} Verdict: ${result.verdict}
|
|
149
|
+
|
|
150
|
+
Dimensions: ${result.dimensions.width}x${result.dimensions.height}
|
|
151
|
+
Total Pixels: ${result.totalPixels.toLocaleString()}
|
|
152
|
+
Different Pixels: ${result.differentPixels.toLocaleString()}
|
|
153
|
+
Difference: ${result.diffPercentFormatted}
|
|
154
|
+
|
|
155
|
+
Analysis: ${result.analysis}
|
|
156
|
+
|
|
157
|
+
${result.diffImagePath ? `Diff Image: ${result.diffImagePath}` : ""}
|
|
158
|
+
${rule}
|
|
159
|
+
`.trim();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export {
|
|
163
|
+
DEFAULT_THRESHOLD,
|
|
164
|
+
DEFAULT_MISMATCH_RATIO,
|
|
165
|
+
comparePng,
|
|
166
|
+
isWithinBudget,
|
|
167
|
+
reconcileFrame,
|
|
168
|
+
describeOutcome,
|
|
169
|
+
compare,
|
|
170
|
+
formatReport
|
|
171
|
+
};
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_THRESHOLD,
|
|
3
|
+
compare,
|
|
4
|
+
describeOutcome,
|
|
5
|
+
formatReport,
|
|
6
|
+
reconcileFrame
|
|
7
|
+
} from "./chunk-AZ4XL3H5.js";
|
|
8
|
+
|
|
9
|
+
// src/cli.ts
|
|
10
|
+
import { existsSync, readFileSync } from "fs";
|
|
11
|
+
import { join } from "path";
|
|
12
|
+
var USAGE = `geonosis-visual-diff \u2014 pixel comparison and baselines for visual regression
|
|
13
|
+
|
|
14
|
+
geonosis-visual-diff compare <actual> <expected> [--threshold <0..1>] [--out <path>] [--include-aa]
|
|
15
|
+
geonosis-visual-diff baseline <name> <actual> [--dir <path>] [--artifacts <path>] [--update]
|
|
16
|
+
|
|
17
|
+
--threshold per-pixel colour tolerance, 0..1 (default ${DEFAULT_THRESHOLD}).
|
|
18
|
+
0 is strict per-pixel equality \u2014 what a parity claim is measured at.
|
|
19
|
+
0.1 absorbs the anti-aliasing noise two renders of one layout produce.
|
|
20
|
+
--out write the rendered diff PNG here (compare).
|
|
21
|
+
--dir where the baselines live (baseline; default ./visual-baselines).
|
|
22
|
+
--artifacts where a failing actual and diff are left (baseline; default <dir>/__artifacts__).
|
|
23
|
+
--update record the baseline instead of checking against it.
|
|
24
|
+
--include-aa count anti-aliased pixels as differences.
|
|
25
|
+
|
|
26
|
+
Exit: 0 equal \xB7 1 differs \xB7 2 refused (missing file, size mismatch, bad argument).`;
|
|
27
|
+
function refuse(message) {
|
|
28
|
+
throw new Error(message);
|
|
29
|
+
}
|
|
30
|
+
var VALUE_FLAGS = /* @__PURE__ */ new Set(["--artifacts", "--dir", "--out", "--threshold"]);
|
|
31
|
+
function parse(argv) {
|
|
32
|
+
const flags = /* @__PURE__ */ new Map();
|
|
33
|
+
const positionals = [];
|
|
34
|
+
for (let index = 0; index < argv.length; index++) {
|
|
35
|
+
const token = argv[index];
|
|
36
|
+
if (token === void 0) continue;
|
|
37
|
+
if (!token.startsWith("--")) {
|
|
38
|
+
positionals.push(token);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (VALUE_FLAGS.has(token)) {
|
|
42
|
+
const value = argv[index + 1];
|
|
43
|
+
if (value === void 0 || value.startsWith("--")) refuse(`${token} needs a value`);
|
|
44
|
+
flags.set(token, value);
|
|
45
|
+
index++;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
flags.set(token, true);
|
|
49
|
+
}
|
|
50
|
+
return { flags, positionals };
|
|
51
|
+
}
|
|
52
|
+
function thresholdFrom(flags) {
|
|
53
|
+
const raw = flags.get("--threshold");
|
|
54
|
+
if (raw === void 0) return DEFAULT_THRESHOLD;
|
|
55
|
+
if (raw === true) refuse("--threshold needs a value");
|
|
56
|
+
const value = Number(raw);
|
|
57
|
+
if (!Number.isFinite(value) || value < 0 || value > 1) {
|
|
58
|
+
refuse(`--threshold must be a number between 0 and 1, got "${raw}"`);
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
function pathFlag(flags, name) {
|
|
63
|
+
const value = flags.get(name);
|
|
64
|
+
return typeof value === "string" ? value : void 0;
|
|
65
|
+
}
|
|
66
|
+
async function runCompare({ flags, positionals }) {
|
|
67
|
+
const [actual, expected] = positionals;
|
|
68
|
+
if (actual === void 0 || expected === void 0) {
|
|
69
|
+
refuse("compare needs two image paths: compare <actual> <expected>");
|
|
70
|
+
}
|
|
71
|
+
const outputPath = pathFlag(flags, "--out");
|
|
72
|
+
const result = await compare({
|
|
73
|
+
image1: actual,
|
|
74
|
+
image2: expected,
|
|
75
|
+
options: {
|
|
76
|
+
includeAA: flags.get("--include-aa") === true,
|
|
77
|
+
threshold: thresholdFrom(flags),
|
|
78
|
+
...outputPath === void 0 ? {} : { outputPath }
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
process.stdout.write(`${formatReport(result)}
|
|
82
|
+
`);
|
|
83
|
+
return result.identical ? 0 : 1;
|
|
84
|
+
}
|
|
85
|
+
function runBaseline({ flags, positionals }) {
|
|
86
|
+
const [name, actual] = positionals;
|
|
87
|
+
if (name === void 0 || actual === void 0) {
|
|
88
|
+
refuse("baseline needs a name and an image: baseline <name> <actual>");
|
|
89
|
+
}
|
|
90
|
+
if (!existsSync(actual)) refuse(`Image not found: ${actual}`);
|
|
91
|
+
const baselineDir = pathFlag(flags, "--dir") ?? "visual-baselines";
|
|
92
|
+
const outcome = reconcileFrame(
|
|
93
|
+
name.endsWith(".png") ? name : `${name}.png`,
|
|
94
|
+
readFileSync(actual),
|
|
95
|
+
{
|
|
96
|
+
artifactsDir: pathFlag(flags, "--artifacts") ?? join(baselineDir, "__artifacts__"),
|
|
97
|
+
baselineDir
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
includeAA: flags.get("--include-aa") === true,
|
|
101
|
+
threshold: thresholdFrom(flags),
|
|
102
|
+
update: flags.get("--update") === true
|
|
103
|
+
}
|
|
104
|
+
);
|
|
105
|
+
process.stdout.write(`${describeOutcome(outcome)}
|
|
106
|
+
`);
|
|
107
|
+
if (outcome.status === "fail" || outcome.status === "missing-baseline") {
|
|
108
|
+
process.stdout.write(`baseline: ${outcome.baselinePath}
|
|
109
|
+
`);
|
|
110
|
+
return 1;
|
|
111
|
+
}
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
114
|
+
async function main(argv) {
|
|
115
|
+
const [command, ...rest] = argv;
|
|
116
|
+
if (command === void 0 || command === "--help" || command === "-h") {
|
|
117
|
+
process.stdout.write(`${USAGE}
|
|
118
|
+
`);
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
const args = parse(rest);
|
|
122
|
+
if (command === "compare") return runCompare(args);
|
|
123
|
+
if (command === "baseline") return runBaseline(args);
|
|
124
|
+
refuse(`unknown command "${command}"
|
|
125
|
+
|
|
126
|
+
${USAGE}`);
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
process.exitCode = await main(process.argv.slice(2));
|
|
130
|
+
} catch (error) {
|
|
131
|
+
process.stderr.write(`geonosis-visual-diff: ${error.message}
|
|
132
|
+
`);
|
|
133
|
+
process.exitCode = 2;
|
|
134
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_MISMATCH_RATIO,
|
|
3
|
+
DEFAULT_THRESHOLD,
|
|
4
|
+
compare,
|
|
5
|
+
comparePng,
|
|
6
|
+
describeOutcome,
|
|
7
|
+
formatReport,
|
|
8
|
+
isWithinBudget,
|
|
9
|
+
reconcileFrame
|
|
10
|
+
} from "./chunk-AZ4XL3H5.js";
|
|
11
|
+
|
|
12
|
+
// src/manifest.ts
|
|
13
|
+
var DEFAULT_BREAKPOINTS = [
|
|
14
|
+
{ height: 1600, name: "base", width: 375 },
|
|
15
|
+
{ height: 1600, name: "sm", width: 640 },
|
|
16
|
+
{ height: 1600, name: "md", width: 768 },
|
|
17
|
+
{ height: 1600, name: "lg", width: 1024 },
|
|
18
|
+
{ height: 1600, name: "xl", width: 1280 }
|
|
19
|
+
];
|
|
20
|
+
function breakpointsFor(target, fallback = DEFAULT_BREAKPOINTS) {
|
|
21
|
+
return target.breakpoints ?? fallback;
|
|
22
|
+
}
|
|
23
|
+
function baselineName(surface, breakpoint) {
|
|
24
|
+
return `${surface}-${breakpoint}.png`;
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
DEFAULT_BREAKPOINTS,
|
|
28
|
+
DEFAULT_MISMATCH_RATIO,
|
|
29
|
+
DEFAULT_THRESHOLD,
|
|
30
|
+
baselineName,
|
|
31
|
+
breakpointsFor,
|
|
32
|
+
compare,
|
|
33
|
+
comparePng,
|
|
34
|
+
describeOutcome,
|
|
35
|
+
formatReport,
|
|
36
|
+
isWithinBudget,
|
|
37
|
+
reconcileFrame
|
|
38
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@geonosis/visual-diff",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Pixel comparison, reporting and baseline management for visual regression — the union of dielime's and during.day's forks. Capture stays with the consumer; nothing here launches a browser.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"visual-regression",
|
|
7
|
+
"pixelmatch",
|
|
8
|
+
"png",
|
|
9
|
+
"screenshot-diff",
|
|
10
|
+
"baseline",
|
|
11
|
+
"pixel-diff"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/microcompanies/geonosis/tree/main/packages/visual-diff",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/microcompanies/geonosis.git",
|
|
17
|
+
"directory": "packages/visual-diff"
|
|
18
|
+
},
|
|
19
|
+
"license": "Apache-2.0",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "dist/index.js",
|
|
22
|
+
"bin": {
|
|
23
|
+
"geonosis-visual-diff": "bin/geonosis-visual-diff.mjs"
|
|
24
|
+
},
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"bin",
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"pixelmatch": "^7.2.0",
|
|
34
|
+
"pngjs": "^7.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/pngjs": "^6.0.5"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=22"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsup",
|
|
47
|
+
"typecheck": "tsc --noEmit"
|
|
48
|
+
}
|
|
49
|
+
}
|