@edjbarron/ux-guard 0.0.0-stage → 0.6.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/LICENSE +201 -0
- package/NOTICE +2 -0
- package/README.md +153 -2
- package/bin/ux-guard.mjs +4 -0
- package/docs/cli-reference.md +98 -0
- package/docs/test-suite.md +175 -0
- package/lib/authoritative.mjs +463 -0
- package/lib/baselines.mjs +1840 -0
- package/lib/catalog.mjs +197 -0
- package/lib/cli.mjs +1938 -0
- package/lib/config.mjs +209 -0
- package/lib/discovery.mjs +581 -0
- package/lib/journey-policy.mjs +590 -0
- package/lib/journeys.mjs +900 -0
- package/lib/observations.mjs +268 -0
- package/lib/patterns.mjs +247 -0
- package/lib/playwright.mjs +344 -0
- package/lib/run-contract.mjs +413 -0
- package/lib/shards.mjs +445 -0
- package/lib/targets.mjs +764 -0
- package/lib/verdicts.mjs +1769 -0
- package/lib/visuals.mjs +1263 -0
- package/lib/yaml.mjs +140 -0
- package/package.json +56 -4
- package/skills/ux-guard/SKILL.md +122 -0
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
|
|
4
|
+
export const RUN_ENVELOPE_KIND = 'ux-guard-run-envelope';
|
|
5
|
+
export const RUN_ENVELOPE_VERSION = 1;
|
|
6
|
+
export const UX_GUARD_TOOL_VERSION = '0.6.0';
|
|
7
|
+
|
|
8
|
+
const SHA256 = /^[a-f0-9]{64}$/;
|
|
9
|
+
const COMMIT = /^[a-f0-9]{40}$/;
|
|
10
|
+
const ID = /^[a-z0-9]+(?:[._-][a-z0-9]+)*$/;
|
|
11
|
+
const COMPLETION_RESULTS = new Set([
|
|
12
|
+
'successful',
|
|
13
|
+
'failed',
|
|
14
|
+
'skipped',
|
|
15
|
+
'infrastructure-error',
|
|
16
|
+
]);
|
|
17
|
+
const ERROR_CATEGORIES = new Set(['assertion', 'infrastructure']);
|
|
18
|
+
|
|
19
|
+
export class RunContractError extends Error {
|
|
20
|
+
constructor(message) {
|
|
21
|
+
super(message);
|
|
22
|
+
this.name = 'RunContractError';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function canonicalize(value) {
|
|
27
|
+
if (Array.isArray(value)) return value.map(canonicalize);
|
|
28
|
+
if (value && typeof value === 'object') {
|
|
29
|
+
return Object.fromEntries(
|
|
30
|
+
Object.keys(value)
|
|
31
|
+
.sort()
|
|
32
|
+
.map((key) => [key, canonicalize(value[key])]),
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function canonicalJson(value) {
|
|
39
|
+
return JSON.stringify(canonicalize(value));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function sha256(value) {
|
|
43
|
+
const bytes = typeof value === 'string' ? value : canonicalJson(value);
|
|
44
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function policyDigest(policy) {
|
|
48
|
+
return `sha256:${sha256(policy)}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function resolveRunIdentity(
|
|
52
|
+
root,
|
|
53
|
+
{
|
|
54
|
+
policy,
|
|
55
|
+
candidateCommit,
|
|
56
|
+
baseCommit,
|
|
57
|
+
browser = { name: 'chromium', profile: 'Desktop Chrome' },
|
|
58
|
+
adapters = { playwright: '1' },
|
|
59
|
+
} = {},
|
|
60
|
+
) {
|
|
61
|
+
const candidate = resolveCommit(
|
|
62
|
+
root,
|
|
63
|
+
candidateCommit ?? process.env.UX_GUARD_CANDIDATE_SHA ?? process.env.GITHUB_SHA,
|
|
64
|
+
'candidate',
|
|
65
|
+
);
|
|
66
|
+
const baseInput =
|
|
67
|
+
baseCommit ?? process.env.UX_GUARD_BASE_SHA ?? process.env.UX_GUARD_BASE_COMMIT;
|
|
68
|
+
const usableBase =
|
|
69
|
+
baseInput && !/^0+$/.test(String(baseInput)) ? baseInput : null;
|
|
70
|
+
const base = usableBase
|
|
71
|
+
? resolveCommit(root, usableBase, 'base')
|
|
72
|
+
: { commit: null, source: 'none' };
|
|
73
|
+
const identity = {
|
|
74
|
+
provenance: { candidate, base },
|
|
75
|
+
tool: {
|
|
76
|
+
name: 'ux-guard',
|
|
77
|
+
version: UX_GUARD_TOOL_VERSION,
|
|
78
|
+
schemas: {
|
|
79
|
+
run: RUN_ENVELOPE_VERSION,
|
|
80
|
+
'journey-report': 5,
|
|
81
|
+
'journey-shard': 1,
|
|
82
|
+
observation: 1,
|
|
83
|
+
policy: 3,
|
|
84
|
+
'journey-baseline': 1,
|
|
85
|
+
'journey-comparison': 2,
|
|
86
|
+
'owner-targets': 2,
|
|
87
|
+
'target-attainment': 1,
|
|
88
|
+
'numerical-score': 1,
|
|
89
|
+
'visual-profile': 1,
|
|
90
|
+
'visual-manifest': 1,
|
|
91
|
+
'visual-comparison': 1,
|
|
92
|
+
'verdict-report': 6,
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
policy: {
|
|
96
|
+
algorithm: 'sha256',
|
|
97
|
+
digest: policyDigest(policy ?? null),
|
|
98
|
+
},
|
|
99
|
+
browser: structuredClone(browser),
|
|
100
|
+
adapters: canonicalize(adapters),
|
|
101
|
+
};
|
|
102
|
+
validateRunIdentity(identity);
|
|
103
|
+
return identity;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function createRunEnvelope(
|
|
107
|
+
identity,
|
|
108
|
+
{ statuses = [], errors = [], artifacts = [] } = {},
|
|
109
|
+
) {
|
|
110
|
+
validateRunIdentity(identity);
|
|
111
|
+
if (!Array.isArray(statuses)) {
|
|
112
|
+
throw new RunContractError('run statuses must be an array');
|
|
113
|
+
}
|
|
114
|
+
const counts = {
|
|
115
|
+
successful: 0,
|
|
116
|
+
failed: 0,
|
|
117
|
+
skipped: 0,
|
|
118
|
+
unexecutable: 0,
|
|
119
|
+
infrastructureError: 0,
|
|
120
|
+
};
|
|
121
|
+
for (const status of statuses) {
|
|
122
|
+
if (status === 'successful') counts.successful += 1;
|
|
123
|
+
else if (status === 'failed') counts.failed += 1;
|
|
124
|
+
else if (status === 'skipped') counts.skipped += 1;
|
|
125
|
+
else if (status === 'unexecutable') counts.unexecutable += 1;
|
|
126
|
+
else if (status === 'infrastructure-error') counts.infrastructureError += 1;
|
|
127
|
+
else throw new RunContractError(`unknown journey lifecycle status ${String(status)}`);
|
|
128
|
+
}
|
|
129
|
+
let result = 'successful';
|
|
130
|
+
if (counts.infrastructureError) result = 'infrastructure-error';
|
|
131
|
+
else if (counts.failed) result = 'failed';
|
|
132
|
+
else if (counts.skipped) result = 'skipped';
|
|
133
|
+
const envelope = {
|
|
134
|
+
kind: RUN_ENVELOPE_KIND,
|
|
135
|
+
version: RUN_ENVELOPE_VERSION,
|
|
136
|
+
...structuredClone(identity),
|
|
137
|
+
completion: {
|
|
138
|
+
status: 'completed',
|
|
139
|
+
result,
|
|
140
|
+
expected: statuses.length,
|
|
141
|
+
received: statuses.length,
|
|
142
|
+
...counts,
|
|
143
|
+
},
|
|
144
|
+
errors: structuredClone(errors).sort(compareError),
|
|
145
|
+
artifacts: structuredClone(artifacts).sort((left, right) =>
|
|
146
|
+
String(left?.id).localeCompare(String(right?.id)),
|
|
147
|
+
),
|
|
148
|
+
};
|
|
149
|
+
validateRunEnvelope(envelope);
|
|
150
|
+
return envelope;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function validateRunIdentity(identity) {
|
|
154
|
+
assertMap(identity, 'run identity');
|
|
155
|
+
assertExactKeys(
|
|
156
|
+
identity,
|
|
157
|
+
['provenance', 'tool', 'policy', 'browser', 'adapters'],
|
|
158
|
+
'run identity',
|
|
159
|
+
);
|
|
160
|
+
assertMap(identity.provenance, 'run identity.provenance');
|
|
161
|
+
assertExactKeys(
|
|
162
|
+
identity.provenance,
|
|
163
|
+
['candidate', 'base'],
|
|
164
|
+
'run identity.provenance',
|
|
165
|
+
);
|
|
166
|
+
validateCommit(identity.provenance.candidate, 'run identity.provenance.candidate', false);
|
|
167
|
+
validateCommit(identity.provenance.base, 'run identity.provenance.base', true);
|
|
168
|
+
assertMap(identity.tool, 'run identity.tool');
|
|
169
|
+
assertExactKeys(identity.tool, ['name', 'version', 'schemas'], 'run identity.tool');
|
|
170
|
+
if (identity.tool.name !== 'ux-guard' || identity.tool.version !== UX_GUARD_TOOL_VERSION) {
|
|
171
|
+
throw new RunContractError('run identity.tool is not this ux-guard version');
|
|
172
|
+
}
|
|
173
|
+
assertMap(identity.tool.schemas, 'run identity.tool.schemas');
|
|
174
|
+
for (const [name, version] of Object.entries(identity.tool.schemas)) {
|
|
175
|
+
if (!ID.test(name) || !Number.isSafeInteger(version) || version < 1) {
|
|
176
|
+
throw new RunContractError('run identity.tool.schemas is invalid');
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
assertMap(identity.policy, 'run identity.policy');
|
|
180
|
+
assertExactKeys(identity.policy, ['algorithm', 'digest'], 'run identity.policy');
|
|
181
|
+
if (
|
|
182
|
+
identity.policy.algorithm !== 'sha256' ||
|
|
183
|
+
typeof identity.policy.digest !== 'string' ||
|
|
184
|
+
!identity.policy.digest.startsWith('sha256:') ||
|
|
185
|
+
!SHA256.test(identity.policy.digest.slice(7))
|
|
186
|
+
) {
|
|
187
|
+
throw new RunContractError('run identity.policy digest is invalid');
|
|
188
|
+
}
|
|
189
|
+
assertMap(identity.browser, 'run identity.browser');
|
|
190
|
+
assertExactKeys(identity.browser, ['name', 'profile'], 'run identity.browser');
|
|
191
|
+
assertText(identity.browser.name, 'run identity.browser.name');
|
|
192
|
+
assertText(identity.browser.profile, 'run identity.browser.profile');
|
|
193
|
+
assertMap(identity.adapters, 'run identity.adapters');
|
|
194
|
+
if (Object.keys(identity.adapters).length === 0) {
|
|
195
|
+
throw new RunContractError('run identity.adapters must not be empty');
|
|
196
|
+
}
|
|
197
|
+
for (const [name, version] of Object.entries(identity.adapters)) {
|
|
198
|
+
if (!ID.test(name)) throw new RunContractError(`invalid adapter name ${name}`);
|
|
199
|
+
assertText(version, `run identity.adapters.${name}`);
|
|
200
|
+
}
|
|
201
|
+
return identity;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function validateRunEnvelope(envelope) {
|
|
205
|
+
assertMap(envelope, 'run');
|
|
206
|
+
assertExactKeys(
|
|
207
|
+
envelope,
|
|
208
|
+
[
|
|
209
|
+
'kind',
|
|
210
|
+
'version',
|
|
211
|
+
'provenance',
|
|
212
|
+
'tool',
|
|
213
|
+
'policy',
|
|
214
|
+
'browser',
|
|
215
|
+
'adapters',
|
|
216
|
+
'completion',
|
|
217
|
+
'errors',
|
|
218
|
+
'artifacts',
|
|
219
|
+
],
|
|
220
|
+
'run',
|
|
221
|
+
);
|
|
222
|
+
if (envelope.kind !== RUN_ENVELOPE_KIND || envelope.version !== RUN_ENVELOPE_VERSION) {
|
|
223
|
+
throw new RunContractError('run envelope has an unsupported kind or version');
|
|
224
|
+
}
|
|
225
|
+
validateRunIdentity({
|
|
226
|
+
provenance: envelope.provenance,
|
|
227
|
+
tool: envelope.tool,
|
|
228
|
+
policy: envelope.policy,
|
|
229
|
+
browser: envelope.browser,
|
|
230
|
+
adapters: envelope.adapters,
|
|
231
|
+
});
|
|
232
|
+
assertMap(envelope.completion, 'run.completion');
|
|
233
|
+
assertExactKeys(
|
|
234
|
+
envelope.completion,
|
|
235
|
+
[
|
|
236
|
+
'status',
|
|
237
|
+
'result',
|
|
238
|
+
'expected',
|
|
239
|
+
'received',
|
|
240
|
+
'successful',
|
|
241
|
+
'failed',
|
|
242
|
+
'skipped',
|
|
243
|
+
'unexecutable',
|
|
244
|
+
'infrastructureError',
|
|
245
|
+
],
|
|
246
|
+
'run.completion',
|
|
247
|
+
);
|
|
248
|
+
if (
|
|
249
|
+
envelope.completion.status !== 'completed' ||
|
|
250
|
+
!COMPLETION_RESULTS.has(envelope.completion.result)
|
|
251
|
+
) {
|
|
252
|
+
throw new RunContractError('run.completion status or result is invalid');
|
|
253
|
+
}
|
|
254
|
+
for (const field of [
|
|
255
|
+
'expected',
|
|
256
|
+
'received',
|
|
257
|
+
'successful',
|
|
258
|
+
'failed',
|
|
259
|
+
'skipped',
|
|
260
|
+
'unexecutable',
|
|
261
|
+
'infrastructureError',
|
|
262
|
+
]) {
|
|
263
|
+
assertCount(envelope.completion[field], `run.completion.${field}`);
|
|
264
|
+
}
|
|
265
|
+
const total =
|
|
266
|
+
envelope.completion.successful +
|
|
267
|
+
envelope.completion.failed +
|
|
268
|
+
envelope.completion.skipped +
|
|
269
|
+
envelope.completion.unexecutable +
|
|
270
|
+
envelope.completion.infrastructureError;
|
|
271
|
+
if (
|
|
272
|
+
total !== envelope.completion.received ||
|
|
273
|
+
envelope.completion.received !== envelope.completion.expected
|
|
274
|
+
) {
|
|
275
|
+
throw new RunContractError('run.completion counts are inconsistent');
|
|
276
|
+
}
|
|
277
|
+
const expectedResult = envelope.completion.infrastructureError
|
|
278
|
+
? 'infrastructure-error'
|
|
279
|
+
: envelope.completion.failed
|
|
280
|
+
? 'failed'
|
|
281
|
+
: envelope.completion.skipped
|
|
282
|
+
? 'skipped'
|
|
283
|
+
: 'successful';
|
|
284
|
+
if (envelope.completion.result !== expectedResult) {
|
|
285
|
+
throw new RunContractError(`run.completion.result must be ${expectedResult}`);
|
|
286
|
+
}
|
|
287
|
+
if (!Array.isArray(envelope.errors) || !Array.isArray(envelope.artifacts)) {
|
|
288
|
+
throw new RunContractError('run errors and artifacts must be arrays');
|
|
289
|
+
}
|
|
290
|
+
const errorIds = new Set();
|
|
291
|
+
for (const [index, error] of envelope.errors.entries()) {
|
|
292
|
+
const path = `run.errors[${index}]`;
|
|
293
|
+
assertMap(error, path);
|
|
294
|
+
assertExactKeys(error, ['id', 'category', 'message', 'journeyRef'], path);
|
|
295
|
+
assertId(error.id, `${path}.id`);
|
|
296
|
+
if (errorIds.has(error.id)) throw new RunContractError(`duplicate run error ${error.id}`);
|
|
297
|
+
errorIds.add(error.id);
|
|
298
|
+
if (!ERROR_CATEGORIES.has(error.category)) {
|
|
299
|
+
throw new RunContractError(`${path}.category is invalid`);
|
|
300
|
+
}
|
|
301
|
+
assertText(error.message, `${path}.message`);
|
|
302
|
+
assertId(error.journeyRef, `${path}.journeyRef`);
|
|
303
|
+
}
|
|
304
|
+
const artifactIds = new Set();
|
|
305
|
+
for (const [index, artifact] of envelope.artifacts.entries()) {
|
|
306
|
+
const path = `run.artifacts[${index}]`;
|
|
307
|
+
assertMap(artifact, path);
|
|
308
|
+
assertExactKeys(artifact, ['id', 'path', 'sha256'], path);
|
|
309
|
+
assertId(artifact.id, `${path}.id`);
|
|
310
|
+
if (artifactIds.has(artifact.id)) {
|
|
311
|
+
throw new RunContractError(`duplicate run artifact ${artifact.id}`);
|
|
312
|
+
}
|
|
313
|
+
artifactIds.add(artifact.id);
|
|
314
|
+
assertSafeRelativePath(artifact.path, `${path}.path`);
|
|
315
|
+
if (typeof artifact.sha256 !== 'string' || !SHA256.test(artifact.sha256)) {
|
|
316
|
+
throw new RunContractError(`${path}.sha256 is invalid`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return envelope;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function resolveCommit(root, input, label) {
|
|
323
|
+
if (input !== undefined && input !== null && input !== '') {
|
|
324
|
+
const commit = gitCommit(root, String(input));
|
|
325
|
+
if (!commit) throw new RunContractError(`${label} commit ${String(input)} is unavailable`);
|
|
326
|
+
return { commit, source: process.env.CI ? 'ci' : 'git' };
|
|
327
|
+
}
|
|
328
|
+
const commit = gitCommit(root, 'HEAD');
|
|
329
|
+
return commit
|
|
330
|
+
? { commit, source: 'git' }
|
|
331
|
+
: { commit: null, source: 'unavailable' };
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function gitCommit(root, ref) {
|
|
335
|
+
try {
|
|
336
|
+
const value = execFileSync('git', ['rev-parse', '--verify', `${ref}^{commit}`], {
|
|
337
|
+
cwd: root,
|
|
338
|
+
encoding: 'utf8',
|
|
339
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
340
|
+
}).trim();
|
|
341
|
+
return COMMIT.test(value) ? value : null;
|
|
342
|
+
} catch {
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function validateCommit(value, path, optional) {
|
|
348
|
+
assertMap(value, path);
|
|
349
|
+
assertExactKeys(value, ['commit', 'source'], path);
|
|
350
|
+
const sources = optional
|
|
351
|
+
? new Set(['ci', 'git', 'none'])
|
|
352
|
+
: new Set(['ci', 'git', 'unavailable']);
|
|
353
|
+
if (!sources.has(value.source)) throw new RunContractError(`${path}.source is invalid`);
|
|
354
|
+
if (value.commit === null) {
|
|
355
|
+
if (!((optional && value.source === 'none') || (!optional && value.source === 'unavailable'))) {
|
|
356
|
+
throw new RunContractError(`${path}.commit cannot be null for ${value.source}`);
|
|
357
|
+
}
|
|
358
|
+
} else if (typeof value.commit !== 'string' || !COMMIT.test(value.commit)) {
|
|
359
|
+
throw new RunContractError(`${path}.commit is invalid`);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function compareError(left, right) {
|
|
364
|
+
return String(left?.journeyRef).localeCompare(String(right?.journeyRef)) ||
|
|
365
|
+
String(left?.id).localeCompare(String(right?.id));
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function assertMap(value, path) {
|
|
369
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
370
|
+
throw new RunContractError(`${path} must be an object`);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function assertExactKeys(value, expected, path) {
|
|
375
|
+
const actual = Object.keys(value).sort();
|
|
376
|
+
const wanted = [...expected].sort();
|
|
377
|
+
if (
|
|
378
|
+
actual.length !== wanted.length ||
|
|
379
|
+
actual.some((key, index) => key !== wanted[index])
|
|
380
|
+
) {
|
|
381
|
+
throw new RunContractError(`${path} must contain ${wanted.join(', ')} only`);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function assertText(value, path) {
|
|
386
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
387
|
+
throw new RunContractError(`${path} must be non-empty text`);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function assertId(value, path) {
|
|
392
|
+
if (typeof value !== 'string' || !ID.test(value)) {
|
|
393
|
+
throw new RunContractError(`${path} is not a safe id`);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function assertCount(value, path) {
|
|
398
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
399
|
+
throw new RunContractError(`${path} must be a non-negative safe integer`);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function assertSafeRelativePath(value, path) {
|
|
404
|
+
if (
|
|
405
|
+
typeof value !== 'string' ||
|
|
406
|
+
!value ||
|
|
407
|
+
value.startsWith('/') ||
|
|
408
|
+
value.includes('\\') ||
|
|
409
|
+
value.split('/').some((part) => !part || part === '.' || part === '..')
|
|
410
|
+
) {
|
|
411
|
+
throw new RunContractError(`${path} must be a safe relative path`);
|
|
412
|
+
}
|
|
413
|
+
}
|