( assertion: AssertionParams['assertion'], result: ScriptAssertionResult, inverse: boolean, labels: ScriptLabels, reasonSuffix?: AssertionParams['assertion']['value'], )
| 113 | * keys, threshold comparison, and inverse logic. |
| 114 | */ |
| 115 | export function normalizeScriptResult( |
| 116 | assertion: AssertionParams['assertion'], |
| 117 | result: ScriptAssertionResult, |
| 118 | inverse: boolean, |
| 119 | labels: ScriptLabels, |
| 120 | reasonSuffix?: AssertionParams['assertion']['value'], |
| 121 | ): GradingResult { |
| 122 | const lowerStringResult = typeof result === 'string' ? result.toLowerCase() : undefined; |
| 123 | |
| 124 | if ((typeof result === 'boolean' && result) || lowerStringResult === 'true') { |
| 125 | return normalizeScriptAssertionResult(assertion, true, inverse, labels, reasonSuffix); |
| 126 | } |
| 127 | |
| 128 | if (typeof result === 'boolean' || lowerStringResult === 'false') { |
| 129 | return normalizeScriptAssertionResult(assertion, false, inverse, labels, reasonSuffix); |
| 130 | } |
| 131 | |
| 132 | if (typeof result === 'string' && result.startsWith('{')) { |
| 133 | let parsed: unknown; |
| 134 | try { |
| 135 | parsed = JSON.parse(result); |
| 136 | } catch (err) { |
| 137 | throw new Error(`Invalid JSON: ${err} when parsing result: ${result}`); |
| 138 | } |
| 139 | |
| 140 | if (typeof parsed !== 'object' || parsed === null) { |
| 141 | throw new Error( |
| 142 | `${labels.language} assertion must return a boolean, number, or {pass, score, reason} object. Got instead: ${result}`, |
| 143 | ); |
| 144 | } |
| 145 | return normalizeScriptObjectResult(assertion, parsed, inverse, labels, reasonSuffix); |
| 146 | } |
| 147 | |
| 148 | if (typeof result === 'object' && result !== null) { |
| 149 | return normalizeScriptObjectResult(assertion, result, inverse, labels, reasonSuffix); |
| 150 | } |
| 151 | |
| 152 | const score = Number.parseFloat(String(result)); |
| 153 | if (Number.isNaN(score)) { |
| 154 | throw new Error( |
| 155 | `${labels.language} assertion must return a boolean, number, or {pass, score, reason} object. Instead got:\n${result}`, |
| 156 | ); |
| 157 | } |
| 158 | return normalizeScriptAssertionResult(assertion, score, inverse, labels, reasonSuffix); |
| 159 | } |
no test coverage detected
searching dependent graphs…