|
| 1 | +/** |
| 2 | + * `<Testing>` as an ordinary function component (spec §5.3). |
| 3 | + * |
| 4 | + * It marks a region as a testing boundary: tests inside it run, their results |
| 5 | + * collect here, and the boundary reports how many ran and how many failed. |
| 6 | + * |
| 7 | + * Its body is projected with `tryContent()` rather than `content()` because |
| 8 | + * `<Testing>` does not intercept raised segments — that is `<Test>`'s job — so |
| 9 | + * an error written beside a test settles where it was written and the boundary |
| 10 | + * still reports. `content()` would replace the whole boundary with that failure |
| 11 | + * and lose the report with it. |
| 12 | + */ |
| 13 | + |
| 14 | +import { hasContent, invocation, tryContent } from "@executablemd/core"; |
| 15 | +import type { ComponentInvocationMetadata, Json, PropsSchema } from "@executablemd/core"; |
| 16 | +import type { Operation } from "effection"; |
| 17 | +import { Test, boundary } from "./test-api.ts"; |
| 18 | +import type { TestResult } from "./test-api.ts"; |
| 19 | +import { persistBoundaryOutcome } from "./journal.ts"; |
| 20 | + |
| 21 | +export const TESTING_PROPS: PropsSchema = { |
| 22 | + type: "object", |
| 23 | + properties: {}, |
| 24 | + additionalProperties: false, |
| 25 | +}; |
| 26 | + |
| 27 | +/** `path:line:column`, `line:column`, or `unknown` — the durable boundary key. */ |
| 28 | +function formatLocation(metadata: ComponentInvocationMetadata): string { |
| 29 | + const position = metadata.position; |
| 30 | + if (!position) { |
| 31 | + return "unknown"; |
| 32 | + } |
| 33 | + const at = `${position.line}:${position.column}`; |
| 34 | + return position.path ? `${position.path}:${at}` : at; |
| 35 | +} |
| 36 | + |
| 37 | +export function* Testing(): Operation<Json> { |
| 38 | + const local: TestResult[] = []; |
| 39 | + yield* Test.around( |
| 40 | + { |
| 41 | + testing: () => true, |
| 42 | + // deno-lint-ignore require-yield |
| 43 | + *results() { |
| 44 | + return local; |
| 45 | + }, |
| 46 | + *record([result], next) { |
| 47 | + local.push(result); |
| 48 | + yield* next(result); |
| 49 | + }, |
| 50 | + }, |
| 51 | + { at: "min" }, |
| 52 | + ); |
| 53 | + |
| 54 | + const projected = (yield* hasContent()) ? yield* tryContent() : { text: "" }; |
| 55 | + |
| 56 | + // Journal the outcome before the root Close so a full replay can restore it |
| 57 | + // without re-expanding this boundary. |
| 58 | + const outcome = yield* persistBoundaryOutcome( |
| 59 | + { |
| 60 | + tests: local.length, |
| 61 | + failed: local.filter((result) => result.status === "fail").length, |
| 62 | + }, |
| 63 | + formatLocation(yield* invocation()), |
| 64 | + ); |
| 65 | + yield* boundary(outcome); |
| 66 | + |
| 67 | + // A body that stopped rather than collected is still the caller's failure to |
| 68 | + // settle: the report is what rendered, and the failure travels on. |
| 69 | + if (projected.failure !== undefined) { |
| 70 | + throw projected.failure; |
| 71 | + } |
| 72 | + return projected.text; |
| 73 | +} |
0 commit comments