|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +/** |
| 5 | + * Expected-failure matcher. |
| 6 | + * |
| 7 | + * Negative scenarios declare an `expected_failure` contract on their |
| 8 | + * expected state. The runner captures the failed setup's log plus a small |
| 9 | + * side-effect inventory (sandbox-created, gateway-started, credentials-written) |
| 10 | + * and asks this module whether the observation matches the contract. |
| 11 | + * |
| 12 | + * The contract has four parts: |
| 13 | + * - phase: which setup stage produced the failure (informational; the |
| 14 | + * runner is responsible for invoking the matcher only when that phase |
| 15 | + * actually ran). |
| 16 | + * - error_class: stable identifier for the failure mode. |
| 17 | + * - message_pattern: regex applied to the captured log when present. |
| 18 | + * - forbidden_side_effects: effects that MUST NOT be observed. |
| 19 | + * |
| 20 | + * Match result is structured (`ExpectedFailureReport`) so the runner can |
| 21 | + * write `expected-vs-actual.json` and surface a useful diff in CI. |
| 22 | + */ |
| 23 | + |
| 24 | +import { compileMessagePattern } from "./load.ts"; |
| 25 | +import type { |
| 26 | + ExpectedFailure, |
| 27 | + ExpectedFailurePhase, |
| 28 | + ExpectedFailureErrorClass, |
| 29 | + ExpectedFailureSideEffect, |
| 30 | +} from "./schema.ts"; |
| 31 | + |
| 32 | +export interface ObservedFailure { |
| 33 | + /** Phase the runner attempted; matched against `expected_failure.phase`. */ |
| 34 | + phase: ExpectedFailurePhase; |
| 35 | + /** |
| 36 | + * Structured reason if the runner could derive one (preferred). When |
| 37 | + * absent, matching falls back to log-content heuristics in the runner. |
| 38 | + */ |
| 39 | + error_class?: ExpectedFailureErrorClass; |
| 40 | + /** Captured setup log; matched against `expected_failure.message_pattern`. */ |
| 41 | + log: string; |
| 42 | + /** |
| 43 | + * Side effects the runner positively observed after the failure. Each |
| 44 | + * effect in `expected_failure.forbidden_side_effects` is checked against |
| 45 | + * this set; presence is a failure. |
| 46 | + */ |
| 47 | + observed_side_effects: ExpectedFailureSideEffect[]; |
| 48 | +} |
| 49 | + |
| 50 | +export interface ExpectedFailureCheck { |
| 51 | + name: "phase" | "error_class" | "message_pattern" | "forbidden_side_effects"; |
| 52 | + ok: boolean; |
| 53 | + expected: string; |
| 54 | + actual: string; |
| 55 | + message?: string; |
| 56 | +} |
| 57 | + |
| 58 | +export interface ExpectedFailureReport { |
| 59 | + ok: boolean; |
| 60 | + expected: ExpectedFailure; |
| 61 | + observed: ObservedFailure; |
| 62 | + checks: ExpectedFailureCheck[]; |
| 63 | +} |
| 64 | + |
| 65 | +export function matchExpectedFailure( |
| 66 | + expected: ExpectedFailure, |
| 67 | + observed: ObservedFailure, |
| 68 | +): ExpectedFailureReport { |
| 69 | + const checks: ExpectedFailureCheck[] = []; |
| 70 | + |
| 71 | + const phaseOk = expected.phase === observed.phase; |
| 72 | + checks.push({ |
| 73 | + name: "phase", |
| 74 | + ok: phaseOk, |
| 75 | + expected: expected.phase, |
| 76 | + actual: observed.phase, |
| 77 | + message: phaseOk |
| 78 | + ? undefined |
| 79 | + : `phase mismatch: expected '${expected.phase}' but observed '${observed.phase}'`, |
| 80 | + }); |
| 81 | + |
| 82 | + if (observed.error_class !== undefined) { |
| 83 | + const classOk = expected.error_class === observed.error_class; |
| 84 | + checks.push({ |
| 85 | + name: "error_class", |
| 86 | + ok: classOk, |
| 87 | + expected: expected.error_class, |
| 88 | + actual: observed.error_class, |
| 89 | + message: classOk |
| 90 | + ? undefined |
| 91 | + : `error_class mismatch: expected '${expected.error_class}' but observed '${observed.error_class}'`, |
| 92 | + }); |
| 93 | + } else { |
| 94 | + // No structured class from the runner; defer to message_pattern as |
| 95 | + // the discriminator. Record a SKIPPED entry so the report makes it |
| 96 | + // obvious that the class was not asserted structurally. |
| 97 | + checks.push({ |
| 98 | + name: "error_class", |
| 99 | + ok: true, |
| 100 | + expected: expected.error_class, |
| 101 | + actual: "<unobserved>", |
| 102 | + message: "skipped: runner did not derive a structured error_class", |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + if (expected.message_pattern) { |
| 107 | + let regex: RegExp; |
| 108 | + try { |
| 109 | + regex = compileMessagePattern(expected.message_pattern); |
| 110 | + } catch (err) { |
| 111 | + checks.push({ |
| 112 | + name: "message_pattern", |
| 113 | + ok: false, |
| 114 | + expected: expected.message_pattern, |
| 115 | + actual: "<invalid regex>", |
| 116 | + message: `message_pattern is not a valid regex: ${(err as Error).message}`, |
| 117 | + }); |
| 118 | + return finalize(expected, observed, checks); |
| 119 | + } |
| 120 | + const ok = regex.test(observed.log); |
| 121 | + checks.push({ |
| 122 | + name: "message_pattern", |
| 123 | + ok, |
| 124 | + expected: expected.message_pattern, |
| 125 | + actual: ok ? "<match>" : "<no match>", |
| 126 | + message: ok |
| 127 | + ? undefined |
| 128 | + : `message_pattern '${expected.message_pattern}' did not match captured log`, |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + if (expected.forbidden_side_effects?.length) { |
| 133 | + const observedSet = new Set(observed.observed_side_effects); |
| 134 | + const found = expected.forbidden_side_effects.filter((e) => observedSet.has(e)); |
| 135 | + const ok = found.length === 0; |
| 136 | + checks.push({ |
| 137 | + name: "forbidden_side_effects", |
| 138 | + ok, |
| 139 | + expected: expected.forbidden_side_effects.join(","), |
| 140 | + actual: observed.observed_side_effects.join(",") || "<none>", |
| 141 | + message: ok |
| 142 | + ? undefined |
| 143 | + : `forbidden side effects observed after failure: ${found.join(", ")}`, |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + return finalize(expected, observed, checks); |
| 148 | +} |
| 149 | + |
| 150 | +function finalize( |
| 151 | + expected: ExpectedFailure, |
| 152 | + observed: ObservedFailure, |
| 153 | + checks: ExpectedFailureCheck[], |
| 154 | +): ExpectedFailureReport { |
| 155 | + return { ok: checks.every((c) => c.ok), expected, observed, checks }; |
| 156 | +} |
| 157 | + |
| 158 | +export function formatExpectedFailureReport(report: ExpectedFailureReport): string { |
| 159 | + const lines: string[] = []; |
| 160 | + lines.push(`expected-failure: ${report.ok ? "OK" : "FAILED"}`); |
| 161 | + for (const c of report.checks) { |
| 162 | + const status = c.ok ? "PASS" : "FAIL"; |
| 163 | + lines.push(` ${status} ${c.name} expected=${c.expected} actual=${c.actual}`); |
| 164 | + if (c.message) lines.push(` ${c.message}`); |
| 165 | + } |
| 166 | + return lines.join("\n"); |
| 167 | +} |
0 commit comments