@digital-commons-official/codeceptjs-storyboard 3.0.0 → 3.1.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/README.md +13 -4
- package/package.json +9 -2
- package/src/convert-baselines.js +16 -0
- package/src/image.js +84 -0
- package/src/index.d.ts +8 -6
- package/src/index.js +60 -36
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
A [CodeceptJS](https://codecept.io) plugin that turns one Gherkin scenario into one human-readable storyboard, straight from the real screenshots the test captures. Markdown is the default; select SVG explicitly when you want a self-contained picture.
|
|
7
7
|
|
|
8
8
|
ONE Gherkin sentence = ONE card = ONE pixel baseline, asserted inside its step at `tolerance: 0`: a visual regression fails on the exact sentence whose image drifted.
|
|
9
|
-
When the scenario ends, Markdown shows screenshots side by side with readable captions, notes and copyable commands. It links the original
|
|
9
|
+
When the scenario ends, Markdown shows screenshots side by side with readable captions, notes and copyable commands. It links the original screenshots instead of embedding their bytes, so reviewers can open the file directly in GitLab. SVG embeds the screenshots and retains selectable text around them (Chromium/Safari).
|
|
10
10
|
|
|
11
11
|
## Installation 📦
|
|
12
12
|
|
|
@@ -147,7 +147,7 @@ A filtered run lists only the scenarios it ran. In baseline-update mode, the rev
|
|
|
147
147
|
|
|
148
148
|
### Read Markdown on a branch or merge request
|
|
149
149
|
|
|
150
|
-
Commit the generated files under `storyboards/` together with their reviewed
|
|
150
|
+
Commit the generated files under `storyboards/` together with their reviewed screenshots under `screenshots/base/`.
|
|
151
151
|
Open the Markdown file on the branch being reviewed. Relative image links use that same revision; after
|
|
152
152
|
merging, opening the file on the target branch uses the merged images. No branch name, GitLab hostname or
|
|
153
153
|
token is stored in the document. A URL to a deleted source branch is not redirected automatically.
|
|
@@ -297,6 +297,14 @@ storyboard.storyboardStep(Given, 'a fresh project with only a README on its main
|
|
|
297
297
|
- `addStoryboardFrame(I, png)` refuses a blank frame, attaches the frame to the open card and asserts it against its own baseline at once. On drift the error names the frame, the pixel diff, the failure storyboard and the command that regenerates the baseline.
|
|
298
298
|
- `annotate({ note, copy })` overrides the feature comments for a value only known at runtime.
|
|
299
299
|
|
|
300
|
+
### PNG or WebP frames
|
|
301
|
+
|
|
302
|
+
Frames keep the format the VisualHelper writes: PNG up to its version 2, lossless WebP by default from its version 3 (`imageFormat`). Baselines, the Markdown links and the SVG images follow the frame. A WebP frame whose reviewed baseline is still a PNG is refused, because the helper would record it as a new baseline and compare nothing. Convert the reviewed baselines once, with the same pixels:
|
|
303
|
+
|
|
304
|
+
```shell
|
|
305
|
+
npx -y -p @digital-commons-official/codeceptjs-storyboard codeceptjs-storyboard-webp tests/e2e/screenshots/base
|
|
306
|
+
```
|
|
307
|
+
|
|
300
308
|
### Gherkin languages and examples
|
|
301
309
|
|
|
302
310
|
Set `# language: fr` (or another Cucumber dialect) on the first line of a feature file. Without this header, Gherkin uses English. Storyboards keep the source keywords, accents and scenario title, including keywords joined directly to the sentence. Register the translated sentences in your CodeceptJS step definitions.
|
|
@@ -339,12 +347,13 @@ by a human, which is why that mode throws when `CI` is set.
|
|
|
339
347
|
| Function | Purpose |
|
|
340
348
|
|------------------------------------------|--------------------------------------------------------------------------|
|
|
341
349
|
| `storyboardStep(register, sentence, fn)` | Register a sentence literally through `Given`, `When` or `Then` |
|
|
342
|
-
| `capturePageFrame(I, name)` | Full-viewport frame, returns the PNG path
|
|
343
|
-
| `captureElementFrame(I, name, selector)` | Element-cropped frame, returns the PNG path
|
|
350
|
+
| `capturePageFrame(I, name)` | Full-viewport frame, returns the PNG or WebP path |
|
|
351
|
+
| `captureElementFrame(I, name, selector)` | Element-cropped frame, returns the PNG or WebP path |
|
|
344
352
|
| `addStoryboardFrame(I, png)` | Attach the frame to the open card and assert it at `tolerance: 0` |
|
|
345
353
|
| `annotate({ note, copy })` | Runtime override of the open card's note and copyable command |
|
|
346
354
|
| `assertOrUpdateBaseline(I, name)` | The assert-or-rewrite primitive `addStoryboardFrame` relies on |
|
|
347
355
|
| `render(outFile, options)` | Write Markdown for `.md` or SVG for `.svg`; theme applies only to SVG |
|
|
356
|
+
| `convertBaselinesToWebp(dir)` | Convert reviewed PNG baselines to lossless WebP with the same pixels |
|
|
348
357
|
| `parseScenarioLines(featureFile, title)` | Every sentence of a scenario with its `# Chapter` / `# Note` / `# Copy` |
|
|
349
358
|
| `begin`, `panel`, `frame`, `panels` | The board primitives the plugin drives (useful for standalone rendering) |
|
|
350
359
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@digital-commons-official/codeceptjs-storyboard",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.0",
|
|
4
4
|
"description": "Storyboard plugin for CodeceptJS: one Gherkin sentence = one card = one pixel baseline, with Markdown or SVG reports",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"codeceptjs-storyboard-webp": "src/convert-baselines.js"
|
|
8
|
+
},
|
|
6
9
|
"types": "src/index.d.ts",
|
|
7
10
|
"exports": {
|
|
8
11
|
".": {
|
|
@@ -24,6 +27,7 @@
|
|
|
24
27
|
},
|
|
25
28
|
"dependencies": {
|
|
26
29
|
"@cucumber/gherkin": "^42.0.0",
|
|
30
|
+
"@jsquash/webp": "^1.5.0",
|
|
27
31
|
"pngjs": "^7.0.0"
|
|
28
32
|
},
|
|
29
33
|
"peerDependencies": {
|
|
@@ -45,7 +49,10 @@
|
|
|
45
49
|
"diff": "^9.0.0",
|
|
46
50
|
"serialize-javascript": "^7.0.5",
|
|
47
51
|
"undici": "^8.0.0",
|
|
48
|
-
"@xmldom/xmldom": "^0.9.12"
|
|
52
|
+
"@xmldom/xmldom": "^0.9.12",
|
|
53
|
+
"glob": "^14.0.2",
|
|
54
|
+
"minimatch": "^11.0.1",
|
|
55
|
+
"follow-redirects": "^1.16.2"
|
|
49
56
|
},
|
|
50
57
|
"keywords": [
|
|
51
58
|
"codeceptjs",
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Convert a folder of reviewed PNG baselines to lossless WebP, same pixels:
|
|
3
|
+
// npx -y -p @digital-commons-official/codeceptjs-storyboard codeceptjs-storyboard-webp <screenshots/base>
|
|
4
|
+
const { convertBaselinesToWebp } = require('./image')
|
|
5
|
+
|
|
6
|
+
const dir = process.argv[2]
|
|
7
|
+
if (!dir) {
|
|
8
|
+
console.error('usage: codeceptjs-storyboard-webp <baseline folder>')
|
|
9
|
+
process.exit(2)
|
|
10
|
+
}
|
|
11
|
+
convertBaselinesToWebp(dir).then(files => {
|
|
12
|
+
console.log(`${files.length} PNG baseline${files.length === 1 ? '' : 's'} converted to lossless WebP in ${dir}`)
|
|
13
|
+
}, err => {
|
|
14
|
+
console.error(err.message)
|
|
15
|
+
process.exit(1)
|
|
16
|
+
})
|
package/src/image.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frames come as PNG or as lossless WebP, the format the visual helper writes
|
|
3
|
+
* (WebP by default from its version 3). The engine reads the pixels to refuse
|
|
4
|
+
* a blank frame, the size to lay out the SVG, and converts reviewed PNG
|
|
5
|
+
* baselines once with the same lossless codec as the helper.
|
|
6
|
+
*/
|
|
7
|
+
const fs = require('fs')
|
|
8
|
+
const path = require('path')
|
|
9
|
+
const { PNG } = require('pngjs')
|
|
10
|
+
|
|
11
|
+
let codec = null
|
|
12
|
+
|
|
13
|
+
function webpCodec () {
|
|
14
|
+
codec ??= (async () => {
|
|
15
|
+
const dir = path.join(path.dirname(require.resolve('@jsquash/webp/package.json')), 'codec')
|
|
16
|
+
const compile = file => WebAssembly.compile(fs.readFileSync(path.join(dir, file)))
|
|
17
|
+
const [encoder, decoder] = await Promise.all([import('@jsquash/webp/encode.js'), import('@jsquash/webp/decode.js')])
|
|
18
|
+
// ponytail: SIMD build only, as in the visual helper; every supported Node runs Wasm SIMD
|
|
19
|
+
await encoder.init(await compile('enc/webp_enc_simd.wasm'))
|
|
20
|
+
await decoder.init(await compile('dec/webp_dec.wasm'))
|
|
21
|
+
return { encode: encoder.default, decode: decoder.default }
|
|
22
|
+
})()
|
|
23
|
+
return codec
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function isWebp (data) {
|
|
27
|
+
return data.toString('latin1', 0, 4) === 'RIFF' && data.toString('latin1', 8, 12) === 'WEBP'
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// RGBA pixels: { width, height, data }.
|
|
31
|
+
async function readImage (file) {
|
|
32
|
+
const data = fs.readFileSync(file)
|
|
33
|
+
if (!isWebp(data)) return PNG.sync.read(data)
|
|
34
|
+
const { decode } = await webpCodec()
|
|
35
|
+
return decode(data)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Width and height from the file header, without decoding the pixels.
|
|
39
|
+
function imageSize (file) {
|
|
40
|
+
const buf = Buffer.alloc(30)
|
|
41
|
+
const fd = fs.openSync(file, 'r')
|
|
42
|
+
fs.readSync(fd, buf, 0, 30, 0)
|
|
43
|
+
fs.closeSync(fd)
|
|
44
|
+
if (!isWebp(buf)) return { w: buf.readUInt32BE(16), h: buf.readUInt32BE(20) }
|
|
45
|
+
const chunk = buf.toString('latin1', 12, 16)
|
|
46
|
+
if (chunk === 'VP8X') return { w: buf.readUIntLE(24, 3) + 1, h: buf.readUIntLE(27, 3) + 1 }
|
|
47
|
+
if (chunk === 'VP8L') {
|
|
48
|
+
const bits = buf.readUInt32LE(21)
|
|
49
|
+
return { w: (bits & 0x3fff) + 1, h: ((bits >> 14) & 0x3fff) + 1 }
|
|
50
|
+
}
|
|
51
|
+
return { w: buf.readUInt16LE(26) & 0x3fff, h: buf.readUInt16LE(28) & 0x3fff }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function mimeType (file) {
|
|
55
|
+
return isWebp(fs.readFileSync(file).subarray(0, 12)) ? 'image/webp' : 'image/png'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Convert every PNG under `dir` to a lossless WebP beside it, check that the
|
|
60
|
+
* WebP reads back the same pixels, then delete the PNG. Returns the WebP
|
|
61
|
+
* files written.
|
|
62
|
+
*/
|
|
63
|
+
async function convertBaselinesToWebp (dir) {
|
|
64
|
+
const { encode, decode } = await webpCodec()
|
|
65
|
+
const written = []
|
|
66
|
+
const pngs = fs.readdirSync(dir, { recursive: true }).filter(file => file.endsWith('.png')).sort()
|
|
67
|
+
for (const relative of pngs) {
|
|
68
|
+
const png = path.join(dir, relative)
|
|
69
|
+
const image = PNG.sync.read(fs.readFileSync(png))
|
|
70
|
+
// exact: keep the colour of transparent pixels, so every pixel reads back the same
|
|
71
|
+
const webp = Buffer.from(await encode({ data: new Uint8ClampedArray(image.data), width: image.width, height: image.height }, { lossless: 1, exact: 1 }))
|
|
72
|
+
const back = await decode(webp)
|
|
73
|
+
if (!Buffer.from(back.data.buffer, back.data.byteOffset, back.data.byteLength).equals(image.data)) {
|
|
74
|
+
throw new Error(`${png}: the WebP copy does not read back the same pixels; the PNG is kept`)
|
|
75
|
+
}
|
|
76
|
+
const target = png.slice(0, -'.png'.length) + '.webp'
|
|
77
|
+
fs.writeFileSync(target, webp)
|
|
78
|
+
fs.rmSync(png)
|
|
79
|
+
written.push(target)
|
|
80
|
+
}
|
|
81
|
+
return written
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = { readImage, imageSize, mimeType, convertBaselinesToWebp }
|
package/src/index.d.ts
CHANGED
|
@@ -117,7 +117,7 @@ export interface RenderOptions {
|
|
|
117
117
|
outcome?: 'passed' | 'failed'
|
|
118
118
|
/** Inline overrides for standalone rendering. */
|
|
119
119
|
theme?: Theme
|
|
120
|
-
/** Substitute every frame with the same-named
|
|
120
|
+
/** Substitute every frame with the same-named image inside this directory (the reviewed baselines). */
|
|
121
121
|
imageDir?: string
|
|
122
122
|
/** Marks the drifted card in red, adds the expected/diff/actual band and the sentences never reached. */
|
|
123
123
|
failure?: { frame: FailureFrame | null, notReached: ScenarioLine[] } | null
|
|
@@ -138,7 +138,7 @@ declare namespace storyboardPlugin {
|
|
|
138
138
|
function parseScenarioLines (featureFile: string, scenarioTitle: string, options?: { occurrence?: number }): ScenarioLine[]
|
|
139
139
|
/** Open a card (the plugin does it on bddStep.before). */
|
|
140
140
|
function panel (title: string, opts?: Annotation): void
|
|
141
|
-
/** Attach a PNG to the open card without asserting it. */
|
|
141
|
+
/** Attach a PNG or WebP frame to the open card without asserting it. */
|
|
142
142
|
function frame (pngPath: string, opts?: Annotation): void
|
|
143
143
|
/** Merge a runtime note/copy into the open card. */
|
|
144
144
|
function annotate (opts: Annotation): void
|
|
@@ -147,18 +147,20 @@ declare namespace storyboardPlugin {
|
|
|
147
147
|
/** Register a Gherkin sentence literally (cucumber-expression metacharacters escaped). */
|
|
148
148
|
function storyboardStep (register: StepRegister, pattern: string, fn: StepFunction): void
|
|
149
149
|
function storyboardStep (register: StepRegister, pattern: string, opts: Annotation | null, fn: StepFunction): void
|
|
150
|
-
/** Full-viewport capture into _output/storyboard-frames/<name>.png; returns the absolute path. */
|
|
150
|
+
/** Full-viewport capture into _output/storyboard-frames/<name>.png or .webp, as the visual helper writes it; returns the absolute path. */
|
|
151
151
|
function capturePageFrame (I: StoryboardActor, frameName: string): Promise<string>
|
|
152
152
|
/** Element-cropped capture; the frame is exactly the selector's box. */
|
|
153
153
|
function captureElementFrame (I: StoryboardActor, frameName: string, selector: string): Promise<string>
|
|
154
|
-
/** Absolute path
|
|
154
|
+
/** Absolute path of the frame of that name: the WebP the visual helper wrote, else the PNG. */
|
|
155
155
|
function frameOutputPath (frameName: string): string
|
|
156
|
-
/** Assert _output/<name
|
|
157
|
-
function assertOrUpdateBaseline (I: StoryboardActor, baselineName: string): Promise<void>
|
|
156
|
+
/** Assert _output/<name><extension> against screenshots/base/<name><extension>, or rewrite the baseline in update mode. */
|
|
157
|
+
function assertOrUpdateBaseline (I: StoryboardActor, baselineName: string, extension?: '.png' | '.webp'): Promise<void>
|
|
158
158
|
/** Attach the frame to the open card and assert it against its own baseline at tolerance 0. */
|
|
159
159
|
function addStoryboardFrame (I: StoryboardActor, png: string): Promise<void>
|
|
160
160
|
/** Write the captured cards as Markdown (.md) or SVG (.svg), selected by extension; returns outFile. */
|
|
161
161
|
function render (outFile: string, options?: RenderOptions): string
|
|
162
|
+
/** Convert every PNG under dir to a lossless WebP with the same pixels, delete the PNG; returns the WebP files written. */
|
|
163
|
+
function convertBaselinesToWebp (dir: string): Promise<string[]>
|
|
162
164
|
/** Re-export of codeceptjs/effects tryTo. */
|
|
163
165
|
function tryTo (fn: () => unknown): Promise<boolean>
|
|
164
166
|
}
|
package/src/index.js
CHANGED
|
@@ -53,8 +53,8 @@
|
|
|
53
53
|
|
|
54
54
|
const fs = require('fs')
|
|
55
55
|
const path = require('path')
|
|
56
|
-
const { PNG } = require('pngjs')
|
|
57
56
|
const { tryTo } = require('codeceptjs/effects')
|
|
57
|
+
const { readImage, imageSize, mimeType, convertBaselinesToWebp } = require('./image')
|
|
58
58
|
const { PROPERTIES, normalizeTheme, loadTheme } = require('./theme')
|
|
59
59
|
const { readScenario } = require('./gherkin')
|
|
60
60
|
const { normalizeMarkdown } = require('./markdown-presentation')
|
|
@@ -156,33 +156,45 @@ function storyboardStep (register, pattern, opts, fn) {
|
|
|
156
156
|
})
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
+
function frameWebpPath (frameName) {
|
|
160
|
+
return path.join(e2eDir(), '_output', 'storyboard-frames', frameName + '.webp')
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// The frame a capture of that name wrote: the visual helper 3 writes lossless
|
|
164
|
+
// WebP, older ones PNG. A step that captures by itself names it this way.
|
|
159
165
|
function frameOutputPath (frameName) {
|
|
160
|
-
|
|
166
|
+
const webp = frameWebpPath(frameName)
|
|
167
|
+
return fs.existsSync(webp) ? webp : webp.slice(0, -'.webp'.length) + '.png'
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Clear a WebP left by an earlier run, so a helper that writes PNG is not
|
|
171
|
+
// credited with it, then capture and return the file the helper wrote.
|
|
172
|
+
async function captureInto (frameName, capture) {
|
|
173
|
+
const webp = frameWebpPath(frameName)
|
|
174
|
+
fs.mkdirSync(path.dirname(webp), { recursive: true })
|
|
175
|
+
fs.rmSync(webp, { force: true })
|
|
176
|
+
await capture()
|
|
177
|
+
return frameOutputPath(frameName)
|
|
161
178
|
}
|
|
162
179
|
|
|
163
180
|
// Full-page snapshot of the CURRENT page (mask its volatile content first)
|
|
164
|
-
// into the frames dir; returns the
|
|
181
|
+
// into the frames dir; returns the frame's absolute path.
|
|
165
182
|
async function capturePageFrame (I, frameName) {
|
|
166
|
-
|
|
167
|
-
await I.takeScreenshot(`storyboard-frames/${frameName}`)
|
|
168
|
-
return frameOutputPath(frameName)
|
|
183
|
+
return captureInto(frameName, () => I.takeScreenshot(`storyboard-frames/${frameName}`))
|
|
169
184
|
}
|
|
170
185
|
|
|
171
186
|
// Element-cropped snapshot: the frame is EXACTLY the selector's box, never
|
|
172
187
|
// the whole viewport — a short verdict yields a short frame, not a mostly
|
|
173
188
|
// empty page. Use for rendered <pre> verdicts and any focused element.
|
|
174
189
|
async function captureElementFrame (I, frameName, selector) {
|
|
175
|
-
|
|
176
|
-
await I.captureScreenshot(`storyboard-frames/${frameName}`, 'actual', selector)
|
|
177
|
-
return frameOutputPath(frameName)
|
|
190
|
+
return captureInto(frameName, () => I.captureScreenshot(`storyboard-frames/${frameName}`, 'actual', selector))
|
|
178
191
|
}
|
|
179
192
|
|
|
180
193
|
// Guarantee the capture actually rendered: a frame whose sampled pixels are
|
|
181
194
|
// ~one single colour is a blank/unfinished capture (page not loaded, empty
|
|
182
195
|
// render) and must fail LOUD at capture time, not survive into a baseline.
|
|
183
|
-
function assertFrameNotEmpty (png) {
|
|
184
|
-
const
|
|
185
|
-
const { width, height, data } = img
|
|
196
|
+
async function assertFrameNotEmpty (png) {
|
|
197
|
+
const { width, height, data } = await readImage(png)
|
|
186
198
|
const step = Math.max(1, Math.floor(Math.sqrt((width * height) / 20000)))
|
|
187
199
|
const counts = new Map()
|
|
188
200
|
let sampled = 0
|
|
@@ -206,7 +218,7 @@ function assertFrameNotEmpty (png) {
|
|
|
206
218
|
}
|
|
207
219
|
|
|
208
220
|
/**
|
|
209
|
-
* Assert `_output/<baselineName
|
|
221
|
+
* Assert `_output/<baselineName><extension>` against its committed baseline, or — in
|
|
210
222
|
* baseline-update mode (TASK_E2E_UPDATE_BASELINES=1) — assert first and only
|
|
211
223
|
* when the assert FAILS persist the freshly captured actual as the baseline:
|
|
212
224
|
* green baselines stay byte-identical, so a regeneration run produces no
|
|
@@ -214,7 +226,7 @@ function assertFrameNotEmpty (png) {
|
|
|
214
226
|
* is why the mode is refused in CI — there it would silently swallow every
|
|
215
227
|
* visual regression.
|
|
216
228
|
*/
|
|
217
|
-
async function assertOrUpdateBaseline (I, baselineName) {
|
|
229
|
+
async function assertOrUpdateBaseline (I, baselineName, extension = '.png') {
|
|
218
230
|
if (process.env.TASK_E2E_UPDATE_BASELINES === '1' && process.env.CI) {
|
|
219
231
|
throw new Error('TASK_E2E_UPDATE_BASELINES is forbidden in CI — baselines must be regenerated and inspected locally')
|
|
220
232
|
}
|
|
@@ -227,8 +239,8 @@ async function assertOrUpdateBaseline (I, baselineName) {
|
|
|
227
239
|
// module re-exports it below.
|
|
228
240
|
const matches = await tryTo(() => I.assertVisualMatch(baselineName, { captureActual: false }))
|
|
229
241
|
if (!matches) {
|
|
230
|
-
const actualPath = path.join(e2eDir(), '_output', baselineName +
|
|
231
|
-
const baselinePath = path.join(e2eDir(), 'screenshots', 'base', baselineName +
|
|
242
|
+
const actualPath = path.join(e2eDir(), '_output', baselineName + extension)
|
|
243
|
+
const baselinePath = path.join(e2eDir(), 'screenshots', 'base', baselineName + extension)
|
|
232
244
|
fs.mkdirSync(path.dirname(baselinePath), { recursive: true })
|
|
233
245
|
fs.copyFileSync(actualPath, baselinePath)
|
|
234
246
|
}
|
|
@@ -237,19 +249,36 @@ async function assertOrUpdateBaseline (I, baselineName) {
|
|
|
237
249
|
await I.assertVisualMatch(baselineName, { captureActual: false })
|
|
238
250
|
}
|
|
239
251
|
|
|
252
|
+
// The visual helper compares a WebP frame with `<name>.webp` only. With the
|
|
253
|
+
// reviewed baseline still a PNG it would record the frame as a new baseline
|
|
254
|
+
// and pass without comparing anything: refuse, and name the conversion.
|
|
255
|
+
function refusePngBaseline (name, extension) {
|
|
256
|
+
if (extension !== '.webp') return
|
|
257
|
+
const base = path.join(e2eDir(), 'screenshots', 'base')
|
|
258
|
+
const png = path.join(base, `${name}.png`)
|
|
259
|
+
if (!fs.existsSync(png) || fs.existsSync(path.join(base, `${name}.webp`))) return
|
|
260
|
+
throw new Error(
|
|
261
|
+
`the reviewed baseline ${path.relative(e2eDir(), png)} is still a PNG while the visual helper writes WebP frames, ` +
|
|
262
|
+
'so this frame would be compared with nothing. Convert the PNG baselines to lossless WebP once, then rerun: ' +
|
|
263
|
+
`npx -y -p @digital-commons-official/codeceptjs-storyboard codeceptjs-storyboard-webp ${base}`
|
|
264
|
+
)
|
|
265
|
+
}
|
|
266
|
+
|
|
240
267
|
// Attach the frame to the current card and assert it against its own
|
|
241
268
|
// baseline immediately (tolerance: 0, update-mode aware): a visual
|
|
242
269
|
// regression fails on the exact sentence whose image drifted, and the error
|
|
243
270
|
// message carries everything a human needs to act on it.
|
|
244
271
|
async function addStoryboardFrame (I, png) {
|
|
245
|
-
assertFrameNotEmpty(png)
|
|
272
|
+
await assertFrameNotEmpty(png)
|
|
273
|
+
const extension = path.extname(png)
|
|
274
|
+
const name = `${baseDir()}/${path.basename(png, extension)}`
|
|
275
|
+
refusePngBaseline(name, extension)
|
|
246
276
|
frame(png)
|
|
247
|
-
const
|
|
248
|
-
const actualPath = path.join(e2eDir(), '_output', `${name}.png`)
|
|
277
|
+
const actualPath = path.join(e2eDir(), '_output', `${name}${extension}`)
|
|
249
278
|
fs.mkdirSync(path.dirname(actualPath), { recursive: true })
|
|
250
279
|
fs.copyFileSync(png, actualPath)
|
|
251
280
|
try {
|
|
252
|
-
await assertOrUpdateBaseline(I, name)
|
|
281
|
+
await assertOrUpdateBaseline(I, name, extension)
|
|
253
282
|
} catch (err) {
|
|
254
283
|
// Exact marker of WHICH frame failed its visual assert — the failure band
|
|
255
284
|
// trusts this, never a heuristic (a diff PNG may not even exist, e.g. on
|
|
@@ -257,8 +286,8 @@ async function addStoryboardFrame (I, png) {
|
|
|
257
286
|
if (board) board.visualFailure = path.basename(png)
|
|
258
287
|
if (err && typeof err.message === 'string') {
|
|
259
288
|
err.message +=
|
|
260
|
-
`\n storyboard frame : ${name}
|
|
261
|
-
`\n pixel diff : screenshots/diff/${path.dirname(name)}/Diff_${path.basename(name)}
|
|
289
|
+
`\n storyboard frame : ${name}${extension}` +
|
|
290
|
+
`\n pixel diff : screenshots/diff/${path.dirname(name)}/Diff_${path.basename(name)}${extension}` +
|
|
262
291
|
`\n failure storyboard : _output/${board && board.baseDir ? board.baseDir : '<scenario>'}.${board && board.format === 'markdown' ? 'md' : 'svg'}` +
|
|
263
292
|
(board && board.rerun
|
|
264
293
|
? `\n deliberate change? regenerate locally, then inspect: TASK_E2E_UPDATE_BASELINES=1 ${board.rerun}`
|
|
@@ -272,13 +301,9 @@ async function addStoryboardFrame (I, png) {
|
|
|
272
301
|
// SVG rendering — pure Node, no dependencies.
|
|
273
302
|
// ---------------------------------------------------------------------------
|
|
274
303
|
|
|
275
|
-
//
|
|
276
|
-
function
|
|
277
|
-
|
|
278
|
-
const fd = fs.openSync(file, 'r')
|
|
279
|
-
fs.readSync(fd, buf, 0, 24, 0)
|
|
280
|
-
fs.closeSync(fd)
|
|
281
|
-
return { w: buf.readUInt32BE(16), h: buf.readUInt32BE(20) }
|
|
304
|
+
// The frame as an SVG href, PNG or WebP as captured.
|
|
305
|
+
function dataUri (file) {
|
|
306
|
+
return `data:${mimeType(file)};base64,${fs.readFileSync(file).toString('base64')}`
|
|
282
307
|
}
|
|
283
308
|
|
|
284
309
|
function esc (text) {
|
|
@@ -411,7 +436,7 @@ function render (outFile, options = {}) {
|
|
|
411
436
|
// Wide: still wideFrom pixels wide at its full-row scale (see GRID).
|
|
412
437
|
const fullRowScale = ({ w, h }) => Math.min(1, rowW / w, g.maxSlotH / h)
|
|
413
438
|
const isWide = (card) => {
|
|
414
|
-
const size =
|
|
439
|
+
const size = imageSize(card.file)
|
|
415
440
|
return size.w * fullRowScale(size) >= g.wideFrom
|
|
416
441
|
}
|
|
417
442
|
const cardWOf = (card) => isWide(card) ? rowW : g.cardW
|
|
@@ -447,15 +472,14 @@ function render (outFile, options = {}) {
|
|
|
447
472
|
// Draw an image letterboxed inside a box (scaled to fit, centred on the
|
|
448
473
|
// slot colour), with an optional native <title> tooltip.
|
|
449
474
|
const drawImage = (file, x, y, boxW, boxH, tooltip) => {
|
|
450
|
-
const { w, h } =
|
|
451
|
-
const data = fs.readFileSync(file).toString('base64')
|
|
475
|
+
const { w, h } = imageSize(file)
|
|
452
476
|
const scale = Math.min(boxW / w, boxH / h)
|
|
453
477
|
const dw = w * scale
|
|
454
478
|
const dh = h * scale
|
|
455
479
|
parts.push(`<rect x="${x}" y="${y}" width="${boxW}" height="${boxH}" fill="${t.slot}"/>`)
|
|
456
480
|
parts.push(
|
|
457
481
|
`<image x="${(x + (boxW - dw) / 2).toFixed(1)}" y="${(y + (boxH - dh) / 2).toFixed(1)}"` +
|
|
458
|
-
` width="${dw.toFixed(1)}" height="${dh.toFixed(1)}" href="
|
|
482
|
+
` width="${dw.toFixed(1)}" height="${dh.toFixed(1)}" href="${dataUri(file)}">` +
|
|
459
483
|
(tooltip ? `<title>${esc(tooltip)}</title>` : '') + '</image>'
|
|
460
484
|
)
|
|
461
485
|
}
|
|
@@ -550,7 +574,7 @@ function render (outFile, options = {}) {
|
|
|
550
574
|
// frames in letterbox. A wide frame's box is the whole row, so a 1440x900
|
|
551
575
|
// web page keeps its full height and shows at 100%.
|
|
552
576
|
const slotOf = (card) => {
|
|
553
|
-
const { w, h } =
|
|
577
|
+
const { w, h } = imageSize(card.file)
|
|
554
578
|
const scale = Math.min(1, cardWOf(card) / w, g.maxSlotH / h)
|
|
555
579
|
return { scale, dispW: w * scale, dispH: h * scale }
|
|
556
580
|
}
|
|
@@ -690,11 +714,10 @@ function render (outFile, options = {}) {
|
|
|
690
714
|
// ways inside the ROW's shared slot: the two cards of a row keep the same
|
|
691
715
|
// image zone and the same text zone, whatever each frame's own size.
|
|
692
716
|
parts.push(`<rect x="${cx}" y="${cy}" width="${cw}" height="${slotH}" fill="${t.slot}"/>`)
|
|
693
|
-
const data = fs.readFileSync(card.file).toString('base64')
|
|
694
717
|
const iy = cy + BADGE_BAND + (slotH - BADGE_BAND - Math.ceil(dispH)) / 2
|
|
695
718
|
parts.push(
|
|
696
719
|
`<image x="${(cx + (cw - dispW) / 2).toFixed(1)}" y="${iy.toFixed(1)}"` +
|
|
697
|
-
` width="${dispW.toFixed(1)}" height="${dispH.toFixed(1)}" href="
|
|
720
|
+
` width="${dispW.toFixed(1)}" height="${dispH.toFixed(1)}" href="${dataUri(card.file)}">` +
|
|
698
721
|
`<title>${esc(path.basename(card.file))}${scale < 1 ? ` (shown at ${(scale * 100).toFixed(0)}%)` : ''}</title></image>`
|
|
699
722
|
)
|
|
700
723
|
|
|
@@ -948,4 +971,5 @@ module.exports.frameOutputPath = frameOutputPath
|
|
|
948
971
|
module.exports.assertOrUpdateBaseline = assertOrUpdateBaseline
|
|
949
972
|
module.exports.addStoryboardFrame = addStoryboardFrame
|
|
950
973
|
module.exports.render = render
|
|
974
|
+
module.exports.convertBaselinesToWebp = convertBaselinesToWebp
|
|
951
975
|
module.exports.tryTo = tryTo
|