( args: MeasurementComparisonInput )
| 126 | * after whitespace collapse. |
| 127 | */ |
| 128 | export function compareMeasurement( |
| 129 | args: MeasurementComparisonInput |
| 130 | ): MeasurementCheck { |
| 131 | const tolerance = |
| 132 | args.spec.toleranceFraction ?? MEASUREMENT_DEFAULT_TOLERANCE; |
| 133 | const measured = args.measured.trim(); |
| 134 | const measuredNumeric = parseFiniteNumber(measured); |
| 135 | const base = { |
| 136 | name: args.spec.name, |
| 137 | command: args.spec.command, |
| 138 | measured, |
| 139 | measuredNumeric, |
| 140 | }; |
| 141 | if (!args.claim) { |
| 142 | return { |
| 143 | ...base, |
| 144 | claim: null, |
| 145 | outcome: "claim-missing", |
| 146 | driftFraction: null, |
| 147 | detail: `worker did not report a claim line named ${JSON.stringify(args.spec.name)}`, |
| 148 | }; |
| 149 | } |
| 150 | const measuredParsed = parseNumericWithUnit(measured); |
| 151 | const claimParsed = parseNumericWithUnit(args.claim.after); |
| 152 | if (measuredParsed && claimParsed) { |
| 153 | if (measuredParsed.unit !== claimParsed.unit) { |
| 154 | const detail = |
| 155 | measuredParsed.unit !== null && claimParsed.unit !== null |
| 156 | ? `unit mismatch; claimed ${claimParsed.unit}, measured ${measuredParsed.unit}` |
| 157 | : `unit inconsistency; claimed ${args.claim.after}, measured ${measured}`; |
| 158 | return { |
| 159 | ...base, |
| 160 | claim: args.claim, |
| 161 | outcome: "value-mismatch", |
| 162 | driftFraction: null, |
| 163 | detail, |
| 164 | }; |
| 165 | } |
| 166 | const denom = Math.max(Math.abs(claimParsed.numeric), 1); |
| 167 | const drift = |
| 168 | Math.abs(measuredParsed.numeric - claimParsed.numeric) / denom; |
| 169 | const outcome: MeasurementCheck["outcome"] = |
| 170 | drift > tolerance ? "value-mismatch" : "match"; |
| 171 | return { |
| 172 | ...base, |
| 173 | claim: args.claim, |
| 174 | outcome, |
| 175 | driftFraction: drift, |
| 176 | detail: |
| 177 | outcome === "match" |
| 178 | ? `numeric within tolerance (drift ${formatFraction(drift)} ≤ ${formatFraction(tolerance)})` |
| 179 | : `numeric drift ${formatFraction(drift)} > tolerance ${formatFraction(tolerance)}; claimed ${args.claim.after}, measured ${measured}`, |
| 180 | }; |
| 181 | } |
| 182 | const matches = |
| 183 | collapseWhitespace(measured) === collapseWhitespace(args.claim.after); |
| 184 | return { |
| 185 | ...base, |
no test coverage detected