shapewall 0.2.4 → 0.3.1
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/CHANGELOG.md +45 -0
- package/README.md +35 -1
- package/dist/agent/package-lock.json +2 -2
- package/dist/client/assets/BoardChat-V1BCXdDB.js +27 -0
- package/dist/client/assets/{BuzzDialog-CZdwIQLF.js → BuzzDialog-Ds7Thpfe.js} +1 -1
- package/dist/client/assets/ChecksPanel-Cm22bCdQ.js +1 -0
- package/dist/client/assets/History-U0bD1BjA.js +1 -0
- package/dist/client/assets/{LocalSettings-B5gtfKEl.js → LocalSettings-DOSuW7VC.js} +1 -1
- package/dist/client/assets/{Management-BxEUZAmI.js → Management-BFcKFdbp.js} +1 -1
- package/dist/client/assets/QuickConnect-DyfPSWbV.js +1 -0
- package/dist/client/assets/{ShareDialog-CdVAreaw.js → ShareDialog-BouvNraR.js} +2 -2
- package/dist/client/assets/{app-update-dialog-BONq9YFp.js → app-update-dialog-DH2Mgby8.js} +1 -1
- package/dist/client/assets/{circle-x-BmT6vHKl.js → circle-x-PRxj7kPi.js} +1 -1
- package/dist/client/assets/index-C9S7HL_o.js +37 -0
- package/dist/client/assets/index-CXtuJi1K.css +2 -0
- package/dist/client/assets/{refresh-cw-ugt3cWKH.js → refresh-cw-aeOBDuGQ.js} +1 -1
- package/dist/client/assets/{square-_KG6tM5A.js → shield-check-DuPBJ6H_.js} +1 -1
- package/dist/client/assets/ui-Cg4TTrec.js +41 -0
- package/dist/client/build-info.json +1 -1
- package/dist/client/index.html +3 -3
- package/dist/client/sw.js +2 -2
- package/dist/console/assets/LocalAccountsApp-lJjkhUcH.js +1 -0
- package/dist/console/assets/LocalConsole-BZHqYKnt.js +1 -0
- package/dist/console/assets/{app-update-dialog-o4QY_jL9.js → app-update-dialog-DCM9sYE3.js} +1 -1
- package/dist/console/assets/console-C2LsvEco.js +55 -0
- package/dist/console/assets/console-Fp4k9iyx.css +2 -0
- package/dist/console/build-info.json +1 -1
- package/dist/console/console.html +2 -2
- package/docs/agent-hardening.md +19 -0
- package/docs/agent-workflow.md +33 -0
- package/docs/agents.md +112 -0
- package/docs/config.md +41 -0
- package/docs/deployment.md +3 -0
- package/docs/hosted.md +10 -0
- package/docs/pull-requests.md +193 -0
- package/package.json +2 -1
- package/src/agent/backends/acp.js +23 -6
- package/src/agent/batch-fixes.js +45 -0
- package/src/agent/batch-queue.js +204 -0
- package/src/agent/branch-mode.js +753 -0
- package/src/agent/config.js +6 -0
- package/src/agent/fix-prompt.js +5 -0
- package/src/agent/routes.js +850 -185
- package/src/agent/run.js +8 -1
- package/src/agent/settings-routes.js +69 -1
- package/src/agent/starter/worktree.js +286 -29
- package/src/agent/threads.js +2 -7
- package/src/agent/tools.js +51 -0
- package/src/agent/user-settings.js +33 -2
- package/src/ci/capture.js +103 -0
- package/src/ci/compare.js +267 -0
- package/src/ci/github.js +307 -0
- package/src/ci/images.js +76 -0
- package/src/ci/settings.js +211 -0
- package/src/ci/start.js +148 -0
- package/src/ci/summary.js +262 -0
- package/src/ci/workflow.js +249 -0
- package/src/cli/args.js +24 -1
- package/src/cli/ci.js +229 -0
- package/src/cli/help.js +23 -1
- package/src/cli/init.js +36 -1
- package/src/cli.js +6 -2
- package/src/comment-drawing.js +161 -0
- package/src/comment-fields.js +51 -0
- package/src/comment-media.js +528 -0
- package/src/config.js +7 -0
- package/src/doctor.js +23 -0
- package/src/export.js +8 -0
- package/src/feedback.js +89 -12
- package/src/github.js +3 -3
- package/src/hosted/admin.js +36 -4
- package/src/hosted/app.js +5 -0
- package/src/hosted/board-routes.js +63 -7
- package/src/hosted/clean-worker.js +22 -4
- package/src/hosted/cleaner.js +4 -1
- package/src/hosted/comment-files.js +200 -0
- package/src/hosted/comments.js +68 -15
- package/src/hosted/db.js +32 -0
- package/src/hosted/guest-view.js +9 -0
- package/src/hosted/import.js +213 -18
- package/src/hosted/operator.js +3 -1
- package/src/hosted/projects.js +3 -2
- package/src/hosted/push.js +6 -3
- package/src/hosted/retention.js +18 -0
- package/src/hosted/tenancy.js +1 -0
- package/src/hosted/token-api.js +110 -9
- package/src/image-sniff.js +11 -0
- package/src/mcp.js +48 -3
- package/src/remote/remote-mcp.js +2 -2
- package/src/remote/remote.js +6 -2
- package/src/remote/share.js +77 -7
- package/src/server/app.js +18 -1
- package/src/store.js +119 -33
- package/src/watch.js +4 -0
- package/dist/client/assets/BoardChat-C0a3DDjv.js +0 -28
- package/dist/client/assets/ChecksPanel-BHWgwJWO.js +0 -1
- package/dist/client/assets/History-C01qLFpP.js +0 -1
- package/dist/client/assets/QuickConnect-BzTM3UNO.js +0 -1
- package/dist/client/assets/index-C1IsGdGs.js +0 -29
- package/dist/client/assets/index-DnsWelgZ.css +0 -2
- package/dist/client/assets/ui-BAGBTB_P.js +0 -41
- package/dist/console/assets/LocalAccountsApp-DtfTDbQg.js +0 -1
- package/dist/console/assets/LocalConsole-BPpsxDq_.js +0 -1
- package/dist/console/assets/console-DeOji9Zv.css +0 -2
- package/dist/console/assets/console-MvFrlUCC.js +0 -51
package/src/agent/tools.js
CHANGED
|
@@ -2,19 +2,70 @@
|
|
|
2
2
|
// writes require the per-turn owner capability. Page ids never become paths
|
|
3
3
|
// or commands. Tool results remain quoted untrusted data (context.js).
|
|
4
4
|
|
|
5
|
+
import fs from 'node:fs'
|
|
5
6
|
import { feedbackContext, feedbackItem } from '../feedback.js'
|
|
6
7
|
import { addReply, captureFiles, commentCounts, getComment, listComments, setAgentStatus, updateComment } from '../store.js'
|
|
7
8
|
import { quoteData } from './context.js'
|
|
8
9
|
|
|
9
10
|
const LIST_MAX = 100
|
|
10
11
|
|
|
12
|
+
// A comment's images, as the model sees them: no local file paths.
|
|
13
|
+
const imagesForModel = (images) =>
|
|
14
|
+
(images || []).map(({ name, mediaType, bytes, width, height }) => ({ name, mediaType, bytes, width, height }))
|
|
15
|
+
|
|
11
16
|
// A comment for the model: no local file paths.
|
|
12
17
|
export function commentForModel(config, board, comment) {
|
|
13
18
|
const item = feedbackItem(feedbackContext(config, board), comment)
|
|
19
|
+
const { images = [], markup = null } = item.files || {}
|
|
14
20
|
delete item.files
|
|
21
|
+
if (images.length) item.images = imagesForModel(images)
|
|
22
|
+
if (markup) item.drawingImage = 'attached as an image'
|
|
23
|
+
item.replies = item.replies.map((reply) => {
|
|
24
|
+
const out = { ...reply }
|
|
25
|
+
if (out.images) out.images = imagesForModel(out.images)
|
|
26
|
+
if ('markup' in out) {
|
|
27
|
+
if (out.markup) out.drawingImage = 'attached as an image'
|
|
28
|
+
delete out.markup
|
|
29
|
+
}
|
|
30
|
+
return out
|
|
31
|
+
})
|
|
15
32
|
return item
|
|
16
33
|
}
|
|
17
34
|
|
|
35
|
+
// The images of a comment's thread, for the model: the comment's drawing
|
|
36
|
+
// rendered on its screenshot, its images, then the newest replies' ones,
|
|
37
|
+
// at most `max` in all. Each comes after a quoted note (untrusted data).
|
|
38
|
+
export const COMMENT_MEDIA_MAX = 4
|
|
39
|
+
export function commentMediaParts(config, board, comment, max = COMMENT_MEDIA_MAX) {
|
|
40
|
+
const item = feedbackItem(feedbackContext(config, board), comment)
|
|
41
|
+
const found = []
|
|
42
|
+
if (item.files.markup) found.push({ path: item.files.markup, mediaType: 'image/png', kind: 'the reviewer drawing on the screenshot' })
|
|
43
|
+
for (const image of item.files.images) found.push({ ...image, kind: 'an image the reviewer attached' })
|
|
44
|
+
for (const reply of [...item.replies].reverse()) {
|
|
45
|
+
if (reply.markup) found.push({ path: reply.markup, mediaType: 'image/png', reply: reply.id, kind: 'a drawing in a reply' })
|
|
46
|
+
for (const image of reply.images || []) found.push({ ...image, reply: reply.id, kind: 'an image attached to a reply' })
|
|
47
|
+
}
|
|
48
|
+
const parts = []
|
|
49
|
+
for (const media of found.slice(0, max)) {
|
|
50
|
+
let data
|
|
51
|
+
try {
|
|
52
|
+
data = fs.readFileSync(media.path).toString('base64')
|
|
53
|
+
} catch {
|
|
54
|
+
continue
|
|
55
|
+
}
|
|
56
|
+
parts.push({
|
|
57
|
+
type: 'text',
|
|
58
|
+
text: quoteData(
|
|
59
|
+
'comment-image',
|
|
60
|
+
{ board: board.id, comment: comment.id, reply: media.reply ?? null },
|
|
61
|
+
{ kind: media.kind, name: media.name || undefined, mediaType: media.mediaType },
|
|
62
|
+
),
|
|
63
|
+
})
|
|
64
|
+
parts.push({ type: 'image', mediaType: media.mediaType, data })
|
|
65
|
+
}
|
|
66
|
+
return parts
|
|
67
|
+
}
|
|
68
|
+
|
|
18
69
|
export const BOARD_TOOLS = [
|
|
19
70
|
{
|
|
20
71
|
name: 'list_pages',
|
|
@@ -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 = [
|
|
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
|
+
}
|