shapewall 0.2.4 → 0.3.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.
Files changed (105) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/README.md +35 -1
  3. package/dist/agent/package-lock.json +2 -2
  4. package/dist/client/assets/BoardChat-CupOxioV.js +27 -0
  5. package/dist/client/assets/{BuzzDialog-CZdwIQLF.js → BuzzDialog-Ds7Thpfe.js} +1 -1
  6. package/dist/client/assets/ChecksPanel-BqrS02Ih.js +1 -0
  7. package/dist/client/assets/History-CQG3NQOC.js +1 -0
  8. package/dist/client/assets/{LocalSettings-B5gtfKEl.js → LocalSettings-CSNMhNyV.js} +1 -1
  9. package/dist/client/assets/{Management-BxEUZAmI.js → Management-CJVNAqAW.js} +1 -1
  10. package/dist/client/assets/QuickConnect-B-4pUgXH.js +1 -0
  11. package/dist/client/assets/{ShareDialog-CdVAreaw.js → ShareDialog-BouvNraR.js} +2 -2
  12. package/dist/client/assets/{app-update-dialog-BONq9YFp.js → app-update-dialog-rhaw0MRk.js} +1 -1
  13. package/dist/client/assets/{circle-x-BmT6vHKl.js → circle-x-PRxj7kPi.js} +1 -1
  14. package/dist/client/assets/index-CXtuJi1K.css +2 -0
  15. package/dist/client/assets/index-DKOCIQiq.js +37 -0
  16. package/dist/client/assets/{refresh-cw-ugt3cWKH.js → refresh-cw-aeOBDuGQ.js} +1 -1
  17. package/dist/client/assets/{square-_KG6tM5A.js → shield-check-DuPBJ6H_.js} +1 -1
  18. package/dist/client/assets/ui-Cg4TTrec.js +41 -0
  19. package/dist/client/build-info.json +1 -1
  20. package/dist/client/index.html +3 -3
  21. package/dist/client/sw.js +2 -2
  22. package/dist/console/assets/LocalAccountsApp-DOKZI6Lt.js +1 -0
  23. package/dist/console/assets/LocalConsole-DXzldLW9.js +1 -0
  24. package/dist/console/assets/{app-update-dialog-o4QY_jL9.js → app-update-dialog-CrqIMEwN.js} +1 -1
  25. package/dist/console/assets/console-76WovHlh.js +55 -0
  26. package/dist/console/assets/console-Fp4k9iyx.css +2 -0
  27. package/dist/console/build-info.json +1 -1
  28. package/dist/console/console.html +2 -2
  29. package/docs/agent-hardening.md +19 -0
  30. package/docs/agent-workflow.md +33 -0
  31. package/docs/agents.md +112 -0
  32. package/docs/config.md +41 -0
  33. package/docs/deployment.md +3 -0
  34. package/docs/hosted.md +10 -0
  35. package/docs/pull-requests.md +192 -0
  36. package/package.json +2 -1
  37. package/src/agent/backends/acp.js +23 -6
  38. package/src/agent/batch-fixes.js +45 -0
  39. package/src/agent/batch-queue.js +204 -0
  40. package/src/agent/branch-mode.js +753 -0
  41. package/src/agent/config.js +6 -0
  42. package/src/agent/fix-prompt.js +5 -0
  43. package/src/agent/routes.js +850 -185
  44. package/src/agent/run.js +8 -1
  45. package/src/agent/settings-routes.js +69 -1
  46. package/src/agent/starter/worktree.js +254 -25
  47. package/src/agent/threads.js +2 -7
  48. package/src/agent/tools.js +51 -0
  49. package/src/agent/user-settings.js +33 -2
  50. package/src/ci/capture.js +103 -0
  51. package/src/ci/compare.js +267 -0
  52. package/src/ci/github.js +307 -0
  53. package/src/ci/images.js +76 -0
  54. package/src/ci/settings.js +211 -0
  55. package/src/ci/start.js +148 -0
  56. package/src/ci/summary.js +262 -0
  57. package/src/ci/workflow.js +238 -0
  58. package/src/cli/args.js +24 -1
  59. package/src/cli/ci.js +229 -0
  60. package/src/cli/help.js +23 -1
  61. package/src/cli/init.js +33 -1
  62. package/src/cli.js +6 -2
  63. package/src/comment-drawing.js +161 -0
  64. package/src/comment-fields.js +51 -0
  65. package/src/comment-media.js +528 -0
  66. package/src/config.js +7 -0
  67. package/src/doctor.js +23 -0
  68. package/src/export.js +8 -0
  69. package/src/feedback.js +89 -12
  70. package/src/github.js +3 -3
  71. package/src/hosted/admin.js +36 -4
  72. package/src/hosted/app.js +5 -0
  73. package/src/hosted/board-routes.js +63 -7
  74. package/src/hosted/clean-worker.js +22 -4
  75. package/src/hosted/cleaner.js +4 -1
  76. package/src/hosted/comment-files.js +200 -0
  77. package/src/hosted/comments.js +68 -15
  78. package/src/hosted/db.js +32 -0
  79. package/src/hosted/guest-view.js +9 -0
  80. package/src/hosted/import.js +213 -18
  81. package/src/hosted/operator.js +3 -1
  82. package/src/hosted/projects.js +3 -2
  83. package/src/hosted/push.js +6 -3
  84. package/src/hosted/retention.js +18 -0
  85. package/src/hosted/tenancy.js +1 -0
  86. package/src/hosted/token-api.js +110 -9
  87. package/src/image-sniff.js +11 -0
  88. package/src/mcp.js +48 -3
  89. package/src/remote/remote-mcp.js +2 -2
  90. package/src/remote/remote.js +6 -2
  91. package/src/remote/share.js +77 -7
  92. package/src/server/app.js +18 -1
  93. package/src/store.js +119 -33
  94. package/src/watch.js +4 -0
  95. package/dist/client/assets/BoardChat-C0a3DDjv.js +0 -28
  96. package/dist/client/assets/ChecksPanel-BHWgwJWO.js +0 -1
  97. package/dist/client/assets/History-C01qLFpP.js +0 -1
  98. package/dist/client/assets/QuickConnect-BzTM3UNO.js +0 -1
  99. package/dist/client/assets/index-C1IsGdGs.js +0 -29
  100. package/dist/client/assets/index-DnsWelgZ.css +0 -2
  101. package/dist/client/assets/ui-BAGBTB_P.js +0 -41
  102. package/dist/console/assets/LocalAccountsApp-DtfTDbQg.js +0 -1
  103. package/dist/console/assets/LocalConsole-BPpsxDq_.js +0 -1
  104. package/dist/console/assets/console-DeOji9Zv.css +0 -2
  105. package/dist/console/assets/console-MvFrlUCC.js +0 -51
@@ -20,9 +20,24 @@ import path from 'node:path'
20
20
  import { splitCommand } from '../watch.js'
21
21
  import { SETTINGS_SOURCE } from './acp/command.js'
22
22
  import { DENIED_KEY_VARS, KEY_VAR_RE, validateAgent } from './config.js'
23
+ import { cleanBranchPrefix } from './branch-mode.js'
24
+ import { cleanBatchFixes } from './batch-fixes.js'
23
25
 
24
26
  export const SETTINGS_FILE_VAR = 'SHAPEWALL_AGENT_SETTINGS_FILE'
25
- export const ENTRY_KEYS = ['backend', 'baseUrl', 'model', 'apiKeyEnv', 'label', 'vision', 'keyHosts', 'acpCommand', 'profiles']
27
+ export const ENTRY_KEYS = [
28
+ 'backend',
29
+ 'baseUrl',
30
+ 'model',
31
+ 'apiKeyEnv',
32
+ 'label',
33
+ 'vision',
34
+ 'keyHosts',
35
+ 'acpCommand',
36
+ 'profiles',
37
+ 'branchMode',
38
+ 'branchPrefix',
39
+ 'batchFixes',
40
+ ]
26
41
  const SECRET_KEYS = ['apiKey', 'key', 'token', 'secret', 'password']
27
42
  const ACP_COMMAND_MAX = 2000
28
43
  const KEY_HOSTS_MAX = 20
@@ -63,7 +78,7 @@ export function validateUserEntry(raw) {
63
78
  problems.push(`${key}: keys are never saved; set the key in the environment and name its variable in apiKeyEnv`)
64
79
  else if (!ENTRY_KEYS.includes(key) && key !== 'updatedAt') problems.push(`${key}: not an agent setting`)
65
80
  }
66
- const { keyHosts, acpCommand, profiles, updatedAt, ...rest } = given
81
+ const { keyHosts, acpCommand, profiles, updatedAt, branchMode, branchPrefix, batchFixes, ...rest } = given
67
82
  const fields = {}
68
83
  for (const key of ['backend', 'baseUrl', 'model', 'apiKeyEnv', 'label', 'vision']) if (rest[key] !== undefined) fields[key] = rest[key]
69
84
  const agentProblems = []
@@ -135,6 +150,22 @@ export function validateUserEntry(raw) {
135
150
  if (checkedProfiles.length) entry.profiles = checkedProfiles
136
151
  }
137
152
  }
153
+ // Agent branch mode (branch-mode.js): the agent works on a branch in a worktree.
154
+ if (branchMode !== undefined) {
155
+ if (typeof branchMode !== 'boolean') problems.push('branchMode: true or false')
156
+ else entry.branchMode = branchMode
157
+ }
158
+ if (branchPrefix !== undefined) {
159
+ const prefix = cleanBranchPrefix(branchPrefix)
160
+ if (!prefix) problems.push('branchPrefix: letters, digits, "-", "_" and "/", at most 40 characters, such as "shapewall/"')
161
+ else entry.branchPrefix = prefix
162
+ }
163
+ // Batch fixes (batch-fixes.js): { enabled, max }.
164
+ if (batchFixes !== undefined) {
165
+ const checked = cleanBatchFixes(batchFixes)
166
+ problems.push(...checked.problems)
167
+ if (checked.value) entry.batchFixes = checked.value
168
+ }
138
169
  if (typeof updatedAt === 'string') entry.updatedAt = updatedAt
139
170
  return { entry: problems.length ? null : entry, problems }
140
171
  }
@@ -0,0 +1,103 @@
1
+ // One side of a pull request comparison: the board's pages captured from one
2
+ // base URL with the normal capture (src/capture/capture.js), into a capture
3
+ // folder (manifest.json and <page>.<viewport>.png) plus side.json.
4
+ //
5
+ // For stable shots the capture runs as configured (fixed viewports, reduced
6
+ // motion, the board's actions, auth and setup) with two additions on every
7
+ // page, as a last waitForFunction action: the ci.ignore selectors are hidden
8
+ // (visibility: hidden, so the layout does not move) and the text caret is
9
+ // made transparent, and the shot waits until web fonts have loaded. Live HTML,
10
+ // versions and the link check are off: a comparison needs the screenshots only.
11
+
12
+ import fs from 'node:fs'
13
+ import path from 'node:path'
14
+ import { runCapture } from '../capture/capture.js'
15
+ import { CI_DEFAULTS } from './settings.js'
16
+
17
+ // The expression the extra action waits on: true once fonts are loaded; it
18
+ // hides the ignored elements on every poll, so ones that render late are hidden too.
19
+ export function settleExpression(ignore = []) {
20
+ const selectors = JSON.stringify(ignore)
21
+ return `(() => {
22
+ const ignore = ${selectors};
23
+ if (!document.getElementById('shapewall-ci-style')) {
24
+ const style = document.createElement('style');
25
+ style.id = 'shapewall-ci-style';
26
+ style.textContent = '*, *::before, *::after { caret-color: transparent !important; }';
27
+ (document.head || document.documentElement).appendChild(style);
28
+ }
29
+ for (const selector of ignore) {
30
+ let found = [];
31
+ try { found = document.querySelectorAll(selector); } catch { found = []; }
32
+ for (const element of found) element.style.setProperty('visibility', 'hidden', 'important');
33
+ }
34
+ return !document.fonts || document.fonts.status === 'loaded';
35
+ })()`
36
+ }
37
+
38
+ const ciSettings = (config) => config.ci || { ...CI_DEFAULTS, pages: null, viewports: null, ignore: [], start: null }
39
+
40
+ // The pages a comparison covers: ci.pages (else every page), each with only
41
+ // the ci.viewports it has, at most `maxPages`.
42
+ export function ciPages(config, board, { maxPages } = {}) {
43
+ const ci = ciSettings(config)
44
+ const limit = maxPages ?? ci.maxPages
45
+ const wanted = ci.pages ? new Set(ci.pages) : null
46
+ const pages = board.pages
47
+ .filter((page) => !wanted || wanted.has(page.id))
48
+ .map((page) => ({ ...page, viewports: ci.viewports ? page.viewports.filter((name) => ci.viewports.includes(name)) : page.viewports }))
49
+ .filter((page) => page.viewports.length)
50
+ return { pages: pages.slice(0, limit), left: Math.max(0, pages.length - limit) }
51
+ }
52
+
53
+ // A copy of the config that captures `board` from `baseUrl` into `outputDir`.
54
+ export function ciConfig(config, board, { baseUrl, outputDir, maxPages } = {}) {
55
+ const ci = ciSettings(config)
56
+ const { pages } = ciPages(config, board, { maxPages })
57
+ const settle = { type: 'waitForFunction', expression: settleExpression(ci.ignore), timeout: 15000 }
58
+ const ciBoard = {
59
+ ...board,
60
+ baseUrl,
61
+ environments: [{ ...board.environments[0], baseUrl }],
62
+ pages: pages.map((page) => ({ ...page, actions: [...(page.actions || []), settle] })),
63
+ }
64
+ return { ...config, outputDir, versions: 0, liveHtml: false, boards: [ciBoard] }
65
+ }
66
+
67
+ // Captures one side into `dir` (replaced). Never throws for a capture that
68
+ // failed: side.json then holds the error and the folder has no manifest.
69
+ // Returns { ok, dir, error, shots, failed }.
70
+ export async function captureSide({ config, board, baseUrl, dir, maxPages, log = () => {}, chromium = null }) {
71
+ const target = path.resolve(dir)
72
+ const work = `${target}.work`
73
+ fs.rmSync(target, { recursive: true, force: true })
74
+ fs.rmSync(work, { recursive: true, force: true })
75
+ fs.mkdirSync(target, { recursive: true })
76
+ const sideConfig = ciConfig(config, board, { baseUrl, outputDir: work, maxPages })
77
+ const sideBoard = sideConfig.boards[0]
78
+ const side = {
79
+ source: baseUrl,
80
+ board: board.id,
81
+ pages: sideBoard.pages.map((page) => ({ id: page.id, title: page.title, path: page.path })),
82
+ error: null,
83
+ }
84
+ let result = null
85
+ try {
86
+ result = await runCapture({ config: sideConfig, boardId: board.id, log, chromium, checkLinks: false })
87
+ const captured = path.join(work, board.id, 'captures')
88
+ for (const name of fs.readdirSync(captured)) {
89
+ // Screenshots and the manifest only; thumbnails are made again at the comment's size.
90
+ if (name === 'manifest.json' || (name.endsWith('.png') && !name.endsWith('.thumb.png')))
91
+ fs.renameSync(path.join(captured, name), path.join(target, name))
92
+ }
93
+ } catch (error) {
94
+ side.error = String(error?.message || error)
95
+ .split('\n')[0]
96
+ .slice(0, 300)
97
+ log(`Capture of ${baseUrl} failed: ${side.error}`)
98
+ } finally {
99
+ fs.rmSync(work, { recursive: true, force: true })
100
+ }
101
+ fs.writeFileSync(path.join(target, 'side.json'), `${JSON.stringify(side, null, 2)}\n`)
102
+ return { ok: !side.error, dir: target, error: side.error, shots: result?.total || 0, failed: result?.failed || 0 }
103
+ }
@@ -0,0 +1,267 @@
1
+ // Compares two capture folders (a "base" and a "head": the folder
2
+ // `shapewall capture` writes, .shapewall/<board>/captures/, or the one
3
+ // `shapewall ci capture` writes) page by page and viewport by viewport, and
4
+ // writes a result folder:
5
+ //
6
+ // summary.json what changed (schema below, versioned)
7
+ // summary.md the pull request comment body (src/ci/summary.js)
8
+ // images/<page>.<viewport>/before.png, after.png, diff.png and a
9
+ // .thumb.png of each, for changed shots only
10
+ //
11
+ // A capture folder holds manifest.json and <page>.<viewport>.png files. One
12
+ // written by `shapewall ci capture` also has side.json: where it was captured
13
+ // from, the pages' titles and paths, and the error when the whole side failed.
14
+
15
+ import fs from 'node:fs'
16
+ import path from 'node:path'
17
+ import { captureWarningText } from '../capture/network-summary.js'
18
+ import { compareShots, thumbnail } from './images.js'
19
+ import { renderSummary } from './summary.js'
20
+
21
+ export const SUMMARY_SCHEMA = 'shapewall-ci-summary'
22
+ export const SUMMARY_VERSION = 1
23
+ const SAFE_NAME = /^[a-z0-9][a-z0-9-]{0,63}$/
24
+ const VIEWPORT_NAME = /^[a-z][a-z0-9-]{0,31}$/
25
+
26
+ function readJson(file) {
27
+ try {
28
+ return JSON.parse(fs.readFileSync(file, 'utf8'))
29
+ } catch {
30
+ return null
31
+ }
32
+ }
33
+
34
+ // { manifest, side } of a capture folder; manifest is null when there is none.
35
+ export function readCaptureDir(dir) {
36
+ const manifest = readJson(path.join(dir, 'manifest.json'))
37
+ const side = readJson(path.join(dir, 'side.json')) || {}
38
+ return { manifest: manifest && typeof manifest.pages === 'object' && manifest.pages ? manifest : null, side }
39
+ }
40
+
41
+ // Page order and titles: the config's board when given, then side.json, then
42
+ // the manifests' own order.
43
+ function pageList({ board, base, head, maxPages }) {
44
+ const meta = new Map()
45
+ const add = (id, info = {}) => {
46
+ if (typeof id !== 'string' || !SAFE_NAME.test(id)) return
47
+ const known = meta.get(id) || { id, title: id, path: null }
48
+ meta.set(id, {
49
+ id,
50
+ title: typeof info.title === 'string' && info.title ? info.title.slice(0, 120) : known.title,
51
+ path: typeof info.path === 'string' ? info.path.slice(0, 300) : known.path,
52
+ })
53
+ }
54
+ for (const page of board?.pages || []) add(page.id, page)
55
+ for (const side of [head.side, base.side]) for (const page of Array.isArray(side.pages) ? side.pages : []) add(page?.id, page)
56
+ for (const side of [head, base]) for (const id of Object.keys(side.manifest?.pages || {})) add(id)
57
+ const listed = new Set([...Object.keys(head.manifest?.pages || {}), ...Object.keys(base.manifest?.pages || {})])
58
+ // Without manifests (a side failed as a whole), the pages that side was asked for.
59
+ for (const side of [head.side, base.side]) for (const page of Array.isArray(side.pages) ? side.pages : []) listed.add(page?.id)
60
+ const all = [...meta.values()].filter((page) => listed.has(page.id))
61
+ return { pages: all.slice(0, maxPages), truncated: Math.max(0, all.length - maxPages) }
62
+ }
63
+
64
+ function viewportSize(manifest, name) {
65
+ const size = manifest?.viewports?.[name]
66
+ return size && Number.isInteger(size.width) && Number.isInteger(size.height) ? { width: size.width, height: size.height } : null
67
+ }
68
+
69
+ const shotWarnings = (entry) => {
70
+ if (!entry) return []
71
+ const warnings = []
72
+ const network = captureWarningText(entry.networkSummary)
73
+ if (network) warnings.push(`network: ${network}`)
74
+ if (Array.isArray(entry.skippedActions) && entry.skippedActions.length)
75
+ warnings.push(`skipped optional action${entry.skippedActions.length === 1 ? '' : 's'} ${entry.skippedActions.join(', ')}`)
76
+ return warnings
77
+ }
78
+
79
+ const errorText = (value) =>
80
+ String(value || 'failed')
81
+ .split('\n')[0]
82
+ .slice(0, 300)
83
+
84
+ // Compares and writes the result folder. `board` (optional) is the config's
85
+ // board, for titles and order. Returns the summary.
86
+ export function compareCaptures({
87
+ baseDir,
88
+ headDir,
89
+ out,
90
+ threshold,
91
+ board = null,
92
+ maxPages = Infinity,
93
+ base: baseSource = null,
94
+ head: headSource = null,
95
+ now = new Date(),
96
+ }) {
97
+ const base = readCaptureDir(baseDir)
98
+ const head = readCaptureDir(headDir)
99
+ const target = path.resolve(out)
100
+ fs.rmSync(path.join(target, 'images'), { recursive: true, force: true })
101
+ fs.mkdirSync(target, { recursive: true })
102
+ const { pages, truncated } = pageList({ board, base, head, maxPages })
103
+ const failures = []
104
+ const warnings = []
105
+ for (const [name, side] of [
106
+ ['base', base],
107
+ ['head', head],
108
+ ]) {
109
+ if (!side.manifest)
110
+ failures.push({ side: name, page: null, viewport: null, error: errorText(side.side.error || 'no captures in this folder') })
111
+ }
112
+ const shots = []
113
+ const pageIds = new Set(pages.map((page) => page.id))
114
+ const newPages = []
115
+ const removedPages = []
116
+ for (const page of pages) {
117
+ const baseEntries = base.manifest?.pages?.[page.id] || null
118
+ const headEntries = head.manifest?.pages?.[page.id] || null
119
+ const viewports = [...new Set([...Object.keys(headEntries || {}), ...Object.keys(baseEntries || {})])].filter((name) =>
120
+ VIEWPORT_NAME.test(name),
121
+ )
122
+ // A page only one side has, or that answers 404 on one side only, is new or removed.
123
+ const missingOn = (entries, other) =>
124
+ Boolean(other) &&
125
+ Object.values(other).some((entry) => entry?.status === 'ok') &&
126
+ (!entries || Object.values(entries).every((entry) => entry?.status === 'failed' && entry.httpStatus === 404))
127
+ const isNew = base.manifest && head.manifest && missingOn(baseEntries, headEntries)
128
+ const isRemoved = base.manifest && head.manifest && missingOn(headEntries, baseEntries)
129
+ if (isNew) newPages.push(page.id)
130
+ if (isRemoved) removedPages.push(page.id)
131
+ for (const viewport of viewports) {
132
+ const b = baseEntries?.[viewport] || null
133
+ const h = headEntries?.[viewport] || null
134
+ const size = viewportSize(head.manifest, viewport) || viewportSize(base.manifest, viewport)
135
+ const shot = {
136
+ page: page.id,
137
+ title: page.title,
138
+ path: page.path,
139
+ viewport,
140
+ viewportSize: size,
141
+ status: 'unchanged',
142
+ changed: false,
143
+ ratio: 0,
144
+ changedPixels: 0,
145
+ totalPixels: 0,
146
+ dimensionsChanged: false,
147
+ base: null,
148
+ head: null,
149
+ images: null,
150
+ error: null,
151
+ warnings: [...shotWarnings(b).map((text) => `base ${text}`), ...shotWarnings(h).map((text) => `head ${text}`)],
152
+ }
153
+ const name = `${page.id}.${viewport}`
154
+ const imagesDir = path.join(target, 'images', name)
155
+ const write = (file, buffer) => {
156
+ fs.mkdirSync(imagesDir, { recursive: true })
157
+ fs.writeFileSync(path.join(imagesDir, file), buffer)
158
+ return `images/${name}/${file}`
159
+ }
160
+ const read = (dir, entry) => {
161
+ if (entry?.status !== 'ok') return null
162
+ try {
163
+ return fs.readFileSync(path.join(dir, `${name}.png`))
164
+ } catch {
165
+ return null
166
+ }
167
+ }
168
+ const before = read(baseDir, b)
169
+ const after = read(headDir, h)
170
+ if (isNew && after) {
171
+ Object.assign(shot, { status: 'new', changed: true })
172
+ shot.images = { after: write('after.png', after), afterThumb: write('after.thumb.png', thumbnail(after)) }
173
+ } else if (isRemoved && before) {
174
+ Object.assign(shot, { status: 'removed', changed: true })
175
+ shot.images = { before: write('before.png', before), beforeThumb: write('before.thumb.png', thumbnail(before)) }
176
+ } else if (!before || !after) {
177
+ shot.status = 'failed'
178
+ const which = []
179
+ if (!before)
180
+ which.push({
181
+ side: 'base',
182
+ error: b
183
+ ? b.status === 'ok'
184
+ ? 'the screenshot file is missing'
185
+ : errorText(b.error)
186
+ : base.manifest
187
+ ? 'not captured'
188
+ : errorText(base.side.error || 'no captures'),
189
+ })
190
+ if (!after)
191
+ which.push({
192
+ side: 'head',
193
+ error: h
194
+ ? h.status === 'ok'
195
+ ? 'the screenshot file is missing'
196
+ : errorText(h.error)
197
+ : head.manifest
198
+ ? 'not captured'
199
+ : errorText(head.side.error || 'no captures'),
200
+ })
201
+ shot.error = which.map((item) => `${item.side}: ${item.error}`).join('; ')
202
+ // A side that failed as a whole is reported once, not per shot.
203
+ for (const item of which)
204
+ if ((item.side === 'base' ? base : head).manifest) failures.push({ side: item.side, page: page.id, viewport, error: item.error })
205
+ } else {
206
+ let result
207
+ try {
208
+ result = compareShots(before, after)
209
+ } catch (error) {
210
+ shot.status = 'failed'
211
+ shot.error = `the screenshots could not be compared (${errorText(error.message)})`
212
+ failures.push({ side: null, page: page.id, viewport, error: shot.error })
213
+ }
214
+ if (result) {
215
+ shot.ratio = result.ratio
216
+ shot.changedPixels = result.changed
217
+ shot.totalPixels = result.total
218
+ shot.base = result.base
219
+ shot.head = result.head
220
+ shot.dimensionsChanged = result.dimensionsChanged
221
+ shot.changed = result.dimensionsChanged || (result.changed > 0 && result.ratio >= threshold)
222
+ shot.status = shot.changed ? 'changed' : 'unchanged'
223
+ if (shot.changed) {
224
+ shot.images = {
225
+ before: write('before.png', before),
226
+ after: write('after.png', after),
227
+ diff: write('diff.png', result.png),
228
+ beforeThumb: write('before.thumb.png', thumbnail(before)),
229
+ afterThumb: write('after.thumb.png', thumbnail(after)),
230
+ diffThumb: write('diff.thumb.png', thumbnail(result.png)),
231
+ }
232
+ }
233
+ }
234
+ }
235
+ for (const text of shot.warnings) warnings.push({ page: page.id, viewport, warning: text })
236
+ shots.push(shot)
237
+ }
238
+ }
239
+ const changedPageIds = new Set(shots.filter((shot) => shot.changed).map((shot) => shot.page))
240
+ const summary = {
241
+ schema: SUMMARY_SCHEMA,
242
+ version: SUMMARY_VERSION,
243
+ generatedAt: now.toISOString(),
244
+ threshold,
245
+ base: { source: baseSource || base.side.source || null, capturedAt: base.manifest?.capturedAt || null },
246
+ head: { source: headSource || head.side.source || null, capturedAt: head.manifest?.capturedAt || null },
247
+ totals: {
248
+ pages: pageIds.size,
249
+ changedPages: changedPageIds.size,
250
+ shots: shots.length,
251
+ changedShots: shots.filter((shot) => shot.changed).length,
252
+ unchangedShots: shots.filter((shot) => shot.status === 'unchanged').length,
253
+ failedShots: shots.filter((shot) => shot.status === 'failed').length,
254
+ newPages: newPages.length,
255
+ removedPages: removedPages.length,
256
+ pagesNotCompared: truncated,
257
+ },
258
+ newPages,
259
+ removedPages,
260
+ failures,
261
+ warnings,
262
+ shots,
263
+ }
264
+ fs.writeFileSync(path.join(target, 'summary.json'), `${JSON.stringify(summary, null, 2)}\n`)
265
+ fs.writeFileSync(path.join(target, 'summary.md'), renderSummary(summary))
266
+ return summary
267
+ }