( returnedData: ExecuteCode2Item[] | null, )
| 11 | import { FunctionMessage } from "../../models"; |
| 12 | |
| 13 | export function checkCodeExecutionOutput( |
| 14 | returnedData: ExecuteCode2Item[] | null, |
| 15 | ): { isValid: true } | { isValid: false; error: string } { |
| 16 | // If data field is null |
| 17 | if (returnedData === null || returnedData.length === 0) { |
| 18 | return { isValid: false, error: "No output data from generated code" }; |
| 19 | } |
| 20 | // If error messages in the data output |
| 21 | const errorMessages = returnedData.filter((m) => m.type === "error"); |
| 22 | if (errorMessages.length > 0) { |
| 23 | return { |
| 24 | isValid: false, |
| 25 | error: errorMessages.map((m) => m.args.message).join("\n"), |
| 26 | }; |
| 27 | } |
| 28 | // If there are log messages starting with the word Error or ERROR |
| 29 | const logMessages = returnedData.filter((m) => m.type === "log"); |
| 30 | if ( |
| 31 | logMessages.some( |
| 32 | (m) => "message" in m.args && m.args.message.match(/^E(rror|RROR)/), |
| 33 | ) |
| 34 | ) { |
| 35 | return { |
| 36 | isValid: false, |
| 37 | error: logMessages.map((m) => m.args.message).join("\n"), |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | // If 1 plot with missing data |
| 42 | const plotMessages = returnedData.filter((m) => m.type === "plot"); |
| 43 | if (plotMessages.length === 1) { |
| 44 | const plotArgs = plotMessages[0].args as BertieGraphData; |
| 45 | const isValue = plotArgs.data.length === 1; |
| 46 | const isTable = plotArgs.type === "table"; |
| 47 | if ( |
| 48 | // No data (exception is if it's a table or value) |
| 49 | !isTable && |
| 50 | !isValue && |
| 51 | // Every data point has only 1 key |
| 52 | // @ts-ignore |
| 53 | plotArgs.data.every((d) => Object.keys(d).length <= 1) |
| 54 | ) { |
| 55 | return { |
| 56 | isValid: false, |
| 57 | error: `Missing columns in data output by code: ${plotMessages |
| 58 | // @ts-ignore |
| 59 | .map((m) => m.args.message) |
| 60 | .join("\n")}`, |
| 61 | }; |
| 62 | } |
| 63 | const minNonNullsAllowed = isValue || isTable ? 1 : 2; |
| 64 | if ( |
| 65 | // @ts-ignore |
| 66 | plotArgs.data.every( |
| 67 | // @ts-ignore |
| 68 | (d) => |
| 69 | Object.values(d).filter( |
| 70 | // @ts-ignore |
no test coverage detected