visual-regression-ci 0.0.0-stage → 0.9.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 (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +953 -2
  3. package/package.json +76 -4
  4. package/src/ci/actionReport.js +340 -0
  5. package/src/ci/prComment.js +420 -0
  6. package/src/cli/capture.js +243 -0
  7. package/src/cli/compare.js +190 -0
  8. package/src/cli/config.js +287 -0
  9. package/src/cli/database.js +69 -0
  10. package/src/cli/display.js +119 -0
  11. package/src/cli/errors.js +17 -0
  12. package/src/cli/index.js +577 -0
  13. package/src/cli/installBrowsers.js +59 -0
  14. package/src/cli/invocation.js +66 -0
  15. package/src/cli/jsonTarget.js +100 -0
  16. package/src/cli/logger.js +15 -0
  17. package/src/cli/manifest.js +139 -0
  18. package/src/cli/naming.js +60 -0
  19. package/src/cli/reporter.js +172 -0
  20. package/src/cli/result.js +225 -0
  21. package/src/cli/serve.js +64 -0
  22. package/src/server/app.js +135 -0
  23. package/src/server/db.js +691 -0
  24. package/src/server/importRuns.js +837 -0
  25. package/src/server/paths.js +107 -0
  26. package/src/server/public/css/styles.css +2362 -0
  27. package/src/server/public/js/common.js +32 -0
  28. package/src/server/public/js/dashboard.js +140 -0
  29. package/src/server/public/js/import-upload.js +169 -0
  30. package/src/server/public/js/review-actions.js +379 -0
  31. package/src/server/public/js/shortcuts.js +242 -0
  32. package/src/server/public/js/slider.js +104 -0
  33. package/src/server/public/js/tabs.js +85 -0
  34. package/src/server/public/js/theme.js +83 -0
  35. package/src/server/routes/api.js +141 -0
  36. package/src/server/routes/views.js +68 -0
  37. package/src/server/security.js +79 -0
  38. package/src/server/uploadImport.js +227 -0
  39. package/src/server/urls.js +31 -0
  40. package/src/server/viewModels.js +366 -0
  41. package/src/server/views/history.ejs +111 -0
  42. package/src/server/views/index.ejs +283 -0
  43. package/src/server/views/layout.ejs +11 -0
  44. package/src/server/views/partials/footer.ejs +11 -0
  45. package/src/server/views/partials/header.ejs +93 -0
  46. package/src/server/views/run-detail.ejs +295 -0
  47. package/src/shared/packageSpec.js +49 -0
  48. package/src/shared/text.js +59 -0
@@ -0,0 +1,295 @@
1
+ <%- include('partials/header', { navLink: 'back', crumbs: [{ label: 'Dashboard', href: '/' }, { label: run.id, mono: true }] }) %>
2
+
3
+ <!-- Main Container -->
4
+ <main class="container" id="main" tabindex="-1">
5
+ <!-- Run Summary Header Card -->
6
+ <div class="detail-header-card">
7
+ <div class="detail-title-row">
8
+ <div style="display: flex; align-items: center; gap: 14px; flex-wrap: wrap;">
9
+ <h1 class="detail-run-id"><%= run.id %></h1>
10
+ <span class="run-status-container" data-run-status="<%= run.status %>">
11
+ <% if (run.status === 'PASSED') { %>
12
+ <span class="status-pill status-pill-passed">
13
+ <svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
14
+ <polyline points="20 6 9 17 4 12"></polyline>
15
+ </svg>
16
+ <span>PASSED</span>
17
+ </span>
18
+ <% } else if (run.status === 'APPROVED') { %>
19
+ <span class="status-pill status-pill-approved">
20
+ <svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
21
+ <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path>
22
+ <polyline points="22 4 12 14.01 9 11.01"></polyline>
23
+ </svg>
24
+ <span>APPROVED</span>
25
+ </span>
26
+ <% } else if (run.status === 'FAILED') { %>
27
+ <span class="status-pill status-pill-failed">
28
+ <svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
29
+ <line x1="18" y1="6" x2="6" y2="18"></line>
30
+ <line x1="6" y1="6" x2="18" y2="18"></line>
31
+ </svg>
32
+ <span>FAILED</span>
33
+ </span>
34
+ <% } else if (run.status === 'REJECTED') { %>
35
+ <span class="status-pill status-pill-rejected">
36
+ <svg aria-hidden="true" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
37
+ <circle cx="12" cy="12" r="10"></circle>
38
+ <line x1="4.93" y1="4.93" x2="19.07" y2="19.07"></line>
39
+ </svg>
40
+ <span>REJECTED</span>
41
+ </span>
42
+ <% } %>
43
+ </span>
44
+ <% if (run.imported_at) { %>
45
+ <span class="imported-badge">Imported</span>
46
+ <% } %>
47
+ </div>
48
+ <% if (locals.pendingCount > 0) { %>
49
+ <%# Disabled while a pending snapshot can't be approved (stale local baseline): approve-all is all or nothing. -%>
50
+ <button type="button" class="btn btn-approve-all<%= locals.stalePendingCount > 0 ? ' is-blocked' : '' %>" data-run-id="<%= run.id %>" data-count="<%= pendingCount %>"<% if (locals.stalePendingCount > 0) { %> disabled aria-describedby="approve-all-stale-note"<% } %>>
51
+ <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" aria-hidden="true">
52
+ <polyline points="20 6 9 17 4 12"></polyline>
53
+ </svg>
54
+ <span class="approve-all-label">Approve all pending (<%= pendingCount %>)</span>
55
+ </button>
56
+ <% } %>
57
+ </div>
58
+
59
+ <div class="detail-meta-row">
60
+ <% if (run.branch) { %>
61
+ <span class="git-badge">
62
+ <svg aria-hidden="true" class="git-icon" width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
63
+ <line x1="6" y1="3" x2="6" y2="15"></line>
64
+ <circle cx="18" cy="6" r="3"></circle>
65
+ <circle cx="6" cy="18" r="3"></circle>
66
+ <path d="M18 9a9 9 0 0 1-9 9"></path>
67
+ </svg>
68
+ <span title="<%= run.branch %>"><%= run.branch %></span>
69
+ </span>
70
+ <% } %>
71
+ <% if (run.commit_hash) { %>
72
+ <span class="git-badge git-commit">#<%= run.commit_hash %></span>
73
+ <% } %>
74
+ <span>• Created on <% if (run.createdAtIso) { %><time datetime="<%= run.createdAtIso %>"><%= run.created_at %> UTC</time><% } else { %><%= run.created_at %> UTC<% } %></span>
75
+ <% if (run.imported_at) { %>
76
+ <span>• Imported on <% if (run.importedAtIso) { %><time datetime="<%= run.importedAtIso %>"><%= run.imported_at %> UTC</time><% } else { %><%= run.imported_at %> UTC<% } %></span>
77
+ <% } %>
78
+ <span>• <%= snapshots.length %> total snapshot(s)</span>
79
+ <%# Opens the shortcuts dialog (shortcuts.js); also how to turn them back on once off. -%>
80
+ <button type="button" class="shortcuts-hint" data-shortcuts-open aria-haspopup="dialog" aria-controls="shortcuts-dialog">Press <kbd>?</kbd> for keyboard shortcuts</button>
81
+ </div>
82
+ <% if (locals.pendingCount > 0 && run.imported_at) { %>
83
+ <p class="baseline-stale-note" id="approve-all-stale-note" data-approve-all-stale<% if (!(locals.stalePendingCount > 0)) { %> hidden<% } %>>
84
+ Approve all is unavailable: your local baseline differs from the one CI compared against for <span data-stale-count><%= locals.stalePendingCount || 0 %></span> pending snapshot(s), so approving them would be refused. Update your branch or re-run the tests locally.
85
+ </p>
86
+ <% } %>
87
+ <%# Errors from "Approve all pending" (review-actions.js); empty and invisible until then. -%>
88
+ <div class="action-error" role="alert" data-action-error></div>
89
+ </div>
90
+
91
+ <!-- Snapshots Inspection List -->
92
+ <% snapshots.forEach(snap => { %>
93
+ <%
94
+ const snapLabel = `${snap.route} at ${snap.viewport}`;
95
+ const idBase = `snapshot-${snap.id}`;
96
+ const views = [{ key: 'side-by-side', label: 'Side-by-Side' }];
97
+ if (snap.baselineUrl) views.push({ key: 'slider', label: 'Interactive Slider' });
98
+ if (snap.diffUrl) views.push({ key: 'diff', label: 'Diff Highlight' });
99
+ // WAI-ARIA tabs: the first view is selected; the others are hidden panels.
100
+ const tabAttrs = (key) => `role="tabpanel" id="${idBase}-panel-${key}" aria-labelledby="${idBase}-tab-${key}" tabindex="0"`;
101
+ %>
102
+ <%# tabindex="-1": the j/k shortcuts move focus to the card. -%>
103
+ <article class="snapshot-card" id="<%= idBase %>" data-status="<%= snap.status %>"<% if (snap.baselineStale) { %> data-baseline-stale="true"<% } %> tabindex="-1" aria-labelledby="<%= idBase %>-heading">
104
+ <h2 class="visually-hidden" id="<%= idBase %>-heading">Snapshot <%= snapLabel %></h2>
105
+ <!-- Header: Route, Viewport & Actions -->
106
+ <div class="snapshot-header">
107
+ <div class="snapshot-title">
108
+ <span class="route-pill"><%= snap.route %></span>
109
+ <span class="viewport-badge"><%= snap.viewport %></span>
110
+ <% if (snap.historyUrl) { %>
111
+ <a href="<%= url(snap.historyUrl) %>" class="history-link" aria-label="History of <%= snap.route %> at <%= snap.viewport %>">History</a>
112
+ <% } %>
113
+
114
+ <div class="status-badge-container">
115
+ <% if (snap.status === 'MATCH') { %>
116
+ <span class="status-pill status-pill-passed">
117
+ <svg aria-hidden="true" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
118
+ <polyline points="20 6 9 17 4 12"></polyline>
119
+ </svg>
120
+ <span>MATCH (0.00%)</span>
121
+ </span>
122
+ <% } else if (snap.status === 'DIFF') { %>
123
+ <span class="status-pill status-pill-failed">
124
+ <svg aria-hidden="true" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
125
+ <line x1="18" y1="6" x2="6" y2="18"></line>
126
+ <line x1="6" y1="6" x2="18" y2="18"></line>
127
+ </svg>
128
+ <span><%= snap.mismatch_percentage.toFixed(2) %>% Mismatch</span>
129
+ </span>
130
+ <% } else if (snap.status === 'MISSING_BASELINE') { %>
131
+ <span class="status-pill status-pill-failed">
132
+ <span>MISSING BASELINE</span>
133
+ </span>
134
+ <% } else if (snap.status === 'NEW_BASELINE') { %>
135
+ <span class="status-pill status-pill-new">
136
+ <span>NEW BASELINE</span>
137
+ </span>
138
+ <% } else if (snap.status === 'APPROVED') { %>
139
+ <span class="status-pill status-pill-approved">
140
+ <svg aria-hidden="true" width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
141
+ <polyline points="20 6 9 17 4 12"></polyline>
142
+ </svg>
143
+ <span>APPROVED</span>
144
+ </span>
145
+ <% } else if (snap.status === 'REJECTED') { %>
146
+ <span class="status-pill status-pill-rejected">
147
+ <span>REJECTED</span>
148
+ </span>
149
+ <% } %>
150
+ </div>
151
+ </div>
152
+
153
+ <div class="snapshot-actions">
154
+ <% if (snap.status === 'DIFF' || snap.status === 'MISSING_BASELINE') { %>
155
+ <% if (snap.baselineStale) { %>
156
+ <button type="button" class="btn btn-approve is-blocked" data-id="<%= snap.id %>" disabled aria-describedby="<%= idBase %>-heading <%= idBase %>-stale">
157
+ <% } else { %>
158
+ <button type="button" class="btn btn-approve" data-id="<%= snap.id %>" aria-describedby="<%= idBase %>-heading">
159
+ <% } %>
160
+ <svg aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
161
+ <polyline points="20 6 9 17 4 12"></polyline>
162
+ </svg>
163
+ <span>Approve & Update Baseline</span>
164
+ </button>
165
+ <button type="button" class="btn btn-reject" data-id="<%= snap.id %>" aria-describedby="<%= idBase %>-heading">
166
+ <svg aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
167
+ <line x1="18" y1="6" x2="6" y2="18"></line>
168
+ <line x1="6" y1="6" x2="18" y2="18"></line>
169
+ </svg>
170
+ <span>Reject</span>
171
+ </button>
172
+ <% } else if (snap.status === 'APPROVED') { %>
173
+ <span style="font-size: 13px; color: var(--success-green); font-weight: 600; display: inline-flex; align-items: center; gap: 4px;">
174
+ <svg aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5">
175
+ <polyline points="20 6 9 17 4 12"></polyline>
176
+ </svg>
177
+ <span>Baseline Promoted</span>
178
+ </span>
179
+ <% } %>
180
+ </div>
181
+ </div>
182
+ <% if (snap.baselineStale) { %>
183
+ <p class="baseline-stale-note" id="<%= idBase %>-stale">
184
+ Your local baseline differs from the one CI compared against, so approving would be refused. Update your branch or re-run the tests locally.
185
+ </p>
186
+ <% } %>
187
+ <%# Approve/reject errors for this snapshot (review-actions.js); empty and invisible until then. -%>
188
+ <div class="action-error" role="alert" data-action-error></div>
189
+
190
+ <!-- Comparison views: WAI-ARIA tabs (tabs.js) -->
191
+ <div class="tabs-nav" role="tablist" aria-label="Comparison views for <%= snapLabel %>">
192
+ <% views.forEach((view, i) => { %>
193
+ <button type="button" class="tab-btn<%= i === 0 ? ' active' : '' %>" role="tab" id="<%= idBase %>-tab-<%= view.key %>" aria-controls="<%= idBase %>-panel-<%= view.key %>" aria-selected="<%= i === 0 ? 'true' : 'false' %>" tabindex="<%= i === 0 ? 0 : -1 %>" data-tab="<%= view.key %>"><%= view.label %></button>
194
+ <% }) %>
195
+ </div>
196
+
197
+ <!-- Tab 1: Side-by-Side View -->
198
+ <div class="tab-panel active" data-panel="side-by-side" <%- tabAttrs('side-by-side') %>>
199
+ <div class="side-by-side">
200
+ <div class="image-preview-container">
201
+ <div class="image-preview-label">Approved Baseline</div>
202
+ <div class="image-frame">
203
+ <% if (snap.baselineUrl) { %>
204
+ <img src="<%= url(snap.baselineUrl) %>" alt="Baseline screenshot of <%= snapLabel %>">
205
+ <% } else { %>
206
+ <div style="padding: 48px; text-align: center; color: var(--text-muted); font-size: 13px;">
207
+ <% if (snap.status === 'MISSING_BASELINE') { %>
208
+ No baseline exists for this route. Approving will make the current screenshot its baseline.
209
+ <% } else { %>
210
+ No prior baseline existed for this route. Current screenshot was promoted as initial golden master.
211
+ <% } %>
212
+ </div>
213
+ <% } %>
214
+ </div>
215
+ </div>
216
+
217
+ <div class="image-preview-container">
218
+ <div class="image-preview-label">Current Test Execution</div>
219
+ <div class="image-frame">
220
+ <% if (snap.currentUrl) { %>
221
+ <img src="<%= url(snap.currentUrl) %>" alt="Current screenshot of <%= snapLabel %>">
222
+ <% } else { %>
223
+ <div style="padding: 48px; text-align: center; color: var(--text-muted); font-size: 13px;">
224
+ Screenshot is not in this dashboard's storage folder.
225
+ </div>
226
+ <% } %>
227
+ </div>
228
+ </div>
229
+ </div>
230
+ </div>
231
+
232
+ <!-- Tab 2: Interactive Draggable Slider View -->
233
+ <% if (snap.baselineUrl) { %>
234
+ <div class="tab-panel" data-panel="slider" <%- tabAttrs('slider') %> hidden>
235
+ <div style="margin-bottom: 14px; font-size: 13px; color: var(--text-secondary); text-align: center;">
236
+ <span aria-hidden="true">⇄</span> Drag the divider, or focus it and use the arrow keys: current screenshot on the left, baseline on the right
237
+ </div>
238
+ <div class="slider-view-container">
239
+ <div class="slider-wrapper">
240
+ <!-- Under Image (Baseline) -->
241
+ <img src="<%= url(snap.baselineUrl) %>" class="slider-img-under" alt="Baseline screenshot of <%= snapLabel %>">
242
+
243
+ <!-- Over Image (Current) with responsive clipping -->
244
+ <div class="slider-img-over-wrapper">
245
+ <img src="<%= url(snap.currentUrl) %>" class="slider-img-over" alt="Current screenshot of <%= snapLabel %>">
246
+ </div>
247
+
248
+ <!-- Draggable vertical divider (slider.js): % of the width showing the current screenshot -->
249
+ <div class="slider-divider" role="slider" tabindex="0" aria-label="Comparison slider for <%= snapLabel %>" aria-orientation="horizontal" aria-valuemin="0" aria-valuemax="100" aria-valuenow="50" aria-valuetext="50% current, 50% baseline">
250
+ <div class="slider-handle-pill" aria-hidden="true">⇄</div>
251
+ </div>
252
+ </div>
253
+ </div>
254
+ </div>
255
+ <% } %>
256
+
257
+ <!-- Tab 3: Diff Highlight View -->
258
+ <% if (snap.diffUrl) { %>
259
+ <div class="tab-panel" data-panel="diff" <%- tabAttrs('diff') %> hidden>
260
+ <div style="margin-bottom: 14px; font-size: 13px; color: var(--error-red); font-weight: 600; text-align: center;">
261
+ Diff Highlight: Differing pixels are highlighted in bright neon red (<%= snap.mismatch_percentage.toFixed(2) %>% mismatch)
262
+ </div>
263
+ <div class="image-frame" style="max-width: 1200px; margin: 0 auto;">
264
+ <img src="<%= url(snap.diffUrl) %>" alt="Diff of <%= snapLabel %>: differing pixels highlighted in red">
265
+ </div>
266
+ </div>
267
+ <% } %>
268
+ </article>
269
+ <% }) %>
270
+ </main>
271
+
272
+ <!-- Keyboard shortcuts help (shortcuts.js) -->
273
+ <dialog id="shortcuts-dialog" class="shortcuts-dialog" aria-labelledby="shortcuts-dialog-title">
274
+ <div class="shortcuts-dialog-header">
275
+ <h2 id="shortcuts-dialog-title">Keyboard shortcuts</h2>
276
+ <button type="button" class="shortcuts-dialog-close" data-shortcuts-close aria-label="Close keyboard shortcuts"><span aria-hidden="true">✕</span></button>
277
+ </div>
278
+ <dl class="shortcuts-list">
279
+ <div><dt><kbd>j</kbd></dt><dd>Next snapshot (the first press goes to the first one awaiting review)</dd></div>
280
+ <div><dt><kbd>k</kbd></dt><dd>Previous snapshot</dd></div>
281
+ <div><dt><kbd>a</kbd></dt><dd>Approve the current snapshot and update its baseline</dd></div>
282
+ <div><dt><kbd>r</kbd></dt><dd>Reject the current snapshot (asks you to confirm)</dd></div>
283
+ <div><dt><kbd>1</kbd> <kbd>2</kbd> <kbd>3</kbd></dt><dd>Side-by-side, slider or diff view of the current snapshot</dd></div>
284
+ <div><dt><kbd>?</kbd></dt><dd>Show this help</dd></div>
285
+ <div><dt><kbd>Esc</kbd></dt><dd>Close this help</dd></div>
286
+ </dl>
287
+ <p class="shortcuts-note">Shortcuts are ignored while you type in a field or hold Ctrl, Alt or Cmd. In the comparison slider, use the arrow keys, Page Up, Page Down, Home and End.</p>
288
+ <div class="shortcuts-toggle">
289
+ <input type="checkbox" id="shortcuts-enabled" checked aria-describedby="shortcuts-toggle-note">
290
+ <label for="shortcuts-enabled">Single-key shortcuts</label>
291
+ </div>
292
+ <p class="shortcuts-note" id="shortcuts-toggle-note">Turn them off if they get in the way of a screen reader or speech input. Saved in this browser.</p>
293
+ </dialog>
294
+
295
+ <%- include('partials/footer', { scripts: ['/js/tabs.js', '/js/slider.js', '/js/review-actions.js', '/js/shortcuts.js'] }) %>
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Where commands we print for others to copy (the PR comment's import snippet, CLI
3
+ * hints) get visual-ci from: the npm package, pinned to an exact version, e.g.
4
+ * `npx -y visual-regression-ci@0.9.0 import ...`. Pinned so the reader runs the same visual-ci
5
+ * that wrote the run (a bare `npx visual-ci` would run whatever is latest on npm, or a
6
+ * different copy that happens to be installed).
7
+ *
8
+ * The version is passed in by the caller, which reads it from its own package.json
9
+ * (src/cli/manifest.js getToolVersion() for the CLI, src/ci/prComment.js for the
10
+ * action's scripts copy, which has package.json next to src/).
11
+ *
12
+ * No imports: src/shared is copied on its own into the action's scripts folder.
13
+ */
14
+
15
+ /** The npm package name. */
16
+ export const PACKAGE_NAME = 'visual-regression-ci';
17
+
18
+ /**
19
+ * This repository as an npm spec (`npx -y -p <it> visual-ci`): the fallback when the
20
+ * version can't be read, so a hint never becomes an unpinned `npx visual-ci`.
21
+ */
22
+ export const GITHUB_PACKAGE_SPEC = 'github:abhishek-k-33/Visual_Regression_Testing';
23
+
24
+ /** A version npm can publish and a release tag can name: X.Y.Z, optionally -prerelease (no build metadata). */
25
+ export const RELEASE_VERSION = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$/;
26
+
27
+ /**
28
+ * `visual-regression-ci@<version>`, or null when `version` is not a release version (e.g. the
29
+ * 'unknown' getToolVersion() returns when package.json can't be read): never an
30
+ * unpinned spec.
31
+ *
32
+ * @param {unknown} version
33
+ * @returns {string|null}
34
+ */
35
+ export function packageSpec(version) {
36
+ return typeof version === 'string' && RELEASE_VERSION.test(version) ? `${PACKAGE_NAME}@${version}` : null;
37
+ }
38
+
39
+ /**
40
+ * The command that runs this exact version from npm without installing it:
41
+ * `npx -y visual-regression-ci@<version>` (-y: no "Ok to proceed?" prompt). Null like packageSpec().
42
+ *
43
+ * @param {unknown} version
44
+ * @returns {string|null}
45
+ */
46
+ export function npxCommand(version) {
47
+ const spec = packageSpec(version);
48
+ return spec ? `npx -y ${spec}` : null;
49
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Text helpers shared by the CLI, the dashboard server and the GitHub Action scripts.
3
+ *
4
+ * Only plain JavaScript, no imports at all: src/ci/*.js load this module inside
5
+ * actions/github-script straight from the action's checkout (no `npm ci` there), and
6
+ * the CLI's result writer uses it without pulling in the server's dependencies.
7
+ */
8
+
9
+ // C0/DEL/C1 controls, format characters (bidi overrides U+202A-E, isolates U+2066-9,
10
+ // marks U+200E/F, zero-width chars, BOM...), lone surrogates and line/paragraph
11
+ // separators: refused in every manifest string and archive entry name, and escaped
12
+ // whenever untrusted text is shown (they can rewrite a terminal or disguise text).
13
+ export const UNSAFE_CHARS = /[\p{Cc}\p{Cf}\p{Cs}\p{Zl}\p{Zp}]/u;
14
+ const UNSAFE_CHARS_GLOBAL = new RegExp(UNSAFE_CHARS.source, 'gu');
15
+
16
+ /**
17
+ * Makes untrusted text safe to print to a terminal or log: every control, format,
18
+ * surrogate or separator character becomes a visible \u{...} escape.
19
+ *
20
+ * @param {unknown} text
21
+ * @returns {string}
22
+ */
23
+ export function sanitizeForDisplay(text) {
24
+ return String(text).replace(UNSAFE_CHARS_GLOBAL, (ch) => `\\u{${ch.codePointAt(0).toString(16).toUpperCase()}}`);
25
+ }
26
+
27
+ /**
28
+ * Untrusted text as a Markdown code span, safe to put into a GitHub comment, a step
29
+ * summary or a table cell. Inside a code span GitHub renders nothing: no @mentions,
30
+ * links, images, HTML, emphasis or emoji shortcodes.
31
+ *
32
+ * - Control, bidi and other invisible characters (newlines included) are escaped
33
+ * first (sanitizeForDisplay), so the span stays on one line and shows what is there.
34
+ * - The backtick fence is one longer than the longest backtick run in the text, and the
35
+ * text is padded with a space when it starts or ends with a backtick (or with spaces
36
+ * on both sides, which CommonMark would otherwise strip), so no text can close it.
37
+ * - With `table` (the default) every `|` becomes `\|`: GitHub splits table cells
38
+ * before it parses code spans, and shows an escaped pipe as `|` even inside one.
39
+ * Pass { table: false } outside tables, where `\|` would show the backslash.
40
+ *
41
+ * @param {unknown} text
42
+ * @param {{ table?: boolean }} [options]
43
+ * @returns {string}
44
+ */
45
+ export function mdCode(text, { table = true } = {}) {
46
+ let s = sanitizeForDisplay(text ?? '');
47
+ const longest = (s.match(/`+/g) || []).reduce((n, run) => Math.max(n, run.length), 0);
48
+ const fence = '`'.repeat(longest + 1);
49
+ const pad = s === '' || s.startsWith('`') || s.endsWith('`') || (s.startsWith(' ') && s.endsWith(' ') && s.trim() !== '');
50
+ if (pad) s = ` ${s} `;
51
+ if (table) s = s.replace(/\|/g, '\\|');
52
+ return `${fence}${s}${fence}`;
53
+ }
54
+
55
+ /** At most `max` code points of `text` (never splitting a surrogate pair), with … when cut. */
56
+ export function truncate(text, max) {
57
+ const chars = Array.from(String(text ?? ''));
58
+ return chars.length <= max ? chars.join('') : `${chars.slice(0, Math.max(0, max - 1)).join('')}…`;
59
+ }