@digital-commons-official/codeceptjs-storyboard 2.0.4 → 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 +18 -24
- package/package.json +9 -2
- package/src/convert-baselines.js +16 -0
- package/src/image.js +84 -0
- package/src/index.d.ts +8 -8
- package/src/index.js +60 -36
- package/src/markdown-presentation.js +3 -58
- package/src/markdown.js +2 -4
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.
|
|
@@ -161,14 +161,13 @@ The report starts with its recorded outcome and frame/step counts, followed by l
|
|
|
161
161
|
the result and replay information. A standalone render without an explicit outcome says
|
|
162
162
|
"Outcome not recorded"; it does not infer success from the absence of a visual failure.
|
|
163
163
|
|
|
164
|
-
|
|
165
|
-
chapters gets a journey banner. Screenshots sit in two equal-width columns by default. Each card keeps
|
|
164
|
+
Each chapter starts with a plain Markdown heading. Screenshots sit in two equal-width columns by default. Each card keeps
|
|
166
165
|
its title, image, note and copyable command together. Each pair has its own table so GitLab does not put
|
|
167
166
|
an entire chapter inside one scrolling region. Commands fold within their card; replay and source
|
|
168
167
|
details fold at the end. A failed comparison keeps Expected / Diff / Actual and unavailable-image
|
|
169
168
|
labels visible, regardless of the details setting.
|
|
170
169
|
|
|
171
|
-
### Markdown
|
|
170
|
+
### Markdown layout
|
|
172
171
|
|
|
173
172
|
Configure presentation directly on the plugin; no CSS file is needed:
|
|
174
173
|
|
|
@@ -177,11 +176,6 @@ storyboard: {
|
|
|
177
176
|
require: '@digital-commons-official/codeceptjs-storyboard',
|
|
178
177
|
enabled: true,
|
|
179
178
|
markdown: {
|
|
180
|
-
colors: {
|
|
181
|
-
banner: '#155e50',
|
|
182
|
-
bannerText: '#ffffff',
|
|
183
|
-
accent: '#559b84'
|
|
184
|
-
},
|
|
185
179
|
columns: 2,
|
|
186
180
|
tableOfContents: true,
|
|
187
181
|
technicalDetails: 'collapsed'
|
|
@@ -194,19 +188,10 @@ narrow screens. GitLab tables do not automatically stack like a responsive HTML
|
|
|
194
188
|
Set `technicalDetails: 'expanded'` to open commands and replay/source details, or
|
|
195
189
|
`tableOfContents: false` to omit navigation. Neither option removes evidence or notes.
|
|
196
190
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
ratio of at least 4.5:1. Invalid colours, unsupported options and invalid layout values raise a clear
|
|
200
|
-
error before the plugin registers listeners or the standalone renderer writes artifacts.
|
|
201
|
-
|
|
202
|
-
Generated banners live in an adjacent `_storyboard-assets/` directory and are named from their content.
|
|
203
|
-
**Commit these assets with the reviewed `.md` files and PNGs.** Keep that relative directory structure
|
|
204
|
-
when downloading runtime reports too. Repeated renders reuse the same asset names; unused older
|
|
205
|
-
assets are not automatically deleted. Banners are small decorations, not an additional full SVG report.
|
|
206
|
-
Long titles are abbreviated in the decoration but remain complete in the native Markdown heading.
|
|
191
|
+
Unsupported options and invalid layout values raise a clear error before the plugin registers
|
|
192
|
+
listeners or the standalone renderer writes artifacts.
|
|
207
193
|
|
|
208
|
-
|
|
209
|
-
background, fonts and native alert colours through the reader's preferences. Essential information
|
|
194
|
+
GitLab controls the page background, fonts and native alert colours through the reader's preferences. Essential information
|
|
210
195
|
remains text even when images cannot load. The renderer never recolours screenshots, loads external
|
|
211
196
|
assets, or applies the SVG-only `theme` option to Markdown. Explicit `format: 'svg'` keeps its existing
|
|
212
197
|
theme behaviour and ignores Markdown presentation settings.
|
|
@@ -312,6 +297,14 @@ storyboard.storyboardStep(Given, 'a fresh project with only a README on its main
|
|
|
312
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.
|
|
313
298
|
- `annotate({ note, copy })` overrides the feature comments for a value only known at runtime.
|
|
314
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
|
+
|
|
315
308
|
### Gherkin languages and examples
|
|
316
309
|
|
|
317
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.
|
|
@@ -354,12 +347,13 @@ by a human, which is why that mode throws when `CI` is set.
|
|
|
354
347
|
| Function | Purpose |
|
|
355
348
|
|------------------------------------------|--------------------------------------------------------------------------|
|
|
356
349
|
| `storyboardStep(register, sentence, fn)` | Register a sentence literally through `Given`, `When` or `Then` |
|
|
357
|
-
| `capturePageFrame(I, name)` | Full-viewport frame, returns the PNG path
|
|
358
|
-
| `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 |
|
|
359
352
|
| `addStoryboardFrame(I, png)` | Attach the frame to the open card and assert it at `tolerance: 0` |
|
|
360
353
|
| `annotate({ note, copy })` | Runtime override of the open card's note and copyable command |
|
|
361
354
|
| `assertOrUpdateBaseline(I, name)` | The assert-or-rewrite primitive `addStoryboardFrame` relies on |
|
|
362
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 |
|
|
363
357
|
| `parseScenarioLines(featureFile, title)` | Every sentence of a scenario with its `# Chapter` / `# Note` / `# Copy` |
|
|
364
358
|
| `begin`, `panel`, `frame`, `panels` | The board primitives the plugin drives (useful for standalone rendering) |
|
|
365
359
|
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@digital-commons-official/codeceptjs-storyboard",
|
|
3
|
-
"version": "
|
|
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
|
@@ -103,8 +103,6 @@ export interface StoryboardConfig {
|
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
export interface MarkdownPresentation {
|
|
106
|
-
/** Local banner colours, in 3/6-digit hex. Text is selected for contrast when omitted. */
|
|
107
|
-
colors?: { banner?: string, bannerText?: string, accent?: string }
|
|
108
106
|
/** Cards per row; defaults to 2. Use 1 for narrow readers. */
|
|
109
107
|
columns?: 1 | 2
|
|
110
108
|
/** Show chapter/result/replay navigation; defaults to true. */
|
|
@@ -119,7 +117,7 @@ export interface RenderOptions {
|
|
|
119
117
|
outcome?: 'passed' | 'failed'
|
|
120
118
|
/** Inline overrides for standalone rendering. */
|
|
121
119
|
theme?: Theme
|
|
122
|
-
/** Substitute every frame with the same-named
|
|
120
|
+
/** Substitute every frame with the same-named image inside this directory (the reviewed baselines). */
|
|
123
121
|
imageDir?: string
|
|
124
122
|
/** Marks the drifted card in red, adds the expected/diff/actual band and the sentences never reached. */
|
|
125
123
|
failure?: { frame: FailureFrame | null, notReached: ScenarioLine[] } | null
|
|
@@ -140,7 +138,7 @@ declare namespace storyboardPlugin {
|
|
|
140
138
|
function parseScenarioLines (featureFile: string, scenarioTitle: string, options?: { occurrence?: number }): ScenarioLine[]
|
|
141
139
|
/** Open a card (the plugin does it on bddStep.before). */
|
|
142
140
|
function panel (title: string, opts?: Annotation): void
|
|
143
|
-
/** Attach a PNG to the open card without asserting it. */
|
|
141
|
+
/** Attach a PNG or WebP frame to the open card without asserting it. */
|
|
144
142
|
function frame (pngPath: string, opts?: Annotation): void
|
|
145
143
|
/** Merge a runtime note/copy into the open card. */
|
|
146
144
|
function annotate (opts: Annotation): void
|
|
@@ -149,18 +147,20 @@ declare namespace storyboardPlugin {
|
|
|
149
147
|
/** Register a Gherkin sentence literally (cucumber-expression metacharacters escaped). */
|
|
150
148
|
function storyboardStep (register: StepRegister, pattern: string, fn: StepFunction): void
|
|
151
149
|
function storyboardStep (register: StepRegister, pattern: string, opts: Annotation | null, fn: StepFunction): void
|
|
152
|
-
/** 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. */
|
|
153
151
|
function capturePageFrame (I: StoryboardActor, frameName: string): Promise<string>
|
|
154
152
|
/** Element-cropped capture; the frame is exactly the selector's box. */
|
|
155
153
|
function captureElementFrame (I: StoryboardActor, frameName: string, selector: string): Promise<string>
|
|
156
|
-
/** Absolute path
|
|
154
|
+
/** Absolute path of the frame of that name: the WebP the visual helper wrote, else the PNG. */
|
|
157
155
|
function frameOutputPath (frameName: string): string
|
|
158
|
-
/** Assert _output/<name
|
|
159
|
-
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>
|
|
160
158
|
/** Attach the frame to the open card and assert it against its own baseline at tolerance 0. */
|
|
161
159
|
function addStoryboardFrame (I: StoryboardActor, png: string): Promise<void>
|
|
162
160
|
/** Write the captured cards as Markdown (.md) or SVG (.svg), selected by extension; returns outFile. */
|
|
163
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[]>
|
|
164
164
|
/** Re-export of codeceptjs/effects tryTo. */
|
|
165
165
|
function tryTo (fn: () => unknown): Promise<boolean>
|
|
166
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
|
|
@@ -1,71 +1,16 @@
|
|
|
1
|
-
const fs = require('fs')
|
|
2
|
-
const path = require('path')
|
|
3
|
-
const { createHash } = require('crypto')
|
|
4
|
-
|
|
5
|
-
const DEFAULT_COLORS = { banner: '#155e50', bannerText: '#ffffff', accent: '#559b84' }
|
|
6
|
-
|
|
7
1
|
function object (value, keys, label) {
|
|
8
2
|
if (!value || typeof value !== 'object' || Array.isArray(value) || Object.keys(value).some(key => !keys.includes(key))) {
|
|
9
3
|
throw new Error(`storyboard markdown ${label} must be an object with supported keys: ${keys.join(', ')}`)
|
|
10
4
|
}
|
|
11
5
|
}
|
|
12
6
|
|
|
13
|
-
function luminance (color) {
|
|
14
|
-
const hex = color.slice(1)
|
|
15
|
-
const expanded = hex.length === 3 ? [...hex].map(character => character + character).join('') : hex
|
|
16
|
-
return [0, 2, 4].map(start => parseInt(expanded.slice(start, start + 2), 16) / 255)
|
|
17
|
-
.map(value => value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4)
|
|
18
|
-
.reduce((total, value, index) => total + value * [0.2126, 0.7152, 0.0722][index], 0)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function contrast (background, foreground) {
|
|
22
|
-
const values = [luminance(background), luminance(foreground)].sort((a, b) => b - a)
|
|
23
|
-
return (values[0] + 0.05) / (values[1] + 0.05)
|
|
24
|
-
}
|
|
25
|
-
|
|
26
7
|
function normalizeMarkdown (value = {}) {
|
|
27
|
-
object(value, ['
|
|
28
|
-
const
|
|
29
|
-
object(supplied, Object.keys(DEFAULT_COLORS), 'colors')
|
|
30
|
-
const colors = { ...DEFAULT_COLORS, ...supplied }
|
|
31
|
-
for (const [key, color] of Object.entries(colors)) {
|
|
32
|
-
if (typeof color !== 'string' || !/^#(?:[\da-f]{3}|[\da-f]{6})$/i.test(color)) {
|
|
33
|
-
throw new Error(`storyboard markdown colors.${key} must be a 3- or 6-digit hex colour`)
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (supplied.banner !== undefined && supplied.bannerText === undefined) {
|
|
37
|
-
colors.bannerText = contrast(colors.banner, '#ffffff') >= contrast(colors.banner, '#111111') ? '#ffffff' : '#111111'
|
|
38
|
-
if (contrast(colors.banner, colors.bannerText) < 4.5) colors.bannerText = '#000000'
|
|
39
|
-
}
|
|
40
|
-
if (contrast(colors.banner, colors.bannerText) < 4.5) throw new Error('storyboard markdown banner text contrast must be at least 4.5:1')
|
|
41
|
-
const options = { columns: 2, tableOfContents: true, technicalDetails: 'collapsed', ...value, colors }
|
|
8
|
+
object(value, ['columns', 'tableOfContents', 'technicalDetails'], 'options')
|
|
9
|
+
const options = { columns: 2, tableOfContents: true, technicalDetails: 'collapsed', ...value }
|
|
42
10
|
if (![1, 2].includes(options.columns)) throw new Error('storyboard markdown columns must be 1 or 2')
|
|
43
11
|
if (typeof options.tableOfContents !== 'boolean') throw new Error('storyboard markdown tableOfContents must be a boolean')
|
|
44
12
|
if (!['collapsed', 'expanded'].includes(options.technicalDetails)) throw new Error('storyboard markdown technicalDetails must be "collapsed" or "expanded"')
|
|
45
13
|
return options
|
|
46
14
|
}
|
|
47
15
|
|
|
48
|
-
|
|
49
|
-
return String(value).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''')
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Banners carry decoration only: the full chapter title also remains native
|
|
53
|
-
// Markdown, selectable and readable when images are unavailable or small.
|
|
54
|
-
function writeBanner (outFile, title, number, colors) {
|
|
55
|
-
const characters = Array.from(String(title).replace(/\s+/g, ' ').trim())
|
|
56
|
-
const label = characters.length > 38 ? characters.slice(0, 37).join('') + '…' : characters.join('')
|
|
57
|
-
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="1120" height="104" viewBox="0 0 1120 104" role="img">
|
|
58
|
-
<title>${xml(title)}</title>
|
|
59
|
-
<rect width="1120" height="104" rx="8" fill="${colors.banner}"/>
|
|
60
|
-
<path d="M940 0h70l104 104h-70z M1060 0h30l30 30v74z" fill="${colors.accent}"/>
|
|
61
|
-
<text x="28" y="32" font-family="Arial, sans-serif" font-size="15" fill="${colors.bannerText}">${number ? 'Chapter ' + number : 'Journey'}</text>
|
|
62
|
-
<text x="28" y="73" font-family="Arial, sans-serif" font-size="26" font-weight="700" fill="${colors.bannerText}">${xml(label)}</text>
|
|
63
|
-
</svg>\n`
|
|
64
|
-
const digest = createHash('sha256').update(svg).digest('hex').slice(0, 20)
|
|
65
|
-
const file = path.join(path.dirname(outFile), '_storyboard-assets', `banner-${digest}.svg`)
|
|
66
|
-
fs.mkdirSync(path.dirname(file), { recursive: true })
|
|
67
|
-
fs.writeFileSync(file, svg)
|
|
68
|
-
return file
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
module.exports = { normalizeMarkdown, writeBanner }
|
|
16
|
+
module.exports = { normalizeMarkdown }
|
package/src/markdown.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const fs = require('fs')
|
|
2
2
|
const path = require('path')
|
|
3
|
-
const { normalizeMarkdown
|
|
3
|
+
const { normalizeMarkdown } = require('./markdown-presentation')
|
|
4
4
|
|
|
5
5
|
// Annotations are plain text, not another source of Markdown or HTML.
|
|
6
6
|
function text (value) {
|
|
@@ -110,7 +110,6 @@ function renderMarkdown (board, outFile, options = {}) {
|
|
|
110
110
|
'[Replay and source](#storyboard-replay)'
|
|
111
111
|
].join(' · '))
|
|
112
112
|
}
|
|
113
|
-
if (!chapters.length) chunks.push(`)})`)
|
|
114
113
|
let pending = []
|
|
115
114
|
let chapterNumber = 0
|
|
116
115
|
const flush = () => {
|
|
@@ -121,8 +120,7 @@ function renderMarkdown (board, outFile, options = {}) {
|
|
|
121
120
|
if (card.chapter) {
|
|
122
121
|
flush()
|
|
123
122
|
chapterNumber++
|
|
124
|
-
|
|
125
|
-
chunks.push(`<a name="storyboard-chapter-${chapterNumber}"></a>`, `})`, `## ${caption(card.chapter).trim()}`)
|
|
123
|
+
chunks.push(`<a name="storyboard-chapter-${chapterNumber}"></a>`, `## ${caption(card.chapter).trim()}`)
|
|
126
124
|
}
|
|
127
125
|
if (failure && failure.frame && path.basename(card.file) === failure.frame.name) {
|
|
128
126
|
flush()
|