@edjbarron/ux-guard 0.0.0-stage → 0.6.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/LICENSE +201 -0
- package/NOTICE +2 -0
- package/README.md +239 -2
- package/bin/ux-guard.mjs +4 -0
- package/docs/cli-reference.md +145 -0
- package/docs/scoring-and-backslide.md +100 -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 +1980 -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 +134 -0
|
@@ -0,0 +1,1840 @@
|
|
|
1
|
+
import {
|
|
2
|
+
JOURNEY_METRICS,
|
|
3
|
+
validateJourneyReport,
|
|
4
|
+
} from './journeys.mjs';
|
|
5
|
+
import {
|
|
6
|
+
canonicalJson,
|
|
7
|
+
canonicalize,
|
|
8
|
+
policyDigest,
|
|
9
|
+
sha256,
|
|
10
|
+
} from './run-contract.mjs';
|
|
11
|
+
|
|
12
|
+
export const JOURNEY_BASELINE_KIND = 'ux-guard-journey-baseline';
|
|
13
|
+
export const JOURNEY_BASELINE_VERSION = 1;
|
|
14
|
+
export const JOURNEY_COMPARISON_KIND = 'ux-guard-journey-comparison';
|
|
15
|
+
export const JOURNEY_COMPARISON_VERSION = 2;
|
|
16
|
+
|
|
17
|
+
const SHA256 = /^[a-f0-9]{64}$/;
|
|
18
|
+
const DIGEST = /^sha256:[a-f0-9]{64}$/;
|
|
19
|
+
const COMMIT = /^[a-f0-9]{40}$/;
|
|
20
|
+
const ID = /^[a-z0-9]+(?:[._-][a-z0-9]+)*$/;
|
|
21
|
+
const TIMESTAMP = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?Z$/;
|
|
22
|
+
const APPROVAL_STATUSES = new Set(['candidate', 'reviewed']);
|
|
23
|
+
const EXECUTION_STATUSES = new Set([
|
|
24
|
+
'successful',
|
|
25
|
+
'failed',
|
|
26
|
+
'skipped',
|
|
27
|
+
'unexecutable',
|
|
28
|
+
'infrastructure-error',
|
|
29
|
+
]);
|
|
30
|
+
const SEVERITIES = new Set(['low', 'medium', 'high', 'critical']);
|
|
31
|
+
const COMPARISON_STATUSES = new Set([
|
|
32
|
+
'regression',
|
|
33
|
+
'improvement',
|
|
34
|
+
'unchanged',
|
|
35
|
+
'incompatible',
|
|
36
|
+
'unavailable',
|
|
37
|
+
]);
|
|
38
|
+
const CHANGE_STATUSES = new Set([
|
|
39
|
+
'regression',
|
|
40
|
+
'improvement',
|
|
41
|
+
'unchanged',
|
|
42
|
+
'unavailable',
|
|
43
|
+
]);
|
|
44
|
+
const CANARY_STATUSES = new Set(['passed', 'not-recorded']);
|
|
45
|
+
const STATUS_RANK = new Map([
|
|
46
|
+
['infrastructure-error', 0],
|
|
47
|
+
['failed', 1],
|
|
48
|
+
['skipped', 2],
|
|
49
|
+
['unexecutable', 3],
|
|
50
|
+
['successful', 4],
|
|
51
|
+
]);
|
|
52
|
+
const SEVERITY_RANK = new Map([
|
|
53
|
+
['low', 0],
|
|
54
|
+
['medium', 1],
|
|
55
|
+
['high', 2],
|
|
56
|
+
['critical', 3],
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
export const JOURNEY_BASELINE_SCHEMA = Object.freeze({
|
|
60
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
61
|
+
$id: 'urn:ux-guard:journey-baseline:v1',
|
|
62
|
+
type: 'object',
|
|
63
|
+
additionalProperties: false,
|
|
64
|
+
required: [
|
|
65
|
+
'kind',
|
|
66
|
+
'version',
|
|
67
|
+
'canonicalHash',
|
|
68
|
+
'approval',
|
|
69
|
+
'provenance',
|
|
70
|
+
'policy',
|
|
71
|
+
'runProfile',
|
|
72
|
+
'tool',
|
|
73
|
+
'coverage',
|
|
74
|
+
'journeys',
|
|
75
|
+
'findings',
|
|
76
|
+
'tolerances',
|
|
77
|
+
'hardFloors',
|
|
78
|
+
'sourceArtifacts',
|
|
79
|
+
'visualManifestRefs',
|
|
80
|
+
'readinessEvidence',
|
|
81
|
+
],
|
|
82
|
+
properties: {
|
|
83
|
+
kind: { const: JOURNEY_BASELINE_KIND },
|
|
84
|
+
version: { const: JOURNEY_BASELINE_VERSION },
|
|
85
|
+
canonicalHash: { type: 'string', pattern: '^sha256:[a-f0-9]{64}$' },
|
|
86
|
+
approval: { type: 'object', additionalProperties: false },
|
|
87
|
+
provenance: { type: 'object', additionalProperties: false },
|
|
88
|
+
policy: { type: 'object', additionalProperties: false },
|
|
89
|
+
runProfile: { type: 'object', additionalProperties: false },
|
|
90
|
+
tool: { type: 'object', additionalProperties: false },
|
|
91
|
+
coverage: { type: 'object', additionalProperties: false },
|
|
92
|
+
journeys: { type: 'array' },
|
|
93
|
+
findings: { type: 'array' },
|
|
94
|
+
tolerances: { type: 'array' },
|
|
95
|
+
hardFloors: { type: 'object', additionalProperties: false },
|
|
96
|
+
sourceArtifacts: { type: 'array' },
|
|
97
|
+
visualManifestRefs: { type: 'array' },
|
|
98
|
+
readinessEvidence: { type: 'object', additionalProperties: false },
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
export const JOURNEY_COMPARISON_SCHEMA = Object.freeze({
|
|
103
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
104
|
+
$id: 'urn:ux-guard:journey-comparison:v2',
|
|
105
|
+
type: 'object',
|
|
106
|
+
additionalProperties: false,
|
|
107
|
+
required: [
|
|
108
|
+
'kind',
|
|
109
|
+
'version',
|
|
110
|
+
'advisory',
|
|
111
|
+
'trust',
|
|
112
|
+
'status',
|
|
113
|
+
'reason',
|
|
114
|
+
'baselineHash',
|
|
115
|
+
'compatibility',
|
|
116
|
+
'completeness',
|
|
117
|
+
'summary',
|
|
118
|
+
'hardFloors',
|
|
119
|
+
'journeys',
|
|
120
|
+
],
|
|
121
|
+
properties: {
|
|
122
|
+
kind: { const: JOURNEY_COMPARISON_KIND },
|
|
123
|
+
version: { const: JOURNEY_COMPARISON_VERSION },
|
|
124
|
+
advisory: { const: true },
|
|
125
|
+
trust: { type: 'object', additionalProperties: false },
|
|
126
|
+
status: { enum: [...COMPARISON_STATUSES] },
|
|
127
|
+
reason: { type: 'string' },
|
|
128
|
+
baselineHash: { type: ['string', 'null'] },
|
|
129
|
+
compatibility: { type: 'object', additionalProperties: false },
|
|
130
|
+
completeness: { type: 'object', additionalProperties: false },
|
|
131
|
+
summary: { type: 'object', additionalProperties: false },
|
|
132
|
+
hardFloors: { type: 'array' },
|
|
133
|
+
journeys: { type: 'array' },
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
export class JourneyBaselineError extends Error {
|
|
138
|
+
constructor(message) {
|
|
139
|
+
super(message);
|
|
140
|
+
this.name = 'JourneyBaselineError';
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function createJourneyBaselineCandidate({
|
|
145
|
+
report,
|
|
146
|
+
policy,
|
|
147
|
+
reportPath,
|
|
148
|
+
reportSha256,
|
|
149
|
+
canaries = defaultCanaries(),
|
|
150
|
+
} = {}) {
|
|
151
|
+
validateJourneyReport(report);
|
|
152
|
+
assertSafeRelativePath(reportPath, 'reportPath');
|
|
153
|
+
assertHash(reportSha256, 'reportSha256');
|
|
154
|
+
if (report.run.policy.digest !== policyDigest(policy)) {
|
|
155
|
+
throw new JourneyBaselineError('source report policy digest does not match policy');
|
|
156
|
+
}
|
|
157
|
+
if (
|
|
158
|
+
report.run.completion.status !== 'completed' ||
|
|
159
|
+
report.run.completion.received !== report.run.completion.expected ||
|
|
160
|
+
report.run.completion.result !== 'successful'
|
|
161
|
+
) {
|
|
162
|
+
throw new JourneyBaselineError(
|
|
163
|
+
'baseline candidates require a completed report without assertion, skip, or infrastructure failures',
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
if (report.run.browser.name === 'none' || report.run.browser.profile === 'not-run') {
|
|
167
|
+
throw new JourneyBaselineError('baseline candidates require a real deterministic run profile');
|
|
168
|
+
}
|
|
169
|
+
if (
|
|
170
|
+
report.journeys.some(
|
|
171
|
+
(journey) => journey.timing && journey.timing.deterministic !== true,
|
|
172
|
+
)
|
|
173
|
+
) {
|
|
174
|
+
throw new JourneyBaselineError('baseline candidates cannot contain wall-clock measurements');
|
|
175
|
+
}
|
|
176
|
+
if (report.run.artifacts.length === 0) {
|
|
177
|
+
throw new JourneyBaselineError('baseline candidates require hashed source artifacts');
|
|
178
|
+
}
|
|
179
|
+
validateCanaries(canaries, 'canaries');
|
|
180
|
+
|
|
181
|
+
const repetitions = parseRepetitions(report.run.adapters.repetitions);
|
|
182
|
+
const coverage = snapshotCoverage(report);
|
|
183
|
+
const candidate = {
|
|
184
|
+
kind: JOURNEY_BASELINE_KIND,
|
|
185
|
+
version: JOURNEY_BASELINE_VERSION,
|
|
186
|
+
canonicalHash: '',
|
|
187
|
+
approval: {
|
|
188
|
+
status: 'candidate',
|
|
189
|
+
reviewer: null,
|
|
190
|
+
reviewedAt: null,
|
|
191
|
+
rationale: null,
|
|
192
|
+
},
|
|
193
|
+
provenance: {
|
|
194
|
+
candidateCommit: report.run.provenance.candidate.commit,
|
|
195
|
+
baseCommit: report.run.provenance.base.commit,
|
|
196
|
+
sourceReport: {
|
|
197
|
+
path: reportPath,
|
|
198
|
+
sha256: reportSha256,
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
policy: structuredClone(report.run.policy),
|
|
202
|
+
runProfile: {
|
|
203
|
+
browser: structuredClone(report.run.browser),
|
|
204
|
+
adapters: structuredClone(report.run.adapters),
|
|
205
|
+
repetitions,
|
|
206
|
+
},
|
|
207
|
+
tool: structuredClone(report.run.tool),
|
|
208
|
+
coverage,
|
|
209
|
+
journeys: snapshotJourneys(report),
|
|
210
|
+
findings: snapshotFindings(report),
|
|
211
|
+
tolerances: [],
|
|
212
|
+
hardFloors: {
|
|
213
|
+
minimumMeasuredVariants: coverage.measuredVariants,
|
|
214
|
+
minimumSuccessfulVariants: coverage.successfulVariants,
|
|
215
|
+
maximumFailedVariants: coverage.statuses.failed,
|
|
216
|
+
maximumSkippedVariants: coverage.statuses.skipped,
|
|
217
|
+
maximumInfrastructureErrorVariants: coverage.statuses.infrastructureError,
|
|
218
|
+
maximumCriticalFindings: report.findings.filter(
|
|
219
|
+
(finding) => finding.severity === 'critical',
|
|
220
|
+
).length,
|
|
221
|
+
},
|
|
222
|
+
sourceArtifacts: [
|
|
223
|
+
{
|
|
224
|
+
id: 'journey-report',
|
|
225
|
+
path: reportPath,
|
|
226
|
+
sha256: reportSha256,
|
|
227
|
+
},
|
|
228
|
+
...structuredClone(report.run.artifacts),
|
|
229
|
+
].sort((left, right) => left.id.localeCompare(right.id)),
|
|
230
|
+
visualManifestRefs: [],
|
|
231
|
+
readinessEvidence: {
|
|
232
|
+
deterministicRun: true,
|
|
233
|
+
evidenceComplete: coverage.measuredVariants === coverage.requiredVariants,
|
|
234
|
+
stableRepetitions: repetitions,
|
|
235
|
+
canaries: structuredClone(canaries),
|
|
236
|
+
trustedExecution: false,
|
|
237
|
+
enforcementApproved: false,
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
candidate.canonicalHash = journeyBaselineHash(candidate);
|
|
241
|
+
return validateJourneyBaseline(candidate);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function promoteJourneyBaseline(
|
|
245
|
+
candidate,
|
|
246
|
+
{ reviewer, reviewedAt, rationale } = {},
|
|
247
|
+
) {
|
|
248
|
+
validateJourneyBaseline(candidate);
|
|
249
|
+
if (candidate.approval.status !== 'candidate') {
|
|
250
|
+
throw new JourneyBaselineError('only an unapproved candidate can be promoted');
|
|
251
|
+
}
|
|
252
|
+
assertText(reviewer, 'reviewer');
|
|
253
|
+
assertTimestamp(reviewedAt, 'reviewedAt');
|
|
254
|
+
assertText(rationale, 'rationale');
|
|
255
|
+
const reviewed = structuredClone(candidate);
|
|
256
|
+
reviewed.approval = {
|
|
257
|
+
status: 'reviewed',
|
|
258
|
+
reviewer: reviewer.trim(),
|
|
259
|
+
reviewedAt,
|
|
260
|
+
rationale: rationale.trim(),
|
|
261
|
+
};
|
|
262
|
+
reviewed.canonicalHash = journeyBaselineHash(reviewed);
|
|
263
|
+
return validateJourneyBaseline(reviewed);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function validateJourneyBaseline(baseline) {
|
|
267
|
+
assertMap(baseline, 'baseline');
|
|
268
|
+
assertExactKeys(
|
|
269
|
+
baseline,
|
|
270
|
+
[
|
|
271
|
+
'kind',
|
|
272
|
+
'version',
|
|
273
|
+
'canonicalHash',
|
|
274
|
+
'approval',
|
|
275
|
+
'provenance',
|
|
276
|
+
'policy',
|
|
277
|
+
'runProfile',
|
|
278
|
+
'tool',
|
|
279
|
+
'coverage',
|
|
280
|
+
'journeys',
|
|
281
|
+
'findings',
|
|
282
|
+
'tolerances',
|
|
283
|
+
'hardFloors',
|
|
284
|
+
'sourceArtifacts',
|
|
285
|
+
'visualManifestRefs',
|
|
286
|
+
'readinessEvidence',
|
|
287
|
+
],
|
|
288
|
+
'baseline',
|
|
289
|
+
);
|
|
290
|
+
if (
|
|
291
|
+
baseline.kind !== JOURNEY_BASELINE_KIND ||
|
|
292
|
+
baseline.version !== JOURNEY_BASELINE_VERSION
|
|
293
|
+
) {
|
|
294
|
+
throw new JourneyBaselineError('baseline has an unsupported kind or version');
|
|
295
|
+
}
|
|
296
|
+
if (
|
|
297
|
+
typeof baseline.canonicalHash !== 'string' ||
|
|
298
|
+
!DIGEST.test(baseline.canonicalHash) ||
|
|
299
|
+
baseline.canonicalHash !== journeyBaselineHash(baseline)
|
|
300
|
+
) {
|
|
301
|
+
throw new JourneyBaselineError('baseline canonicalHash does not match canonical content');
|
|
302
|
+
}
|
|
303
|
+
validateApproval(baseline.approval);
|
|
304
|
+
validateProvenance(baseline.provenance);
|
|
305
|
+
validatePolicy(baseline.policy);
|
|
306
|
+
validateRunProfile(baseline.runProfile);
|
|
307
|
+
validateTool(baseline.tool);
|
|
308
|
+
validateCoverage(baseline.coverage);
|
|
309
|
+
validateJourneys(baseline.journeys);
|
|
310
|
+
validateFindings(baseline.findings, new Set(baseline.journeys.map(({ journeyRef }) => journeyRef)));
|
|
311
|
+
const findingIds = new Set(baseline.findings.map(({ id }) => id));
|
|
312
|
+
for (const journey of baseline.journeys) {
|
|
313
|
+
if (journey.findingRefs.some((findingRef) => !findingIds.has(findingRef))) {
|
|
314
|
+
throw new JourneyBaselineError(
|
|
315
|
+
`${journey.journeyRef} references an unknown baseline finding`,
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
validateTolerances(
|
|
320
|
+
baseline.tolerances,
|
|
321
|
+
new Map(baseline.journeys.map((journey) => [journey.journeyRef, journey])),
|
|
322
|
+
);
|
|
323
|
+
validateHardFloors(baseline.hardFloors);
|
|
324
|
+
validateArtifacts(baseline.sourceArtifacts, 'baseline.sourceArtifacts');
|
|
325
|
+
validateArtifacts(baseline.visualManifestRefs, 'baseline.visualManifestRefs', true);
|
|
326
|
+
const artifactIds = [
|
|
327
|
+
...baseline.sourceArtifacts,
|
|
328
|
+
...baseline.visualManifestRefs,
|
|
329
|
+
].map(({ id }) => id);
|
|
330
|
+
const artifactPaths = [
|
|
331
|
+
...baseline.sourceArtifacts,
|
|
332
|
+
...baseline.visualManifestRefs,
|
|
333
|
+
].map(({ path }) => path);
|
|
334
|
+
if (
|
|
335
|
+
new Set(artifactIds).size !== artifactIds.length ||
|
|
336
|
+
new Set(artifactPaths).size !== artifactPaths.length
|
|
337
|
+
) {
|
|
338
|
+
throw new JourneyBaselineError(
|
|
339
|
+
'baseline source and visual artifacts must have unique ids and paths',
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
validateReadinessEvidence(baseline.readinessEvidence);
|
|
343
|
+
if (
|
|
344
|
+
baseline.coverage.measuredVariants !== baseline.journeys.length ||
|
|
345
|
+
baseline.coverage.successfulVariants !==
|
|
346
|
+
baseline.journeys.filter(({ status }) => status === 'successful').length
|
|
347
|
+
) {
|
|
348
|
+
throw new JourneyBaselineError('baseline coverage does not match journey snapshots');
|
|
349
|
+
}
|
|
350
|
+
const statusCounts = lifecycleCounts(baseline.journeys);
|
|
351
|
+
for (const [field, value] of Object.entries(statusCounts)) {
|
|
352
|
+
if (baseline.coverage.statuses[field] !== value) {
|
|
353
|
+
throw new JourneyBaselineError('baseline lifecycle counts do not match journey snapshots');
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
for (const finding of baseline.findings) {
|
|
357
|
+
for (const journeyRef of finding.journeyRefs) {
|
|
358
|
+
const journey = baseline.journeys.find((entry) => entry.journeyRef === journeyRef);
|
|
359
|
+
if (!journey.findingRefs.includes(finding.id)) {
|
|
360
|
+
throw new JourneyBaselineError(`${finding.id} is not referenced by ${journeyRef}`);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return baseline;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export function parseJourneyBaseline(text) {
|
|
368
|
+
if (typeof text !== 'string') {
|
|
369
|
+
throw new JourneyBaselineError('journey baseline must be JSON text');
|
|
370
|
+
}
|
|
371
|
+
let baseline;
|
|
372
|
+
try {
|
|
373
|
+
baseline = JSON.parse(text);
|
|
374
|
+
} catch {
|
|
375
|
+
throw new JourneyBaselineError('journey baseline is not valid JSON');
|
|
376
|
+
}
|
|
377
|
+
if (
|
|
378
|
+
baseline?.kind === JOURNEY_BASELINE_KIND &&
|
|
379
|
+
baseline.version !== JOURNEY_BASELINE_VERSION
|
|
380
|
+
) {
|
|
381
|
+
throw new JourneyBaselineError(
|
|
382
|
+
`unsupported journey baseline version ${String(baseline.version)}; migrate to version ${JOURNEY_BASELINE_VERSION}`,
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
return validateJourneyBaseline(baseline);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
export function serializeJourneyBaseline(baseline) {
|
|
389
|
+
validateJourneyBaseline(baseline);
|
|
390
|
+
return `${JSON.stringify(canonicalize(baseline), null, 2)}\n`;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
export function journeyBaselineHash(baseline) {
|
|
394
|
+
const content = structuredClone(baseline);
|
|
395
|
+
delete content.canonicalHash;
|
|
396
|
+
return `sha256:${sha256(content)}`;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
export function compareJourneyBaseline({
|
|
400
|
+
baseline,
|
|
401
|
+
report,
|
|
402
|
+
policy,
|
|
403
|
+
trusted = false,
|
|
404
|
+
} = {}) {
|
|
405
|
+
validateJourneyBaseline(baseline);
|
|
406
|
+
validateJourneyReport(report);
|
|
407
|
+
const compatibilityReasons = baselineCompatibilityReasons(
|
|
408
|
+
baseline,
|
|
409
|
+
report,
|
|
410
|
+
policy,
|
|
411
|
+
trusted,
|
|
412
|
+
);
|
|
413
|
+
if (compatibilityReasons.length) {
|
|
414
|
+
return validateJourneyComparison(
|
|
415
|
+
emptyComparison({
|
|
416
|
+
status:
|
|
417
|
+
baseline.approval.status === 'reviewed' && trusted
|
|
418
|
+
? 'incompatible'
|
|
419
|
+
: 'unavailable',
|
|
420
|
+
reason: compatibilityReasons.join('; '),
|
|
421
|
+
baselineHash: baseline.canonicalHash,
|
|
422
|
+
compatibilityReasons,
|
|
423
|
+
}),
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const current = {
|
|
428
|
+
coverage: snapshotCoverage(report),
|
|
429
|
+
journeys: snapshotJourneys(report),
|
|
430
|
+
findings: snapshotFindings(report),
|
|
431
|
+
};
|
|
432
|
+
const comparison = compareSnapshots(baseline, current);
|
|
433
|
+
comparison.trust = {
|
|
434
|
+
policy: 'trusted-base',
|
|
435
|
+
execution: 'candidate-untrusted',
|
|
436
|
+
};
|
|
437
|
+
comparison.reason =
|
|
438
|
+
comparison.status === 'regression'
|
|
439
|
+
? 'One or more independent journey, finding, metric, or hard-floor regressions were detected.'
|
|
440
|
+
: comparison.status === 'improvement'
|
|
441
|
+
? comparison.completeness.status === 'complete'
|
|
442
|
+
? 'No evaluated regression was detected and at least one independent improvement was measured.'
|
|
443
|
+
: 'At least one independent improvement was measured; some metric evidence remains not evaluated.'
|
|
444
|
+
: comparison.status === 'unchanged'
|
|
445
|
+
? comparison.completeness.status === 'complete'
|
|
446
|
+
? 'All configured comparison evidence is unchanged.'
|
|
447
|
+
: 'All evaluated comparison evidence is unchanged; some metric evidence remains not evaluated.'
|
|
448
|
+
: 'Some metric evidence is not evaluated because a reviewed tolerance or comparable measurement is unavailable.';
|
|
449
|
+
return validateJourneyComparison(comparison);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
export function runJourneyComparisonCanaries() {
|
|
453
|
+
const baseJourney = {
|
|
454
|
+
journeyRef: 'journey.canary.primary',
|
|
455
|
+
useCaseId: 'canary',
|
|
456
|
+
journeyId: 'primary',
|
|
457
|
+
status: 'successful',
|
|
458
|
+
metrics: { clicks: 1 },
|
|
459
|
+
findingRefs: [],
|
|
460
|
+
};
|
|
461
|
+
const baseline = {
|
|
462
|
+
canonicalHash: `sha256:${'a'.repeat(64)}`,
|
|
463
|
+
coverage: canaryCoverage('successful'),
|
|
464
|
+
journeys: [baseJourney],
|
|
465
|
+
findings: [],
|
|
466
|
+
tolerances: [
|
|
467
|
+
{
|
|
468
|
+
journeyRef: baseJourney.journeyRef,
|
|
469
|
+
metric: 'clicks',
|
|
470
|
+
direction: 'lower-is-better',
|
|
471
|
+
permittedRegression: 0,
|
|
472
|
+
},
|
|
473
|
+
],
|
|
474
|
+
hardFloors: {
|
|
475
|
+
minimumMeasuredVariants: 1,
|
|
476
|
+
minimumSuccessfulVariants: 1,
|
|
477
|
+
maximumFailedVariants: 0,
|
|
478
|
+
maximumSkippedVariants: 0,
|
|
479
|
+
maximumInfrastructureErrorVariants: 0,
|
|
480
|
+
maximumCriticalFindings: 0,
|
|
481
|
+
},
|
|
482
|
+
};
|
|
483
|
+
const snapshot = (journey) => ({
|
|
484
|
+
coverage: canaryCoverage(journey.status),
|
|
485
|
+
journeys: [journey],
|
|
486
|
+
findings: [],
|
|
487
|
+
});
|
|
488
|
+
const regression = compareSnapshots(baseline, snapshot({
|
|
489
|
+
...baseJourney,
|
|
490
|
+
metrics: { clicks: 2 },
|
|
491
|
+
}));
|
|
492
|
+
const improvement = compareSnapshots(baseline, snapshot({
|
|
493
|
+
...baseJourney,
|
|
494
|
+
metrics: { clicks: 0 },
|
|
495
|
+
}));
|
|
496
|
+
const unchanged = compareSnapshots(baseline, snapshot(baseJourney));
|
|
497
|
+
const withoutTolerance = compareSnapshots(
|
|
498
|
+
{ ...baseline, tolerances: [] },
|
|
499
|
+
snapshot({
|
|
500
|
+
...baseJourney,
|
|
501
|
+
metrics: { clicks: 2 },
|
|
502
|
+
}),
|
|
503
|
+
);
|
|
504
|
+
const knownBadJourney = {
|
|
505
|
+
...baseJourney,
|
|
506
|
+
status: 'unexecutable',
|
|
507
|
+
metrics: { clicks: 1 },
|
|
508
|
+
findingRefs: ['finding.canary.known-bad'],
|
|
509
|
+
};
|
|
510
|
+
const knownBad = {
|
|
511
|
+
...baseline,
|
|
512
|
+
coverage: canaryCoverage('unexecutable'),
|
|
513
|
+
journeys: [knownBadJourney],
|
|
514
|
+
findings: [
|
|
515
|
+
{
|
|
516
|
+
id: 'finding.canary.known-bad',
|
|
517
|
+
ruleId: 'canary.known-bad',
|
|
518
|
+
severity: 'high',
|
|
519
|
+
dimension: 'clarity',
|
|
520
|
+
type: 'requirement',
|
|
521
|
+
journeyRefs: [knownBadJourney.journeyRef],
|
|
522
|
+
},
|
|
523
|
+
],
|
|
524
|
+
hardFloors: {
|
|
525
|
+
...baseline.hardFloors,
|
|
526
|
+
minimumSuccessfulVariants: 0,
|
|
527
|
+
},
|
|
528
|
+
};
|
|
529
|
+
const knownBadResult = compareSnapshots(knownBad, {
|
|
530
|
+
coverage: canaryCoverage('unexecutable'),
|
|
531
|
+
journeys: [knownBadJourney],
|
|
532
|
+
findings: knownBad.findings,
|
|
533
|
+
});
|
|
534
|
+
const canaryPolicy = { canary: true };
|
|
535
|
+
const profileBaseline = {
|
|
536
|
+
approval: { status: 'reviewed' },
|
|
537
|
+
policy: { digest: policyDigest(canaryPolicy) },
|
|
538
|
+
tool: { name: 'ux-guard', version: 'canary', schemas: { canary: 1 } },
|
|
539
|
+
runProfile: {
|
|
540
|
+
browser: { name: 'chromium', profile: 'canary' },
|
|
541
|
+
adapters: { canary: '1' },
|
|
542
|
+
},
|
|
543
|
+
};
|
|
544
|
+
const profileReport = {
|
|
545
|
+
run: {
|
|
546
|
+
policy: { digest: policyDigest(canaryPolicy) },
|
|
547
|
+
tool: { name: 'ux-guard', version: 'canary', schemas: { canary: 1 } },
|
|
548
|
+
browser: { name: 'chromium', profile: 'different' },
|
|
549
|
+
adapters: { canary: '1' },
|
|
550
|
+
},
|
|
551
|
+
};
|
|
552
|
+
const compatibility = baselineCompatibilityReasons(
|
|
553
|
+
profileBaseline,
|
|
554
|
+
profileReport,
|
|
555
|
+
canaryPolicy,
|
|
556
|
+
true,
|
|
557
|
+
);
|
|
558
|
+
if (
|
|
559
|
+
regression.status !== 'regression' ||
|
|
560
|
+
improvement.status !== 'improvement' ||
|
|
561
|
+
unchanged.status !== 'unchanged' ||
|
|
562
|
+
withoutTolerance.status !== 'unavailable' ||
|
|
563
|
+
withoutTolerance.journeys[0].metrics[0].status !== 'unavailable' ||
|
|
564
|
+
knownBadResult.status !== 'unchanged' ||
|
|
565
|
+
!compatibility.includes('browser profile differs from the reviewed baseline')
|
|
566
|
+
) {
|
|
567
|
+
throw new JourneyBaselineError('synthetic journey comparison canary did not detect its known changes');
|
|
568
|
+
}
|
|
569
|
+
return {
|
|
570
|
+
regression: 'passed',
|
|
571
|
+
improvement: 'passed',
|
|
572
|
+
compatibility: 'passed',
|
|
573
|
+
knownBad: 'passed',
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
export function compareJourneyBaselineEnvelopes(before, after) {
|
|
578
|
+
validateJourneyBaseline(before);
|
|
579
|
+
validateJourneyBaseline(after);
|
|
580
|
+
const compatibilityReasons = [];
|
|
581
|
+
if (before.policy.digest !== after.policy.digest) {
|
|
582
|
+
compatibilityReasons.push('policy digest differs');
|
|
583
|
+
}
|
|
584
|
+
if (
|
|
585
|
+
before.tool.name !== after.tool.name ||
|
|
586
|
+
canonicalJson(comparisonCriticalSchemas(before.tool.schemas)) !==
|
|
587
|
+
canonicalJson(comparisonCriticalSchemas(after.tool.schemas))
|
|
588
|
+
) {
|
|
589
|
+
compatibilityReasons.push('tool or measurement schema versions differ');
|
|
590
|
+
}
|
|
591
|
+
if (
|
|
592
|
+
canonicalJson(before.runProfile.browser) !==
|
|
593
|
+
canonicalJson(after.runProfile.browser) ||
|
|
594
|
+
canonicalJson(before.runProfile.adapters) !==
|
|
595
|
+
canonicalJson(after.runProfile.adapters)
|
|
596
|
+
) {
|
|
597
|
+
compatibilityReasons.push('run profile differs');
|
|
598
|
+
}
|
|
599
|
+
if (compatibilityReasons.length) {
|
|
600
|
+
return validateJourneyComparison(
|
|
601
|
+
emptyComparison({
|
|
602
|
+
status: 'incompatible',
|
|
603
|
+
reason: compatibilityReasons.join('; '),
|
|
604
|
+
baselineHash: before.canonicalHash,
|
|
605
|
+
compatibilityReasons,
|
|
606
|
+
}),
|
|
607
|
+
);
|
|
608
|
+
}
|
|
609
|
+
const comparison = compareSnapshots(before, after);
|
|
610
|
+
comparison.trust = {
|
|
611
|
+
policy: before.approval.status === 'reviewed' ? 'reviewed-artifact' : 'candidate',
|
|
612
|
+
execution: 'artifact-diff',
|
|
613
|
+
};
|
|
614
|
+
comparison.reason =
|
|
615
|
+
comparison.status === 'regression'
|
|
616
|
+
? 'The candidate snapshot contains one or more independent regressions.'
|
|
617
|
+
: comparison.status === 'improvement'
|
|
618
|
+
? comparison.completeness.status === 'complete'
|
|
619
|
+
? 'The candidate snapshot contains improvements and no evaluated regressions.'
|
|
620
|
+
: 'The candidate snapshot contains improvements while some metric evidence remains not evaluated.'
|
|
621
|
+
: comparison.status === 'unchanged'
|
|
622
|
+
? comparison.completeness.status === 'complete'
|
|
623
|
+
? 'The candidate snapshot is unchanged for every configured comparison.'
|
|
624
|
+
: 'All evaluated candidate comparison evidence is unchanged while some metric evidence remains not evaluated.'
|
|
625
|
+
: 'Some candidate metric evidence is not evaluated because a reviewed tolerance or comparable measurement is unavailable.';
|
|
626
|
+
return validateJourneyComparison(comparison);
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
export function unavailableJourneyComparison(reason) {
|
|
630
|
+
assertText(reason, 'reason');
|
|
631
|
+
return validateJourneyComparison(
|
|
632
|
+
emptyComparison({
|
|
633
|
+
status: 'unavailable',
|
|
634
|
+
reason,
|
|
635
|
+
baselineHash: null,
|
|
636
|
+
compatibilityReasons: [],
|
|
637
|
+
}),
|
|
638
|
+
);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
export function validateJourneyComparison(comparison) {
|
|
642
|
+
assertMap(comparison, 'comparison');
|
|
643
|
+
assertExactKeys(
|
|
644
|
+
comparison,
|
|
645
|
+
[
|
|
646
|
+
'kind',
|
|
647
|
+
'version',
|
|
648
|
+
'advisory',
|
|
649
|
+
'trust',
|
|
650
|
+
'status',
|
|
651
|
+
'reason',
|
|
652
|
+
'baselineHash',
|
|
653
|
+
'compatibility',
|
|
654
|
+
'completeness',
|
|
655
|
+
'summary',
|
|
656
|
+
'hardFloors',
|
|
657
|
+
'journeys',
|
|
658
|
+
],
|
|
659
|
+
'comparison',
|
|
660
|
+
);
|
|
661
|
+
if (
|
|
662
|
+
comparison.kind !== JOURNEY_COMPARISON_KIND ||
|
|
663
|
+
comparison.version !== JOURNEY_COMPARISON_VERSION ||
|
|
664
|
+
comparison.advisory !== true
|
|
665
|
+
) {
|
|
666
|
+
throw new JourneyBaselineError('comparison has an unsupported contract');
|
|
667
|
+
}
|
|
668
|
+
assertMap(comparison.trust, 'comparison.trust');
|
|
669
|
+
assertExactKeys(comparison.trust, ['policy', 'execution'], 'comparison.trust');
|
|
670
|
+
assertText(comparison.trust.policy, 'comparison.trust.policy');
|
|
671
|
+
assertText(comparison.trust.execution, 'comparison.trust.execution');
|
|
672
|
+
if (!COMPARISON_STATUSES.has(comparison.status)) {
|
|
673
|
+
throw new JourneyBaselineError('comparison.status is invalid');
|
|
674
|
+
}
|
|
675
|
+
assertText(comparison.reason, 'comparison.reason');
|
|
676
|
+
if (
|
|
677
|
+
comparison.baselineHash !== null &&
|
|
678
|
+
(typeof comparison.baselineHash !== 'string' || !DIGEST.test(comparison.baselineHash))
|
|
679
|
+
) {
|
|
680
|
+
throw new JourneyBaselineError('comparison.baselineHash is invalid');
|
|
681
|
+
}
|
|
682
|
+
assertMap(comparison.compatibility, 'comparison.compatibility');
|
|
683
|
+
assertExactKeys(
|
|
684
|
+
comparison.compatibility,
|
|
685
|
+
['status', 'reasons'],
|
|
686
|
+
'comparison.compatibility',
|
|
687
|
+
);
|
|
688
|
+
if (!['compatible', 'incompatible', 'unavailable'].includes(comparison.compatibility.status)) {
|
|
689
|
+
throw new JourneyBaselineError('comparison.compatibility.status is invalid');
|
|
690
|
+
}
|
|
691
|
+
if (
|
|
692
|
+
!Array.isArray(comparison.compatibility.reasons) ||
|
|
693
|
+
comparison.compatibility.reasons.some((reason) => typeof reason !== 'string' || !reason)
|
|
694
|
+
) {
|
|
695
|
+
throw new JourneyBaselineError('comparison.compatibility.reasons is invalid');
|
|
696
|
+
}
|
|
697
|
+
validateComparisonCompleteness(comparison.completeness);
|
|
698
|
+
assertMap(comparison.summary, 'comparison.summary');
|
|
699
|
+
assertExactKeys(
|
|
700
|
+
comparison.summary,
|
|
701
|
+
['regressions', 'improvements', 'unchanged', 'unavailable'],
|
|
702
|
+
'comparison.summary',
|
|
703
|
+
);
|
|
704
|
+
for (const field of ['regressions', 'improvements', 'unchanged', 'unavailable']) {
|
|
705
|
+
assertCount(comparison.summary[field], `comparison.summary.${field}`);
|
|
706
|
+
}
|
|
707
|
+
if (!Array.isArray(comparison.hardFloors) || !Array.isArray(comparison.journeys)) {
|
|
708
|
+
throw new JourneyBaselineError('comparison hardFloors and journeys must be arrays');
|
|
709
|
+
}
|
|
710
|
+
const floorIds = new Set();
|
|
711
|
+
for (const [index, floor] of comparison.hardFloors.entries()) {
|
|
712
|
+
const path = `comparison.hardFloors[${index}]`;
|
|
713
|
+
assertMap(floor, path);
|
|
714
|
+
assertExactKeys(floor, ['id', 'status', 'baseline', 'current'], path);
|
|
715
|
+
assertId(floor.id, `${path}.id`);
|
|
716
|
+
if (floorIds.has(floor.id)) throw new JourneyBaselineError(`duplicate hard floor ${floor.id}`);
|
|
717
|
+
floorIds.add(floor.id);
|
|
718
|
+
if (!CHANGE_STATUSES.has(floor.status)) {
|
|
719
|
+
throw new JourneyBaselineError(`${path}.status is invalid`);
|
|
720
|
+
}
|
|
721
|
+
assertCount(floor.baseline, `${path}.baseline`);
|
|
722
|
+
assertCount(floor.current, `${path}.current`);
|
|
723
|
+
}
|
|
724
|
+
const journeyIds = new Set();
|
|
725
|
+
for (const [index, journey] of comparison.journeys.entries()) {
|
|
726
|
+
validateJourneyComparisonRecord(journey, `comparison.journeys[${index}]`);
|
|
727
|
+
if (journeyIds.has(journey.journeyRef)) {
|
|
728
|
+
throw new JourneyBaselineError(`duplicate comparison journey ${journey.journeyRef}`);
|
|
729
|
+
}
|
|
730
|
+
journeyIds.add(journey.journeyRef);
|
|
731
|
+
}
|
|
732
|
+
if (comparison.compatibility.status === 'compatible') {
|
|
733
|
+
const expected = metricCompleteness(
|
|
734
|
+
comparison.journeys.flatMap((journey) => journey.metrics),
|
|
735
|
+
);
|
|
736
|
+
if (canonicalJson(comparison.completeness) !== canonicalJson(expected)) {
|
|
737
|
+
throw new JourneyBaselineError(
|
|
738
|
+
'comparison.completeness does not match its metric records',
|
|
739
|
+
);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
return comparison;
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
export function serializeJourneyComparison(comparison) {
|
|
746
|
+
validateJourneyComparison(comparison);
|
|
747
|
+
return `${JSON.stringify(canonicalize(comparison), null, 2)}\n`;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
export function renderJourneyBaselineMarkdown(baseline) {
|
|
751
|
+
validateJourneyBaseline(baseline);
|
|
752
|
+
const lines = [
|
|
753
|
+
'# ux-guard journey baseline candidate',
|
|
754
|
+
'',
|
|
755
|
+
`- **Approval:** ${baseline.approval.status === 'reviewed' ? `Reviewed by ${baseline.approval.reviewer}` : 'Unapproved candidate — not trusted for comparison'}`,
|
|
756
|
+
`- **Canonical hash:** \`${baseline.canonicalHash}\``,
|
|
757
|
+
`- **Policy digest:** \`${baseline.policy.digest}\``,
|
|
758
|
+
`- **Profile:** ${baseline.runProfile.browser.name} / ${baseline.runProfile.browser.profile}`,
|
|
759
|
+
`- **Repetitions:** ${baseline.runProfile.repetitions}`,
|
|
760
|
+
`- **Coverage snapshot:** ${baseline.coverage.successfulVariants}/${baseline.coverage.requiredVariants} successful; ${baseline.coverage.measuredVariants}/${baseline.coverage.requiredVariants} measured`,
|
|
761
|
+
`- **Metric tolerances:** ${baseline.tolerances.length ? `${baseline.tolerances.length} explicitly reviewed` : 'None; raw metrics will be unavailable for regression decisions until reviewed'}`,
|
|
762
|
+
'- **Meaning:** This is measured current-state evidence, not approval of existing defects.',
|
|
763
|
+
'',
|
|
764
|
+
'## Hard floors',
|
|
765
|
+
'',
|
|
766
|
+
...Object.entries(baseline.hardFloors).map(
|
|
767
|
+
([name, value]) => `- \`${name}\`: ${value}`,
|
|
768
|
+
),
|
|
769
|
+
'',
|
|
770
|
+
'## Findings snapshot',
|
|
771
|
+
'',
|
|
772
|
+
];
|
|
773
|
+
if (baseline.findings.length === 0) lines.push('None.');
|
|
774
|
+
else {
|
|
775
|
+
for (const finding of baseline.findings) {
|
|
776
|
+
lines.push(`- **${finding.ruleId}** — ${finding.severity}; ${finding.journeyRefs.length} journey(s)`);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
return `${lines.join('\n')}\n`;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
export function renderJourneyComparisonMarkdown(comparison) {
|
|
783
|
+
validateJourneyComparison(comparison);
|
|
784
|
+
const lines = [
|
|
785
|
+
'# ux-guard advisory journey comparison',
|
|
786
|
+
'',
|
|
787
|
+
'> Candidate code performs this comparison. It is advisory until trusted execution is pinned in roadmap step 7.',
|
|
788
|
+
'',
|
|
789
|
+
`- **Status:** ${comparison.status}`,
|
|
790
|
+
`- **Reason:** ${comparison.reason}`,
|
|
791
|
+
`- **Baseline:** ${comparison.baselineHash ? `\`${comparison.baselineHash}\`` : 'Not available'}`,
|
|
792
|
+
`- **Changes:** ${comparison.summary.regressions} regression(s), ${comparison.summary.improvements} improvement(s), ${comparison.summary.unchanged} unchanged, ${comparison.summary.unavailable} unavailable`,
|
|
793
|
+
`- **Metric completeness:** ${comparison.completeness.status} (${comparison.completeness.metrics.evaluated}/${comparison.completeness.metrics.total} evaluated; ${comparison.completeness.metrics.notEvaluated} not evaluated)`,
|
|
794
|
+
'',
|
|
795
|
+
'## Hard floors',
|
|
796
|
+
'',
|
|
797
|
+
];
|
|
798
|
+
if (comparison.hardFloors.length === 0) lines.push('Not evaluated.');
|
|
799
|
+
else {
|
|
800
|
+
for (const floor of comparison.hardFloors) {
|
|
801
|
+
lines.push(
|
|
802
|
+
`- \`${floor.id}\`: ${floor.status} (baseline ${floor.baseline}, current ${floor.current})`,
|
|
803
|
+
);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
lines.push('', '## Journey changes', '');
|
|
807
|
+
if (comparison.journeys.length === 0) lines.push('Not evaluated.');
|
|
808
|
+
else {
|
|
809
|
+
for (const journey of comparison.journeys) {
|
|
810
|
+
lines.push(`- \`${journey.journeyRef}\`: ${journey.status}`);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
return `${lines.join('\n')}\n`;
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
function baselineCompatibilityReasons(baseline, report, policy, trusted) {
|
|
817
|
+
const reasons = [];
|
|
818
|
+
if (baseline.approval.status !== 'reviewed') {
|
|
819
|
+
reasons.push('baseline is an unapproved candidate');
|
|
820
|
+
}
|
|
821
|
+
if (!trusted) {
|
|
822
|
+
reasons.push('baseline was not loaded from an explicit trusted revision');
|
|
823
|
+
}
|
|
824
|
+
if (baseline.policy.digest !== policyDigest(policy)) {
|
|
825
|
+
reasons.push('policy digest differs from the reviewed baseline');
|
|
826
|
+
}
|
|
827
|
+
if (baseline.policy.digest !== report.run.policy.digest) {
|
|
828
|
+
reasons.push('current report policy digest differs from the reviewed baseline');
|
|
829
|
+
}
|
|
830
|
+
if (baseline.tool.name !== report.run.tool.name) {
|
|
831
|
+
reasons.push('tool name differs from the reviewed baseline');
|
|
832
|
+
}
|
|
833
|
+
if (
|
|
834
|
+
canonicalJson(comparisonCriticalSchemas(baseline.tool.schemas)) !==
|
|
835
|
+
canonicalJson(comparisonCriticalSchemas(report.run.tool.schemas))
|
|
836
|
+
) {
|
|
837
|
+
reasons.push('measurement schema versions differ from the reviewed baseline');
|
|
838
|
+
}
|
|
839
|
+
if (
|
|
840
|
+
canonicalJson(baseline.runProfile.browser) !==
|
|
841
|
+
canonicalJson(report.run.browser)
|
|
842
|
+
) {
|
|
843
|
+
reasons.push('browser profile differs from the reviewed baseline');
|
|
844
|
+
}
|
|
845
|
+
if (
|
|
846
|
+
canonicalJson(baseline.runProfile.adapters) !==
|
|
847
|
+
canonicalJson(report.run.adapters)
|
|
848
|
+
) {
|
|
849
|
+
reasons.push('adapter profile differs from the reviewed baseline');
|
|
850
|
+
}
|
|
851
|
+
return reasons;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
function compareSnapshots(baseline, current) {
|
|
855
|
+
const tolerances = new Map(
|
|
856
|
+
baseline.tolerances.map((tolerance) => [
|
|
857
|
+
`${tolerance.journeyRef}\0${tolerance.metric}`,
|
|
858
|
+
tolerance,
|
|
859
|
+
]),
|
|
860
|
+
);
|
|
861
|
+
const beforeFindings = new Map(baseline.findings.map((finding) => [finding.id, finding]));
|
|
862
|
+
const afterFindings = new Map(current.findings.map((finding) => [finding.id, finding]));
|
|
863
|
+
const currentJourneys = new Map(
|
|
864
|
+
current.journeys.map((journey) => [journey.journeyRef, journey]),
|
|
865
|
+
);
|
|
866
|
+
const journeys = baseline.journeys.map((before) => {
|
|
867
|
+
const after = currentJourneys.get(before.journeyRef);
|
|
868
|
+
if (!after) {
|
|
869
|
+
return {
|
|
870
|
+
journeyRef: before.journeyRef,
|
|
871
|
+
status: 'regression',
|
|
872
|
+
lifecycle: {
|
|
873
|
+
status: 'regression',
|
|
874
|
+
baseline: before.status,
|
|
875
|
+
current: 'unavailable',
|
|
876
|
+
},
|
|
877
|
+
findings: {
|
|
878
|
+
status: 'unavailable',
|
|
879
|
+
added: [],
|
|
880
|
+
removed: [],
|
|
881
|
+
severityChanges: [],
|
|
882
|
+
},
|
|
883
|
+
metrics: [],
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
const lifecycle = compareLifecycle(before.status, after.status);
|
|
887
|
+
const findingComparison = compareFindings(
|
|
888
|
+
before.findingRefs,
|
|
889
|
+
after.findingRefs,
|
|
890
|
+
beforeFindings,
|
|
891
|
+
afterFindings,
|
|
892
|
+
);
|
|
893
|
+
const metricNames = [...new Set([
|
|
894
|
+
...Object.keys(before.metrics ?? {}),
|
|
895
|
+
...Object.keys(after.metrics ?? {}),
|
|
896
|
+
])].sort();
|
|
897
|
+
const metrics = metricNames.map((metric) =>
|
|
898
|
+
compareMetric(
|
|
899
|
+
before.journeyRef,
|
|
900
|
+
metric,
|
|
901
|
+
before.metrics?.[metric],
|
|
902
|
+
after.metrics?.[metric],
|
|
903
|
+
tolerances.get(`${before.journeyRef}\0${metric}`),
|
|
904
|
+
),
|
|
905
|
+
);
|
|
906
|
+
const evaluatedMetricStatuses = metrics
|
|
907
|
+
.filter(({ status }) => status !== 'unavailable')
|
|
908
|
+
.map(({ status }) => status);
|
|
909
|
+
const nonMetricStatuses = [lifecycle.status, findingComparison.status];
|
|
910
|
+
const detectedNonMetricChange = nonMetricStatuses.some(
|
|
911
|
+
(status) => status === 'regression' || status === 'improvement',
|
|
912
|
+
);
|
|
913
|
+
return {
|
|
914
|
+
journeyRef: before.journeyRef,
|
|
915
|
+
status:
|
|
916
|
+
evaluatedMetricStatuses.length > 0 || detectedNonMetricChange
|
|
917
|
+
? combineEvaluatedStatuses([
|
|
918
|
+
...nonMetricStatuses,
|
|
919
|
+
...evaluatedMetricStatuses,
|
|
920
|
+
])
|
|
921
|
+
: 'unavailable',
|
|
922
|
+
lifecycle,
|
|
923
|
+
findings: findingComparison,
|
|
924
|
+
metrics,
|
|
925
|
+
};
|
|
926
|
+
});
|
|
927
|
+
for (const after of current.journeys) {
|
|
928
|
+
if (!baseline.journeys.some(({ journeyRef }) => journeyRef === after.journeyRef)) {
|
|
929
|
+
journeys.push({
|
|
930
|
+
journeyRef: after.journeyRef,
|
|
931
|
+
status: 'unavailable',
|
|
932
|
+
lifecycle: {
|
|
933
|
+
status: 'unavailable',
|
|
934
|
+
baseline: 'unavailable',
|
|
935
|
+
current: after.status,
|
|
936
|
+
},
|
|
937
|
+
findings: {
|
|
938
|
+
status: 'unavailable',
|
|
939
|
+
added: [],
|
|
940
|
+
removed: [],
|
|
941
|
+
severityChanges: [],
|
|
942
|
+
},
|
|
943
|
+
metrics: [],
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
journeys.sort((left, right) => left.journeyRef.localeCompare(right.journeyRef));
|
|
948
|
+
|
|
949
|
+
const currentCritical = current.findings.filter(
|
|
950
|
+
(finding) => finding.severity === 'critical',
|
|
951
|
+
).length;
|
|
952
|
+
const floorValues = [
|
|
953
|
+
[
|
|
954
|
+
'required-evidence',
|
|
955
|
+
baseline.hardFloors.minimumMeasuredVariants,
|
|
956
|
+
current.coverage.measuredVariants,
|
|
957
|
+
(value, floor) => value < floor,
|
|
958
|
+
],
|
|
959
|
+
[
|
|
960
|
+
'required-completion',
|
|
961
|
+
baseline.hardFloors.minimumSuccessfulVariants,
|
|
962
|
+
current.coverage.successfulVariants,
|
|
963
|
+
(value, floor) => value < floor,
|
|
964
|
+
],
|
|
965
|
+
[
|
|
966
|
+
'failed-lifecycle',
|
|
967
|
+
baseline.hardFloors.maximumFailedVariants,
|
|
968
|
+
current.coverage.statuses.failed,
|
|
969
|
+
(value, floor) => value > floor,
|
|
970
|
+
],
|
|
971
|
+
[
|
|
972
|
+
'skipped-lifecycle',
|
|
973
|
+
baseline.hardFloors.maximumSkippedVariants,
|
|
974
|
+
current.coverage.statuses.skipped,
|
|
975
|
+
(value, floor) => value > floor,
|
|
976
|
+
],
|
|
977
|
+
[
|
|
978
|
+
'infrastructure-lifecycle',
|
|
979
|
+
baseline.hardFloors.maximumInfrastructureErrorVariants,
|
|
980
|
+
current.coverage.statuses.infrastructureError,
|
|
981
|
+
(value, floor) => value > floor,
|
|
982
|
+
],
|
|
983
|
+
[
|
|
984
|
+
'critical-findings',
|
|
985
|
+
baseline.hardFloors.maximumCriticalFindings,
|
|
986
|
+
currentCritical,
|
|
987
|
+
(value, floor) => value > floor,
|
|
988
|
+
],
|
|
989
|
+
];
|
|
990
|
+
const hardFloors = floorValues.map(([id, floor, value, regressed]) => ({
|
|
991
|
+
id,
|
|
992
|
+
status: regressed(value, floor)
|
|
993
|
+
? 'regression'
|
|
994
|
+
: value === floor
|
|
995
|
+
? 'unchanged'
|
|
996
|
+
: 'improvement',
|
|
997
|
+
baseline: floor,
|
|
998
|
+
current: value,
|
|
999
|
+
}));
|
|
1000
|
+
const allStatuses = [
|
|
1001
|
+
...journeys.map(({ status }) => status),
|
|
1002
|
+
...hardFloors.map(({ status }) => status),
|
|
1003
|
+
];
|
|
1004
|
+
const completeness = metricCompleteness(
|
|
1005
|
+
journeys.flatMap((journey) => journey.metrics),
|
|
1006
|
+
);
|
|
1007
|
+
const detectedChange = allStatuses.some(
|
|
1008
|
+
(status) => status === 'regression' || status === 'improvement',
|
|
1009
|
+
);
|
|
1010
|
+
return {
|
|
1011
|
+
kind: JOURNEY_COMPARISON_KIND,
|
|
1012
|
+
version: JOURNEY_COMPARISON_VERSION,
|
|
1013
|
+
advisory: true,
|
|
1014
|
+
trust: { policy: 'reviewed-artifact', execution: 'candidate-untrusted' },
|
|
1015
|
+
status:
|
|
1016
|
+
completeness.metrics.evaluated > 0 || detectedChange
|
|
1017
|
+
? combineEvaluatedStatuses(allStatuses)
|
|
1018
|
+
: 'unavailable',
|
|
1019
|
+
reason: 'Comparison completed.',
|
|
1020
|
+
baselineHash: baseline.canonicalHash,
|
|
1021
|
+
compatibility: { status: 'compatible', reasons: [] },
|
|
1022
|
+
completeness,
|
|
1023
|
+
summary: statusSummary(allStatuses),
|
|
1024
|
+
hardFloors,
|
|
1025
|
+
journeys,
|
|
1026
|
+
};
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
function compareLifecycle(before, after) {
|
|
1030
|
+
let status = 'unchanged';
|
|
1031
|
+
if (before !== after) {
|
|
1032
|
+
status = STATUS_RANK.get(after) > STATUS_RANK.get(before) ? 'improvement' : 'regression';
|
|
1033
|
+
}
|
|
1034
|
+
return { status, baseline: before, current: after };
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
function compareFindings(beforeRefs, afterRefs, beforeFindings, afterFindings) {
|
|
1038
|
+
const before = new Set(beforeRefs);
|
|
1039
|
+
const after = new Set(afterRefs);
|
|
1040
|
+
const added = [...after].filter((id) => !before.has(id)).sort();
|
|
1041
|
+
const removed = [...before].filter((id) => !after.has(id)).sort();
|
|
1042
|
+
const severityChanges = [...before]
|
|
1043
|
+
.filter((id) => after.has(id))
|
|
1044
|
+
.map((id) => ({
|
|
1045
|
+
findingRef: id,
|
|
1046
|
+
baseline: beforeFindings.get(id)?.severity ?? 'unavailable',
|
|
1047
|
+
current: afterFindings.get(id)?.severity ?? 'unavailable',
|
|
1048
|
+
}))
|
|
1049
|
+
.filter(({ baseline, current }) => baseline !== current)
|
|
1050
|
+
.sort((left, right) => left.findingRef.localeCompare(right.findingRef));
|
|
1051
|
+
const severityStatuses = severityChanges.map(({ baseline, current }) =>
|
|
1052
|
+
SEVERITY_RANK.has(baseline) && SEVERITY_RANK.has(current)
|
|
1053
|
+
? SEVERITY_RANK.get(current) > SEVERITY_RANK.get(baseline)
|
|
1054
|
+
? 'regression'
|
|
1055
|
+
: 'improvement'
|
|
1056
|
+
: 'unavailable',
|
|
1057
|
+
);
|
|
1058
|
+
return {
|
|
1059
|
+
status: combineStatuses([
|
|
1060
|
+
...added.map(() => 'regression'),
|
|
1061
|
+
...removed.map(() => 'improvement'),
|
|
1062
|
+
...severityStatuses,
|
|
1063
|
+
...(added.length || removed.length || severityStatuses.length ? [] : ['unchanged']),
|
|
1064
|
+
]),
|
|
1065
|
+
added,
|
|
1066
|
+
removed,
|
|
1067
|
+
severityChanges,
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
function compareMetric(journeyRef, metric, before, after, tolerance) {
|
|
1072
|
+
if (before === undefined || after === undefined || !tolerance) {
|
|
1073
|
+
return {
|
|
1074
|
+
metric,
|
|
1075
|
+
status: 'unavailable',
|
|
1076
|
+
baseline: before ?? null,
|
|
1077
|
+
current: after ?? null,
|
|
1078
|
+
delta: before === undefined || after === undefined ? null : after - before,
|
|
1079
|
+
tolerance: tolerance
|
|
1080
|
+
? {
|
|
1081
|
+
direction: tolerance.direction,
|
|
1082
|
+
permittedRegression: tolerance.permittedRegression,
|
|
1083
|
+
}
|
|
1084
|
+
: null,
|
|
1085
|
+
};
|
|
1086
|
+
}
|
|
1087
|
+
const delta = after - before;
|
|
1088
|
+
let status = 'unchanged';
|
|
1089
|
+
if (tolerance.direction === 'lower-is-better') {
|
|
1090
|
+
if (delta > tolerance.permittedRegression) status = 'regression';
|
|
1091
|
+
else if (delta < 0) status = 'improvement';
|
|
1092
|
+
} else {
|
|
1093
|
+
if (-delta > tolerance.permittedRegression) status = 'regression';
|
|
1094
|
+
else if (delta > 0) status = 'improvement';
|
|
1095
|
+
}
|
|
1096
|
+
return {
|
|
1097
|
+
metric,
|
|
1098
|
+
status,
|
|
1099
|
+
baseline: before,
|
|
1100
|
+
current: after,
|
|
1101
|
+
delta,
|
|
1102
|
+
tolerance: {
|
|
1103
|
+
direction: tolerance.direction,
|
|
1104
|
+
permittedRegression: tolerance.permittedRegression,
|
|
1105
|
+
},
|
|
1106
|
+
};
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
function metricCompleteness(metrics) {
|
|
1110
|
+
const total = metrics.length;
|
|
1111
|
+
const notEvaluated = metrics.filter(
|
|
1112
|
+
({ status }) => status === 'unavailable',
|
|
1113
|
+
).length;
|
|
1114
|
+
const evaluated = total - notEvaluated;
|
|
1115
|
+
return {
|
|
1116
|
+
status:
|
|
1117
|
+
total > 0 && evaluated === 0
|
|
1118
|
+
? 'not-evaluated'
|
|
1119
|
+
: notEvaluated > 0
|
|
1120
|
+
? 'partial'
|
|
1121
|
+
: 'complete',
|
|
1122
|
+
metrics: {
|
|
1123
|
+
total,
|
|
1124
|
+
evaluated,
|
|
1125
|
+
notEvaluated,
|
|
1126
|
+
},
|
|
1127
|
+
};
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
function combineStatuses(statuses) {
|
|
1131
|
+
if (statuses.includes('regression')) return 'regression';
|
|
1132
|
+
if (statuses.includes('improvement')) return 'improvement';
|
|
1133
|
+
if (statuses.includes('unavailable')) return 'unavailable';
|
|
1134
|
+
return 'unchanged';
|
|
1135
|
+
}
|
|
1136
|
+
|
|
1137
|
+
function combineEvaluatedStatuses(statuses) {
|
|
1138
|
+
return combineStatuses(statuses.filter((status) => status !== 'unavailable'));
|
|
1139
|
+
}
|
|
1140
|
+
|
|
1141
|
+
function validateComparisonCompleteness(completeness) {
|
|
1142
|
+
assertMap(completeness, 'comparison.completeness');
|
|
1143
|
+
assertExactKeys(
|
|
1144
|
+
completeness,
|
|
1145
|
+
['status', 'metrics'],
|
|
1146
|
+
'comparison.completeness',
|
|
1147
|
+
);
|
|
1148
|
+
if (!['complete', 'partial', 'not-evaluated'].includes(completeness.status)) {
|
|
1149
|
+
throw new JourneyBaselineError('comparison.completeness.status is invalid');
|
|
1150
|
+
}
|
|
1151
|
+
assertMap(completeness.metrics, 'comparison.completeness.metrics');
|
|
1152
|
+
assertExactKeys(
|
|
1153
|
+
completeness.metrics,
|
|
1154
|
+
['total', 'evaluated', 'notEvaluated'],
|
|
1155
|
+
'comparison.completeness.metrics',
|
|
1156
|
+
);
|
|
1157
|
+
for (const [name, value] of Object.entries(completeness.metrics)) {
|
|
1158
|
+
assertCount(value, `comparison.completeness.metrics.${name}`);
|
|
1159
|
+
}
|
|
1160
|
+
const { total, evaluated, notEvaluated } = completeness.metrics;
|
|
1161
|
+
if (evaluated + notEvaluated !== total) {
|
|
1162
|
+
throw new JourneyBaselineError(
|
|
1163
|
+
'comparison.completeness metric counts are inconsistent',
|
|
1164
|
+
);
|
|
1165
|
+
}
|
|
1166
|
+
const expected =
|
|
1167
|
+
total > 0 && evaluated === 0
|
|
1168
|
+
? 'not-evaluated'
|
|
1169
|
+
: notEvaluated > 0
|
|
1170
|
+
? 'partial'
|
|
1171
|
+
: 'complete';
|
|
1172
|
+
if (
|
|
1173
|
+
completeness.status !== expected &&
|
|
1174
|
+
!(
|
|
1175
|
+
completeness.status === 'not-evaluated' &&
|
|
1176
|
+
total === 0 &&
|
|
1177
|
+
evaluated === 0 &&
|
|
1178
|
+
notEvaluated === 0
|
|
1179
|
+
)
|
|
1180
|
+
) {
|
|
1181
|
+
throw new JourneyBaselineError(
|
|
1182
|
+
'comparison.completeness.status does not match its metric counts',
|
|
1183
|
+
);
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
function comparisonCriticalSchemas(schemas) {
|
|
1188
|
+
const names = [
|
|
1189
|
+
'run',
|
|
1190
|
+
'journey-report',
|
|
1191
|
+
'journey-shard',
|
|
1192
|
+
'observation',
|
|
1193
|
+
'policy',
|
|
1194
|
+
];
|
|
1195
|
+
return Object.fromEntries(
|
|
1196
|
+
names.map((name) => [name, schemas[name] ?? null]),
|
|
1197
|
+
);
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
function statusSummary(statuses) {
|
|
1201
|
+
return {
|
|
1202
|
+
regressions: statuses.filter((status) => status === 'regression').length,
|
|
1203
|
+
improvements: statuses.filter((status) => status === 'improvement').length,
|
|
1204
|
+
unchanged: statuses.filter((status) => status === 'unchanged').length,
|
|
1205
|
+
unavailable: statuses.filter((status) => status === 'unavailable').length,
|
|
1206
|
+
};
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
function emptyComparison({
|
|
1210
|
+
status,
|
|
1211
|
+
reason,
|
|
1212
|
+
baselineHash,
|
|
1213
|
+
compatibilityReasons,
|
|
1214
|
+
}) {
|
|
1215
|
+
return {
|
|
1216
|
+
kind: JOURNEY_COMPARISON_KIND,
|
|
1217
|
+
version: JOURNEY_COMPARISON_VERSION,
|
|
1218
|
+
advisory: true,
|
|
1219
|
+
trust: {
|
|
1220
|
+
policy: status === 'unavailable' ? 'unavailable' : 'trusted-base',
|
|
1221
|
+
execution: 'candidate-untrusted',
|
|
1222
|
+
},
|
|
1223
|
+
status,
|
|
1224
|
+
reason,
|
|
1225
|
+
baselineHash,
|
|
1226
|
+
compatibility: {
|
|
1227
|
+
status: status === 'unavailable' ? 'unavailable' : 'incompatible',
|
|
1228
|
+
reasons: compatibilityReasons,
|
|
1229
|
+
},
|
|
1230
|
+
completeness: {
|
|
1231
|
+
status: 'not-evaluated',
|
|
1232
|
+
metrics: {
|
|
1233
|
+
total: 0,
|
|
1234
|
+
evaluated: 0,
|
|
1235
|
+
notEvaluated: 0,
|
|
1236
|
+
},
|
|
1237
|
+
},
|
|
1238
|
+
summary: {
|
|
1239
|
+
regressions: 0,
|
|
1240
|
+
improvements: 0,
|
|
1241
|
+
unchanged: 0,
|
|
1242
|
+
unavailable: 0,
|
|
1243
|
+
},
|
|
1244
|
+
hardFloors: [],
|
|
1245
|
+
journeys: [],
|
|
1246
|
+
};
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
function snapshotCoverage(report) {
|
|
1250
|
+
const statuses = lifecycleCounts(report.journeys);
|
|
1251
|
+
return {
|
|
1252
|
+
requiredVariants: report.run.completion.expected,
|
|
1253
|
+
measuredVariants: report.run.completion.received,
|
|
1254
|
+
successfulVariants: report.run.completion.successful,
|
|
1255
|
+
statuses,
|
|
1256
|
+
};
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
function snapshotJourneys(report) {
|
|
1260
|
+
return report.journeys
|
|
1261
|
+
.map((journey) => ({
|
|
1262
|
+
journeyRef: journey.id,
|
|
1263
|
+
useCaseId: journey.useCaseId,
|
|
1264
|
+
journeyId: journey.journeyId,
|
|
1265
|
+
status: journey.status,
|
|
1266
|
+
metrics: journey.metrics === null ? null : structuredClone(journey.metrics),
|
|
1267
|
+
findingRefs: [...journey.findingRefs].sort(),
|
|
1268
|
+
}))
|
|
1269
|
+
.sort((left, right) => left.journeyRef.localeCompare(right.journeyRef));
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
function snapshotFindings(report) {
|
|
1273
|
+
return report.findings
|
|
1274
|
+
.map((finding) => ({
|
|
1275
|
+
id: finding.id,
|
|
1276
|
+
ruleId: finding.ruleId,
|
|
1277
|
+
severity: finding.severity,
|
|
1278
|
+
dimension: finding.dimension,
|
|
1279
|
+
type: finding.type,
|
|
1280
|
+
journeyRefs: [...finding.journeyRefs].sort(),
|
|
1281
|
+
}))
|
|
1282
|
+
.sort((left, right) => left.id.localeCompare(right.id));
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
function lifecycleCounts(journeys) {
|
|
1286
|
+
return {
|
|
1287
|
+
successful: journeys.filter(({ status }) => status === 'successful').length,
|
|
1288
|
+
failed: journeys.filter(({ status }) => status === 'failed').length,
|
|
1289
|
+
skipped: journeys.filter(({ status }) => status === 'skipped').length,
|
|
1290
|
+
unexecutable: journeys.filter(({ status }) => status === 'unexecutable').length,
|
|
1291
|
+
infrastructureError: journeys.filter(
|
|
1292
|
+
({ status }) => status === 'infrastructure-error',
|
|
1293
|
+
).length,
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
function canaryCoverage(status) {
|
|
1298
|
+
return {
|
|
1299
|
+
requiredVariants: 1,
|
|
1300
|
+
measuredVariants: 1,
|
|
1301
|
+
successfulVariants: status === 'successful' ? 1 : 0,
|
|
1302
|
+
statuses: {
|
|
1303
|
+
successful: status === 'successful' ? 1 : 0,
|
|
1304
|
+
failed: status === 'failed' ? 1 : 0,
|
|
1305
|
+
skipped: status === 'skipped' ? 1 : 0,
|
|
1306
|
+
unexecutable: status === 'unexecutable' ? 1 : 0,
|
|
1307
|
+
infrastructureError: status === 'infrastructure-error' ? 1 : 0,
|
|
1308
|
+
},
|
|
1309
|
+
};
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
function defaultCanaries() {
|
|
1313
|
+
return {
|
|
1314
|
+
regression: 'not-recorded',
|
|
1315
|
+
improvement: 'not-recorded',
|
|
1316
|
+
compatibility: 'not-recorded',
|
|
1317
|
+
knownBad: 'not-recorded',
|
|
1318
|
+
};
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
function parseRepetitions(value) {
|
|
1322
|
+
if (value === undefined) return 1;
|
|
1323
|
+
if (typeof value !== 'string' || !/^[1-9]\d*$/.test(value)) {
|
|
1324
|
+
throw new JourneyBaselineError('run adapter repetitions must be a positive integer string');
|
|
1325
|
+
}
|
|
1326
|
+
const repetitions = Number(value);
|
|
1327
|
+
if (!Number.isSafeInteger(repetitions)) {
|
|
1328
|
+
throw new JourneyBaselineError('run adapter repetitions is too large');
|
|
1329
|
+
}
|
|
1330
|
+
return repetitions;
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
function validateApproval(approval) {
|
|
1334
|
+
assertMap(approval, 'baseline.approval');
|
|
1335
|
+
assertExactKeys(
|
|
1336
|
+
approval,
|
|
1337
|
+
['status', 'reviewer', 'reviewedAt', 'rationale'],
|
|
1338
|
+
'baseline.approval',
|
|
1339
|
+
);
|
|
1340
|
+
if (!APPROVAL_STATUSES.has(approval.status)) {
|
|
1341
|
+
throw new JourneyBaselineError('baseline.approval.status is invalid');
|
|
1342
|
+
}
|
|
1343
|
+
if (approval.status === 'candidate') {
|
|
1344
|
+
if (
|
|
1345
|
+
approval.reviewer !== null ||
|
|
1346
|
+
approval.reviewedAt !== null ||
|
|
1347
|
+
approval.rationale !== null
|
|
1348
|
+
) {
|
|
1349
|
+
throw new JourneyBaselineError('candidate baseline cannot claim review metadata');
|
|
1350
|
+
}
|
|
1351
|
+
} else {
|
|
1352
|
+
assertText(approval.reviewer, 'baseline.approval.reviewer');
|
|
1353
|
+
assertTimestamp(approval.reviewedAt, 'baseline.approval.reviewedAt');
|
|
1354
|
+
assertText(approval.rationale, 'baseline.approval.rationale');
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
function validateProvenance(provenance) {
|
|
1359
|
+
assertMap(provenance, 'baseline.provenance');
|
|
1360
|
+
assertExactKeys(
|
|
1361
|
+
provenance,
|
|
1362
|
+
['candidateCommit', 'baseCommit', 'sourceReport'],
|
|
1363
|
+
'baseline.provenance',
|
|
1364
|
+
);
|
|
1365
|
+
validateOptionalCommit(provenance.candidateCommit, 'baseline.provenance.candidateCommit');
|
|
1366
|
+
validateOptionalCommit(provenance.baseCommit, 'baseline.provenance.baseCommit');
|
|
1367
|
+
assertMap(provenance.sourceReport, 'baseline.provenance.sourceReport');
|
|
1368
|
+
assertExactKeys(
|
|
1369
|
+
provenance.sourceReport,
|
|
1370
|
+
['path', 'sha256'],
|
|
1371
|
+
'baseline.provenance.sourceReport',
|
|
1372
|
+
);
|
|
1373
|
+
assertSafeRelativePath(
|
|
1374
|
+
provenance.sourceReport.path,
|
|
1375
|
+
'baseline.provenance.sourceReport.path',
|
|
1376
|
+
);
|
|
1377
|
+
assertHash(provenance.sourceReport.sha256, 'baseline.provenance.sourceReport.sha256');
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
function validatePolicy(policy) {
|
|
1381
|
+
assertMap(policy, 'baseline.policy');
|
|
1382
|
+
assertExactKeys(policy, ['algorithm', 'digest'], 'baseline.policy');
|
|
1383
|
+
if (policy.algorithm !== 'sha256' || typeof policy.digest !== 'string' || !DIGEST.test(policy.digest)) {
|
|
1384
|
+
throw new JourneyBaselineError('baseline.policy is invalid');
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
function validateRunProfile(profile) {
|
|
1389
|
+
assertMap(profile, 'baseline.runProfile');
|
|
1390
|
+
assertExactKeys(
|
|
1391
|
+
profile,
|
|
1392
|
+
['browser', 'adapters', 'repetitions'],
|
|
1393
|
+
'baseline.runProfile',
|
|
1394
|
+
);
|
|
1395
|
+
assertMap(profile.browser, 'baseline.runProfile.browser');
|
|
1396
|
+
assertExactKeys(
|
|
1397
|
+
profile.browser,
|
|
1398
|
+
['name', 'profile'],
|
|
1399
|
+
'baseline.runProfile.browser',
|
|
1400
|
+
);
|
|
1401
|
+
assertText(profile.browser.name, 'baseline.runProfile.browser.name');
|
|
1402
|
+
assertText(profile.browser.profile, 'baseline.runProfile.browser.profile');
|
|
1403
|
+
assertStringMap(profile.adapters, 'baseline.runProfile.adapters');
|
|
1404
|
+
if (!Number.isSafeInteger(profile.repetitions) || profile.repetitions < 1) {
|
|
1405
|
+
throw new JourneyBaselineError('baseline.runProfile.repetitions is invalid');
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
|
|
1409
|
+
function validateTool(tool) {
|
|
1410
|
+
assertMap(tool, 'baseline.tool');
|
|
1411
|
+
assertExactKeys(tool, ['name', 'version', 'schemas'], 'baseline.tool');
|
|
1412
|
+
assertText(tool.name, 'baseline.tool.name');
|
|
1413
|
+
assertText(tool.version, 'baseline.tool.version');
|
|
1414
|
+
assertMap(tool.schemas, 'baseline.tool.schemas');
|
|
1415
|
+
if (
|
|
1416
|
+
Object.keys(tool.schemas).length === 0 ||
|
|
1417
|
+
Object.entries(tool.schemas).some(
|
|
1418
|
+
([name, version]) => !ID.test(name) || !Number.isSafeInteger(version) || version < 1,
|
|
1419
|
+
)
|
|
1420
|
+
) {
|
|
1421
|
+
throw new JourneyBaselineError('baseline.tool.schemas is invalid');
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
function validateCoverage(coverage) {
|
|
1426
|
+
assertMap(coverage, 'baseline.coverage');
|
|
1427
|
+
assertExactKeys(
|
|
1428
|
+
coverage,
|
|
1429
|
+
['requiredVariants', 'measuredVariants', 'successfulVariants', 'statuses'],
|
|
1430
|
+
'baseline.coverage',
|
|
1431
|
+
);
|
|
1432
|
+
for (const field of ['requiredVariants', 'measuredVariants', 'successfulVariants']) {
|
|
1433
|
+
assertCount(coverage[field], `baseline.coverage.${field}`);
|
|
1434
|
+
}
|
|
1435
|
+
if (
|
|
1436
|
+
coverage.successfulVariants > coverage.measuredVariants ||
|
|
1437
|
+
coverage.measuredVariants > coverage.requiredVariants
|
|
1438
|
+
) {
|
|
1439
|
+
throw new JourneyBaselineError('baseline.coverage counts are inconsistent');
|
|
1440
|
+
}
|
|
1441
|
+
assertMap(coverage.statuses, 'baseline.coverage.statuses');
|
|
1442
|
+
assertExactKeys(
|
|
1443
|
+
coverage.statuses,
|
|
1444
|
+
['successful', 'failed', 'skipped', 'unexecutable', 'infrastructureError'],
|
|
1445
|
+
'baseline.coverage.statuses',
|
|
1446
|
+
);
|
|
1447
|
+
for (const [field, value] of Object.entries(coverage.statuses)) {
|
|
1448
|
+
assertCount(value, `baseline.coverage.statuses.${field}`);
|
|
1449
|
+
}
|
|
1450
|
+
if (
|
|
1451
|
+
Object.values(coverage.statuses).reduce((total, value) => total + value, 0) !==
|
|
1452
|
+
coverage.measuredVariants
|
|
1453
|
+
) {
|
|
1454
|
+
throw new JourneyBaselineError('baseline.coverage statuses are inconsistent');
|
|
1455
|
+
}
|
|
1456
|
+
}
|
|
1457
|
+
|
|
1458
|
+
function validateJourneys(journeys) {
|
|
1459
|
+
if (!Array.isArray(journeys)) throw new JourneyBaselineError('baseline.journeys must be an array');
|
|
1460
|
+
const ids = new Set();
|
|
1461
|
+
for (const [index, journey] of journeys.entries()) {
|
|
1462
|
+
const path = `baseline.journeys[${index}]`;
|
|
1463
|
+
assertMap(journey, path);
|
|
1464
|
+
assertExactKeys(
|
|
1465
|
+
journey,
|
|
1466
|
+
[
|
|
1467
|
+
'journeyRef',
|
|
1468
|
+
'useCaseId',
|
|
1469
|
+
'journeyId',
|
|
1470
|
+
'status',
|
|
1471
|
+
'metrics',
|
|
1472
|
+
'findingRefs',
|
|
1473
|
+
],
|
|
1474
|
+
path,
|
|
1475
|
+
);
|
|
1476
|
+
assertId(journey.journeyRef, `${path}.journeyRef`);
|
|
1477
|
+
assertId(journey.useCaseId, `${path}.useCaseId`);
|
|
1478
|
+
assertId(journey.journeyId, `${path}.journeyId`);
|
|
1479
|
+
if (ids.has(journey.journeyRef)) {
|
|
1480
|
+
throw new JourneyBaselineError(`baseline.journeys repeats ${journey.journeyRef}`);
|
|
1481
|
+
}
|
|
1482
|
+
ids.add(journey.journeyRef);
|
|
1483
|
+
if (!EXECUTION_STATUSES.has(journey.status)) {
|
|
1484
|
+
throw new JourneyBaselineError(`${path}.status is invalid`);
|
|
1485
|
+
}
|
|
1486
|
+
if (journey.metrics === null) {
|
|
1487
|
+
if (!['failed', 'skipped', 'infrastructure-error'].includes(journey.status)) {
|
|
1488
|
+
throw new JourneyBaselineError(`${path}.metrics cannot be null for measured evidence`);
|
|
1489
|
+
}
|
|
1490
|
+
} else {
|
|
1491
|
+
assertMap(journey.metrics, `${path}.metrics`);
|
|
1492
|
+
for (const [metric, value] of Object.entries(journey.metrics)) {
|
|
1493
|
+
if (!JOURNEY_METRICS.includes(metric)) {
|
|
1494
|
+
throw new JourneyBaselineError(`${path}.metrics has unknown metric ${metric}`);
|
|
1495
|
+
}
|
|
1496
|
+
assertCount(value, `${path}.metrics.${metric}`);
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
if (
|
|
1500
|
+
!Array.isArray(journey.findingRefs) ||
|
|
1501
|
+
new Set(journey.findingRefs).size !== journey.findingRefs.length ||
|
|
1502
|
+
journey.findingRefs.some((id) => typeof id !== 'string' || !ID.test(id))
|
|
1503
|
+
) {
|
|
1504
|
+
throw new JourneyBaselineError(`${path}.findingRefs is invalid`);
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
function validateFindings(findings, journeyIds) {
|
|
1510
|
+
if (!Array.isArray(findings)) throw new JourneyBaselineError('baseline.findings must be an array');
|
|
1511
|
+
const ids = new Set();
|
|
1512
|
+
for (const [index, finding] of findings.entries()) {
|
|
1513
|
+
const path = `baseline.findings[${index}]`;
|
|
1514
|
+
assertMap(finding, path);
|
|
1515
|
+
assertExactKeys(
|
|
1516
|
+
finding,
|
|
1517
|
+
['id', 'ruleId', 'severity', 'dimension', 'type', 'journeyRefs'],
|
|
1518
|
+
path,
|
|
1519
|
+
);
|
|
1520
|
+
assertId(finding.id, `${path}.id`);
|
|
1521
|
+
assertId(finding.ruleId, `${path}.ruleId`);
|
|
1522
|
+
if (ids.has(finding.id)) throw new JourneyBaselineError(`duplicate finding ${finding.id}`);
|
|
1523
|
+
ids.add(finding.id);
|
|
1524
|
+
if (!SEVERITIES.has(finding.severity)) {
|
|
1525
|
+
throw new JourneyBaselineError(`${path}.severity is invalid`);
|
|
1526
|
+
}
|
|
1527
|
+
assertText(finding.dimension, `${path}.dimension`);
|
|
1528
|
+
assertText(finding.type, `${path}.type`);
|
|
1529
|
+
if (
|
|
1530
|
+
!Array.isArray(finding.journeyRefs) ||
|
|
1531
|
+
finding.journeyRefs.length === 0 ||
|
|
1532
|
+
new Set(finding.journeyRefs).size !== finding.journeyRefs.length ||
|
|
1533
|
+
finding.journeyRefs.some((id) => !journeyIds.has(id))
|
|
1534
|
+
) {
|
|
1535
|
+
throw new JourneyBaselineError(`${path}.journeyRefs is invalid`);
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
|
|
1540
|
+
function validateTolerances(tolerances, journeys) {
|
|
1541
|
+
if (!Array.isArray(tolerances)) {
|
|
1542
|
+
throw new JourneyBaselineError('baseline.tolerances must be an array');
|
|
1543
|
+
}
|
|
1544
|
+
const ids = new Set();
|
|
1545
|
+
for (const [index, tolerance] of tolerances.entries()) {
|
|
1546
|
+
const path = `baseline.tolerances[${index}]`;
|
|
1547
|
+
assertMap(tolerance, path);
|
|
1548
|
+
assertExactKeys(
|
|
1549
|
+
tolerance,
|
|
1550
|
+
['journeyRef', 'metric', 'direction', 'permittedRegression'],
|
|
1551
|
+
path,
|
|
1552
|
+
);
|
|
1553
|
+
const journey = journeys.get(tolerance.journeyRef);
|
|
1554
|
+
if (!journey) throw new JourneyBaselineError(`${path}.journeyRef is unknown`);
|
|
1555
|
+
if (
|
|
1556
|
+
!JOURNEY_METRICS.includes(tolerance.metric) ||
|
|
1557
|
+
!journey.metrics ||
|
|
1558
|
+
!Object.hasOwn(journey.metrics, tolerance.metric)
|
|
1559
|
+
) {
|
|
1560
|
+
throw new JourneyBaselineError(`${path}.metric is not measured by its journey`);
|
|
1561
|
+
}
|
|
1562
|
+
if (!['lower-is-better', 'higher-is-better'].includes(tolerance.direction)) {
|
|
1563
|
+
throw new JourneyBaselineError(`${path}.direction is invalid`);
|
|
1564
|
+
}
|
|
1565
|
+
assertCount(tolerance.permittedRegression, `${path}.permittedRegression`);
|
|
1566
|
+
const id = `${tolerance.journeyRef}\0${tolerance.metric}`;
|
|
1567
|
+
if (ids.has(id)) throw new JourneyBaselineError(`${path} duplicates a tolerance`);
|
|
1568
|
+
ids.add(id);
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
function validateHardFloors(floors) {
|
|
1573
|
+
assertMap(floors, 'baseline.hardFloors');
|
|
1574
|
+
assertExactKeys(
|
|
1575
|
+
floors,
|
|
1576
|
+
[
|
|
1577
|
+
'minimumMeasuredVariants',
|
|
1578
|
+
'minimumSuccessfulVariants',
|
|
1579
|
+
'maximumFailedVariants',
|
|
1580
|
+
'maximumSkippedVariants',
|
|
1581
|
+
'maximumInfrastructureErrorVariants',
|
|
1582
|
+
'maximumCriticalFindings',
|
|
1583
|
+
],
|
|
1584
|
+
'baseline.hardFloors',
|
|
1585
|
+
);
|
|
1586
|
+
for (const [name, value] of Object.entries(floors)) {
|
|
1587
|
+
assertCount(value, `baseline.hardFloors.${name}`);
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
function validateArtifacts(artifacts, path, visual = false) {
|
|
1592
|
+
if (!Array.isArray(artifacts)) throw new JourneyBaselineError(`${path} must be an array`);
|
|
1593
|
+
const ids = new Set();
|
|
1594
|
+
const paths = new Set();
|
|
1595
|
+
for (const [index, artifact] of artifacts.entries()) {
|
|
1596
|
+
const itemPath = `${path}[${index}]`;
|
|
1597
|
+
assertMap(artifact, itemPath);
|
|
1598
|
+
assertExactKeys(
|
|
1599
|
+
artifact,
|
|
1600
|
+
visual ? ['id', 'path', 'sha256', 'schemaVersion'] : ['id', 'path', 'sha256'],
|
|
1601
|
+
itemPath,
|
|
1602
|
+
);
|
|
1603
|
+
assertId(artifact.id, `${itemPath}.id`);
|
|
1604
|
+
assertSafeRelativePath(artifact.path, `${itemPath}.path`);
|
|
1605
|
+
assertHash(artifact.sha256, `${itemPath}.sha256`);
|
|
1606
|
+
if (visual) {
|
|
1607
|
+
if (!Number.isSafeInteger(artifact.schemaVersion) || artifact.schemaVersion < 1) {
|
|
1608
|
+
throw new JourneyBaselineError(`${itemPath}.schemaVersion is invalid`);
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
if (ids.has(artifact.id)) throw new JourneyBaselineError(`${path} repeats id ${artifact.id}`);
|
|
1612
|
+
if (paths.has(artifact.path)) {
|
|
1613
|
+
throw new JourneyBaselineError(`${path} repeats path ${artifact.path}`);
|
|
1614
|
+
}
|
|
1615
|
+
ids.add(artifact.id);
|
|
1616
|
+
paths.add(artifact.path);
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
|
|
1620
|
+
function validateReadinessEvidence(evidence) {
|
|
1621
|
+
assertMap(evidence, 'baseline.readinessEvidence');
|
|
1622
|
+
assertExactKeys(
|
|
1623
|
+
evidence,
|
|
1624
|
+
[
|
|
1625
|
+
'deterministicRun',
|
|
1626
|
+
'evidenceComplete',
|
|
1627
|
+
'stableRepetitions',
|
|
1628
|
+
'canaries',
|
|
1629
|
+
'trustedExecution',
|
|
1630
|
+
'enforcementApproved',
|
|
1631
|
+
],
|
|
1632
|
+
'baseline.readinessEvidence',
|
|
1633
|
+
);
|
|
1634
|
+
for (const field of [
|
|
1635
|
+
'deterministicRun',
|
|
1636
|
+
'evidenceComplete',
|
|
1637
|
+
'trustedExecution',
|
|
1638
|
+
'enforcementApproved',
|
|
1639
|
+
]) {
|
|
1640
|
+
if (typeof evidence[field] !== 'boolean') {
|
|
1641
|
+
throw new JourneyBaselineError(`baseline.readinessEvidence.${field} must be boolean`);
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
if (
|
|
1645
|
+
!Number.isSafeInteger(evidence.stableRepetitions) ||
|
|
1646
|
+
evidence.stableRepetitions < 1
|
|
1647
|
+
) {
|
|
1648
|
+
throw new JourneyBaselineError('baseline.readinessEvidence.stableRepetitions is invalid');
|
|
1649
|
+
}
|
|
1650
|
+
validateCanaries(evidence.canaries, 'baseline.readinessEvidence.canaries');
|
|
1651
|
+
}
|
|
1652
|
+
|
|
1653
|
+
function validateCanaries(canaries, path) {
|
|
1654
|
+
assertMap(canaries, path);
|
|
1655
|
+
assertExactKeys(
|
|
1656
|
+
canaries,
|
|
1657
|
+
['regression', 'improvement', 'compatibility', 'knownBad'],
|
|
1658
|
+
path,
|
|
1659
|
+
);
|
|
1660
|
+
for (const [name, status] of Object.entries(canaries)) {
|
|
1661
|
+
if (!CANARY_STATUSES.has(status)) {
|
|
1662
|
+
throw new JourneyBaselineError(`${path}.${name} is invalid`);
|
|
1663
|
+
}
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
function validateJourneyComparisonRecord(journey, path) {
|
|
1668
|
+
assertMap(journey, path);
|
|
1669
|
+
assertExactKeys(
|
|
1670
|
+
journey,
|
|
1671
|
+
['journeyRef', 'status', 'lifecycle', 'findings', 'metrics'],
|
|
1672
|
+
path,
|
|
1673
|
+
);
|
|
1674
|
+
assertId(journey.journeyRef, `${path}.journeyRef`);
|
|
1675
|
+
if (!CHANGE_STATUSES.has(journey.status)) {
|
|
1676
|
+
throw new JourneyBaselineError(`${path}.status is invalid`);
|
|
1677
|
+
}
|
|
1678
|
+
assertMap(journey.lifecycle, `${path}.lifecycle`);
|
|
1679
|
+
assertExactKeys(
|
|
1680
|
+
journey.lifecycle,
|
|
1681
|
+
['status', 'baseline', 'current'],
|
|
1682
|
+
`${path}.lifecycle`,
|
|
1683
|
+
);
|
|
1684
|
+
if (!CHANGE_STATUSES.has(journey.lifecycle.status)) {
|
|
1685
|
+
throw new JourneyBaselineError(`${path}.lifecycle.status is invalid`);
|
|
1686
|
+
}
|
|
1687
|
+
for (const field of ['baseline', 'current']) {
|
|
1688
|
+
if (
|
|
1689
|
+
journey.lifecycle[field] !== 'unavailable' &&
|
|
1690
|
+
!EXECUTION_STATUSES.has(journey.lifecycle[field])
|
|
1691
|
+
) {
|
|
1692
|
+
throw new JourneyBaselineError(`${path}.lifecycle.${field} is invalid`);
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
assertMap(journey.findings, `${path}.findings`);
|
|
1696
|
+
assertExactKeys(
|
|
1697
|
+
journey.findings,
|
|
1698
|
+
['status', 'added', 'removed', 'severityChanges'],
|
|
1699
|
+
`${path}.findings`,
|
|
1700
|
+
);
|
|
1701
|
+
if (!CHANGE_STATUSES.has(journey.findings.status)) {
|
|
1702
|
+
throw new JourneyBaselineError(`${path}.findings.status is invalid`);
|
|
1703
|
+
}
|
|
1704
|
+
for (const field of ['added', 'removed', 'severityChanges']) {
|
|
1705
|
+
if (!Array.isArray(journey.findings[field])) {
|
|
1706
|
+
throw new JourneyBaselineError(`${path}.findings.${field} must be an array`);
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
if (!Array.isArray(journey.metrics)) {
|
|
1710
|
+
throw new JourneyBaselineError(`${path}.metrics must be an array`);
|
|
1711
|
+
}
|
|
1712
|
+
const metrics = new Set();
|
|
1713
|
+
for (const [index, metric] of journey.metrics.entries()) {
|
|
1714
|
+
const metricPath = `${path}.metrics[${index}]`;
|
|
1715
|
+
assertMap(metric, metricPath);
|
|
1716
|
+
assertExactKeys(
|
|
1717
|
+
metric,
|
|
1718
|
+
['metric', 'status', 'baseline', 'current', 'delta', 'tolerance'],
|
|
1719
|
+
metricPath,
|
|
1720
|
+
);
|
|
1721
|
+
if (!JOURNEY_METRICS.includes(metric.metric) || metrics.has(metric.metric)) {
|
|
1722
|
+
throw new JourneyBaselineError(`${metricPath}.metric is invalid`);
|
|
1723
|
+
}
|
|
1724
|
+
metrics.add(metric.metric);
|
|
1725
|
+
if (!CHANGE_STATUSES.has(metric.status)) {
|
|
1726
|
+
throw new JourneyBaselineError(`${metricPath}.status is invalid`);
|
|
1727
|
+
}
|
|
1728
|
+
for (const field of ['baseline', 'current']) {
|
|
1729
|
+
if (metric[field] !== null) assertCount(metric[field], `${metricPath}.${field}`);
|
|
1730
|
+
}
|
|
1731
|
+
if (metric.delta !== null && !Number.isSafeInteger(metric.delta)) {
|
|
1732
|
+
throw new JourneyBaselineError(`${metricPath}.delta is invalid`);
|
|
1733
|
+
}
|
|
1734
|
+
if (metric.tolerance !== null) {
|
|
1735
|
+
assertMap(metric.tolerance, `${metricPath}.tolerance`);
|
|
1736
|
+
assertExactKeys(
|
|
1737
|
+
metric.tolerance,
|
|
1738
|
+
['direction', 'permittedRegression'],
|
|
1739
|
+
`${metricPath}.tolerance`,
|
|
1740
|
+
);
|
|
1741
|
+
if (
|
|
1742
|
+
!['lower-is-better', 'higher-is-better'].includes(metric.tolerance.direction)
|
|
1743
|
+
) {
|
|
1744
|
+
throw new JourneyBaselineError(`${metricPath}.tolerance.direction is invalid`);
|
|
1745
|
+
}
|
|
1746
|
+
assertCount(
|
|
1747
|
+
metric.tolerance.permittedRegression,
|
|
1748
|
+
`${metricPath}.tolerance.permittedRegression`,
|
|
1749
|
+
);
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
function assertMap(value, path) {
|
|
1755
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
1756
|
+
throw new JourneyBaselineError(`${path} must be an object`);
|
|
1757
|
+
}
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
function assertExactKeys(value, expected, path) {
|
|
1761
|
+
const actual = Object.keys(value).sort();
|
|
1762
|
+
const wanted = [...expected].sort();
|
|
1763
|
+
if (
|
|
1764
|
+
actual.length !== wanted.length ||
|
|
1765
|
+
actual.some((key, index) => key !== wanted[index])
|
|
1766
|
+
) {
|
|
1767
|
+
throw new JourneyBaselineError(`${path} must contain ${wanted.join(', ')} only`);
|
|
1768
|
+
}
|
|
1769
|
+
}
|
|
1770
|
+
|
|
1771
|
+
function assertText(value, path) {
|
|
1772
|
+
if (typeof value !== 'string' || !value.trim()) {
|
|
1773
|
+
throw new JourneyBaselineError(`${path} must be non-empty text`);
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
function assertId(value, path) {
|
|
1778
|
+
if (typeof value !== 'string' || !ID.test(value)) {
|
|
1779
|
+
throw new JourneyBaselineError(`${path} is not a safe id`);
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
function assertCount(value, path) {
|
|
1784
|
+
if (!Number.isSafeInteger(value) || value < 0) {
|
|
1785
|
+
throw new JourneyBaselineError(`${path} must be a non-negative safe integer`);
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
|
|
1789
|
+
function assertHash(value, path) {
|
|
1790
|
+
if (typeof value !== 'string' || !SHA256.test(value)) {
|
|
1791
|
+
throw new JourneyBaselineError(`${path} must be a canonical SHA-256 hash`);
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
function assertTimestamp(value, path) {
|
|
1796
|
+
const parsed = typeof value === 'string' ? new Date(value) : null;
|
|
1797
|
+
const normalized =
|
|
1798
|
+
parsed && !Number.isNaN(parsed.valueOf())
|
|
1799
|
+
? parsed.toISOString().replace('.000Z', 'Z')
|
|
1800
|
+
: null;
|
|
1801
|
+
if (
|
|
1802
|
+
typeof value !== 'string' ||
|
|
1803
|
+
!TIMESTAMP.test(value) ||
|
|
1804
|
+
normalized !== value.replace('.000Z', 'Z')
|
|
1805
|
+
) {
|
|
1806
|
+
throw new JourneyBaselineError(`${path} must be an explicit UTC timestamp`);
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
function assertSafeRelativePath(value, path) {
|
|
1811
|
+
if (
|
|
1812
|
+
typeof value !== 'string' ||
|
|
1813
|
+
!value ||
|
|
1814
|
+
value.startsWith('/') ||
|
|
1815
|
+
value.includes('\\') ||
|
|
1816
|
+
value.includes('\0') ||
|
|
1817
|
+
value.split('/').some((part) => !part || part === '.' || part === '..')
|
|
1818
|
+
) {
|
|
1819
|
+
throw new JourneyBaselineError(`${path} must be a safe repository-relative path`);
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
|
|
1823
|
+
function validateOptionalCommit(value, path) {
|
|
1824
|
+
if (value !== null && (typeof value !== 'string' || !COMMIT.test(value))) {
|
|
1825
|
+
throw new JourneyBaselineError(`${path} must be a commit or null`);
|
|
1826
|
+
}
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
function assertStringMap(value, path) {
|
|
1830
|
+
assertMap(value, path);
|
|
1831
|
+
if (
|
|
1832
|
+
Object.keys(value).length === 0 ||
|
|
1833
|
+
Object.entries(value).some(
|
|
1834
|
+
([key, entry]) =>
|
|
1835
|
+
!ID.test(key) || typeof entry !== 'string' || !entry.trim(),
|
|
1836
|
+
)
|
|
1837
|
+
) {
|
|
1838
|
+
throw new JourneyBaselineError(`${path} must be a non-empty version map`);
|
|
1839
|
+
}
|
|
1840
|
+
}
|