Files
rea/scripts/verify/clients/result-discovery.mjs
T

39 lines
1.3 KiB
JavaScript

/** Find a result through native JSON containers without hiding assertion faults. */
export const findClientResult = (value, matches, decodeText = JSON.parse) => {
if (typeof value === "string") {
let decoded;
try {
decoded = decodeText(value);
} catch (cause) {
if (cause instanceof SyntaxError) return undefined;
throw cause;
}
return findClientResult(decoded, matches, decodeText);
}
if (value !== null && typeof value === "object") {
if (matches(value)) return value;
for (const nested of Object.values(value)) {
const result = findClientResult(nested, matches, decodeText);
if (result !== undefined) return result;
}
}
return undefined;
};
/** Empty graphs remain candidates: the assertion owner must reject bad fixtures. */
export const isClientJavaScriptEvidence = (value) =>
value !== null &&
typeof value === "object" &&
typeof value.evidence_id === "string" &&
Array.isArray(value.normalized_result?.graph?.nodes);
/** Search string values in a native client message or reply. */
export const containsText = (value, text) => {
if (typeof value === "string") return value.includes(text);
return (
value !== null &&
typeof value === "object" &&
Object.values(value).some((item) => containsText(item, text))
);
};