From d3d02138126bff0bed929a7ac5b0f7635dfa3aa1 Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Mon, 16 Nov 2026 21:37:15 -0500 Subject: [PATCH 01/11] Expose the shared test journal resource --- deno.json | 1 + packages/acp/tests/acceptance.test.ts | 362 ++++---- packages/agent/tests/agent-components.test.ts | 285 +++--- packages/cli/src/test-diagnostics.ts | 56 +- packages/cli/tests/repl-execution.test.ts | 629 +++++++------ packages/cli/tests/repl-model.test.ts | 22 +- ...ained-repository-observations-deno.test.ts | 154 ++-- .../retained-repository-observations.test.ts | 125 +-- packages/cli/tests/run-composition.test.ts | 37 +- .../cli/tests/support/repl-observations.ts | 10 + .../tests/component-invocation-hooks.test.ts | 280 +++--- packages/core/tests/each.test.ts | 162 ++-- packages/core/tests/ephemeral-service.test.ts | 46 +- packages/core/tests/eval-bindings.test.ts | 58 +- packages/core/tests/eval-error-mode.test.ts | 627 ++++++------- packages/core/tests/eval-return.test.ts | 24 +- packages/core/tests/eval-scope.test.ts | 12 +- packages/core/tests/eval-timeout.test.ts | 121 +-- packages/core/tests/execute.test.ts | 453 +++++----- .../core/tests/fixtures/held-host-journal.ts | 126 +++ .../core/tests/generated-xmd-root.test.ts | 110 +-- .../core/tests/host-generated-root.test.ts | 624 ++++++++----- packages/core/tests/if.test.ts | 110 ++- packages/core/tests/let.test.ts | 156 ++-- packages/core/tests/loop.test.ts | 94 +- packages/core/tests/sample-component.test.ts | 273 +++--- packages/core/tests/source-position.test.ts | 176 ++-- .../core/tests/streaming-emission.test.ts | 149 +++- packages/core/tests/support/held-host.ts | 16 +- packages/core/tests/switch.test.ts | 99 ++- .../core/tests/validation-integration.test.ts | 150 ++-- .../git/tests/git-observations-deno.test.ts | 208 ++--- packages/git/tests/git-observations.test.ts | 834 +++++++++--------- packages/git/tests/issue-markdown.test.ts | 25 +- .../git/tests/run-composition-ambient.test.ts | 615 +++++++------ .../git/tests/run-composition-managed.test.ts | 460 +++++++--- .../git/tests/run-composition-remote.test.ts | 150 ++-- packages/git/tests/support/issue-scenario.ts | 15 +- .../tests/support/run-composition-child.ts | 21 +- packages/git/tests/support/run-composition.ts | 26 +- .../test-agent/tests/command-lazy.test.ts | 169 ++-- .../tests/session-configuration.test.ts | 270 +++--- packages/test-support/bdd.ts | 78 +- packages/test-support/case-outcome.ts | 14 + packages/test-support/failure-journal.ts | 119 +++ packages/test-support/journal-cases.ts | 15 + packages/test-support/package.json | 2 + .../testing/tests/agent-composition.test.ts | 45 +- packages/testing/tests/assert-throws.test.ts | 141 +-- packages/testing/tests/assertions.test.ts | 263 +++--- .../testing/tests/boundary-contract.test.ts | 177 ++-- .../testing/tests/execution-harness.test.ts | 156 ++-- packages/testing/tests/helpers.ts | 23 + packages/testing/tests/test-component.test.ts | 250 +++--- .../testing/tests/testing-boundary.test.ts | 45 +- packages/testing/tests/testing-mode.test.ts | 251 +++--- packages/testing/tests/use-testing.test.ts | 227 +++-- packages/web/tests/component-support.ts | 16 + packages/web/tests/component.test.ts | 235 +++-- packages/web/tests/document.test.ts | 95 +- .../tests/generated-observations.test.ts | 327 ++++--- pnpm-lock.yaml | 3 + tsconfig.node.json | 1 + 63 files changed, 6236 insertions(+), 4587 deletions(-) create mode 100644 packages/core/tests/fixtures/held-host-journal.ts create mode 100644 packages/test-support/case-outcome.ts create mode 100644 packages/test-support/failure-journal.ts create mode 100644 packages/test-support/journal-cases.ts diff --git a/deno.json b/deno.json index 2ed68f451..aad3a4480 100644 --- a/deno.json +++ b/deno.json @@ -20,6 +20,7 @@ "@durable-streams/server": "npm:@durable-streams/server@^0.3.8", "@executablemd/test-support/bdd": "./packages/test-support/bdd.ts", "@executablemd/test-support/expect": "./packages/test-support/expect.ts", + "@executablemd/test-support/failure-journal": "./packages/test-support/failure-journal.ts", "@executablemd/test-support/journal": "./packages/test-support/journal.ts", "@executablemd/test-support/launch": "./packages/test-support/launch.ts", "@effectionx/test-adapter": "npm:@effectionx/test-adapter@0.7.4", diff --git a/packages/acp/tests/acceptance.test.ts b/packages/acp/tests/acceptance.test.ts index 2350ed8b6..548d51f38 100644 --- a/packages/acp/tests/acceptance.test.ts +++ b/packages/acp/tests/acceptance.test.ts @@ -14,10 +14,11 @@ import { Agent } from "@executablemd/agent/api"; import { agentIdentityComponents, installAgentComponents } from "@executablemd/agent/host"; import { executeInstalled } from "@executablemd/core/host"; import type { AgentSessionRequest } from "@executablemd/agent/api"; -import { InMemoryStream } from "@executablemd/durable-streams"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; import { createAcpxProvider } from "../mod.ts"; import { createFakeRuntime, makeRegistry, makeStore, useFlatWorld } from "./helpers.ts"; +const OWNER = "packages/acp/tests/acceptance.test.ts"; const CWD = "/work"; const DOC = [ '', @@ -29,7 +30,8 @@ const DOC = [ ].join("\n"); describe("Tier XA — ACPX provider through the rootProvider seam", () => { - it("XA1: agent availability → session → normalized prompt → structured teardown", function* () { + const lifecycle = "XA1: agent availability → session → normalized prompt → structured teardown"; + it(lifecycle, function* () { const harness = createFakeRuntime(); const dir = path.join(os.tmpdir(), `xmd-acp-accept-${randomUUID()}`); yield* ensureDir(dir); @@ -38,45 +40,47 @@ describe("Tier XA — ACPX provider through the rootProvider seam", () => { const docPath = path.join(dir, "doc.md"); yield* writeTextFile(docPath, DOC); - const result = yield* scoped(function* () { - yield* useFlatWorld(CWD); - yield* installAgentComponents({ - rootProvider: { - factory: createAcpxProvider({ - createRuntime: harness.create, - sessionStore: makeStore(), - agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), - }), - options: { defaultAgent: "gemini", permissionMode: "deny-all" }, - }, + yield* captureTestJournal({ owner: OWNER, name: lifecycle }, docPath, function* (stream) { + const result = yield* scoped(function* () { + yield* useFlatWorld(CWD); + yield* installAgentComponents({ + rootProvider: { + factory: createAcpxProvider({ + createRuntime: harness.create, + sessionStore: makeStore(), + agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), + }), + options: { defaultAgent: "gemini", permissionMode: "deny-all" }, + }, + }); + + // `` names durable work after its own invocation, so the host + // declares it to the execution rather than registering it. + const execution = yield* executeInstalled({ path: docPath, stream }, [ + { components: agentIdentityComponents() }, + ]); + const subscription = yield* execution.output; + let next = yield* subscription.next(); + while (!next.done) { + next = yield* subscription.next(); + } + const output = next.value; + const outcome = yield* execution; + // Structured teardown: the handle is closed by the time the + // DocumentExecution completion settles (bridgeRootProvider resolves + // completion only after provider finalizers run). + const closedByCompletion = harness.closeCalls.length; + return { output, outcome, closedByCompletion }; }); - // `` names durable work after its own invocation, so the host - // declares it to the execution rather than registering it. - const execution = yield* executeInstalled({ path: docPath, stream: new InMemoryStream() }, [ - { components: agentIdentityComponents() }, - ]); - const subscription = yield* execution.output; - let next = yield* subscription.next(); - while (!next.done) { - next = yield* subscription.next(); - } - const output = next.value; - const outcome = yield* execution; - // Structured teardown: the handle is closed by the time the - // DocumentExecution completion settles (bridgeRootProvider resolves - // completion only after provider finalizers run). - const closedByCompletion = harness.closeCalls.length; - return { output, outcome, closedByCompletion }; + expect(harness.doctorCalls).toBeGreaterThan(0); + expect(harness.ensureCalls.length).toBeGreaterThan(0); + // Normalized prompt output — output-stream deltas only (thought hidden). + expect(result.output).toContain("hello world"); + expect(result.outcome.ok).toBe(true); + // Handle closed before completion settled. + expect(result.closedByCompletion).toBeGreaterThan(0); }); - - expect(harness.doctorCalls).toBeGreaterThan(0); - expect(harness.ensureCalls.length).toBeGreaterThan(0); - // Normalized prompt output — output-stream deltas only (thought hidden). - expect(result.output).toContain("hello world"); - expect(result.outcome.ok).toBe(true); - // Handle closed before completion settled. - expect(result.closedByCompletion).toBeGreaterThan(0); }); }); @@ -89,7 +93,9 @@ describe("Tier XA — ACPX provider through the rootProvider seam", () => { * through the coordinator delivered to this provider. */ describe("Tier WAP — same-named sibling Sessions", () => { - it('WAP7: two sibling sites name two identities the middleware cannot touch', function* () { + const siblings = + 'WAP7: two sibling sites name two identities the middleware cannot touch'; + it(siblings, function* () { const harness = createFakeRuntime(); const requests: Array<{ session: string | undefined; sessionIdentity?: string }> = []; /** The session keys this host was asked to retain, in order. */ @@ -115,86 +121,90 @@ describe("Tier WAP — same-named sibling Sessions", () => { ].join("\n"), ); - yield* scoped(function* () { - yield* useFlatWorld(CWD); - yield* installAgentComponents({ - rootProvider: { - factory: createAcpxProvider({ - createRuntime: harness.create, - sessionStore: makeStore(), - agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), - sessions: { - // deno-lint-ignore require-yield - *requestSession(request) { - requests.push({ - session: request.session, - ...(request.sessionIdentity === undefined - ? {} - : { sessionIdentity: request.sessionIdentity }), - }); - return { - sessionKey: `session:${request.sessionIdentity ?? "none"}`, - cwd: "/sessions", - state: "new", - }; - }, - // A host that decides where sessions live retains what stands - // there. This one keeps the keys it was asked to retain, in - // order, which is how the test sees that each request became its - // own confirmed session rather than one shared by the name. - // deno-lint-ignore require-yield - *recordSessionIdentity(details) { - recorded.push(details.sessionKey); + yield* captureTestJournal({ owner: OWNER, name: siblings }, docPath, function* (stream) { + yield* scoped(function* () { + yield* useFlatWorld(CWD); + yield* installAgentComponents({ + rootProvider: { + factory: createAcpxProvider({ + createRuntime: harness.create, + sessionStore: makeStore(), + agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), + sessions: { + // deno-lint-ignore require-yield + *requestSession(request) { + requests.push({ + session: request.session, + ...(request.sessionIdentity === undefined + ? {} + : { sessionIdentity: request.sessionIdentity }), + }); + return { + sessionKey: `session:${request.sessionIdentity ?? "none"}`, + cwd: "/sessions", + state: "new", + }; + }, + // A host that decides where sessions live retains what stands + // there. This one keeps the keys it was asked to retain, in + // order, which is how the test sees that each request became its + // own confirmed session rather than one shared by the name. + // deno-lint-ignore require-yield + *recordSessionIdentity(details) { + recorded.push(details.sessionKey); + }, }, - }, - }), - options: { defaultAgent: "gemini", permissionMode: "deny-all" }, - }, - }); - // Middleware that rewrites the descriptive name on its way through, which - // is exactly what the public chain is for. - yield* Agent.around({ - *session([routed], next) { - const renamed = - typeof routed === "string" || routed === undefined - ? "rewritten" - : routed.with({ name: "rewritten" }); - return yield* next(renamed); - }, + }), + options: { defaultAgent: "gemini", permissionMode: "deny-all" }, + }, + }); + // Middleware that rewrites the descriptive name on its way through, which + // is exactly what the public chain is for. + yield* Agent.around({ + *session([routed], next) { + const renamed = + typeof routed === "string" || routed === undefined + ? "rewritten" + : routed.with({ name: "rewritten" }); + return yield* next(renamed); + }, + }); + + // `` names durable work after its own invocation, so the host + // declares it to the execution rather than registering it. + const execution = yield* executeInstalled({ path: docPath, stream }, [ + { components: agentIdentityComponents() }, + ]); + const subscription = yield* execution.output; + let next = yield* subscription.next(); + while (!next.done) { + next = yield* subscription.next(); + } + yield* execution; }); - // `` names durable work after its own invocation, so the host - // declares it to the execution rather than registering it. - const execution = yield* executeInstalled({ path: docPath, stream: new InMemoryStream() }, [ - { components: agentIdentityComponents() }, - ]); - const subscription = yield* execution.output; - let next = yield* subscription.next(); - while (!next.done) { - next = yield* subscription.next(); - } - yield* execution; + expect(requests).toHaveLength(2); + // The middleware's rewrite reached the provider, because the name is the + // compositional half. + expect(requests.map((request) => request.session)).toEqual(["rewritten", "rewritten"]); + // The identities did not, and are two: same authored name, two elements, + // two sessions. + const identities = requests.map((request) => request.sessionIdentity); + expect(identities.every((identity) => typeof identity === "string")).toBe(true); + expect(new Set(identities).size).toBe(2); + expect(identities).not.toContain("rewritten"); + // And each request is its own session, not one shared by the name. + expect(new Set(harness.ensureCalls.map((call) => call.sessionKey)).size).toBe(2); + // The host was asked to retain both, in the order they were confirmed. A + // provider that skipped a host with nothing to skip would show up here as + // an empty list. + expect(recorded).toEqual(requests.map((request) => `session:${request.sessionIdentity}`)); }); - - expect(requests).toHaveLength(2); - // The middleware's rewrite reached the provider, because the name is the - // compositional half. - expect(requests.map((request) => request.session)).toEqual(["rewritten", "rewritten"]); - // The identities did not, and are two: same authored name, two elements, - // two sessions. - const identities = requests.map((request) => request.sessionIdentity); - expect(identities.every((identity) => typeof identity === "string")).toBe(true); - expect(new Set(identities).size).toBe(2); - expect(identities).not.toContain("rewritten"); - // And each request is its own session, not one shared by the name. - expect(new Set(harness.ensureCalls.map((call) => call.sessionKey)).size).toBe(2); - // The host was asked to retain both, in the order they were confirmed. A - // provider that skipped a host with nothing to skip would show up here as - // an empty list. - expect(recorded).toEqual(requests.map((request) => `session:${request.sessionIdentity}`)); }); - it("WAP7: a request kept from the first Session cannot be routed for the second", function* () { + const retainedRequest = + "WAP7: a request kept from the first Session cannot be routed for the second"; + it(retainedRequest, function* () { const harness = createFakeRuntime(); const requests: Array = []; /** The session keys this host was asked to retain, in order. */ @@ -221,72 +231,74 @@ describe("Tier WAP — same-named sibling Sessions", () => { ].join("\n"), ); - yield* scoped(function* () { - yield* useFlatWorld(CWD); - yield* installAgentComponents({ - rootProvider: { - factory: createAcpxProvider({ - createRuntime: harness.create, - sessionStore: makeStore(), - agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), - sessions: { - // deno-lint-ignore require-yield - *requestSession(request) { - requests.push(request.sessionIdentity); - return { - sessionKey: `session:${request.sessionIdentity ?? "none"}`, - cwd: "/sessions", - state: "new", - }; - }, - // deno-lint-ignore require-yield - *recordSessionIdentity(details) { - recorded.push(details.sessionKey); + yield* captureTestJournal({ owner: OWNER, name: retainedRequest }, docPath, function* (stream) { + yield* scoped(function* () { + yield* useFlatWorld(CWD); + yield* installAgentComponents({ + rootProvider: { + factory: createAcpxProvider({ + createRuntime: harness.create, + sessionStore: makeStore(), + agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), + sessions: { + // deno-lint-ignore require-yield + *requestSession(request) { + requests.push(request.sessionIdentity); + return { + sessionKey: `session:${request.sessionIdentity ?? "none"}`, + cwd: "/sessions", + state: "new", + }; + }, + // deno-lint-ignore require-yield + *recordSessionIdentity(details) { + recorded.push(details.sessionKey); + }, }, - }, - }), - options: { defaultAgent: "gemini", permissionMode: "deny-all" }, - }, - }); + }), + options: { defaultAgent: "gemini", permissionMode: "deny-all" }, + }, + }); - // The attack: keep the first element's real request and route it for - // the second. Reading it again would answer with the first element's - // identity, and both sessions would collapse into one. - let kept: string | AgentSessionRequest | undefined; - let keeping = false; - yield* Agent.around({ - *session([routed], next) { - if (!keeping) { - keeping = true; - kept = routed; - return yield* next(routed); - } - return yield* next(kept); - }, + // The attack: keep the first element's real request and route it for + // the second. Reading it again would answer with the first element's + // identity, and both sessions would collapse into one. + let kept: string | AgentSessionRequest | undefined; + let keeping = false; + yield* Agent.around({ + *session([routed], next) { + if (!keeping) { + keeping = true; + kept = routed; + return yield* next(routed); + } + return yield* next(kept); + }, + }); + + // `` names durable work after its own invocation, so the host + // declares it to the execution rather than registering it. + const execution = yield* executeInstalled({ path: docPath, stream }, [ + { components: agentIdentityComponents() }, + ]); + const subscription = yield* execution.output; + let next = yield* subscription.next(); + while (!next.done) { + next = yield* subscription.next(); + } + const outcome = yield* execution; + if (!outcome.ok) { + refusal = outcome.error.message; + } }); - // `` names durable work after its own invocation, so the host - // declares it to the execution rather than registering it. - const execution = yield* executeInstalled({ path: docPath, stream: new InMemoryStream() }, [ - { components: agentIdentityComponents() }, - ]); - const subscription = yield* execution.output; - let next = yield* subscription.next(); - while (!next.done) { - next = yield* subscription.next(); - } - const outcome = yield* execution; - if (!outcome.ok) { - refusal = outcome.error.message; - } + // Refused before the provider named anything for the second element: the + // issuance the first one opened closed when that element finished. + expect(refusal).toContain("already resolved its session"); + expect(requests).toHaveLength(1); + // One session was confirmed, so one identity was retained — the refused + // element contributed no mapping of its own. + expect(recorded).toEqual([`session:${requests[0]}`]); }); - - // Refused before the provider named anything for the second element: the - // issuance the first one opened closed when that element finished. - expect(refusal).toContain("already resolved its session"); - expect(requests).toHaveLength(1); - // One session was confirmed, so one identity was retained — the refused - // element contributed no mapping of its own. - expect(recorded).toEqual([`session:${requests[0]}`]); }); }); diff --git a/packages/agent/tests/agent-components.test.ts b/packages/agent/tests/agent-components.test.ts index b6a6eb06c..292ad70f8 100644 --- a/packages/agent/tests/agent-components.test.ts +++ b/packages/agent/tests/agent-components.test.ts @@ -28,6 +28,7 @@ import type { AgentProviderFactory } from "../src/provider-api.ts"; import { installAgentComponents } from "../src/components.ts"; import { Component } from "../../core/src/component-api.ts"; import type { Json } from "@executablemd/core"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; interface StubResponse { status?: "completed" | "failed" | "cancelled"; @@ -147,8 +148,8 @@ function* installStub(stub: Stub): Operation { function* runDoc( doc: string, stream: InMemoryStream, + dir = path.join(os.tmpdir(), `xmd-ac-test-${randomUUID()}`), ): Operation<{ output: string; result: Result }> { - const dir = path.join(os.tmpdir(), `xmd-ac-test-${randomUUID()}`); yield* ensureDir(dir); return yield* scoped(function* () { yield* ensure(() => rm(dir, { recursive: true, force: true })); @@ -170,36 +171,54 @@ function* runDoc( }); } -describe("Tier AC — agent components", () => { - it("AC1: wrapper always sends rendered children, untrimmed", function* () { - const stub = createStubProvider(); - yield* installStub(stub); - const { output, result } = yield* runDoc( - '\nSay hello\n\n', - new InMemoryStream(), +const SUITE = "Tier AC — agent components"; + +function agentCase( + name: string, + body: (run: (doc: string) => ReturnType) => Operation, +): void { + it(name, () => { + const dir = path.join(os.tmpdir(), `xmd-ac-test-${randomUUID()}`); + return captureTestJournal( + { owner: "packages/agent/tests/agent-components.test.ts", suite: SUITE, name }, + path.join(dir, "doc.md"), + (stream) => body((doc) => runDoc(doc, stream, dir)), ); - expect(result.ok).toBe(true); - expect(stub.promptCalls.length).toBe(1); - const sent = stub.promptCalls[0]!.content; - expect(sent).toContain("Say hello"); - expect(sent).not.toBe("fallback"); - expect(sent).not.toBe(sent.trim()); - expect(output).toContain("[stub-agent:stub:default:"); }); +} + +describe(SUITE, () => { + agentCase( + "AC1: wrapper always sends rendered children, untrimmed", + function* (runFresh) { + const stub = createStubProvider(); + yield* installStub(stub); + const { output, result } = yield* runFresh( + '\nSay hello\n\n', + ); + expect(result.ok).toBe(true); + expect(stub.promptCalls.length).toBe(1); + const sent = stub.promptCalls[0]!.content; + expect(sent).toContain("Say hello"); + expect(sent).not.toBe("fallback"); + expect(sent).not.toBe(sent.trim()); + expect(output).toContain("[stub-agent:stub:default:"); + }, + ); - it("AC2: empty wrapper children win over the text prop", function* () { + agentCase("AC2: empty wrapper children win over the text prop", function* (runFresh) { const stub = createStubProvider(); yield* installStub(stub); - const { result } = yield* runDoc('\n', new InMemoryStream()); + const { result } = yield* runFresh('\n'); expect(result.ok).toBe(true); expect(stub.promptCalls.length).toBe(1); expect(stub.promptCalls[0]!.content).toBe(""); }); - it("AC3: self-closing falls back to the text prop", function* () { + agentCase("AC3: self-closing falls back to the text prop", function* (runFresh) { const stub = createStubProvider(); yield* installStub(stub); - const { output, result } = yield* runDoc('\n', new InMemoryStream()); + const { output, result } = yield* runFresh('\n'); expect(result.ok).toBe(true); expect(stub.promptCalls[0]!.content).toBe("hello"); expect(output).toContain("[stub-agent:stub:default:hello]"); @@ -209,10 +228,10 @@ describe("Tier AC — agent components", () => { // function component: it names the component and lists the offending prop, // where the claimed handler built the same account behind a ` ` // prefix of its own. - it("AC19: the removed prompt prop is rejected as an unknown prop", function* () { + agentCase("AC19: the removed prompt prop is rejected as an unknown prop", function* (runFresh) { const stub = createStubProvider(); yield* installStub(stub); - const { result } = yield* runDoc('\n', new InMemoryStream()); + const { result } = yield* runFresh('\n'); expect(result.ok).toBe(false); expect(stub.promptCalls.length).toBe(0); const reported = result.ok ? "" : result.error.message; @@ -220,26 +239,28 @@ describe("Tier AC — agent components", () => { expect(reported).toContain("prompt"); }); - it("AC20: a component's own validation printed error is observed once", function* () { - const observed: string[] = []; - const stub = createStubProvider(); - yield* installStub(stub); - yield* Component.around({ - *raise([error], next) { - observed.push(error.message); - return yield* next(error); - }, - }); - // Written in a region that prints, because what this pins is that the - // diagnostic is observed once and rendered once — which needs it rendered. - const { output } = yield* runDoc( - '\n\n\n', - new InMemoryStream(), - ); - expect(observed).toHaveLength(1); - expect(observed[0]).toContain("Prop validation failed for "); - expect(output.match(//g)).toHaveLength(1); - }); + agentCase( + "AC20: a component's own validation printed error is observed once", + function* (runFresh) { + const observed: string[] = []; + const stub = createStubProvider(); + yield* installStub(stub); + yield* Component.around({ + *raise([error], next) { + observed.push(error.message); + return yield* next(error); + }, + }); + // Written in a region that prints, because what this pins is that the + // diagnostic is observed once and rendered once — which needs it rendered. + const { output } = yield* runFresh( + '\n\n\n', + ); + expect(observed).toHaveLength(1); + expect(observed[0]).toContain("Prop validation failed for "); + expect(output.match(//g)).toHaveLength(1); + }, + ); // The composition the whole component exists for, black box: a value bound by // ``, rendered by ``, consumed and retained by ``. Import @@ -273,109 +294,115 @@ describe("Tier AC — agent components", () => { } }); - it("AC4: as binding captures the response instead of emitting it", function* () { + agentCase("AC4: as binding captures the response instead of emitting it", function* (runFresh) { const stub = createStubProvider(); yield* installStub(stub); - const { output, result } = yield* runDoc( + const { output, result } = yield* runFresh( '\n\nCaptured: {answer}\n', - new InMemoryStream(), ); expect(result.ok).toBe(true); expect(output).toContain("Captured: [stub-agent:stub:default:hi]"); expect(output.indexOf("[stub-agent")).toBe(output.lastIndexOf("[stub-agent")); }); - it("AC5: agent and session scopes compose; per-prompt props override", function* () { - const stub = createStubProvider(); - yield* installStub(stub); - const doc = [ - '', - ' ', - ' ', - " ", - "", - "", - '', - "", - ].join("\n"); - const { result } = yield* runDoc(doc, new InMemoryStream()); - expect(result.ok).toBe(true); - expect(stub.promptCalls.length).toBe(2); - expect(stub.promptCalls[0]).toMatchObject({ - content: "one", - agent: "agent-two", - sessionKey: "stub:review", - }); - expect(stub.promptCalls[1]).toMatchObject({ - content: "two", - agent: "agent-three", - sessionKey: "stub:named", - timeout: 500, - }); - }); - - it("AC6: self-closing and validate without output", function* () { - const stub = createStubProvider(); - yield* installStub(stub); - const { output, result } = yield* runDoc( - '\n\nplain text\n', - new InMemoryStream(), - ); - expect(result.ok).toBe(true); - expect(stub.agentProbes).toContain("probe-me"); - expect(stub.promptCalls.length).toBe(0); - expect(output).toContain("plain text"); - expect(output).not.toContain("probe-me"); - }); - - it("AC7: failed prompts render partial text, later content runs, completion aggregates in order", function* () { - const stub = createStubProvider((content) => { - if (content.includes("first-fail")) { - return { status: "failed", stopReason: "max_tokens", deltas: ["partial-one "] }; - } - if (content.includes("second-fail")) { - return { status: "failed", stopReason: "refusal", deltas: [] }; - } - return {}; - }); - yield* installStub(stub); - const doc = [ - '', - "", - '', - "", - '', - "", - "after all prompts", - "", - ].join("\n"); - const { output, result } = yield* runDoc(doc, new InMemoryStream()); - expect(output).toContain("partial-one"); - expect(output).toContain("after all prompts"); - expect(result.ok).toBe(false); - if (!result.ok) { - expect(result.error).toBeInstanceOf(AggregateError); - if (result.error instanceof AggregateError) { - expect(result.error.message).toBe("2 agent prompt(s) failed"); - const [first, second] = result.error.errors; - expect(first).toBeInstanceOf(AgentPromptError); - expect(second).toBeInstanceOf(AgentPromptError); - if (first instanceof AgentPromptError && second instanceof AgentPromptError) { - expect(first.stopReason).toBe("max_tokens"); - expect(first.agent).toBe("stub-agent"); - expect(first.sessionKey).toBe("stub:default"); - expect(second.stopReason).toBe("refusal"); + agentCase( + "AC5: agent and session scopes compose; per-prompt props override", + function* (runFresh) { + const stub = createStubProvider(); + yield* installStub(stub); + const doc = [ + '', + ' ', + ' ', + " ", + "", + "", + '', + "", + ].join("\n"); + const { result } = yield* runFresh(doc); + expect(result.ok).toBe(true); + expect(stub.promptCalls.length).toBe(2); + expect(stub.promptCalls[0]).toMatchObject({ + content: "one", + agent: "agent-two", + sessionKey: "stub:review", + }); + expect(stub.promptCalls[1]).toMatchObject({ + content: "two", + agent: "agent-three", + sessionKey: "stub:named", + timeout: 500, + }); + }, + ); + + agentCase( + "AC6: self-closing and validate without output", + function* (runFresh) { + const stub = createStubProvider(); + yield* installStub(stub); + const { output, result } = yield* runFresh( + '\n\nplain text\n', + ); + expect(result.ok).toBe(true); + expect(stub.agentProbes).toContain("probe-me"); + expect(stub.promptCalls.length).toBe(0); + expect(output).toContain("plain text"); + expect(output).not.toContain("probe-me"); + }, + ); + + agentCase( + "AC7: failed prompts render partial text, later content runs, completion aggregates in order", + function* (runFresh) { + const stub = createStubProvider((content) => { + if (content.includes("first-fail")) { + return { status: "failed", stopReason: "max_tokens", deltas: ["partial-one "] }; + } + if (content.includes("second-fail")) { + return { status: "failed", stopReason: "refusal", deltas: [] }; + } + return {}; + }); + yield* installStub(stub); + const doc = [ + '', + "", + '', + "", + '', + "", + "after all prompts", + "", + ].join("\n"); + const { output, result } = yield* runFresh(doc); + expect(output).toContain("partial-one"); + expect(output).toContain("after all prompts"); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error).toBeInstanceOf(AggregateError); + if (result.error instanceof AggregateError) { + expect(result.error.message).toBe("2 agent prompt(s) failed"); + const [first, second] = result.error.errors; + expect(first).toBeInstanceOf(AgentPromptError); + expect(second).toBeInstanceOf(AgentPromptError); + if (first instanceof AgentPromptError && second instanceof AgentPromptError) { + expect(first.stopReason).toBe("max_tokens"); + expect(first.agent).toBe("stub-agent"); + expect(first.sessionKey).toBe("stub:default"); + expect(second.stopReason).toBe("refusal"); + } } } - } - }); + }, + ); - it("AC8: throwOnError aborts the document immediately", function* () { + agentCase("AC8: throwOnError aborts the document immediately", function* (runFresh) { const stub = createStubProvider(() => ({ status: "failed", stopReason: "refusal" })); yield* installStub(stub); - const { output, result } = yield* runDoc( + const { output, result } = yield* runFresh( '\n\nnever reached\n', - new InMemoryStream(), ); expect(result.ok).toBe(false); if (!result.ok) { diff --git a/packages/cli/src/test-diagnostics.ts b/packages/cli/src/test-diagnostics.ts index fdc371422..83d79797c 100644 --- a/packages/cli/src/test-diagnostics.ts +++ b/packages/cli/src/test-diagnostics.ts @@ -44,6 +44,13 @@ const DEFAULT_TEST_ARTIFACTS = join(".xmd", "test-artifacts"); const PREFIX = "[xmd test]"; +/** The repository test declaration that owns an automatic journal. */ +export interface JournalCase { + readonly owner: string; + readonly name: string; + readonly suite?: string; +} + /** This command's diagnostics, shared by its root documents and their children. */ export interface TestDiagnostics { /** @@ -97,14 +104,35 @@ function* artifactDirectory(working: string): Operation { * diagnostics drains what was accepted before it returns — on success, failure * and cancellation alike — and nothing is written after that. */ -export function useTestDiagnostics( +export function* useTestDiagnostics( sink: DeliverySink, + mode?: JournalCase | boolean, +): Operation { + const testCase = typeof mode === "boolean" ? undefined : mode; + return yield* (yield* prepareTestDiagnostics(sink, testCase, mode === true))(); +} + +/** Capture the artifact location before work whose diagnostics open after it joins. */ +export function* prepareTestDiagnostics( + sink: DeliverySink, + testCase?: JournalCase, failuresOnly = false, +): Operation<() => Operation> { + const working = yield* cwd(); + const directory = yield* artifactDirectory(working); + const shown = relativeTo(working); + const attribution = testCase === undefined ? undefined : { ...testCase }; + return () => testDiagnosticsAt(sink, directory, shown, attribution, failuresOnly); +} + +function testDiagnosticsAt( + sink: DeliverySink, + directory: string, + shown: (source: string) => string, + attribution: JournalCase | undefined, + failuresOnly: boolean, ): Operation { return resource(function* (provide) { - const working = yield* cwd(); - const directory = yield* artifactDirectory(working); - const shown = relativeTo(working); const owner = yield* useScope(); const lines = createQueue(); let delivery: Result = Ok(undefined); @@ -133,10 +161,11 @@ export function useTestDiagnostics( const results = { pass: 0, fail: 0 }; yield* provide({ execution(kind, source) { - counts[kind] += 1; - const label = `${kind} ${counts[kind]}`; - const file = join(directory, `${kind}-${counts[kind]}.jsonl`); - const named = `${label} ${shown(source)}`; + const ordinal = ++counts[kind]; + const stem = join(directory, `${kind}-${ordinal}`); + const file = `${stem}.jsonl`; + const displayedSource = shown(source); + const named = `${kind} ${ordinal} ${displayedSource}`; const context = kind === "root" ? "" : `${named}: `; return { observe: () => @@ -158,6 +187,17 @@ export function useTestDiagnostics( failed, *save(events: readonly DurableEvent[]) { yield* ensureDir(directory); + if (attribution !== undefined) { + yield* writeTextFile( + `${stem}.case.json`, + JSON.stringify({ + ...attribution, + kind, + ordinal, + source: displayedSource, + }) + "\n", + ); + } yield* writeTextFile(file, events.map(serializeDurableEvent).join("")); emit(`journal saved: ${file} (${named})`); }, diff --git a/packages/cli/tests/repl-execution.test.ts b/packages/cli/tests/repl-execution.test.ts index b29cf7213..9d3ef1335 100644 --- a/packages/cli/tests/repl-execution.test.ts +++ b/packages/cli/tests/repl-execution.test.ts @@ -28,9 +28,8 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import process from "node:process"; import { Writable } from "node:stream"; -import { withFailureJournal } from "@executablemd/testing"; import { partitionEntrySegments, ROOT_IMPORT } from "../src/repl/entries.ts"; -import { useTestDiagnostics } from "../src/test-diagnostics.ts"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; import type { DeliverySink } from "../src/stdout-delivery.ts"; import { useTempDirectory } from "@executablemd/test-support/temp"; @@ -237,93 +236,106 @@ function* readAll(holder: ReplExecution): Operation { const REFERENCE_PATH = join(REFERENCE_DIRECTORY, "entry.md"); +const OWNER = "packages/cli/tests/repl-execution.test.ts"; +const SUBMIT_SUITE = "REPL execution: submitting one entry"; + function captureReferenceEntry( + name: string, body: (holder: ReplExecution) => Operation, sink: DeliverySink = process.stderr, + suite = SUBMIT_SUITE, ): Operation { - return scoped(function* () { - const diagnostics = yield* useTestDiagnostics(sink); - const journal = diagnostics.execution("root", REFERENCE_PATH).journal(() => false); - yield* withFailureJournal( - { - ...journal, - *save(events) { - const partition = partitionEntrySegments(events); - if (!partition.ok) { - throw partition.error; - } - if (partition.value.length > 1) { - throw new Error("reference execution holds more than one admitted entry"); - } - const [entry] = partition.value; - if (entry !== undefined) { - yield* journal.save(entry.events); - } - }, + return captureTestJournal( + { owner: OWNER, suite, name }, + REFERENCE_PATH, + (stream) => body({ id: "kf39sla2", stream }), + { + sink, + selectEvents(events) { + const partition = partitionEntrySegments(events); + if (!partition.ok) { + throw partition.error; + } + if (partition.value.length > 1) { + throw new Error("reference execution holds more than one admitted entry"); + } + return partition.value[0]?.events ?? []; }, - (stream) => body({ id: "kf39sla2", stream }), - ); - }); + }, + ); +} + +function referenceCases(suite: string) { + return (name: string, body: (holder: ReplExecution) => Operation): void => { + it(name, () => captureReferenceEntry(name, body, process.stderr, suite)); + }; } function admitReferenceEntry( + name: string, check?: (holder: ReplExecution) => Operation, sink?: DeliverySink, ): Operation { - return captureReferenceEntry(function* (holder) { - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); + return captureReferenceEntry( + name, + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); - const question = yield* nextQuestion(session); - expect(question.message).toContain("Approve Ship the REPL?"); - // The whole form the schema reads as, member for member: every field the - // drawer draws, with its annotations, its required marker and the values it - // accepts. - expect(question.form).toEqual({ - title: undefined, - description: undefined, - condition: undefined, - fields: [ - { - name: "decision", - title: undefined, - description: undefined, - choices: ["approve", "decline"], - minLength: undefined, - required: true, - }, - ], - }); - expect(session.model.entries[0]?.scope.elicitations).toEqual([]); + const question = yield* nextQuestion(session); + expect(question.message).toContain("Approve Ship the REPL?"); + // The whole form the schema reads as, member for member: every field the + // drawer draws, with its annotations, its required marker and the values it + // accepts. + expect(question.form).toEqual({ + title: undefined, + description: undefined, + condition: undefined, + fields: [ + { + name: "decision", + title: undefined, + description: undefined, + choices: ["approve", "decline"], + minLength: undefined, + required: true, + }, + ], + }); + expect(session.model.entries[0]?.scope.elicitations).toEqual([]); - expect(question.submit({ decision: "approve" }).kind).toBe("answered"); - yield* session.join(); + expect(question.submit({ decision: "approve" }).kind).toBe("answered"); + yield* session.join(); - const entry = session.model.entries[0]?.scope; - if (entry === undefined) { - throw new Error("the entry was admitted"); - } - expect(entry.source).toBe(source); - expect(entry.bindings.map((binding) => binding.name)).toContain("plan"); - expect(entry.scopes.map((scope) => scope.key)).toEqual(["Checklist-1", "generated-1"]); - expect(entry.elicitations[0].answer).toEqual({ decision: "approve" }); - expect(session.model.settled).toBe(true); - expect(session.model.terminal?.output).toContain("Decision: approve"); - expect(session.overlay.output).toBe(session.model.terminal?.output); - if (check !== undefined) { - yield* check(holder); - } - }, sink); + const entry = session.model.entries[0]?.scope; + if (entry === undefined) { + throw new Error("the entry was admitted"); + } + expect(entry.source).toBe(source); + expect(entry.bindings.map((binding) => binding.name)).toContain("plan"); + expect(entry.scopes.map((scope) => scope.key)).toEqual(["Checklist-1", "generated-1"]); + expect(entry.elicitations[0].answer).toEqual({ decision: "approve" }); + expect(session.model.settled).toBe(true); + expect(session.model.terminal?.output).toContain("Decision: approve"); + expect(session.overlay.output).toBe(session.model.terminal?.output); + if (check !== undefined) { + yield* check(holder); + } + }, + sink, + ); } -describe("REPL execution: submitting one entry", () => { +describe(SUBMIT_SUITE, () => { beforeAll(() => useTempFileCompiler()); + const referenceCase = referenceCases(SUBMIT_SUITE); - it("X1: admits the exact source, binds, generates, asks, and settles", () => - admitReferenceEntry()); + const admission = "X1: admits the exact source, binds, generates, asks, and settles"; + it(admission, () => admitReferenceEntry(admission)); - it("X1: the overlay reports its output as the document prints it", () => - captureReferenceEntry(function* (holder) { + const overlay = "X1: the overlay reports its output as the document prints it"; + it(overlay, () => + captureReferenceEntry(overlay, function* (holder) { const source = yield* referenceSource(); const session = opened(yield* submitReplEntry({ ...options(holder), source })); @@ -354,9 +366,11 @@ describe("REPL execution: submitting one entry", () => { question.submit({ decision: "approve" }); yield* session.join(); - })); + }), + ); - it("saves the joined reference entry only when its case fails after admission", function* () { + const witness = "saves the joined reference entry only when its case fails after admission"; + it(witness, function* () { const directory = yield* useTempDirectory("xmd-repl-entry-journal-"); const failed = join(directory, "failed"); const passed = join(directory, "passed"); @@ -386,19 +400,23 @@ describe("REPL execution: submitting one entry", () => { try { yield* atArtifactRoot( failed, - admitReferenceEntry(function* (holder) { - recorded = yield* readAll(holder); - try { - expect( - recorded.some( - (event) => event.type === "yield" && event.description.name === ROOT_IMPORT, - ), - ).toBe(false); - } catch (error) { - assertionFailure = error; - throw error; - } - }, sink), + admitReferenceEntry( + witness, + function* (holder) { + recorded = yield* readAll(holder); + try { + expect( + recorded.some( + (event) => event.type === "yield" && event.description.name === ROOT_IMPORT, + ), + ).toBe(false); + } catch (error) { + assertionFailure = error; + throw error; + } + }, + sink, + ), ); } catch (error) { caught = error; @@ -417,14 +435,22 @@ describe("REPL execution: submitting one entry", () => { const invocations = yield* readdir(failed); expect(invocations).toHaveLength(1); const invocation = join(failed, invocations[0]); - expect(yield* readdir(invocation)).toEqual(["root-1.jsonl"]); + expect((yield* readdir(invocation)).sort()).toEqual(["root-1.case.json", "root-1.jsonl"]); + expect(JSON.parse(yield* readTextFile(join(invocation, "root-1.case.json")))).toEqual({ + owner: OWNER, + suite: SUBMIT_SUITE, + name: witness, + kind: "root", + ordinal: 1, + source: "packages/cli/tests/fixtures/repl/entry.md", + }); const journal = join(invocation, "root-1.jsonl"); expect(yield* readRecords(journal)).toEqual(entry.events); expect(lines).toHaveLength(1); expect(lines[0]).toContain(`journal saved: ${journal} (root 1 `); expect(lines[0]).toContain("packages/cli/tests/fixtures/repl/entry.md)"); - yield* atArtifactRoot(passed, admitReferenceEntry(undefined, sink)); + yield* atArtifactRoot(passed, admitReferenceEntry(witness, undefined, sink)); expect(yield* exists(passed)).toBe(false); const beforeAdmission = new Error("pre-admission failure"); @@ -432,10 +458,14 @@ describe("REPL execution: submitting one entry", () => { try { yield* atArtifactRoot( empty, - captureReferenceEntry(function* (holder) { - expect(yield* readAll(holder)).toEqual([]); - throw beforeAdmission; - }, sink), + captureReferenceEntry( + witness, + function* (holder) { + expect(yield* readAll(holder)).toEqual([]); + throw beforeAdmission; + }, + sink, + ), ); } catch (error) { emptyFailure = error; @@ -445,62 +475,68 @@ describe("REPL execution: submitting one entry", () => { expect(lines).toHaveLength(1); }); - it("X1: the generated fragment is shown before it is admitted, and follows the binding", function* () { - const holder = execution(); - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - (yield* nextQuestion(session)).submit({ decision: "decline" }); - yield* session.join(); + referenceCase( + "X1: the generated fragment is shown before it is admitted, and follows the binding", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + (yield* nextQuestion(session)).submit({ decision: "decline" }); + yield* session.join(); - const generated = session.model.entries[0]?.scope.generated[0]; - const fragment = session.model.entries[0]?.scope.scopes.find( - (scope) => scope.kind === "generated", - ); - expect(generated?.decision).toBe("admitted"); - // The value the durable evaluation published is what the fragment says, - // which is what makes it deterministic rather than merely repeatable. - expect(fragment?.source).toBe(''); - expect(session.model.terminal?.output).toContain("Decision: decline"); - }); + const generated = session.model.entries[0]?.scope.generated[0]; + const fragment = session.model.entries[0]?.scope.scopes.find( + (scope) => scope.kind === "generated", + ); + expect(generated?.decision).toBe("admitted"); + // The value the durable evaluation published is what the fragment says, + // which is what makes it deterministic rather than merely repeatable. + expect(fragment?.source).toBe(''); + expect(session.model.terminal?.output).toContain("Decision: decline"); + }, + ); - it("X1: shows the generated source before admitting it, then admits and evaluates it", function* () { - const holder = execution(); - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - const fragment = ''; + referenceCase( + "X1: shows the generated source before admitting it, then admits and evaluates it", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + const fragment = ''; - // Stepped rather than inspected at the end: a terminal model shows that - // both things happened and says nothing about which came first. Pausing at - // each boundary is what lets the order itself be observed. - controlling(session).pause(); - let shownBeforeAdmission = false; - let admitted = false; - for (let step = 0; step < 500 && session.live && !admitted; step++) { - yield* pausedOrDone(session); - if (!session.live) { - break; - } - admitted = counted(yield* readAll(holder), "generated_xmd") > 0; - if (!admitted && session.overlay.output.includes(fragment)) { - shownBeforeAdmission = true; - } - controlling(session).resume(); + // Stepped rather than inspected at the end: a terminal model shows that + // both things happened and says nothing about which came first. Pausing at + // each boundary is what lets the order itself be observed. controlling(session).pause(); - } + let shownBeforeAdmission = false; + let admitted = false; + for (let step = 0; step < 500 && session.live && !admitted; step++) { + yield* pausedOrDone(session); + if (!session.live) { + break; + } + admitted = counted(yield* readAll(holder), "generated_xmd") > 0; + if (!admitted && session.overlay.output.includes(fragment)) { + shownBeforeAdmission = true; + } + controlling(session).resume(); + controlling(session).pause(); + } - expect(shownBeforeAdmission).toBe(true); - expect(admitted).toBe(true); + expect(shownBeforeAdmission).toBe(true); + expect(admitted).toBe(true); - controlling(session).resume(); - (yield* nextQuestion(session)).submit({ decision: "approve" }); - yield* session.join(); + controlling(session).resume(); + (yield* nextQuestion(session)).submit({ decision: "approve" }); + yield* session.join(); - // The admitted fragment then ran where it was written: its rendering - // follows the source it was rendered from. - const settled = session.model.terminal?.output ?? ""; - expect(settled.indexOf(fragment)).toBeGreaterThan(-1); - expect(settled.indexOf('"title": "Ship the REPL"')).toBeGreaterThan(settled.indexOf(fragment)); - }); + // The admitted fragment then ran where it was written: its rendering + // follows the source it was rendered from. + const settled = session.model.terminal?.output ?? ""; + expect(settled.indexOf(fragment)).toBeGreaterThan(-1); + expect(settled.indexOf('"title": "Ship the REPL"')).toBeGreaterThan( + settled.indexOf(fragment), + ); + }, + ); it("X1: an entry this environment cannot run leaves the history empty", function* () { const holder = execution(); @@ -515,31 +551,33 @@ describe("REPL execution: submitting one entry", () => { expect(yield* holder.stream.readAll()).toEqual([]); }); - it("X1: a choice the form does not offer answers nothing and leaves the question open", function* () { - const holder = execution(); - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); + referenceCase( + "X1: a choice the form does not offer answers nothing and leaves the question open", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); - const question = yield* nextQuestion(session); - // Not an offered value, so the schema rejects it: the question stays open and - // nothing is recorded. - expect(question.submit({ decision: "maybe" }).kind).toBe("invalid"); - yield* sleep(10); - - // An incomplete interaction, not a rejected answer. The provider never - // returned, so there is nothing for core to judge and nothing to record. - // The other case — a provider that really hands back a value the schema - // rejects — settles the durable operation `err` with no answer, and E1 - // proves that in `packages/core/tests/elicit-component.test.ts`. This case - // does not prove that one. - expect(session.elicitation.pending).toBe(question); - expect(counted(yield* readAll(holder), "elicit")).toBe(0); - expect(session.model.settled).toBe(false); - - question.submit({ decision: "approve" }); - yield* session.join(); - expect(counted(yield* readAll(holder), "elicit")).toBe(1); - }); + const question = yield* nextQuestion(session); + // Not an offered value, so the schema rejects it: the question stays open and + // nothing is recorded. + expect(question.submit({ decision: "maybe" }).kind).toBe("invalid"); + yield* sleep(10); + + // An incomplete interaction, not a rejected answer. The provider never + // returned, so there is nothing for core to judge and nothing to record. + // The other case — a provider that really hands back a value the schema + // rejects — settles the durable operation `err` with no answer, and E1 + // proves that in `packages/core/tests/elicit-component.test.ts`. This case + // does not prove that one. + expect(session.elicitation.pending).toBe(question); + expect(counted(yield* readAll(holder), "elicit")).toBe(0); + expect(session.model.settled).toBe(false); + + question.submit({ decision: "approve" }); + yield* session.join(); + expect(counted(yield* readAll(holder), "elicit")).toBe(1); + }, + ); it("X1: an entry is refused while the entry before it has not settled", function* () { // The reference history without its root close: an entry that never reached @@ -560,6 +598,7 @@ describe("REPL execution: submitting one entry", () => { describe("REPL execution: reopening one history", () => { beforeAll(() => useTempFileCompiler()); + const referenceCase = referenceCases("REPL execution: reopening one history"); it("X2: replays what was recorded and goes live at the first unrecorded step", function* () { const settled = yield* referenceEvents(); @@ -592,26 +631,28 @@ describe("REPL execution: reopening one history", () => { expect(session.model.terminal?.output).toContain("Decision: approve"); }); - it("X2: the same counters do register the work a first run really performs", function* () { - const holder = execution(); - const source = yield* referenceSource(); - const performed = yield* countPerformed(); - const asked = yield* countAsked(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); + referenceCase( + "X2: the same counters do register the work a first run really performs", + function* (holder) { + const source = yield* referenceSource(); + const performed = yield* countPerformed(); + const asked = yield* countAsked(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); - (yield* nextQuestion(session)).submit({ decision: "approve" }); - yield* session.join(); + (yield* nextQuestion(session)).submit({ decision: "approve" }); + yield* session.join(); - // The positive control for every count the negatives rely on. Without it, a - // counter that never increments for any reason at all would read as proof - // of replay, or of a provider that was never reached. - expect(performed.reads.filter((path) => path.endsWith("Checklist.md")).length).toBeGreaterThan( - 0, - ); - expect(performed.compiles).toBeGreaterThan(0); - expect(asked.calls).toBe(1); - expect(session.elicitation.asked).toBe(1); - }); + // The positive control for every count the negatives rely on. Without it, a + // counter that never increments for any reason at all would read as proof + // of replay, or of a provider that was never reached. + expect( + performed.reads.filter((path) => path.endsWith("Checklist.md")).length, + ).toBeGreaterThan(0); + expect(performed.compiles).toBeGreaterThan(0); + expect(asked.calls).toBe(1); + expect(session.elicitation.asked).toBe(1); + }, + ); it("X2: a recorded answer is restored without the provider being reached", function* () { const settled = yield* referenceEvents(); @@ -773,79 +814,83 @@ describe("REPL execution: reopening one history", () => { describe("REPL execution: pausing expansion", () => { beforeAll(() => useTempFileCompiler()); + const referenceCase = referenceCases("REPL execution: pausing expansion"); - it("P1: stops at every boundary it crosses and continues past each one", function* () { - const holder = execution(); - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); + referenceCase( + "P1: stops at every boundary it crosses and continues past each one", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); - // Held for the length of the test. The session stops *offering* the - // capability once nothing is expanding, which is what the cold control - // below is about; the controller this run owned is still the thing that - // released its holds. - const controller = controlling(session); - controller.pause(); - let stops = 0; - while (session.live && stops < 500) { - yield* pausedOrDone(session); - if (!session.live) { - break; + // Held for the length of the test. The session stops *offering* the + // capability once nothing is expanding, which is what the cold control + // below is about; the controller this run owned is still the thing that + // released its holds. + const controller = controlling(session); + controller.pause(); + let stops = 0; + while (session.live && stops < 500) { + yield* pausedOrDone(session); + if (!session.live) { + break; + } + expect(session.expansion.state).toBe("paused"); + stops++; + controller.resume(); + // Re-armed in the same turn as the release, so the continuation this + // released stops at the next boundary rather than running to the end. + controller.pause(); } - expect(session.expansion.state).toBe("paused"); - stops++; controller.resume(); - // Re-armed in the same turn as the release, so the continuation this - // released stops at the next boundary rather than running to the end. - controller.pause(); - } - controller.resume(); - yield* session.join(); + yield* session.join(); - expect(stops).toBeGreaterThan(5); - expect(controller.released).toBe(stops); - expect(session.model.settled).toBe(true); - expect(session.model.terminal?.output).toContain("Decision: approve"); - }); + expect(stops).toBeGreaterThan(5); + expect(controller.released).toBe(stops); + expect(session.model.settled).toBe(true); + expect(session.model.terminal?.output).toContain("Decision: approve"); + }, + ); - it("P1: the Journal advances after pause is requested and before it takes effect", function* () { - const holder = execution(); - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - const controller = controlling(session); + referenceCase( + "P1: the Journal advances after pause is requested and before it takes effect", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + const controller = controlling(session); - // A question is work that has already started: the walk is suspended - // inside the provider, not held by the controller, so requesting a pause - // here cannot take effect until that operation finishes. - const question = yield* nextQuestion(session); - const before = (yield* readAll(holder)).length; + // A question is work that has already started: the walk is suspended + // inside the provider, not held by the controller, so requesting a pause + // here cannot take effect until that operation finishes. + const question = yield* nextQuestion(session); + const before = (yield* readAll(holder)).length; - controller.pause(); - expect(session.expansion.state).toBe("pausing"); + controller.pause(); + expect(session.expansion.state).toBe("pausing"); - // Answering lets the already-started operation settle. Its record reaches - // the Journal while the pause request is outstanding, which is the whole - // point: the expansion position and the Journal head are two positions. - question.submit({ decision: "approve" }); - yield* pausedOrDone(session); + // Answering lets the already-started operation settle. Its record reaches + // the Journal while the pause request is outstanding, which is the whole + // point: the expansion position and the Journal head are two positions. + question.submit({ decision: "approve" }); + yield* pausedOrDone(session); - const atPause = (yield* readAll(holder)).length; - expect(session.expansion.state).toBe("paused"); - expect(atPause).toBeGreaterThan(before); + const atPause = (yield* readAll(holder)).length; + expect(session.expansion.state).toBe("paused"); + expect(atPause).toBeGreaterThan(before); - // And once every walk is satisfied it goes no farther on its own. - yield* sleep(25); - expect((yield* readAll(holder)).length).toBe(atPause); - expect(session.expansion.state).toBe("paused"); + // And once every walk is satisfied it goes no farther on its own. + yield* sleep(25); + expect((yield* readAll(holder)).length).toBe(atPause); + expect(session.expansion.state).toBe("paused"); - // Only Continue moves it, and then it finishes. - controller.resume(); - yield* session.join(); - expect(session.model.settled).toBe(true); - expect((yield* readAll(holder)).length).toBeGreaterThan(atPause); - }); + // Only Continue moves it, and then it finishes. + controller.resume(); + yield* session.join(); + expect(session.model.settled).toBe(true); + expect((yield* readAll(holder)).length).toBeGreaterThan(atPause); + }, + ); - it("P1: continuing twice releases nothing the second time", function* () { - const holder = execution(); + referenceCase("P1: continuing twice releases nothing the second time", function* (holder) { const source = yield* referenceSource(); const session = opened(yield* submitReplEntry({ ...options(holder), source })); @@ -863,28 +908,29 @@ describe("REPL execution: pausing expansion", () => { yield* session.join(); }); - it("P1: a paused expansion is stopped, not cancelled, and the run is still there", function* () { - const holder = execution(); - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); + referenceCase( + "P1: a paused expansion is stopped, not cancelled, and the run is still there", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); - controlling(session).pause(); - yield* pausedOrDone(session); - const frozen = (yield* readAll(holder)).length; - yield* sleep(25); + controlling(session).pause(); + yield* pausedOrDone(session); + const frozen = (yield* readAll(holder)).length; + yield* sleep(25); - expect(session.expansion.state).toBe("paused"); - expect(session.live).toBe(true); - expect((yield* readAll(holder)).length).toBe(frozen); + expect(session.expansion.state).toBe("paused"); + expect(session.live).toBe(true); + expect((yield* readAll(holder)).length).toBe(frozen); - controlling(session).resume(); - (yield* nextQuestion(session)).submit({ decision: "approve" }); - yield* session.join(); - expect(session.model.settled).toBe(true); - }); + controlling(session).resume(); + (yield* nextQuestion(session)).submit({ decision: "approve" }); + yield* session.join(); + expect(session.model.settled).toBe(true); + }, + ); - it("P1: tearing the owner down releases nothing that was held", function* () { - const holder = execution(); + referenceCase("P1: tearing the owner down releases nothing that was held", function* (holder) { const source = yield* referenceSource(); let held = -1; @@ -929,6 +975,7 @@ describe("REPL execution: pausing expansion", () => { describe("REPL execution: the boundary inventory", () => { beforeAll(() => useTempFileCompiler()); + const referenceCase = referenceCases("REPL execution: the boundary inventory"); it("P1: every member of every wrapped Api is a walk, a gate, or declared not to be one", function* () { const partitioned = (partition: BoundaryPartition): string[] => @@ -939,36 +986,38 @@ describe("REPL execution: the boundary inventory", () => { expect(partitioned(OUTPUT_BOUNDARIES)).toEqual(Object.keys(DocumentOutput.operations).sort()); }); - it("P1: the reference entry crosses the boundaries this controller accounts for", function* () { - const holder = execution(); - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - // The controller, not a snapshot of its crossings: `crossings` answers - // with what has been crossed *so far*, and so far is nothing yet. - const controller = controlling(session); - (yield* nextQuestion(session)).submit({ decision: "approve" }); - yield* session.join(); - const crossings = controller.crossings; - - const controlled = [ - ...COMPONENT_BOUNDARIES.walks, - ...COMPONENT_BOUNDARIES.gates, - ...EXECUTION_BOUNDARIES.walks, - ...OUTPUT_BOUNDARIES.gates, - HOST_WALK, - ]; - const crossed = [...crossings.keys()].sort(); - for (const boundary of crossed) { - expect(controlled).toContain(boundary); - } - // The ones this document is written to reach. A boundary disappearing - // from it is what this list is here to notice. - expect(crossed).toContain("document"); - expect(crossed).toContain("importComponent"); - expect(crossed).toContain("applyModifiers"); - expect(crossed).toContain("output"); - expect(crossings.get("document")).toBe(1); - }); + referenceCase( + "P1: the reference entry crosses the boundaries this controller accounts for", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + // The controller, not a snapshot of its crossings: `crossings` answers + // with what has been crossed *so far*, and so far is nothing yet. + const controller = controlling(session); + (yield* nextQuestion(session)).submit({ decision: "approve" }); + yield* session.join(); + const crossings = controller.crossings; + + const controlled = [ + ...COMPONENT_BOUNDARIES.walks, + ...COMPONENT_BOUNDARIES.gates, + ...EXECUTION_BOUNDARIES.walks, + ...OUTPUT_BOUNDARIES.gates, + HOST_WALK, + ]; + const crossed = [...crossings.keys()].sort(); + for (const boundary of crossed) { + expect(controlled).toContain(boundary); + } + // The ones this document is written to reach. A boundary disappearing + // from it is what this list is here to notice. + expect(crossed).toContain("document"); + expect(crossed).toContain("importComponent"); + expect(crossed).toContain("applyModifiers"); + expect(crossed).toContain("output"); + expect(crossings.get("document")).toBe(1); + }, + ); }); describe("REPL execution: one execution's file", () => { diff --git a/packages/cli/tests/repl-model.test.ts b/packages/cli/tests/repl-model.test.ts index 4ff20c653..ab73ba344 100644 --- a/packages/cli/tests/repl-model.test.ts +++ b/packages/cli/tests/repl-model.test.ts @@ -19,6 +19,7 @@ import { * binding in a scope that never published it. */ +import { captureTestJournal } from "../../test-support/failure-journal.ts"; import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { Component, useTempFileCompiler } from "@executablemd/core"; @@ -451,14 +452,21 @@ describe("REPL model: the histories it refuses", () => { expect(refusal(stripped)).toContain("does not retain the schema"); }); - it("E1: a refused answer leaves no answered question in the projection", function* () { - const run = yield* runReference(answering({ decision: "maybe" })); - const model = projected(run.events); + const rejectedAnswer = "E1: a refused answer leaves no answered question in the projection"; + it(rejectedAnswer, () => + captureTestJournal( + { owner: "packages/cli/tests/repl-model.test.ts", name: rejectedAnswer }, + "", + function* (stream) { + const run = yield* runReference(answering({ decision: "maybe" }), stream); + const model = projected(run.events); - expect(run.asked).toHaveLength(1); - expect(only(model)?.elicitations).toEqual([]); - expect(model.transcript.some((row) => row.kind === "elicit")).toBe(false); - }); + expect(run.asked).toHaveLength(1); + expect(only(model)?.elicitations).toEqual([]); + expect(model.transcript.some((row) => row.kind === "elicit")).toBe(false); + }, + ), + ); }); /** diff --git a/packages/cli/tests/retained-repository-observations-deno.test.ts b/packages/cli/tests/retained-repository-observations-deno.test.ts index ce1bd56b2..208bcec3f 100644 --- a/packages/cli/tests/retained-repository-observations-deno.test.ts +++ b/packages/cli/tests/retained-repository-observations-deno.test.ts @@ -33,6 +33,7 @@ import { import { useCommonDirectoryLease } from "../src/repl/repositories/deno/leases.ts"; import { useObservationFixture, writeFixtureFile } from "../../git/tests/support/observations.ts"; import { + captureObservationEntry, entryCounters, methodsCalled, readKinds, @@ -128,7 +129,8 @@ describe("ROB5 — cancellation, persistence and ownership, on a native host", ( ); }); - it("keeps the same entry's mutation out of a held read, then lets it through", function* () { + const heldRead = "keeps the same entry's mutation out of a held read, then lets it through"; + it(heldRead, function* () { const fixture = yield* useObservationFixture(); const place = yield* useEntryPlace(fixture); const counted = entryCounters(); @@ -140,80 +142,86 @@ describe("ROB5 — cancellation, persistence and ownership, on a native host", ( let holding = false; let enteredWhileHeld = false; - const settled = yield* scoped(function* () { - const task = yield* spawn(() => - runEntry( - place, - [ - "", - '', - '', - "", - "", - ].join("\n"), - new InMemoryStream(), - counted, - { - *between(): Operation { - // The second branch starts its mutation only once the read is - // genuinely held, so the race the regression needs is arranged - // rather than hoped for. - yield* registerComponents([ - { - name: "WaitsForTheRead", - origin: "test://waits-for-the-read", - props: { type: "object", properties: {}, additionalProperties: false }, - *fn(): Operation { - yield* readHeld.operation; - return ""; - }, + yield* captureObservationEntry( + { owner: "packages/cli/tests/retained-repository-observations-deno.test.ts", name: heldRead }, + place, + function* (stream) { + const settled = yield* scoped(function* () { + const task = yield* spawn(() => + runEntry( + place, + [ + "", + '', + '', + "", + "", + ].join("\n"), + stream, + counted, + { + *between(): Operation { + // The second branch starts its mutation only once the read is + // genuinely held, so the race the regression needs is arranged + // rather than hoped for. + yield* registerComponents([ + { + name: "WaitsForTheRead", + origin: "test://waits-for-the-read", + props: { type: "object", properties: {}, additionalProperties: false }, + *fn(): Operation { + yield* readHeld.operation; + return ""; + }, + }, + ]); }, - ]); - }, - *barrier(method): Operation { - if (method === "add") { - if (holding) { - enteredWhileHeld = true; - } - mutationEntered.resolve(); - return; - } - if (method === "observe" && !holding) { - holding = true; - readHeld.resolve(); - // Long enough that a mutation on a queue of its own would have - // reached the host by now, and short enough to end the case. - yield* race([ - mutationEntered.operation, - (function* (): Operation { - yield* sleep(2000); - })(), - ]); - holding = false; - releaseRead.resolve(); - } - }, - }, - ), - ); - yield* releaseRead.operation; - // The read has finished its own turn; the mutation may now proceed. - yield* race([ - mutationEntered.operation, - (function* (): Operation { - yield* sleep(30_000); - throw new Error("the mutation never reached native Git"); - })(), - ]); - return yield* task; - }); + *barrier(method): Operation { + if (method === "add") { + if (holding) { + enteredWhileHeld = true; + } + mutationEntered.resolve(); + return; + } + if (method === "observe" && !holding) { + holding = true; + readHeld.resolve(); + // Long enough that a mutation on a queue of its own would have + // reached the host by now, and short enough to end the case. + yield* race([ + mutationEntered.operation, + (function* (): Operation { + yield* sleep(2000); + })(), + ]); + holding = false; + releaseRead.resolve(); + } + }, + }, + ), + ); + yield* releaseRead.operation; + // The read has finished its own turn; the mutation may now proceed. + yield* race([ + mutationEntered.operation, + (function* (): Operation { + yield* sleep(30_000); + throw new Error("the mutation never reached native Git"); + })(), + ]); + return yield* task; + }); - // Nothing of this entry's own Git entered while its read held the turn, - // and the mutation did run once the read released it. - expect(enteredWhileHeld).toBe(false); - expect(settled.refusal).toBe(undefined); - expect(readKinds(counted)).toContain("diff"); - expect(methodsCalled(counted)).toContain("add"); + // Nothing of this entry's own Git entered while its read held the turn, + // and the mutation did run once the read released it. + expect(enteredWhileHeld).toBe(false); + expect(settled.refusal).toBe(undefined); + expect(readKinds(counted)).toContain("diff"); + expect(methodsCalled(counted)).toContain("add"); + }, + ); }); it("answers while another holder has the checkout's mutation lease", function* () { diff --git a/packages/cli/tests/retained-repository-observations.test.ts b/packages/cli/tests/retained-repository-observations.test.ts index f1a65dc09..b307d1c0b 100644 --- a/packages/cli/tests/retained-repository-observations.test.ts +++ b/packages/cli/tests/retained-repository-observations.test.ts @@ -55,6 +55,7 @@ import { admitReplRepositoryHistory } from "../src/repl/repositories/admission.t import { Elicitation } from "@executablemd/core"; import { useObservationFixture, writeFixtureFile } from "../../git/tests/support/observations.ts"; import { + captureObservationEntry, entryCounters, methodsCalled, readKinds, @@ -583,79 +584,93 @@ describe("ROB2 — the record is closed, and named by the request it carries", ( }); describe("ROB3 — ordinary middleware keeps its ordinary meaning", () => { - it("creates no record when a replacement answers above the provider", function* () { + const replacement = "creates no record when a replacement answers above the provider"; + it(replacement, function* () { const fixture = yield* useObservationFixture(); const place = yield* useEntryPlace(fixture); const counted = entryCounters(); const answered: string[] = []; - const run = yield* runEntry( + yield* captureObservationEntry( + { owner: "packages/cli/tests/retained-repository-observations.test.ts", name: replacement }, place, - ['', "", "", ""].join("\n"), - new InMemoryStream(), - counted, - { - *between(): Operation { - yield* GitQuery.around({ - // deno-lint-ignore require-yield - *diff([invocation]: [GitDiffInvocation]): Operation { - answered.push(invocation.request.kind); - return { - comparison: { compare: "unstaged" }, - text: "answered above", - offset: 0, - length: 14, - next: null, - }; + function* (stream) { + const run = yield* runEntry( + place, + ['', "", "", ""].join("\n"), + stream, + counted, + { + *between(): Operation { + yield* GitQuery.around({ + // deno-lint-ignore require-yield + *diff([invocation]: [GitDiffInvocation]): Operation { + answered.push(invocation.request.kind); + return { + comparison: { compare: "unstaged" }, + text: "answered above", + offset: 0, + length: 14, + next: null, + }; + }, + }); }, - }); - }, + }, + ); + + expect(run.refusal).toBe(undefined); + expect(answered).toEqual(["diff"]); + expect(run.output).toContain("answered above"); + // Answered entirely above the provider, so no provider was reached: no + // native read, no scratch directory and no observation record. + expect(observations(run.events)).toEqual([]); + // Ambient discovery still happened — the component asks which repository is + // in scope before it asks anything of it — but not one native *read* ran. + expect(readKinds(counted)).toEqual([]); }, ); - - expect(run.refusal).toBe(undefined); - expect(answered).toEqual(["diff"]); - expect(run.output).toContain("answered above"); - // Answered entirely above the provider, so no provider was reached: no - // native read, no scratch directory and no observation record. - expect(observations(run.events)).toEqual([]); - // Ambient discovery still happened — the component asks which repository is - // in scope before it asks anything of it — but not one native *read* ran. - expect(readKinds(counted)).toEqual([]); }); - it("records the request a delegating replacement actually passed down", function* () { + const delegation = "records the request a delegating replacement actually passed down"; + it(delegation, function* () { const fixture = yield* useObservationFixture(); const place = yield* useEntryPlace(fixture); const counted = entryCounters(); - const run = yield* runEntry( + yield* captureObservationEntry( + { owner: "packages/cli/tests/retained-repository-observations.test.ts", name: delegation }, place, - ['', ""].join("\n"), - new InMemoryStream(), - counted, - { - *between(): Operation { - yield* GitQuery.around({ - diff: ([invocation]: [GitDiffInvocation], next): Operation => - next({ ...invocation, request: { ...invocation.request, limit: 7 } }), - }); - }, + function* (stream) { + const run = yield* runEntry( + place, + ['', ""].join("\n"), + stream, + counted, + { + *between(): Operation { + yield* GitQuery.around({ + diff: ([invocation]: [GitDiffInvocation], next): Operation => + next({ ...invocation, request: { ...invocation.request, limit: 7 } }), + }); + }, + }, + ); + + expect(run.refusal).toBe(undefined); + const recorded = observations(run.events); + expect(recorded).toHaveLength(1); + // The request the provider received, not the one the document wrote. + expect(recorded[0]?.record.request.query).toEqual({ + kind: "diff", + comparison: { compare: "unstaged" }, + paths: null, + offset: 0, + limit: 7, + cursor: null, + }); }, ); - - expect(run.refusal).toBe(undefined); - const recorded = observations(run.events); - expect(recorded).toHaveLength(1); - // The request the provider received, not the one the document wrote. - expect(recorded[0]?.record.request.query).toEqual({ - kind: "diff", - comparison: { compare: "unstaged" }, - paths: null, - offset: 0, - limit: 7, - cursor: null, - }); }); }); diff --git a/packages/cli/tests/run-composition.test.ts b/packages/cli/tests/run-composition.test.ts index 00864c686..ace09a8a7 100644 --- a/packages/cli/tests/run-composition.test.ts +++ b/packages/cli/tests/run-composition.test.ts @@ -35,6 +35,9 @@ import { installPlugins } from "../src/plugin-host.ts"; import type { CommandPlugins } from "../src/plugin-host.ts"; import { BUNDLED_PLUGIN } from "../src/run-profile.ts"; import { DEFAULT_REPOSITORY_ROOT } from "../src/run-repositories.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/cli/tests/run-composition.test.ts"); /** Every element an author can write that needs a repository provider. */ const OPERATIONS: readonly { readonly name: string; readonly source: string }[] = [ @@ -187,21 +190,25 @@ describe("ORC1 — describing the vocabulary reaches nothing", () => { expect(yield* readdir(managed)).toEqual([]); }); - it("leaves every repository operation unprovided after inspection", function* () { - // Registering the declarations installs no provider: the Apis still answer - // with their own defaults, which is what a catalog is allowed to leave - // behind. - // Assembled rather than bootstrapped: the vocabulary belongs to the - // bundled Plugin, and installing it is what a run profile does. Installing - // it still installs no provider, which is the claim. - yield* gitProfile("run"); - const failure = yield* raisedValue( - collect(yield* execute({ ...inlineSource(``), stream: new InMemoryStream() })), - ); - // `` has no lexical Repository, so the first thing it asks for - // is the ambient one — and that is the Api reporting absence. - expect(String(failure)).toContain("no Repository composition provider is installed"); - }); + journalCase( + "leaves every repository operation unprovided after inspection", + "", + function* (stream) { + // Registering the declarations installs no provider: the Apis still answer + // with their own defaults, which is what a catalog is allowed to leave + // behind. + // Assembled rather than bootstrapped: the vocabulary belongs to the + // bundled Plugin, and installing it is what a run profile does. Installing + // it still installs no provider, which is the claim. + yield* gitProfile("run"); + const failure = yield* raisedValue( + collect(yield* execute({ ...inlineSource(``), stream })), + ); + // `` has no lexical Repository, so the first thing it asks for + // is the ambient one — and that is the Api reporting absence. + expect(String(failure)).toContain("no Repository composition provider is installed"); + }, + ); }); describe("ORC2 — one language, described everywhere and operated somewhere", () => { diff --git a/packages/cli/tests/support/repl-observations.ts b/packages/cli/tests/support/repl-observations.ts index e55939b07..f2dbd02eb 100644 --- a/packages/cli/tests/support/repl-observations.ts +++ b/packages/cli/tests/support/repl-observations.ts @@ -42,6 +42,8 @@ import { useCompositionComponents } from "@executablemd/git"; import { useReplRepositoryRecording } from "../../src/repl/repositories/deno/provider.ts"; import { admitReplRepositoryHistory } from "../../src/repl/repositories/admission.ts"; import type { ObservationFixture } from "../../../git/tests/support/observations.ts"; +import { captureTestJournal } from "../../../test-support/failure-journal.ts"; +import type { JournalCase } from "../../src/test-diagnostics.ts"; /** What the repository handle beneath one entry was asked to do. */ export interface EntryCounters { @@ -265,6 +267,14 @@ export function runEntry( }); } +export function captureObservationEntry( + testCase: JournalCase, + place: EntryPlace, + body: (stream: InMemoryStream) => Operation, +): Operation { + return captureTestJournal(testCase, join(place.root, "entry.md"), body); +} + /** * The prefix an entry left behind when it was left at a question. * diff --git a/packages/core/tests/component-invocation-hooks.test.ts b/packages/core/tests/component-invocation-hooks.test.ts index 4a32981df..8360c357b 100644 --- a/packages/core/tests/component-invocation-hooks.test.ts +++ b/packages/core/tests/component-invocation-hooks.test.ts @@ -20,7 +20,9 @@ import type { IdentityComponent, } from "../host.ts"; import { retainedSource } from "../src/root-source.ts"; -import { useHeldHost } from "./support/held-host.ts"; +import { heldHostCases, useHeldHost } from "./support/held-host.ts"; + +const heldHostCase = heldHostCases("packages/core/tests/component-invocation-hooks.test.ts"); const props = { type: "object", properties: {}, additionalProperties: false }; function declaration(body: () => Operation, name = "Observed"): IdentityComponent { @@ -39,145 +41,153 @@ function* text(): Operation { } describe("Installation-owned component observation", () => { - it("R4a/R6: preserves dual-form dispatch, generated identity and accepted values", function* () { - const events: string[] = []; - const observations: ComponentInvocationObservation[] = []; - const values: unknown[] = []; - const names: string[] = []; - let projected = 0; - const component: IdentityComponent = { - name: "Observed", - origin: "test://observation", - props: { - type: "object", - properties: { - nested: { - type: "object", - properties: { label: { type: "string", default: "default" } }, - additionalProperties: false, + heldHostCase( + "R4a/R6: preserves dual-form dispatch, generated identity and accepted values", + function* (stream) { + const events: string[] = []; + const observations: ComponentInvocationObservation[] = []; + const values: unknown[] = []; + const names: string[] = []; + let projected = 0; + const component: IdentityComponent = { + name: "Observed", + origin: "test://observation", + props: { + type: "object", + properties: { + nested: { + type: "object", + properties: { label: { type: "string", default: "default" } }, + additionalProperties: false, + }, }, + additionalProperties: false, }, - additionalProperties: false, - }, - captures: ["raw"], - returns: { - type: "object", - properties: { form: { type: "string" }, accepted: { type: "boolean", default: true } }, - required: ["form", "accepted"], - additionalProperties: false, - }, - forms: ["self-closing", "paired"], - generated: { - "self-closing": { identity: "https://executable.md/test/observed/read", effect: "read" }, - paired: { identity: "https://executable.md/test/observed/write", effect: "write" }, - }, - factory(claim) { - function body(form: string) { - return function* (input: Record, invocation: Parameters[0]) { - const name = yield* claim(invocation); - names.push(name); - if (form === "write") { - yield* content(); - } - const nested = input.nested; - if (nested !== null && typeof nested === "object" && !Array.isArray(nested)) { - nested.label = "changed"; - } - yield createDurableOperation({ type: "observed", name }, function* () { - events.push(form); - return form; - }); - return { form }; + captures: ["raw"], + returns: { + type: "object", + properties: { form: { type: "string" }, accepted: { type: "boolean", default: true } }, + required: ["form", "accepted"], + additionalProperties: false, + }, + forms: ["self-closing", "paired"], + generated: { + "self-closing": { identity: "https://executable.md/test/observed/read", effect: "read" }, + paired: { identity: "https://executable.md/test/observed/write", effect: "write" }, + }, + factory(claim) { + function body(form: string) { + return function* ( + input: Record, + invocation: Parameters[0], + ) { + const name = yield* claim(invocation); + names.push(name); + if (form === "write") { + yield* content(); + } + const nested = input.nested; + if (nested !== null && typeof nested === "object" && !Array.isArray(nested)) { + nested.label = "changed"; + } + yield createDurableOperation({ type: "observed", name }, function* () { + events.push(form); + return form; + }); + return { form }; + }; + } + return formDispatcher({ + forms: "both", + "self-closing": body("read"), + paired: body("write"), + }); + }, + }; + const hooks: ComponentInvocationHooks = { + *before(observation) { + expect(projected).toBe(observations.length < 2 ? 0 : 1); + observations.push(observation); + expect(Object.isFrozen(observation)).toBe(true); + expect(Object.isFrozen(observation.props.nested)).toBe(true); + expect(observation.props).toEqual({ nested: { label: "default" } }); + yield* sleep(0); + }, + *after(observation, outcome, cancelled) { + expect(observations.includes(observation)).toBe(true); + expect(cancelled).toBe(false); + expect(observation.props).toEqual({ nested: { label: "default" } }); + if (outcome.ok) { + values.push(outcome.value); + } + yield* sleep(0); + }, + }; + const installation: ExecutionInstallation = { + components: [component], + componentInvocations: hooks, + *install() { + hooks.before = function* () { + throw new Error("late mutation"); }; - } - return formDispatcher({ - forms: "both", - "self-closing": body("read"), - paired: body("write"), - }); - }, - }; - const hooks: ComponentInvocationHooks = { - *before(observation) { - expect(projected).toBe(observations.length < 2 ? 0 : 1); - observations.push(observation); - expect(Object.isFrozen(observation)).toBe(true); - expect(Object.isFrozen(observation.props.nested)).toBe(true); - expect(observation.props).toEqual({ nested: { label: "default" } }); - yield* sleep(0); - }, - *after(observation, outcome, cancelled) { - expect(observations.includes(observation)).toBe(true); - expect(cancelled).toBe(false); - expect(observation.props).toEqual({ nested: { label: "default" } }); - if (outcome.ok) { - values.push(outcome.value); - } - yield* sleep(0); - }, - }; - const installation: ExecutionInstallation = { - components: [component], - componentInvocations: hooks, - *install() { - hooks.before = function* () { - throw new Error("late mutation"); - }; - hooks.after = hooks.before; - yield* registerComponents([ - { - name: "Projected", - origin: "test://projection", - props, - *fn() { - projected++; - yield* sleep(0); - return "content"; + hooks.after = hooks.before; + yield* registerComponents([ + { + name: "Projected", + origin: "test://projection", + props, + *fn() { + projected++; + yield* sleep(0); + return "content"; + }, }, - }, - ]); - yield* Component.around({ generated: () => ({ Observed: ["self-closing", "paired"] }) }); - }, - }; - const held = yield* useHeldHost({ - installations: [installation], - root: { - path: "hooks.md", + ]); + yield* Component.around({ generated: () => ({ Observed: ["self-closing", "paired"] }) }); + }, + }; + const held = yield* useHeldHost({ + stream, + installations: [installation], + root: { + path: "hooks.md", + source: + '\n\n', + }, + inside: function* () { + const caller = yield* Component.operations.env; + expect(caller?.values.inputs).toEqual({}); + expect(caller?.values.read).toEqual({ form: "read", accepted: true }); + expect(caller?.values.write).toEqual({ form: "write", accepted: true }); + }, + }); + expect(events).toEqual(["read", "write"]); + const evaluated = yield* held.execution.evaluateGeneratedXmdRoot({ + id: "dual", source: - '\n\n', - }, - inside: function* () { - const caller = yield* Component.operations.env; - expect(caller?.values.inputs).toEqual({}); - expect(caller?.values.read).toEqual({ form: "read", accepted: true }); - expect(caller?.values.write).toEqual({ form: "write", accepted: true }); - }, - }); - expect(events).toEqual(["read", "write"]); - const evaluated = yield* held.execution.evaluateGeneratedXmdRoot({ - id: "dual", - source: - '\nbody\n', - allow: ["read", "write"], - }); - expect(evaluated.ok).toBe(true); - expect(events).toEqual(["read", "write", "read", "write"]); - expect(values).toEqual([ - { form: "read", accepted: true }, - { form: "write", accepted: true }, - { form: "read", accepted: true }, - { form: "write", accepted: true }, - ]); - expect(new Set(names).size).toBe(4); - expect(new Set(observations).size).toBe(4); - const refused = yield* held.execution.evaluateGeneratedXmdRoot({ - id: "readonly", - source: 'body', - allow: ["read"], - }); - expect(refused.ok).toBe(false); - expect(events).toHaveLength(4); - }); + '\nbody\n', + allow: ["read", "write"], + }); + expect(evaluated.ok).toBe(true); + expect(events).toEqual(["read", "write", "read", "write"]); + expect(values).toEqual([ + { form: "read", accepted: true }, + { form: "write", accepted: true }, + { form: "read", accepted: true }, + { form: "write", accepted: true }, + ]); + expect(new Set(names).size).toBe(4); + expect(new Set(observations).size).toBe(4); + const refused = yield* held.execution.evaluateGeneratedXmdRoot({ + id: "readonly", + source: 'body', + allow: ["read"], + }); + expect(refused.ok).toBe(false); + expect(events).toHaveLength(4); + }, + "hooks.md", + ); it("R4b: independently optional callbacks and invalid inputs", function* () { for (const mode of ["none", "before", "after"]) { diff --git a/packages/core/tests/each.test.ts b/packages/core/tests/each.test.ts index 819dfd901..c3ec7923f 100644 --- a/packages/core/tests/each.test.ts +++ b/packages/core/tests/each.test.ts @@ -7,11 +7,13 @@ import { expandSegments } from "../src/expand.ts"; import { Component } from "../src/component-api.ts"; import { scanSegments } from "../src/scanner.ts"; import { renderSegments } from "../src/render.ts"; -import { InMemoryStream } from "@executablemd/durable-streams"; import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import type { CodeBlockResult, Segment } from "../src/types.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/each.test.ts"); interface EachRun { segments: Segment[]; @@ -217,31 +219,33 @@ describe("Tier Each — native iteration directive", () => { describe("Tier Each — eval in the loop body", () => { beforeAll(() => useTempFileCompiler()); - it("E18: a body eval block reads the current item binding", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "test.md": [ - "```js eval", - "const items = [{ name: 'alpha' }, { name: 'beta' }];", - "```", - '', - "```js eval", - "output('got:' + item.name);", - "```", - "", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("got:alpha"); - expect(output).toContain("got:beta"); - expect(output).not.toContain("ERROR"); - }); - - it("E19: renders a table row per item end to end", function* () { - const stream = new InMemoryStream(); + journalCase( + "E18: a body eval block reads the current item binding", + "test.md", + function* (stream) { + yield* useStubFs({ + "test.md": [ + "```js eval", + "const items = [{ name: 'alpha' }, { name: 'beta' }];", + "```", + '', + "```js eval", + "output('got:' + item.name);", + "```", + "", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).toContain("got:alpha"); + expect(output).toContain("got:beta"); + expect(output).not.toContain("ERROR"); + }, + ); + + journalCase("E19: renders a table row per item end to end", "test.md", function* (stream) { yield* useStubFs({ "test.md": [ "```js eval", @@ -261,54 +265,60 @@ describe("Tier Each — eval in the loop body", () => { expect(output).not.toContain("ERROR"); }); - it("E22: each iteration's eval env is fresh — body bindings do not carry over", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "test.md": [ - "```js eval", - "const items = [1, 2];", - "```", - '', - "```js eval", - "output('iter' + n + ':' + ('carry' in env ? env.carry : 'absent'));", - "env.carry = 'from-' + n;", - "```", - "", - "```js eval", - "output('final:' + (typeof carry === 'undefined' ? 'absent' : carry));", - "```", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("iter1:absent"); - expect(output).toContain("iter2:absent"); - expect(output).toContain("final:absent"); - expect(output).not.toContain("ERROR"); - }); - - it("E21: an projected through resolves caller bindings", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Wrap.md": "", - "test.md": [ - "```js eval", - "const items = [{ n: 1 }, { n: 2 }];", - "const label = 'L';", - "```", - "", - '[{label}:{item.n}]', - "", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("[L:1]"); - expect(output).toContain("[L:2]"); - expect(output).not.toContain("ERROR"); - }); + journalCase( + "E22: each iteration's eval env is fresh — body bindings do not carry over", + "test.md", + function* (stream) { + yield* useStubFs({ + "test.md": [ + "```js eval", + "const items = [1, 2];", + "```", + '', + "```js eval", + "output('iter' + n + ':' + ('carry' in env ? env.carry : 'absent'));", + "env.carry = 'from-' + n;", + "```", + "", + "```js eval", + "output('final:' + (typeof carry === 'undefined' ? 'absent' : carry));", + "```", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).toContain("iter1:absent"); + expect(output).toContain("iter2:absent"); + expect(output).toContain("final:absent"); + expect(output).not.toContain("ERROR"); + }, + ); + + journalCase( + "E21: an projected through resolves caller bindings", + "test.md", + function* (stream) { + yield* useStubFs({ + "components/Wrap.md": "", + "test.md": [ + "```js eval", + "const items = [{ n: 1 }, { n: 2 }];", + "const label = 'L';", + "```", + "", + '[{label}:{item.n}]', + "", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).toContain("[L:1]"); + expect(output).toContain("[L:2]"); + expect(output).not.toContain("ERROR"); + }, + ); }); diff --git a/packages/core/tests/ephemeral-service.test.ts b/packages/core/tests/ephemeral-service.test.ts index 03de69a46..f83cf8132 100644 --- a/packages/core/tests/ephemeral-service.test.ts +++ b/packages/core/tests/ephemeral-service.test.ts @@ -17,6 +17,9 @@ import { collect } from "../src/collect.ts"; import { execute } from "../src/execute.ts"; import { LiveBindingCollisionError } from "../src/live-env.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/ephemeral-service.test.ts"); const SAMPLE = `--- meta: @@ -548,11 +551,13 @@ const shared = "must-not-execute"; ).toHaveLength(1); }); - it("allows a later ephemeral eval to update an existing live binding", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "doc.md": `\n`, - "components/Provider.md": `--- + journalCase( + "allows a later ephemeral eval to update an existing live binding", + "doc.md", + function* (stream) { + yield* useStubFs({ + "doc.md": `\n`, + "components/Provider.md": `--- meta: componentName: Provider --- @@ -574,21 +579,22 @@ yield* Sample.around({ `, - "components/Sample.md": SAMPLE, - }); + "components/Sample.md": SAMPLE, + }); - const output = String( - yield* scoped(function* () { - return yield* collect( - yield* execute({ - path: "doc.md", - stream, - includes: ["components", "."], - }), - ); - }), - ); + const output = String( + yield* scoped(function* () { + return yield* collect( + yield* execute({ + path: "doc.md", + stream, + includes: ["components", "."], + }), + ); + }), + ); - expect(output).toContain("second"); - }); + expect(output).toContain("second"); + }, + ); }); diff --git a/packages/core/tests/eval-bindings.test.ts b/packages/core/tests/eval-bindings.test.ts index c11817c84..08f35155f 100644 --- a/packages/core/tests/eval-bindings.test.ts +++ b/packages/core/tests/eval-bindings.test.ts @@ -17,12 +17,14 @@ import { asText, completion, failureMessage } from "./helpers.ts"; import type { Json } from "../src/types.ts"; import type { DurableEvent } from "@executablemd/durable-streams"; import type { Operation } from "effection"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/eval-bindings.test.ts"); describe("Tier T5 — Binding environment", () => { beforeAll(() => useTempFileCompiler()); // T38: Block 2 reads binding exported by Block 1 via env preamble - it("T38: block 2 reads binding from block 1", function* () { - const stream = new InMemoryStream(); + journalCase("T38: block 2 reads binding from block 1", "test.md", function* (stream) { yield* useStubFs({ "test.md": "```js eval\nconst port = 8080;\n```\n\n```js eval\nconst url = 'http://localhost:' + port;\n```\n", @@ -46,30 +48,32 @@ describe("Tier T5 — Binding environment", () => { }); // T39: Block 3 shadowing Block 1's binding — downstream sees Block 3's value - it("T39: shadowing — later block overrides earlier binding", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "test.md": - "```js eval\nconst x = 1;\n```\n\n```js eval\nconst x = 2;\n```\n\n```js eval\nconst y = x;\n```\n", - }); - yield* useEchoExec(); - - // Should succeed — x is 2 in block 3 (shadowed by block 2) - const output = asText( - yield* collect( - yield* execute({ - path: "test.md", - stream, - }), - ), - ); - - expect(output).not.toContain("ERROR"); - }); + journalCase( + "T39: shadowing — later block overrides earlier binding", + "test.md", + function* (stream) { + yield* useStubFs({ + "test.md": + "```js eval\nconst x = 1;\n```\n\n```js eval\nconst x = 2;\n```\n\n```js eval\nconst y = x;\n```\n", + }); + yield* useEchoExec(); + + // Should succeed — x is 2 in block 3 (shadowed by block 2) + const output = asText( + yield* collect( + yield* execute({ + path: "test.md", + stream, + }), + ), + ); + + expect(output).not.toContain("ERROR"); + }, + ); // T40: Empty block — no exports, no error - it("T40: empty eval block — no exports, no error", function* () { - const stream = new InMemoryStream(); + journalCase("T40: empty eval block — no exports, no error", "test.md", function* (stream) { yield* useStubFs({ "test.md": "```js eval\n\n```\n", }); @@ -89,8 +93,7 @@ describe("Tier T5 — Binding environment", () => { // T41: Block referencing undeclared binding not in env — nothing recovers it, // so the run's outcome is that failure. - it("T41: undeclared reference → the run fails", function* () { - const stream = new InMemoryStream(); + journalCase("T41: undeclared reference → the run fails", "test.md", function* (stream) { yield* useStubFs({ "test.md": "```js eval\nconst y = undeclaredVar + 1;\n```\n", }); @@ -103,8 +106,7 @@ describe("Tier T5 — Binding environment", () => { }); // T42: Syntax error in block — parse-time error before execution - it("T42: syntax error → the run fails", function* () { - const stream = new InMemoryStream(); + journalCase("T42: syntax error → the run fails", "test.md", function* (stream) { yield* useStubFs({ "test.md": "```js eval\nconst x = ;\n```\n", }); diff --git a/packages/core/tests/eval-error-mode.test.ts b/packages/core/tests/eval-error-mode.test.ts index c57dd6c42..0555a1d51 100644 --- a/packages/core/tests/eval-error-mode.test.ts +++ b/packages/core/tests/eval-error-mode.test.ts @@ -9,14 +9,16 @@ * the block projects. */ -import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; +import { beforeAll, describe } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; -import { InMemoryStream } from "@executablemd/durable-streams"; import { useEchoExec, useStubFs } from "@executablemd/runtime/test"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { DocumentationError } from "../src/errors.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/eval-error-mode.test.ts"); const PROJECTING_BLOCK = [ "```ts persist eval", @@ -30,332 +32,365 @@ describe("Tier O — Eval scope hierarchy", () => { // O23: the block sits in documentation, so its projection settles under the // throwing error mode — the failure aborts the body instead of rendering. - it("O23: a persistent projection in documentation settles under the throwing error mode", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Wrap.md": [...PROJECTING_BLOCK, "", "", "done", ""].join("\n"), - "doc.md": "\n\n", - }); - yield* useEchoExec(); - - let failure: unknown; - try { - yield* collect(yield* execute({ path: "doc.md", stream })); - } catch (error) { - failure = error; - } - - // Documentation fail-fast: the projected error aborts the body rather than - // rendering, which a printing error mode would never do. - expect(failure).toBeInstanceOf(DocumentationError); - expect(String(failure)).toContain("Missing"); - }); + journalCase( + "O23: a persistent projection in documentation settles under the throwing error mode", + "doc.md", + function* (stream) { + yield* useStubFs({ + "components/Wrap.md": [...PROJECTING_BLOCK, "", "", "done", ""].join("\n"), + "doc.md": "\n\n", + }); + yield* useEchoExec(); + + let failure: unknown; + try { + yield* collect(yield* execute({ path: "doc.md", stream })); + } catch (error) { + failure = error; + } + + // Documentation fail-fast: the projected error aborts the body rather than + // rendering, which a printing error mode would never do. + expect(failure).toBeInstanceOf(DocumentationError); + expect(String(failure)).toContain("Missing"); + }, + ); // O28: a claimed in a documentation region settles under the // throwing error mode — the projected error stops the body instead of being // printed and then discarded with the region's rendered output. - it("O28: Markdown in documentation fails fast on a projected error", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Wrap.md": ["", "", "", "done", ""].join("\n"), - "doc.md": "\n\n", - }); - yield* useEchoExec(); - - let failure: unknown; - try { - yield* collect(yield* execute({ path: "doc.md", stream })); - } catch (error) { - failure = error; - } - - expect(failure).toBeInstanceOf(DocumentationError); - expect(String(failure)).toContain("Missing"); - }); + journalCase( + "O28: Markdown in documentation fails fast on a projected error", + "doc.md", + function* (stream) { + yield* useStubFs({ + "components/Wrap.md": ["", "", "", "done", ""].join("\n"), + "doc.md": "\n\n", + }); + yield* useEchoExec(); + + let failure: unknown; + try { + yield* collect(yield* execute({ path: "doc.md", stream })); + } catch (error) { + failure = error; + } + + expect(failure).toBeInstanceOf(DocumentationError); + expect(String(failure)).toContain("Missing"); + }, + ); // O29: the same projection inside carries the region's error mode, // so the projected error fails the region and nothing after it renders. - it("O29: Markdown inside fails the region on a projected error", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Wrap.md": ["", "", "", "done", ""].join("\n"), - "doc.md": "\n\n", - }); - yield* useEchoExec(); - - let failure: unknown; - try { - yield* collect(yield* execute({ path: "doc.md", stream })); - } catch (error) { - failure = error; - } - - expect(String(failure)).toContain("Missing"); - }); + journalCase( + "O29: Markdown inside fails the region on a projected error", + "doc.md", + function* (stream) { + yield* useStubFs({ + "components/Wrap.md": ["", "", "", "done", ""].join("\n"), + "doc.md": "\n\n", + }); + yield* useEchoExec(); + + let failure: unknown; + try { + yield* collect(yield* execute({ path: "doc.md", stream })); + } catch (error) { + failure = error; + } + + expect(String(failure)).toContain("Missing"); + }, + ); // O30: value-component documentation carries the same error mode, so a claimed // that projects an error fails fast instead of being discarded // along with the documentation's rendered output. - it("O30: a projected error in value-component documentation fails fast", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Value.md": [ - "---", - "returns:", - " ok: { type: boolean }", - "---", - "", - "", - "", - '', - ].join("\n"), - "doc.md": '\n\n\n\nafter', - }); - yield* useEchoExec(); - - let failure: unknown; - try { - yield* collect(yield* execute({ path: "doc.md", stream })); - } catch (error) { - failure = error; - } - - expect(failure).toBeInstanceOf(DocumentationError); - expect(String(failure)).toContain("Missing"); - }); + journalCase( + "O30: a projected error in value-component documentation fails fast", + "doc.md", + function* (stream) { + yield* useStubFs({ + "components/Value.md": [ + "---", + "returns:", + " ok: { type: boolean }", + "---", + "", + "", + "", + '', + ].join("\n"), + "doc.md": '\n\n\n\nafter', + }); + yield* useEchoExec(); + + let failure: unknown; + try { + yield* collect(yield* execute({ path: "doc.md", stream })); + } catch (error) { + failure = error; + } + + expect(failure).toBeInstanceOf(DocumentationError); + expect(String(failure)).toContain("Missing"); + }, + ); // O25: an evaluation sees the bindings that existed when it started, and its // declared exports become shared once it completes. - it("O25: exports are committed to the shared bindings for later blocks", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "doc.md": [ - "```js eval", - "const port = 4321;", - // A function is not JSON, so the journal cannot carry it: only the - // explicit commit makes it reachable from the next block. - "const describe = (n) => `port-${n}`;", - "```", - "", - "```js eval", - "output(`port=${port} ${describe(port)}`);", - "```", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - // `describe` is a function — `serializeExports` drops it, so reaching it - // from the next block proves the commit publishes live values. - expect(output).toContain("port=4321 port-4321"); - }); + journalCase( + "O25: exports are committed to the shared bindings for later blocks", + "doc.md", + function* (stream) { + yield* useStubFs({ + "doc.md": [ + "```js eval", + "const port = 4321;", + // A function is not JSON, so the journal cannot carry it: only the + // explicit commit makes it reachable from the next block. + "const describe = (n) => `port-${n}`;", + "```", + "", + "```js eval", + "output(`port=${port} ${describe(port)}`);", + "```", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + // `describe` is a function — `serializeExports` drops it, so reaching it + // from the next block proves the commit publishes live values. + expect(output).toContain("port=4321 port-4321"); + }, + ); // O26: persistent work keeps the values and the mode-bound capabilities it // captured; a later evaluation gets its own snapshot and cannot reach in. - it("O26: a later evaluation cannot alter an earlier block's captured bindings", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Wrap.md": [ - "```js persist eval", - "const captured = { seen: null };", - "const renderer = renderChildren;", - "yield* spawn(function*() {", - " yield* sleep(5);", - " captured.seen = typeof renderer;", - "});", - "```", - "", - "```js eval", - "const renderer = null;", - "```", - "", - "```js eval", - "yield* when(function*() {", - ' if (captured.seen === null) throw new Error("not yet");', - "});", - "output(`seen=${captured.seen}`);", - "```", - ].join("\n"), - "doc.md": "\ncontent\n", - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - // The middle block rebinding `renderer` did not reach the closure the - // persistent task captured from its own snapshot. - expect(output).toContain("seen=function"); - }); + journalCase( + "O26: a later evaluation cannot alter an earlier block's captured bindings", + "doc.md", + function* (stream) { + yield* useStubFs({ + "components/Wrap.md": [ + "```js persist eval", + "const captured = { seen: null };", + "const renderer = renderChildren;", + "yield* spawn(function*() {", + " yield* sleep(5);", + " captured.seen = typeof renderer;", + "});", + "```", + "", + "```js eval", + "const renderer = null;", + "```", + "", + "```js eval", + "yield* when(function*() {", + ' if (captured.seen === null) throw new Error("not yet");', + "});", + "output(`seen=${captured.seen}`);", + "```", + ].join("\n"), + "doc.md": "\ncontent\n", + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + // The middle block rebinding `renderer` did not reach the closure the + // persistent task captured from its own snapshot. + expect(output).toContain("seen=function"); + }, + ); // O27: an explicit import shadows the injected binding without the preamble // declaring it twice — the module would not compile otherwise. - it("O27: an explicitly imported useContent compiles alongside the injected one", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Wrap.md": [ - "```ts eval", - 'import { useContent } from "@executablemd/core";', - "const kind = typeof useContent;", - "output(`imported=${kind}`);", - "```", - ].join("\n"), - "doc.md": "\ncontent\n", - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - expect(output).toContain("imported=function"); - expect(output).not.toContain("ERROR"); - }); + journalCase( + "O27: an explicitly imported useContent compiles alongside the injected one", + "doc.md", + function* (stream) { + yield* useStubFs({ + "components/Wrap.md": [ + "```ts eval", + 'import { useContent } from "@executablemd/core";', + "const kind = typeof useContent;", + "output(`imported=${kind}`);", + "```", + ].join("\n"), + "doc.md": "\ncontent\n", + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + expect(output).toContain("imported=function"); + expect(output).not.toContain("ERROR"); + }, + ); // O24: the same block inside an region settles under `output`, so // the projected error fails the run rather than rendering as a comment. - it("O24: a persistent projection inside settles under the output error mode", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Wrap.md": ["", ...PROJECTING_BLOCK, ""].join("\n"), - "doc.md": "\n\n", - }); - yield* useEchoExec(); - - let failure: unknown; - try { - yield* collect(yield* execute({ path: "doc.md", stream })); - } catch (error) { - failure = error; - } - - expect(String(failure)).toContain("Missing"); - }); + journalCase( + "O24: a persistent projection inside settles under the output error mode", + "doc.md", + function* (stream) { + yield* useStubFs({ + "components/Wrap.md": ["", ...PROJECTING_BLOCK, ""].join("\n"), + "doc.md": "\n\n", + }); + yield* useEchoExec(); + + let failure: unknown; + try { + yield* collect(yield* execute({ path: "doc.md", stream })); + } catch (error) { + failure = error; + } + + expect(String(failure)).toContain("Missing"); + }, + ); // O31: the same printing projection, captured — the string rendered the // error away, so the capture is refused (§6.5) and the recorded segment // returns to the caller instead of hiding inside the bound string. - it("O31: a captured markdown projection refuses the binding on a projected error", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Wrap.md": [ - "", - "", - ...PROJECTING_BLOCK, - "", - "", - ].join("\n"), - "doc.md": '\n\n\n\nvalue:{cap}:end', - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - expect(output).toContain("ERROR"); - // The binding stays unset, so the interpolation stays literal and the - // sibling text still renders under the printing error mode. - expect(output).toContain("value:{cap}:end"); - // Reported once on the way out, not again when it crosses back. - expect(String(output).split("Cannot resolve component: Missing").length - 1).toBe(1); - }); + journalCase( + "O31: a captured markdown projection refuses the binding on a projected error", + "doc.md", + function* (stream) { + yield* useStubFs({ + "components/Wrap.md": [ + "", + "", + ...PROJECTING_BLOCK, + "", + "", + ].join("\n"), + "doc.md": '\n\n\n\nvalue:{cap}:end', + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + expect(output).toContain("ERROR"); + // The binding stays unset, so the interpolation stays literal and the + // sibling text still renders under the printing error mode. + expect(output).toContain("value:{cap}:end"); + // Reported once on the way out, not again when it crosses back. + expect(String(output).split("Cannot resolve component: Missing").length - 1).toBe(1); + }, + ); // O32: a caught DocumentationError is explicit recovery. The throw-bound // projection fails before anything is recorded for the capture refusal, so // the recovered component completes and the capture succeeds. - it("O32: a caught projection error recovers without a later capture refusal", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Wrap.md": [ - "```ts eval", - "const projected = yield* call(function* () {", - " try {", - " return yield* renderChildren();", - " } catch (error) {", - ' return "recovered";', - " }", - "});", - "```", - "", - "", - "```ts eval", - "output(projected);", - "```", - "", - ].join("\n"), - "doc.md": '\n\n\n\nvalue:{cap}:end', - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - // The caught error is not reasserted: the binding holds the recovered - // output and no error segment reaches the caller. - expect(output).toContain("recovered"); - expect(output).not.toContain("{cap}"); - expect(output).not.toContain("ERROR"); - }); + journalCase( + "O32: a caught projection error recovers without a later capture refusal", + "doc.md", + function* (stream) { + yield* useStubFs({ + "components/Wrap.md": [ + "```ts eval", + "const projected = yield* call(function* () {", + " try {", + " return yield* renderChildren();", + " } catch (error) {", + ' return "recovered";', + " }", + "});", + "```", + "", + "", + "```ts eval", + "output(projected);", + "```", + "", + ].join("\n"), + "doc.md": '\n\n\n\nvalue:{cap}:end', + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + // The caught error is not reasserted: the binding holds the recovered + // output and no error segment reaches the caller. + expect(output).toContain("recovered"); + expect(output).not.toContain("{cap}"); + expect(output).not.toContain("ERROR"); + }, + ); // O33: work the projected content started is torn down with its scope, not // leaked past the invocation because the projection's error was caught. - it("O33: projection-owned work does not escape a caught projection error", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Wrap.md": [ - "```ts eval", - "const projected = yield* call(function* () {", - " try {", - " return yield* renderChildren();", - " } catch (error) {", - ' return "recovered";', - " }", - "});", - "```", - "", - "", - "```ts eval", - "output(projected);", - "```", - "", - ].join("\n"), - "doc.md": [ - "```js eval", - "const probe = { started: false, halted: false };", - "```", - "", - '', - "```js persist eval", - "yield* spawn(function*() {", - " probe.started = true;", - " try {", - " yield* suspend();", - " } finally {", - " probe.halted = true;", - " }", - "});", - "```", - "", - "```js eval", - "yield* when(function*() {", - ' if (!probe.started) throw new Error("not yet");', - "});", - "```", - "", - "", - "", - "", - "value:{cap}:end", - "", - "```js eval", - "output(`halted=${probe.halted}`);", - "```", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - // The suspended task the children spawned is gone by the time the sibling - // runs: recovery completed, and catching the error leaked nothing. - expect(output).toContain("recovered"); - expect(output).toContain("halted=true"); - expect(output).not.toContain("ERROR"); - }); + journalCase( + "O33: projection-owned work does not escape a caught projection error", + "doc.md", + function* (stream) { + yield* useStubFs({ + "components/Wrap.md": [ + "```ts eval", + "const projected = yield* call(function* () {", + " try {", + " return yield* renderChildren();", + " } catch (error) {", + ' return "recovered";', + " }", + "});", + "```", + "", + "", + "```ts eval", + "output(projected);", + "```", + "", + ].join("\n"), + "doc.md": [ + "```js eval", + "const probe = { started: false, halted: false };", + "```", + "", + '', + "```js persist eval", + "yield* spawn(function*() {", + " probe.started = true;", + " try {", + " yield* suspend();", + " } finally {", + " probe.halted = true;", + " }", + "});", + "```", + "", + "```js eval", + "yield* when(function*() {", + ' if (!probe.started) throw new Error("not yet");', + "});", + "```", + "", + "", + "", + "", + "value:{cap}:end", + "", + "```js eval", + "output(`halted=${probe.halted}`);", + "```", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + // The suspended task the children spawned is gone by the time the sibling + // runs: recovery completed, and catching the error leaked nothing. + expect(output).toContain("recovered"); + expect(output).toContain("halted=true"); + expect(output).not.toContain("ERROR"); + }, + ); }); diff --git a/packages/core/tests/eval-return.test.ts b/packages/core/tests/eval-return.test.ts index 6db39cdbb..f23f11600 100644 --- a/packages/core/tests/eval-return.test.ts +++ b/packages/core/tests/eval-return.test.ts @@ -12,12 +12,14 @@ import { InMemoryStream } from "@executablemd/durable-streams"; import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/eval-return.test.ts"); describe("Eval block return value", () => { beforeAll(() => useTempFileCompiler()); // ER1: return string → rendered output - it("ER1: return string → rendered output", function* () { - const stream = new InMemoryStream(); + journalCase("ER1: return string → rendered output", "test.md", function* (stream) { yield* useStubFs({ "test.md": '```js eval\nreturn "hello from eval";\n```\n', }); @@ -34,8 +36,7 @@ describe("Eval block return value", () => { }); // ER2: return number → coerced to string - it("ER2: return number → coerced to string", function* () { - const stream = new InMemoryStream(); + journalCase("ER2: return number → coerced to string", "test.md", function* (stream) { yield* useStubFs({ "test.md": "```js eval\nreturn 42;\n```\n", }); @@ -52,8 +53,7 @@ describe("Eval block return value", () => { }); // ER3: return null → no output - it("ER3: return null → no output", function* () { - const stream = new InMemoryStream(); + journalCase("ER3: return null → no output", "test.md", function* (stream) { yield* useStubFs({ "test.md": "before\n```js eval\nreturn null;\n```\nafter", }); @@ -71,8 +71,7 @@ describe("Eval block return value", () => { }); // ER4: return undefined → no output - it("ER4: return undefined → no output", function* () { - const stream = new InMemoryStream(); + journalCase("ER4: return undefined → no output", "test.md", function* (stream) { yield* useStubFs({ "test.md": "before\n```js eval\nreturn undefined;\n```\nafter", }); @@ -89,8 +88,7 @@ describe("Eval block return value", () => { }); // ER5: output() takes precedence over return - it("ER5: output() takes precedence over return", function* () { - const stream = new InMemoryStream(); + journalCase("ER5: output() takes precedence over return", "test.md", function* (stream) { yield* useStubFs({ "test.md": '```js eval\noutput("explicit");\nreturn "ignored";\n```\n', }); @@ -108,8 +106,7 @@ describe("Eval block return value", () => { }); // ER6: return in persist eval → works same as normal - it("ER6: return in persist eval → works same as normal", function* () { - const stream = new InMemoryStream(); + journalCase("ER6: return in persist eval → works same as normal", "test.md", function* (stream) { yield* useStubFs({ "test.md": '```js persist eval\nreturn "persisted output";\n```\n', }); @@ -126,8 +123,7 @@ describe("Eval block return value", () => { }); // ER7: no return → no output (existing behavior) - it("ER7: no return → no output (existing behavior)", function* () { - const stream = new InMemoryStream(); + journalCase("ER7: no return → no output (existing behavior)", "test.md", function* (stream) { yield* useStubFs({ "test.md": "before\n```js eval\nconst x = 42;\n```\nafter", }); diff --git a/packages/core/tests/eval-scope.test.ts b/packages/core/tests/eval-scope.test.ts index 14f937f5a..7dc8f9285 100644 --- a/packages/core/tests/eval-scope.test.ts +++ b/packages/core/tests/eval-scope.test.ts @@ -4,21 +4,22 @@ * Tests that eval scopes are properly scoped to components and * that child/parent scope relationships work correctly. */ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { describe, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; -import { InMemoryStream } from "@executablemd/durable-streams"; import { Stdio } from "@effectionx/process"; import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { asText } from "./helpers.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/eval-scope.test.ts"); describe("Tier T10 — eval-scope hierarchy", () => { beforeAll(() => useTempFileCompiler()); // T61: Document with eval blocks — scope created per document - it("T61: eval blocks run within document scope", function* () { - const stream = new InMemoryStream(); + journalCase("T61: eval blocks run within document scope", "test.md", function* (stream) { yield* useStubFs({ "test.md": "```js eval\nconst a = 1;\n```\n\n```js eval\nconst b = a + 1;\n```\n", }); @@ -40,8 +41,7 @@ describe("Tier T10 — eval-scope hierarchy", () => { }); // T62: Eval blocks coexist with exec blocks - it("T62: eval and exec blocks coexist", function* () { - const stream = new InMemoryStream(); + journalCase("T62: eval and exec blocks coexist", "test.md", function* (stream) { yield* useStubFs({ "test.md": "```js eval\nconst x = 42;\n```\n\n```bash exec\necho hello\n```\n\n```js eval\nconst y = x + 1;\n```\n", diff --git a/packages/core/tests/eval-timeout.test.ts b/packages/core/tests/eval-timeout.test.ts index 4e01570d9..6c6bb6e16 100644 --- a/packages/core/tests/eval-timeout.test.ts +++ b/packages/core/tests/eval-timeout.test.ts @@ -10,18 +10,19 @@ import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; -import { InMemoryStream } from "@executablemd/durable-streams"; import { Config } from "@executablemd/runtime"; import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { completion, failureMessage } from "./helpers.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/eval-timeout.test.ts"); describe("Tier T7 — timeout modifier", () => { beforeAll(() => useTempFileCompiler()); - it("T49: block completes before timeout → success", function* () { - const stream = new InMemoryStream(); + journalCase("T49: block completes before timeout → success", "test.md", function* (stream) { yield* useStubFs({ "test.md": "```js timeout=30s eval\nconst x = 42;\n```\n", }); @@ -38,60 +39,72 @@ describe("Tier T7 — timeout modifier", () => { expect(output).not.toContain("ERROR"); }); - it("T50: a bare timeout boxes the block at the run's exec default", function* () { - const stream = new InMemoryStream(); - yield* Config.around({ timeoutExec: () => 30_000 }, { at: "min" }); - yield* useStubFs({ - "test.md": "```js timeout eval\nconst x = 1;\n```\n", - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).not.toContain("timed out"); - }); - - it("T50a: a bare timeout with no exec default refuses instead of running unbounded", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "test.md": "```js timeout eval\nconst x = 1;\n```\n", - }); - - const result = yield* completion({ path: "test.md", stream }); - - expect(result.ok).toBe(false); - expect(failureMessage(result)).toContain("names no duration"); - }); + journalCase( + "T50: a bare timeout boxes the block at the run's exec default", + "test.md", + function* (stream) { + yield* Config.around({ timeoutExec: () => 30_000 }, { at: "min" }); + yield* useStubFs({ + "test.md": "```js timeout eval\nconst x = 1;\n```\n", + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).not.toContain("timed out"); + }, + ); + + journalCase( + "T50a: a bare timeout with no exec default refuses instead of running unbounded", + "test.md", + function* (stream) { + yield* useStubFs({ + "test.md": "```js timeout eval\nconst x = 1;\n```\n", + }); + + const result = yield* completion({ path: "test.md", stream }); + + expect(result.ok).toBe(false); + expect(failureMessage(result)).toContain("names no duration"); + }, + ); /** * The timebox itself, at the value the run configured: an eval block that * sleeps past it is cancelled, and the message names the duration used. */ - it("T57: the timebox of a bare timeout is the run's exec default", function* () { - const stream = new InMemoryStream(); - yield* Config.around({ timeoutExec: () => 25 }, { at: "min" }); - yield* useStubFs({ - "test.md": '```ts timeout eval\nyield* sleep(300);\noutput("SLEPT");\n```\n', - }); - - const result = yield* completion({ path: "test.md", stream }); - - expect(result.ok).toBe(false); - expect(failureMessage(result)).toContain("eval block timed out after 25ms"); - expect(failureMessage(result)).not.toContain("SLEPT"); - }); - - it("T58: a declared duration boxes the block at what it declared", function* () { - const stream = new InMemoryStream(); - yield* Config.around({ timeoutExec: () => 60_000 }, { at: "min" }); - yield* useStubFs({ - "test.md": '```ts timeout=25ms eval\nyield* sleep(300);\noutput("SLEPT");\n```\n', - }); - - const result = yield* completion({ path: "test.md", stream }); - - expect(result.ok).toBe(false); - expect(failureMessage(result)).toContain("eval block timed out after 25ms"); - expect(failureMessage(result)).not.toContain("SLEPT"); - }); + journalCase( + "T57: the timebox of a bare timeout is the run's exec default", + "test.md", + function* (stream) { + yield* Config.around({ timeoutExec: () => 25 }, { at: "min" }); + yield* useStubFs({ + "test.md": '```ts timeout eval\nyield* sleep(300);\noutput("SLEPT");\n```\n', + }); + + const result = yield* completion({ path: "test.md", stream }); + + expect(result.ok).toBe(false); + expect(failureMessage(result)).toContain("eval block timed out after 25ms"); + expect(failureMessage(result)).not.toContain("SLEPT"); + }, + ); + + journalCase( + "T58: a declared duration boxes the block at what it declared", + "test.md", + function* (stream) { + yield* Config.around({ timeoutExec: () => 60_000 }, { at: "min" }); + yield* useStubFs({ + "test.md": '```ts timeout=25ms eval\nyield* sleep(300);\noutput("SLEPT");\n```\n', + }); + + const result = yield* completion({ path: "test.md", stream }); + + expect(result.ok).toBe(false); + expect(failureMessage(result)).toContain("eval block timed out after 25ms"); + expect(failureMessage(result)).not.toContain("SLEPT"); + }, + ); }); diff --git a/packages/core/tests/execute.test.ts b/packages/core/tests/execute.test.ts index d051bdb0d..a07ec4061 100644 --- a/packages/core/tests/execute.test.ts +++ b/packages/core/tests/execute.test.ts @@ -26,6 +26,9 @@ import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { asText } from "./helpers.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/execute.test.ts"); /** * A stand-in child that behaves like one: it writes its output to the display @@ -206,19 +209,22 @@ describe("Tier B — durable import", () => { }); // B9: import missing component — uncaught at the root, so it fails the run - it("B9: import missing component — the run fails and reports it", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ "README.md": "\n" }); - yield* useStubExec(); - - const message = yield* failure(function* () { - return yield* collect(yield* execute({ path: "README.md", stream })); - }); + journalCase( + "B9: import missing component — the run fails and reports it", + "README.md", + function* (stream) { + yield* useStubFs({ "README.md": "\n" }); + yield* useStubExec(); + + const message = yield* failure(function* () { + return yield* collect(yield* execute({ path: "README.md", stream })); + }); - expect( - message.includes("Cannot resolve component") || message.includes("Failed to import"), - ).toBeTruthy(); - }); + expect( + message.includes("Cannot resolve component") || message.includes("Failed to import"), + ).toBeTruthy(); + }, + ); // B12: root document as component — __root__ import, same journal shape it("B12: root document imported as __root__", function* () { @@ -282,9 +288,9 @@ describe("Tier B — durable import", () => { }); // B15: default resolver middleware — resolves via stat in search path order - it("B15: resolver searches component dirs in order", function* () { + journalCase("B15: resolver searches component dirs in order", "README.md", function* (stream) { // Component exists in root dir (second search path), not components/ - const stream = new InMemoryStream(); + yield* useStubFs({ "README.md": "\n", "Banner.md": "Banner from root\n", @@ -367,8 +373,7 @@ describe("Tier D — code execution and modifiers", () => { }); // D3: a nonzero exit is a checked failure, with or without (#441) - it("D3: non-zero exit code → the run fails", function* () { - const stream = new InMemoryStream(); + journalCase("D3: non-zero exit code → the run fails", "README.md", function* (stream) { yield* useStubFs({ "README.md": ["```bash exec", "failing-command", "```"].join("\n") }); yield* useFailingExec(1, "command not found"); @@ -382,23 +387,26 @@ describe("Tier D — code execution and modifiers", () => { // D3b: the same rule for a command that printed before it failed (#307), run // as a real subprocess so the exit code travels the whole way through // `execFactory` rather than a stub's return value. - it("D3b: non-zero exit with stdout → output, then the printed error", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "README.md": ["```bash exec", "echo partial; exit 1", "```"].join("\n"), - }); + journalCase( + "D3b: non-zero exit with stdout → output, then the printed error", + "README.md", + function* (stream) { + yield* useStubFs({ + "README.md": ["```bash exec", "echo partial; exit 1", "```"].join("\n"), + }); - const run = yield* displayed(function* () { - return yield* failure(function* () { - return yield* collect(yield* execute({ path: "README.md", stream })); + const run = yield* displayed(function* () { + return yield* failure(function* () { + return yield* collect(yield* execute({ path: "README.md", stream })); + }); }); - }); - // What the command printed before it failed reached the reader; the run - // then fails rather than rendering the diagnostic and carrying on (#441). - expect(run.stdout).toContain("partial"); - expect(run.value).toContain("Command failed (exit 1)"); - }); + // What the command printed before it failed reached the reader; the run + // then fails rather than rendering the diagnostic and carrying on (#441). + expect(run.stdout).toContain("partial"); + expect(run.value).toContain("Command failed (exit 1)"); + }, + ); // D4: multi-line command — full script passed to -c it("D4: multi-line command — full script in journal", function* () { @@ -597,88 +605,97 @@ describe("Tier D — code execution and modifiers", () => { }); // D15: unknown modifier in chain → error, uncaught, so the run fails - it("D15: unknown modifier → the run fails and reports it", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "README.md": ["```bash frobnicate exec", "echo test", "```"].join("\n"), - }); - yield* useStubExec(); + journalCase( + "D15: unknown modifier → the run fails and reports it", + "README.md", + function* (stream) { + yield* useStubFs({ + "README.md": ["```bash frobnicate exec", "echo test", "```"].join("\n"), + }); + yield* useStubExec(); - const message = yield* failure(function* () { - return yield* collect(yield* execute({ path: "README.md", stream })); - }); + const message = yield* failure(function* () { + return yield* collect(yield* execute({ path: "README.md", stream })); + }); - expect(message.includes("Unknown modifier") || message.includes("frobnicate")).toBeTruthy(); - }); + expect(message.includes("Unknown modifier") || message.includes("frobnicate")).toBeTruthy(); + }, + ); // D16: no terminal modifier → error - it("D16: no terminal modifier — code block without exec/eval is passive text", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "README.md": ["```bash", "echo test", "```"].join("\n"), - }); - yield* useStubExec(); + journalCase( + "D16: no terminal modifier — code block without exec/eval is passive text", + "README.md", + function* (stream) { + yield* useStubFs({ + "README.md": ["```bash", "echo test", "```"].join("\n"), + }); + yield* useStubExec(); - const result = asText( - yield* collect( - yield* execute({ - path: "README.md", - stream, - }), - ), - ); + const result = asText( + yield* collect( + yield* execute({ + path: "README.md", + stream, + }), + ), + ); - // Without exec/eval, code block is passive text — preserved as-is - expect(result).toContain("```bash"); - expect(result).toContain("echo test"); - }); + // Without exec/eval, code block is passive text — preserved as-is + expect(result).toContain("```bash"); + expect(result).toContain("echo test"); + }, + ); // D17: custom modifier registration - it("D17: custom modifier registration — handler runs in chain", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - // A forwarded block renders nothing, so the text a modifier transforms - // comes from the region that asked for it (#441). - "README.md": [ - '', - "```bash uppercase exec", - "echo hello", - "```", - "", - "", - "{shouted}", - ].join("\n"), - }); - yield* useStubExec(); + journalCase( + "D17: custom modifier registration — handler runs in chain", + "README.md", + function* (stream) { + yield* useStubFs({ + // A forwarded block renders nothing, so the text a modifier transforms + // comes from the region that asked for it (#441). + "README.md": [ + '', + "```bash uppercase exec", + "echo hello", + "```", + "", + "", + "{shouted}", + ].join("\n"), + }); + yield* useStubExec(); - const result = asText( - yield* collect( - yield* execute({ - path: "README.md", - stream, - modifiers: { - uppercase: (_params) => (_args, next) => - (function* () { - const inner = yield* next(); - return { - output: inner.output.toUpperCase(), - exitCode: inner.exitCode, - stderr: inner.stderr, - }; - })(), - }, - }), - ), - ); + const result = asText( + yield* collect( + yield* execute({ + path: "README.md", + stream, + modifiers: { + uppercase: (_params) => (_args, next) => + (function* () { + const inner = yield* next(); + return { + output: inner.output.toUpperCase(), + exitCode: inner.exitCode, + stderr: inner.stderr, + }; + })(), + }, + }), + ), + ); - expect(result).toContain("HELLO"); - }); + expect(result).toContain("HELLO"); + }, + ); // D19: modifier parsing — timeout=30s - it("D19: modifier with params parsed correctly", function* () { + journalCase("D19: modifier with params parsed correctly", "README.md", function* (stream) { // We test that the modifier receives its params let receivedParams: string | undefined; - const stream = new InMemoryStream(); + yield* useStubFs({ "README.md": ["```bash timeout=30s exec", "echo test", "```"].join("\n"), }); @@ -796,8 +813,7 @@ describe("execute", () => { }); // E6: Props flow through expansion - it("E6: validated props flow through expansion", function* () { - const stream = new InMemoryStream(); + journalCase("E6: validated props flow through expansion", "README.md", function* (stream) { yield* useStubFs({ "README.md": '\n', "components/Greeting.md": [ @@ -829,8 +845,7 @@ describe("execute", () => { }); // E7: Undeclared prop in full document — uncaught, so the run fails - it("E7: undeclared prop fails the run and reports it", function* () { - const stream = new InMemoryStream(); + journalCase("E7: undeclared prop fails the run and reports it", "README.md", function* (stream) { yield* useStubFs({ "README.md": '\n', "components/Badge.md": ["---", "color: blue", "---", "", "badge"].join("\n"), @@ -875,8 +890,7 @@ describe("execute", () => { }); // Simple text document — no components, no exec - it("simple text document — passthrough", function* () { - const stream = new InMemoryStream(); + journalCase("simple text document — passthrough", "README.md", function* (stream) { yield* useStubFs({ "README.md": "# Hello World\n\nThis is a test.\n" }); yield* useStubExec(); @@ -893,8 +907,7 @@ describe("execute", () => { }); // Default props applied - it("default props applied when not provided", function* () { - const stream = new InMemoryStream(); + journalCase("default props applied when not provided", "README.md", function* (stream) { yield* useStubFs({ "README.md": "\n", "components/Greeting.md": [ @@ -1035,36 +1048,39 @@ describe("execute", () => { }); // E10: unclosed bold across component boundary — healed - it("E10: unclosed bold across component boundary — healed in first segment", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "README.md": "This is **bold\n\nmore text\n", - "components/Greeting.md": "greeting\n", - }); - yield* useStubExec(); + journalCase( + "E10: unclosed bold across component boundary — healed in first segment", + "README.md", + function* (stream) { + yield* useStubFs({ + "README.md": "This is **bold\n\nmore text\n", + "components/Greeting.md": "greeting\n", + }); + yield* useStubExec(); - const result = asText( - yield* collect( - yield* execute({ - path: "README.md", - stream, - }), - ), - ); + const result = asText( + yield* collect( + yield* execute({ + path: "README.md", + stream, + }), + ), + ); - // The **bold should be healed (closed) before component expansion. - // remend appends closing ** after the text segment (including trailing \n). - // Result: "This is **bold\n**" + "greeting\n" + "\nmore text\n" - expect(result).toContain("greeting"); - expect(result).toContain("more text"); - - // The first text segment should be healed — it should contain both - // opening ** and closing ** (remend closes the unclosed bold). - // The closing ** appears after the newline: "**bold\n**" - const beforeComponent = result.split("greeting")[0]!; - const openCount = (beforeComponent.match(/\*\*/g) || []).length; - expect(openCount).toBe(2); - }); + // The **bold should be healed (closed) before component expansion. + // remend appends closing ** after the text segment (including trailing \n). + // Result: "This is **bold\n**" + "greeting\n" + "\nmore text\n" + expect(result).toContain("greeting"); + expect(result).toContain("more text"); + + // The first text segment should be healed — it should contain both + // opening ** and closing ** (remend closes the unclosed bold). + // The closing ** appears after the newline: "**bold\n**" + const beforeComponent = result.split("greeting")[0]!; + const openCount = (beforeComponent.match(/\*\*/g) || []).length; + expect(openCount).toBe(2); + }, + ); // E2 complex: full replay with component + exec — zero I/O it("E2 complex: full replay with component + exec — zero I/O on second run", function* () { @@ -1190,8 +1206,7 @@ describe("execute", () => { }); // Content slot with exec - it("Content slot + exec — both work together", function* () { - const stream = new InMemoryStream(); + journalCase("Content slot + exec — both work together", "README.md", function* (stream) { yield* useStubFs({ "README.md": ["", "```bash exec", "echo inside", "```", ""].join("\n"), "components/Wrapper.md": "BEFORE\n\nAFTER\n", @@ -1246,93 +1261,107 @@ describe("Component Api dispatch — journal shape", () => { }); describe("component-declared output — document workflow", () => { - it("applies to an imported component invoked from a streaming root", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "README.md": "Intro paragraph.\n\n\n", - "components/Warn.md": "Docs line.\n\n\nWARNING_SHOWN\n\n", - }); - yield* useStubExec(); - const output = yield* collect(yield* execute({ path: "README.md", stream })); - expect(output).toContain("Intro paragraph."); - expect(output).toContain("WARNING_SHOWN"); - expect(output).not.toContain("Docs line"); - }); + journalCase( + "applies to an imported component invoked from a streaming root", + "README.md", + function* (stream) { + yield* useStubFs({ + "README.md": "Intro paragraph.\n\n\n", + "components/Warn.md": "Docs line.\n\n\nWARNING_SHOWN\n\n", + }); + yield* useStubExec(); + const output = yield* collect(yield* execute({ path: "README.md", stream })); + expect(output).toContain("Intro paragraph."); + expect(output).toContain("WARNING_SHOWN"); + expect(output).not.toContain("Docs line"); + }, + ); - it("applies to a root document the same way", function* () { - const stream = new InMemoryStream(); + journalCase("applies to a root document the same way", "README.md", function* (stream) { yield* useStubFs({ "README.md": "Root docs.\n\n\nROOT_SHOWN\n\n" }); const output = yield* collect(yield* execute({ path: "README.md", stream })); expect(output).toContain("ROOT_SHOWN"); expect(output).not.toContain("Root docs"); }); - it("buffers a root with into a single emission after success", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ "README.md": "Root docs.\n\n\nROOT_SHOWN\n\n" }); - - const execution = yield* execute({ path: "README.md", stream }); - const chunks: string[] = []; - const full = yield* forEach(function* (chunk: string) { - chunks.push(chunk); - }, execution.output); - - expect(full).toContain("ROOT_SHOWN"); - expect(chunks).toHaveLength(1); - }); - - it("emits what a buffered root selected before its documentation failed", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "README.md": "\nSELECTED\n\n\n```bash exec\nfailing-command\n```\n", - }); - yield* useFailingExec(1, "command not found"); - - const execution = yield* execute({ path: "README.md", stream }); - const chunks: string[] = []; - yield* forEach(function* (chunk: string) { - chunks.push(chunk); - }, execution.output); - - const result = yield* execution; - expect(result.ok).toBe(false); - // The region completed before the documentation ran, so its text is part - // of what this run rendered — and a run that fails still hands over what it - // rendered (§6.9 Partial output). - expect(chunks.join("")).toContain("SELECTED"); - }); - - it("keeps per-segment streaming for roots without ", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - // Two rendering segments around a foreground block, whose own output - // goes to the reader rather than into the document (#441). - "README.md": "one\n\n```bash exec\necho between\n```\n\ntwo\n", - }); - yield* useStubExec(); - - const execution = yield* execute({ path: "README.md", stream }); - const chunks: string[] = []; - yield* forEach(function* (chunk: string) { - chunks.push(chunk); - }, execution.output); + journalCase( + "buffers a root with into a single emission after success", + "README.md", + function* (stream) { + yield* useStubFs({ "README.md": "Root docs.\n\n\nROOT_SHOWN\n\n" }); - expect(chunks.length).toBeGreaterThanOrEqual(2); - }); + const execution = yield* execute({ path: "README.md", stream }); + const chunks: string[] = []; + const full = yield* forEach(function* (chunk: string) { + chunks.push(chunk); + }, execution.output); - it("emits no output event for an empty buffered root ", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ "README.md": "Root docs.\n\n\n" }); + expect(full).toContain("ROOT_SHOWN"); + expect(chunks).toHaveLength(1); + }, + ); + + journalCase( + "emits what a buffered root selected before its documentation failed", + "README.md", + function* (stream) { + yield* useStubFs({ + "README.md": "\nSELECTED\n\n\n```bash exec\nfailing-command\n```\n", + }); + yield* useFailingExec(1, "command not found"); + + const execution = yield* execute({ path: "README.md", stream }); + const chunks: string[] = []; + yield* forEach(function* (chunk: string) { + chunks.push(chunk); + }, execution.output); + + const result = yield* execution; + expect(result.ok).toBe(false); + // The region completed before the documentation ran, so its text is part + // of what this run rendered — and a run that fails still hands over what it + // rendered (§6.9 Partial output). + expect(chunks.join("")).toContain("SELECTED"); + }, + ); + + journalCase( + "keeps per-segment streaming for roots without ", + "README.md", + function* (stream) { + yield* useStubFs({ + // Two rendering segments around a foreground block, whose own output + // goes to the reader rather than into the document (#441). + "README.md": "one\n\n```bash exec\necho between\n```\n\ntwo\n", + }); + yield* useStubExec(); - const execution = yield* execute({ path: "README.md", stream }); - const chunks: string[] = []; - const full = yield* forEach(function* (chunk: string) { - chunks.push(chunk); - }, execution.output); + const execution = yield* execute({ path: "README.md", stream }); + const chunks: string[] = []; + yield* forEach(function* (chunk: string) { + chunks.push(chunk); + }, execution.output); - expect(chunks).toHaveLength(0); - expect(full).toBe(""); - }); + expect(chunks.length).toBeGreaterThanOrEqual(2); + }, + ); + + journalCase( + "emits no output event for an empty buffered root ", + "README.md", + function* (stream) { + yield* useStubFs({ "README.md": "Root docs.\n\n\n" }); + + const execution = yield* execute({ path: "README.md", stream }); + const chunks: string[] = []; + const full = yield* forEach(function* (chunk: string) { + chunks.push(chunk); + }, execution.output); + + expect(chunks).toHaveLength(0); + expect(full).toBe(""); + }, + ); it("replays a buffered root to the same result", function* () { const stream = new InMemoryStream(); diff --git a/packages/core/tests/fixtures/held-host-journal.ts b/packages/core/tests/fixtures/held-host-journal.ts new file mode 100644 index 000000000..67cb90816 --- /dev/null +++ b/packages/core/tests/fixtures/held-host-journal.ts @@ -0,0 +1,126 @@ +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; +import { expect } from "@executablemd/test-support/expect"; +import { API, env } from "@executablemd/runtime"; +import { all, ensure, scoped, spawn, suspend, withResolvers } from "effection"; +import type { Operation } from "effection"; +import { join } from "node:path"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; +import { answerProvider } from "../support/answer-provider.ts"; +import { useSelection } from "../support/generated-selection.ts"; +import { useHeldHost, withHeldHost } from "../support/held-host.ts"; + +const OWNER = "packages/core/tests/fixtures/held-host-journal.ts"; + +function witness(name: string, body: () => Operation): void { + describe("held host journal " + name, () => { + beforeEach(function* () { + const root = yield* env("XMD_TEST_ARTIFACTS"); + if (!root) { + throw new Error("The held-host journal witness needs an artifact root."); + } + yield* API.Env.around({ + *env([variable], next) { + return variable === "XMD_TEST_ARTIFACTS" ? join(root, name) : yield* next(variable); + }, + }); + }); + beforeEach(journalBeforeEach(OWNER)); + it(name, body); + }); +} + +function* completeOverlappingRoots(): Operation { + const both = withResolvers(); + let started = 0; + const closed: string[] = []; + const sources = ["host/first.md", "host/second.md"]; + yield* all( + sources.map((path) => + (function* () { + const run = yield* withHeldHost( + { + root: { path, source: "\n" }, + *inside() { + yield* ensure(() => { + closed.push(path); + }); + }, + }, + function* (held) { + yield* held.started; + if (++started === sources.length) { + both.resolve(); + } + yield* both.operation; + }, + ); + expect(run.completion.ok).toBe(true); + })(), + ), + ); + expect(closed.sort()).toEqual(sources); +} + +witness("passing roots", completeOverlappingRoots); + +witness("late assertion", function* () { + yield* completeOverlappingRoots(); + expect("both held roots joined").toBe("deliberate held-host late assertion"); +}); + +witness("rejected finalizer", function* () { + let armed = false; + yield* ensure(() => { + if (armed) { + throw new Error("deliberate held-host case teardown"); + } + }); + yield* scoped(function* () { + const entered = withResolvers(); + let cleaned = false; + const held = yield* useHeldHost({ + root: { path: "host/teardown.md", source: "\n" }, + installations: [ + { + componentAnswers: [ + answerProvider("Hold", { + kind: "function", + name: "Hold", + props: { type: "object", properties: {}, additionalProperties: false }, + generated: { + "self-closing": { + identity: "https://executable.md/test/journal/hold", + effect: "read", + }, + }, + *fn(): Operation { + yield* ensure(() => { + cleaned = true; + }); + entered.resolve(); + yield* suspend(); + return ""; + }, + }), + ], + *install() { + yield* useSelection({ Hold: ["self-closing"] }); + }, + }, + ], + }); + const evaluation = yield* spawn(() => + held.execution.evaluateGeneratedXmdRoot({ + id: "held-journal", + source: "", + allow: ["read"], + }), + ); + yield* entered.operation; + yield* evaluation.halt(); + expect(cleaned).toBe(true); + held.release(); + expect((yield* held.watched).ok).toBe(true); + }); + armed = true; +}); diff --git a/packages/core/tests/generated-xmd-root.test.ts b/packages/core/tests/generated-xmd-root.test.ts index d39c15f21..3939cf2de 100644 --- a/packages/core/tests/generated-xmd-root.test.ts +++ b/packages/core/tests/generated-xmd-root.test.ts @@ -52,7 +52,7 @@ import { Documentation } from "../src/documentation-api.ts"; import { Execution } from "../src/execute.ts"; import { retainedSource } from "../src/root-source.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; -import { HELD_ROOT_PATH, withHeldHost } from "./support/held-host.ts"; +import { heldHostCases, HELD_ROOT_PATH, withHeldHost } from "./support/held-host.ts"; // Imported from the module rather than the host surface: there is no public // siteless evaluator, and these pinned factories are core-internal — the same // import main's own generated-xmd tier uses. @@ -73,6 +73,8 @@ import type { } from "../src/generated-xmd.ts"; import type { FunctionComponent, FunctionComponentDefinition, Json } from "../src/types.ts"; +const heldHostCase = heldHostCases("packages/core/tests/generated-xmd-root.test.ts"); + const ROOT_PATH = "repl/sidekick.md"; const ROOT_SOURCE = "The host evaluated a generated root.\n"; const URL_ONE = "https://api.example.test/one"; @@ -1767,55 +1769,59 @@ describe("993 generated nested roots", () => { describe("1178 generated root references and scope", () => { beforeAll(() => useTempFileCompiler()); - it("keeps root engine availability local across sibling roots and restores enclosing installed syntax", function* () { - const calls: string[] = []; - const names = ["If", "Output", "Content", "Return", "Banner", "BannerLine"]; - const named = ``; - const candidate = request(`${named}1178-ROOT`); - const run = yield* withHeldHost( - { - installations: [candidate.installation, syntaxInstallation(calls)], - root: { path: HELD_ROOT_PATH, source: `${named}` }, - }, - function* (held) { - const first = yield* held.execution.evaluateGeneratedXmdRoot(candidate.root); - if (!first.ok) { - throw first.error; - } - expect(first.value.output).toContain("1178-ROOT"); - for (const name of ["If", "Output"]) { - expectAvailability(first.value.output, name, true); - } - for (const name of ["Content", "Return", "Banner", "BannerLine"]) { - expectAvailability(first.value.output, name, false); - } - const second = yield* held.execution.evaluateGeneratedXmdRoot({ - id: "turn-2", - source: `{before}1178-CUT{reference}1178-CUT{after}`, - }); - if (!second.ok) { - throw second.error; - } - const [before, reference, after] = second.value.output.split("1178-CUT"); - expect(before).toBe(baseline1178["root-base.txt"]); - expect(after).toBe(baseline1178["root-base.txt"]); - expectAvailability(reference, "If", true); - const third = yield* held.execution.evaluateGeneratedXmdRoot({ - id: "turn-3", - source: "", - }); - if (!third.ok) { - throw third.error; - } - expect(third.value.output).toBe(baseline1178["root-base.txt"]); - }, - ); - if (!run.completion.ok) { - throw run.completion.error; - } - for (const name of names) { - expectAvailability(String(run.completion.value), name, true); - } - expect(calls).toEqual([]); - }); + heldHostCase( + "keeps root engine availability local across sibling roots and restores enclosing installed syntax", + function* (stream) { + const calls: string[] = []; + const names = ["If", "Output", "Content", "Return", "Banner", "BannerLine"]; + const named = ``; + const candidate = request(`${named}1178-ROOT`); + const run = yield* withHeldHost( + { + stream, + installations: [candidate.installation, syntaxInstallation(calls)], + root: { path: HELD_ROOT_PATH, source: `${named}` }, + }, + function* (held) { + const first = yield* held.execution.evaluateGeneratedXmdRoot(candidate.root); + if (!first.ok) { + throw first.error; + } + expect(first.value.output).toContain("1178-ROOT"); + for (const name of ["If", "Output"]) { + expectAvailability(first.value.output, name, true); + } + for (const name of ["Content", "Return", "Banner", "BannerLine"]) { + expectAvailability(first.value.output, name, false); + } + const second = yield* held.execution.evaluateGeneratedXmdRoot({ + id: "turn-2", + source: `{before}1178-CUT{reference}1178-CUT{after}`, + }); + if (!second.ok) { + throw second.error; + } + const [before, reference, after] = second.value.output.split("1178-CUT"); + expect(before).toBe(baseline1178["root-base.txt"]); + expect(after).toBe(baseline1178["root-base.txt"]); + expectAvailability(reference, "If", true); + const third = yield* held.execution.evaluateGeneratedXmdRoot({ + id: "turn-3", + source: "", + }); + if (!third.ok) { + throw third.error; + } + expect(third.value.output).toBe(baseline1178["root-base.txt"]); + }, + ); + if (!run.completion.ok) { + throw run.completion.error; + } + for (const name of names) { + expectAvailability(String(run.completion.value), name, true); + } + expect(calls).toEqual([]); + }, + ); }); diff --git a/packages/core/tests/host-generated-root.test.ts b/packages/core/tests/host-generated-root.test.ts index d235c195b..c1a2d780e 100644 --- a/packages/core/tests/host-generated-root.test.ts +++ b/packages/core/tests/host-generated-root.test.ts @@ -46,8 +46,17 @@ import { DurablePersistenceError, InMemoryStream, createDurableOperation, + parseDurableEvent, } from "@executablemd/durable-streams"; import type { DurableEvent } from "@executablemd/durable-streams"; +import { exists, readdir, readTextFile } from "@effectionx/fs"; +import { API } from "@executablemd/runtime"; +import { useTempDirectory } from "@executablemd/test-support/temp"; +import { join } from "node:path"; +import { execPath } from "node:process"; +import { fileURLToPath } from "node:url"; +import { cliRuntime, runShell, shellQuote } from "@executablemd/test-support/launch"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; import { contributeDocumentation, Documentation } from "../src/documentation-api.ts"; import { registerComponents } from "../src/components/registration.ts"; @@ -55,7 +64,7 @@ import { durabilityFailure } from "../src/errors.ts"; import { Execution, execute } from "../src/execute.ts"; import { retainedSource } from "../src/root-source.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; -import { HELD_ROOT_PATH, useHeldHost, withHeldHost } from "./support/held-host.ts"; +import { heldHostCases, HELD_ROOT_PATH, useHeldHost, withHeldHost } from "./support/held-host.ts"; import * as hostModule from "../host.ts"; import { executeInstalled, HostExecutionError } from "../host.ts"; // The request and the class it names are taken from the trusted host @@ -73,6 +82,164 @@ import type { Json } from "../src/types.ts"; import type { FunctionComponent } from "../src/types.ts"; import { formDispatcher } from "../src/invocation-identity.ts"; +const OWNER = "packages/core/tests/host-generated-root.test.ts"; +const heldHostCase = heldHostCases(OWNER); + +describe("held host failure journal", () => { + const name = "saves the held root after cleanup and an enclosing assertion failure"; + it(name, function* () { + const root = yield* useTempDirectory("xmd-held-journal-"); + const failure = new Error("post-execution assertion"); + function attempt(artifacts: string, check: () => void): Operation { + return scoped(function* () { + yield* API.Env.around({ + *env([variable], next) { + return variable === "XMD_TEST_ARTIFACTS" ? artifacts : yield* next(variable); + }, + }); + yield* captureTestJournal({ owner: OWNER, name }, HELD_ROOT_PATH, function* (stream) { + let cleaned = false; + const run = yield* withHeldHost( + { + stream, + *inside() { + yield* ensure(() => { + cleaned = true; + }); + }, + }, + function* (held) { + yield* held.started; + }, + ); + expect(run.completion.ok).toBe(true); + expect(cleaned).toBe(true); + check(); + }); + }); + } + const passed = join(root, "passed"); + yield* attempt(passed, () => undefined); + expect(yield* exists(passed)).toBe(false); + + const failed = join(root, "failed"); + let caught: unknown; + try { + yield* attempt(failed, () => { + throw failure; + }); + } catch (error) { + caught = error; + } + expect(caught).toBe(failure); + const invocations = yield* readdir(failed); + expect(invocations).toHaveLength(1); + const directory = join(failed, invocations[0]); + expect(JSON.parse(yield* readTextFile(join(directory, "root-1.case.json")))).toEqual({ + owner: OWNER, + name, + kind: "root", + ordinal: 1, + source: HELD_ROOT_PATH, + }); + const lines = (yield* readTextFile(join(directory, "root-1.jsonl"))).trimEnd().split("\n"); + expect(parseDurableEvent(lines.at(-1) ?? "")).toMatchObject({ + ok: true, + value: { type: "close", coroutineId: "root" }, + }); + }); +}); + +describe("held host suite journal", () => { + it("saves only failed cases after the BDD runner joins", function* () { + const root = yield* useTempDirectory("xmd-held-suite-journal-"); + const cwd = fileURLToPath(new URL("../../../", import.meta.url)); + const owner = "packages/core/tests/fixtures/held-host-journal.ts"; + const runtime = cliRuntime(); + const flags = { + deno: ["test", "--allow-all", "--frozen"], + node: ["--import", "tsx", "--test", "--test-reporter=spec"], + bun: ["test", "--timeout=30000"], + }[runtime]; + const command = [execPath, ...flags, "./" + owner].map(shellQuote).join(" "); + const result = yield* runShell("exec " + command, { + cwd, + env: { + XMD_TEST_ARTIFACTS: root, + TSX_TSCONFIG_PATH: join(cwd, "tsconfig.node.json"), + NO_COLOR: "1", + }, + }).join(); + const output = result.stdout + result.stderr; + if (result.code !== 1) { + throw new Error(`Expected the fixture to exit with 1, received ${result.code}.\n${output}`); + } + expect(output).toMatch(/(?:✔|\(pass\)).*passing roots|passing roots \.\.\. ok/); + expect(output).toContain('Expected: "deliberate held-host late assertion"'); + expect(output).toContain('Received: "both held roots joined"'); + expect(output).toMatch( + /^\s*(?:error: (?:Error: )?|Error: )deliberate held-host case teardown$/m, + ); + expect(yield* exists(join(root, "passing roots"))).toBe(false); + + for (const [name, sources] of [ + ["late assertion", ["host/first.md", "host/second.md"]], + ["rejected finalizer", ["host/teardown.md"]], + ] as const) { + const artifacts = join(root, name); + const invocations = yield* readdir(artifacts); + expect(invocations).toHaveLength(1); + const directory = join(artifacts, invocations[0]); + expect((yield* readdir(directory)).sort()).toEqual( + sources.flatMap((_, index) => [`root-${index + 1}.case.json`, `root-${index + 1}.jsonl`]), + ); + const actualSources: string[] = []; + for (const [index] of sources.entries()) { + const ordinal = index + 1; + const stem = join(directory, `root-${ordinal}`); + const sidecar: unknown = JSON.parse(yield* readTextFile(stem + ".case.json")); + const lines = (yield* readTextFile(stem + ".jsonl")).trimEnd().split("\n"); + const imported = parseDurableEvent(lines[0]); + expect(imported).toMatchObject({ + ok: true, + value: { description: { type: "import_component", name: "__root__" } }, + }); + if ( + !imported.ok || + imported.value.type !== "yield" || + imported.value.result.status !== "ok" + ) { + throw new Error("The held root journal has no successful import."); + } + const value = imported.value.result.value; + if ( + value === null || + typeof value !== "object" || + !("path" in value) || + typeof value.path !== "string" + ) { + throw new Error("The held root import has no source path."); + } + const source = value.path; + actualSources.push(source); + expect(sidecar).toEqual({ + owner, + name, + suite: "held host journal " + name, + kind: "root", + ordinal, + source, + }); + expect(parseDurableEvent(lines.at(-1) ?? "")).toMatchObject({ + ok: true, + value: { type: "close", coroutineId: "root" }, + }); + } + expect(actualSources.sort()).toEqual(sources); + } + }); +}); + describe("Generated ordinary component identity", () => { it("captures installed forms and names their durable work without protected dispatch", function* () { const claimed: string[] = []; @@ -565,52 +732,55 @@ describe("Tier E3 — the documentation this execution captured", () => { expect(counted.reads).toBe(1); }); - it("E3b: late Documentation middleware in the document's own scope changes nothing", function* () { - const counted = { reads: 0 }; - const invoked: string[] = []; - const answer: { lookup?: Result } = {}; - - // Installed in an `Execution.document` wrapper: after canonical capture and - // before the document workflow, which is the scope the evaluation runner is - // attached inside. A recollection there *would* encounter this, so the row - // distinguishes a captured reference from one rebuilt at evaluation time. - // Middleware installed only in a sibling caller scope could not. - yield* Execution.around({ - *document([claimed], next) { - yield* Documentation.around({ - *contributions([read], nextChapter) { - const held = yield* nextChapter(read); - return held.map((one) => - one.source.owner === "@executablemd/test" - ? { - ...one, - source: { ...one.source, text: "## Marker\n\nSUBSTITUTED LATE.\n" }, - } - : one, - ); - }, - }); - yield* next(claimed); - }, - }); + heldHostCase( + "E3b: late Documentation middleware in the document's own scope changes nothing", + function* (stream) { + const counted = { reads: 0 }; + const invoked: string[] = []; + const answer: { lookup?: Result } = {}; + + // Installed in an `Execution.document` wrapper: after canonical capture and + // before the document workflow, which is the scope the evaluation runner is + // attached inside. A recollection there *would* encounter this, so the row + // distinguishes a captured reference from one rebuilt at evaluation time. + // Middleware installed only in a sibling caller scope could not. + yield* Execution.around({ + *document([claimed], next) { + yield* Documentation.around({ + *contributions([read], nextChapter) { + const held = yield* nextChapter(read); + return held.map((one) => + one.source.owner === "@executablemd/test" + ? { + ...one, + source: { ...one.source, text: "## Marker\n\nSUBSTITUTED LATE.\n" }, + } + : one, + ); + }, + }); + yield* next(claimed); + }, + }); - const run = yield* withHeldHost( - { installations: [markerPackage(MARKER_PROSE, counted), selecting([probe()])] }, - function* (open) { - answer.lookup = yield* open.execution.evaluateGeneratedXmdRoot( - request('\n\n\n', invoked), - ); - }, - ); + const run = yield* withHeldHost( + { stream, installations: [markerPackage(MARKER_PROSE, counted), selecting([probe()])] }, + function* (open) { + answer.lookup = yield* open.execution.evaluateGeneratedXmdRoot( + request('\n\n\n', invoked), + ); + }, + ); - expect(run.completion.ok).toBe(true); - const described = rendered(answer.lookup ?? { ok: false, error: new Error("none") }).output; - expect(described).toContain("THE MARKER CHAPTER."); - expect(described).not.toContain("SUBSTITUTED LATE."); - // No new collection happened, which is the mechanism rather than the - // symptom: the count is the same before and after the evaluation. - expect(counted.reads).toBe(1); - }); + expect(run.completion.ok).toBe(true); + const described = rendered(answer.lookup ?? { ok: false, error: new Error("none") }).output; + expect(described).toContain("THE MARKER CHAPTER."); + expect(described).not.toContain("SUBSTITUTED LATE."); + // No new collection happened, which is the mechanism rather than the + // symptom: the count is the same before and after the evaluation. + expect(counted.reads).toBe(1); + }, + ); it("E3c: two live executions each keep the capture they made", function* () { const mine = { reads: 0 }; @@ -918,67 +1088,71 @@ describe("Tier E5 — cancelling joins before anything else starts", () => { ).toBe(false); }); - it("E5b: closing the execution joins a held evaluation before closure completes", function* () { - const order: string[] = []; - const reached = withResolvers(); - const release = withResolvers(); - const saved: { execution?: HostExecution } = {}; - const interrupted: { answer?: Result } = {}; - const invoked: string[] = []; - - yield* scoped(function* () { - const open = yield* useHeldHost({ - // The latch is resolved by the document element's own cleanup, which - // unwinds before the evaluation runner does. So the evaluation's - // cleanup is genuinely held at the moment closure begins, and it is the - // closing that frees it rather than this case pre-arranging the order. - *inside(): Operation { - yield* ensure(() => { - release.resolve(); - }); - }, - installations: [selecting([...declared(), holding(reached, release.operation, order)])], - }); - saved.execution = open.execution; - const running = yield* spawn(() => - open.execution.evaluateGeneratedXmdRoot({ - id: "turn-1", - source: "\n\n\n", - }), - ); - yield* reached.operation; - // Closing the execution: halting the handle's consumer takes the - // invocation down with it. - const closing = yield* spawn(function* () { - yield* open.watched.halt(); - order.push("closed"); + heldHostCase( + "E5b: closing the execution joins a held evaluation before closure completes", + function* (stream) { + const order: string[] = []; + const reached = withResolvers(); + const release = withResolvers(); + const saved: { execution?: HostExecution } = {}; + const interrupted: { answer?: Result } = {}; + const invoked: string[] = []; + + yield* scoped(function* () { + const open = yield* useHeldHost({ + stream, + // The latch is resolved by the document element's own cleanup, which + // unwinds before the evaluation runner does. So the evaluation's + // cleanup is genuinely held at the moment closure begins, and it is the + // closing that frees it rather than this case pre-arranging the order. + *inside(): Operation { + yield* ensure(() => { + release.resolve(); + }); + }, + installations: [selecting([...declared(), holding(reached, release.operation, order)])], + }); + saved.execution = open.execution; + const running = yield* spawn(() => + open.execution.evaluateGeneratedXmdRoot({ + id: "turn-1", + source: "\n\n\n", + }), + ); + yield* reached.operation; + // Closing the execution: halting the handle's consumer takes the + // invocation down with it. + const closing = yield* spawn(function* () { + yield* open.watched.halt(); + order.push("closed"); + }); + yield* closing; + interrupted.answer = yield* running; }); - yield* closing; - interrupted.answer = yield* running; - }); - // The held cleanup joined, and only then did closure complete. - expect(order).toEqual(["evaluation cleanup", "closed"]); - // The caller was told the execution closed rather than left waiting on an - // answer nothing was left to produce. - expect(interrupted.answer?.ok).toBe(false); - expect(interrupted.answer?.ok === false && interrupted.answer.error).toBeInstanceOf( - HostExecutionError, - ); - expect(interrupted.answer?.ok === false && interrupted.answer.error.message).toContain( - "closed while it was evaluating", - ); + // The held cleanup joined, and only then did closure complete. + expect(order).toEqual(["evaluation cleanup", "closed"]); + // The caller was told the execution closed rather than left waiting on an + // answer nothing was left to produce. + expect(interrupted.answer?.ok).toBe(false); + expect(interrupted.answer?.ok === false && interrupted.answer.error).toBeInstanceOf( + HostExecutionError, + ); + expect(interrupted.answer?.ok === false && interrupted.answer.error.message).toContain( + "closed while it was evaluating", + ); - const execution = saved.execution; - if (execution === undefined) { - throw new Error("the case never held an execution"); - } - const after = yield* execution.evaluateGeneratedXmdRoot( - request("\n\n\n", invoked), - ); - expect(after.ok).toBe(false); - expect(invoked).toEqual([]); - }); + const execution = saved.execution; + if (execution === undefined) { + throw new Error("the case never held an execution"); + } + const after = yield* execution.evaluateGeneratedXmdRoot( + request("\n\n\n", invoked), + ); + expect(after.ok).toBe(false); + expect(invoked).toEqual([]); + }, + ); }); describe("Tier E6 — reopening without replaying a response", () => { @@ -1242,29 +1416,32 @@ function failingMain(): NonNullable[number] describe("Tier E7 — which failures stay the run's", () => { beforeAll(() => useTempFileCompiler()); - it("E7a: an ordinary control failure is a Result carrying the real partial render", function* () { - const invoked: string[] = []; - const answer: { failed?: Result } = {}; + heldHostCase( + "E7a: an ordinary control failure is a Result carrying the real partial render", + function* (stream) { + const invoked: string[] = []; + const answer: { failed?: Result } = {}; - const run = yield* withHeldHost( - { installations: [selecting(declared(invoked))] }, - function* (open) { - answer.failed = yield* open.execution.evaluateGeneratedXmdRoot({ - id: "turn-1", - source: "\nbefore the failure\n\n\n\n", - }); - }, - ); + const run = yield* withHeldHost( + { stream, installations: [selecting(declared(invoked))] }, + function* (open) { + answer.failed = yield* open.execution.evaluateGeneratedXmdRoot({ + id: "turn-1", + source: "\nbefore the failure\n\n\n\n", + }); + }, + ); - // The request's own failure, so the execution is untouched by it. - expect(run.completion.ok).toBe(true); - expect(answer.failed?.ok).toBe(false); - const error: (Error & { partial?: GeneratedXmdRootResult }) | undefined = - answer.failed?.ok === false ? answer.failed.error : undefined; - expect(error?.message).toContain("the control refused this request"); - expect(error?.partial?.output).toContain("before the failure"); - expect(invoked).toEqual(["boomed"]); - }); + // The request's own failure, so the execution is untouched by it. + expect(run.completion.ok).toBe(true); + expect(answer.failed?.ok).toBe(false); + const error: (Error & { partial?: GeneratedXmdRootResult }) | undefined = + answer.failed?.ok === false ? answer.failed.error : undefined; + expect(error?.message).toContain("the control refused this request"); + expect(error?.partial?.output).toContain("before the failure"); + expect(invoked).toEqual(["boomed"]); + }, + ); it("E7b: a persistence failure stays itself, and the run does not succeed", function* () { const stream = new InMemoryStream(); @@ -1307,29 +1484,32 @@ describe("Tier E7 — which failures stay the run's", () => { expect(invoked).toEqual([]); }); - it("E7c: a teardown failure stays itself, and the run does not succeed", function* () { - const caught: { raised?: unknown; completion?: Result } = {}; + heldHostCase( + "E7c: a teardown failure stays itself, and the run does not succeed", + function* (stream) { + const caught: { raised?: unknown; completion?: Result } = {}; - yield* scoped(function* () { - const open = yield* useHeldHost({ installations: [selecting(declared())] }); - try { - yield* open.execution.evaluateGeneratedXmdRoot({ - id: "turn-1", - source: "\n\n\n", - }); - } catch (error) { - caught.raised = error; - } - open.release(); - caught.completion = yield* open.watched; - }); + yield* scoped(function* () { + const open = yield* useHeldHost({ stream, installations: [selecting(declared())] }); + try { + yield* open.execution.evaluateGeneratedXmdRoot({ + id: "turn-1", + source: "\n\n\n", + }); + } catch (error) { + caught.raised = error; + } + open.release(); + caught.completion = yield* open.watched; + }); - // Work the root owned did not finish unwinding, so a rendering reported - // beside it would describe a root still running. - expect(caught.raised).not.toBe(undefined); - expect(String((caught.raised as Error).message)).toContain("cleanup"); - expect(caught.completion?.ok).toBe(false); - }); + // Work the root owned did not finish unwinding, so a rendering reported + // beside it would describe a root still running. + expect(caught.raised).not.toBe(undefined); + expect(String((caught.raised as Error).message)).toContain("cleanup"); + expect(caught.completion?.ok).toBe(false); + }, + ); it("E7d: a fatal evaluation outranks the ordinary failure the document went on to have", function* () { const stream = new InMemoryStream(); @@ -1430,58 +1610,69 @@ describe("Tier E7 — which failures stay the run's", () => { expect(run.completion.ok).toBe(false); }); - it("E7f: an authoritative evaluation failure of no classified kind is still the run's", function* () { - const caught: { raised?: unknown } = {}; - - // A failed evaluation teardown is neither a durability nor a Files - // failure, so the classified tiers do not reach it — and the document - // fails afterwards for an ordinary reason of its own. Which of the two the - // completion reports is decided by occurrence, and the evaluation's came - // first. - const run = yield* withHeldHost( - { - root: { path: "host/ranking.md", source: "\n\n" }, - installations: [{ components: [failingMain()] }, selecting([probe(), failingCleanup()])], - }, - function* (open) { - try { - yield* open.execution.evaluateGeneratedXmdRoot({ - id: "turn-1", - source: "\n\n\n", - }); - } catch (error) { - caught.raised = error; - } - }, - ); - - // The object the caller was handed, not a rebuilt one carrying its words. - expect(caught.raised).toBeInstanceOf(Error); - expect(run.completion.ok).toBe(false); - expect(run.completion.ok ? undefined : run.completion.error).toBe(caught.raised); - }); + heldHostCase( + "E7f: an authoritative evaluation failure of no classified kind is still the run's", + function* (stream) { + const caught: { raised?: unknown } = {}; + + // A failed evaluation teardown is neither a durability nor a Files + // failure, so the classified tiers do not reach it — and the document + // fails afterwards for an ordinary reason of its own. Which of the two the + // completion reports is decided by occurrence, and the evaluation's came + // first. + const run = yield* withHeldHost( + { + stream, + root: { path: "host/ranking.md", source: "\n\n" }, + installations: [{ components: [failingMain()] }, selecting([probe(), failingCleanup()])], + }, + function* (open) { + try { + yield* open.execution.evaluateGeneratedXmdRoot({ + id: "turn-1", + source: "\n\n\n", + }); + } catch (error) { + caught.raised = error; + } + }, + ); - it("E7g: the same failure is the run's when the document succeeded — by identity", function* () { - const caught: { raised?: unknown } = {}; + // The object the caller was handed, not a rebuilt one carrying its words. + expect(caught.raised).toBeInstanceOf(Error); + expect(run.completion.ok).toBe(false); + expect(run.completion.ok ? undefined : run.completion.error).toBe(caught.raised); + }, + "host/ranking.md", + ); - // The control for E7f. E7c already states that such a run does not report - // success; what this adds is that the completion is the *same* failure, so - // E7f's row is about ranking rather than about reporting one at all. - const run = yield* withHeldHost({ installations: [selecting(declared())] }, function* (open) { - try { - yield* open.execution.evaluateGeneratedXmdRoot({ - id: "turn-1", - source: "\n\n\n", - }); - } catch (error) { - caught.raised = error; - } - }); + heldHostCase( + "E7g: the same failure is the run's when the document succeeded — by identity", + function* (stream) { + const caught: { raised?: unknown } = {}; + + // The control for E7f. E7c already states that such a run does not report + // success; what this adds is that the completion is the *same* failure, so + // E7f's row is about ranking rather than about reporting one at all. + const run = yield* withHeldHost( + { stream, installations: [selecting(declared())] }, + function* (open) { + try { + yield* open.execution.evaluateGeneratedXmdRoot({ + id: "turn-1", + source: "\n\n\n", + }); + } catch (error) { + caught.raised = error; + } + }, + ); - expect(caught.raised).toBeInstanceOf(Error); - expect(run.completion.ok).toBe(false); - expect(run.completion.ok ? undefined : run.completion.error).toBe(caught.raised); - }); + expect(caught.raised).toBeInstanceOf(Error); + expect(run.completion.ok).toBe(false); + expect(run.completion.ok ? undefined : run.completion.error).toBe(caught.raised); + }, + ); }); /** @@ -1689,28 +1880,31 @@ describe("Tier E9 — an availability refusal is the call's answer", () => { * evaluator lives in, so the four decided members and the class name the * request uses are checked at the published boundary. */ - it("E9d: the public request is the four-member shape, typed through core/host", function* () { - const invoked: string[] = []; - const classes: readonly GeneratedEffectClass[] = ["read"]; - const asked: GeneratedXmdRequest = { - id: "turn-1", - source: "\n\n\n", - allow: classes, - position: { line: 1, column: 1, offset: 0 }, - }; - const answer: { value?: Result } = {}; - const run = yield* withHeldHost( - { installations: [selecting([probe(invoked)])] }, - function* (open) { - answer.value = yield* open.execution.evaluateGeneratedXmdRoot(asked); - }, - ); + heldHostCase( + "E9d: the public request is the four-member shape, typed through core/host", + function* (stream) { + const invoked: string[] = []; + const classes: readonly GeneratedEffectClass[] = ["read"]; + const asked: GeneratedXmdRequest = { + id: "turn-1", + source: "\n\n\n", + allow: classes, + position: { line: 1, column: 1, offset: 0 }, + }; + const answer: { value?: Result } = {}; + const run = yield* withHeldHost( + { stream, installations: [selecting([probe(invoked)])] }, + function* (open) { + answer.value = yield* open.execution.evaluateGeneratedXmdRoot(asked); + }, + ); - expect(run.completion.ok).toBe(true); - expect(rendered(answer.value ?? { ok: false, error: new Error("none") }).output).toContain( - "probed", - ); - }); + expect(run.completion.ok).toBe(true); + expect(rendered(answer.value ?? { ok: false, error: new Error("none") }).output).toContain( + "probed", + ); + }, + ); }); describe("R6 — generated ordinary identity remains execution-owned", () => { diff --git a/packages/core/tests/if.test.ts b/packages/core/tests/if.test.ts index 696e75d11..a3cccc579 100644 --- a/packages/core/tests/if.test.ts +++ b/packages/core/tests/if.test.ts @@ -21,6 +21,9 @@ import { Sample } from "../src/sample-api.ts"; import { readTextFile } from "@effectionx/fs"; import type { ComponentElement, FunctionComponentDefinition, Segment } from "../src/types.ts"; import { asText } from "./helpers.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/if.test.ts"); interface IfRun { segments: Segment[]; @@ -478,41 +481,47 @@ describe("Tier IF — document execution", () => { expect(evaluated).not.toContain("UNSELECTED_RAN"); }); - it("IF40: an effect in the unselected branch never happens", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "test.md": [ - "", - "```js eval", - "throw new Error('UNSELECTED_EFFECT');", - "```", - "selected", - "", - ].join("\n"), - }); - yield* useEchoExec(); + journalCase( + "IF40: an effect in the unselected branch never happens", + "test.md", + function* (stream) { + yield* useStubFs({ + "test.md": [ + "", + "```js eval", + "throw new Error('UNSELECTED_EFFECT');", + "```", + "selected", + "", + ].join("\n"), + }); + yield* useEchoExec(); - const output = yield* collect(yield* execute({ path: "test.md", stream })); + const output = yield* collect(yield* execute({ path: "test.md", stream })); - expect(output).toContain("selected"); - expect(output).not.toContain("UNSELECTED_EFFECT"); - expect(output).not.toContain("ERROR"); - }); + expect(output).toContain("selected"); + expect(output).not.toContain("UNSELECTED_EFFECT"); + expect(output).not.toContain("ERROR"); + }, + ); - it("IF41: a component in the unselected branch is never imported", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Boom.md": "```js eval\noutput('BOOM_RAN');\n```\n", - "test.md": "alt", - }); - yield* useEchoExec(); + journalCase( + "IF41: a component in the unselected branch is never imported", + "test.md", + function* (stream) { + yield* useStubFs({ + "components/Boom.md": "```js eval\noutput('BOOM_RAN');\n```\n", + "test.md": "alt", + }); + yield* useEchoExec(); - const output = yield* collect(yield* execute({ path: "test.md", stream })); + const output = yield* collect(yield* execute({ path: "test.md", stream })); - expect(output).toContain("alt"); - expect(output).not.toContain("BOOM_RAN"); - expect(output).not.toContain("ERROR"); - }); + expect(output).toContain("alt"); + expect(output).not.toContain("BOOM_RAN"); + expect(output).not.toContain("ERROR"); + }, + ); it("IF42: selects the same branch from a restored binding on partial replay", function* () { const REPLAY_DOC = [ @@ -561,27 +570,30 @@ describe("Tier IF — document execution", () => { expect(evaluated).not.toContain("CLEAN_RAN"); }); - it("IF43: an projected through resolves the caller's binding", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/Wrap.md": "", - "test.md": [ - "```js eval", - "const ready = false;", - "```", - "", - "READYWAITING", - "", - ].join("\n"), - }); - yield* useEchoExec(); + journalCase( + "IF43: an projected through resolves the caller's binding", + "test.md", + function* (stream) { + yield* useStubFs({ + "components/Wrap.md": "", + "test.md": [ + "```js eval", + "const ready = false;", + "```", + "", + "READYWAITING", + "", + ].join("\n"), + }); + yield* useEchoExec(); - const output = yield* collect(yield* execute({ path: "test.md", stream })); + const output = yield* collect(yield* execute({ path: "test.md", stream })); - expect(output).toContain("WAITING"); - expect(output).not.toContain("READY"); - expect(output).not.toContain("ERROR"); - }); + expect(output).toContain("WAITING"); + expect(output).not.toContain("READY"); + expect(output).not.toContain("ERROR"); + }, + ); }); /** diff --git a/packages/core/tests/let.test.ts b/packages/core/tests/let.test.ts index c6cd810ea..1b42963d7 100644 --- a/packages/core/tests/let.test.ts +++ b/packages/core/tests/let.test.ts @@ -23,6 +23,9 @@ import { Component } from "../src/component-api.ts"; import { scanSegments } from "../src/scanner.ts"; import { renderSegments } from "../src/render.ts"; import { execute, executeInstalled } from "../src/execute.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/let.test.ts"); import type { HostExecution } from "../src/execute.ts"; import { inlineSource } from "../src/root-source.ts"; import { collect } from "../src/collect.ts"; @@ -454,46 +457,49 @@ describe("Tier LET — failure and overwrite", () => { describe("Tier LET — a whole document", () => { beforeAll(() => useTempFileCompiler()); - it("LET10: a direct binding reaches prose, an expression prop, and a later eval", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "README.md": [ - "```ts eval", - 'const settings = { title: "Release" };', - "```", - "", - '', - "", - '', - "", - "Prose reads {title}.", - "", - "", - "", - "```ts eval", - 'return alias === settings ? "identical" : "different";', - "```", - ].join("\n"), - "components/Note.md": [ - "---", - "props:", - " type: object", - " properties:", - " label:", - " type: string", - " additionalProperties: false", - "---", - "", - "Note says {props.label}.", - ].join("\n"), - }); + journalCase( + "LET10: a direct binding reaches prose, an expression prop, and a later eval", + "README.md", + function* (stream) { + yield* useStubFs({ + "README.md": [ + "```ts eval", + 'const settings = { title: "Release" };', + "```", + "", + '', + "", + '', + "", + "Prose reads {title}.", + "", + "", + "", + "```ts eval", + 'return alias === settings ? "identical" : "different";', + "```", + ].join("\n"), + "components/Note.md": [ + "---", + "props:", + " type: object", + " properties:", + " label:", + " type: string", + " additionalProperties: false", + "---", + "", + "Note says {props.label}.", + ].join("\n"), + }); - const output = asText(yield* collect(yield* execute({ path: "README.md", stream }))); + const output = asText(yield* collect(yield* execute({ path: "README.md", stream }))); - expect(output).toContain("Prose reads Release."); - expect(output).toContain("Note says Release."); - expect(output).toContain("identical"); - }); + expect(output).toContain("Prose reads Release."); + expect(output).toContain("Note says Release."); + expect(output).toContain("identical"); + }, + ); it("LET11: a partial replay rebinds by reference and records nothing of its own", function* () { const document = [ @@ -542,19 +548,22 @@ describe("Tier LET — a whole document", () => { ]); }); - it("LET12: Let is the syntax; Capture is an ordinary component", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "README.md": ['', "", ""].join("\n"), - "components/Let.md": "A REPOSITORY FILE RAN\n", - "components/Capture.md": "an ordinary component ran.\n", - }); + journalCase( + "LET12: Let is the syntax; Capture is an ordinary component", + "README.md", + function* (stream) { + yield* useStubFs({ + "README.md": ['', "", ""].join("\n"), + "components/Let.md": "A REPOSITORY FILE RAN\n", + "components/Capture.md": "an ordinary component ran.\n", + }); - const output = asText(yield* collect(yield* execute({ path: "README.md", stream }))); + const output = asText(yield* collect(yield* execute({ path: "README.md", stream }))); - expect(output).toContain("an ordinary component ran."); - expect(output).not.toContain("A REPOSITORY FILE RAN"); - }); + expect(output).toContain("an ordinary component ran."); + expect(output).not.toContain("A REPOSITORY FILE RAN"); + }, + ); }); /** @@ -729,29 +738,32 @@ describe("Tier LET — the ordinary inventory, and which values are eligible", ( expect(stream.snapshot().map(kindOf)).toEqual(["import_component", "eval", "close"]); }); - it("C7: the eligible data is the JSON subset of the same document's root", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ "README.md": EVERY_KIND }); - - const execution = yield* executeInstalled({ path: "README.md", stream }, []); - const settled = yield* execution; - expect(settled.ok).toBe(true); - - // The live bindings are unchanged — the document read every one of them - // above — and what crosses is decided by one strict JSON walk rather than - // by which construct produced the value. - const held = yield* readBindings(execution); - expect(held["nested"]).toEqual({ list: [1, { deep: true }], nothing: null }); - expect(held["nothing"]).toBe(null); - expect(held["rendered"]).toBe("plain text"); - // Omitted whole, never coerced: a value that is not plain JSON, a cycle, a - // sparse hole, a non-finite number, `undefined` and a function. - for (const omitted of ["notPlain", "cycle", "sparse", "infinite", "absent", "work"]) { - expect(Object.hasOwn(held, omitted)).toBe(false); - } - // `props` is each execution's own namespace and never crosses. - expect(Object.hasOwn(held, "props")).toBe(false); - }); + journalCase( + "C7: the eligible data is the JSON subset of the same document's root", + "README.md", + function* (stream) { + yield* useStubFs({ "README.md": EVERY_KIND }); + + const execution = yield* executeInstalled({ path: "README.md", stream }, []); + const settled = yield* execution; + expect(settled.ok).toBe(true); + + // The live bindings are unchanged — the document read every one of them + // above — and what crosses is decided by one strict JSON walk rather than + // by which construct produced the value. + const held = yield* readBindings(execution); + expect(held["nested"]).toEqual({ list: [1, { deep: true }], nothing: null }); + expect(held["nothing"]).toBe(null); + expect(held["rendered"]).toBe("plain text"); + // Omitted whole, never coerced: a value that is not plain JSON, a cycle, a + // sparse hole, a non-finite number, `undefined` and a function. + for (const omitted of ["notPlain", "cycle", "sparse", "infinite", "absent", "work"]) { + expect(Object.hasOwn(held, omitted)).toBe(false); + } + // `props` is each execution's own namespace and never crosses. + expect(Object.hasOwn(held, "props")).toBe(false); + }, + ); }); describe("Tier LET — the computed root data an execution hands back", () => { beforeAll(() => useTempFileCompiler()); diff --git a/packages/core/tests/loop.test.ts b/packages/core/tests/loop.test.ts index 08cac0564..ae8c95a14 100644 --- a/packages/core/tests/loop.test.ts +++ b/packages/core/tests/loop.test.ts @@ -19,6 +19,9 @@ import { collect } from "../src/collect.ts"; import { printErrors } from "../src/component-failures.ts"; import type { ComponentElement, FunctionComponent, Segment } from "../src/types.ts"; import { asText } from "./helpers.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/loop.test.ts"); interface LoopRun { segments: Segment[]; @@ -744,8 +747,7 @@ describe("Tier LOOP — document execution", () => { expect(evalNames(stream.snapshot())).toHaveLength(1); }); - it("LOOP37: a binding accumulates across iterations", function* () { - const stream = new InMemoryStream(); + journalCase("LOOP37: a binding accumulates across iterations", "test.md", function* (stream) { yield* useHostFiles(); yield* useStubFs({ "test.md": [ @@ -773,19 +775,22 @@ describe("Tier LOOP — document execution", () => { expect(output).toContain("TALLY:0,1,2,3"); }); - it("LOOP38: a component in the loop body is imported once per iteration", function* () { - const stream = new InMemoryStream(); - yield* useHostFiles(); - yield* useStubFs({ - "components/Step.md": "step\n", - "test.md": "", - }); - yield* useEchoExec(); + journalCase( + "LOOP38: a component in the loop body is imported once per iteration", + "test.md", + function* (stream) { + yield* useHostFiles(); + yield* useStubFs({ + "components/Step.md": "step\n", + "test.md": "", + }); + yield* useEchoExec(); - const output = asText(yield* collect(yield* execute({ path: "test.md", stream }))); - expect(output.match(/step/g)).toHaveLength(3); - expect(output).not.toContain("ERROR"); - }); + const output = asText(yield* collect(yield* execute({ path: "test.md", stream }))); + expect(output.match(/step/g)).toHaveLength(3); + expect(output).not.toContain("ERROR"); + }, + ); it("LOOP39: a truncated journal replays one iteration and runs the rest live", function* () { const DOC = [ @@ -842,36 +847,39 @@ describe("Tier LOOP — document execution", () => { ]); }); - it("LOOP40: a chosen from a binding stops the document's loop", function* () { - const stream = new InMemoryStream(); - yield* useHostFiles(); - yield* useStubFs({ - "test.md": [ - "```js eval", - "const seen = [];", - "```", - "", - "", - "```js eval", - "seen.push('iteration');", - "```", - "", - "", - "", - "", - "", - "", - "```js eval", - "output(`ITERATIONS:${seen.length}`);", - "```", - ].join("\n"), - }); - yield* useEchoExec(); + journalCase( + "LOOP40: a chosen from a binding stops the document's loop", + "test.md", + function* (stream) { + yield* useHostFiles(); + yield* useStubFs({ + "test.md": [ + "```js eval", + "const seen = [];", + "```", + "", + "", + "```js eval", + "seen.push('iteration');", + "```", + "", + "", + "", + "", + "", + "", + "```js eval", + "output(`ITERATIONS:${seen.length}`);", + "```", + ].join("\n"), + }); + yield* useEchoExec(); - const output = asText(yield* collect(yield* execute({ path: "test.md", stream }))); - expect(output).toContain("ITERATIONS:2"); - expect(output).not.toContain("ERROR"); - }); + const output = asText(yield* collect(yield* execute({ path: "test.md", stream }))); + expect(output).toContain("ITERATIONS:2"); + expect(output).not.toContain("ERROR"); + }, + ); }); /** diff --git a/packages/core/tests/sample-component.test.ts b/packages/core/tests/sample-component.test.ts index 4dddc7f86..29c63eacc 100644 --- a/packages/core/tests/sample-component.test.ts +++ b/packages/core/tests/sample-component.test.ts @@ -23,6 +23,9 @@ import { ensureDir, readTextFile, writeTextFile } from "@effectionx/fs"; import { useTempDirectory } from "@executablemd/test-support/temp"; import type { Operation } from "effection"; import * as path from "node:path"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/sample-component.test.ts"); function* writeFiles(dir: string, files: Record): Operation { for (const [filePath, content] of Object.entries(files)) { @@ -415,8 +418,7 @@ describe("Tier SC — Sample component", () => { describe("Tier EO — eval output() function", () => { beforeAll(() => useTempFileCompiler()); // EO1: output() sets eval block output - it("EO1: output() produces eval block output", function* () { - const stream = new InMemoryStream(); + journalCase("EO1: output() produces eval block output", "test.md", function* (stream) { yield* useStubFs({ "test.md": '```js eval\noutput("hello from eval");\n```\n', }); @@ -462,8 +464,7 @@ describe("Tier EO — eval output() function", () => { }); // EO3: eval block without output() still returns empty - it("EO3: eval block without output() produces no output", function* () { - const stream = new InMemoryStream(); + journalCase("EO3: eval block without output() produces no output", "test.md", function* (stream) { yield* useStubFs({ "test.md": "```js eval\nconst x = 42;\n```\nafter-eval", }); @@ -482,8 +483,7 @@ describe("Tier EO — eval output() function", () => { }); // EO4: output() with multiline content - it("EO4: output() with multiline content", function* () { - const stream = new InMemoryStream(); + journalCase("EO4: output() with multiline content", "test.md", function* (stream) { yield* useStubFs({ "test.md": '```js eval\noutput("line1\\nline2\\nline3");\n```\n', }); @@ -500,8 +500,7 @@ describe("Tier EO — eval output() function", () => { }); // EO5: output() converts non-string to string - it("EO5: output() converts non-string to string", function* () { - const stream = new InMemoryStream(); + journalCase("EO5: output() converts non-string to string", "test.md", function* (stream) { yield* useStubFs({ "test.md": "```js eval\noutput(12345);\n```\n", }); @@ -521,37 +520,39 @@ describe("Tier EO — eval output() function", () => { describe("Tier RC — renderChildren and render closures", () => { beforeAll(() => useTempFileCompiler()); // RC1: renderChildren() returns empty string for self-closing component - it("RC1: renderChildren returns empty for self-closing component", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/TestComp.md": [ - "---", - "meta:", - " componentName: TestComp", - "---", - "", - "```js eval", - "const result = yield* renderChildren();", - "output('children:[' + result + ']');", - "```", - ].join("\n"), - "test.md": "", - }); - yield* useEchoExec(); - - const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), - ); - - expect(output).toContain("children:[]"); - }); + journalCase( + "RC1: renderChildren returns empty for self-closing component", + "test.md", + function* (stream) { + yield* useStubFs({ + "components/TestComp.md": [ + "---", + "meta:", + " componentName: TestComp", + "---", + "", + "```js eval", + "const result = yield* renderChildren();", + "output('children:[' + result + ']');", + "```", + ].join("\n"), + "test.md": "", + }); + yield* useEchoExec(); + + const output = yield* collect( + yield* execute({ + path: "test.md", + stream, + }), + ); + + expect(output).toContain("children:[]"); + }, + ); // RC2: renderChildren() captures children text content - it("RC2: renderChildren captures children text", function* () { - const stream = new InMemoryStream(); + journalCase("RC2: renderChildren captures children text", "test.md", function* (stream) { yield* useStubFs({ "components/TestComp.md": [ "---", @@ -579,8 +580,7 @@ describe("Tier RC — renderChildren and render closures", () => { }); // RC3: render() expands arbitrary markdown string - it("RC3: render() expands arbitrary markdown string", function* () { - const stream = new InMemoryStream(); + journalCase("RC3: render() expands arbitrary markdown string", "test.md", function* (stream) { yield* useStubFs({ "components/TestComp.md": [ "---", @@ -607,99 +607,108 @@ describe("Tier RC — renderChildren and render closures", () => { expect(output).toContain("rendered:[arbitrary **markdown** content]"); }); - it("RC4: renderChildren(override) is visible to children and wins over the caller value", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/TestComp.md": [ - "---", - "meta:", - " componentName: TestComp", - "---", - "", - "```js eval", - "const result = yield* renderChildren({ item: 'injected', shadow: 'override-wins' });", - "output('r:[' + result.trim() + ']');", - "```", - ].join("\n"), - "test.md": [ - "```js eval", - "const shadow = 'caller-value';", - "```", - "", - "text item={item} shadow={shadow}", - "```js eval", - "output('child-eval:' + shadow + '/' + item);", - "```", - "", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("text item=injected shadow=override-wins"); - expect(output).toContain("child-eval:override-wins/injected"); - expect(output).not.toContain("caller-value"); - }); - - it("RC5: renderChildren(override) does not leak into the caller env", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/TestComp.md": [ - "---", - "meta:", - " componentName: TestComp", - "---", - "", - "```js eval", - "yield* renderChildren({ item: 'injected' });", - "```", - ].join("\n"), - "test.md": [ - "x={item}", - "```js eval", - "output('caller-after:' + (typeof item === 'undefined' ? 'undefined' : item));", - "```", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("caller-after:undefined"); - expect(output).not.toContain("ERROR"); - }); - - it("RC6: renderChildren(override) rejects null, arrays, primitives, and non-plain objects", function* () { - const stream = new InMemoryStream(); - yield* useStubFs({ - "components/TestComp.md": [ - "---", - "meta:", - " componentName: TestComp", - "---", - "", - "```js eval", - "const bad = [null, [1, 2, 3], 42, 'str', new Date(), new Map()];", - "let caught = 0;", - "for (const value of bad) {", - " try {", - " yield* renderChildren(value);", - " } catch (e) {", - " if (e.message === 'renderChildren(override) requires a plain object.') caught++;", - " }", - "}", - "output('caught:' + caught + '/' + bad.length);", - "```", - ].join("\n"), - "test.md": "body", - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("caught:6/6"); - }); + journalCase( + "RC4: renderChildren(override) is visible to children and wins over the caller value", + "test.md", + function* (stream) { + yield* useStubFs({ + "components/TestComp.md": [ + "---", + "meta:", + " componentName: TestComp", + "---", + "", + "```js eval", + "const result = yield* renderChildren({ item: 'injected', shadow: 'override-wins' });", + "output('r:[' + result.trim() + ']');", + "```", + ].join("\n"), + "test.md": [ + "```js eval", + "const shadow = 'caller-value';", + "```", + "", + "text item={item} shadow={shadow}", + "```js eval", + "output('child-eval:' + shadow + '/' + item);", + "```", + "", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).toContain("text item=injected shadow=override-wins"); + expect(output).toContain("child-eval:override-wins/injected"); + expect(output).not.toContain("caller-value"); + }, + ); + + journalCase( + "RC5: renderChildren(override) does not leak into the caller env", + "test.md", + function* (stream) { + yield* useStubFs({ + "components/TestComp.md": [ + "---", + "meta:", + " componentName: TestComp", + "---", + "", + "```js eval", + "yield* renderChildren({ item: 'injected' });", + "```", + ].join("\n"), + "test.md": [ + "x={item}", + "```js eval", + "output('caller-after:' + (typeof item === 'undefined' ? 'undefined' : item));", + "```", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).toContain("caller-after:undefined"); + expect(output).not.toContain("ERROR"); + }, + ); + + journalCase( + "RC6: renderChildren(override) rejects null, arrays, primitives, and non-plain objects", + "test.md", + function* (stream) { + yield* useStubFs({ + "components/TestComp.md": [ + "---", + "meta:", + " componentName: TestComp", + "---", + "", + "```js eval", + "const bad = [null, [1, 2, 3], 42, 'str', new Date(), new Map()];", + "let caught = 0;", + "for (const value of bad) {", + " try {", + " yield* renderChildren(value);", + " } catch (e) {", + " if (e.message === 'renderChildren(override) requires a plain object.') caught++;", + " }", + "}", + "output('caught:' + caught + '/' + bad.length);", + "```", + ].join("\n"), + "test.md": "body", + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).toContain("caught:6/6"); + }, + ); }); describe("Tier IN — Instruction component", () => { diff --git a/packages/core/tests/source-position.test.ts b/packages/core/tests/source-position.test.ts index 749d28486..5cfdc8121 100644 --- a/packages/core/tests/source-position.test.ts +++ b/packages/core/tests/source-position.test.ts @@ -1,6 +1,5 @@ import { describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; -import { InMemoryStream } from "@executablemd/durable-streams"; import { useStubFs } from "@executablemd/runtime/test"; import { scanSegments } from "../src/scanner.ts"; import { SOURCE_POSITION_FIELD, sourceDescription } from "../src/source-position.ts"; @@ -9,6 +8,9 @@ import { registerComponents } from "../src/components/registration.ts"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import type { ComponentElement, SourcePosition } from "../src/types.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/source-position.test.ts"); function componentsOf(segments: ReturnType): ComponentElement[] { const found: ComponentElement[] = []; @@ -51,92 +53,100 @@ describe("source positions", () => { }); }); - it("computes original-file positions past frontmatter, immune to repeated body text", function* () { - // The frontmatter contains a copy of the body's first line: a content - // search for the body would false-match inside the frontmatter, so only - // the suffix computation yields the correct base. - const doc = [ - "---", - 'title: ""', - "note: intro line", - "---", - "intro line", - "", - "", - ].join("\n"); - - const positions: SourcePosition[] = []; - const stream = new InMemoryStream(); - yield* useStubFs({ "README.md": doc }); - // Registered rather than a repository file, so the probe can record what - // the engine tells a component about its own call site. That is the public - // guarantee now — `getExpansion()` — rather than what a claim was offered. - yield* registerComponents([ - { - name: "Probe", - origin: "test", - props: { type: "object", properties: {}, additionalProperties: false }, - *fn() { - const position = (yield* getExpansion()).position; - if (position) { - positions.push(position); - } - return "probed"; + journalCase( + "computes original-file positions past frontmatter, immune to repeated body text", + "README.md", + function* (stream) { + // The frontmatter contains a copy of the body's first line: a content + // search for the body would false-match inside the frontmatter, so only + // the suffix computation yields the correct base. + const doc = [ + "---", + 'title: ""', + "note: intro line", + "---", + "intro line", + "", + "", + ].join("\n"); + + const positions: SourcePosition[] = []; + + yield* useStubFs({ "README.md": doc }); + // Registered rather than a repository file, so the probe can record what + // the engine tells a component about its own call site. That is the public + // guarantee now — `getExpansion()` — rather than what a claim was offered. + yield* registerComponents([ + { + name: "Probe", + origin: "test", + props: { type: "object", properties: {}, additionalProperties: false }, + *fn() { + const position = (yield* getExpansion()).position; + if (position) { + positions.push(position); + } + return "probed"; + }, }, - }, - ]); - - const output = yield* collect(yield* execute({ path: "README.md", stream })); - expect(output).toContain("probed"); - - const probeLine = doc.split("\n").indexOf("") + 1; - expect(positions).toEqual([ - { - path: "README.md", - offset: doc.indexOf("\n") + 1, - line: probeLine, - column: 1, - }, - ]); - }); - - it("positions imported-component invocations in the component's own file", function* () { - const readme = "\n"; - const wrapper = ["---", "title: Wrapper", "---", "before", "", ""].join("\n"); - - const positions: Array<{ name: string; position?: SourcePosition }> = []; - const stream = new InMemoryStream(); - yield* useStubFs({ - "README.md": readme, - "components/Wrapper.md": wrapper, - }); - yield* registerComponents([ - { - name: "Probe", - origin: "test", - props: { type: "object", properties: {}, additionalProperties: false }, - *fn() { - const metadata = yield* getExpansion(); - positions.push({ - name: metadata.name, - ...(metadata.position ? { position: metadata.position } : {}), - }); - return "probed"; + ]); + + const output = yield* collect(yield* execute({ path: "README.md", stream })); + expect(output).toContain("probed"); + + const probeLine = doc.split("\n").indexOf("") + 1; + expect(positions).toEqual([ + { + path: "README.md", + offset: doc.indexOf("\n") + 1, + line: probeLine, + column: 1, }, - }, - ]); + ]); + }, + ); + + journalCase( + "positions imported-component invocations in the component's own file", + "README.md", + function* (stream) { + const readme = "\n"; + const wrapper = ["---", "title: Wrapper", "---", "before", "", ""].join("\n"); + + const positions: Array<{ name: string; position?: SourcePosition }> = []; + + yield* useStubFs({ + "README.md": readme, + "components/Wrapper.md": wrapper, + }); + yield* registerComponents([ + { + name: "Probe", + origin: "test", + props: { type: "object", properties: {}, additionalProperties: false }, + *fn() { + const metadata = yield* getExpansion(); + positions.push({ + name: metadata.name, + ...(metadata.position ? { position: metadata.position } : {}), + }); + return "probed"; + }, + }, + ]); - const output = yield* collect(yield* execute({ path: "README.md", stream })); - expect(output).toContain("probed"); + const output = yield* collect(yield* execute({ path: "README.md", stream })); + expect(output).toContain("probed"); - const probe = positions.find((p) => p.name === "Probe"); - expect(probe?.position).toEqual({ - path: "components/Wrapper.md", - offset: wrapper.indexOf(""), - line: 5, - column: 1, - }); - }); + const probe = positions.find((p) => p.name === "Probe"); + expect(probe?.position).toEqual({ + path: "components/Wrapper.md", + offset: wrapper.indexOf(""), + line: 5, + column: 1, + }); + }, + ); }); /** diff --git a/packages/core/tests/streaming-emission.test.ts b/packages/core/tests/streaming-emission.test.ts index c5bc48267..826e4fc9b 100644 --- a/packages/core/tests/streaming-emission.test.ts +++ b/packages/core/tests/streaming-emission.test.ts @@ -6,16 +6,15 @@ */ import { describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; -import { withFailureJournal } from "@executablemd/testing"; import { API } from "@executablemd/runtime"; import { useTempDirectory } from "@executablemd/test-support/temp"; -import { exists, readdir } from "@effectionx/fs"; -import { scoped } from "effection"; +import { exists, readdir, readTextFile } from "@effectionx/fs"; +import { ensure, scoped, spawn, suspend, withResolvers } from "effection"; import type { Operation } from "effection"; import { join } from "node:path"; import process from "node:process"; import { Writable } from "node:stream"; -import { useTestDiagnostics } from "../../cli/src/test-diagnostics.ts"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; import type { DeliverySink } from "../../cli/src/stdout-delivery.ts"; import { readRecords } from "../../cli/src/repl/journal-records.ts"; import { forEach } from "@effectionx/stream-helpers"; @@ -45,52 +44,63 @@ describe("Tier BC — Block ID counter", () => { const SIMPLE = "packages/core/tests/fixtures/streaming/simple.md"; const MULTI = "packages/core/tests/fixtures/streaming/multi-segment.md"; +const OWNER = "packages/core/tests/streaming-emission.test.ts"; +const SUITE = "Tier SE — Streaming emission"; + function runStreamingCase( + name: string, path: string, check: (chunks: string[], fullOutput: string) => void, sink: DeliverySink = process.stderr, ): Operation { - return scoped(function* () { - const diagnostics = yield* useTestDiagnostics(sink); - yield* withFailureJournal( - diagnostics.execution("root", path).journal(() => false), - function* (stream) { - const chunks: string[] = []; - const fullOutput = yield* scoped(function* () { - const execution = yield* execute({ path, stream }); - const output = yield* forEach(function* (chunk: string) { - chunks.push(chunk); - }, execution.output); - yield* execution; - return output; - }); - check(chunks, fullOutput); - }, - ); - }); + return captureTestJournal( + { owner: OWNER, suite: SUITE, name }, + path, + function* (stream) { + const chunks: string[] = []; + const fullOutput = yield* scoped(function* () { + const execution = yield* execute({ path, stream }); + const output = yield* forEach(function* (chunk: string) { + chunks.push(chunk); + }, execution.output); + yield* execution; + return output; + }); + check(chunks, fullOutput); + }, + { sink }, + ); } -describe("Tier SE — Streaming emission", () => { - it("SE1: segments emitted in document order via stream", () => - runStreamingCase(MULTI, (chunks, fullOutput) => { +describe(SUITE, () => { + const segments = "SE1: segments emitted in document order via stream"; + it(segments, () => + runStreamingCase(segments, MULTI, (chunks, fullOutput) => { expect(chunks.length).toBeGreaterThan(0); expect(fullOutput).toBe(chunks.join("")); - })); + }), + ); - it("SE10: no empty strings in output chunks", () => - runStreamingCase(SIMPLE, (chunks) => { + const nonempty = "SE10: no empty strings in output chunks"; + it(nonempty, () => + runStreamingCase(nonempty, SIMPLE, (chunks) => { for (const chunk of chunks) { expect(chunk.length).toBeGreaterThan(0); } - })); + }), + ); - it("SE9: output() inside durable workflow reaches stream consumer", () => - runStreamingCase(SIMPLE, (chunks) => { + const output = "SE9: output() inside durable workflow reaches stream consumer"; + it(output, () => + runStreamingCase(output, SIMPLE, (chunks) => { expect(chunks.length).toBeGreaterThan(0); expect(chunks.join("")).toContain("Hello"); - })); + }), + ); - it("saves the settled streaming journal only after a failing assertion", function* () { + const witness = + "saves a journal after an assertion fails and leaves a passing execution unwritten"; + it(witness, function* () { const directory = yield* useTempDirectory("xmd-streaming-journal-"); const failed = join(directory, "failed"); const passed = join(directory, "passed"); @@ -110,6 +120,7 @@ describe("Tier SE — Streaming emission", () => { }, }); yield* runStreamingCase( + witness, SIMPLE, function (chunks) { expect(chunks.join("")).toContain("Hello"); @@ -137,7 +148,20 @@ describe("Tier SE — Streaming emission", () => { const invocations = yield* readdir(failed); expect(invocations).toHaveLength(1); const directoryWithJournal = join(failed, invocations[0]); - expect(yield* readdir(directoryWithJournal)).toEqual(["root-1.jsonl"]); + expect((yield* readdir(directoryWithJournal)).sort()).toEqual([ + "root-1.case.json", + "root-1.jsonl", + ]); + expect(JSON.parse(yield* readTextFile(join(directoryWithJournal, "root-1.case.json")))).toEqual( + { + owner: OWNER, + suite: SUITE, + name: witness, + kind: "root", + ordinal: 1, + source: SIMPLE, + }, + ); const journal = join(directoryWithJournal, "root-1.jsonl"); expect(lines).toEqual([ `[xmd test] journal saved: ${journal} (root 1 ${SIMPLE}) @@ -150,4 +174,61 @@ describe("Tier SE — Streaming emission", () => { expect(yield* exists(passed)).toBe(false); expect(lines).toHaveLength(1); }); + const cancelled = "saves the streaming journal after cancellation joins teardown"; + it(cancelled, function* () { + const artifacts = join( + yield* useTempDirectory("xmd-streaming-cancellation-"), + ".xmd", + "test-artifacts", + ); + yield* API.Env.around({ + *env([name], next) { + return name === "XMD_TEST_ARTIFACTS" ? artifacts : yield* next(name); + }, + }); + const { resolve, operation } = withResolvers(); + const order: string[] = []; + const sink = new Writable({ + write(chunk, _encoding, done) { + order.push(String(chunk)); + done(); + }, + }); + const task = yield* spawn(() => + captureTestJournal( + { owner: OWNER, suite: SUITE, name: cancelled }, + SIMPLE, + function* (stream) { + const execution = yield* execute({ path: SIMPLE, stream }); + yield* forEach(function* (chunk: string) { + expect(chunk.length).toBeGreaterThan(0); + }, execution.output); + yield* execution; + yield* ensure(() => { + order.push("teardown"); + }); + resolve(); + yield* suspend(); + }, + { sink }, + ), + ); + yield* operation; + yield* task.halt(); + expect(order[0]).toBe("teardown"); + const invocations = yield* readdir(artifacts); + expect(invocations).toHaveLength(1); + const directory = join(artifacts, invocations[0]); + expect(JSON.parse(yield* readTextFile(join(directory, "root-1.case.json")))).toEqual({ + owner: OWNER, + suite: SUITE, + name: cancelled, + kind: "root", + ordinal: 1, + source: SIMPLE, + }); + const file = join(directory, "root-1.jsonl"); + expect(order[1]).toContain(file); + expect((yield* readRecords(file)).at(-1)).toMatchObject({ type: "close", coroutineId: "root" }); + }); }); diff --git a/packages/core/tests/support/held-host.ts b/packages/core/tests/support/held-host.ts index 1dd8cb2b8..f369f086c 100644 --- a/packages/core/tests/support/held-host.ts +++ b/packages/core/tests/support/held-host.ts @@ -17,13 +17,15 @@ import { resource, scoped, spawn, withResolvers } from "effection"; import type { Operation, Result, Task } from "effection"; -import { InMemoryStream } from "@executablemd/durable-streams"; +import type { InMemoryStream } from "@executablemd/durable-streams"; import type { Json as DurableJson } from "@executablemd/durable-streams"; import { executeInstalled } from "../../host.ts"; import type { ExecutionInstallation, HostExecution } from "../../host.ts"; import { retainedSource } from "../../src/root-source.ts"; import type { Json } from "../../src/types.ts"; +import { it } from "@executablemd/test-support/bdd"; +import { captureTestJournal, useJournalStream } from "@executablemd/test-support/failure-journal"; const NO_PROPS = { type: "object", properties: {}, additionalProperties: false } as const; @@ -34,6 +36,16 @@ const HOLD_COMPONENT = "Held"; export const HELD_ROOT_PATH = "host/held.md"; const HELD_ROOT_SOURCE = `<${HOLD_COMPONENT} />\n`; +export function heldHostCases(owner: string) { + return ( + name: string, + body: (stream: InMemoryStream) => Operation, + source = HELD_ROOT_PATH, + ): void => { + it(name, () => captureTestJournal({ owner, name }, source, body)); + }; +} + /** The document a held execution runs, for a case that needs its own. */ export interface HeldRoot { readonly path: string; @@ -112,8 +124,8 @@ export function useHeldHost(request: HeldHostRequest = {}): Operation }, ], }; - const stream = request.stream ?? new InMemoryStream(); const document = request.root ?? { path: HELD_ROOT_PATH, source: HELD_ROOT_SOURCE }; + const stream = request.stream ?? (yield* useJournalStream(document.path)); const execution = yield* executeInstalled( { ...retainedSource(document.path, document.source), stream }, [...(request.installations ?? []), installation], diff --git a/packages/core/tests/switch.test.ts b/packages/core/tests/switch.test.ts index 5a59b6cfc..6ebf803c3 100644 --- a/packages/core/tests/switch.test.ts +++ b/packages/core/tests/switch.test.ts @@ -22,6 +22,9 @@ import type { SourceOrigin } from "../src/scanner.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import type { ComponentElement, FunctionComponentDefinition, Segment } from "../src/types.ts"; import { asText } from "./helpers.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/switch.test.ts"); interface SwitchRun { segments: Segment[]; @@ -833,55 +836,59 @@ describe("Tier SWITCH / CASE — provider boundary", () => { describe("Tier SWITCH / CASE — document execution", () => { beforeAll(() => useTempFileCompiler()); - it("SW55: a in the selected case satisfies the value body that owns it", function* () { - yield* useStubFs({ - "Verdict.md": [ - "---", - "returns:", - " type: string", - "---", - "", - '', - '', - '', - "", - "", - ].join("\n"), - "test.md": '\n\nGot {v}\n', - }); - yield* useEchoExec(); + journalCase( + "SW55: a in the selected case satisfies the value body that owns it", + "test.md", + function* (journal) { + yield* useStubFs({ + "Verdict.md": [ + "---", + "returns:", + " type: string", + "---", + "", + '', + '', + '', + "", + "", + ].join("\n"), + "test.md": '\n\nGot {v}\n', + }); + yield* useEchoExec(); - const output = asText( - yield* collect(yield* execute({ path: "test.md", stream: new InMemoryStream() })), - ); - expect(output).toContain("Got picked"); - expect(output).not.toContain("held"); - }); + const output = asText(yield* collect(yield* execute({ path: "test.md", stream: journal }))); + expect(output).toContain("Got picked"); + expect(output).not.toContain("held"); + }, + ); - it("SW56: a projected through reads the caller's bindings", function* () { - yield* useStubFs({ - "components/Wrap.md": "", - "test.md": [ - "```js eval", - "const state = 'waiting';", - "```", - "", - "", - "READY", - "WAITING", - "", - "", - ].join("\n"), - }); - yield* useEchoExec(); + journalCase( + "SW56: a projected through reads the caller's bindings", + "test.md", + function* (journal) { + yield* useStubFs({ + "components/Wrap.md": "", + "test.md": [ + "```js eval", + "const state = 'waiting';", + "```", + "", + "", + "READY", + "WAITING", + "", + "", + ].join("\n"), + }); + yield* useEchoExec(); - const output = asText( - yield* collect(yield* execute({ path: "test.md", stream: new InMemoryStream() })), - ); - expect(output).toContain("WAITING"); - expect(output).not.toContain("READY"); - expect(output).not.toContain("ERROR"); - }); + const output = asText(yield* collect(yield* execute({ path: "test.md", stream: journal }))); + expect(output).toContain("WAITING"); + expect(output).not.toContain("READY"); + expect(output).not.toContain("ERROR"); + }, + ); it("SW57: only the selected case reaches the journal", function* () { const stream = new InMemoryStream(); diff --git a/packages/core/tests/validation-integration.test.ts b/packages/core/tests/validation-integration.test.ts index d37ec507b..4094e4198 100644 --- a/packages/core/tests/validation-integration.test.ts +++ b/packages/core/tests/validation-integration.test.ts @@ -2,7 +2,6 @@ import { describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { scoped } from "effection"; import type { Operation } from "effection"; -import { InMemoryStream } from "@executablemd/durable-streams"; import { useStubFs } from "@executablemd/runtime/test"; import { expandSegments } from "../src/expand.ts"; import { Component } from "../src/component-api.ts"; @@ -13,6 +12,9 @@ import { asText } from "./helpers.ts"; import { parseFrontmatter } from "../src/frontmatter.ts"; import { compilePropsSchema } from "../src/validate.ts"; import type { ComponentDefinition, EvalEnv, Json, Segment } from "../src/types.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const journalCase = journalCases("packages/core/tests/validation-integration.test.ts"); function markdownComponent(name: string, props: Record): ComponentDefinition { return { @@ -101,7 +103,7 @@ describe("prop-validation error segment", () => { // A full `inputs` schema parses — `inputs` lands in meta — so the failure // surfaces later, when a caller passes a prop the component does not declare. describe("`inputs` is not a compatibility alias for `props`", () => { - it("a component declaring `inputs` declares no props", function* () { + journalCase("a component declaring `inputs` declares no props", "README.md", function* (journal) { yield* useStubFs({ "README.md": '\n', "Legacy.md": [ @@ -118,9 +120,7 @@ describe("`inputs` is not a compatibility alias for `props`", () => { }); let message = ""; try { - message = asText( - yield* collect(yield* execute({ path: "README.md", stream: new InMemoryStream() })), - ); + message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); } catch (error) { message = error instanceof Error ? error.message : String(error); } @@ -129,75 +129,81 @@ describe("`inputs` is not a compatibility alias for `props`", () => { }); describe("definition-loading rejects invalid props schemas", () => { - it("rejects an async schema at the Markdown load boundary", function* () { - yield* useStubFs({ - "README.md": "\n", - "Bad.md": [ - "---", - "props:", - " $async: true", - " type: object", - " properties: {}", - " additionalProperties: false", - "---", - "body", - "", - ].join("\n"), - }); - let message = ""; - try { - message = asText( - yield* collect(yield* execute({ path: "README.md", stream: new InMemoryStream() })), - ); - } catch (error) { - message = error instanceof Error ? error.message : String(error); - } - expect(message).toContain("async"); - }); + journalCase( + "rejects an async schema at the Markdown load boundary", + "README.md", + function* (journal) { + yield* useStubFs({ + "README.md": "\n", + "Bad.md": [ + "---", + "props:", + " $async: true", + " type: object", + " properties: {}", + " additionalProperties: false", + "---", + "body", + "", + ].join("\n"), + }); + let message = ""; + try { + message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); + } catch (error) { + message = error instanceof Error ? error.message : String(error); + } + expect(message).toContain("async"); + }, + ); - it("rejects a reserved-name schema at the Markdown load boundary", function* () { - yield* useStubFs({ - "README.md": "\n", - "Bad.md": [ - "---", - "props:", - " type: object", - " properties:", - " slot: { type: string }", - " additionalProperties: false", - "---", - "body", - "", - ].join("\n"), - }); - let message = ""; - try { - message = asText( - yield* collect(yield* execute({ path: "README.md", stream: new InMemoryStream() })), - ); - } catch (error) { - message = error instanceof Error ? error.message : String(error); - } - expect(message).toContain("reserved"); - }); + journalCase( + "rejects a reserved-name schema at the Markdown load boundary", + "README.md", + function* (journal) { + yield* useStubFs({ + "README.md": "\n", + "Bad.md": [ + "---", + "props:", + " type: object", + " properties:", + " slot: { type: string }", + " additionalProperties: false", + "---", + "body", + "", + ].join("\n"), + }); + let message = ""; + try { + message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); + } catch (error) { + message = error instanceof Error ? error.message : String(error); + } + expect(message).toContain("reserved"); + }, + ); - it("rejects a reserved name declared through a props map", function* () { - yield* useStubFs({ - "README.md": "\n", - "Bad.md": ["---", "props:", " slot: { type: string }", "---", "body", ""].join("\n"), - }); - let message = ""; - try { - message = asText( - yield* collect(yield* execute({ path: "README.md", stream: new InMemoryStream() })), - ); - } catch (error) { - message = error instanceof Error ? error.message : String(error); - } - // Normalization runs first, so the contract still sees `slot` under - // `properties`. - expect(message).toContain("reserved"); - }); + journalCase( + "rejects a reserved name declared through a props map", + "README.md", + function* (journal) { + yield* useStubFs({ + "README.md": "\n", + "Bad.md": ["---", "props:", " slot: { type: string }", "---", "body", ""].join("\n"), + }); + let message = ""; + try { + message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); + } catch (error) { + message = error instanceof Error ? error.message : String(error); + } + // Normalization runs first, so the contract still sees `slot` under + // `properties`. + expect(message).toContain("reserved"); + }, + ); }); describe("the root-object contract applies to the normalized schema", () => { diff --git a/packages/git/tests/git-observations-deno.test.ts b/packages/git/tests/git-observations-deno.test.ts index 67a29ba03..58d4ede7a 100644 --- a/packages/git/tests/git-observations-deno.test.ts +++ b/packages/git/tests/git-observations-deno.test.ts @@ -36,7 +36,7 @@ import process from "node:process"; import type { Json } from "@executablemd/durable-streams"; import { capturingProbe, writeFixtureFile } from "./support/observations.ts"; import { - runOrdinaryDocument, + ordinaryDocumentCases, useHostCheckout, useManagedRoot, useOriginlessCheckout, @@ -44,6 +44,8 @@ import { import { useBareRemote } from "./support/git-remotes.ts"; import { REMOTE } from "./support/run-composition-tier.ts"; import { denoRepositoryHost } from "../src/deno/composition/host.ts"; + +const ordinaryCase = ordinaryDocumentCases("packages/git/tests/git-observations-deno.test.ts"); import type { GitBytes, GitInvocation, @@ -68,58 +70,61 @@ function paths(value: Json | undefined): string[] { } describe("GO9 — which checkout answers, through a managed selection", () => { - it("reads the ambient checkout, a lexical Repository, a Worktree and a Dir inside one", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const ambient = yield* useHostCheckout(remote.locator); - yield* writeFixtureFile(ambient.root, "only-here.txt", "ambient\n"); - ambient.run("add", "--", "only-here.txt"); - ambient.run("commit", "-m", "ambient only"); - const seen: Json[] = []; + ordinaryCase( + "reads the ambient checkout, a lexical Repository, a Worktree and a Dir inside one", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const ambient = yield* useHostCheckout(remote.locator); + yield* writeFixtureFile(ambient.root, "only-here.txt", "ambient\n"); + ambient.run("add", "--", "only-here.txt"); + ambient.run("commit", "-m", "ambient only"); + const seen: Json[] = []; - yield* runOrdinaryDocument( - [ - '', - "", - "", - "", - ``, - "", - '', - "", - "", - "", - '', - "", - '', - "", - "", - "", - '', - "", - '', - "", - "", - "", - "", - "", - "", - "", - "", - "", - ].join("\n"), - { root, cwd: ambient.root, components: [capturingProbe(seen)] }, - ); + yield* runFresh( + [ + '', + "", + "", + "", + ``, + "", + '', + "", + "", + "", + '', + "", + '', + "", + "", + "", + '', + "", + '', + "", + "", + "", + "", + "", + "", + "", + "", + "", + ].join("\n"), + { root, cwd: ambient.root, components: [capturingProbe(seen)] }, + ); - // The ambient checkout holds a path the managed clone never received, which - // is how a read that answered from the wrong one would say so. - expect(paths(seen[0])).toContain("only-here.txt"); - expect(paths(seen[1])).toEqual(["which.txt"]); - expect(object(seen[2]).changes).toEqual([]); - // A `` beneath a Worktree stays inside that checkout, and the answer - // is still relative to its repository root. - expect(paths(seen[3])).toEqual(["which.txt"]); - }); + // The ambient checkout holds a path the managed clone never received, which + // is how a read that answered from the wrong one would say so. + expect(paths(seen[0])).toContain("only-here.txt"); + expect(paths(seen[1])).toEqual(["which.txt"]); + expect(object(seen[2]).changes).toEqual([]); + // A `` beneath a Worktree stays inside that checkout, and the answer + // is still relative to its repository root. + expect(paths(seen[3])).toEqual(["which.txt"]); + }, + ); }); /** What a watched `git` child reported about its own lifetime. */ @@ -222,57 +227,60 @@ function watchingHost( } describe("GO9 — what an interrupted native read leaves behind", () => { - it("joins the read's own child and removes its scratch before settling", function* () { - const checkout = yield* useOriginlessCheckout(); - const root = yield* useManagedRoot(); - yield* writeFixtureFile(checkout.root, "src/a.ts", "export const a = 1;\n"); - checkout.run("add", "--", "src/a.ts"); - checkout.run("commit", "-m", "a"); - yield* writeFixtureFile(checkout.root, "src/a.ts", "export const a = 11;\n"); + ordinaryCase( + "joins the read's own child and removes its scratch before settling", + function* (runFresh) { + const checkout = yield* useOriginlessCheckout(); + const root = yield* useManagedRoot(); + yield* writeFixtureFile(checkout.root, "src/a.ts", "export const a = 1;\n"); + checkout.run("add", "--", "src/a.ts"); + checkout.run("commit", "-m", "a"); + yield* writeFixtureFile(checkout.root, "src/a.ts", "export const a = 11;\n"); - const watched = watchChildren(); - const scratch: string[] = []; - const marker = join(checkout.home, "read-ran"); - const script = join(checkout.home, "waiting-fsmonitor"); - yield* waitingScript(script, marker); + const watched = watchChildren(); + const scratch: string[] = []; + const marker = join(checkout.home, "read-ran"); + const script = join(checkout.home, "waiting-fsmonitor"); + yield* waitingScript(script, marker); - // The comparison refreshes the index before it answers, and a file-system - // monitor is consulted while it does. This one reports that it ran and then - // waits, holding the pipes the parent inherited — which is what makes the - // cut land on a real child rather than between commands. - const intercept = (invocation: GitInvocation): Partial | undefined => - invocation.args.includes("diff") - ? { - args: ["-c", `core.fsmonitor=${script}`, ...invocation.args], - observe: watched.observe, - } - : undefined; + // The comparison refreshes the index before it answers, and a file-system + // monitor is consulted while it does. This one reports that it ran and then + // waits, holding the pipes the parent inherited — which is what makes the + // cut land on a real child rather than between commands. + const intercept = (invocation: GitInvocation): Partial | undefined => + invocation.args.includes("diff") + ? { + args: ["-c", `core.fsmonitor=${script}`, ...invocation.args], + observe: watched.observe, + } + : undefined; - yield* scoped(function* () { - const task = yield* spawn(() => - runOrdinaryDocument( - [ - '', - 'sentinel', - ].join("\n"), - { root, cwd: checkout.root, host: watchingHost(scratch, intercept) }, - ), - ); - // The marker says the descendant really ran and is holding the pipes. - yield* appears(marker); - yield* watched.spawned; - yield* task.halt(); - }); + yield* scoped(function* () { + const task = yield* spawn(() => + runFresh( + [ + '', + 'sentinel', + ].join("\n"), + { root, cwd: checkout.root, host: watchingHost(scratch, intercept) }, + ), + ); + // The marker says the descendant really ran and is holding the pipes. + yield* appears(marker); + yield* watched.spawned; + yield* task.halt(); + }); - // The child is gone and both its pipes have ended; the scratch the read - // owned is gone with it; and nothing after the read ran. - expect(watched.observed() > 0).toBe(true); - expect(watched.closed()).toBe(true); - expect(watched.alive()).toBe(false); - expect(scratch.length > 0).toBe(true); - for (const directory of scratch) { - expect(yield* exists(directory)).toBe(false); - } - expect(yield* exists(join(checkout.root, "sentinel.md"))).toBe(false); - }); + // The child is gone and both its pipes have ended; the scratch the read + // owned is gone with it; and nothing after the read ran. + expect(watched.observed() > 0).toBe(true); + expect(watched.closed()).toBe(true); + expect(watched.alive()).toBe(false); + expect(scratch.length > 0).toBe(true); + for (const directory of scratch) { + expect(yield* exists(directory)).toBe(false); + } + expect(yield* exists(join(checkout.root, "sentinel.md"))).toBe(false); + }, + ); }); diff --git a/packages/git/tests/git-observations.test.ts b/packages/git/tests/git-observations.test.ts index 78fb83332..802fad39e 100644 --- a/packages/git/tests/git-observations.test.ts +++ b/packages/git/tests/git-observations.test.ts @@ -56,6 +56,7 @@ import { writeFixtureFile, } from "./support/observations.ts"; import { + ordinaryDocumentCases, commonDirectoryOf, countingOrdinaryHost, raised, @@ -95,6 +96,8 @@ import type { import { denoRepositoryHost } from "../src/deno/composition/host.ts"; import type { GitInvocation, RepositoryHost } from "../src/deno/composition/host.ts"; import { performGitObservation } from "../src/deno/composition/observations.ts"; + +const ordinaryCase = ordinaryDocumentCases("packages/git/tests/git-observations.test.ts"); import type { ObservationPlace } from "../src/deno/composition/observations.ts"; /** @@ -387,12 +390,12 @@ function paths(value: Json | undefined): string[] { } describe("GO1 — discovery addresses the chosen repository", () => { - it("lists the tracked index paths and nothing else", function* () { + ordinaryCase("lists the tracked index paths and nothing else", function* (runFresh) { const root = yield* useManagedRoot(); const fixture = yield* useObservationFixture(); const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( ['', "", "", ""].join("\n"), { root, cwd: fixture.root, components: [capturingProbe(seen)] }, ); @@ -417,56 +420,62 @@ describe("GO1 — discovery addresses the chosen repository", () => { }); describe("GO2 — discovery excludes what the index does not hold", () => { - it("excludes untracked files, a nested checkout's contents and a sibling checkout", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - yield* writeFixtureFile(fixture.root, "untracked.md", "not tracked\n"); - const seen: Json[] = []; - - yield* runOrdinaryDocument( - ['', "", "", ""].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + ordinaryCase( + "excludes untracked files, a nested checkout's contents and a sibling checkout", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + yield* writeFixtureFile(fixture.root, "untracked.md", "not tracked\n"); + const seen: Json[] = []; + + yield* runFresh( + ['', "", "", ""].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - const listed = paths(seen[0]); - expect(listed).not.toContain("untracked.md"); - // The tracked gitlink is one path. Nothing beneath it, and nothing in the - // untracked checkout beside it, is a path this repository's index holds. - expect(listed).toContain("nested"); - expect(listed.filter((path) => path.startsWith("nested/"))).toEqual([]); - expect(listed.filter((path) => path.startsWith("scratch-repo"))).toEqual([]); - }); + const listed = paths(seen[0]); + expect(listed).not.toContain("untracked.md"); + // The tracked gitlink is one path. Nothing beneath it, and nothing in the + // untracked checkout beside it, is a path this repository's index holds. + expect(listed).toContain("nested"); + expect(listed.filter((path) => path.startsWith("nested/"))).toEqual([]); + expect(listed.filter((path) => path.startsWith("scratch-repo"))).toEqual([]); + }, + ); - it("matches prefix literally and answers root-relative paths from a subdirectory", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const seen: Json[] = []; + ordinaryCase( + "matches prefix literally and answers root-relative paths from a subdirectory", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const seen: Json[] = []; - yield* runOrdinaryDocument( - [ - '', - "", - '', - "", - "", - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + yield* runFresh( + [ + '', + "", + '', + "", + "", + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - // An incomplete final segment is a prefix, and the answer is relative to - // the repository root rather than to the directory the element was in. - expect(paths(seen[0])).toEqual(["src/a.ts", "src/added.ts"]); - }); + // An incomplete final segment is a prefix, and the answer is relative to + // the repository root rather than to the directory the element was in. + expect(paths(seen[0])).toEqual(["src/a.ts", "src/added.ts"]); + }, + ); - it("reads a prefix holding glob characters as those characters", function* () { + ordinaryCase("reads a prefix holding glob characters as those characters", function* (runFresh) { const root = yield* useManagedRoot(); const fixture = yield* useObservationFixture(); const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ ``, "", @@ -481,12 +490,12 @@ describe("GO2 — discovery excludes what the index does not hold", () => { }); describe("GO3 — the three comparisons", () => { - it("compares two commits with every status the fixture produces", function* () { + ordinaryCase("compares two commits with every status the fixture produces", function* (runFresh) { const root = yield* useManagedRoot(); const fixture = yield* useObservationFixture(); const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ ``, "", @@ -514,38 +523,41 @@ describe("GO3 — the three comparisons", () => { expect(result.next).toBe(null); }); - it("compares the index with the working tree, and HEAD with the index", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - yield* writeFixtureFile(fixture.root, "src/a.ts", "export const a = 2;\n"); - yield* writeFixtureFile(fixture.root, "staged.ts", "export const staged = true;\n"); - fixture.run("add", "--", "staged.ts"); - yield* writeFixtureFile(fixture.root, "untracked.md", "not tracked\n"); - const seen: Json[] = []; - - yield* runOrdinaryDocument( - [ - '', - "", - "", - "", - '', - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + ordinaryCase( + "compares the index with the working tree, and HEAD with the index", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + yield* writeFixtureFile(fixture.root, "src/a.ts", "export const a = 2;\n"); + yield* writeFixtureFile(fixture.root, "staged.ts", "export const staged = true;\n"); + fixture.run("add", "--", "staged.ts"); + yield* writeFixtureFile(fixture.root, "untracked.md", "not tracked\n"); + const seen: Json[] = []; + + yield* runFresh( + [ + '', + "", + "", + "", + '', + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - const unstaged = object(seen[0]); - expect(unstaged.comparison).toEqual({ compare: "unstaged" }); - // The untracked file is not a tracked change, so it is not here. - expect(unstaged.changes).toEqual([{ path: "src/a.ts", status: "M", previousPath: null }]); + const unstaged = object(seen[0]); + expect(unstaged.comparison).toEqual({ compare: "unstaged" }); + // The untracked file is not a tracked change, so it is not here. + expect(unstaged.changes).toEqual([{ path: "src/a.ts", status: "M", previousPath: null }]); - const staged = object(seen[1]); - expect(staged.comparison).toEqual({ compare: "staged", base: fixture.c2 }); - expect(staged.changes).toEqual([{ path: "staged.ts", status: "A", previousPath: null }]); - }); + const staged = object(seen[1]); + expect(staged.comparison).toEqual({ compare: "staged", base: fixture.c2 }); + expect(staged.changes).toEqual([{ path: "staged.ts", status: "A", previousPath: null }]); + }, + ); // Driven at the native performer rather than through a document, because no // installed provider can select an unborn repository: a selection's identity @@ -577,114 +589,123 @@ describe("GO3 — the three comparisons", () => { }); describe("GO4 — the patch a comparison produces", () => { - it("answers the comparison's own unified text, with Git's binary and mode metadata", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const seen: Json[] = []; - - yield* runOrdinaryDocument( - [ - ``, - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + ordinaryCase( + "answers the comparison's own unified text, with Git's binary and mode metadata", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const seen: Json[] = []; + + yield* runFresh( + [ + ``, + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - const result = object(seen[0]); - const patch = String(result.text); - expect(result.comparison).toEqual({ compare: "commits", base: fixture.c1, head: fixture.c2 }); - // Every shape the fixture produces, as patch text rather than as a summary. - expect(patch).toContain("rename from doc/orig.md"); - expect(patch).toContain("rename to doc/renamed.md"); - expect(patch).toContain("Binary files a/src/bin.dat and b/src/bin.dat differ"); - expect(patch).toContain("old mode 100644"); - expect(patch).toContain("new mode 100755"); - expect(patch).toContain("new file mode 100644"); - expect(patch).toContain("deleted file mode 100644"); - expect(patch).toContain("Subproject commit"); - expect(result.offset).toBe(0); - expect(result.length).toBe([...patch].length); - expect(result.next).toBe(null); - }); + const result = object(seen[0]); + const patch = String(result.text); + expect(result.comparison).toEqual({ compare: "commits", base: fixture.c1, head: fixture.c2 }); + // Every shape the fixture produces, as patch text rather than as a summary. + expect(patch).toContain("rename from doc/orig.md"); + expect(patch).toContain("rename to doc/renamed.md"); + expect(patch).toContain("Binary files a/src/bin.dat and b/src/bin.dat differ"); + expect(patch).toContain("old mode 100644"); + expect(patch).toContain("new mode 100755"); + expect(patch).toContain("new file mode 100644"); + expect(patch).toContain("deleted file mode 100644"); + expect(patch).toContain("Subproject commit"); + expect(result.offset).toBe(0); + expect(result.length).toBe([...patch].length); + expect(result.next).toBe(null); + }, + ); - it("narrows to literal files and directories, matching a rename on either side", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const seen: Json[] = []; + ordinaryCase( + "narrows to literal files and directories, matching a rename on either side", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const seen: Json[] = []; - yield* runOrdinaryDocument( - [ - ``, - "", - "", - "", - ``, - "", - "", - "", - ``, - "", - "", - "", - ``, - "", - "", - "", - ``, - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + yield* runFresh( + [ + ``, + "", + "", + "", + ``, + "", + "", + "", + ``, + "", + "", + "", + ``, + "", + "", + "", + ``, + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - expect(object(seen[0]).changes).toEqual([ - { path: "src/added.ts", status: "A", previousPath: null }, - { path: "src/bin.dat", status: "M", previousPath: null }, - ]); - // The old path selects the whole rename record, not half of it. - expect(object(seen[1]).changes).toEqual([ - { path: "doc/renamed.md", status: "R", previousPath: "doc/orig.md" }, - ]); - expect(String(object(seen[2]).text)).toContain("rename from doc/orig.md"); - // An empty selector is a thing a document says, and it selects nothing. - expect(object(seen[3]).changes).toEqual([]); - expect(object(seen[4]).text).toBe(""); - expect(object(seen[4]).length).toBe(0); - expect(object(seen[4]).next).toBe(null); - }); + expect(object(seen[0]).changes).toEqual([ + { path: "src/added.ts", status: "A", previousPath: null }, + { path: "src/bin.dat", status: "M", previousPath: null }, + ]); + // The old path selects the whole rename record, not half of it. + expect(object(seen[1]).changes).toEqual([ + { path: "doc/renamed.md", status: "R", previousPath: "doc/orig.md" }, + ]); + expect(String(object(seen[2]).text)).toContain("rename from doc/orig.md"); + // An empty selector is a thing a document says, and it selects nothing. + expect(object(seen[3]).changes).toEqual([]); + expect(object(seen[4]).text).toBe(""); + expect(object(seen[4]).length).toBe(0); + expect(object(seen[4]).next).toBe(null); + }, + ); - it("keeps a tracked gitlink's moved pointer without reading inside it", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const seen: Json[] = []; + ordinaryCase( + "keeps a tracked gitlink's moved pointer without reading inside it", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const seen: Json[] = []; - yield* runOrdinaryDocument( - [ - ``, - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + yield* runFresh( + [ + ``, + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - const patch = String(object(seen[0]).text); - expect(patch).toContain(`-Subproject commit ${fixture.gitlink.before}`); - expect(patch).toContain(`+Subproject commit ${fixture.gitlink.after}`); - // One path and one pointer. Nothing beneath the gitlink is read, so the - // nested repository's own file never appears. - expect(patch).not.toContain("inner.txt"); - }); + const patch = String(object(seen[0]).text); + expect(patch).toContain(`-Subproject commit ${fixture.gitlink.before}`); + expect(patch).toContain(`+Subproject commit ${fixture.gitlink.after}`); + // One path and one pointer. Nothing beneath the gitlink is read, so the + // nested repository's own file never appears. + expect(patch).not.toContain("inner.txt"); + }, + ); - it("collapses an unmerged path to one record", function* () { + ordinaryCase("collapses an unmerged path to one record", function* (runFresh) { const root = yield* useManagedRoot(); const conflicted = yield* useConflictedCheckout(); const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ '', "", @@ -749,7 +770,7 @@ describe("GO5 — pages are bounded, complete, and about one observation", () => expect(object(whole[0]).next).toBe(null); }); - it("pages changes at a limit of one and at the default", function* () { + ordinaryCase("pages changes at a limit of one and at the default", function* (runFresh) { const root = yield* useManagedRoot(); const fixture = yield* useGeneratedFixture(1002); for (const path of fixture.paths) { @@ -757,7 +778,7 @@ describe("GO5 — pages are bounded, complete, and about one observation", () => } const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ '', "", @@ -890,12 +911,12 @@ describe("GO5 — pages are bounded, complete, and about one observation", () => }); describe("GO6 — a changed observation cannot be paged over", () => { - it("refuses a file listing whose paths changed between pages", function* () { + ordinaryCase("refuses a file listing whose paths changed between pages", function* (runFresh) { const root = yield* useManagedRoot(); const fixture = yield* useGeneratedFixture(150); const raised_ = yield* raised( - runOrdinaryDocument( + runFresh( [ '', "", @@ -927,114 +948,123 @@ describe("GO6 — a changed observation cannot be paged over", () => { // editing a path already reported as modified leaves the normalized listing // identical, and a read that restarted for that would refuse a page it could // honestly answer. So this adds a record the first page never described. - it("refuses a changes listing whose records changed between pages", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useGeneratedFixture(150); - for (const path of fixture.paths.slice(0, 120)) { - yield* writeFixtureFile(fixture.root, path, "changed\n"); - } + ordinaryCase( + "refuses a changes listing whose records changed between pages", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useGeneratedFixture(150); + for (const path of fixture.paths.slice(0, 120)) { + yield* writeFixtureFile(fixture.root, path, "changed\n"); + } - const raised_ = yield* raised( - runOrdinaryDocument( - [ - '', - "", - "", - "", - '', - "", - ].join("\n"), - { - root, - cwd: fixture.root, - components: [ - actionProbe("Mutates", function* () { - yield* writeFixtureFile(fixture.root, fixture.paths[149] ?? "", "changed too\n"); - }), - ], - }, - ), - ); + const raised_ = yield* raised( + runFresh( + [ + '', + "", + "", + "", + '', + "", + ].join("\n"), + { + root, + cwd: fixture.root, + components: [ + actionProbe("Mutates", function* () { + yield* writeFixtureFile(fixture.root, fixture.paths[149] ?? "", "changed too\n"); + }), + ], + }, + ), + ); - expect(reasonOf(raised_)).toBe("stale-continuation"); - expect(messageOf(raised_)).toBe( - "The Git observation changed. Restart without cursor.", - ); - }); + expect(reasonOf(raised_)).toBe("stale-continuation"); + expect(messageOf(raised_)).toBe( + "The Git observation changed. Restart without cursor.", + ); + }, + ); + + ordinaryCase( + "refuses a patch whose text changed between windows, and names offset zero", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useGeneratedFixture(150); + for (const path of fixture.paths) { + yield* writeFixtureFile(fixture.root, path, "changed\n"); + } - it("refuses a patch whose text changed between windows, and names offset zero", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useGeneratedFixture(150); - for (const path of fixture.paths) { - yield* writeFixtureFile(fixture.root, path, "changed\n"); - } + const raised_ = yield* raised( + runFresh( + [ + '', + "", + "", + "", + '', + "", + ].join("\n"), + { + root, + cwd: fixture.root, + components: [ + actionProbe("Mutates", function* () { + yield* writeFixtureFile(fixture.root, fixture.paths[0] ?? "", "changed again\n"); + }), + ], + }, + ), + ); - const raised_ = yield* raised( - runOrdinaryDocument( + expect(reasonOf(raised_)).toBe("stale-continuation"); + expect(messageOf(raised_)).toBe( + "The Git observation changed. Restart without cursor and offset={0}.", + ); + }, + ); + + ordinaryCase( + "follows a continuation unchanged when the repository changed elsewhere", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useGeneratedFixture(150); + const seen: Json[] = []; + + yield* runFresh( [ - '', + '', "", "", "", - '', + '', + "", + "", "", ].join("\n"), { root, cwd: fixture.root, components: [ + capturingProbe(seen), actionProbe("Mutates", function* () { - yield* writeFixtureFile(fixture.root, fixture.paths[0] ?? "", "changed again\n"); + // Outside the selected observation: an untracked file changes no + // tracked path, so this page is still a page of the same answer. + yield* writeFixtureFile(fixture.root, "untracked-elsewhere.txt", "elsewhere\n"); }), ], }, - ), - ); - - expect(reasonOf(raised_)).toBe("stale-continuation"); - expect(messageOf(raised_)).toBe( - "The Git observation changed. Restart without cursor and offset={0}.", - ); - }); - - it("follows a continuation unchanged when the repository changed elsewhere", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useGeneratedFixture(150); - const seen: Json[] = []; - - yield* runOrdinaryDocument( - [ - '', - "", - "", - "", - '', - "", - "", - "", - ].join("\n"), - { - root, - cwd: fixture.root, - components: [ - capturingProbe(seen), - actionProbe("Mutates", function* () { - // Outside the selected observation: an untracked file changes no - // tracked path, so this page is still a page of the same answer. - yield* writeFixtureFile(fixture.root, "untracked-elsewhere.txt", "elsewhere\n"); - }), - ], - }, - ); + ); - expect(paths(seen[0])).toEqual([ - "gen/file-00005.txt", - "gen/file-00006.txt", - "gen/file-00007.txt", - "gen/file-00008.txt", - "gen/file-00009.txt", - ]); - }); + expect(paths(seen[0])).toEqual([ + "gen/file-00005.txt", + "gen/file-00006.txt", + "gen/file-00007.txt", + "gen/file-00008.txt", + "gen/file-00009.txt", + ]); + }, + ); it("refuses a cursor that is malformed, for another read, or for another checkout", function* () { const root = yield* useManagedRoot(); @@ -1156,55 +1186,58 @@ describe("GO7 — a file is read as one commit holds it", () => { expect(windows.join("")).toBe(LONG_BLOB); }); - it("answers a middle window, an empty window past the end, and an empty file", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useFileFixture(); - const seen: Json[] = []; - const total = [...LONG_BLOB].length; - - yield* runOrdinaryDocument( - [ - ``, - "", - "", - "", - ``, - "", - "", - "", - ``, - "", - "", - "", - ``, - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + ordinaryCase( + "answers a middle window, an empty window past the end, and an empty file", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useFileFixture(); + const seen: Json[] = []; + const total = [...LONG_BLOB].length; - expect(object(seen[0]).text).toBe("aaaaa"); - expect(object(seen[0]).next).toEqual({ - commit: fixture.f1, - path: "big.txt", - offset: 15, - limit: 5, - }); + yield* runFresh( + [ + ``, + "", + "", + "", + ``, + "", + "", + "", + ``, + "", + "", + "", + ``, + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - for (const past of [object(seen[1]), object(seen[2])]) { - expect(past.text).toBe(""); - expect(past.length).toBe(total); - expect(past.next).toBe(null); - } - expect(object(seen[1]).offset).toBe(total); - expect(object(seen[2]).offset).toBe(total + 500); + expect(object(seen[0]).text).toBe("aaaaa"); + expect(object(seen[0]).next).toEqual({ + commit: fixture.f1, + path: "big.txt", + offset: 15, + limit: 5, + }); + + for (const past of [object(seen[1]), object(seen[2])]) { + expect(past.text).toBe(""); + expect(past.length).toBe(total); + expect(past.next).toBe(null); + } + expect(object(seen[1]).offset).toBe(total); + expect(object(seen[2]).offset).toBe(total + 500); - const empty = object(seen[3]); - expect(empty.text).toBe(""); - expect(empty.length).toBe(0); - expect(empty.next).toBe(null); - }); + const empty = object(seen[3]); + expect(empty.text).toBe(""); + expect(empty.length).toBe(0); + expect(empty.next).toBe(null); + }, + ); it("refuses an unusable revision, an absent path, a tree, a gitlink and a nontext blob", function* () { const root = yield* useManagedRoot(); @@ -1268,12 +1301,12 @@ describe("GO7 — a file is read as one commit holds it", () => { } }); - it("still reads a directory's own child by its exact path", function* () { + ordinaryCase("still reads a directory's own child by its exact path", function* (runFresh) { const root = yield* useManagedRoot(); const fixture = yield* useFileFixture(); const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ ``, "", @@ -1289,25 +1322,28 @@ describe("GO7 — a file is read as one commit holds it", () => { expect(object(seen[0]).text).toBe("inside\n"); }); - it("answers a symbolic link's own stored text rather than what it points at", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useFileFixture(); - const seen: Json[] = []; + ordinaryCase( + "answers a symbolic link's own stored text rather than what it points at", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useFileFixture(); + const seen: Json[] = []; - yield* runOrdinaryDocument( - [ - ``, - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + yield* runFresh( + [ + ``, + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - // The link's target, as the blob holds it. Nothing followed it. - expect(object(seen[0]).text).toBe("big.txt"); - expect(object(seen[0]).length).toBe(7); - }); + // The link's target, as the blob holds it. Nothing followed it. + expect(object(seen[0]).text).toBe("big.txt"); + expect(object(seen[0]).length).toBe(7); + }, + ); }); describe("GO8 — a read runs no repository-defined program and changes nothing", () => { @@ -1361,45 +1397,48 @@ describe("GO8 — a read runs no repository-defined program and changes nothing" } }); - it("reads a linked checkout's own split index and leaves all three untouched", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const linked = yield* useLinkedWorktree(fixture); - fixture.run("update-index", "--split-index"); - linked.run("update-index", "--split-index"); - yield* writeFixtureFile(linked.root, "src/a.ts", "export const a = 9;\n"); + ordinaryCase( + "reads a linked checkout's own split index and leaves all three untouched", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const linked = yield* useLinkedWorktree(fixture); + fixture.run("update-index", "--split-index"); + linked.run("update-index", "--split-index"); + yield* writeFixtureFile(linked.root, "src/a.ts", "export const a = 9;\n"); - const indexes = yield* indexFingerprints(fixture, linked); - const seen: Json[] = []; + const indexes = yield* indexFingerprints(fixture, linked); + const seen: Json[] = []; - yield* runOrdinaryDocument( - [ - '', - "", - "", - "", - '', - "", - "", - "", - '', - "", - "", - "", - ].join("\n"), - { root, cwd: linked.root, components: [capturingProbe(seen)] }, - ); + yield* runFresh( + [ + '', + "", + "", + "", + '', + "", + "", + "", + '', + "", + "", + "", + ].join("\n"), + { root, cwd: linked.root, components: [capturingProbe(seen)] }, + ); - // The linked checkout's own index answered: its working tree holds the - // edit, and the primary checkout's does not. - expect(paths(seen[0])).toContain("src/a.ts"); - expect(object(seen[1]).changes).toEqual([ - { path: "src/a.ts", status: "M", previousPath: null }, - ]); - expect(String(object(seen[2]).text)).toContain("+export const a = 9;"); + // The linked checkout's own index answered: its working tree holds the + // edit, and the primary checkout's does not. + expect(paths(seen[0])).toContain("src/a.ts"); + expect(object(seen[1]).changes).toEqual([ + { path: "src/a.ts", status: "M", previousPath: null }, + ]); + expect(String(object(seen[2]).text)).toContain("+export const a = 9;"); - expect(yield* indexFingerprints(fixture, linked)).toEqual(indexes); - }); + expect(yield* indexFingerprints(fixture, linked)).toEqual(indexes); + }, + ); }); describe("GO9 — which checkout answers, and what cannot choose one", () => { @@ -1482,39 +1521,42 @@ describe("GO9 — which checkout answers, and what cannot choose one", () => { }); describe("GO10 — the comparison sees an edit the index's own times would hide", () => { - it("reports a modification whose size and recorded time match the index entry", function* () { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const indexPath = join(fixture.root, ".git", "index"); - const indexTimes = yield* lstat(indexPath); - // The same length, and the index's own modification time. This is exactly - // the state Git calls racily clean: the cached stat data matches, so the - // only way to see the change is to compare content — and the only reason - // Git does is that the entry is no older than the index holding it. A - // private copy stamped with the current time destroys that relationship. - yield* writeFixtureFile(fixture.root, "src/a.ts", "export const a = 7;\n"); - yield* until(utimes(join(fixture.root, "src", "a.ts"), indexTimes.mtime, indexTimes.mtime)); - const seen: Json[] = []; - - yield* runOrdinaryDocument( - [ - '', - "", - "", - "", - '', - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + ordinaryCase( + "reports a modification whose size and recorded time match the index entry", + function* (runFresh) { + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const indexPath = join(fixture.root, ".git", "index"); + const indexTimes = yield* lstat(indexPath); + // The same length, and the index's own modification time. This is exactly + // the state Git calls racily clean: the cached stat data matches, so the + // only way to see the change is to compare content — and the only reason + // Git does is that the entry is no older than the index holding it. A + // private copy stamped with the current time destroys that relationship. + yield* writeFixtureFile(fixture.root, "src/a.ts", "export const a = 7;\n"); + yield* until(utimes(join(fixture.root, "src", "a.ts"), indexTimes.mtime, indexTimes.mtime)); + const seen: Json[] = []; + + yield* runFresh( + [ + '', + "", + "", + "", + '', + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - expect(object(seen[0]).changes).toEqual([ - { path: "src/a.ts", status: "M", previousPath: null }, - ]); - expect(String(object(seen[1]).text)).toContain("+export const a = 7;"); - }); + expect(object(seen[0]).changes).toEqual([ + { path: "src/a.ts", status: "M", previousPath: null }, + ]); + expect(String(object(seen[1]).text)).toContain("+export const a = 7;"); + }, + ); }); describe("GO11 — the authored contract, before a provider is reached", () => { diff --git a/packages/git/tests/issue-markdown.test.ts b/packages/git/tests/issue-markdown.test.ts index 1a8e17db9..41ee6ce19 100644 --- a/packages/git/tests/issue-markdown.test.ts +++ b/packages/git/tests/issue-markdown.test.ts @@ -20,6 +20,7 @@ import type { Operation } from "effection"; import { SOURCE_POSITION_FIELD } from "@executablemd/core"; import { issueYields, runScenario, useScenarioFixture } from "./support/issue-scenario.ts"; import type { ScenarioObservation } from "./support/issue-scenario.ts"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; /** The scenarios, in the order a reader meets them. */ const SCENARIOS = [ @@ -59,9 +60,27 @@ function* stated(observation: ScenarioObservation): Operation { for (const name of SCENARIOS) { - it(`${name} states a contract that holds`, function* () { - yield* stated(yield* runScenario(pathOf(name))); - }); + const title = `${name} states a contract that holds`; + const path = pathOf(name); + if (name === "IssueRead.test.md" || name === "IssueUpsert.test.md") { + it(title, () => + captureTestJournal( + { + owner: "packages/git/tests/issue-markdown.test.ts", + suite: "workflow Issue scenarios", + name: title, + }, + path, + function* (stream) { + yield* stated(yield* runScenario(path, stream)); + }, + ), + ); + } else { + it(title, function* () { + yield* stated(yield* runScenario(path)); + }); + } } it("IssueDurability.test.md states a contract that holds", function* () { diff --git a/packages/git/tests/run-composition-ambient.test.ts b/packages/git/tests/run-composition-ambient.test.ts index 772158508..e14f4b930 100644 --- a/packages/git/tests/run-composition-ambient.test.ts +++ b/packages/git/tests/run-composition-ambient.test.ts @@ -33,6 +33,7 @@ import { UnresolvedGitIdentityError } from "../src/deno/run-composition/errors.t import { useBareRemote } from "./support/git-remotes.ts"; import { gitHubSource } from "../src/deno/composition/github.ts"; import { + ordinaryDocumentCases, causedBy, commonDirectoryOf, countingOrdinaryHost, @@ -54,27 +55,32 @@ import { reviewRoutes, } from "./support/run-composition-tier.ts"; -describe("ORC3 — the ambient primary checkout", () => { - it("switches, stages and commits in the repository the command was run in", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); +const ordinaryCase = ordinaryDocumentCases("packages/git/tests/run-composition-ambient.test.ts"); - yield* runOrdinaryDocument( - [ - ``, - `ordinary`, - ``, - ``, - ].join("\n"), - { root, cwd: checkout.root }, - ); +describe("ORC3 — the ambient primary checkout", () => { + ordinaryCase( + "switches, stages and commits in the repository the command was run in", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + + yield* runFresh( + [ + ``, + `ordinary`, + ``, + ``, + ].join("\n"), + { root, cwd: checkout.root }, + ); - // The person's own checkout moved, and it is what a later `git` sees. - expect(checkout.run("rev-parse", "--abbrev-ref", "HEAD")).toBe("feature"); - expect(checkout.run("log", "-1", "--pretty=%s")).toBe("Write notes"); - expect(checkout.run("show", "--pretty=", "--name-only", "HEAD")).toContain("notes.md"); - }); + // The person's own checkout moved, and it is what a later `git` sees. + expect(checkout.run("rev-parse", "--abbrev-ref", "HEAD")).toBe("feature"); + expect(checkout.run("log", "-1", "--pretty=%s")).toBe("Write notes"); + expect(checkout.run("show", "--pretty=", "--name-only", "HEAD")).toContain("notes.md"); + }, + ); it("refuses every root element that needs a repository outside a Git checkout", function* () { const root = yield* useManagedRoot(); @@ -113,32 +119,35 @@ describe("ORC3 — the ambient primary checkout", () => { }); describe("ORC4 — the ambient linked worktree", () => { - it("follows the common directory for identity and the worktree root for work", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const primary = yield* useHostCheckout(remote.locator); - // A linked worktree made by hand, exactly as a person would. - const linked = `${primary.root}-linked`; - primary.run("worktree", "add", "-b", "sidecar", linked); - - const before = primary.run("rev-parse", "HEAD"); - - yield* scoped(function* () { - yield* runOrdinaryDocument( - [ - `here`, - ``, - ``, - ].join("\n"), - // The command is run *in the linked worktree*. - { root, cwd: linked }, - ); - }); - - // The worktree advanced; the primary checkout did not. - expect(primary.run("rev-parse", "HEAD")).toBe(before); - expect(primary.run("log", "-1", "--pretty=%s", "sidecar")).toBe("In the worktree"); - }); + ordinaryCase( + "follows the common directory for identity and the worktree root for work", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const primary = yield* useHostCheckout(remote.locator); + // A linked worktree made by hand, exactly as a person would. + const linked = `${primary.root}-linked`; + primary.run("worktree", "add", "-b", "sidecar", linked); + + const before = primary.run("rev-parse", "HEAD"); + + yield* scoped(function* () { + yield* runFresh( + [ + `here`, + ``, + ``, + ].join("\n"), + // The command is run *in the linked worktree*. + { root, cwd: linked }, + ); + }); + + // The worktree advanced; the primary checkout did not. + expect(primary.run("rev-parse", "HEAD")).toBe(before); + expect(primary.run("log", "-1", "--pretty=%s", "sidecar")).toBe("In the worktree"); + }, + ); }); describe("ORC5 — origin is not local admission", () => { @@ -196,25 +205,28 @@ describe("ORC5 — origin is not local admission", () => { } }); - it("opens a session and transports when there is an origin, so the counters can fail", function* () { - // The same counters, on a repository that *does* have an origin. Without - // this, every assertion above would pass on a counter that can never be - // incremented — which is the one way "nothing was reached" lies. - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const counting = countingOrdinaryHost(); + ordinaryCase( + "opens a session and transports when there is an origin, so the counters can fail", + function* (runFresh) { + // The same counters, on a repository that *does* have an origin. Without + // this, every assertion above would pass on a counter that can never be + // incremented — which is the one way "nothing was reached" lies. + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const counting = countingOrdinaryHost(); - yield* runOrdinaryDocument([``, ``].join("\n"), { - root, - cwd: checkout.root, - host: counting.host, - }); + yield* runFresh([``, ``].join("\n"), { + root, + cwd: checkout.root, + host: counting.host, + }); - expect(counting.counters.sessions).toEqual([remote.locator]); - expect(subcommands(counting.counters)).toContain("ls-remote"); - expect(subcommands(counting.counters)).toContain("push"); - }); + expect(counting.counters.sessions).toEqual([remote.locator]); + expect(subcommands(counting.counters)).toContain("ls-remote"); + expect(subcommands(counting.counters)).toContain("push"); + }, + ); it("reaches a Git host when one is configured, so those counters can fail too", function* () { const remote = yield* useBareRemote(REMOTE); @@ -241,53 +253,56 @@ describe("ORC5 — origin is not local admission", () => { }); describe("ORC6 — lexical working directories", () => { - it("restores the enclosing directory after a Worktree body and a Dir body", function* () { - yield* Permissions.around( - { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, - { at: "min" }, - ); - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - - // A relative `` path resolves against the contextual working - // directory, so where each one lands is where the document was standing. - const common = commonDirectoryOf(checkout); - yield* runOrdinaryDocument( - [ - `outer`, - ``, - ``, - `within`, - "", - "", - `bound`, - "", - `after`, - ].join("\n"), - { root, cwd: checkout.root }, - ); + ordinaryCase( + "restores the enclosing directory after a Worktree body and a Dir body", + function* (runFresh) { + yield* Permissions.around( + { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, + { at: "min" }, + ); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + + // A relative `` path resolves against the contextual working + // directory, so where each one lands is where the document was standing. + const common = commonDirectoryOf(checkout); + yield* runFresh( + [ + `outer`, + ``, + ``, + `within`, + "", + "", + `bound`, + "", + `after`, + ].join("\n"), + { root, cwd: checkout.root }, + ); - const lexical = worktreeSlotOf(root, common, "lexical"); - const bound = worktreeSlotOf(root, common, "inner"); - expect(yield* exists(`${checkout.root}/outer.md`)).toBe(true); - // Each body observed its own checkout. - expect(yield* exists(`${lexical.checkout}/within.md`)).toBe(true); - expect(yield* exists(`${bound.checkout}/bound.md`)).toBe(true); - // Restored: the sibling after both is back in the enclosing directory. - expect(yield* exists(`${checkout.root}/after.md`)).toBe(true); - expect(yield* exists(`${lexical.checkout}/after.md`)).toBe(false); - expect(yield* exists(`${bound.checkout}/after.md`)).toBe(false); - }); + const lexical = worktreeSlotOf(root, common, "lexical"); + const bound = worktreeSlotOf(root, common, "inner"); + expect(yield* exists(`${checkout.root}/outer.md`)).toBe(true); + // Each body observed its own checkout. + expect(yield* exists(`${lexical.checkout}/within.md`)).toBe(true); + expect(yield* exists(`${bound.checkout}/bound.md`)).toBe(true); + // Restored: the sibling after both is back in the enclosing directory. + expect(yield* exists(`${checkout.root}/after.md`)).toBe(true); + expect(yield* exists(`${lexical.checkout}/after.md`)).toBe(false); + expect(yield* exists(`${bound.checkout}/after.md`)).toBe(false); + }, + ); - it("restores the enclosing directory when the body fails", function* () { + ordinaryCase("restores the enclosing directory when the body fails", function* (runFresh) { const remote = yield* useBareRemote(REMOTE); const root = yield* useManagedRoot(); const checkout = yield* useHostCheckout(remote.locator); // The refusal is printed rather than fatal, so the document goes on — and // what it goes on in is the directory the Worktree body was installed over. - yield* runOrdinaryDocument( + yield* runFresh( [ "", ``, @@ -339,28 +354,31 @@ describe("ORC6 — lexical working directories", () => { }); describe("ORC13 — live local Git", () => { - it("makes real, non-transactional changes and claims no rollback on failure", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + ordinaryCase( + "makes real, non-transactional changes and claims no rollback on failure", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - const failure = yield* raised( - runOrdinaryDocument( - [ - ``, - `staged before the failure`, - ``, - ``, - ].join("\n"), - { root, cwd: checkout.root }, - ), - ); - expect(failure).toBeInstanceOf(Error); + const failure = yield* raised( + runFresh( + [ + ``, + `staged before the failure`, + ``, + ``, + ].join("\n"), + { root, cwd: checkout.root }, + ), + ); + expect(failure).toBeInstanceOf(Error); - // The switch and the first Add really happened, and nothing took them back. - expect(checkout.run("rev-parse", "--abbrev-ref", "HEAD")).toBe("partway"); - expect(checkout.run("diff", "--cached", "--name-only")).toContain("staged.md"); - }); + // The switch and the first Add really happened, and nothing took them back. + expect(checkout.run("rev-parse", "--abbrev-ref", "HEAD")).toBe("partway"); + expect(checkout.run("diff", "--cached", "--name-only")).toContain("staged.md"); + }, + ); it("keeps what a cancelled document had already done, and claims no rollback", function* () { const remote = yield* useBareRemote(REMOTE); @@ -385,12 +403,12 @@ describe("ORC13 — live local Git", () => { expect(checkout.run("log", "-1", "--pretty=%s")).not.toBe("never reached"); }); - it("commits as the invoking user, not as the workflow identity", function* () { + ordinaryCase("commits as the invoking user, not as the workflow identity", function* (runFresh) { const remote = yield* useBareRemote(REMOTE); const root = yield* useManagedRoot(); const checkout = yield* useHostCheckout(remote.locator); - yield* runOrdinaryDocument( + yield* runFresh( [ `mine`, ``, @@ -409,85 +427,94 @@ describe("ORC13 — live local Git", () => { expect(checkout.run("log", "-1", "--pretty=%an")).not.toBe("Executable.md workflow"); }); - it("takes author and committer separately when the host resolves them apart", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + ordinaryCase( + "takes author and committer separately when the host resolves them apart", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - yield* runOrdinaryDocument( - [ - `pair`, - ``, - ``, - ].join("\n"), - { - root, - cwd: checkout.root, - identity: statedIdentity( - "Ada Lovelace 1 +0000", - "Grace Hopper 1 +0000", + yield* runFresh( + [ + `pair`, + ``, + ``, + ].join("\n"), + { + root, + cwd: checkout.root, + identity: statedIdentity( + "Ada Lovelace 1 +0000", + "Grace Hopper 1 +0000", + ), + }, + ); + + expect(checkout.run("log", "-1", "--pretty=%an|%ae|%cn|%ce")).toBe( + "Ada Lovelace|ada@example.test|Grace Hopper|grace@example.test", + ); + }, + ); + + ordinaryCase( + "refuses to commit when the host cannot say who the commit is by", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const before = checkout.run("rev-parse", "HEAD"); + + const failure = yield* raised( + runFresh( + [ + `orphan`, + ``, + ``, + ].join("\n"), + { root, cwd: checkout.root, identity: statedIdentity(undefined) }, ), - }, - ); + ); - expect(checkout.run("log", "-1", "--pretty=%an|%ae|%cn|%ce")).toBe( - "Ada Lovelace|ada@example.test|Grace Hopper|grace@example.test", - ); - }); + expect(failure).toBeInstanceOf(UnresolvedGitIdentityError); + expect(String(failure)).toContain("git config --global user.name"); + // Nothing was committed, and no identity was substituted. + expect(checkout.run("rev-parse", "HEAD")).toBe(before); + // The staging that came before it still happened: this refuses the commit, + // not the document that led to it. + expect(checkout.run("diff", "--cached", "--name-only")).toContain("orphan.md"); + }, + ); - it("refuses to commit when the host cannot say who the commit is by", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const before = checkout.run("rev-parse", "HEAD"); + ordinaryCase( + "leaves every other component usable when no identity resolves", + function* (runFresh) { + yield* Permissions.around( + { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, + { at: "min" }, + ); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - const failure = yield* raised( - runOrdinaryDocument( + // Repository, Worktree, Dir, Switch and Add all work: none of them writes a + // commit object, so none of them needs to know who anybody is. + const rendered = yield* runFresh( [ - `orphan`, - ``, - ``, + ``, + ``, + "", + ``, + `fine`, + ``, + "", + "", + "ran", ].join("\n"), { root, cwd: checkout.root, identity: statedIdentity(undefined) }, - ), - ); - - expect(failure).toBeInstanceOf(UnresolvedGitIdentityError); - expect(String(failure)).toContain("git config --global user.name"); - // Nothing was committed, and no identity was substituted. - expect(checkout.run("rev-parse", "HEAD")).toBe(before); - // The staging that came before it still happened: this refuses the commit, - // not the document that led to it. - expect(checkout.run("diff", "--cached", "--name-only")).toContain("orphan.md"); - }); - - it("leaves every other component usable when no identity resolves", function* () { - yield* Permissions.around( - { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, - { at: "min" }, - ); - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - - // Repository, Worktree, Dir, Switch and Add all work: none of them writes a - // commit object, so none of them needs to know who anybody is. - const rendered = yield* runOrdinaryDocument( - [ - ``, - ``, - "", - ``, - `fine`, - ``, - "", - "", - "ran", - ].join("\n"), - { root, cwd: checkout.root, identity: statedIdentity(undefined) }, - ); - expect(String(rendered)).toContain("ran"); - }); + ); + expect(String(rendered)).toContain("ran"); + }, + ); it("keeps hooks, monitors, signing and repository helpers disabled", function* () { const remote = yield* useBareRemote(REMOTE); @@ -571,86 +598,89 @@ function selectionProbe(seen: (RepositorySelection | undefined)[]): ComponentReg describe("ORC6 — Dir makes the directory it names", () => { // ORC6: the ordering claim and the two path rules, in one document. - it("ORC6: creates missing parents, keeps an absolute path, and finishes before content", function* () { - yield* Permissions.around( - { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, - { at: "min" }, - ); - const root = yield* useManagedRoot(); - const checkout = (yield* useOriginlessCheckout()).root; - const elsewhere = yield* useTempDirectory("xmd-orc6-absolute-"); - - // The ordering probe. A nested `` proves nothing about ordering, - // because a write creates its own parents recursively — it would land - // whether or not the ensure had finished. This runs *first* inside the - // region and records what it finds: whether the directory is already there, - // and what the contextual working directory is at that moment. - const observed: { exists: boolean; cwd: string }[] = []; - const probe: ComponentRegistration = { - name: "Observes", - origin: "test://observes", - props: { type: "object", properties: {}, additionalProperties: false }, - *fn(): Operation { - const here = yield* cwd(); - observed.push({ exists: yield* exists(here), cwd: here }); - return ""; - }, - }; - - yield* runOrdinaryDocument( - [ - '', - "", - "", - "", - 'landed', - "", - "", - "", - ``, - "", - "", - "", - 'also landed', - "", - "", - "", - ].join("\n"), - { root, cwd: checkout, components: [probe] }, - ); + ordinaryCase( + "ORC6: creates missing parents, keeps an absolute path, and finishes before content", + function* (runFresh) { + yield* Permissions.around( + { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, + { at: "min" }, + ); + const root = yield* useManagedRoot(); + const checkout = (yield* useOriginlessCheckout()).root; + const elsewhere = yield* useTempDirectory("xmd-orc6-absolute-"); + + // The ordering probe. A nested `` proves nothing about ordering, + // because a write creates its own parents recursively — it would land + // whether or not the ensure had finished. This runs *first* inside the + // region and records what it finds: whether the directory is already there, + // and what the contextual working directory is at that moment. + const observed: { exists: boolean; cwd: string }[] = []; + const probe: ComponentRegistration = { + name: "Observes", + origin: "test://observes", + props: { type: "object", properties: {}, additionalProperties: false }, + *fn(): Operation { + const here = yield* cwd(); + observed.push({ exists: yield* exists(here), cwd: here }); + return ""; + }, + }; - // Before any other content ran, the directory already existed and was the - // contextual working directory. That is the ordering claim, observed rather - // than inferred. - expect(observed).toHaveLength(2); - expect(observed[0]).toEqual({ exists: true, cwd: join(checkout, "made", "deep") }); - expect(observed[1]).toEqual({ - exists: true, - cwd: join(elsewhere, "written", "here"), - }); + yield* runFresh( + [ + '', + "", + "", + "", + 'landed', + "", + "", + "", + ``, + "", + "", + "", + 'also landed', + "", + "", + "", + ].join("\n"), + { root, cwd: checkout, components: [probe] }, + ); - // Every missing parent, and the file that landed after them. - expect(yield* readTextFile(join(checkout, "made", "deep", "inside.md"))).toBe("landed"); - // The absolute target names exactly that place — not a rebase beneath cwd. - expect(yield* readTextFile(join(elsewhere, "written", "here", "outside.md"))).toBe( - "also landed", - ); - expect(yield* exists(join(checkout, elsewhere.replace(/^\//, "")))).toBe(false); - }); + // Before any other content ran, the directory already existed and was the + // contextual working directory. That is the ordering claim, observed rather + // than inferred. + expect(observed).toHaveLength(2); + expect(observed[0]).toEqual({ exists: true, cwd: join(checkout, "made", "deep") }); + expect(observed[1]).toEqual({ + exists: true, + cwd: join(elsewhere, "written", "here"), + }); + + // Every missing parent, and the file that landed after them. + expect(yield* readTextFile(join(checkout, "made", "deep", "inside.md"))).toBe("landed"); + // The absolute target names exactly that place — not a rebase beneath cwd. + expect(yield* readTextFile(join(elsewhere, "written", "here", "outside.md"))).toBe( + "also landed", + ); + expect(yield* exists(join(checkout, elsewhere.replace(/^\//, "")))).toBe(false); + }, + ); // ORC6a: an existing directory is adopted. Asserted on the bytes and on the // entry list, because a provider that removed and recreated it would pass a // test that only checked the directory exists. - it("ORC6a: an existing directory keeps its contents", function* () { + ordinaryCase("ORC6a: an existing directory keeps its contents", function* (runFresh) { const root = yield* useManagedRoot(); const checkout = (yield* useOriginlessCheckout()).root; yield* ensureDir(join(checkout, "kept", "sub")); yield* writeTextFile(join(checkout, "kept", "planted.txt"), "the bytes that were here"); - yield* runOrdinaryDocument( - '\n\nadded\n\n\n', - { root, cwd: checkout }, - ); + yield* runFresh('\n\nadded\n\n\n', { + root, + cwd: checkout, + }); expect(yield* readTextFile(join(checkout, "kept", "planted.txt"))).toBe( "the bytes that were here", @@ -791,35 +821,38 @@ describe("ORC6 — Dir makes the directory it names", () => { // ORC6d: the operation is about a directory. The Repository selection in // scope, and every member of its identity, is the same inside the region and // after it. - it("ORC6d: Repository selection and identity are unchanged by Dir", function* () { - const root = yield* useManagedRoot(); - const remote = yield* useBareRemote(REMOTE); - const checkout = (yield* useHostCheckout(remote.locator)).root; - const seen: (RepositorySelection | undefined)[] = []; - - yield* runOrdinaryDocument( - [ - '', - '', - "", - '', - "", - "", - "", - '', - "", - ].join("\n"), - { root, cwd: checkout, components: [selectionProbe(seen)] }, - ); + ordinaryCase( + "ORC6d: Repository selection and identity are unchanged by Dir", + function* (runFresh) { + const root = yield* useManagedRoot(); + const remote = yield* useBareRemote(REMOTE); + const checkout = (yield* useHostCheckout(remote.locator)).root; + const seen: (RepositorySelection | undefined)[] = []; + + yield* runFresh( + [ + '', + '', + "", + '', + "", + "", + "", + '', + "", + ].join("\n"), + { root, cwd: checkout, components: [selectionProbe(seen)] }, + ); - expect(seen).toHaveLength(3); - // The whole selection, member by member: identifier, name, checkout path - // and every member of the identity. A Dir that had re-selected anything - // would differ in one of them. - expect(seen[0]).toBeDefined(); - expect(seen[1]).toEqual(seen[0]); - expect(seen[2]).toEqual(seen[0]); - }); + expect(seen).toHaveLength(3); + // The whole selection, member by member: identifier, name, checkout path + // and every member of the identity. A Dir that had re-selected anything + // would differ in one of them. + expect(seen[0]).toBeDefined(); + expect(seen[1]).toEqual(seen[0]); + expect(seen[2]).toEqual(seen[0]); + }, + ); // ORC6e: creation is persistent. A directory made inside a region whose // content then fails is still there, and so is one made in a run that was diff --git a/packages/git/tests/run-composition-managed.test.ts b/packages/git/tests/run-composition-managed.test.ts index e4c8ccd96..ff03522da 100644 --- a/packages/git/tests/run-composition-managed.test.ts +++ b/packages/git/tests/run-composition-managed.test.ts @@ -15,7 +15,14 @@ import { describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { scoped, type Operation } from "effection"; -import { exists, readTextFile, rm, writeTextFile } from "@effectionx/fs"; +import { exists, readdir, readTextFile, rm, writeTextFile } from "@effectionx/fs"; +import { API } from "@executablemd/runtime"; +import { parseDurableEvent } from "@executablemd/durable-streams"; +import type { DurableEvent } from "@executablemd/durable-streams"; +import { useTempDirectory } from "@executablemd/test-support/temp"; +import { withFailureJournal } from "@executablemd/testing"; +import { useTestDiagnostics } from "../../cli/src/test-diagnostics.ts"; +import { join } from "node:path"; import { git, useBareRemote } from "./support/git-remotes.ts"; import { spawnSync } from "node:child_process"; import process from "node:process"; @@ -23,6 +30,7 @@ import { spawn, withResolvers } from "effection"; import { registerComponents } from "@executablemd/core"; import type { ChildOutcome } from "./support/run-composition-child.ts"; import { + ordinaryDocumentCases, causedBy, commonDirectoryOf, countingOrdinaryHost, @@ -39,89 +47,101 @@ import { useManagedRoot, worktreeSlotOf, type HostCheckout, + type ManagedSlot, } from "./support/run-composition.ts"; import { CHILD, REMOTE, entriesOf, isManagedRefusal } from "./support/run-composition-tier.ts"; -describe("ORC8 — managed checkouts are persistent", () => { - it("leaves the checkout, its metadata and its working files after the run", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const common = commonDirectoryOf(checkout); - const slot = worktreeSlotOf(root, common, "kept"); - - yield* runOrdinaryDocument( - [ - ``, - `unfinished`, - "", - ].join("\n"), - { root, cwd: checkout.root }, - ); - - expect(yield* exists(slot.checkout)).toBe(true); - expect(yield* exists(`${slot.checkout}/draft.md`)).toBe(true); - const sidecar = yield* readSidecar(slot); - expect(sidecar).toMatchObject({ - kind: "worktree", - version: 1, - name: "kept", - requestedBranch: "kept", - requestedBase: null, - owner: common, - }); - }); +const ordinaryCase = ordinaryDocumentCases("packages/git/tests/run-composition-managed.test.ts"); - it("leaves a managed Repository, its metadata and its files after the run", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const slot = repositorySlotOf(root, remote.locator, "project"); - - yield* runOrdinaryDocument( - [ - ``, - `unfinished`, - "", - ].join("\n"), - { root, cwd: checkout.root }, - ); - - expect(yield* exists(`${slot.checkout}/draft.md`)).toBe(true); - expect(yield* readSidecar(slot)).toMatchObject({ - kind: "repository", - version: 1, - name: "project", - locator: remote.locator, - requestedBase: null, - }); - }); +describe("ORC8 — managed checkouts are persistent", () => { + ordinaryCase( + "leaves the checkout, its metadata and its working files after the run", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const common = commonDirectoryOf(checkout); + const slot = worktreeSlotOf(root, common, "kept"); + + yield* runFresh( + [ + ``, + `unfinished`, + "", + ].join("\n"), + { root, cwd: checkout.root }, + ); - it("issues no Git or delete command for a checkout while tearing down", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const counting = countingOrdinaryHost(); + expect(yield* exists(slot.checkout)).toBe(true); + expect(yield* exists(`${slot.checkout}/draft.md`)).toBe(true); + const sidecar = yield* readSidecar(slot); + expect(sidecar).toMatchObject({ + kind: "worktree", + version: 1, + name: "kept", + requestedBranch: "kept", + requestedBase: null, + owner: common, + }); + }, + ); + + ordinaryCase( + "leaves a managed Repository, its metadata and its files after the run", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const slot = repositorySlotOf(root, remote.locator, "project"); + + yield* runFresh( + [ + ``, + `unfinished`, + "", + ].join("\n"), + { root, cwd: checkout.root }, + ); - yield* runOrdinaryDocument( - [ - ``, - ``, - ].join("\n"), - { root, cwd: checkout.root, host: counting.host }, - ); + expect(yield* exists(`${slot.checkout}/draft.md`)).toBe(true); + expect(yield* readSidecar(slot)).toMatchObject({ + kind: "repository", + version: 1, + name: "project", + locator: remote.locator, + requestedBase: null, + }); + }, + ); + + ordinaryCase( + "issues no Git or delete command for a checkout while tearing down", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const counting = countingOrdinaryHost(); + + yield* runFresh( + [ + ``, + ``, + ].join("\n"), + { root, cwd: checkout.root, host: counting.host }, + ); - // Nothing that could undo a checkout ever ran — not while the document was - // expanding, and not on the way out. - const issued = subcommands(counting.counters); - for (const undoing of ["reset", "clean", "restore", "prune", "gc", "fetch"]) { - expect(`${undoing} ${issued.includes(undoing)}`).toBe(`${undoing} false`); - } - expect(counting.counters.commands.some((args) => args.includes("--force"))).toBe(false); - expect( - counting.counters.commands.some((args) => args[0] === "worktree" && args[1] === "remove"), - ).toBe(false); - }); + // Nothing that could undo a checkout ever ran — not while the document was + // expanding, and not on the way out. + const issued = subcommands(counting.counters); + for (const undoing of ["reset", "clean", "restore", "prune", "gc", "fetch"]) { + expect(`${undoing} ${issued.includes(undoing)}`).toBe(`${undoing} false`); + } + expect(counting.counters.commands.some((args) => args.includes("--force"))).toBe(false); + expect( + counting.counters.commands.some((args) => args[0] === "worktree" && args[1] === "remove"), + ).toBe(false); + }, + ); it("keeps both kinds of checkout after a cancellation", function* () { const remote = yield* useBareRemote(REMOTE); @@ -148,66 +168,72 @@ describe("ORC8 — managed checkouts are persistent", () => { expect(yield* exists(`${worktree.checkout}/in-flight.md`)).toBe(true); }); - it("keeps a managed Repository after an authored failure inside its body", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const slot = repositorySlotOf(root, remote.locator, "surviving"); + ordinaryCase( + "keeps a managed Repository after an authored failure inside its body", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const slot = repositorySlotOf(root, remote.locator, "surviving"); - yield* raised( - runOrdinaryDocument( - [ - ``, - `written before the failure`, - ``, - ``, - ``, - "", - ].join("\n"), - { root, cwd: checkout.root }, - ), - ); - - // The path, the sidecar, the Git state and the working file all survive. - expect(yield* exists(slot.checkout)).toBe(true); - expect(yield* readSidecar(slot)).toMatchObject({ - kind: "repository", - version: 1, - name: "surviving", - locator: remote.locator, - }); - expect(yield* readTextFile(`${slot.checkout}/kept.md`)).toBe("written before the failure"); - // The branch the document switched to and the staging it did are both still - // there: nothing rolled back, and nothing was cleaned up on the way out. - expect(git(["rev-parse", "--abbrev-ref", "HEAD"], slot.checkout, checkout.home)).toBe( - "in-progress", - ); - expect(git(["diff", "--cached", "--name-only"], slot.checkout, checkout.home)).toContain( - "kept.md", - ); - }); - - it("keeps the checkout after an authored failure inside the Worktree body", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const slot = worktreeSlotOf(root, commonDirectoryOf(checkout), "survivor"); + yield* raised( + runFresh( + [ + ``, + `written before the failure`, + ``, + ``, + ``, + "", + ].join("\n"), + { root, cwd: checkout.root }, + ), + ); - yield* raised( - runOrdinaryDocument( - [ - ``, - `written before the failure`, - ``, - "", - ].join("\n"), - { root, cwd: checkout.root }, - ), - ); + // The path, the sidecar, the Git state and the working file all survive. + expect(yield* exists(slot.checkout)).toBe(true); + expect(yield* readSidecar(slot)).toMatchObject({ + kind: "repository", + version: 1, + name: "surviving", + locator: remote.locator, + }); + expect(yield* readTextFile(`${slot.checkout}/kept.md`)).toBe("written before the failure"); + // The branch the document switched to and the staging it did are both still + // there: nothing rolled back, and nothing was cleaned up on the way out. + expect(git(["rev-parse", "--abbrev-ref", "HEAD"], slot.checkout, checkout.home)).toBe( + "in-progress", + ); + expect(git(["diff", "--cached", "--name-only"], slot.checkout, checkout.home)).toContain( + "kept.md", + ); + }, + ); + + ordinaryCase( + "keeps the checkout after an authored failure inside the Worktree body", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const slot = worktreeSlotOf(root, commonDirectoryOf(checkout), "survivor"); + + yield* raised( + runFresh( + [ + ``, + `written before the failure`, + ``, + "", + ].join("\n"), + { root, cwd: checkout.root }, + ), + ); - expect(yield* exists(`${slot.checkout}/kept.md`)).toBe(true); - expect(yield* readSidecar(slot)).toMatchObject({ kind: "worktree" }); - }); + expect(yield* exists(`${slot.checkout}/kept.md`)).toBe(true); + expect(yield* readSidecar(slot)).toMatchObject({ kind: "worktree" }); + }, + ); }); describe("ORC9 — compatible reuse", () => { @@ -509,19 +535,77 @@ describe("ORC11 — an interrupted creation", () => { }); }); -describe("ORC12 — exclusive ownership across processes", () => { +const ORC12_SUITE = "ORC12 — exclusive ownership across processes"; +const OWNER = "packages/git/tests/run-composition-managed.test.ts"; + +function parseJournal(value: unknown): DurableEvent[] { + if (!Array.isArray(value)) { + return []; + } + const events: DurableEvent[] = []; + for (const record of value) { + if (typeof record !== "string") { + return []; + } + const parsed = parseDurableEvent(record); + if (!parsed.ok) { + return []; + } + events.push(parsed.value); + } + return events; +} + +describe(ORC12_SUITE, () => { /** One second process, run to completion, and what it reported. */ - function* elsewhere(root: string, cwd: string, source: string): Operation { + function* elsewhere( + root: string, + cwd: string, + source: string, + capture = false, + ): Operation & { events: DurableEvent[] }> { const outcome = spawnSync( process.execPath, - ["run", "--allow-all", "--frozen", CHILD, root, cwd, source], + [ + "run", + "--allow-all", + "--frozen", + CHILD, + root, + cwd, + source, + ...(capture ? ["--capture-journal"] : []), + ], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] }, ); - const printed = outcome.stdout.trim().split("\n").at(-1) ?? ""; - if (printed === "") { - throw new Error(`the child printed nothing: ${outcome.stderr}`); + const printed = outcome.stdout?.trim().split("\n").at(-1); + if (outcome.status !== 0 || !printed) { + throw new Error( + `the checkout fixture returned no successful result (exit ${outcome.status})`, + ); + } + let result: unknown; + try { + result = JSON.parse(printed); + } catch { + throw new Error("the checkout fixture result is invalid JSON"); } - return JSON.parse(printed) as ChildOutcome; + if ( + typeof result !== "object" || + result === null || + !("kind" in result) || + (result.kind !== "selected" && result.kind !== "refused" && result.kind !== "failed") + ) { + throw new Error("the checkout fixture result has an invalid kind"); + } + return { + kind: result.kind, + bound: "bound" in result && typeof result.bound === "string" ? result.bound : undefined, + reason: "reason" in result && typeof result.reason === "string" ? result.reason : undefined, + message: + "message" in result && typeof result.message === "string" ? result.message : undefined, + events: capture && "journal" in result ? parseJournal(result.journal) : [], + }; } it("refuses a second process the slot a first is holding, and changes nothing", function* () { @@ -565,6 +649,7 @@ describe("ORC12 — exclusive ownership across processes", () => { expect(refused.kind).toBe("refused"); expect(refused.reason).toBe("in-use"); expect(refused.message).toContain("another process is working in"); + expect(refused.events).toEqual([]); // And nothing under the slot moved. expect(yield* fingerprintTree(held.slot)).toEqual(bytes); @@ -590,7 +675,15 @@ describe("ORC12 — exclusive ownership across processes", () => { expect(yield* exists(`${held.checkout}/held.md`)).toBe(true); }); - it("hands a slot on after a normal release, with the checkout intact", function* () { + function* checkSerialSidecar(slot: ManagedSlot): Operation { + expect(yield* readSidecar(slot)).toMatchObject({ kind: "worktree", name: "serial" }); + } + + function* normalRelease( + name: string, + check: (slot: ManagedSlot, records: readonly DurableEvent[]) => Operation, + artifacts?: string, + ): Operation { const remote = yield* useBareRemote(REMOTE); const root = yield* useManagedRoot(); const checkout = yield* useHostCheckout(remote.locator); @@ -601,11 +694,84 @@ describe("ORC12 — exclusive ownership across processes", () => { const bound = yield* runOrdinaryDocument(document, { root, cwd: checkout.root }); expect(String(bound).trim()).toBe(slot.checkout); - // A real second process then takes it, and finds the same checkout. - const later = yield* elsewhere(root, checkout.root, document); - expect(later.kind).toBe("selected"); - expect(later.bound).toBe(slot.checkout); - expect(yield* readSidecar(slot)).toMatchObject({ kind: "worktree", name: "serial" }); + yield* scoped(function* () { + if (artifacts !== undefined) { + yield* API.Env.around({ + *env([variable], next) { + return variable === "XMD_TEST_ARTIFACTS" ? artifacts : yield* next(variable); + }, + }); + } + const diagnostics = yield* useTestDiagnostics(process.stderr, { + owner: OWNER, + suite: ORC12_SUITE, + name, + }); + yield* withFailureJournal( + diagnostics.execution("child", "").journal(() => false), + function* (buffer) { + // The real second process takes the same checkout. Its result is available after exit. + const later = yield* elsewhere(root, checkout.root, document, true); + for (const event of later.events) { + yield* buffer.append(event); + } + expect(later.kind).toBe("selected"); + expect(later.bound).toBe(slot.checkout); + yield* check(slot, later.events); + }, + ); + }); + } + + const normalCase = "hands a slot on after a normal release, with the checkout intact"; + it(normalCase, () => normalRelease(normalCase, checkSerialSidecar)); + + const witness = "attributes the child journal when the post-exit sidecar assertion fails"; + it(witness, function* () { + const root = yield* useTempDirectory("xmd-orc12-failure-"); + const success = join(root, "passed", ".xmd", "test-artifacts"); + yield* normalRelease(witness, checkSerialSidecar, success); + expect(yield* exists(success)).toBe(false); + const artifacts = join(root, ".xmd", "test-artifacts"); + let received: readonly DurableEvent[] = []; + let assertion: unknown; + let caught: unknown; + try { + yield* normalRelease( + witness, + function* (slot, records) { + received = records; + yield* rm(slot.metadata); + try { + yield* checkSerialSidecar(slot); + } catch (error) { + assertion = error; + throw error; + } + }, + artifacts, + ); + } catch (error) { + caught = error; + } + expect(assertion).toBeInstanceOf(Error); + expect(caught).toBe(assertion); + const invocations = yield* readdir(artifacts); + expect(invocations).toHaveLength(1); + const directory = join(artifacts, invocations[0]); + expect((yield* readdir(directory)).sort()).toEqual(["child-1.case.json", "child-1.jsonl"]); + expect(JSON.parse(yield* readTextFile(join(directory, "child-1.case.json")))).toEqual({ + owner: OWNER, + suite: ORC12_SUITE, + name: witness, + kind: "child", + ordinal: 1, + source: "", + }); + const lines = (yield* readTextFile(join(directory, "child-1.jsonl"))).trimEnd().split("\n"); + const events = parseJournal(lines); + expect(events).toEqual(received); + expect(events.at(-1)).toMatchObject({ type: "close", coroutineId: "root" }); }); }); diff --git a/packages/git/tests/run-composition-remote.test.ts b/packages/git/tests/run-composition-remote.test.ts index a3753204a..07ecc9366 100644 --- a/packages/git/tests/run-composition-remote.test.ts +++ b/packages/git/tests/run-composition-remote.test.ts @@ -33,6 +33,7 @@ import { patches, } from "./support/github.ts"; import { + ordinaryDocumentCases, causedBy, countingOrdinaryHost, fingerprintTree, @@ -56,34 +57,39 @@ import { isAdmissionFailure, } from "./support/run-composition-tier.ts"; +const ordinaryCase = ordinaryDocumentCases("packages/git/tests/run-composition-remote.test.ts"); + describe("ORC14 — live Push evidence", () => { - it("records a performed publication and lets exactly that head be authorized", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + ordinaryCase( + "records a performed publication and lets exactly that head be authorized", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - const failure = yield* raised( - runOrdinaryDocument( - [ - ``, - `published`, - ``, - ``, - ``, - ``, - ].join("\n"), - { root, cwd: checkout.root }, - ), - ); + const failure = yield* raised( + runFresh( + [ + ``, + `published`, + ``, + ``, + ``, + ``, + ].join("\n"), + { root, cwd: checkout.root }, + ), + ); - // The branch really is at the remote, at the commit this execution made. - expect(remoteBranch(remote, "published")).toBe(checkout.run("rev-parse", "HEAD")); - // And the pull request got past the evidence gate: what stopped it is the - // adapter declining a locator that is not a github.com repository, which is - // the step *after* the local authorization this criterion is about. - expect(String(failure)).toContain("only for repositories on github.com"); - expect(String(failure)).not.toContain("holds no successful result"); - }); + // The branch really is at the remote, at the commit this execution made. + expect(remoteBranch(remote, "published")).toBe(checkout.run("rev-parse", "HEAD")); + // And the pull request got past the evidence gate: what stopped it is the + // adapter declining a locator that is not a github.com repository, which is + // the step *after* the local authorization this criterion is about. + expect(String(failure)).toContain("only for repositories on github.com"); + expect(String(failure)).not.toContain("holds no successful result"); + }, + ); it("records an already-equal publication the same way it records a performed one", function* () { const remote = yield* useBareRemote(REMOTE); @@ -225,7 +231,7 @@ describe("ORC14 — live Push evidence", () => { expect((stale as LivePushEvidenceError).reason).toBe("conflicting-push-evidence"); }); - it("lets the latest publication of a destination decide", function* () { + ordinaryCase("lets the latest publication of a destination decide", function* (runFresh) { const remote = yield* useBareRemote(REMOTE); const root = yield* useManagedRoot(); const checkout = yield* useHostCheckout(remote.locator); @@ -233,7 +239,7 @@ describe("ORC14 — live Push evidence", () => { // Two publications of one destination, at successive commits, and then a // third commit nothing published. const failure = yield* raised( - runOrdinaryDocument( + runFresh( [ ``, `one`, @@ -258,49 +264,55 @@ describe("ORC14 — live Push evidence", () => { expect(remoteBranch(remote, "moving")).toBe(checkout.run("rev-parse", "HEAD~1")); }); - it("authorizes at the second publication's commit, not the first's", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + ordinaryCase( + "authorizes at the second publication's commit, not the first's", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - const failure = yield* raised( - runOrdinaryDocument( - [ - ``, - `one`, - ``, - ``, - ``, - `two`, - ``, - ``, - ``, - ``, - ].join("\n"), - { root, cwd: checkout.root }, - ), - ); - // Past the gate: the latest publication names the head the pull request - // would open from. - expect(String(failure)).toContain("only for repositories on github.com"); - expect(remoteBranch(remote, "latest")).toBe(checkout.run("rev-parse", "HEAD")); - }); + const failure = yield* raised( + runFresh( + [ + ``, + `one`, + ``, + ``, + ``, + `two`, + ``, + ``, + ``, + ``, + ].join("\n"), + { root, cwd: checkout.root }, + ), + ); + // Past the gate: the latest publication names the head the pull request + // would open from. + expect(String(failure)).toContain("only for repositories on github.com"); + expect(remoteBranch(remote, "latest")).toBe(checkout.run("rev-parse", "HEAD")); + }, + ); }); describe("ORC15 — evidence cannot cross runs", () => { - it("refuses a PullRequest in an execution that published nothing", function* () { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + ordinaryCase( + "refuses a PullRequest in an execution that published nothing", + function* (runFresh) { + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - const failure = yield* raised( - runOrdinaryDocument(``, { - root, - cwd: checkout.root, - }), - ); - expect(String(failure)).toContain("holds no successful result"); - }); + const failure = yield* raised( + runFresh(``, { + root, + cwd: checkout.root, + }), + ); + expect(String(failure)).toContain("holds no successful result"); + }, + ); it("does not let one execution's real publication authorize the next", function* () { const remote = yield* useBareRemote(REMOTE); @@ -554,13 +566,13 @@ describe("ORC16 — live Issues", () => { expect(store.requests).toHaveLength(0); }); - it("installs no matching provider when nothing is configured", function* () { + ordinaryCase("installs no matching provider when nothing is configured", function* (runFresh) { const remote = yield* useBareRemote(REMOTE); const root = yield* useManagedRoot(); const checkout = yield* useHostCheckout(remote.locator); const failure = yield* raised( - runOrdinaryDocument(``, { + runFresh(``, { root, cwd: checkout.root, }), @@ -972,7 +984,7 @@ describe("checkout admission is the whole repository identity", () => { * at the end is the ordinary provider's own. */ describe("ORC18 — the authored Git vocabulary", () => { - it("reaches Repository and Git middleware, and still publishes", function* () { + ordinaryCase("reaches Repository and Git middleware, and still publishes", function* (runFresh) { const remote = yield* useBareRemote(REMOTE); const root = yield* useManagedRoot(); const here = yield* useHostCheckout(remote.locator); @@ -980,7 +992,7 @@ describe("ORC18 — the authored Git vocabulary", () => { const selections: string[] = []; const transitions: string[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ ``, ``, diff --git a/packages/git/tests/support/issue-scenario.ts b/packages/git/tests/support/issue-scenario.ts index 849bbe11d..2e739d037 100644 --- a/packages/git/tests/support/issue-scenario.ts +++ b/packages/git/tests/support/issue-scenario.ts @@ -149,7 +149,7 @@ export interface ScenarioFixture { * staged attempt anyway. What it asks about the scenario's journal it asks * through ``. */ - observe(path: string): Operation; + observe(path: string, stream?: DurableStream): Operation; } export function* useScenarioFixture(): Operation { @@ -179,8 +179,9 @@ export function* useScenarioFixture(): Operation { journal(): DurableEvent[] { return held.snapshot(RUN.runId); }, - observe(path: string): Operation { - return observeDocument(path, held, `observing:${basename(path)}`); + observe(path, stream): Operation { + const runId = `observing:${basename(path)}`; + return observeDocument(path, stream ?? held.for(runId), runId); }, }; } @@ -197,12 +198,12 @@ export function* useScenarioFixture(): Operation { */ function observeDocument( path: string, - held: Journals, + stream: DurableStream, runId: string, ): Operation { return scoped(function* () { const tests = yield* useTesting(); - const execution = yield* executeInstalled({ path, stream: held.for(runId) }, [ + const execution = yield* executeInstalled({ path, stream }, [ retainedWorkflowInstallation({ ...RUN, runId }), ]); // Drained to its close value first: output closes with the rendered text — @@ -957,9 +958,9 @@ function stageAttempt( * left. A scenario that does stage attempts acquires the fixture itself, so it * can stage them between acquiring it and observing the document. */ -export function runScenario(path: string): Operation { +export function runScenario(path: string, stream?: DurableStream): Operation { return scoped(function* () { const fixture = yield* useScenarioFixture(); - return yield* fixture.observe(path); + return yield* fixture.observe(path, stream); }); } diff --git a/packages/git/tests/support/run-composition-child.ts b/packages/git/tests/support/run-composition-child.ts index f894dbea5..5cb9e57e5 100644 --- a/packages/git/tests/support/run-composition-child.ts +++ b/packages/git/tests/support/run-composition-child.ts @@ -13,7 +13,8 @@ import { main } from "effection"; import process from "node:process"; -import { InMemoryStream } from "@executablemd/durable-streams"; +import { Buffer } from "node:buffer"; +import { InMemoryStream, serializeDurableEvent } from "@executablemd/durable-streams"; import { collect, execute, inlineSource } from "@executablemd/core"; import { API, useHostFiles } from "@executablemd/runtime"; import { useCompositionComponents } from "../../src/composition/installation.ts"; @@ -28,14 +29,16 @@ export interface ChildOutcome { /** The fixed word a managed-checkout refusal is reported under. */ readonly reason?: string; readonly message?: string; + readonly journal?: readonly string[]; } await main(function* () { - const [root, cwd, source] = process.argv.slice(2); + const [root, cwd, source, capture] = process.argv.slice(2); if (root === undefined || cwd === undefined || source === undefined) { throw new Error("the child needs a managed root, a starting directory and a document"); } + const stream = new InMemoryStream(); let outcome: ChildOutcome; try { const rendered = yield* (function* () { @@ -51,9 +54,7 @@ await main(function* () { yield* useHostFiles(); yield* useCompositionComponents(); yield* useRunComposition({ root, cwd }); - return yield* collect( - yield* execute({ ...inlineSource(source), stream: new InMemoryStream() }), - ); + return yield* collect(yield* execute({ ...inlineSource(source), stream })); })(); outcome = { kind: "selected", bound: String(rendered).trim() }; } catch (error) { @@ -64,7 +65,15 @@ await main(function* () { : { kind: "refused", reason: refusal.reason, message: refusal.message }; } - process.stdout.write(`${JSON.stringify(outcome)}\n`); + let line = `${JSON.stringify(outcome)}\n`; + if (capture === "--capture-journal") { + const journal = (yield* stream.readAll()).map(serializeDurableEvent); + const enriched = `${JSON.stringify({ ...outcome, journal })}\n`; + if (Buffer.byteLength(enriched) <= 64 * 1024) { + line = enriched; + } + } + process.stdout.write(line); }); /** The managed-checkout refusal in this error's chain, if there is one. */ diff --git a/packages/git/tests/support/run-composition.ts b/packages/git/tests/support/run-composition.ts index e1ac9b684..328a415e9 100644 --- a/packages/git/tests/support/run-composition.ts +++ b/packages/git/tests/support/run-composition.ts @@ -8,8 +8,8 @@ * the user's own `~/.xmd/repositories`, and the Git subprocess where a suite * counts invocations. * - * There is no database, no journal and no WorkflowRun anywhere in this file. - * That is the point of the profile: an ordinary run has none of them. + * The ordinary profile installs no run store. The test owns any diagnostic + * snapshot around the execution. */ import { scoped, spawn, suspend, until, withResolvers, type Operation } from "effection"; @@ -25,6 +25,8 @@ import { API, useHostFiles } from "@executablemd/runtime"; import { InMemoryStream } from "@executablemd/durable-streams"; import type { Json } from "@executablemd/durable-streams"; import { useTempDirectory } from "@executablemd/test-support/temp"; +import { it } from "@executablemd/test-support/bdd"; +import { captureTestJournal } from "../../../test-support/failure-journal.ts"; import { useCompositionComponents } from "../../src/composition/installation.ts"; import { RepositoryContext } from "../../src/composition/context.ts"; import type { RepositorySelection } from "../../src/composition/selection.ts"; @@ -236,7 +238,11 @@ export interface RunOptions extends Omit * green on a developer's laptop, red on every shard. A case that is *about* * identity resolution passes its own `identity` and overrides this. */ -export function runOrdinaryDocument(source: string, options: RunOptions): Operation { +export function runOrdinaryDocument( + source: string, + options: RunOptions, + stream = new InMemoryStream(), +): Operation { return scoped(function* () { yield* API.Env.around( { @@ -267,13 +273,25 @@ export function runOrdinaryDocument(source: string, options: RunOptions): Operat return yield* collect( yield* execute({ ...inlineSource(source), - stream: new InMemoryStream(), + stream, ...(options.props === undefined ? {} : { props: options.props }), }), ); }); } +type RunOrdinary = (source: string, options: RunOptions) => Operation; + +export function ordinaryDocumentCases(owner: string) { + return (name: string, body: (run: RunOrdinary) => Operation): void => { + it(name, () => + captureTestJournal({ owner, name }, "", (stream) => + body((source, options) => runOrdinaryDocument(source, options, stream)), + ), + ); + }; +} + /** What a suite reads back about one managed slot. */ export interface ManagedSlot { readonly slot: string; diff --git a/packages/test-agent/tests/command-lazy.test.ts b/packages/test-agent/tests/command-lazy.test.ts index e0189b6cf..42c2197e7 100644 --- a/packages/test-agent/tests/command-lazy.test.ts +++ b/packages/test-agent/tests/command-lazy.test.ts @@ -16,7 +16,7 @@ import { ensureDir, rm, writeTextFile } from "@effectionx/fs"; import { API } from "@executablemd/runtime"; import { execute, useTempFileCompiler } from "@executablemd/core"; import { installAgentComponents } from "@executablemd/agent/host"; -import { InMemoryStream } from "@executablemd/durable-streams"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; import { useTesting } from "@executablemd/testing"; import type { TestResult } from "@executablemd/testing"; import { installTestAgentComponents } from "../src/components.ts"; @@ -44,109 +44,94 @@ function* refuseCommand(): Operation { }); } -function* runDoc( +const OWNER = "packages/test-agent/tests/command-lazy.test.ts"; +const SUITE = "Tier XL — lazy command resolution"; + +function* checkDocument( + name: string, doc: string, install: () => Operation, -): Operation<{ output: string; results: readonly TestResult[] }> { + check: (run: { output: string; results: readonly TestResult[] }) => void, + scenario = false, +): Operation { const dir = path.join(os.tmpdir(), `xmd-xl-${randomUUID()}`); - yield* ensureDir(dir); - return yield* scoped(function* () { - yield* ensure(() => rm(dir, { recursive: true, force: true })); - yield* writeTextFile(path.join(dir, "doc.md"), doc); - - return yield* scoped(function* () { - const testing = yield* useTesting(); - yield* install(); - yield* installTestAgentComponents(); - yield* installAgentComponents(); - const execution = yield* execute({ - path: path.join(dir, "doc.md"), - stream: new InMemoryStream(), - }); - const subscription = yield* execution.output; - let next = yield* subscription.next(); - while (!next.done) { - next = yield* subscription.next(); + const docPath = path.join(dir, "doc.md"); + yield* captureTestJournal({ owner: OWNER, suite: SUITE, name }, docPath, function* (stream) { + const result = yield* scoped(function* () { + yield* ensureDir(dir); + yield* ensure(() => rm(dir, { recursive: true, force: true })); + yield* writeTextFile(docPath, doc); + if (scenario) { + yield* writeTextFile( + path.join(dir, "agent.md"), + '\n\nhello there\n', + ); } - return { output: next.value, results: yield* testing.results }; + return yield* scoped(function* () { + const testing = yield* useTesting(); + yield* install(); + yield* installTestAgentComponents(); + yield* installAgentComponents(); + const execution = yield* execute({ path: docPath, stream }); + const subscription = yield* execution.output; + let next = yield* subscription.next(); + while (!next.done) { + next = yield* subscription.next(); + } + if (scenario) { + yield* execution; + } + return { output: next.value, results: yield* testing.results }; + }); }); + check(result); }); } -describe( - "Tier XL — lazy command resolution", - { sanitizeOps: false, sanitizeResources: false }, - () => { - beforeAll(() => useTempFileCompiler()); +describe(SUITE, { sanitizeOps: false, sanitizeResources: false }, () => { + beforeAll(() => useTempFileCompiler()); - it("XL1: installing the test-agent components does not ask for the command", function* () { - yield* refuseCommand(); - yield* installTestAgentComponents(); - }); + it("XL1: installing the test-agent components does not ask for the command", function* () { + yield* refuseCommand(); + yield* installTestAgentComponents(); + }); - it("XL2: `xmd test` on a document without runs where command throws", function* () { - const { output } = yield* runDoc("# plain\n\nno agents here\n", refuseCommand); + const withoutAgent = + "XL2: `xmd test` on a document without runs where command throws"; + it(withoutAgent, () => + checkDocument(withoutAgent, "# plain\n\nno agents here\n", refuseCommand, ({ output }) => { expect(output).toContain("no agents here"); expect(output).not.toContain("ERROR"); - }); + }), + ); - it("XL3: a document using asks for command(['test-agent'])", function* () { - const asked: Asked = { calls: [] }; - const doc = [ - "", - ' ', - ' ', - ' ', - " ", - "", - "", - ].join("\n"); - const { results } = yield* runDocWithScenario(doc, asked); - expect(results.map(({ status, error }) => ({ status, error }))).toEqual([ - { status: "pass", error: undefined }, - ]); - // One provider per isolation boundary — the scope and each - // lease — so the count tracks boundaries, not documents. What - // matters is that every request is for the worker subcommand and nothing - // else, and that each one appends it exactly once. - expect(asked.calls.length).toBeGreaterThan(0); - for (const args of asked.calls) { - expect(args).toEqual(["test-agent"]); - } - }); - }, -); - -function* runDocWithScenario( - doc: string, - asked: Asked, -): Operation<{ results: readonly TestResult[] }> { - const dir = path.join(os.tmpdir(), `xmd-xl-${randomUUID()}`); - yield* ensureDir(dir); - return yield* scoped(function* () { - yield* ensure(() => rm(dir, { recursive: true, force: true })); - yield* writeTextFile(path.join(dir, "doc.md"), doc); - yield* writeTextFile( - path.join(dir, "agent.md"), - '\n\nhello there\n', + const withAgent = "XL3: a document using asks for command(['test-agent'])"; + it(withAgent, function* () { + const asked: Asked = { calls: [] }; + const doc = [ + "", + ' ', + ' ', + ' ', + " ", + "", + "", + ].join("\n"); + yield* checkDocument( + withAgent, + doc, + () => recordCommand(asked), + ({ results }) => { + expect(results.map(({ status, error }) => ({ status, error }))).toEqual([ + { status: "pass", error: undefined }, + ]); + // Each isolation boundary requests the worker subcommand once. + expect(asked.calls.length).toBeGreaterThan(0); + for (const args of asked.calls) { + expect(args).toEqual(["test-agent"]); + } + }, + true, ); - - return yield* scoped(function* () { - const testing = yield* useTesting(); - yield* recordCommand(asked); - yield* installTestAgentComponents(); - yield* installAgentComponents(); - const execution = yield* execute({ - path: path.join(dir, "doc.md"), - stream: new InMemoryStream(), - }); - const subscription = yield* execution.output; - let next = yield* subscription.next(); - while (!next.done) { - next = yield* subscription.next(); - } - yield* execution; - return { results: yield* testing.results }; - }); }); -} +}); diff --git a/packages/test-agent/tests/session-configuration.test.ts b/packages/test-agent/tests/session-configuration.test.ts index 93176d6e2..745b70a3a 100644 --- a/packages/test-agent/tests/session-configuration.test.ts +++ b/packages/test-agent/tests/session-configuration.test.ts @@ -9,7 +9,7 @@ import { agentIdentityComponents, installAgentComponents } from "@executablemd/a import { parseDurableEvent } from "@executablemd/durable-streams"; import type { InMemoryStream } from "@executablemd/durable-streams"; import { API } from "@executablemd/runtime"; -import { useTesting, withFailureJournal } from "@executablemd/testing"; +import { useTesting } from "@executablemd/testing"; import type { TestResult } from "@executablemd/testing"; import { exists, readdir, readTextFile, writeTextFile } from "@effectionx/fs"; import { Err, Ok, scoped } from "effection"; @@ -17,7 +17,7 @@ import type { Operation, Result } from "effection"; import * as path from "node:path"; import process from "node:process"; import { Writable } from "node:stream"; -import { useTestDiagnostics } from "../../cli/src/test-diagnostics.ts"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; import type { DeliverySink } from "../../cli/src/stdout-delivery.ts"; import { installTestAgentComponents } from "../src/components.ts"; import { NativeSessionObserver } from "../src/controller.ts"; @@ -26,18 +26,16 @@ import { useCommand } from "./command.ts"; const DOC = path.resolve("packages/test-agent/src/SessionConfiguration.replay.md"); +const OWNER = "packages/test-agent/tests/session-configuration.test.ts"; +const SUITE = "Session configuration through TestAgent"; + function captureConfiguration( + name: string, doc: string, body: (stream: InMemoryStream) => Operation, sink: DeliverySink = process.stderr, ): Operation { - return scoped(function* () { - const diagnostics = yield* useTestDiagnostics(sink); - yield* withFailureJournal( - diagnostics.execution("root", doc).journal(() => false), - body, - ); - }); + return captureTestJournal({ owner: OWNER, suite: SUITE, name }, doc, body, { sink }); } /** A native session report stamped with milliseconds since the run began. */ @@ -130,141 +128,151 @@ function configurationSucceeded(run: { } } -describe( - "Session configuration through TestAgent", - { sanitizeOps: false, sanitizeResources: false }, - () => { - it("refuses unknown, unsupported and unavailable Session selections before consuming the first stage", function* () { - const doc = path.resolve("packages/test-agent/src/SessionConfiguration.refusals.md"); - yield* captureConfiguration(doc, function* (stream) { - const run = yield* runConfiguration(stream, cliBase(), doc); - configurationSucceeded(run); - expect(run.results.map((entry) => entry.status)).toEqual(["pass", "pass"]); - expect(new Set(run.sessions.map((report) => report.nativeSessionId)).size).toBe(2); - }); +describe(SUITE, { sanitizeOps: false, sanitizeResources: false }, () => { + const refusals = + "refuses unknown, unsupported and unavailable Session selections before consuming the first stage"; + it(refusals, function* () { + const doc = path.resolve("packages/test-agent/src/SessionConfiguration.refusals.md"); + yield* captureConfiguration(refusals, doc, function* (stream) { + const run = yield* runConfiguration(stream, cliBase(), doc); + configurationSucceeded(run); + expect(run.results.map((entry) => entry.status)).toEqual(["pass", "pass"]); + expect(new Set(run.sessions.map((report) => report.nativeSessionId)).size).toBe(2); }); + }); - it("retains named native identity, isolates Tests, and replays without a worker", function* () { - yield* captureConfiguration(DOC, function* (stream) { - const live = yield* runConfiguration(stream, cliBase(), DOC); - configurationSucceeded(live); - expect(live.results.map((entry) => entry.status)).toEqual(["pass", "pass"]); - const configuredReports = live.sessions.filter( - (report) => report.scenarioId === live.sessions[0]?.scenarioId, - ); - const independentReports = live.sessions.filter( - (report) => report.scenarioId !== live.sessions[0]?.scenarioId, - ); - expect(configuredReports.length).toBeGreaterThan(1); - expect(independentReports.length).toBe(1); - expect(new Set(configuredReports.map((report) => report.nativeSessionId)).size).toBe(1); - expect(independentReports[0]?.nativeSessionId).not.toBe( - configuredReports[0]?.nativeSessionId, - ); - const events = JSON.stringify(yield* stream.readAll()); - expect(events).toContain('"configuration":{"model":"fixture-review","effort":"deep"}'); - const appended = stream.appendCount; - const replay = yield* runConfiguration( - stream, - ["/nonexistent/xmd-configuration-must-not-spawn"], - DOC, - ); - configurationSucceeded(replay); - expect(replay.results.map((entry) => entry.status)).toEqual( - live.results.map((entry) => entry.status), - ); - expect(replay.sessions).toEqual([]); - expect(replay.output).toBe(live.output); - expect(stream.appendCount).toBe(appended); - }); + const replay = "retains named native identity, isolates Tests, and replays without a worker"; + it(replay, function* () { + yield* captureConfiguration(replay, DOC, function* (stream) { + const live = yield* runConfiguration(stream, cliBase(), DOC); + configurationSucceeded(live); + expect(live.results.map((entry) => entry.status)).toEqual(["pass", "pass"]); + const configuredReports = live.sessions.filter( + (report) => report.scenarioId === live.sessions[0]?.scenarioId, + ); + const independentReports = live.sessions.filter( + (report) => report.scenarioId !== live.sessions[0]?.scenarioId, + ); + expect(configuredReports.length).toBeGreaterThan(1); + expect(independentReports.length).toBe(1); + expect(new Set(configuredReports.map((report) => report.nativeSessionId)).size).toBe(1); + expect(independentReports[0]?.nativeSessionId).not.toBe( + configuredReports[0]?.nativeSessionId, + ); + const events = JSON.stringify(yield* stream.readAll()); + expect(events).toContain('"configuration":{"model":"fixture-review","effort":"deep"}'); + const appended = stream.appendCount; + const replay = yield* runConfiguration( + stream, + ["/nonexistent/xmd-configuration-must-not-spawn"], + DOC, + ); + configurationSucceeded(replay); + expect(replay.results.map((entry) => entry.status)).toEqual( + live.results.map((entry) => entry.status), + ); + expect(replay.sessions).toEqual([]); + expect(replay.output).toBe(live.output); + expect(stream.appendCount).toBe(appended); }); + }); - it("saves a journal after an assertion fails and leaves a passing execution unwritten", function* () { - const directory = yield* useTempDirectory("xmd-session-configuration-"); - const doc = path.join(directory, "journal-witness.md"); - const marker = "Configuration journal witness"; - yield* writeTextFile( - doc, - ` + const witness = + "saves a journal after an assertion fails and leaves a passing execution unwritten"; + it(witness, function* () { + const directory = yield* useTempDirectory("xmd-session-configuration-"); + const doc = path.join(directory, "journal-witness.md"); + const marker = "Configuration journal witness"; + yield* writeTextFile( + doc, + ` ${marker} `, - ); - const failureRoot = path.join(directory, "failed"); - const successRoot = path.join(directory, "passed"); - const failure = new Error("post-settlement assertion failed"); - - function* runWitness( - artifacts: string, - assert: () => void, - ): Operation<{ outcome: Result; stderr: string }> { - return yield* scoped(function* () { - let stderr = ""; - const sink = new Writable({ - write(chunk, _encoding, callback) { - stderr += String(chunk); - callback(); - }, - }); - yield* API.Env.around({ - *env([name], next) { - return name === "XMD_TEST_ARTIFACTS" ? artifacts : yield* next(name); - }, - }); + ); + const failureRoot = path.join(directory, "failed"); + const successRoot = path.join(directory, "passed"); + const failure = new Error("post-settlement assertion failed"); - try { - yield* captureConfiguration( - doc, - function* (stream) { - const run = yield* runConfiguration(stream, [], doc); - configurationSucceeded(run); - expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); - expect(stream.appendCount).toBeGreaterThan(0); - assert(); - }, - sink, - ); - } catch (error) { - if (!(error instanceof Error)) { - throw error; - } - return { outcome: Err(error), stderr }; - } - return { outcome: Ok(), stderr }; + function* runWitness( + artifacts: string, + assert: () => void, + ): Operation<{ outcome: Result; stderr: string }> { + return yield* scoped(function* () { + let stderr = ""; + const sink = new Writable({ + write(chunk, _encoding, callback) { + stderr += String(chunk); + callback(); + }, + }); + yield* API.Env.around({ + *env([name], next) { + return name === "XMD_TEST_ARTIFACTS" ? artifacts : yield* next(name); + }, }); - } - const failed = yield* runWitness(failureRoot, () => { - throw failure; - }); - expect(failed.outcome.ok ? undefined : failed.outcome.error).toBe(failure); - const invocations = yield* readdir(failureRoot); - expect(invocations.length).toBe(1); - const invocation = path.join(failureRoot, invocations[0]); - expect(yield* readdir(invocation)).toEqual(["root-1.jsonl"]); - const journal = path.join(invocation, "root-1.jsonl"); - expect(failed.stderr).toBe(`[xmd test] journal saved: ${journal} (root 1 ${doc}) -`); - const jsonl = yield* readTextFile(journal); - const events = jsonl - .trimEnd() - .split("\n") - .map((line) => { - const event = parseDurableEvent(line); - if (!event.ok) { - throw event.error; + try { + yield* captureConfiguration( + witness, + doc, + function* (stream) { + const run = yield* runConfiguration(stream, [], doc); + configurationSucceeded(run); + expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); + expect(stream.appendCount).toBeGreaterThan(0); + assert(); + }, + sink, + ); + } catch (error) { + if (!(error instanceof Error)) { + throw error; } - return event.value; - }); - expect(JSON.stringify(events)).toContain(marker); + return { outcome: Err(error), stderr }; + } + return { outcome: Ok(), stderr }; + }); + } - expect(yield* runWitness(successRoot, () => undefined)).toEqual({ - outcome: Ok(), - stderr: "", + const failed = yield* runWitness(failureRoot, () => { + throw failure; + }); + expect(failed.outcome.ok ? undefined : failed.outcome.error).toBe(failure); + const invocations = yield* readdir(failureRoot); + expect(invocations.length).toBe(1); + const invocation = path.join(failureRoot, invocations[0]); + expect((yield* readdir(invocation)).sort()).toEqual(["root-1.case.json", "root-1.jsonl"]); + expect(JSON.parse(yield* readTextFile(path.join(invocation, "root-1.case.json")))).toEqual({ + owner: OWNER, + suite: SUITE, + name: witness, + kind: "root", + ordinal: 1, + source: doc, + }); + const journal = path.join(invocation, "root-1.jsonl"); + expect(failed.stderr).toBe(`[xmd test] journal saved: ${journal} (root 1 ${doc}) +`); + const jsonl = yield* readTextFile(journal); + const events = jsonl + .trimEnd() + .split("\n") + .map((line) => { + const event = parseDurableEvent(line); + if (!event.ok) { + throw event.error; + } + return event.value; }); - expect(yield* exists(successRoot)).toBe(false); + expect(JSON.stringify(events)).toContain(marker); + + expect(yield* runWitness(successRoot, () => undefined)).toEqual({ + outcome: Ok(), + stderr: "", }); - }, -); + expect(yield* exists(successRoot)).toBe(false); + }); +}); diff --git a/packages/test-support/bdd.ts b/packages/test-support/bdd.ts index 62a97aa0c..1de22b2de 100644 --- a/packages/test-support/bdd.ts +++ b/packages/test-support/bdd.ts @@ -10,7 +10,12 @@ import { createTestAdapter } from "@effectionx/test-adapter"; import type { TestAdapter } from "@effectionx/test-adapter"; +import { ensure, run } from "effection"; import type { Operation } from "effection"; +import { CaseOutcome } from "./case-outcome.ts"; +import type { CaseSetup, SettledCase } from "./case-outcome.ts"; + +type Setup = () => Operation; interface TestPrimitives { // deno-lint-ignore no-explicit-any @@ -148,8 +153,63 @@ export function beforeAll(body: () => Operation): void { current?.addOnetimeSetup(body); } -export function beforeEach(body: () => Operation): void { - current?.addSetup(body); +export function beforeEach(body: Setup | CaseSetup): void { + current?.addSetup( + typeof body === "function" ? body : Object.assign(() => body.setup(), { caseOutcome: true }), + ); +} + +function runCase(adapter: TestAdapter, name: string, body: Setup): Promise { + const needsOutcome = adapter.lineage.some((suite) => + suite.setup.each.some((setup) => "caseOutcome" in setup), + ); + if (!needsOutcome) { + return adapter.runTest(body).then((result) => { + if (!result.ok) { + throw result.error; + } + }); + } + + const test = createTestAdapter({ parent: adapter, name }); + const outcome: SettledCase = { + name, + suite: adapter.fullname, + }; + test.addOnetimeSetup(function* () { + yield* CaseOutcome.set(outcome); + }); + + const task = run(function* () { + let failed = false; + yield* ensure(function* () { + try { + yield* test.destroy(); + } catch (error) { + if (!failed) { + throw error; + } + } + }); + try { + const result = yield* test.runTest(body); + if (!result.ok) { + throw result.error; + } + } catch (error) { + failed = true; + try { + if (outcome.save !== undefined) { + yield* outcome.save(); + } + } catch { + // A diagnostics failure cannot change the adapter's verdict. + } + throw error; + } + }); + // Bun waits for a native promise from the test callback. + return task.then(() => undefined); } export function it(desc: string, body?: () => Operation): void { @@ -161,12 +221,7 @@ export function it(desc: string, body?: () => Operation): void { prims().itSkip(desc, () => {}); return; } - prims().it(desc, async () => { - const result = await adapter.runTest(body); - if (!result.ok) { - throw result.error; - } - }); + prims().it(desc, () => runCase(adapter, desc, body)); } it.skip = (...args: Parameters): ReturnType => { @@ -179,10 +234,5 @@ it.only = (desc: string, body: () => Operation): void => { throw new Error("it.only() must be called within a describe() block"); } const adapter = current; - prims().itOnly(desc, async () => { - const result = await adapter.runTest(body); - if (!result.ok) { - throw result.error; - } - }); + prims().itOnly(desc, () => runCase(adapter, desc, body)); }; diff --git a/packages/test-support/case-outcome.ts b/packages/test-support/case-outcome.ts new file mode 100644 index 000000000..8beb92da5 --- /dev/null +++ b/packages/test-support/case-outcome.ts @@ -0,0 +1,14 @@ +import { createContext } from "effection"; +import type { Operation } from "effection"; + +export interface CaseSetup { + setup(): Operation; +} + +export interface SettledCase { + name: string; + suite: string; + save?: () => Operation; +} + +export const CaseOutcome = createContext("@executablemd/test-support/case-outcome"); diff --git a/packages/test-support/failure-journal.ts b/packages/test-support/failure-journal.ts new file mode 100644 index 000000000..8bddc323a --- /dev/null +++ b/packages/test-support/failure-journal.ts @@ -0,0 +1,119 @@ +import { createContext, resource, scoped } from "effection"; +import type { Operation } from "effection"; +import { InMemoryStream } from "@executablemd/durable-streams"; +import type { DurableEvent } from "@executablemd/durable-streams"; +import { withFailureJournal } from "@executablemd/testing"; +import process from "node:process"; +import { prepareTestDiagnostics, useTestDiagnostics } from "../cli/src/test-diagnostics.ts"; +import type { JournalCase, TestDiagnostics } from "../cli/src/test-diagnostics.ts"; +import type { DeliverySink } from "../cli/src/stdout-delivery.ts"; +import { CaseOutcome } from "./case-outcome.ts"; +import type { CaseSetup } from "./case-outcome.ts"; + +const CaseJournals = createContext<(source: string) => InMemoryStream>( + "@executablemd/test-support/case-journals", +); + +/** + * Give ordinary cases fresh attributed roots with one suite setup. For example, + * the held-host witness registers `beforeEach(journalBeforeEach(OWNER))`. Its + * fixture selects `request.stream ?? (yield* useJournalStream(document.path))`. + * Keep explicit replay and private inputs at the fixture that owns them. + */ +export function journalBeforeEach(owner: string): CaseSetup { + return { + *setup(): Operation { + if ((yield* CaseJournals.get()) === undefined) { + yield* CaseJournals.set(yield* useCaseJournals(owner)); + } + }, + }; +} + +function useCaseJournals(owner: string): Operation<(source: string) => InMemoryStream> { + return resource(function* (provide) { + const outcome = yield* CaseOutcome.expect(); + const openDiagnostics = yield* prepareTestDiagnostics(process.stderr, { + owner, + name: outcome.name, + suite: outcome.suite, + }); + const roots: Array<{ source: string; stream: InMemoryStream }> = []; + try { + yield* provide((source) => { + const stream = new InMemoryStream(); + roots.push({ source, stream }); + return stream; + }); + } finally { + // The adapter can reject after destroying the entire case scope. + if (roots.length > 0) { + outcome.save = () => saveCaseJournals(openDiagnostics, roots); + } + } + }); +} + +function* saveCaseJournals( + openDiagnostics: () => Operation, + roots: readonly { source: string; stream: InMemoryStream }[], +): Operation { + const diagnostics = yield* openDiagnostics(); + for (const { source, stream } of roots) { + const host = diagnostics.execution("root", source).journal(() => false); + const events = yield* stream.readAll(); + if (events.length === 0) { + continue; + } + try { + yield* host.save(events); + } catch (error) { + yield* host.reportSaveFailure(error instanceof Error ? error : new Error(String(error))); + } + } +} + +/** Open a fresh journal at the actual source of an independent execution. */ +export function* useJournalStream(source: string): Operation { + const open = yield* CaseJournals.get(); + return open === undefined ? new InMemoryStream() : open(source); +} + +/** + * Protect a repository test's fresh guarded execution and its assertions. + * Pass the outer case's declared name and repository-relative file as the + * label. A saved journal has a sibling .case.json with that label and the + * execution's kind, ordinal and displayed source. The JSONL carries the + * original durable records. + * + * A host with detached children passes its diagnostics to their journal + * factory. An owner whose stream holds more than the admitted execution can + * select the owned events for persistence after teardown. + */ +export function captureTestJournal( + testCase: JournalCase, + source: string, + body: (stream: InMemoryStream, diagnostics: TestDiagnostics) => Operation, + options: { + sink?: DeliverySink; + failed?: (value: T) => boolean; + selectEvents?: (events: readonly DurableEvent[]) => readonly DurableEvent[]; + } = {}, +): Operation { + return scoped(function* () { + const diagnostics = yield* useTestDiagnostics(options.sink ?? process.stderr, testCase); + const journal = diagnostics.execution("root", source).journal(options.failed ?? (() => false)); + return yield* withFailureJournal( + { + ...journal, + *save(events) { + const selected = options.selectEvents?.(events) ?? events; + if (selected.length > 0) { + yield* journal.save(selected); + } + }, + }, + (stream) => body(stream, diagnostics), + ); + }); +} diff --git a/packages/test-support/journal-cases.ts b/packages/test-support/journal-cases.ts new file mode 100644 index 000000000..2fa110cfb --- /dev/null +++ b/packages/test-support/journal-cases.ts @@ -0,0 +1,15 @@ +import { it } from "./bdd.ts"; +import { captureTestJournal } from "./failure-journal.ts"; +import type { InMemoryStream } from "@executablemd/durable-streams"; +import type { Operation } from "effection"; + +/** Register a case that owns one fresh guarded document execution. */ +export function journalCases(owner: string) { + return ( + name: string, + source: string, + body: (stream: InMemoryStream) => Operation, + ): void => { + it(name, () => captureTestJournal({ owner, name }, source, body)); + }; +} diff --git a/packages/test-support/package.json b/packages/test-support/package.json index dd0117448..975ae5fdb 100644 --- a/packages/test-support/package.json +++ b/packages/test-support/package.json @@ -6,6 +6,7 @@ "exports": { "./bdd": "./bdd.ts", "./expect": "./expect.ts", + "./failure-journal": "./failure-journal.ts", "./host-boundary": "./host-boundary.ts", "./journal": "./journal.ts", "./launch": "./launch.ts", @@ -19,6 +20,7 @@ "@effectionx/timebox": "0.4.3", "@executablemd/core": "workspace:*", "@executablemd/durable-streams": "workspace:*", + "@executablemd/testing": "workspace:*", "effection": "4.1.0", "expect": "^30.0.0" } diff --git a/packages/testing/tests/agent-composition.test.ts b/packages/testing/tests/agent-composition.test.ts index 619824762..4fd87023f 100644 --- a/packages/testing/tests/agent-composition.test.ts +++ b/packages/testing/tests/agent-composition.test.ts @@ -7,7 +7,7 @@ */ import { describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; -import { InMemoryStream } from "@executablemd/durable-streams"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; import { ensure } from "effection"; import type { Result, Stream } from "effection"; import { ensureDir, rm, writeTextFile } from "@effectionx/fs"; @@ -60,7 +60,8 @@ function stubStream( } describe("agent + testing composition", () => { - it("both component sets decorate one execution", function* () { + const name = "both component sets decorate one execution"; + it(name, function* () { const calls: string[] = []; yield* installTestingComponents(); yield* installAgentComponents({ @@ -114,23 +115,29 @@ describe("agent + testing composition", () => { ); // `` names durable work after its own invocation, so the host // declares it to the execution rather than registering it. - const execution = yield* executeInstalled({ path: docPath, stream: new InMemoryStream() }, [ - { components: agentIdentityComponents() }, - ]); - const subscription = yield* execution.output; - let next = yield* subscription.next(); - while (!next.done) { - next = yield* subscription.next(); - } - const result: Result = yield* execution; + yield* captureTestJournal( + { owner: "packages/testing/tests/agent-composition.test.ts", name }, + docPath, + function* (stream) { + const execution = yield* executeInstalled({ path: docPath, stream }, [ + { components: agentIdentityComponents() }, + ]); + const subscription = yield* execution.output; + let next = yield* subscription.next(); + while (!next.done) { + next = yield* subscription.next(); + } + const result: Result = yield* execution; - expect(calls).toEqual(["good", "bad"]); - expect(result.ok).toBe(false); - if (!result.ok) { - expect(result.error).toBeInstanceOf(AggregateError); - if (result.error instanceof AggregateError) { - expect(result.error.message).toBe("1 agent prompt(s) failed"); - } - } + expect(calls).toEqual(["good", "bad"]); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error).toBeInstanceOf(AggregateError); + if (result.error instanceof AggregateError) { + expect(result.error.message).toBe("1 agent prompt(s) failed"); + } + } + }, + ); }); }); diff --git a/packages/testing/tests/assert-throws.test.ts b/packages/testing/tests/assert-throws.test.ts index 33a66e24c..ebe44fc56 100644 --- a/packages/testing/tests/assert-throws.test.ts +++ b/packages/testing/tests/assert-throws.test.ts @@ -1,11 +1,13 @@ -import { describe, it } from "@executablemd/test-support/bdd"; +import { describe } from "@executablemd/test-support/bdd"; import { scoped } from "effection"; import { expect } from "@executablemd/test-support/expect"; import { AssertionError } from "node:assert/strict"; import { Component } from "@executablemd/core"; import type { ComponentElement } from "@executablemd/core"; import { createTestHandlers } from "../src/handlers.ts"; -import { failureOf, runDoc } from "./helpers.ts"; +import { documentCases, failureOf } from "./helpers.ts"; + +const documentCase = documentCases("packages/testing/tests/assert-throws.test.ts"); const STRICT = [ "---", @@ -28,43 +30,43 @@ function inTest(body: string): Record { } describe("", () => { - it("passes when the body raises an error matching a substring", function* () { - const run = yield* runDoc( + documentCase("passes when the body raises an error matching a substring", function* (runFresh) { + const run = yield* runFresh( inTest(''), ); expect(run.completion.ok).toBe(true); expect(run.results[0]?.status).toBe("pass"); }); - it("passes when the body raises an error matching a RegExp", function* () { - const run = yield* runDoc( + documentCase("passes when the body raises an error matching a RegExp", function* (runFresh) { + const run = yield* runFresh( inTest(''), ); expect(run.results[0]?.status).toBe("pass"); }); - it("fails when the body raises no error", function* () { - const run = yield* runDoc( + documentCase("fails when the body raises no error", function* (runFresh) { + const run = yield* runFresh( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); }); - it("fails when the raised error does not match", function* () { - const run = yield* runDoc( + documentCase("fails when the raised error does not match", function* (runFresh) { + const run = yield* runFresh( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); }); - it("rejects a missing message prop", function* () { - const run = yield* runDoc(inTest('')); + documentCase("rejects a missing message prop", function* (runFresh) { + const run = yield* runFresh(inTest('')); expect(run.results[0]?.status).toBe("fail"); expect(run.output).toContain('requires a "message"'); }); - it("rejects an unknown prop", function* () { - const run = yield* runDoc( + documentCase("rejects an unknown prop", function* (runFresh) { + const run = yield* runFresh( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); @@ -74,8 +76,8 @@ describe("", () => { expect(run.results[0]?.error?.message).toContain("must NOT have additional properties"); }); - it("rejects an expression-valued as", function* () { - const run = yield* runDoc( + documentCase("rejects an expression-valued as", function* (runFresh) { + const run = yield* runFresh( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); @@ -83,24 +85,24 @@ describe("", () => { expect(run.results[0]?.error?.message).toContain("must be a string literal"); }); - it("rejects an invalid as identifier", function* () { - const run = yield* runDoc( + documentCase("rejects an invalid as identifier", function* (runFresh) { + const run = yield* runFresh( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); expect(run.output).toContain("identifier"); }); - it("rejects a message that evaluates to an unsupported type", function* () { - const run = yield* runDoc( + documentCase("rejects a message that evaluates to an unsupported type", function* (runFresh) { + const run = yield* runFresh( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); expect(run.results[0]?.error?.message).toContain("string or a RegExp"); }); - it("binds the complete caught error (incl. cause) via as", function* () { - const run = yield* runDoc( + documentCase("binds the complete caught error (incl. cause) via as", function* (runFresh) { + const run = yield* runFresh( inTest( [ '', @@ -112,35 +114,41 @@ describe("", () => { expect(run.results.every((r) => r.status === "pass")).toBe(true); }); - it("re-propagates a non-raise throw (a failing assertion in the body)", function* () { - const run = yield* runDoc( - inTest( - '', - ), - ); - expect(run.results[0]?.status).toBe("fail"); - expect(run.results[0]?.error).toMatchObject({ - kind: "assertion", - actual: "1", - expected: "2", - }); - expect(run.output).toContain("**AssertEquals** failed"); - expect(run.output).not.toContain("**AssertThrows** failed"); - }); - - it("stops expanding body children after the first raised error, rendering nothing", function* () { - const run = yield* runDoc({ - "Strict.md": STRICT, - "README.md": - 'SHOULD-NOT-APPEAR\n', - }); - expect(run.completion.ok).toBe(true); - expect(run.output).not.toContain("SHOULD-NOT-APPEAR"); - expect(run.output).not.toContain("must be number"); - }); - - it("swallows silently outside a during regular execution", function* () { - const run = yield* runDoc({ + documentCase( + "re-propagates a non-raise throw (a failing assertion in the body)", + function* (runFresh) { + const run = yield* runFresh( + inTest( + '', + ), + ); + expect(run.results[0]?.status).toBe("fail"); + expect(run.results[0]?.error).toMatchObject({ + kind: "assertion", + actual: "1", + expected: "2", + }); + expect(run.output).toContain("**AssertEquals** failed"); + expect(run.output).not.toContain("**AssertThrows** failed"); + }, + ); + + documentCase( + "stops expanding body children after the first raised error, rendering nothing", + function* (runFresh) { + const run = yield* runFresh({ + "Strict.md": STRICT, + "README.md": + 'SHOULD-NOT-APPEAR\n', + }); + expect(run.completion.ok).toBe(true); + expect(run.output).not.toContain("SHOULD-NOT-APPEAR"); + expect(run.output).not.toContain("must be number"); + }, + ); + + documentCase("swallows silently outside a during regular execution", function* (runFresh) { + const run = yield* runFresh({ "Strict.md": STRICT, "README.md": 'before\n\nafter\n', @@ -155,8 +163,8 @@ describe("", () => { // to bind, and no other channel preserves a durable rendered segment — so a // passing renders nothing, even with --verbose. Failure // behavior is unchanged, which the cases above pin. - it("emits no pass diagnostic, even with verbose", function* () { - const run = yield* runDoc( + documentCase("emits no pass diagnostic, even with verbose", function* (runFresh) { + const run = yield* runFresh( { "Strict.md": STRICT, "README.md": '\n', @@ -167,16 +175,19 @@ describe("", () => { expect(run.output).not.toContain("**AssertThrows** passed"); }); - it("aborts the document outside a when no error is raised, with a visible diagnostic", function* () { - const run = yield* runDoc( - { - "Strict.md": STRICT, - "README.md": 'before\n\nnever\n', - }, - { verbose: true }, - ); - expect(failureOf(run)).toBeInstanceOf(AssertionError); - expect(run.output).toContain("**AssertThrows** failed"); - expect(run.output).not.toContain("never"); - }); + documentCase( + "aborts the document outside a when no error is raised, with a visible diagnostic", + function* (runFresh) { + const run = yield* runFresh( + { + "Strict.md": STRICT, + "README.md": 'before\n\nnever\n', + }, + { verbose: true }, + ); + expect(failureOf(run)).toBeInstanceOf(AssertionError); + expect(run.output).toContain("**AssertThrows** failed"); + expect(run.output).not.toContain("never"); + }, + ); }); diff --git a/packages/testing/tests/assertions.test.ts b/packages/testing/tests/assertions.test.ts index 692a8e19b..36df03a15 100644 --- a/packages/testing/tests/assertions.test.ts +++ b/packages/testing/tests/assertions.test.ts @@ -3,12 +3,14 @@ import { Component, useTempFileCompiler } from "@executablemd/core"; import { expect } from "@executablemd/test-support/expect"; import { AssertionError } from "node:assert/strict"; import { TestFailureError } from "../src/test-api.ts"; -import { failureOf, runDoc } from "./helpers.ts"; +import { documentCases, failureOf } from "./helpers.ts"; + +const documentCase = documentCases("packages/testing/tests/assertions.test.ts"); describe("assertion components", () => { beforeAll(() => useTempFileCompiler()); - it("passing assertions in testing mode emit diagnostics", function* () { - const run = yield* runDoc( + documentCase("passing assertions in testing mode emit diagnostics", function* (runFresh) { + const run = yield* runFresh( { "README.md": '\n', @@ -20,7 +22,7 @@ describe("assertion components", () => { expect(run.results).toEqual([{ status: "pass", name: "eq", location: "README.md:1:10" }]); }); - it("a validation diagnostic is observed once and rendered once", function* () { + documentCase("a validation diagnostic is observed once and rendered once", function* (runFresh) { const observed: string[] = []; yield* Component.around({ *raise([error], next) { @@ -30,7 +32,7 @@ describe("assertion components", () => { }); // Written in a region that prints: rendering the diagnostic once is half of // what this pins, and reading it needs the document to render it. - const run = yield* runDoc({ + const run = yield* runFresh({ "README.md": "\n\n\n", }); @@ -42,18 +44,21 @@ describe("assertion components", () => { expect(run.output.match(/must NOT have additional properties/g)).toHaveLength(1); }); - it("assertions outside a test pass silently during regular execution", function* () { - const run = yield* runDoc({ - "README.md": "before\n\nafter\n", - }); - expect(run.completion.ok).toBe(true); - expect(run.output).not.toContain("AssertEquals"); - expect(run.output).toContain("before"); - expect(run.output).toContain("after"); - }); + documentCase( + "assertions outside a test pass silently during regular execution", + function* (runFresh) { + const run = yield* runFresh({ + "README.md": "before\n\nafter\n", + }); + expect(run.completion.ok).toBe(true); + expect(run.output).not.toContain("AssertEquals"); + expect(run.output).toContain("before"); + expect(run.output).toContain("after"); + }, + ); - it("assertions outside a test emit diagnostics with verbose", function* () { - const run = yield* runDoc( + documentCase("assertions outside a test emit diagnostics with verbose", function* (runFresh) { + const run = yield* runFresh( { "README.md": "\n" }, { verbose: true }, ); @@ -61,8 +66,8 @@ describe("assertion components", () => { expect(run.output).toContain("**AssertEquals** passed"); }); - it("a failed assertion outside a test aborts the document", function* () { - const run = yield* runDoc({ + documentCase("a failed assertion outside a test aborts the document", function* (runFresh) { + const run = yield* runFresh({ "README.md": "before\n\nnever\n", }); const error = failureOf(run); @@ -72,17 +77,20 @@ describe("assertion components", () => { expect(run.output).not.toContain("AssertEquals"); }); - it("a failed assertion outside a test keeps its diagnostic with verbose", function* () { - const run = yield* runDoc( - { "README.md": "before\n\nnever\n" }, - { verbose: true }, - ); - expect(failureOf(run)).toBeInstanceOf(AssertionError); - expect(run.output).toContain("**AssertEquals** failed"); - expect(run.output).toContain("before"); - }); + documentCase( + "a failed assertion outside a test keeps its diagnostic with verbose", + function* (runFresh) { + const run = yield* runFresh( + { "README.md": "before\n\nnever\n" }, + { verbose: true }, + ); + expect(failureOf(run)).toBeInstanceOf(AssertionError); + expect(run.output).toContain("**AssertEquals** failed"); + expect(run.output).toContain("before"); + }, + ); - it("expected children behave like trimming", function* () { + documentCase("expected children behave like trimming", function* (runFresh) { const doc = [ "", '', @@ -94,45 +102,45 @@ describe("assertion components", () => { "", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results.map((r) => r.status)).toEqual(["pass"]); }); - it("rejects both expected prop and expected children", function* () { + documentCase("rejects both expected prop and expected children", function* (runFresh) { const doc = "1\n"; - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(failureOf(run)).toBeInstanceOf(TestFailureError); expect(run.results[0]?.status).toBe("fail"); expect(run.results[0]?.error?.kind).toBe("error"); expect(run.results[0]?.error?.message).toContain("not both"); }); - it("rejects expected children on unary and numeric assertions", function* () { + documentCase("rejects expected children on unary and numeric assertions", function* (runFresh) { const doc = "x\n"; - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results[0]?.error?.message).toContain("expected children"); }); - it("match assertions require a real RegExp", function* () { + documentCase("match assertions require a real RegExp", function* (runFresh) { const doc = '\n'; - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results[0]?.status).toBe("fail"); expect(run.results[0]?.error?.message).toContain("RegExp"); }); - it("match assertions accept a RegExp expression", function* () { + documentCase("match assertions accept a RegExp expression", function* (runFresh) { const doc = '\n'; - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results[0]?.status).toBe("pass"); }); - it("unknown props are rejected per kind", function* () { + documentCase("unknown props are rejected per kind", function* (runFresh) { const doc = "\n"; - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); // `actual` is a capture for the binary kinds but not for unary-truthy, so // never declares it and the schema rejects it like any other // undeclared prop. @@ -140,13 +148,13 @@ describe("assertion components", () => { expect(run.results[0]?.error?.message).toContain("must NOT have additional properties"); }); - it("missing required props are rejected", function* () { + documentCase("missing required props are rejected", function* (runFresh) { const doc = "\n"; - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results[0]?.error?.message).toContain('"expected"'); }); - it("assertion expressions see live bindings from eval blocks", function* () { + documentCase("assertion expressions see live bindings from eval blocks", function* (runFresh) { const doc = [ "", "```js eval", @@ -156,28 +164,31 @@ describe("assertion components", () => { "", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results[0]?.status).toBe("pass"); }); - it("assertion expressions see caller bindings through projection", function* () { - const doc = [ - "```js eval", - 'const fromCaller = "outer-value";', - "```", - "", - '', - "", - "", - ].join("\n"); - const wrap = "projected: \n"; - const run = yield* runDoc({ "README.md": doc, "components/Wrap.md": wrap }); - expect(run.completion.ok).toBe(true); - expect(run.results[0]?.status).toBe("pass"); - }); + documentCase( + "assertion expressions see caller bindings through projection", + function* (runFresh) { + const doc = [ + "```js eval", + 'const fromCaller = "outer-value";', + "```", + "", + '', + "", + "", + ].join("\n"); + const wrap = "projected: \n"; + const run = yield* runFresh({ "README.md": doc, "components/Wrap.md": wrap }); + expect(run.completion.ok).toBe(true); + expect(run.results[0]?.status).toBe("pass"); + }, + ); - it("formatter-visible toJSON/toString cannot change the outcome", function* () { + documentCase("formatter-visible toJSON/toString cannot change the outcome", function* (runFresh) { const doc = [ "", "```js eval", @@ -187,13 +198,13 @@ describe("assertion components", () => { "", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results[0]?.status).toBe("pass"); expect(run.output).toContain("unformattable"); }); - it("a non-string msg is rejected by type check, never formatted", function* () { + documentCase("a non-string msg is rejected by type check, never formatted", function* (runFresh) { // If msg were formatted before the assertion, the hostile toJSON or // toString would throw and replace the validation outcome. const doc = [ @@ -202,7 +213,7 @@ describe("assertion components", () => { "", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(failureOf(run)).toBeInstanceOf(TestFailureError); expect(run.results[0]?.status).toBe("fail"); expect(run.results[0]?.error?.kind).toBe("error"); @@ -211,42 +222,48 @@ describe("assertion components", () => { expect(run.results[0]?.error?.message).not.toContain("hostile-string"); }); - it("a throwing getter read at format time cannot change the outcome", function* () { - const doc = [ - "", - "", - "", - "", - ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); - expect(run.completion.ok).toBe(true); - expect(run.results[0]?.status).toBe("pass"); - expect(run.output).toContain("[object Object]"); - }); + documentCase( + "a throwing getter read at format time cannot change the outcome", + function* (runFresh) { + const doc = [ + "", + "", + "", + "", + ].join("\n"); + const run = yield* runFresh({ "README.md": doc }); + expect(run.completion.ok).toBe(true); + expect(run.results[0]?.status).toBe("pass"); + expect(run.output).toContain("[object Object]"); + }, + ); // §5b: `as` and `slot` are the engine's, consumed before validation, so an // assertion accepts them like any other component rather than rejecting them // from a hand-written allowed-list. - it("accepts `as` on a value assertion, binding its diagnostic text", function* () { - const doc = [ - "", - '', - '', - '', - "", - "", - "", - ].join("\n"); + documentCase( + "accepts `as` on a value assertion, binding its diagnostic text", + function* (runFresh) { + const doc = [ + "", + '', + '', + '', + "", + "", + "", + ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); - expect(run.results[0]?.status).toBe("pass"); - }); + expect(run.results[0]?.status).toBe("pass"); + }, + ); // A registration is a default, so a repository file of the same name wins. // The local component declares a props schema: a markdown component without // one accepts no props at all, and would be rejected before it could answer. - it("a repository AssertEquals overrides the registered default", function* () { + documentCase("a repository AssertEquals overrides the registered default", function* (runFresh) { const local = [ "---", "props:", @@ -260,7 +277,7 @@ describe("assertion components", () => { "", ].join("\n"); - const run = yield* runDoc({ + const run = yield* runFresh({ "README.md": [ "", '', @@ -277,41 +294,49 @@ describe("assertion components", () => { // Parity with the handler this replaced: an operand that throws is reported // by the assertion that owns it, not as the invocation's own failure. - it("reports an operand expression that throws as its own diagnostic", function* () { - const doc = [ - "", - '', - ' { throw new Error("operand exploded"); })()} expected={1} />', - "", - "", - "", - ].join("\n"); + documentCase( + "reports an operand expression that throws as its own diagnostic", + function* (runFresh) { + const doc = [ + "", + '', + ' { throw new Error("operand exploded"); })()} expected={1} />', + "", + "", + "", + ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); - expect(run.results[0]?.status).toBe("fail"); - expect(run.results[0]?.error?.message).toContain('failed to evaluate the "actual" expression'); - expect(run.results[0]?.error?.message).toContain("operand exploded"); - }); + expect(run.results[0]?.status).toBe("fail"); + expect(run.results[0]?.error?.message).toContain( + 'failed to evaluate the "actual" expression', + ); + expect(run.results[0]?.error?.message).toContain("operand exploded"); + }, + ); // `undefined` cannot survive the JSON gate, so before captures this could not // be written at all — the engine rejected the prop before the assertion ran. - it(" fails on undefined rather than rejecting the prop", function* () { - const doc = [ - "", - '', - "", - "", - "", - "", - ].join("\n"); + documentCase( + " fails on undefined rather than rejecting the prop", + function* (runFresh) { + const doc = [ + "", + '', + "", + "", + "", + "", + ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); - // The assertion's own comparison failed — not the engine refusing the prop, - // which is what happened before `actual` became a capture. - expect(run.results[0]?.status).toBe("fail"); - expect(run.results[0]?.error?.kind).toBe("assertion"); - expect(run.results[0]?.error?.message).not.toContain("non-serializable"); - }); + // The assertion's own comparison failed — not the engine refusing the prop, + // which is what happened before `actual` became a capture. + expect(run.results[0]?.status).toBe("fail"); + expect(run.results[0]?.error?.kind).toBe("assertion"); + expect(run.results[0]?.error?.message).not.toContain("non-serializable"); + }, + ); }); diff --git a/packages/testing/tests/boundary-contract.test.ts b/packages/testing/tests/boundary-contract.test.ts index 789c406ea..b06ad186f 100644 --- a/packages/testing/tests/boundary-contract.test.ts +++ b/packages/testing/tests/boundary-contract.test.ts @@ -7,7 +7,9 @@ import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "@executablemd/core"; import { expect } from "@executablemd/test-support/expect"; -import { runDoc } from "./helpers.ts"; +import { documentCases } from "./helpers.ts"; + +const documentCase = documentCases("packages/testing/tests/boundary-contract.test.ts"); describe("Tier TC — the boundary", () => { beforeAll(() => useTempFileCompiler()); @@ -16,33 +18,36 @@ describe("Tier TC — the boundary", () => { // that is ``'s job — so an error beside a test renders where it was // written and the boundary still reports. Projecting the body through // `content()` instead would abort the boundary and lose both. - it("TC1: an error beside a test renders inline, and the boundary still reports", function* () { - // The region prints, which is what makes "renders inline and the boundary - // still reports" a thing the document asked for. - const doc = [ - "", - "", - "", - "", - "", - '', - "", - "", - "", - "", - ].join("\n"); - - const run = yield* runDoc({ "README.md": doc }); - - // The stray error is rendered, not swallowed and not fatal… - expect(run.output).toContain("Failed to import component Missing"); - // …the test beside it still ran… - expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); - // …and the boundary still reported its outcome. - expect(run.boundaries).toEqual([{ tests: 1, failed: 0 }]); - }); - - it("TC2: text around a test is preserved in order", function* () { + documentCase( + "TC1: an error beside a test renders inline, and the boundary still reports", + function* (runFresh) { + // The region prints, which is what makes "renders inline and the boundary + // still reports" a thing the document asked for. + const doc = [ + "", + "", + "", + "", + "", + '', + "", + "", + "", + "", + ].join("\n"); + + const run = yield* runFresh({ "README.md": doc }); + + // The stray error is rendered, not swallowed and not fatal… + expect(run.output).toContain("Failed to import component Missing"); + // …the test beside it still ran… + expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); + // …and the boundary still reported its outcome. + expect(run.boundaries).toEqual([{ tests: 1, failed: 0 }]); + }, + ); + + documentCase("TC2: text around a test is preserved in order", function* (runFresh) { const doc = [ "", "", @@ -56,14 +61,14 @@ describe("Tier TC — the boundary", () => { "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.output.indexOf("BEFORE")).toBeGreaterThan(-1); expect(run.output.indexOf("AFTER")).toBeGreaterThan(run.output.indexOf("BEFORE")); }); - it("TC3: an empty boundary reports zero tests", function* () { - const run = yield* runDoc({ "README.md": "\n\nnothing here\n\n\n" }); + documentCase("TC3: an empty boundary reports zero tests", function* (runFresh) { + const run = yield* runFresh({ "README.md": "\n\nnothing here\n\n\n" }); expect(run.boundaries).toEqual([{ tests: 0, failed: 0 }]); }); @@ -74,33 +79,36 @@ describe("Tier TC — what a keeps when it fails", () => { // The behavior `content()` cannot express: a test that fails partway still // shows what it rendered up to that point, then its diagnostics. - it("TC4: output produced before a failure survives, and precedes the diagnostic", function* () { - const doc = [ - "", - '', - "", - "RENDERED FIRST", - "", - "", - "", - "NEVER REACHED", - "", - "", - "", - "", - ].join("\n"); - - const run = yield* runDoc({ "README.md": doc }); - - expect(run.results.map((entry) => entry.status)).toEqual(["fail"]); - expect(run.output).toContain("RENDERED FIRST"); - // The body stopped where it failed. - expect(run.output).not.toContain("NEVER REACHED"); - // The diagnostic follows what was rendered, rather than replacing it. - expect(run.output.indexOf("RENDERED FIRST")).toBeLessThan(run.output.indexOf("partial")); - }); - - it("TC5: exactly one result is recorded for a failing test", function* () { + documentCase( + "TC4: output produced before a failure survives, and precedes the diagnostic", + function* (runFresh) { + const doc = [ + "", + '', + "", + "RENDERED FIRST", + "", + "", + "", + "NEVER REACHED", + "", + "", + "", + "", + ].join("\n"); + + const run = yield* runFresh({ "README.md": doc }); + + expect(run.results.map((entry) => entry.status)).toEqual(["fail"]); + expect(run.output).toContain("RENDERED FIRST"); + // The body stopped where it failed. + expect(run.output).not.toContain("NEVER REACHED"); + // The diagnostic follows what was rendered, rather than replacing it. + expect(run.output.indexOf("RENDERED FIRST")).toBeLessThan(run.output.indexOf("partial")); + }, + ); + + documentCase("TC5: exactly one result is recorded for a failing test", function* (runFresh) { const doc = [ "", '', @@ -108,31 +116,34 @@ describe("Tier TC — what a keeps when it fails", () => { "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results).toHaveLength(1); expect(run.boundaries).toEqual([{ tests: 1, failed: 1 }]); }); - it("TC6: a failing test returns only text — no error segment escapes it", function* () { - const doc = [ - "", - '', - "", - "AFTER", - "", - "", - ].join("\n"); - - const run = yield* runDoc({ "README.md": doc }); - - expect(run.results.map((entry) => entry.status)).toEqual(["fail"]); - // The failure is contained in the test's own report; the boundary carries on. - expect(run.output).toContain("AFTER"); - expect(run.boundaries).toEqual([{ tests: 1, failed: 1 }]); - }); - - it("TC7: a later test runs after an earlier one fails", function* () { + documentCase( + "TC6: a failing test returns only text — no error segment escapes it", + function* (runFresh) { + const doc = [ + "", + '', + "", + "AFTER", + "", + "", + ].join("\n"); + + const run = yield* runFresh({ "README.md": doc }); + + expect(run.results.map((entry) => entry.status)).toEqual(["fail"]); + // The failure is contained in the test's own report; the boundary carries on. + expect(run.output).toContain("AFTER"); + expect(run.boundaries).toEqual([{ tests: 1, failed: 1 }]); + }, + ); + + documentCase("TC7: a later test runs after an earlier one fails", function* (runFresh) { const doc = [ "", '', @@ -141,13 +152,13 @@ describe("Tier TC — what a keeps when it fails", () => { "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results.map((entry) => entry.name)).toEqual(["first", "second"]); expect(run.results.map((entry) => entry.status)).toEqual(["fail", "pass"]); }); - it("TC8: a nested fails the enclosing test, and only it", function* () { + documentCase("TC8: a nested fails the enclosing test, and only it", function* (runFresh) { const doc = [ "", '', @@ -157,14 +168,14 @@ describe("Tier TC — what a keeps when it fails", () => { "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results).toHaveLength(1); expect(run.results[0]?.name).toBe("outer"); expect(run.results[0]?.status).toBe("fail"); }); - it("TC9: bindings a test captures do not escape it", function* () { + documentCase("TC9: bindings a test captures do not escape it", function* (runFresh) { const doc = [ "", '', @@ -179,7 +190,7 @@ describe("Tier TC — what a keeps when it fails", () => { "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); // The binding did not leak into the document after the boundary. diff --git a/packages/testing/tests/execution-harness.test.ts b/packages/testing/tests/execution-harness.test.ts index c9ca9af57..ef14cfae9 100644 --- a/packages/testing/tests/execution-harness.test.ts +++ b/packages/testing/tests/execution-harness.test.ts @@ -15,15 +15,14 @@ import type { Operation, Result } from "effection"; import { forEach } from "@effectionx/stream-helpers"; import { InMemoryStream } from "@executablemd/durable-streams"; import { API } from "@executablemd/runtime"; -import { readdir } from "@effectionx/fs"; +import { readdir, readTextFile } from "@effectionx/fs"; import { useTempDirectory } from "@executablemd/test-support/temp"; import { join } from "node:path"; import process from "node:process"; import { Writable } from "node:stream"; -import { useTestDiagnostics } from "../../cli/src/test-diagnostics.ts"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; import type { DeliverySink } from "../../cli/src/stdout-delivery.ts"; import { readRecords } from "../../cli/src/repl/journal-records.ts"; -import { withFailureJournal } from "../src/failure-journal.ts"; import { useStubFs } from "@executablemd/runtime/test"; import { Component, @@ -103,30 +102,33 @@ function* runHarness( }); } +const OWNER = "packages/testing/tests/execution-harness.test.ts"; +const RUN_SUITE = ''; + function captureHarness( + name: string, files: Record, check: (run: HarnessRun) => void, sink: DeliverySink = process.stderr, onTeardown?: StubHostOptions["onTeardown"], ): Operation { - return scoped(function* () { - const diagnostics = yield* useTestDiagnostics(sink); - yield* withFailureJournal( - diagnostics.execution("root", "README.md").journal(() => false), - function* (stream) { - check( - yield* runHarness(files, { - stream, - childJournal: (source) => - diagnostics - .execution("child", source) - .journal(({ outcome }) => outcome.kind === "settled" && !outcome.result.ok), - ...(onTeardown === undefined ? {} : { onTeardown }), - }), - ); - }, - ); - }); + return captureTestJournal( + { owner: OWNER, suite: RUN_SUITE, name }, + "README.md", + function* (stream, diagnostics) { + check( + yield* runHarness(files, { + stream, + childJournal: (source) => + diagnostics + .execution("child", source) + .journal(({ outcome }) => outcome.kind === "settled" && !outcome.result.ok), + ...(onTeardown === undefined ? {} : { onTeardown }), + }), + ); + }, + { sink }, + ); } /** What ended a run that produced no test result. */ @@ -160,9 +162,11 @@ const CHILD_WITH_PROPS = doc( "hello {props.who}", ); -describe('', () => { - it("runs a referenced document as a root, with root props and text return", function* () { +describe(RUN_SUITE, () => { + const referenced = "runs a referenced document as a root, with root props and text return"; + it(referenced, function* () { yield* captureHarness( + referenced, { "README.md": doc( '', @@ -184,7 +188,9 @@ describe('', () => { ); }); - it("attributes bound child failures and post-run root failures to joined, separate executions", function* () { + const witnessCase = + "attributes bound child failures and post-run root failures to joined, separate executions"; + it(witnessCase, function* () { const artifacts = join(yield* useTempDirectory("xmd-nested-journals-"), "journals"); yield* API.Env.around({ *env([name], next) { @@ -204,6 +210,7 @@ describe('', () => { let caught: unknown; try { yield* captureHarness( + witnessCase, { "README.md": doc( '', @@ -238,11 +245,22 @@ describe('', () => { expect(order[0]).toBe("child teardown"); const invocations = yield* readdir(artifacts); - const invocation = invocations.find((name) => - order[1]?.includes(join(artifacts, name, "child-1.jsonl")), - ); + const attribution = { owner: OWNER, suite: RUN_SUITE, name: witnessCase, ordinal: 1 }; + const childCase = { ...attribution, kind: "child", source: target }; + let invocation: string | undefined; + for (const candidate of invocations) { + const file = join(artifacts, candidate, "child-1.case.json"); + if ((yield* readdir(join(artifacts, candidate))).includes("child-1.case.json")) { + const metadata = JSON.parse(yield* readTextFile(file)); + if (metadata.source === target) { + expect(metadata).toEqual(childCase); + invocation = candidate; + break; + } + } + } if (invocation === undefined) { - throw new Error(`no child artifact for ${target}: ${order.join("")}`); + throw new Error(`no child artifact for ${target}`); } const directory = join(artifacts, invocation); const child = join(directory, "child-1.jsonl"); @@ -258,13 +276,20 @@ describe('', () => { : []), ]); expect((yield* readdir(directory)).sort()).toEqual( - failRoot ? ["child-1.jsonl", "root-1.jsonl"] : ["child-1.jsonl"], + failRoot + ? ["child-1.case.json", "child-1.jsonl", "root-1.case.json", "root-1.jsonl"] + : ["child-1.case.json", "child-1.jsonl"], ); const childEvents = yield* readRecords(child); const isExec = (event: (typeof childEvents)[number]) => event.type === "yield" && event.description.type === "exec"; expect(childEvents.some(isExec)).toBe(true); if (failRoot) { + expect(JSON.parse(yield* readTextFile(join(directory, "root-1.case.json")))).toEqual({ + ...attribution, + kind: "root", + source: "README.md", + }); const rootEvents = yield* readRecords(root); expect(rootEvents.some(isExec)).toBe(false); expect(rootEvents.at(-1)).toMatchObject({ type: "close", coroutineId: "root" }); @@ -280,39 +305,52 @@ describe('', () => { expect((yield* readdir(artifacts)).sort()).toEqual([...directories].sort()); }); - it("selects one target inside a document without component addressability", function* () { - const run = yield* runHarness({ - "README.md": doc( - '', - '', - '', - "", - "", - '', - "", - "", - ), - "guide.md": doc("# First", "", "first body", "", "# Second", "", "second body"), - }); - expect(only(run).status).toBe("pass"); - expect(run.output).not.toContain("first body"); + const selectedTarget = "selects one target inside a document without component addressability"; + it(selectedTarget, function* () { + yield* captureHarness( + selectedTarget, + { + "README.md": doc( + '', + '', + '', + "", + "", + '', + "", + "", + ), + "guide.md": doc("# First", "", "first body", "", "# Second", "", "second body"), + }, + (run) => { + expect(only(run).status).toBe("pass"); + expect(run.output).not.toContain("first body"); + }, + ); }); - it("carries a text root's rendered markdown as the settled value", function* () { - const run = yield* runHarness({ - "README.md": doc( - '', - '', - '', - "", - "", - ), - "child.md": doc("child body"), - }); - expect(only(run).status).toBe("pass"); + const textRoot = "carries a text root's rendered markdown as the settled value"; + it(textRoot, function* () { + yield* captureHarness( + textRoot, + { + "README.md": doc( + '', + '', + '', + "", + "", + ), + "child.md": doc("child body"), + }, + (run) => { + expect(only(run).status).toBe("pass"); + }, + ); }); - it("runs inline source under the identity, with no authored file", function* () { + const inline = "runs inline source under the identity, with no authored file"; + it(inline, function* () { const files = { "README.md": "" }; files["README.md"] = doc( '', @@ -323,7 +361,7 @@ describe('', () => { "", "", ); - yield* captureHarness(files, (run) => { + yield* captureHarness(inline, files, (run) => { expect(only(run).status).toBe("pass"); // The identity the child reports is the inline one, and the harness named // no file for it to have come from. That production takes the same path is diff --git a/packages/testing/tests/helpers.ts b/packages/testing/tests/helpers.ts index f95092a40..d5669be17 100644 --- a/packages/testing/tests/helpers.ts +++ b/packages/testing/tests/helpers.ts @@ -12,6 +12,8 @@ import { InMemoryStream } from "@executablemd/durable-streams"; import type { DurableStream } from "@executablemd/durable-streams"; import { useStubFs } from "@executablemd/runtime/test"; import { execute } from "@executablemd/core"; +import { it } from "@executablemd/test-support/bdd"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; import { useTesting } from "../src/use-testing.ts"; import { installHandlers, installTestingComponents } from "../src/components.ts"; import { activateTesting } from "../src/activation.ts"; @@ -41,6 +43,27 @@ export interface RunDocOptions { stream?: DurableStream; } +type RunFreshDoc = ( + files: Record, + options?: Omit, +) => Operation; + +export function documentCases(owner: string) { + return (name: string, body: (run: RunFreshDoc) => Operation): void => { + it(name, () => withFreshDocument(owner, name, body)); + }; +} + +export function withFreshDocument( + owner: string, + name: string, + body: (run: RunFreshDoc) => Operation, +): Operation { + return captureTestJournal({ owner, name }, "README.md", (stream) => + body((files, options) => runDoc(files, { ...options, stream })), + ); +} + export function* runDoc( files: Record, options: RunDocOptions = {}, diff --git a/packages/testing/tests/test-component.test.ts b/packages/testing/tests/test-component.test.ts index 7e270d9f3..5ee77a0a6 100644 --- a/packages/testing/tests/test-component.test.ts +++ b/packages/testing/tests/test-component.test.ts @@ -18,7 +18,10 @@ import { Component, registerComponents, useTempFileCompiler } from "@executablem import type { ErrorSegment } from "@executablemd/core"; import { createTestHandlers } from "../src/handlers.ts"; import { absorbTestFailure, Staging } from "../src/test-component.ts"; -import { runDoc } from "./helpers.ts"; +import { documentCases, runDoc, withFreshDocument } from "./helpers.ts"; + +const OWNER = "packages/testing/tests/test-component.test.ts"; +const documentCase = documentCases(OWNER); describe(" as a function component", () => { beforeAll(() => useTempFileCompiler()); @@ -29,56 +32,59 @@ describe(" as a function component", () => { 'yield* spawn(function* () { try { yield* suspend(); } finally { throw new Error("cleanup exploded"); } });', "```", ].join("\n"); - it("keeps Try release safety local to the canonical failed Test report", function* () { - let released = 0; - let replacement = 0; - let finalized = 0; - yield* registerComponents([ - { - name: "BrokenTestRelease", - origin: "test-interaction", - props: { type: "object" }, - *fn() { - yield* ensure(() => { - released++; - throw new Error("test release failed"); - }); - return "body"; + documentCase( + "keeps Try release safety local to the canonical failed Test report", + function* (runFresh) { + let released = 0; + let replacement = 0; + let finalized = 0; + yield* registerComponents([ + { + name: "BrokenTestRelease", + origin: "test-interaction", + props: { type: "object" }, + *fn() { + yield* ensure(() => { + released++; + throw new Error("test release failed"); + }); + return "body"; + }, }, - }, - { - name: "TestReplacement", - origin: "test-interaction", - props: { type: "object" }, - *fn() { - replacement++; - return "WRONG"; + { + name: "TestReplacement", + origin: "test-interaction", + props: { type: "object" }, + *fn() { + replacement++; + return "WRONG"; + }, }, - }, - { - name: "TestFinally", - origin: "test-interaction", - props: { type: "object" }, - *fn() { - finalized++; - return "WRONG"; + { + name: "TestFinally", + origin: "test-interaction", + props: { type: "object" }, + *fn() { + finalized++; + return "WRONG"; + }, }, - }, - ]); - const run = yield* runDoc({ - "README.md": - '', - }); - expect(released).toBe(1); - expect(replacement).toBe(0); - expect(finalized).toBe(0); - expect(run.results.map((result) => [result.name, result.status])).toEqual([ - ["broken", "fail"], - ["fine", "pass"], - ]); - expect(run.completion.ok).toBe(false); - expect(run.output).not.toContain("WRONG"); - }); + ]); + const run = yield* runFresh({ + "README.md": + '', + }); + expect(released).toBe(1); + expect(replacement).toBe(0); + expect(finalized).toBe(0); + expect(run.results.map((result) => [result.name, result.status])).toEqual([ + ["broken", "fail"], + ["fine", "pass"], + ]); + expect(run.completion.ok).toBe(false); + expect(run.output).not.toContain("WRONG"); + }, + ); it("keeps actual Files infrastructure failure globally terminal inside canonical Test", function* () { let fatal: unknown; @@ -140,7 +146,7 @@ describe(" as a function component", () => { // diagnostic before the test claims it. That must not cost the property the // interception exists for: a raise the test does NOT claim still fails it, // even when other middleware observed the segment first. - it("fails the test on a raise an outer observer already saw", function* () { + documentCase("fails the test on a raise an outer observer already saw", function* (runFresh) { const observed: ErrorSegment[] = []; const doc = [ "", @@ -170,7 +176,7 @@ describe(" as a function component", () => { return yield* next(segment); }, }); - return yield* runDoc({ "README.md": doc, "Strict.md": strict }); + return yield* runFresh({ "README.md": doc, "Strict.md": strict }); }); // The observer saw it, and the test still failed because of it. @@ -182,7 +188,7 @@ describe(" as a function component", () => { // Content a component projects is the CALLER's markdown, so `{meta.x}` in it // resolves against the expansion that wrote it, which projection has to be // handed. - it("resolves the document's frontmatter inside a test body", function* () { + documentCase("resolves the document's frontmatter inside a test body", function* (runFresh) { const doc = [ "---", "title: Executable MDX", @@ -197,7 +203,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results[0]?.status).toBe("pass"); expect(run.completion.ok).toBe(true); @@ -207,30 +213,33 @@ describe(" as a function component", () => { // own eval scope, so its cleanup failing IS the test's invocation teardown. // That arrives at the session boundary after has returned, and has to // land on that test's staged result. - it("records a teardown failure as that test's outcome, and fails only it", function* () { - const doc = [ - "", - '', - FAILING_CLEANUP, - "", - "", - '', - "", - "", - ].join("\n"); - - const run = yield* runDoc({ "README.md": doc }); - - expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ - ["tears", "fail", "teardown"], - ["fine", "pass", undefined], - ]); - expect(run.results[0]?.error?.message).toContain("cleanup exploded"); - }); + documentCase( + "records a teardown failure as that test's outcome, and fails only it", + function* (runFresh) { + const doc = [ + "", + '', + FAILING_CLEANUP, + "", + "", + '', + "", + "", + ].join("\n"); + + const run = yield* runFresh({ "README.md": doc }); + + expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ + ["tears", "fail", "teardown"], + ["fine", "pass", undefined], + ]); + expect(run.results[0]?.error?.message).toContain("cleanup exploded"); + }, + ); // The body's account is the more useful one, so a test that already failed // keeps its own classification when teardown fails too. - it("keeps the body's classification when teardown also fails", function* () { + documentCase("keeps the body's classification when teardown also fails", function* (runFresh) { const doc = [ "", '', @@ -241,7 +250,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results[0]?.status).toBe("fail"); expect(run.results[0]?.error?.kind).toBe("assertion"); @@ -249,33 +258,38 @@ describe(" as a function component", () => { // A component failing beside a test is not the test's outcome: the session // layer delegates anything that is not a invocation. - it("does not turn a neighbouring component's failure into a test result", function* () { - const doc = ["", '', "", ""].join("\n"); - - const run = yield* scoped(function* () { - yield* registerComponents([ - { - name: "Wilting", - origin: "test", - props: { type: "object", properties: {}, additionalProperties: false }, - *fn() { - yield* ensure(function* () { - throw new Error("component cleanup exploded"); - }); - return ""; + documentCase( + "does not turn a neighbouring component's failure into a test result", + function* (runFresh) { + const doc = ["", '', "", ""].join( + "\n", + ); + + const run = yield* scoped(function* () { + yield* registerComponents([ + { + name: "Wilting", + origin: "test", + props: { type: "object", properties: {}, additionalProperties: false }, + *fn() { + yield* ensure(function* () { + throw new Error("component cleanup exploded"); + }); + return ""; + }, }, - }, - ]); - return yield* runDoc({ "README.md": doc }); - }); + ]); + return yield* runFresh({ "README.md": doc }); + }); - // One result, for the test that contained it — classified as an ordinary - // error, because the component's teardown is the component's, not the - // test's. - expect(run.results).toHaveLength(1); - expect(run.results[0]?.name).toBe("only"); - expect(run.results[0]?.error?.kind).toBe("error"); - }); + // One result, for the test that contained it — classified as an ordinary + // error, because the component's teardown is the component's, not the + // test's. + expect(run.results).toHaveLength(1); + expect(run.results[0]?.name).toBe("only"); + expect(run.results[0]?.error?.kind).toBe("error"); + }, + ); /** Every journalled entry of `type`, by the name it recorded, in order. */ function* entriesOf(stream: InMemoryStream, type: string): Operation { @@ -365,7 +379,7 @@ describe(" as a function component", () => { // A timed-out body was halted mid-flight, so there is no partial output to // keep — the report is the diagnostic alone (§5.3). - it("renders only the diagnostic when a test times out", function* () { + documentCase("renders only the diagnostic when a test times out", function* (runFresh) { const doc = [ "", '', @@ -379,7 +393,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runDoc( + const run = yield* runFresh( { "README.md": doc }, { handlers: createTestHandlers({ timeoutMs: 60 }) }, ); @@ -391,7 +405,7 @@ describe(" as a function component", () => { // `timeout=` is the test's own bound: a body the installed default would cut // short finishes inside the duration the element declares (§5.3). - it("lets a declared timeout outlive a shorter installed default", function* () { + documentCase("lets a declared timeout outlive a shorter installed default", function* (runFresh) { const doc = [ "", '', @@ -404,7 +418,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runDoc( + const run = yield* runFresh( { "README.md": doc }, { handlers: createTestHandlers({ timeoutMs: 60 }) }, ); @@ -414,7 +428,7 @@ describe(" as a function component", () => { // The declared bound also cuts, and the diagnostic names the duration the // element declared rather than the default it replaced. - it("times out at a declared timeout below the default", function* () { + documentCase("times out at a declared timeout below the default", function* (runFresh) { const doc = [ "", '', @@ -426,7 +440,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results[0]?.error?.kind).toBe("timeout"); expect(run.results[0]?.error?.message).toContain("0.06 seconds"); @@ -434,7 +448,7 @@ describe(" as a function component", () => { // A malformed duration is refused where it was written, and where it was // written is one test — the ones after it still run. - it("fails only the declaring test on a malformed timeout", function* () { + documentCase("fails only the declaring test on a malformed timeout", function* (runFresh) { const doc = [ "", 'inside', @@ -443,7 +457,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results).toHaveLength(2); expect(run.results[0]?.status).toBe("fail"); @@ -453,7 +467,7 @@ describe(" as a function component", () => { // A teardown-failed test had already returned, so the engine replaces its // output with the boundary's diagnostic — no body text survives (§5.4). - it("renders only the boundary diagnostic when teardown fails", function* () { + documentCase("renders only the boundary diagnostic when teardown fails", function* (runFresh) { const doc = [ "", '', @@ -465,7 +479,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results[0]?.error?.kind).toBe("teardown"); expect(run.output).not.toContain("BODY TEXT"); @@ -476,22 +490,24 @@ describe(" as a function component", () => { // around it are the author's and stay, exactly as they would around any // element that renders nothing — so the comparison is against the element // removed, not against the lines it sat between removed too (§5.2). - it("renders nothing of itself when skipped", function* () { + const skippedCase = "renders nothing of itself when skipped"; + documentCase(skippedCase, function* (runFresh) { const withTest = ["before", "", 'inside', "", "after", ""].join( "\n", ); const withoutTest = ["before", "", "", "", "after", ""].join("\n"); - const skipped = yield* runDoc({ "README.md": withTest }, { testing: false }); - const plain = yield* runDoc({ "README.md": withoutTest }, { testing: false }); - - expect(skipped.output).not.toContain("inside"); - expect(skipped.output).toBe(plain.output); + const skipped = yield* runFresh({ "README.md": withTest }, { testing: false }); + yield* withFreshDocument(OWNER, skippedCase, function* (runPlain) { + const plain = yield* runPlain({ "README.md": withoutTest }, { testing: false }); + expect(skipped.output).not.toContain("inside"); + expect(skipped.output).toBe(plain.output); + }); }); // A nested is invalid, and what that means is the ENCLOSING test's // recorded outcome — not the shape of anything it returned. - it("fails the enclosing test on nesting, recorded as an error", function* () { + documentCase("fails the enclosing test on nesting, recorded as an error", function* (runFresh) { const doc = [ "", '', @@ -499,7 +515,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ ["outer", "fail", "error"], diff --git a/packages/testing/tests/testing-boundary.test.ts b/packages/testing/tests/testing-boundary.test.ts index 6fb97d617..e88029219 100644 --- a/packages/testing/tests/testing-boundary.test.ts +++ b/packages/testing/tests/testing-boundary.test.ts @@ -14,7 +14,9 @@ import { scoped } from "effection"; import type { Operation } from "effection"; import { InMemoryStream } from "@executablemd/durable-streams"; import { registerComponents, useTempFileCompiler } from "@executablemd/core"; -import { runDoc } from "./helpers.ts"; +import { documentCases, runDoc } from "./helpers.ts"; + +const documentCase = documentCases("packages/testing/tests/testing-boundary.test.ts"); import type { DocRun } from "./helpers.ts"; /** Every journalled boundary entry, by the name it recorded. */ @@ -125,23 +127,26 @@ describe("Tier TB — a boundary that never finished", () => { describe("Tier TB — a repository component replaces ", () => { beforeAll(() => useTempFileCompiler()); - it("TB4: a local Testing component is selected, and the package's never runs", function* () { - const run = yield* runDoc({ - "README.md": [ - "", - '', - "", - "", - ].join("\n"), - "components/Testing.md": "LOCAL TESTING BOUNDARY\n", - }); - - // The repository component answered… - expect(run.output).toContain("LOCAL TESTING BOUNDARY"); - // …so the package implementation never activated testing: the `` - // inside stayed inert, recorded no result, and reported no boundary. - expect(run.results).toEqual([]); - expect(run.boundaries).toEqual([]); - expect(run.output).not.toContain("would-run"); - }); + documentCase( + "TB4: a local Testing component is selected, and the package's never runs", + function* (runFresh) { + const run = yield* runFresh({ + "README.md": [ + "", + '', + "", + "", + ].join("\n"), + "components/Testing.md": "LOCAL TESTING BOUNDARY\n", + }); + + // The repository component answered… + expect(run.output).toContain("LOCAL TESTING BOUNDARY"); + // …so the package implementation never activated testing: the `` + // inside stayed inert, recorded no result, and reported no boundary. + expect(run.results).toEqual([]); + expect(run.boundaries).toEqual([]); + expect(run.output).not.toContain("would-run"); + }, + ); }); diff --git a/packages/testing/tests/testing-mode.test.ts b/packages/testing/tests/testing-mode.test.ts index ceafaf300..06e586f0e 100644 --- a/packages/testing/tests/testing-mode.test.ts +++ b/packages/testing/tests/testing-mode.test.ts @@ -7,11 +7,13 @@ import { API } from "@executablemd/runtime"; import { useFailingExec } from "@executablemd/runtime/test"; import { TestFailureError } from "../src/test-api.ts"; import { createTestHandlers } from "../src/handlers.ts"; -import { failureOf, runDoc } from "./helpers.ts"; +import { documentCases, failureOf, runDoc } from "./helpers.ts"; + +const documentCase = documentCases("packages/testing/tests/testing-mode.test.ts"); describe("testing mode", () => { beforeAll(() => useTempFileCompiler()); - it("skips entirely during regular execution", function* () { + documentCase("skips entirely during regular execution", function* (runFresh) { const execCalls: string[] = []; yield* API.Process.around({ // deno-lint-ignore require-yield @@ -31,7 +33,7 @@ describe("testing mode", () => { "after", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.output).toContain("before"); expect(run.output).toContain("after"); @@ -40,49 +42,49 @@ describe("testing mode", () => { expect(run.results).toEqual([]); }); - it("explicit runs its subtree during a regular run", function* () { + documentCase("explicit runs its subtree during a regular run", function* (runFresh) { const doc = '\n'; - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results.map((r) => [r.name, r.status])).toEqual([["t", "pass"]]); expect(run.boundaries).toEqual([{ tests: 1, failed: 0 }]); }); - it("a failing boundary rejects an ordinary run", function* () { + documentCase("a failing boundary rejects an ordinary run", function* (runFresh) { const doc = "\n"; - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(failureOf(run)).toBeInstanceOf(TestFailureError); expect(run.output).toContain("Test"); }); - it("an empty boundary rejects an ordinary run", function* () { - const run = yield* runDoc({ "README.md": "no tests here\n" }); + documentCase("an empty boundary rejects an ordinary run", function* (runFresh) { + const run = yield* runFresh({ "README.md": "no tests here\n" }); expect(failureOf(run)).toBeInstanceOf(TestFailureError); expect(run.boundaries).toEqual([{ tests: 0, failed: 0 }]); }); - it("root activation runs tests without ", function* () { + documentCase("root activation runs tests without ", function* (runFresh) { const doc = '\n'; - const run = yield* runDoc({ "README.md": doc }, { testing: true }); + const run = yield* runFresh({ "README.md": doc }, { testing: true }); expect(run.completion.ok).toBe(true); expect(run.results.map((r) => [r.name, r.status])).toEqual([["rooted", "pass"]]); }); - it("root activation with zero tests fails", function* () { - const run = yield* runDoc({ "README.md": "just text\n" }, { testing: true }); + documentCase("root activation with zero tests fails", function* (runFresh) { + const run = yield* runFresh({ "README.md": "just text\n" }, { testing: true }); const error = failureOf(run); expect(error).toBeInstanceOf(TestFailureError); expect(error?.message).toContain("no tests were discovered"); }); - it("nested delegates results outward", function* () { + documentCase("nested delegates results outward", function* (runFresh) { const doc = [ "", "", "", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.completion.ok).toBe(true); // Inner boundary reports first; the outer boundary sees the delegated // result, so neither boundary is empty. @@ -93,7 +95,7 @@ describe("testing mode", () => { expect(run.results).toHaveLength(1); }); - it("a failing exec block fails the test; later tests run", function* () { + documentCase("a failing exec block fails the test; later tests run", function* (runFresh) { yield* useFailingExec(3, "command exploded"); const doc = [ "", @@ -106,7 +108,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(failureOf(run)).toBeInstanceOf(TestFailureError); expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ ["broken", "fail", "error"], @@ -114,22 +116,25 @@ describe("testing mode", () => { ]); }); - it("an unresolvable component import fails the test; later tests run", function* () { - const doc = [ - "", - '', - '', - "", - "", - ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); - expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ - ["missing", "fail", "error"], - ["fine", "pass", undefined], - ]); - }); + documentCase( + "an unresolvable component import fails the test; later tests run", + function* (runFresh) { + const doc = [ + "", + '', + '', + "", + "", + ].join("\n"); + const run = yield* runFresh({ "README.md": doc }); + expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ + ["missing", "fail", "error"], + ["fine", "pass", undefined], + ]); + }, + ); - it("nested fails the enclosing test; later tests run", function* () { + documentCase("nested fails the enclosing test; later tests run", function* (runFresh) { const doc = [ "", '', @@ -137,7 +142,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ ["outer", "fail", "error"], ["fine", "pass", undefined], @@ -145,38 +150,44 @@ describe("testing mode", () => { expect(run.results[0]?.error?.message).toContain("Nested "); }); - it("an error raised inside a component-owned region fails the test", function* () { - const comp = "\nregion start\n\n\n"; - const doc = [ - "", - '', - '', - "", - "", - ].join("\n"); - const run = yield* runDoc({ "README.md": doc, "components/Regioned.md": comp }); - expect(run.results.map((r) => [r.name, r.status])).toEqual([ - ["output-region", "fail"], - ["fine", "pass"], - ]); - }); + documentCase( + "an error raised inside a component-owned region fails the test", + function* (runFresh) { + const comp = "\nregion start\n\n\n"; + const doc = [ + "", + '', + '', + "", + "", + ].join("\n"); + const run = yield* runFresh({ "README.md": doc, "components/Regioned.md": comp }); + expect(run.results.map((r) => [r.name, r.status])).toEqual([ + ["output-region", "fail"], + ["fine", "pass"], + ]); + }, + ); - it("a completed failing test inside a documentation region stays contained", function* () { - const comp = "\n\n\n"; - const doc = [ - "", - '', - "", - "", - ].join("\n"); - const run = yield* runDoc({ "README.md": doc, "components/Regioned.md": comp }); - // The document expands fully — the failure surfaces as a testing - // outcome, not an expansion abort. - expect(failureOf(run)).toBeInstanceOf(TestFailureError); - expect(run.results.map((r) => [r.name, r.status])).toEqual([["contained", "fail"]]); - }); + documentCase( + "a completed failing test inside a documentation region stays contained", + function* (runFresh) { + const comp = "\n\n\n"; + const doc = [ + "", + '', + "", + "", + ].join("\n"); + const run = yield* runFresh({ "README.md": doc, "components/Regioned.md": comp }); + // The document expands fully — the failure surfaces as a testing + // outcome, not an expansion abort. + expect(failureOf(run)).toBeInstanceOf(TestFailureError); + expect(run.results.map((r) => [r.name, r.status])).toEqual([["contained", "fail"]]); + }, + ); - it("bindings written in one test are invisible to the next", function* () { + documentCase("bindings written in one test are invisible to the next", function* (runFresh) { const doc = [ "```js eval", "const inherited = 7;", @@ -196,32 +207,35 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results.map((r) => r.status)).toEqual(["pass", "pass"]); }); - it("a projected through sees caller bindings in its eval blocks", function* () { - const doc = [ - "```js eval", - 'const callerValue = "from-caller";', - "```", - "", - '', - "```js eval", - 'const copied = callerValue + "!";', - "```", - '', - "", - "", - "", - ].join("\n"); - const run = yield* runDoc({ "README.md": doc, "components/Wrap.md": "\n" }); - expect(run.completion.ok).toBe(true); - expect(run.results.map((r) => [r.name, r.status])).toEqual([["projected", "pass"]]); - }); + documentCase( + "a projected through sees caller bindings in its eval blocks", + function* (runFresh) { + const doc = [ + "```js eval", + 'const callerValue = "from-caller";', + "```", + "", + '', + "```js eval", + 'const copied = callerValue + "!";', + "```", + '', + "", + "", + "", + ].join("\n"); + const run = yield* runFresh({ "README.md": doc, "components/Wrap.md": "\n" }); + expect(run.completion.ok).toBe(true); + expect(run.results.map((r) => [r.name, r.status])).toEqual([["projected", "pass"]]); + }, + ); - it("captures persist for the immediately following assertion", function* () { + documentCase("captures persist for the immediately following assertion", function* (runFresh) { const doc = [ "", 'Hello World', @@ -229,37 +243,40 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results.map((r) => r.status)).toEqual(["pass"]); }); - it("effects spawned in one test are torn down before the next begins", function* () { - Reflect.deleteProperty(globalThis, "__testingLeaseAlive"); - const doc = [ - "", - '', - "```js persist eval", - "globalThis.__testingLeaseAlive = true;", - "yield* spawn(function* () { try { yield* suspend(); } finally { globalThis.__testingLeaseAlive = false; } });", - "```", - "", - "", - '', - "", - "", - "", - "", - ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); - expect(run.completion.ok).toBe(true); - expect(run.results.map((r) => [r.name, r.status])).toEqual([ - ["spawner", "pass"], - ["observer", "pass"], - ]); - }); + documentCase( + "effects spawned in one test are torn down before the next begins", + function* (runFresh) { + Reflect.deleteProperty(globalThis, "__testingLeaseAlive"); + const doc = [ + "", + '', + "```js persist eval", + "globalThis.__testingLeaseAlive = true;", + "yield* spawn(function* () { try { yield* suspend(); } finally { globalThis.__testingLeaseAlive = false; } });", + "```", + "", + "", + '', + "", + "", + "", + "", + ].join("\n"); + const run = yield* runFresh({ "README.md": doc }); + expect(run.completion.ok).toBe(true); + expect(run.results.map((r) => [r.name, r.status])).toEqual([ + ["spawner", "pass"], + ["observer", "pass"], + ]); + }, + ); - it("output before a failure is kept, followed by the diagnostic", function* () { + documentCase("output before a failure is kept, followed by the diagnostic", function* (runFresh) { const doc = [ "", "first line", @@ -268,7 +285,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.output).toContain("first line"); expect(run.output).toContain("**Assert** failed"); expect(run.output).not.toContain("never rendered"); @@ -277,7 +294,7 @@ describe("testing mode", () => { expect(firstAt).toBeLessThan(diagnosticAt); }); - it("a hanging test times out, is torn down, and later tests run", function* () { + documentCase("a hanging test times out, is torn down, and later tests run", function* (runFresh) { yield* API.Process.around({ *exec(_args, _next) { yield* sleep(10_000); @@ -295,7 +312,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runDoc( + const run = yield* runFresh( { "README.md": doc }, { handlers: createTestHandlers({ timeoutMs: 100 }) }, ); @@ -306,7 +323,7 @@ describe("testing mode", () => { ]); }); - it("unnamed tests are identified by source location", function* () { + documentCase("unnamed tests are identified by source location", function* (runFresh) { const doc = [ "---", "title: Located", @@ -316,7 +333,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runDoc({ "README.md": doc }); + const run = yield* runFresh({ "README.md": doc }); expect(run.results[0]?.location).toBe("README.md:5:1"); expect(run.output).toContain("test at README.md:5:1"); }); @@ -328,7 +345,7 @@ describe("testing mode", () => { * exactly as it would be anywhere else a document did not authorize one * (#441). */ - it("a repository Test of the same name contains nothing", function* () { + documentCase("a repository Test of the same name contains nothing", function* (runFresh) { yield* useFailingExec(3, "command exploded"); const doc = [ "", @@ -341,7 +358,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runDoc({ + const run = yield* runFresh({ "README.md": doc, // Chosen ahead of the package's, and a different function object. "Test.md": "\n", diff --git a/packages/testing/tests/use-testing.test.ts b/packages/testing/tests/use-testing.test.ts index e10d38053..a2099053f 100644 --- a/packages/testing/tests/use-testing.test.ts +++ b/packages/testing/tests/use-testing.test.ts @@ -5,13 +5,22 @@ import { when } from "@effectionx/converge"; import { scoped, sleep, spawn } from "effection"; import type { Operation, Subscription } from "effection"; import { InMemoryStream } from "@executablemd/durable-streams"; +import { parseDurableEvent } from "@executablemd/durable-streams"; +import { exists, readdir, readTextFile } from "@effectionx/fs"; +import { useTempDirectory } from "@executablemd/test-support/temp"; +import { join } from "node:path"; import { useStubFs } from "@executablemd/runtime/test"; import { API } from "@executablemd/runtime"; import { execute, Execution, registerComponents } from "@executablemd/core"; import type { Json } from "@executablemd/core"; import { Test, TestFailureError, testing } from "../src/test-api.ts"; import { useTesting } from "../src/use-testing.ts"; -import { failureOf, runDoc } from "./helpers.ts"; +import { documentCases, failureOf, runDoc, withFreshDocument } from "./helpers.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; + +const OWNER = "packages/testing/tests/use-testing.test.ts"; +const documentCase = documentCases(OWNER); +const journalCase = journalCases(OWNER); function* drain( subscription: Subscription, @@ -27,29 +36,87 @@ function* drain( describe("useTesting composition", () => { beforeAll(() => useTempFileCompiler()); - it("composes around core execute and returns session results", function* () { - const outcome = yield* scoped(function* () { - yield* useStubFs({ - "README.md": '\n', + + const journalWitness = "attributes a fresh document when its caller fails after execution"; + it(journalWitness, function* () { + const root = yield* useTempDirectory("xmd-testing-fresh-document-"); + const failure = new Error("outer assertion"); + function attempt(directory: string, check: () => void): Operation { + return scoped(function* () { + yield* API.Env.around({ + *env([variable], next) { + return variable === "XMD_TEST_ARTIFACTS" ? directory : yield* next(variable); + }, + }); + yield* withFreshDocument(OWNER, journalWitness, function* (runFresh) { + const result = yield* runFresh( + { "README.md": "\n" }, + { testing: true }, + ); + expect(result.completion.ok).toBe(false); + check(); + }); }); - const tests = yield* useTesting(); - const execution = yield* execute({ path: "README.md", stream: new InMemoryStream() }); - const result = yield* execution; - const results = yield* tests.results; - return { result, results }; + } + + const passed = join(root, "passed"); + yield* attempt(passed, () => undefined); + expect(yield* exists(passed)).toBe(false); + + const failed = join(root, "failed"); + let caught: unknown; + try { + yield* attempt(failed, () => { + throw failure; + }); + } catch (error) { + caught = error; + } + expect(caught).toBe(failure); + const invocations = yield* readdir(failed); + expect(invocations).toHaveLength(1); + const directory = join(failed, invocations[0]); + expect(JSON.parse(yield* readTextFile(join(directory, "root-1.case.json")))).toEqual({ + owner: OWNER, + name: journalWitness, + kind: "root", + ordinal: 1, + source: "README.md", + }); + const lines = (yield* readTextFile(join(directory, "root-1.jsonl"))).trimEnd().split("\n"); + expect(parseDurableEvent(lines.at(-1) ?? "")).toMatchObject({ + ok: true, + value: { type: "close", coroutineId: "root" }, }); - expect(outcome.result.ok).toBe(true); - expect(outcome.results.map((r) => [r.name, r.status])).toEqual([["one", "pass"]]); - expect(Object.isFrozen(outcome.results)).toBe(true); }); - it("keeps results available after a testing failure", function* () { + journalCase( + "composes around core execute and returns session results", + "README.md", + function* (stream) { + const outcome = yield* scoped(function* () { + yield* useStubFs({ + "README.md": '\n', + }); + const tests = yield* useTesting(); + const execution = yield* execute({ path: "README.md", stream }); + const result = yield* execution; + const results = yield* tests.results; + return { result, results }; + }); + expect(outcome.result.ok).toBe(true); + expect(outcome.results.map((r) => [r.name, r.status])).toEqual([["one", "pass"]]); + expect(Object.isFrozen(outcome.results)).toBe(true); + }, + ); + + journalCase("keeps results available after a testing failure", "README.md", function* (stream) { const outcome = yield* scoped(function* () { yield* useStubFs({ "README.md": '\n', }); const tests = yield* useTesting(); - const execution = yield* execute({ path: "README.md", stream: new InMemoryStream() }); + const execution = yield* execute({ path: "README.md", stream }); const result = yield* execution; const results = yield* tests.results; return { result, results }; @@ -112,7 +179,7 @@ describe("useTesting composition", () => { expect(error?.message).toContain("one session per execution scope"); }); - it("removes all session middleware with its scope", function* () { + documentCase("removes all session middleware with its scope", function* (runFresh) { yield* scoped(function* () { yield* useStubFs({ "README.md": "\n" }); yield* useTesting(); @@ -124,7 +191,7 @@ describe("useTesting composition", () => { // Outside the session scope: activation, collectors, and policy are gone. expect(yield* testing).toBe(false); expect(yield* Test.operations.results()).toEqual([]); - const second = yield* runDoc({ "README.md": "\n" }); + const second = yield* runFresh({ "README.md": "\n" }); expect(second.completion.ok).toBe(true); expect(second.output).not.toContain("Assert"); expect(second.results).toEqual([]); @@ -204,69 +271,77 @@ describe("useTesting composition", () => { expect(thrown?.message).toBe("pre-handle setup exploded"); }); - it("a late subscriber still receives all chunks and the close value", function* () { - const { late, value } = yield* scoped(function* () { - yield* useStubFs({ "README.md": "hello world\n" }); - yield* useTesting(); - const execution = yield* execute({ - path: "README.md", - stream: new InMemoryStream(), - }); - const result = yield* execution; - const drained = yield* scoped(function* () { - return yield* drain(yield* execution.output); + journalCase( + "a late subscriber still receives all chunks and the close value", + "README.md", + function* (stream) { + const { late, value } = yield* scoped(function* () { + yield* useStubFs({ "README.md": "hello world\n" }); + yield* useTesting(); + const execution = yield* execute({ + path: "README.md", + stream, + }); + const result = yield* execution; + const drained = yield* scoped(function* () { + return yield* drain(yield* execution.output); + }); + return { late: drained, value: result }; }); - return { late: drained, value: result }; - }); - // No tests in the doc — the session rejects, but output is intact. - expect(value.ok).toBe(false); - expect(late.close).toContain("hello world"); - expect(late.chunks.join("")).toContain("hello world"); - }); + // No tests in the doc — the session rejects, but output is intact. + expect(value.ok).toBe(false); + expect(late.close).toContain("hello world"); + expect(late.chunks.join("")).toContain("hello world"); + }, + ); - it("an early caller-scope halt tears down the document and leased eval scope", function* () { - Reflect.deleteProperty(globalThis, "__useTestingHaltMarker"); - yield* API.Process.around({ - *exec(_args, _next) { - yield* sleep(10_000); - return { exitCode: 0, stdout: "", stderr: "" }; - }, - }); - const doc = [ - "", - '', - "```js persist eval", - "globalThis.__useTestingHaltMarker = true;", - "yield* spawn(function* () { try { yield* suspend(); } finally { globalThis.__useTestingHaltMarker = false; } });", - "```", - "```bash exec", - "sleep 600", - "```", - "", - "", - "", - ].join("\n"); - yield* useStubFs({ "README.md": doc }); + journalCase( + "an early caller-scope halt tears down the document and leased eval scope", + "README.md", + function* (stream) { + Reflect.deleteProperty(globalThis, "__useTestingHaltMarker"); + yield* API.Process.around({ + *exec(_args, _next) { + yield* sleep(10_000); + return { exitCode: 0, stdout: "", stderr: "" }; + }, + }); + const doc = [ + "", + '', + "```js persist eval", + "globalThis.__useTestingHaltMarker = true;", + "yield* spawn(function* () { try { yield* suspend(); } finally { globalThis.__useTestingHaltMarker = false; } });", + "```", + "```bash exec", + "sleep 600", + "```", + "", + "", + "", + ].join("\n"); + yield* useStubFs({ "README.md": doc }); - yield* scoped(function* () { - yield* useTesting(); - const execution = yield* execute({ path: "README.md", stream: new InMemoryStream() }); - yield* spawn(function* () { - yield* scoped(function* () { - yield* drain(yield* execution.output); + yield* scoped(function* () { + yield* useTesting(); + const execution = yield* execute({ path: "README.md", stream }); + yield* spawn(function* () { + yield* scoped(function* () { + yield* drain(yield* execution.output); + }); }); + // Converge on the document having run the eval block and spawned its + // effect, then leave the scope — halting everything mid-test. A fixed + // sleep races variable startup cost (#335). + yield* when( + function* () { + return Reflect.get(globalThis, "__useTestingHaltMarker") === true; + }, + { timeout: 10_000 }, + ); }); - // Converge on the document having run the eval block and spawned its - // effect, then leave the scope — halting everything mid-test. A fixed - // sleep races variable startup cost (#335). - yield* when( - function* () { - return Reflect.get(globalThis, "__useTestingHaltMarker") === true; - }, - { timeout: 10_000 }, - ); - }); - expect(Reflect.get(globalThis, "__useTestingHaltMarker")).toBe(false); - }); + expect(Reflect.get(globalThis, "__useTestingHaltMarker")).toBe(false); + }, + ); }); diff --git a/packages/web/tests/component-support.ts b/packages/web/tests/component-support.ts index 22b645ad5..2e3a5a59c 100644 --- a/packages/web/tests/component-support.ts +++ b/packages/web/tests/component-support.ts @@ -19,6 +19,8 @@ import { mkdtemp, realpath } from "node:fs/promises"; import process from "node:process"; import { basename, join } from "node:path"; import { rm, writeTextFile } from "@effectionx/fs"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import type { JournalCase } from "../../cli/src/test-diagnostics.ts"; import { FormAssets } from "../src/assets.ts"; import type { Json } from "../src/json.ts"; @@ -76,6 +78,20 @@ export interface DocRun { stream: DurableStream; } +export function withWebFormDocument( + testCase: JournalCase, + source: string, + options: Omit, + check: (run: DocRun) => void | Operation, +): Operation { + return captureTestJournal(testCase, options.realFiles ? source : "README.md", function* (stream) { + const assertions = check(yield* runWebFormDoc(source, { ...options, stream })); + if (assertions !== undefined) { + yield* assertions; + } + }); +} + /** * Run one document with `` registered and every seam observed. * diff --git a/packages/web/tests/component.test.ts b/packages/web/tests/component.test.ts index 0926e4c73..2c1d3f170 100644 --- a/packages/web/tests/component.test.ts +++ b/packages/web/tests/component.test.ts @@ -4,6 +4,10 @@ import { expect } from "@executablemd/test-support/expect"; import { InMemoryStream } from "@executablemd/durable-streams"; import type { Operation } from "effection"; import { join } from "node:path"; +import { scoped } from "effection"; +import { exists, readdir, readTextFile } from "@effectionx/fs"; +import { API } from "@executablemd/runtime"; +import { useTempDirectory } from "@executablemd/test-support/temp"; import { journaled, @@ -12,8 +16,26 @@ import { REVIEW_SCHEMA, runWebFormDoc, useLocalFixture, + withWebFormDocument, writeFile, } from "./component-support.ts"; +import type { DocRun, RunOptions } from "./component-support.ts"; + +type RunFreshForm = ( + source: string, + options: Omit, + check: (run: DocRun) => void | Operation, +) => Operation; + +const OWNER = "packages/web/tests/component.test.ts"; + +function formCase(name: string, body: (run: RunFreshForm) => Operation): void { + it(name, () => + body((source, options, check) => + withWebFormDocument({ owner: OWNER, name }, source, options, check), + ), + ); +} const SCHEMA_BLOCK = [ "```js eval", @@ -26,8 +48,61 @@ function document(body: string, props = "schema={reviewSchema}"): string { } describe("WebForm: what a document gets", () => { - it("binds the validated response and renders nothing", function* () { - const run = yield* runWebFormDoc( + const artifactCase = "attributes a failed assertion to the settled live form"; + it(artifactCase, function* () { + const root = yield* useTempDirectory("xmd-webform-artifacts-"); + const failure = new Error("post-response assertion"); + function attempt(artifacts: string, assertion: () => void): Operation { + return scoped(function* () { + yield* API.Env.around({ + *env([variable], next) { + return variable === "XMD_TEST_ARTIFACTS" ? artifacts : yield* next(variable); + }, + }); + yield* withWebFormDocument( + { owner: OWNER, name: artifactCase }, + document("Decide."), + { answer: { decision: "approve" } }, + (run) => { + expect(run.completion.ok).toBe(true); + expect(run.effects.served).toBe(1); + assertion(); + }, + ); + }); + } + + const passed = join(root, "passed"); + yield* attempt(passed, () => undefined); + expect(yield* exists(passed)).toBe(false); + + const failed = join(root, "failed"); + let caught: unknown; + try { + yield* attempt(failed, () => { + throw failure; + }); + } catch (error) { + caught = error; + } + expect(caught).toBe(failure); + const invocations = yield* readdir(failed); + expect(invocations).toHaveLength(1); + const directory = join(failed, invocations[0]); + expect(JSON.parse(yield* readTextFile(join(directory, "root-1.case.json")))).toEqual({ + owner: OWNER, + name: artifactCase, + kind: "root", + ordinal: 1, + source: "README.md", + }); + const records = yield* readTextFile(join(directory, "root-1.jsonl")); + expect(records).toContain('"type":"web_form"'); + expect(records).not.toContain("127.0.0.1"); + }); + + formCase("binds the validated response and renders nothing", function* (runFresh) { + yield* runFresh( [ document("# Review required\n\nDecide."), "```js eval", @@ -35,38 +110,45 @@ describe("WebForm: what a document gets", () => { "```", ].join("\n"), { answer: { decision: "approve", note: "fine" } }, + function (run) { + expect(run.completion.ok).toBe(true); + // The captured value, and no trace of the form around it. + expect(run.output).toContain('"decision":"approve"'); + expect(run.output).toContain('"note":"fine"'); + expect(run.output.includes("127.0.0.1")).toBe(false); + expect(run.output.includes("Review required")).toBe(false); + }, ); - - expect(run.completion.ok).toBe(true); - // The captured value, and no trace of the form around it. - expect(run.output).toContain('"decision":"approve"'); - expect(run.output).toContain('"note":"fine"'); - expect(run.output.includes("127.0.0.1")).toBe(false); - expect(run.output.includes("Review required")).toBe(false); }); - it("is answered through its own live server", function* () { - const run = yield* runWebFormDoc(document("Decide."), { - answer: { decision: "reject" }, - }); - - expect(run.completion.ok).toBe(true); - expect(run.effects.served).toBe(1); - expect(run.effects.opened.length).toBe(1); - expect(run.effects.responded.length).toBe(1); - // Opener and responder were handed the same live URL. - expect(run.effects.opened[0]).toBe(run.effects.responded[0]); - expect(run.effects.opened[0]).toMatch(/^http:\/\/127\.0\.0\.1:\d+\/f\/[A-Za-z0-9_-]{43}\/$/); + formCase("is answered through its own live server", function* (runFresh) { + yield* runFresh( + document("Decide."), + { + answer: { decision: "reject" }, + }, + function (run) { + expect(run.completion.ok).toBe(true); + expect(run.effects.served).toBe(1); + expect(run.effects.opened.length).toBe(1); + expect(run.effects.responded.length).toBe(1); + // Opener and responder were handed the same live URL. + expect(run.effects.opened[0]).toBe(run.effects.responded[0]); + expect(run.effects.opened[0]).toMatch( + /^http:\/\/127\.0\.0\.1:\d+\/f\/[A-Za-z0-9_-]{43}\/$/, + ); + }, + ); }); - it("prints the URL a person can use", function* () { - const run = yield* runWebFormDoc(document("Decide."), { answer: { decision: "approve" } }); - - const url = run.effects.opened[0]; - expect(run.effects.printed.some((line) => line.includes(url))).toBe(true); + formCase("prints the URL a person can use", function* (runFresh) { + yield* runFresh(document("Decide."), { answer: { decision: "approve" } }, function (run) { + const url = run.effects.opened[0]; + expect(run.effects.printed.some((line) => line.includes(url))).toBe(true); + }); }); - it("resolves an expression-valued uiSchema through core", function* () { + formCase("resolves an expression-valued uiSchema through core", function* (runFresh) { const source = [ "```js eval", `const reviewSchema = ${JSON.stringify(REVIEW_SCHEMA)};`, @@ -82,10 +164,10 @@ describe("WebForm: what a document gets", () => { "```", ].join("\n"); - const run = yield* runWebFormDoc(source, { answer: { decision: "approve" } }); - - expect(run.completion.ok).toBe(true); - expect(run.output).toContain("approve"); + yield* runFresh(source, { answer: { decision: "approve" } }, function (run) { + expect(run.completion.ok).toBe(true); + expect(run.output).toContain("approve"); + }); }); }); @@ -102,7 +184,7 @@ describe("WebForm: nothing happens before the question is ready", () => { * this asserts is the consequence — the diagnostic reached the document and * nothing was served. */ - it("causes no live effect when its content fails", function* () { + formCase("causes no live effect when its content fails", function* (runFresh) { const source = [ SCHEMA_BLOCK, "", @@ -113,22 +195,22 @@ describe("WebForm: nothing happens before the question is ready", () => { "", ].join("\n"); - const run = yield* runWebFormDoc(source, { answer: { decision: "approve" } }); - - // Nothing recovers the body's failure, so the run reports it. - expect(run.completion.ok).toBe(false); - expect(run.completion.ok ? "" : run.completion.error.message).toContain("the body failed"); - expect(run.effects.served).toBe(0); - expect(run.effects.opened).toEqual([]); - expect(run.effects.responded).toEqual([]); - expect(run.effects.printed).toEqual([]); - // No entry at all, not merely no successful one: a durable operation that - // began and failed would leave an `err` event, and that is still work done - // for a question nobody was ever asked. - expect(yield* journalEvents(run.stream, "web_form")).toEqual([]); + yield* runFresh(source, { answer: { decision: "approve" } }, function* (run) { + // Nothing recovers the body's failure, so the run reports it. + expect(run.completion.ok).toBe(false); + expect(run.completion.ok ? "" : run.completion.error.message).toContain("the body failed"); + expect(run.effects.served).toBe(0); + expect(run.effects.opened).toEqual([]); + expect(run.effects.responded).toEqual([]); + expect(run.effects.printed).toEqual([]); + // No entry at all, not merely no successful one: a durable operation that + // began and failed would leave an `err` event, and that is still work done + // for a question nobody was ever asked. + expect(yield* journalEvents(run.stream, "web_form")).toEqual([]); + }); }); - it("causes no live effect when the schema cannot be used", function* () { + formCase("causes no live effect when the schema cannot be used", function* (runFresh) { const unusable: [string, string][] = [ ["not JSON", 'x'], ["not an object", 'x'], @@ -148,33 +230,34 @@ describe("WebForm: nothing happens before the question is ready", () => { ]; for (const [label, source] of unusable) { - const run = yield* runWebFormDoc(source, { answer: { decision: "approve" } }); - - expect({ label, ok: run.completion.ok }).toEqual({ label, ok: false }); - expect({ label, served: run.effects.served }).toEqual({ label, served: 0 }); - expect({ label, opened: run.effects.opened }).toEqual({ label, opened: [] }); - expect({ label, responded: run.effects.responded }).toEqual({ label, responded: [] }); - expect({ label, printed: run.effects.printed }).toEqual({ label, printed: [] }); - // Every durable yield, not the successful ones: an operation that began and - // failed leaves an `err` event, and that is still work done for a question - // nobody was ever asked. - expect({ label, events: yield* journalEvents(run.stream, "web_form") }).toEqual({ - label, - events: [], + yield* runFresh(source, { answer: { decision: "approve" } }, function* (run) { + expect({ label, ok: run.completion.ok }).toEqual({ label, ok: false }); + expect({ label, served: run.effects.served }).toEqual({ label, served: 0 }); + expect({ label, opened: run.effects.opened }).toEqual({ label, opened: [] }); + expect({ label, responded: run.effects.responded }).toEqual({ label, responded: [] }); + expect({ label, printed: run.effects.printed }).toEqual({ label, printed: [] }); + // Every durable yield, not the successful ones: an operation that began and + // failed leaves an `err` event, and that is still work done for a question + // nobody was ever asked. + expect({ label, events: yield* journalEvents(run.stream, "web_form") }).toEqual({ + label, + events: [], + }); }); } }); /** Unmarked, so #251's default applies: the failure is the document's. */ - it("fails the document rather than printing", function* () { - const run = yield* runWebFormDoc( + formCase("fails the document rather than printing", function* (runFresh) { + yield* runFresh( 'Decide.\n\nafter\n', { answer: { decision: "approve" } }, + function (run) { + expect(run.completion.ok).toBe(false); + // Nothing after it ran, which a printed error would have allowed. + expect(run.output.includes("after")).toBe(false); + }, ); - - expect(run.completion.ok).toBe(false); - // Nothing after it ran, which a printed error would have allowed. - expect(run.output.includes("after")).toBe(false); }); }); @@ -309,20 +392,20 @@ describe("WebForm: a repository file outranks the registration", () => { * Real files rather than a stubbed filesystem: a `.ts` component is `import()`ed * from a real path, so a stub can resolve its name and then fail to load it. */ - it("prefers a repository WebForm.md", function* () { + formCase("prefers a repository WebForm.md", function* (runFresh) { const dir = yield* useLocalFixture(); yield* writeFile(join(dir, "WebForm.md"), "the repository's own form\n"); const doc = join(dir, "README.md"); yield* writeFile(doc, "\n"); - const run = yield* runWebFormDoc(doc, { realFiles: true, includes: [dir] }); - - expect(run.output).toContain("the repository's own form"); - // The registered default never ran, so nothing was served. - expect(run.effects.served).toBe(0); + yield* runFresh(doc, { realFiles: true, includes: [dir] }, function (run) { + expect(run.output).toContain("the repository's own form"); + // The registered default never ran, so nothing was served. + expect(run.effects.served).toBe(0); + }); }); - it("prefers a repository WebForm.ts", function* () { + formCase("prefers a repository WebForm.ts", function* (runFresh) { yield* useComponentModuleLoader(); const dir = yield* useLocalFixture(); yield* writeFile( @@ -334,9 +417,9 @@ describe("WebForm: a repository file outranks the registration", () => { const doc = join(dir, "README.md"); yield* writeFile(doc, "\n"); - const run = yield* runWebFormDoc(doc, { realFiles: true, includes: [dir] }); - - expect(run.output).toContain("the repository's own function"); - expect(run.effects.served).toBe(0); + yield* runFresh(doc, { realFiles: true, includes: [dir] }, function (run) { + expect(run.output).toContain("the repository's own function"); + expect(run.effects.served).toBe(0); + }); }); }); diff --git a/packages/web/tests/document.test.ts b/packages/web/tests/document.test.ts index 6344c3d4e..501d6e185 100644 --- a/packages/web/tests/document.test.ts +++ b/packages/web/tests/document.test.ts @@ -3,7 +3,7 @@ import { expect } from "@executablemd/test-support/expect"; import { scoped } from "effection"; import { readTextFile } from "@effectionx/fs"; import { collect, execute, registerComponents, useTempFileCompiler } from "@executablemd/core"; -import { InMemoryStream } from "@executablemd/durable-streams"; +import { captureTestJournal } from "../../test-support/failure-journal.ts"; import { useTesting } from "@executablemd/testing"; import { fileURLToPath } from "node:url"; @@ -24,53 +24,60 @@ const DOCUMENT = fileURLToPath(new URL("../src/WebForm.test.md", import.meta.url * is not coming. */ describe("WebForm.test.md", () => { - it("passes with an installed responder answering its forms", function* () { - const source = yield* readTextFile(DOCUMENT); + const name = "passes with an installed responder answering its forms"; + it(name, () => + captureTestJournal( + { owner: "packages/web/tests/document.test.ts", name }, + "WebForm.test.md", + function* (stream) { + const source = yield* readTextFile(DOCUMENT); - const outcome = yield* scoped(function* () { - yield* useTempFileCompiler(); - yield* useStubFs({ "WebForm.test.md": source }); + const outcome = yield* scoped(function* () { + yield* useTempFileCompiler(); + yield* useStubFs({ "WebForm.test.md": source }); - yield* FormAssets.around({ - // deno-lint-ignore require-yield - *assets() { - return { clientJs: "/* fixture */", themeCss: "/* fixture */" }; - }, - }); - yield* FormOpener.around({ - // deno-lint-ignore require-yield - *open() {}, - }); - yield* FormResponder.around({ - *respond([url]) { - yield* submitForm(url, { decision: "approve", note: "looks right" }); - }, - }); + yield* FormAssets.around({ + // deno-lint-ignore require-yield + *assets() { + return { clientJs: "/* fixture */", themeCss: "/* fixture */" }; + }, + }); + yield* FormOpener.around({ + // deno-lint-ignore require-yield + *open() {}, + }); + yield* FormResponder.around({ + *respond([url]) { + yield* submitForm(url, { decision: "approve", note: "looks right" }); + }, + }); - yield* useTesting(); - yield* registerComponents([ - { - name: "WebForm", - origin: "@executablemd/web", - fn: WebForm, - props: WEB_FORM_PROPS, - returns: WEB_FORM_RETURNS, - }, - ]); + yield* useTesting(); + yield* registerComponents([ + { + name: "WebForm", + origin: "@executablemd/web", + fn: WebForm, + props: WEB_FORM_PROPS, + returns: WEB_FORM_RETURNS, + }, + ]); - const execution = yield* execute({ - path: "WebForm.test.md", - stream: new InMemoryStream(), - }); + const execution = yield* execute({ + path: "WebForm.test.md", + stream, + }); - try { - yield* collect(execution); - return { ok: true, message: "" }; - } catch (error) { - return { ok: false, message: error instanceof Error ? error.message : String(error) }; - } - }); + try { + yield* collect(execution); + return { ok: true, message: "" }; + } catch (error) { + return { ok: false, message: error instanceof Error ? error.message : String(error) }; + } + }); - expect(outcome).toEqual({ ok: true, message: "" }); - }); + expect(outcome).toEqual({ ok: true, message: "" }); + }, + ), + ); }); diff --git a/packages/workflow/tests/generated-observations.test.ts b/packages/workflow/tests/generated-observations.test.ts index 314714347..2e442e02e 100644 --- a/packages/workflow/tests/generated-observations.test.ts +++ b/packages/workflow/tests/generated-observations.test.ts @@ -33,8 +33,10 @@ import { import type { GeneratedEvaluationOptions } from "../src/deno/workspace/evaluate.ts"; import { recordedFiles, useRecordedFiles } from "../../core/tests/support/fragment-files.ts"; import type { RecordedFiles } from "../../core/tests/support/fragment-files.ts"; +import { journalCases } from "../../test-support/journal-cases.ts"; const ROOT_PATH = "workflows/agent.md"; +const journalCase = journalCases("packages/workflow/tests/generated-observations.test.ts"); const URL_ONE = "https://api.example.test/one"; const URL_TWO = "https://api.example.test/two"; const NOTE = "the retained note\n"; @@ -99,10 +101,11 @@ function run( events?: DurableEvent[]; files?: RecordedFiles; } = {}, + stream?: InMemoryStream, ): Operation { return scoped(function* () { yield* useWorkflowGenerated(options.generated ?? {}); - return yield* once(source, options); + return yield* once(source, options, stream); }); } @@ -117,9 +120,9 @@ function run( function once( source: string, options: { events?: DurableEvent[]; files?: RecordedFiles } = {}, + stream = new InMemoryStream(options.events ?? []), ): Operation { return scoped(function* () { - const stream = new InMemoryStream(options.events ?? []); if (options.files !== undefined) { yield* useRecordedFiles(options.files); } @@ -265,114 +268,145 @@ describe("Tier WGX — the selection a workflow run offers", () => { yield* useScope(); }); - it("WGX2: a marker-4 admission holds the selection and nothing live", function* () { - const files = recordedFiles({ "notes.md": NOTE }); - - const attempt = yield* run(evaluating(`\n`), { files }); - - expect(attempt.failure).toBe(undefined); - expect(attempt.output).toContain("the retained note"); - const policy = policyOf(admissions(attempt.events)[0]); - // The closed shape this build writes: the marker, the selected classes and - // the rows. No Workspace basis and no request inventory — what a run stands - // on and what a request may reach are both live state, and a grant compared - // against a copy of either would refuse a run for its own progress. - expect(Object.keys(policy ?? {}).sort()).toEqual(["allow", "allowed", "version"]); - expect(policy?.version).toBe(4); - expect(policy?.allow).toEqual(["read"]); - expect(policy?.allowed).toEqual([ - { - name: "Json", - form: "self-closing", - identity: "https://executable.md/components/json", - kind: "capability", - effect: "composition", - }, - { - name: "File", - form: "self-closing", - identity: "https://executable.md/components/file/read", - kind: "component-answer", - effect: "read", - }, - ]); - }); - - it("WGX3: a fragment that asks for nothing is admitted and retains its source", function* () { - const attempt = yield* run(evaluating("nothing to observe here.\n")); - - expect(attempt.failure).toBe(undefined); - expect(attempt.output).toContain("nothing to observe here."); - expect(admissions(attempt.events)).toHaveLength(1); - expect(reads(attempt.events)).toHaveLength(0); - }); + journalCase( + "WGX2: a marker-4 admission holds the selection and nothing live", + ROOT_PATH, + function* (stream) { + const files = recordedFiles({ "notes.md": NOTE }); + + const attempt = yield* run(evaluating(`\n`), { files }, stream); + + expect(attempt.failure).toBe(undefined); + expect(attempt.output).toContain("the retained note"); + const policy = policyOf(admissions(attempt.events)[0]); + // The closed shape this build writes: the marker, the selected classes and + // the rows. No Workspace basis and no request inventory — what a run stands + // on and what a request may reach are both live state, and a grant compared + // against a copy of either would refuse a run for its own progress. + expect(Object.keys(policy ?? {}).sort()).toEqual(["allow", "allowed", "version"]); + expect(policy?.version).toBe(4); + expect(policy?.allow).toEqual(["read"]); + expect(policy?.allowed).toEqual([ + { + name: "Json", + form: "self-closing", + identity: "https://executable.md/components/json", + kind: "capability", + effect: "composition", + }, + { + name: "File", + form: "self-closing", + identity: "https://executable.md/components/file/read", + kind: "component-answer", + effect: "read", + }, + ]); + }, + ); - it("WGX4: a document with no `` makes no admission at all", function* () { - // The untaken half of the previous row: offering a selection is not - // evaluating anything, so a run that never writes the element appends no - // admission and resolves nothing on its behalf. - const attempt = yield* run("The run wrote no fragment.\n"); + journalCase( + "WGX3: a fragment that asks for nothing is admitted and retains its source", + ROOT_PATH, + function* (stream) { + const attempt = yield* run(evaluating("nothing to observe here.\n"), {}, stream); - expect(attempt.failure).toBe(undefined); - expect(attempt.output).toContain("The run wrote no fragment."); - expect(admissions(attempt.events)).toHaveLength(0); - }); + expect(attempt.failure).toBe(undefined); + expect(attempt.output).toContain("nothing to observe here."); + expect(admissions(attempt.events)).toHaveLength(1); + expect(reads(attempt.events)).toHaveLength(0); + }, + ); - it("WGX5: a name this host did not offer is refused before any effect", function* () { - const files = recordedFiles({ "notes.md": NOTE }); + journalCase( + "WGX4: a document with no `` makes no admission at all", + ROOT_PATH, + function* (stream) { + // The untaken half of the previous row: offering a selection is not + // evaluating anything, so a run that never writes the element appends no + // admission and resolves nothing on its behalf. + const attempt = yield* run("The run wrote no fragment.\n", {}, stream); + + expect(attempt.failure).toBe(undefined); + expect(attempt.output).toContain("The run wrote no fragment."); + expect(admissions(attempt.events)).toHaveLength(0); + }, + ); - // `` is a core default this host did not choose. A generic workflow - // run therefore has no search at all, and the refusal lands before the read - // beside it in the same fragment. - const attempt = yield* run( - evaluating(`\n\n`), - { files }, - ); + journalCase( + "WGX5: a name this host did not offer is refused before any effect", + ROOT_PATH, + function* (stream) { + const files = recordedFiles({ "notes.md": NOTE }); + + // `` is a core default this host did not choose. A generic workflow + // run therefore has no search at all, and the refusal lands before the read + // beside it in the same fragment. + const attempt = yield* run( + evaluating(`\n\n`), + { files }, + stream, + ); - expect(attempt.failure).toContain("did not admit"); - expect(files.performed).toEqual([]); - expect(admissions(attempt.events).length).toBeLessThanOrEqual(1); - expect(reads(attempt.events)).toHaveLength(0); - }); + expect(attempt.failure).toContain("did not admit"); + expect(files.performed).toEqual([]); + expect(admissions(attempt.events).length).toBeLessThanOrEqual(1); + expect(reads(attempt.events)).toHaveLength(0); + }, + ); }); describe("Tier WGX — what an admitted request may reach is live", () => { - it("WGX6: a host that bounds no request offers `` not at all", function* () { - const transport = yield* useTransport(); + journalCase( + "WGX6: a host that bounds no request offers `` not at all", + ROOT_PATH, + function* (stream) { + const transport = yield* useTransport(); - const attempt = yield* run(evaluating(`\n`)); + const attempt = yield* run(evaluating(`\n`), {}, stream); - expect(attempt.failure).toContain("did not admit"); - expect(transport.performed).toHaveLength(0); - }); + expect(attempt.failure).toContain("did not admit"); + expect(transport.performed).toHaveLength(0); + }, + ); - it("WGX7: a bounded destination performs once and retains one observation", function* () { - const transport = yield* useTransport(); + journalCase( + "WGX7: a bounded destination performs once and retains one observation", + ROOT_PATH, + function* (stream) { + const transport = yield* useTransport(); - const attempt = yield* run( - evaluating(`\n\n`), - { generated: { requests: [{ url: URL_ONE }] } }, - ); - - expect(attempt.failure).toBe(undefined); - expect(transport.performed.map((call) => call.url)).toEqual([URL_ONE]); - expect(fetches(attempt.events)).toHaveLength(1); - }); + const attempt = yield* run( + evaluating(`\n\n`), + { generated: { requests: [{ url: URL_ONE }] } }, + stream, + ); - it("WGX8: a destination outside the bound reaches no transport", function* () { - const transport = yield* useTransport(); + expect(attempt.failure).toBe(undefined); + expect(transport.performed.map((call) => call.url)).toEqual([URL_ONE]); + expect(fetches(attempt.events)).toHaveLength(1); + }, + ); - // The name is offered — this host bounds a request — and the destination is - // not. So the refusal is the live terminal's, asked immediately before - // transport, and nothing is sent. - const attempt = yield* run( - evaluating(`\n\n`), - { generated: { requests: [{ url: URL_ONE }] } }, - ); + journalCase( + "WGX8: a destination outside the bound reaches no transport", + ROOT_PATH, + function* (stream) { + const transport = yield* useTransport(); + + // The name is offered — this host bounds a request — and the destination is + // not. So the refusal is the live terminal's, asked immediately before + // transport, and nothing is sent. + const attempt = yield* run( + evaluating(`\n\n`), + { generated: { requests: [{ url: URL_ONE }] } }, + stream, + ); - expect(attempt.failure).toContain("not permitted in this scope"); - expect(transport.performed).toHaveLength(0); - }); + expect(attempt.failure).toContain("not permitted in this scope"); + expect(transport.performed).toHaveLength(0); + }, + ); it("WGX9: a bound this host stated cannot be moved by editing what it was given", function* () { const transport = yield* useTransport(); @@ -474,55 +508,70 @@ describe("Tier WGX — what an admitted request may reach is live", () => { expect(transport.performed).toHaveLength(0); }); - it("WGX15: the request allowance is read once, and `` comes from that reading", function* () { - const server = yield* useLoopback(); - let reads = 0; - // An ordinary accessor-backed options object, which is all a host has to be - // for this to matter: the first reading states no bound and a later one - // states one. Whichever this host meant, the name a fragment may write and - // the policy deciding what it may reach have to come from one reading. - const changing: GeneratedEvaluationOptions = { - get requests() { - reads++; - return reads === 1 ? [] : [{ url: `${server.origin}/admitted` }]; - }, - }; - - const attempt = yield* run( - evaluating(`\n\n`), - { generated: changing }, - ); + journalCase( + "WGX15: the request allowance is read once, and `` comes from that reading", + ROOT_PATH, + function* (stream) { + const server = yield* useLoopback(); + let reads = 0; + // An ordinary accessor-backed options object, which is all a host has to be + // for this to matter: the first reading states no bound and a later one + // states one. Whichever this host meant, the name a fragment may write and + // the policy deciding what it may reach have to come from one reading. + const changing: GeneratedEvaluationOptions = { + get requests() { + reads++; + return reads === 1 ? [] : [{ url: `${server.origin}/admitted` }]; + }, + }; - // Asked first, because it is the consequence: the destination the fragment - // names is one no reading of this host's list ever admitted, and a - // selection taken from a second reading would send it under no policy at - // all. - expect(server.asked).toEqual([]); - // The retained reading bounded nothing, so `` was never offered: the - // fragment is refused at admission, by name, and retains no effect. - expect(attempt.failure).toContain("did not admit"); - expect(fetches(attempt.events)).toEqual([]); - // One reading for the whole setup. A second one is what would let the - // selection and the policy disagree about the same host statement. - expect(reads).toBe(1); - }); + const attempt = yield* run( + evaluating( + `\n\n`, + ), + { generated: changing }, + stream, + ); - it("WGX10: an enclosing restriction still refuses what this host bounded", function* () { - const transport = yield* useTransport(); + // Asked first, because it is the consequence: the destination the fragment + // names is one no reading of this host's list ever admitted, and a + // selection taken from a second reading would send it under no policy at + // all. + expect(server.asked).toEqual([]); + // The retained reading bounded nothing, so `` was never offered: the + // fragment is refused at admission, by name, and retains no effect. + expect(attempt.failure).toContain("did not admit"); + expect(fetches(attempt.events)).toEqual([]); + // One reading for the whole setup. A second one is what would let the + // selection and the policy disagree about the same host statement. + expect(reads).toBe(1); + }, + ); - const attempt = yield* scoped(function* () { - // Narrower than the host, installed outside it. A restriction delegates - // rather than answering `true`, so the enclosing refusal is never asked - // to delegate and the bound cannot widen it. - yield* Permissions.around({ fetch: () => false }); - return yield* run(evaluating(`\n\n`), { - generated: { requests: [{ url: URL_ONE }] }, + journalCase( + "WGX10: an enclosing restriction still refuses what this host bounded", + ROOT_PATH, + function* (stream) { + const transport = yield* useTransport(); + + const attempt = yield* scoped(function* () { + // Narrower than the host, installed outside it. A restriction delegates + // rather than answering `true`, so the enclosing refusal is never asked + // to delegate and the bound cannot widen it. + yield* Permissions.around({ fetch: () => false }); + return yield* run( + evaluating(`\n\n`), + { + generated: { requests: [{ url: URL_ONE }] }, + }, + stream, + ); }); - }); - expect(attempt.failure).toContain("not permitted in this scope"); - expect(transport.performed).toHaveLength(0); - }); + expect(attempt.failure).toContain("not permitted in this scope"); + expect(transport.performed).toHaveLength(0); + }, + ); }); describe("Tier WGX — a completed effect restores from its record", () => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 40b028acf..45abc45f5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -477,6 +477,9 @@ importers: '@executablemd/durable-streams': specifier: workspace:* version: link:../durable-streams + '@executablemd/testing': + specifier: workspace:* + version: link:../testing effection: specifier: 4.1.0 version: 4.1.0 diff --git a/tsconfig.node.json b/tsconfig.node.json index 26f97d196..3ff627ba6 100644 --- a/tsconfig.node.json +++ b/tsconfig.node.json @@ -29,6 +29,7 @@ "@executablemd/test-agent": ["./packages/test-agent/mod.ts"], "@executablemd/test-support/bdd": ["./packages/test-support/bdd.ts"], "@executablemd/test-support/expect": ["./packages/test-support/expect.ts"], + "@executablemd/test-support/failure-journal": ["./packages/test-support/failure-journal.ts"], "@executablemd/test-support/journal": ["./packages/test-support/journal.ts"], "@executablemd/test-support/launch": ["./packages/test-support/launch.ts"], "@executablemd/test-support/temp": ["./packages/test-support/temp.ts"], From ee5eb2dd063040c245c5f322f857669aabe930c3 Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Mon, 16 Nov 2026 21:49:36 -0500 Subject: [PATCH 02/11] Adopt shared journals in Core evaluator tests --- packages/core/tests/eval-bindings.test.ts | 13 ++++++++++--- packages/core/tests/eval-error-mode.test.ts | 15 ++++++++++++--- packages/core/tests/eval-return.test.ts | 14 +++++++++++--- packages/core/tests/eval-scope.test.ts | 15 ++++++++++++--- packages/core/tests/eval-timeout.test.ts | 15 ++++++++++++--- packages/core/tests/execute.test.ts | 16 +++++++++++++--- 6 files changed, 70 insertions(+), 18 deletions(-) diff --git a/packages/core/tests/eval-bindings.test.ts b/packages/core/tests/eval-bindings.test.ts index 08f35155f..d53a203ae 100644 --- a/packages/core/tests/eval-bindings.test.ts +++ b/packages/core/tests/eval-bindings.test.ts @@ -4,7 +4,7 @@ * Tests cross-block binding sharing, shadowing, empty blocks, * undeclared references, and syntax errors. */ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; import { InMemoryStream } from "@executablemd/durable-streams"; @@ -17,11 +17,18 @@ import { asText, completion, failureMessage } from "./helpers.ts"; import type { Json } from "../src/types.ts"; import type { DurableEvent } from "@executablemd/durable-streams"; import type { Operation } from "effection"; -import { journalCases } from "../../test-support/journal-cases.ts"; +import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; -const journalCase = journalCases("packages/core/tests/eval-bindings.test.ts"); +const OWNER = "packages/core/tests/eval-bindings.test.ts"; + +function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { + it(name, function* () { + yield* body(yield* useJournalStream(source)); + }); +} describe("Tier T5 — Binding environment", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); // T38: Block 2 reads binding exported by Block 1 via env preamble journalCase("T38: block 2 reads binding from block 1", "test.md", function* (stream) { diff --git a/packages/core/tests/eval-error-mode.test.ts b/packages/core/tests/eval-error-mode.test.ts index 0555a1d51..f85d6bbd0 100644 --- a/packages/core/tests/eval-error-mode.test.ts +++ b/packages/core/tests/eval-error-mode.test.ts @@ -9,16 +9,24 @@ * the block projects. */ -import { beforeAll, describe } from "@executablemd/test-support/bdd"; +import { beforeAll, beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; +import type { InMemoryStream } from "@executablemd/durable-streams"; import { useEchoExec, useStubFs } from "@executablemd/runtime/test"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { DocumentationError } from "../src/errors.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; +import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; +import type { Operation } from "effection"; -const journalCase = journalCases("packages/core/tests/eval-error-mode.test.ts"); +const OWNER = "packages/core/tests/eval-error-mode.test.ts"; + +function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { + it(name, function* () { + yield* body(yield* useJournalStream(source)); + }); +} const PROJECTING_BLOCK = [ "```ts persist eval", @@ -28,6 +36,7 @@ const PROJECTING_BLOCK = [ ]; describe("Tier O — Eval scope hierarchy", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); // O23: the block sits in documentation, so its projection settles under the diff --git a/packages/core/tests/eval-return.test.ts b/packages/core/tests/eval-return.test.ts index f23f11600..173e3d412 100644 --- a/packages/core/tests/eval-return.test.ts +++ b/packages/core/tests/eval-return.test.ts @@ -5,18 +5,26 @@ * that value becomes the block's rendered output. output() takes * precedence over return. Null/undefined returns produce no output. */ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; import { InMemoryStream } from "@executablemd/durable-streams"; import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; +import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; +import type { Operation } from "effection"; -const journalCase = journalCases("packages/core/tests/eval-return.test.ts"); +const OWNER = "packages/core/tests/eval-return.test.ts"; + +function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { + it(name, function* () { + yield* body(yield* useJournalStream(source)); + }); +} describe("Eval block return value", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); // ER1: return string → rendered output journalCase("ER1: return string → rendered output", "test.md", function* (stream) { diff --git a/packages/core/tests/eval-scope.test.ts b/packages/core/tests/eval-scope.test.ts index 7dc8f9285..7522821ba 100644 --- a/packages/core/tests/eval-scope.test.ts +++ b/packages/core/tests/eval-scope.test.ts @@ -4,19 +4,28 @@ * Tests that eval scopes are properly scoped to components and * that child/parent scope relationships work correctly. */ -import { describe, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; +import type { InMemoryStream } from "@executablemd/durable-streams"; import { Stdio } from "@effectionx/process"; import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { asText } from "./helpers.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; +import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; +import type { Operation } from "effection"; -const journalCase = journalCases("packages/core/tests/eval-scope.test.ts"); +const OWNER = "packages/core/tests/eval-scope.test.ts"; + +function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { + it(name, function* () { + yield* body(yield* useJournalStream(source)); + }); +} describe("Tier T10 — eval-scope hierarchy", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); // T61: Document with eval blocks — scope created per document journalCase("T61: eval blocks run within document scope", "test.md", function* (stream) { diff --git a/packages/core/tests/eval-timeout.test.ts b/packages/core/tests/eval-timeout.test.ts index 6c6bb6e16..6085bee17 100644 --- a/packages/core/tests/eval-timeout.test.ts +++ b/packages/core/tests/eval-timeout.test.ts @@ -7,19 +7,28 @@ * lives with the parser (Tier DU) and what reaches a command lives with the * exec terminal (Tier XE). */ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; +import type { InMemoryStream } from "@executablemd/durable-streams"; import { Config } from "@executablemd/runtime"; import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { completion, failureMessage } from "./helpers.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; +import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; +import type { Operation } from "effection"; -const journalCase = journalCases("packages/core/tests/eval-timeout.test.ts"); +const OWNER = "packages/core/tests/eval-timeout.test.ts"; + +function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { + it(name, function* () { + yield* body(yield* useJournalStream(source)); + }); +} describe("Tier T7 — timeout modifier", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); journalCase("T49: block completes before timeout → success", "test.md", function* (stream) { diff --git a/packages/core/tests/execute.test.ts b/packages/core/tests/execute.test.ts index a07ec4061..64046632f 100644 --- a/packages/core/tests/execute.test.ts +++ b/packages/core/tests/execute.test.ts @@ -8,7 +8,7 @@ * Golden run — InMemoryStream() (empty) + useStubFs(files) → assert output + journal * Replay — reuse same InMemoryStream (has events) + useNoIO() → zero I/O */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { InMemoryStream } from "@executablemd/durable-streams"; import { useStubFs, useFailingExec } from "@executablemd/runtime/test"; @@ -26,9 +26,15 @@ import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { asText } from "./helpers.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; +import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; -const journalCase = journalCases("packages/core/tests/execute.test.ts"); +const OWNER = "packages/core/tests/execute.test.ts"; + +function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { + it(name, function* () { + yield* body(yield* useJournalStream(source)); + }); +} /** * A stand-in child that behaves like one: it writes its output to the display @@ -116,6 +122,7 @@ function* useFailingStdoutExec(): Operation { } describe("Tier B — durable import", () => { + beforeEach(journalBeforeEach(OWNER)); // B1: durableImportComponent golden run — journal shape it("B1: import golden run — journal has import_component with path and content", function* () { const stream = new InMemoryStream(); @@ -311,6 +318,7 @@ describe("Tier B — durable import", () => { }); describe("Tier D — code execution and modifiers", () => { + beforeEach(journalBeforeEach(OWNER)); // D1: bash exec golden run it("D1: bash exec golden run — stdout displayed, exec in journal", function* () { const stream = new InMemoryStream(); @@ -719,6 +727,7 @@ describe("Tier D — code execution and modifiers", () => { }); describe("execute", () => { + beforeEach(journalBeforeEach(OWNER)); // E1: Full document golden run it("E1: full document golden run — root + component + exec", function* () { const stream = new InMemoryStream(); @@ -1261,6 +1270,7 @@ describe("Component Api dispatch — journal shape", () => { }); describe("component-declared output — document workflow", () => { + beforeEach(journalBeforeEach(OWNER)); journalCase( "applies to an imported component invoked from a streaming root", "README.md", From 9f04b2cdc8d1d42d18f80cf847bd449e572b21d3 Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Mon, 16 Nov 2026 21:53:19 -0500 Subject: [PATCH 03/11] Use shared failure journal package in evaluator cases --- packages/core/tests/eval-bindings.test.ts | 63 +- packages/core/tests/eval-error-mode.test.ts | 631 +++++++++----------- packages/core/tests/eval-return.test.ts | 30 +- packages/core/tests/eval-scope.test.ts | 16 +- packages/core/tests/eval-timeout.test.ts | 127 ++-- packages/core/tests/execute.test.ts | 456 +++++++------- 6 files changed, 611 insertions(+), 712 deletions(-) diff --git a/packages/core/tests/eval-bindings.test.ts b/packages/core/tests/eval-bindings.test.ts index d53a203ae..0c78999b6 100644 --- a/packages/core/tests/eval-bindings.test.ts +++ b/packages/core/tests/eval-bindings.test.ts @@ -17,21 +17,16 @@ import { asText, completion, failureMessage } from "./helpers.ts"; import type { Json } from "../src/types.ts"; import type { DurableEvent } from "@executablemd/durable-streams"; import type { Operation } from "effection"; -import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; const OWNER = "packages/core/tests/eval-bindings.test.ts"; -function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { - it(name, function* () { - yield* body(yield* useJournalStream(source)); - }); -} - describe("Tier T5 — Binding environment", () => { beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); // T38: Block 2 reads binding exported by Block 1 via env preamble - journalCase("T38: block 2 reads binding from block 1", "test.md", function* (stream) { + it("T38: block 2 reads binding from block 1", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\nconst port = 8080;\n```\n\n```js eval\nconst url = 'http://localhost:' + port;\n```\n", @@ -55,32 +50,30 @@ describe("Tier T5 — Binding environment", () => { }); // T39: Block 3 shadowing Block 1's binding — downstream sees Block 3's value - journalCase( - "T39: shadowing — later block overrides earlier binding", - "test.md", - function* (stream) { - yield* useStubFs({ - "test.md": - "```js eval\nconst x = 1;\n```\n\n```js eval\nconst x = 2;\n```\n\n```js eval\nconst y = x;\n```\n", - }); - yield* useEchoExec(); - - // Should succeed — x is 2 in block 3 (shadowed by block 2) - const output = asText( - yield* collect( - yield* execute({ - path: "test.md", - stream, - }), - ), - ); - - expect(output).not.toContain("ERROR"); - }, - ); + it("T39: shadowing — later block overrides earlier binding", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "test.md": + "```js eval\nconst x = 1;\n```\n\n```js eval\nconst x = 2;\n```\n\n```js eval\nconst y = x;\n```\n", + }); + yield* useEchoExec(); + + // Should succeed — x is 2 in block 3 (shadowed by block 2) + const output = asText( + yield* collect( + yield* execute({ + path: "test.md", + stream, + }), + ), + ); + + expect(output).not.toContain("ERROR"); + }); // T40: Empty block — no exports, no error - journalCase("T40: empty eval block — no exports, no error", "test.md", function* (stream) { + it("T40: empty eval block — no exports, no error", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\n\n```\n", }); @@ -100,7 +93,8 @@ describe("Tier T5 — Binding environment", () => { // T41: Block referencing undeclared binding not in env — nothing recovers it, // so the run's outcome is that failure. - journalCase("T41: undeclared reference → the run fails", "test.md", function* (stream) { + it("T41: undeclared reference → the run fails", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\nconst y = undeclaredVar + 1;\n```\n", }); @@ -113,7 +107,8 @@ describe("Tier T5 — Binding environment", () => { }); // T42: Syntax error in block — parse-time error before execution - journalCase("T42: syntax error → the run fails", "test.md", function* (stream) { + it("T42: syntax error → the run fails", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\nconst x = ;\n```\n", }); diff --git a/packages/core/tests/eval-error-mode.test.ts b/packages/core/tests/eval-error-mode.test.ts index f85d6bbd0..c3860cabd 100644 --- a/packages/core/tests/eval-error-mode.test.ts +++ b/packages/core/tests/eval-error-mode.test.ts @@ -11,23 +11,15 @@ import { beforeAll, beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; -import type { InMemoryStream } from "@executablemd/durable-streams"; import { useEchoExec, useStubFs } from "@executablemd/runtime/test"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { DocumentationError } from "../src/errors.ts"; -import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; -import type { Operation } from "effection"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; const OWNER = "packages/core/tests/eval-error-mode.test.ts"; -function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { - it(name, function* () { - yield* body(yield* useJournalStream(source)); - }); -} - const PROJECTING_BLOCK = [ "```ts persist eval", "const projected = yield* renderChildren();", @@ -41,365 +33,332 @@ describe("Tier O — Eval scope hierarchy", () => { // O23: the block sits in documentation, so its projection settles under the // throwing error mode — the failure aborts the body instead of rendering. - journalCase( - "O23: a persistent projection in documentation settles under the throwing error mode", - "doc.md", - function* (stream) { - yield* useStubFs({ - "components/Wrap.md": [...PROJECTING_BLOCK, "", "", "done", ""].join("\n"), - "doc.md": "\n\n", - }); - yield* useEchoExec(); - - let failure: unknown; - try { - yield* collect(yield* execute({ path: "doc.md", stream })); - } catch (error) { - failure = error; - } - - // Documentation fail-fast: the projected error aborts the body rather than - // rendering, which a printing error mode would never do. - expect(failure).toBeInstanceOf(DocumentationError); - expect(String(failure)).toContain("Missing"); - }, - ); + it("O23: a persistent projection in documentation settles under the throwing error mode", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "components/Wrap.md": [...PROJECTING_BLOCK, "", "", "done", ""].join("\n"), + "doc.md": "\n\n", + }); + yield* useEchoExec(); + + let failure: unknown; + try { + yield* collect(yield* execute({ path: "doc.md", stream })); + } catch (error) { + failure = error; + } + + // Documentation fail-fast: the projected error aborts the body rather than + // rendering, which a printing error mode would never do. + expect(failure).toBeInstanceOf(DocumentationError); + expect(String(failure)).toContain("Missing"); + }); // O28: a claimed in a documentation region settles under the // throwing error mode — the projected error stops the body instead of being // printed and then discarded with the region's rendered output. - journalCase( - "O28: Markdown in documentation fails fast on a projected error", - "doc.md", - function* (stream) { - yield* useStubFs({ - "components/Wrap.md": ["", "", "", "done", ""].join("\n"), - "doc.md": "\n\n", - }); - yield* useEchoExec(); - - let failure: unknown; - try { - yield* collect(yield* execute({ path: "doc.md", stream })); - } catch (error) { - failure = error; - } - - expect(failure).toBeInstanceOf(DocumentationError); - expect(String(failure)).toContain("Missing"); - }, - ); + it("O28: Markdown in documentation fails fast on a projected error", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "components/Wrap.md": ["", "", "", "done", ""].join("\n"), + "doc.md": "\n\n", + }); + yield* useEchoExec(); + + let failure: unknown; + try { + yield* collect(yield* execute({ path: "doc.md", stream })); + } catch (error) { + failure = error; + } + + expect(failure).toBeInstanceOf(DocumentationError); + expect(String(failure)).toContain("Missing"); + }); // O29: the same projection inside carries the region's error mode, // so the projected error fails the region and nothing after it renders. - journalCase( - "O29: Markdown inside fails the region on a projected error", - "doc.md", - function* (stream) { - yield* useStubFs({ - "components/Wrap.md": ["", "", "", "done", ""].join("\n"), - "doc.md": "\n\n", - }); - yield* useEchoExec(); - - let failure: unknown; - try { - yield* collect(yield* execute({ path: "doc.md", stream })); - } catch (error) { - failure = error; - } - - expect(String(failure)).toContain("Missing"); - }, - ); + it("O29: Markdown inside fails the region on a projected error", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "components/Wrap.md": ["", "", "", "done", ""].join("\n"), + "doc.md": "\n\n", + }); + yield* useEchoExec(); + + let failure: unknown; + try { + yield* collect(yield* execute({ path: "doc.md", stream })); + } catch (error) { + failure = error; + } + + expect(String(failure)).toContain("Missing"); + }); // O30: value-component documentation carries the same error mode, so a claimed // that projects an error fails fast instead of being discarded // along with the documentation's rendered output. - journalCase( - "O30: a projected error in value-component documentation fails fast", - "doc.md", - function* (stream) { - yield* useStubFs({ - "components/Value.md": [ - "---", - "returns:", - " ok: { type: boolean }", - "---", - "", - "", - "", - '', - ].join("\n"), - "doc.md": '\n\n\n\nafter', - }); - yield* useEchoExec(); - - let failure: unknown; - try { - yield* collect(yield* execute({ path: "doc.md", stream })); - } catch (error) { - failure = error; - } - - expect(failure).toBeInstanceOf(DocumentationError); - expect(String(failure)).toContain("Missing"); - }, - ); + it("O30: a projected error in value-component documentation fails fast", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "components/Value.md": [ + "---", + "returns:", + " ok: { type: boolean }", + "---", + "", + "", + "", + '', + ].join("\n"), + "doc.md": '\n\n\n\nafter', + }); + yield* useEchoExec(); + + let failure: unknown; + try { + yield* collect(yield* execute({ path: "doc.md", stream })); + } catch (error) { + failure = error; + } + + expect(failure).toBeInstanceOf(DocumentationError); + expect(String(failure)).toContain("Missing"); + }); // O25: an evaluation sees the bindings that existed when it started, and its // declared exports become shared once it completes. - journalCase( - "O25: exports are committed to the shared bindings for later blocks", - "doc.md", - function* (stream) { - yield* useStubFs({ - "doc.md": [ - "```js eval", - "const port = 4321;", - // A function is not JSON, so the journal cannot carry it: only the - // explicit commit makes it reachable from the next block. - "const describe = (n) => `port-${n}`;", - "```", - "", - "```js eval", - "output(`port=${port} ${describe(port)}`);", - "```", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - // `describe` is a function — `serializeExports` drops it, so reaching it - // from the next block proves the commit publishes live values. - expect(output).toContain("port=4321 port-4321"); - }, - ); + it("O25: exports are committed to the shared bindings for later blocks", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "doc.md": [ + "```js eval", + "const port = 4321;", + // A function is not JSON, so the journal cannot carry it: only the + // explicit commit makes it reachable from the next block. + "const describe = (n) => `port-${n}`;", + "```", + "", + "```js eval", + "output(`port=${port} ${describe(port)}`);", + "```", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + // `describe` is a function — `serializeExports` drops it, so reaching it + // from the next block proves the commit publishes live values. + expect(output).toContain("port=4321 port-4321"); + }); // O26: persistent work keeps the values and the mode-bound capabilities it // captured; a later evaluation gets its own snapshot and cannot reach in. - journalCase( - "O26: a later evaluation cannot alter an earlier block's captured bindings", - "doc.md", - function* (stream) { - yield* useStubFs({ - "components/Wrap.md": [ - "```js persist eval", - "const captured = { seen: null };", - "const renderer = renderChildren;", - "yield* spawn(function*() {", - " yield* sleep(5);", - " captured.seen = typeof renderer;", - "});", - "```", - "", - "```js eval", - "const renderer = null;", - "```", - "", - "```js eval", - "yield* when(function*() {", - ' if (captured.seen === null) throw new Error("not yet");', - "});", - "output(`seen=${captured.seen}`);", - "```", - ].join("\n"), - "doc.md": "\ncontent\n", - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - // The middle block rebinding `renderer` did not reach the closure the - // persistent task captured from its own snapshot. - expect(output).toContain("seen=function"); - }, - ); + it("O26: a later evaluation cannot alter an earlier block's captured bindings", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "components/Wrap.md": [ + "```js persist eval", + "const captured = { seen: null };", + "const renderer = renderChildren;", + "yield* spawn(function*() {", + " yield* sleep(5);", + " captured.seen = typeof renderer;", + "});", + "```", + "", + "```js eval", + "const renderer = null;", + "```", + "", + "```js eval", + "yield* when(function*() {", + ' if (captured.seen === null) throw new Error("not yet");', + "});", + "output(`seen=${captured.seen}`);", + "```", + ].join("\n"), + "doc.md": "\ncontent\n", + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + // The middle block rebinding `renderer` did not reach the closure the + // persistent task captured from its own snapshot. + expect(output).toContain("seen=function"); + }); // O27: an explicit import shadows the injected binding without the preamble // declaring it twice — the module would not compile otherwise. - journalCase( - "O27: an explicitly imported useContent compiles alongside the injected one", - "doc.md", - function* (stream) { - yield* useStubFs({ - "components/Wrap.md": [ - "```ts eval", - 'import { useContent } from "@executablemd/core";', - "const kind = typeof useContent;", - "output(`imported=${kind}`);", - "```", - ].join("\n"), - "doc.md": "\ncontent\n", - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - expect(output).toContain("imported=function"); - expect(output).not.toContain("ERROR"); - }, - ); + it("O27: an explicitly imported useContent compiles alongside the injected one", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "components/Wrap.md": [ + "```ts eval", + 'import { useContent } from "@executablemd/core";', + "const kind = typeof useContent;", + "output(`imported=${kind}`);", + "```", + ].join("\n"), + "doc.md": "\ncontent\n", + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + expect(output).toContain("imported=function"); + expect(output).not.toContain("ERROR"); + }); // O24: the same block inside an region settles under `output`, so // the projected error fails the run rather than rendering as a comment. - journalCase( - "O24: a persistent projection inside settles under the output error mode", - "doc.md", - function* (stream) { - yield* useStubFs({ - "components/Wrap.md": ["", ...PROJECTING_BLOCK, ""].join("\n"), - "doc.md": "\n\n", - }); - yield* useEchoExec(); - - let failure: unknown; - try { - yield* collect(yield* execute({ path: "doc.md", stream })); - } catch (error) { - failure = error; - } - - expect(String(failure)).toContain("Missing"); - }, - ); + it("O24: a persistent projection inside settles under the output error mode", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "components/Wrap.md": ["", ...PROJECTING_BLOCK, ""].join("\n"), + "doc.md": "\n\n", + }); + yield* useEchoExec(); + + let failure: unknown; + try { + yield* collect(yield* execute({ path: "doc.md", stream })); + } catch (error) { + failure = error; + } + + expect(String(failure)).toContain("Missing"); + }); // O31: the same printing projection, captured — the string rendered the // error away, so the capture is refused (§6.5) and the recorded segment // returns to the caller instead of hiding inside the bound string. - journalCase( - "O31: a captured markdown projection refuses the binding on a projected error", - "doc.md", - function* (stream) { - yield* useStubFs({ - "components/Wrap.md": [ - "", - "", - ...PROJECTING_BLOCK, - "", - "", - ].join("\n"), - "doc.md": '\n\n\n\nvalue:{cap}:end', - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - expect(output).toContain("ERROR"); - // The binding stays unset, so the interpolation stays literal and the - // sibling text still renders under the printing error mode. - expect(output).toContain("value:{cap}:end"); - // Reported once on the way out, not again when it crosses back. - expect(String(output).split("Cannot resolve component: Missing").length - 1).toBe(1); - }, - ); + it("O31: a captured markdown projection refuses the binding on a projected error", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "components/Wrap.md": [ + "", + "", + ...PROJECTING_BLOCK, + "", + "", + ].join("\n"), + "doc.md": '\n\n\n\nvalue:{cap}:end', + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + expect(output).toContain("ERROR"); + // The binding stays unset, so the interpolation stays literal and the + // sibling text still renders under the printing error mode. + expect(output).toContain("value:{cap}:end"); + // Reported once on the way out, not again when it crosses back. + expect(String(output).split("Cannot resolve component: Missing").length - 1).toBe(1); + }); // O32: a caught DocumentationError is explicit recovery. The throw-bound // projection fails before anything is recorded for the capture refusal, so // the recovered component completes and the capture succeeds. - journalCase( - "O32: a caught projection error recovers without a later capture refusal", - "doc.md", - function* (stream) { - yield* useStubFs({ - "components/Wrap.md": [ - "```ts eval", - "const projected = yield* call(function* () {", - " try {", - " return yield* renderChildren();", - " } catch (error) {", - ' return "recovered";', - " }", - "});", - "```", - "", - "", - "```ts eval", - "output(projected);", - "```", - "", - ].join("\n"), - "doc.md": '\n\n\n\nvalue:{cap}:end', - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - // The caught error is not reasserted: the binding holds the recovered - // output and no error segment reaches the caller. - expect(output).toContain("recovered"); - expect(output).not.toContain("{cap}"); - expect(output).not.toContain("ERROR"); - }, - ); + it("O32: a caught projection error recovers without a later capture refusal", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "components/Wrap.md": [ + "```ts eval", + "const projected = yield* call(function* () {", + " try {", + " return yield* renderChildren();", + " } catch (error) {", + ' return "recovered";', + " }", + "});", + "```", + "", + "", + "```ts eval", + "output(projected);", + "```", + "", + ].join("\n"), + "doc.md": '\n\n\n\nvalue:{cap}:end', + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + // The caught error is not reasserted: the binding holds the recovered + // output and no error segment reaches the caller. + expect(output).toContain("recovered"); + expect(output).not.toContain("{cap}"); + expect(output).not.toContain("ERROR"); + }); // O33: work the projected content started is torn down with its scope, not // leaked past the invocation because the projection's error was caught. - journalCase( - "O33: projection-owned work does not escape a caught projection error", - "doc.md", - function* (stream) { - yield* useStubFs({ - "components/Wrap.md": [ - "```ts eval", - "const projected = yield* call(function* () {", - " try {", - " return yield* renderChildren();", - " } catch (error) {", - ' return "recovered";', - " }", - "});", - "```", - "", - "", - "```ts eval", - "output(projected);", - "```", - "", - ].join("\n"), - "doc.md": [ - "```js eval", - "const probe = { started: false, halted: false };", - "```", - "", - '', - "```js persist eval", - "yield* spawn(function*() {", - " probe.started = true;", - " try {", - " yield* suspend();", - " } finally {", - " probe.halted = true;", - " }", - "});", - "```", - "", - "```js eval", - "yield* when(function*() {", - ' if (!probe.started) throw new Error("not yet");', - "});", - "```", - "", - "", - "", - "", - "value:{cap}:end", - "", - "```js eval", - "output(`halted=${probe.halted}`);", - "```", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "doc.md", stream })); - - // The suspended task the children spawned is gone by the time the sibling - // runs: recovery completed, and catching the error leaked nothing. - expect(output).toContain("recovered"); - expect(output).toContain("halted=true"); - expect(output).not.toContain("ERROR"); - }, - ); + it("O33: projection-owned work does not escape a caught projection error", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "components/Wrap.md": [ + "```ts eval", + "const projected = yield* call(function* () {", + " try {", + " return yield* renderChildren();", + " } catch (error) {", + ' return "recovered";', + " }", + "});", + "```", + "", + "", + "```ts eval", + "output(projected);", + "```", + "", + ].join("\n"), + "doc.md": [ + "```js eval", + "const probe = { started: false, halted: false };", + "```", + "", + '', + "```js persist eval", + "yield* spawn(function*() {", + " probe.started = true;", + " try {", + " yield* suspend();", + " } finally {", + " probe.halted = true;", + " }", + "});", + "```", + "", + "```js eval", + "yield* when(function*() {", + ' if (!probe.started) throw new Error("not yet");', + "});", + "```", + "", + "", + "", + "", + "value:{cap}:end", + "", + "```js eval", + "output(`halted=${probe.halted}`);", + "```", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "doc.md", stream })); + + // The suspended task the children spawned is gone by the time the sibling + // runs: recovery completed, and catching the error leaked nothing. + expect(output).toContain("recovered"); + expect(output).toContain("halted=true"); + expect(output).not.toContain("ERROR"); + }); }); diff --git a/packages/core/tests/eval-return.test.ts b/packages/core/tests/eval-return.test.ts index 173e3d412..e2fc088cc 100644 --- a/packages/core/tests/eval-return.test.ts +++ b/packages/core/tests/eval-return.test.ts @@ -12,22 +12,16 @@ import { InMemoryStream } from "@executablemd/durable-streams"; import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; -import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; -import type { Operation } from "effection"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; const OWNER = "packages/core/tests/eval-return.test.ts"; -function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { - it(name, function* () { - yield* body(yield* useJournalStream(source)); - }); -} - describe("Eval block return value", () => { beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); // ER1: return string → rendered output - journalCase("ER1: return string → rendered output", "test.md", function* (stream) { + it("ER1: return string → rendered output", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": '```js eval\nreturn "hello from eval";\n```\n', }); @@ -44,7 +38,8 @@ describe("Eval block return value", () => { }); // ER2: return number → coerced to string - journalCase("ER2: return number → coerced to string", "test.md", function* (stream) { + it("ER2: return number → coerced to string", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\nreturn 42;\n```\n", }); @@ -61,7 +56,8 @@ describe("Eval block return value", () => { }); // ER3: return null → no output - journalCase("ER3: return null → no output", "test.md", function* (stream) { + it("ER3: return null → no output", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "before\n```js eval\nreturn null;\n```\nafter", }); @@ -79,7 +75,8 @@ describe("Eval block return value", () => { }); // ER4: return undefined → no output - journalCase("ER4: return undefined → no output", "test.md", function* (stream) { + it("ER4: return undefined → no output", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "before\n```js eval\nreturn undefined;\n```\nafter", }); @@ -96,7 +93,8 @@ describe("Eval block return value", () => { }); // ER5: output() takes precedence over return - journalCase("ER5: output() takes precedence over return", "test.md", function* (stream) { + it("ER5: output() takes precedence over return", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": '```js eval\noutput("explicit");\nreturn "ignored";\n```\n', }); @@ -114,7 +112,8 @@ describe("Eval block return value", () => { }); // ER6: return in persist eval → works same as normal - journalCase("ER6: return in persist eval → works same as normal", "test.md", function* (stream) { + it("ER6: return in persist eval → works same as normal", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": '```js persist eval\nreturn "persisted output";\n```\n', }); @@ -131,7 +130,8 @@ describe("Eval block return value", () => { }); // ER7: no return → no output (existing behavior) - journalCase("ER7: no return → no output (existing behavior)", "test.md", function* (stream) { + it("ER7: no return → no output (existing behavior)", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "before\n```js eval\nconst x = 42;\n```\nafter", }); diff --git a/packages/core/tests/eval-scope.test.ts b/packages/core/tests/eval-scope.test.ts index 7522821ba..ab20afc84 100644 --- a/packages/core/tests/eval-scope.test.ts +++ b/packages/core/tests/eval-scope.test.ts @@ -7,28 +7,21 @@ import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; -import type { InMemoryStream } from "@executablemd/durable-streams"; import { Stdio } from "@effectionx/process"; import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { asText } from "./helpers.ts"; -import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; -import type { Operation } from "effection"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; const OWNER = "packages/core/tests/eval-scope.test.ts"; -function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { - it(name, function* () { - yield* body(yield* useJournalStream(source)); - }); -} - describe("Tier T10 — eval-scope hierarchy", () => { beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); // T61: Document with eval blocks — scope created per document - journalCase("T61: eval blocks run within document scope", "test.md", function* (stream) { + it("T61: eval blocks run within document scope", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\nconst a = 1;\n```\n\n```js eval\nconst b = a + 1;\n```\n", }); @@ -50,7 +43,8 @@ describe("Tier T10 — eval-scope hierarchy", () => { }); // T62: Eval blocks coexist with exec blocks - journalCase("T62: eval and exec blocks coexist", "test.md", function* (stream) { + it("T62: eval and exec blocks coexist", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\nconst x = 42;\n```\n\n```bash exec\necho hello\n```\n\n```js eval\nconst y = x + 1;\n```\n", diff --git a/packages/core/tests/eval-timeout.test.ts b/packages/core/tests/eval-timeout.test.ts index 6085bee17..ae41c3cdf 100644 --- a/packages/core/tests/eval-timeout.test.ts +++ b/packages/core/tests/eval-timeout.test.ts @@ -10,28 +10,21 @@ import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; -import type { InMemoryStream } from "@executablemd/durable-streams"; import { Config } from "@executablemd/runtime"; import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { completion, failureMessage } from "./helpers.ts"; -import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; -import type { Operation } from "effection"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; const OWNER = "packages/core/tests/eval-timeout.test.ts"; -function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { - it(name, function* () { - yield* body(yield* useJournalStream(source)); - }); -} - describe("Tier T7 — timeout modifier", () => { beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); - journalCase("T49: block completes before timeout → success", "test.md", function* (stream) { + it("T49: block completes before timeout → success", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js timeout=30s eval\nconst x = 42;\n```\n", }); @@ -48,72 +41,60 @@ describe("Tier T7 — timeout modifier", () => { expect(output).not.toContain("ERROR"); }); - journalCase( - "T50: a bare timeout boxes the block at the run's exec default", - "test.md", - function* (stream) { - yield* Config.around({ timeoutExec: () => 30_000 }, { at: "min" }); - yield* useStubFs({ - "test.md": "```js timeout eval\nconst x = 1;\n```\n", - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).not.toContain("timed out"); - }, - ); - - journalCase( - "T50a: a bare timeout with no exec default refuses instead of running unbounded", - "test.md", - function* (stream) { - yield* useStubFs({ - "test.md": "```js timeout eval\nconst x = 1;\n```\n", - }); - - const result = yield* completion({ path: "test.md", stream }); - - expect(result.ok).toBe(false); - expect(failureMessage(result)).toContain("names no duration"); - }, - ); + it("T50: a bare timeout boxes the block at the run's exec default", function* () { + const stream = yield* useJournalStream("test.md"); + yield* Config.around({ timeoutExec: () => 30_000 }, { at: "min" }); + yield* useStubFs({ + "test.md": "```js timeout eval\nconst x = 1;\n```\n", + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).not.toContain("timed out"); + }); + + it("T50a: a bare timeout with no exec default refuses instead of running unbounded", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "test.md": "```js timeout eval\nconst x = 1;\n```\n", + }); + + const result = yield* completion({ path: "test.md", stream }); + + expect(result.ok).toBe(false); + expect(failureMessage(result)).toContain("names no duration"); + }); /** * The timebox itself, at the value the run configured: an eval block that * sleeps past it is cancelled, and the message names the duration used. */ - journalCase( - "T57: the timebox of a bare timeout is the run's exec default", - "test.md", - function* (stream) { - yield* Config.around({ timeoutExec: () => 25 }, { at: "min" }); - yield* useStubFs({ - "test.md": '```ts timeout eval\nyield* sleep(300);\noutput("SLEPT");\n```\n', - }); - - const result = yield* completion({ path: "test.md", stream }); - - expect(result.ok).toBe(false); - expect(failureMessage(result)).toContain("eval block timed out after 25ms"); - expect(failureMessage(result)).not.toContain("SLEPT"); - }, - ); - - journalCase( - "T58: a declared duration boxes the block at what it declared", - "test.md", - function* (stream) { - yield* Config.around({ timeoutExec: () => 60_000 }, { at: "min" }); - yield* useStubFs({ - "test.md": '```ts timeout=25ms eval\nyield* sleep(300);\noutput("SLEPT");\n```\n', - }); - - const result = yield* completion({ path: "test.md", stream }); - - expect(result.ok).toBe(false); - expect(failureMessage(result)).toContain("eval block timed out after 25ms"); - expect(failureMessage(result)).not.toContain("SLEPT"); - }, - ); + it("T57: the timebox of a bare timeout is the run's exec default", function* () { + const stream = yield* useJournalStream("test.md"); + yield* Config.around({ timeoutExec: () => 25 }, { at: "min" }); + yield* useStubFs({ + "test.md": '```ts timeout eval\nyield* sleep(300);\noutput("SLEPT");\n```\n', + }); + + const result = yield* completion({ path: "test.md", stream }); + + expect(result.ok).toBe(false); + expect(failureMessage(result)).toContain("eval block timed out after 25ms"); + expect(failureMessage(result)).not.toContain("SLEPT"); + }); + + it("T58: a declared duration boxes the block at what it declared", function* () { + const stream = yield* useJournalStream("test.md"); + yield* Config.around({ timeoutExec: () => 60_000 }, { at: "min" }); + yield* useStubFs({ + "test.md": '```ts timeout=25ms eval\nyield* sleep(300);\noutput("SLEPT");\n```\n', + }); + + const result = yield* completion({ path: "test.md", stream }); + + expect(result.ok).toBe(false); + expect(failureMessage(result)).toContain("eval block timed out after 25ms"); + expect(failureMessage(result)).not.toContain("SLEPT"); + }); }); diff --git a/packages/core/tests/execute.test.ts b/packages/core/tests/execute.test.ts index 64046632f..19649d447 100644 --- a/packages/core/tests/execute.test.ts +++ b/packages/core/tests/execute.test.ts @@ -26,16 +26,10 @@ import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { asText } from "./helpers.ts"; -import { journalBeforeEach, useJournalStream } from "../../test-support/failure-journal.ts"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; const OWNER = "packages/core/tests/execute.test.ts"; -function journalCase(name: string, source: string, body: (stream: InMemoryStream) => Operation): void { - it(name, function* () { - yield* body(yield* useJournalStream(source)); - }); -} - /** * A stand-in child that behaves like one: it writes its output to the display * channel as it "runs", and hands back only what the caller asked to retain. @@ -216,22 +210,19 @@ describe("Tier B — durable import", () => { }); // B9: import missing component — uncaught at the root, so it fails the run - journalCase( - "B9: import missing component — the run fails and reports it", - "README.md", - function* (stream) { - yield* useStubFs({ "README.md": "\n" }); - yield* useStubExec(); - - const message = yield* failure(function* () { - return yield* collect(yield* execute({ path: "README.md", stream })); - }); + it("B9: import missing component — the run fails and reports it", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ "README.md": "\n" }); + yield* useStubExec(); - expect( - message.includes("Cannot resolve component") || message.includes("Failed to import"), - ).toBeTruthy(); - }, - ); + const message = yield* failure(function* () { + return yield* collect(yield* execute({ path: "README.md", stream })); + }); + + expect( + message.includes("Cannot resolve component") || message.includes("Failed to import"), + ).toBeTruthy(); + }); // B12: root document as component — __root__ import, same journal shape it("B12: root document imported as __root__", function* () { @@ -295,7 +286,8 @@ describe("Tier B — durable import", () => { }); // B15: default resolver middleware — resolves via stat in search path order - journalCase("B15: resolver searches component dirs in order", "README.md", function* (stream) { + it("B15: resolver searches component dirs in order", function* () { + const stream = yield* useJournalStream("README.md"); // Component exists in root dir (second search path), not components/ yield* useStubFs({ @@ -381,7 +373,8 @@ describe("Tier D — code execution and modifiers", () => { }); // D3: a nonzero exit is a checked failure, with or without (#441) - journalCase("D3: non-zero exit code → the run fails", "README.md", function* (stream) { + it("D3: non-zero exit code → the run fails", function* () { + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": ["```bash exec", "failing-command", "```"].join("\n") }); yield* useFailingExec(1, "command not found"); @@ -395,26 +388,23 @@ describe("Tier D — code execution and modifiers", () => { // D3b: the same rule for a command that printed before it failed (#307), run // as a real subprocess so the exit code travels the whole way through // `execFactory` rather than a stub's return value. - journalCase( - "D3b: non-zero exit with stdout → output, then the printed error", - "README.md", - function* (stream) { - yield* useStubFs({ - "README.md": ["```bash exec", "echo partial; exit 1", "```"].join("\n"), - }); + it("D3b: non-zero exit with stdout → output, then the printed error", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ + "README.md": ["```bash exec", "echo partial; exit 1", "```"].join("\n"), + }); - const run = yield* displayed(function* () { - return yield* failure(function* () { - return yield* collect(yield* execute({ path: "README.md", stream })); - }); + const run = yield* displayed(function* () { + return yield* failure(function* () { + return yield* collect(yield* execute({ path: "README.md", stream })); }); + }); - // What the command printed before it failed reached the reader; the run - // then fails rather than rendering the diagnostic and carrying on (#441). - expect(run.stdout).toContain("partial"); - expect(run.value).toContain("Command failed (exit 1)"); - }, - ); + // What the command printed before it failed reached the reader; the run + // then fails rather than rendering the diagnostic and carrying on (#441). + expect(run.stdout).toContain("partial"); + expect(run.value).toContain("Command failed (exit 1)"); + }); // D4: multi-line command — full script passed to -c it("D4: multi-line command — full script in journal", function* () { @@ -613,94 +603,86 @@ describe("Tier D — code execution and modifiers", () => { }); // D15: unknown modifier in chain → error, uncaught, so the run fails - journalCase( - "D15: unknown modifier → the run fails and reports it", - "README.md", - function* (stream) { - yield* useStubFs({ - "README.md": ["```bash frobnicate exec", "echo test", "```"].join("\n"), - }); - yield* useStubExec(); + it("D15: unknown modifier → the run fails and reports it", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ + "README.md": ["```bash frobnicate exec", "echo test", "```"].join("\n"), + }); + yield* useStubExec(); - const message = yield* failure(function* () { - return yield* collect(yield* execute({ path: "README.md", stream })); - }); + const message = yield* failure(function* () { + return yield* collect(yield* execute({ path: "README.md", stream })); + }); - expect(message.includes("Unknown modifier") || message.includes("frobnicate")).toBeTruthy(); - }, - ); + expect(message.includes("Unknown modifier") || message.includes("frobnicate")).toBeTruthy(); + }); // D16: no terminal modifier → error - journalCase( - "D16: no terminal modifier — code block without exec/eval is passive text", - "README.md", - function* (stream) { - yield* useStubFs({ - "README.md": ["```bash", "echo test", "```"].join("\n"), - }); - yield* useStubExec(); + it("D16: no terminal modifier — code block without exec/eval is passive text", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ + "README.md": ["```bash", "echo test", "```"].join("\n"), + }); + yield* useStubExec(); - const result = asText( - yield* collect( - yield* execute({ - path: "README.md", - stream, - }), - ), - ); + const result = asText( + yield* collect( + yield* execute({ + path: "README.md", + stream, + }), + ), + ); - // Without exec/eval, code block is passive text — preserved as-is - expect(result).toContain("```bash"); - expect(result).toContain("echo test"); - }, - ); + // Without exec/eval, code block is passive text — preserved as-is + expect(result).toContain("```bash"); + expect(result).toContain("echo test"); + }); // D17: custom modifier registration - journalCase( - "D17: custom modifier registration — handler runs in chain", - "README.md", - function* (stream) { - yield* useStubFs({ - // A forwarded block renders nothing, so the text a modifier transforms - // comes from the region that asked for it (#441). - "README.md": [ - '', - "```bash uppercase exec", - "echo hello", - "```", - "", - "", - "{shouted}", - ].join("\n"), - }); - yield* useStubExec(); + it("D17: custom modifier registration — handler runs in chain", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ + // A forwarded block renders nothing, so the text a modifier transforms + // comes from the region that asked for it (#441). + "README.md": [ + '', + "```bash uppercase exec", + "echo hello", + "```", + "", + "", + "{shouted}", + ].join("\n"), + }); + yield* useStubExec(); - const result = asText( - yield* collect( - yield* execute({ - path: "README.md", - stream, - modifiers: { - uppercase: (_params) => (_args, next) => - (function* () { - const inner = yield* next(); - return { - output: inner.output.toUpperCase(), - exitCode: inner.exitCode, - stderr: inner.stderr, - }; - })(), - }, - }), - ), - ); + const result = asText( + yield* collect( + yield* execute({ + path: "README.md", + stream, + modifiers: { + uppercase: (_params) => (_args, next) => + (function* () { + const inner = yield* next(); + return { + output: inner.output.toUpperCase(), + exitCode: inner.exitCode, + stderr: inner.stderr, + }; + })(), + }, + }), + ), + ); - expect(result).toContain("HELLO"); - }, - ); + expect(result).toContain("HELLO"); + }); // D19: modifier parsing — timeout=30s - journalCase("D19: modifier with params parsed correctly", "README.md", function* (stream) { + it("D19: modifier with params parsed correctly", function* () { + const stream = yield* useJournalStream("README.md"); // We test that the modifier receives its params let receivedParams: string | undefined; @@ -822,7 +804,8 @@ describe("execute", () => { }); // E6: Props flow through expansion - journalCase("E6: validated props flow through expansion", "README.md", function* (stream) { + it("E6: validated props flow through expansion", function* () { + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": '\n', "components/Greeting.md": [ @@ -854,7 +837,8 @@ describe("execute", () => { }); // E7: Undeclared prop in full document — uncaught, so the run fails - journalCase("E7: undeclared prop fails the run and reports it", "README.md", function* (stream) { + it("E7: undeclared prop fails the run and reports it", function* () { + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": '\n', "components/Badge.md": ["---", "color: blue", "---", "", "badge"].join("\n"), @@ -899,7 +883,8 @@ describe("execute", () => { }); // Simple text document — no components, no exec - journalCase("simple text document — passthrough", "README.md", function* (stream) { + it("simple text document — passthrough", function* () { + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": "# Hello World\n\nThis is a test.\n" }); yield* useStubExec(); @@ -916,7 +901,8 @@ describe("execute", () => { }); // Default props applied - journalCase("default props applied when not provided", "README.md", function* (stream) { + it("default props applied when not provided", function* () { + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": "\n", "components/Greeting.md": [ @@ -1057,39 +1043,36 @@ describe("execute", () => { }); // E10: unclosed bold across component boundary — healed - journalCase( - "E10: unclosed bold across component boundary — healed in first segment", - "README.md", - function* (stream) { - yield* useStubFs({ - "README.md": "This is **bold\n\nmore text\n", - "components/Greeting.md": "greeting\n", - }); - yield* useStubExec(); + it("E10: unclosed bold across component boundary — healed in first segment", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ + "README.md": "This is **bold\n\nmore text\n", + "components/Greeting.md": "greeting\n", + }); + yield* useStubExec(); - const result = asText( - yield* collect( - yield* execute({ - path: "README.md", - stream, - }), - ), - ); + const result = asText( + yield* collect( + yield* execute({ + path: "README.md", + stream, + }), + ), + ); - // The **bold should be healed (closed) before component expansion. - // remend appends closing ** after the text segment (including trailing \n). - // Result: "This is **bold\n**" + "greeting\n" + "\nmore text\n" - expect(result).toContain("greeting"); - expect(result).toContain("more text"); - - // The first text segment should be healed — it should contain both - // opening ** and closing ** (remend closes the unclosed bold). - // The closing ** appears after the newline: "**bold\n**" - const beforeComponent = result.split("greeting")[0]!; - const openCount = (beforeComponent.match(/\*\*/g) || []).length; - expect(openCount).toBe(2); - }, - ); + // The **bold should be healed (closed) before component expansion. + // remend appends closing ** after the text segment (including trailing \n). + // Result: "This is **bold\n**" + "greeting\n" + "\nmore text\n" + expect(result).toContain("greeting"); + expect(result).toContain("more text"); + + // The first text segment should be healed — it should contain both + // opening ** and closing ** (remend closes the unclosed bold). + // The closing ** appears after the newline: "**bold\n**" + const beforeComponent = result.split("greeting")[0]!; + const openCount = (beforeComponent.match(/\*\*/g) || []).length; + expect(openCount).toBe(2); + }); // E2 complex: full replay with component + exec — zero I/O it("E2 complex: full replay with component + exec — zero I/O on second run", function* () { @@ -1215,7 +1198,8 @@ describe("execute", () => { }); // Content slot with exec - journalCase("Content slot + exec — both work together", "README.md", function* (stream) { + it("Content slot + exec — both work together", function* () { + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": ["", "```bash exec", "echo inside", "```", ""].join("\n"), "components/Wrapper.md": "BEFORE\n\nAFTER\n", @@ -1271,107 +1255,93 @@ describe("Component Api dispatch — journal shape", () => { describe("component-declared output — document workflow", () => { beforeEach(journalBeforeEach(OWNER)); - journalCase( - "applies to an imported component invoked from a streaming root", - "README.md", - function* (stream) { - yield* useStubFs({ - "README.md": "Intro paragraph.\n\n\n", - "components/Warn.md": "Docs line.\n\n\nWARNING_SHOWN\n\n", - }); - yield* useStubExec(); - const output = yield* collect(yield* execute({ path: "README.md", stream })); - expect(output).toContain("Intro paragraph."); - expect(output).toContain("WARNING_SHOWN"); - expect(output).not.toContain("Docs line"); - }, - ); + it("applies to an imported component invoked from a streaming root", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ + "README.md": "Intro paragraph.\n\n\n", + "components/Warn.md": "Docs line.\n\n\nWARNING_SHOWN\n\n", + }); + yield* useStubExec(); + const output = yield* collect(yield* execute({ path: "README.md", stream })); + expect(output).toContain("Intro paragraph."); + expect(output).toContain("WARNING_SHOWN"); + expect(output).not.toContain("Docs line"); + }); - journalCase("applies to a root document the same way", "README.md", function* (stream) { + it("applies to a root document the same way", function* () { + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": "Root docs.\n\n\nROOT_SHOWN\n\n" }); const output = yield* collect(yield* execute({ path: "README.md", stream })); expect(output).toContain("ROOT_SHOWN"); expect(output).not.toContain("Root docs"); }); - journalCase( - "buffers a root with into a single emission after success", - "README.md", - function* (stream) { - yield* useStubFs({ "README.md": "Root docs.\n\n\nROOT_SHOWN\n\n" }); + it("buffers a root with into a single emission after success", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ "README.md": "Root docs.\n\n\nROOT_SHOWN\n\n" }); - const execution = yield* execute({ path: "README.md", stream }); - const chunks: string[] = []; - const full = yield* forEach(function* (chunk: string) { - chunks.push(chunk); - }, execution.output); + const execution = yield* execute({ path: "README.md", stream }); + const chunks: string[] = []; + const full = yield* forEach(function* (chunk: string) { + chunks.push(chunk); + }, execution.output); - expect(full).toContain("ROOT_SHOWN"); - expect(chunks).toHaveLength(1); - }, - ); - - journalCase( - "emits what a buffered root selected before its documentation failed", - "README.md", - function* (stream) { - yield* useStubFs({ - "README.md": "\nSELECTED\n\n\n```bash exec\nfailing-command\n```\n", - }); - yield* useFailingExec(1, "command not found"); - - const execution = yield* execute({ path: "README.md", stream }); - const chunks: string[] = []; - yield* forEach(function* (chunk: string) { - chunks.push(chunk); - }, execution.output); - - const result = yield* execution; - expect(result.ok).toBe(false); - // The region completed before the documentation ran, so its text is part - // of what this run rendered — and a run that fails still hands over what it - // rendered (§6.9 Partial output). - expect(chunks.join("")).toContain("SELECTED"); - }, - ); - - journalCase( - "keeps per-segment streaming for roots without ", - "README.md", - function* (stream) { - yield* useStubFs({ - // Two rendering segments around a foreground block, whose own output - // goes to the reader rather than into the document (#441). - "README.md": "one\n\n```bash exec\necho between\n```\n\ntwo\n", - }); - yield* useStubExec(); + expect(full).toContain("ROOT_SHOWN"); + expect(chunks).toHaveLength(1); + }); - const execution = yield* execute({ path: "README.md", stream }); - const chunks: string[] = []; - yield* forEach(function* (chunk: string) { - chunks.push(chunk); - }, execution.output); + it("emits what a buffered root selected before its documentation failed", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ + "README.md": "\nSELECTED\n\n\n```bash exec\nfailing-command\n```\n", + }); + yield* useFailingExec(1, "command not found"); - expect(chunks.length).toBeGreaterThanOrEqual(2); - }, - ); - - journalCase( - "emits no output event for an empty buffered root ", - "README.md", - function* (stream) { - yield* useStubFs({ "README.md": "Root docs.\n\n\n" }); - - const execution = yield* execute({ path: "README.md", stream }); - const chunks: string[] = []; - const full = yield* forEach(function* (chunk: string) { - chunks.push(chunk); - }, execution.output); - - expect(chunks).toHaveLength(0); - expect(full).toBe(""); - }, - ); + const execution = yield* execute({ path: "README.md", stream }); + const chunks: string[] = []; + yield* forEach(function* (chunk: string) { + chunks.push(chunk); + }, execution.output); + + const result = yield* execution; + expect(result.ok).toBe(false); + // The region completed before the documentation ran, so its text is part + // of what this run rendered — and a run that fails still hands over what it + // rendered (§6.9 Partial output). + expect(chunks.join("")).toContain("SELECTED"); + }); + + it("keeps per-segment streaming for roots without ", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ + // Two rendering segments around a foreground block, whose own output + // goes to the reader rather than into the document (#441). + "README.md": "one\n\n```bash exec\necho between\n```\n\ntwo\n", + }); + yield* useStubExec(); + + const execution = yield* execute({ path: "README.md", stream }); + const chunks: string[] = []; + yield* forEach(function* (chunk: string) { + chunks.push(chunk); + }, execution.output); + + expect(chunks.length).toBeGreaterThanOrEqual(2); + }); + + it("emits no output event for an empty buffered root ", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ "README.md": "Root docs.\n\n\n" }); + + const execution = yield* execute({ path: "README.md", stream }); + const chunks: string[] = []; + const full = yield* forEach(function* (chunk: string) { + chunks.push(chunk); + }, execution.output); + + expect(chunks).toHaveLength(0); + expect(full).toBe(""); + }); it("replays a buffered root to the same result", function* () { const stream = new InMemoryStream(); From 51102a8fcc1ee49418d83d9cac0f10c5608146be Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Mon, 16 Nov 2026 22:14:37 -0500 Subject: [PATCH 04/11] Integrate #1356 shared journal consumers --- packages/acp/tests/acceptance.test.ts | 357 +++++++++--------- packages/agent/tests/agent-components.test.ts | 48 ++- packages/cli/tests/repl-execution.test.ts | 2 +- packages/cli/tests/repl-model.test.ts | 2 +- packages/cli/tests/run-composition.test.ts | 42 +-- .../cli/tests/support/repl-observations.ts | 2 +- packages/core/tests/each.test.ts | 168 ++++----- packages/core/tests/ephemeral-service.test.ts | 53 ++- .../core/tests/generated-xmd-root.test.ts | 118 +++--- .../core/tests/host-generated-root.test.ts | 148 ++++---- packages/core/tests/if.test.ts | 122 +++--- packages/core/tests/let.test.ts | 166 ++++---- packages/core/tests/loop.test.ts | 141 ++++--- packages/core/tests/sample-component.test.ts | 349 +++++++++-------- packages/core/tests/source-position.test.ts | 184 +++++---- packages/core/tests/switch.test.ts | 115 +++--- .../core/tests/validation-integration.test.ts | 171 ++++----- packages/git/tests/issue-markdown.test.ts | 2 +- packages/git/tests/support/run-composition.ts | 2 +- .../test-agent/tests/command-lazy.test.ts | 4 +- .../tests/session-configuration.test.ts | 2 +- .../testing/tests/agent-composition.test.ts | 46 ++- packages/testing/tests/assert-throws.test.ts | 6 +- packages/testing/tests/assertions.test.ts | 6 +- .../testing/tests/boundary-contract.test.ts | 7 +- .../testing/tests/execution-harness.test.ts | 2 +- packages/testing/tests/helpers.ts | 12 +- packages/testing/tests/test-component.test.ts | 6 +- .../testing/tests/testing-boundary.test.ts | 6 +- packages/testing/tests/testing-mode.test.ts | 6 +- packages/testing/tests/use-testing.test.ts | 6 +- packages/web/tests/component-support.ts | 2 +- packages/web/tests/document.test.ts | 98 +++-- 33 files changed, 1194 insertions(+), 1207 deletions(-) diff --git a/packages/acp/tests/acceptance.test.ts b/packages/acp/tests/acceptance.test.ts index 548d51f38..ee2aa8179 100644 --- a/packages/acp/tests/acceptance.test.ts +++ b/packages/acp/tests/acceptance.test.ts @@ -3,7 +3,7 @@ * `rootProvider` seam drives the full Agent → session → prompt → teardown * lifecycle. Fake ACPX runtime, no subprocess. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { ensure, scoped } from "effection"; import { ensureDir, rm, writeTextFile } from "@effectionx/fs"; @@ -14,7 +14,7 @@ import { Agent } from "@executablemd/agent/api"; import { agentIdentityComponents, installAgentComponents } from "@executablemd/agent/host"; import { executeInstalled } from "@executablemd/core/host"; import type { AgentSessionRequest } from "@executablemd/agent/api"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; import { createAcpxProvider } from "../mod.ts"; import { createFakeRuntime, makeRegistry, makeStore, useFlatWorld } from "./helpers.ts"; @@ -30,6 +30,7 @@ const DOC = [ ].join("\n"); describe("Tier XA — ACPX provider through the rootProvider seam", () => { + beforeEach(journalBeforeEach(OWNER)); const lifecycle = "XA1: agent availability → session → normalized prompt → structured teardown"; it(lifecycle, function* () { const harness = createFakeRuntime(); @@ -40,47 +41,46 @@ describe("Tier XA — ACPX provider through the rootProvider seam", () => { const docPath = path.join(dir, "doc.md"); yield* writeTextFile(docPath, DOC); - yield* captureTestJournal({ owner: OWNER, name: lifecycle }, docPath, function* (stream) { - const result = yield* scoped(function* () { - yield* useFlatWorld(CWD); - yield* installAgentComponents({ - rootProvider: { - factory: createAcpxProvider({ - createRuntime: harness.create, - sessionStore: makeStore(), - agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), - }), - options: { defaultAgent: "gemini", permissionMode: "deny-all" }, - }, - }); - - // `` names durable work after its own invocation, so the host - // declares it to the execution rather than registering it. - const execution = yield* executeInstalled({ path: docPath, stream }, [ - { components: agentIdentityComponents() }, - ]); - const subscription = yield* execution.output; - let next = yield* subscription.next(); - while (!next.done) { - next = yield* subscription.next(); - } - const output = next.value; - const outcome = yield* execution; - // Structured teardown: the handle is closed by the time the - // DocumentExecution completion settles (bridgeRootProvider resolves - // completion only after provider finalizers run). - const closedByCompletion = harness.closeCalls.length; - return { output, outcome, closedByCompletion }; + const stream = yield* useJournalStream(docPath); + const result = yield* scoped(function* () { + yield* useFlatWorld(CWD); + yield* installAgentComponents({ + rootProvider: { + factory: createAcpxProvider({ + createRuntime: harness.create, + sessionStore: makeStore(), + agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), + }), + options: { defaultAgent: "gemini", permissionMode: "deny-all" }, + }, }); - expect(harness.doctorCalls).toBeGreaterThan(0); - expect(harness.ensureCalls.length).toBeGreaterThan(0); - // Normalized prompt output — output-stream deltas only (thought hidden). - expect(result.output).toContain("hello world"); - expect(result.outcome.ok).toBe(true); - // Handle closed before completion settled. - expect(result.closedByCompletion).toBeGreaterThan(0); + // `` names durable work after its own invocation, so the host + // declares it to the execution rather than registering it. + const execution = yield* executeInstalled({ path: docPath, stream }, [ + { components: agentIdentityComponents() }, + ]); + const subscription = yield* execution.output; + let next = yield* subscription.next(); + while (!next.done) { + next = yield* subscription.next(); + } + const output = next.value; + const outcome = yield* execution; + // Structured teardown: the handle is closed by the time the + // DocumentExecution completion settles (bridgeRootProvider resolves + // completion only after provider finalizers run). + const closedByCompletion = harness.closeCalls.length; + return { output, outcome, closedByCompletion }; }); + + expect(harness.doctorCalls).toBeGreaterThan(0); + expect(harness.ensureCalls.length).toBeGreaterThan(0); + // Normalized prompt output — output-stream deltas only (thought hidden). + expect(result.output).toContain("hello world"); + expect(result.outcome.ok).toBe(true); + // Handle closed before completion settled. + expect(result.closedByCompletion).toBeGreaterThan(0); }); }); @@ -93,6 +93,7 @@ describe("Tier XA — ACPX provider through the rootProvider seam", () => { * through the coordinator delivered to this provider. */ describe("Tier WAP — same-named sibling Sessions", () => { + beforeEach(journalBeforeEach(OWNER)); const siblings = 'WAP7: two sibling sites name two identities the middleware cannot touch'; it(siblings, function* () { @@ -121,85 +122,84 @@ describe("Tier WAP — same-named sibling Sessions", () => { ].join("\n"), ); - yield* captureTestJournal({ owner: OWNER, name: siblings }, docPath, function* (stream) { - yield* scoped(function* () { - yield* useFlatWorld(CWD); - yield* installAgentComponents({ - rootProvider: { - factory: createAcpxProvider({ - createRuntime: harness.create, - sessionStore: makeStore(), - agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), - sessions: { - // deno-lint-ignore require-yield - *requestSession(request) { - requests.push({ - session: request.session, - ...(request.sessionIdentity === undefined - ? {} - : { sessionIdentity: request.sessionIdentity }), - }); - return { - sessionKey: `session:${request.sessionIdentity ?? "none"}`, - cwd: "/sessions", - state: "new", - }; - }, - // A host that decides where sessions live retains what stands - // there. This one keeps the keys it was asked to retain, in - // order, which is how the test sees that each request became its - // own confirmed session rather than one shared by the name. - // deno-lint-ignore require-yield - *recordSessionIdentity(details) { - recorded.push(details.sessionKey); - }, + const stream = yield* useJournalStream(docPath); + yield* scoped(function* () { + yield* useFlatWorld(CWD); + yield* installAgentComponents({ + rootProvider: { + factory: createAcpxProvider({ + createRuntime: harness.create, + sessionStore: makeStore(), + agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), + sessions: { + // deno-lint-ignore require-yield + *requestSession(request) { + requests.push({ + session: request.session, + ...(request.sessionIdentity === undefined + ? {} + : { sessionIdentity: request.sessionIdentity }), + }); + return { + sessionKey: `session:${request.sessionIdentity ?? "none"}`, + cwd: "/sessions", + state: "new", + }; }, - }), - options: { defaultAgent: "gemini", permissionMode: "deny-all" }, - }, - }); - // Middleware that rewrites the descriptive name on its way through, which - // is exactly what the public chain is for. - yield* Agent.around({ - *session([routed], next) { - const renamed = - typeof routed === "string" || routed === undefined - ? "rewritten" - : routed.with({ name: "rewritten" }); - return yield* next(renamed); - }, - }); - - // `` names durable work after its own invocation, so the host - // declares it to the execution rather than registering it. - const execution = yield* executeInstalled({ path: docPath, stream }, [ - { components: agentIdentityComponents() }, - ]); - const subscription = yield* execution.output; - let next = yield* subscription.next(); - while (!next.done) { - next = yield* subscription.next(); - } - yield* execution; + // A host that decides where sessions live retains what stands + // there. This one keeps the keys it was asked to retain, in + // order, which is how the test sees that each request became its + // own confirmed session rather than one shared by the name. + // deno-lint-ignore require-yield + *recordSessionIdentity(details) { + recorded.push(details.sessionKey); + }, + }, + }), + options: { defaultAgent: "gemini", permissionMode: "deny-all" }, + }, + }); + // Middleware that rewrites the descriptive name on its way through, which + // is exactly what the public chain is for. + yield* Agent.around({ + *session([routed], next) { + const renamed = + typeof routed === "string" || routed === undefined + ? "rewritten" + : routed.with({ name: "rewritten" }); + return yield* next(renamed); + }, }); - expect(requests).toHaveLength(2); - // The middleware's rewrite reached the provider, because the name is the - // compositional half. - expect(requests.map((request) => request.session)).toEqual(["rewritten", "rewritten"]); - // The identities did not, and are two: same authored name, two elements, - // two sessions. - const identities = requests.map((request) => request.sessionIdentity); - expect(identities.every((identity) => typeof identity === "string")).toBe(true); - expect(new Set(identities).size).toBe(2); - expect(identities).not.toContain("rewritten"); - // And each request is its own session, not one shared by the name. - expect(new Set(harness.ensureCalls.map((call) => call.sessionKey)).size).toBe(2); - // The host was asked to retain both, in the order they were confirmed. A - // provider that skipped a host with nothing to skip would show up here as - // an empty list. - expect(recorded).toEqual(requests.map((request) => `session:${request.sessionIdentity}`)); + // `` names durable work after its own invocation, so the host + // declares it to the execution rather than registering it. + const execution = yield* executeInstalled({ path: docPath, stream }, [ + { components: agentIdentityComponents() }, + ]); + const subscription = yield* execution.output; + let next = yield* subscription.next(); + while (!next.done) { + next = yield* subscription.next(); + } + yield* execution; }); + + expect(requests).toHaveLength(2); + // The middleware's rewrite reached the provider, because the name is the + // compositional half. + expect(requests.map((request) => request.session)).toEqual(["rewritten", "rewritten"]); + // The identities did not, and are two: same authored name, two elements, + // two sessions. + const identities = requests.map((request) => request.sessionIdentity); + expect(identities.every((identity) => typeof identity === "string")).toBe(true); + expect(new Set(identities).size).toBe(2); + expect(identities).not.toContain("rewritten"); + // And each request is its own session, not one shared by the name. + expect(new Set(harness.ensureCalls.map((call) => call.sessionKey)).size).toBe(2); + // The host was asked to retain both, in the order they were confirmed. A + // provider that skipped a host with nothing to skip would show up here as + // an empty list. + expect(recorded).toEqual(requests.map((request) => `session:${request.sessionIdentity}`)); }); const retainedRequest = @@ -231,74 +231,73 @@ describe("Tier WAP — same-named sibling Sessions", () => { ].join("\n"), ); - yield* captureTestJournal({ owner: OWNER, name: retainedRequest }, docPath, function* (stream) { - yield* scoped(function* () { - yield* useFlatWorld(CWD); - yield* installAgentComponents({ - rootProvider: { - factory: createAcpxProvider({ - createRuntime: harness.create, - sessionStore: makeStore(), - agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), - sessions: { - // deno-lint-ignore require-yield - *requestSession(request) { - requests.push(request.sessionIdentity); - return { - sessionKey: `session:${request.sessionIdentity ?? "none"}`, - cwd: "/sessions", - state: "new", - }; - }, - // deno-lint-ignore require-yield - *recordSessionIdentity(details) { - recorded.push(details.sessionKey); - }, + const stream = yield* useJournalStream(docPath); + yield* scoped(function* () { + yield* useFlatWorld(CWD); + yield* installAgentComponents({ + rootProvider: { + factory: createAcpxProvider({ + createRuntime: harness.create, + sessionStore: makeStore(), + agentRegistry: makeRegistry({ gemini: "gemini-cmd" }), + sessions: { + // deno-lint-ignore require-yield + *requestSession(request) { + requests.push(request.sessionIdentity); + return { + sessionKey: `session:${request.sessionIdentity ?? "none"}`, + cwd: "/sessions", + state: "new", + }; }, - }), - options: { defaultAgent: "gemini", permissionMode: "deny-all" }, - }, - }); - - // The attack: keep the first element's real request and route it for - // the second. Reading it again would answer with the first element's - // identity, and both sessions would collapse into one. - let kept: string | AgentSessionRequest | undefined; - let keeping = false; - yield* Agent.around({ - *session([routed], next) { - if (!keeping) { - keeping = true; - kept = routed; - return yield* next(routed); - } - return yield* next(kept); - }, - }); + // deno-lint-ignore require-yield + *recordSessionIdentity(details) { + recorded.push(details.sessionKey); + }, + }, + }), + options: { defaultAgent: "gemini", permissionMode: "deny-all" }, + }, + }); - // `` names durable work after its own invocation, so the host - // declares it to the execution rather than registering it. - const execution = yield* executeInstalled({ path: docPath, stream }, [ - { components: agentIdentityComponents() }, - ]); - const subscription = yield* execution.output; - let next = yield* subscription.next(); - while (!next.done) { - next = yield* subscription.next(); - } - const outcome = yield* execution; - if (!outcome.ok) { - refusal = outcome.error.message; - } + // The attack: keep the first element's real request and route it for + // the second. Reading it again would answer with the first element's + // identity, and both sessions would collapse into one. + let kept: string | AgentSessionRequest | undefined; + let keeping = false; + yield* Agent.around({ + *session([routed], next) { + if (!keeping) { + keeping = true; + kept = routed; + return yield* next(routed); + } + return yield* next(kept); + }, }); - // Refused before the provider named anything for the second element: the - // issuance the first one opened closed when that element finished. - expect(refusal).toContain("already resolved its session"); - expect(requests).toHaveLength(1); - // One session was confirmed, so one identity was retained — the refused - // element contributed no mapping of its own. - expect(recorded).toEqual([`session:${requests[0]}`]); + // `` names durable work after its own invocation, so the host + // declares it to the execution rather than registering it. + const execution = yield* executeInstalled({ path: docPath, stream }, [ + { components: agentIdentityComponents() }, + ]); + const subscription = yield* execution.output; + let next = yield* subscription.next(); + while (!next.done) { + next = yield* subscription.next(); + } + const outcome = yield* execution; + if (!outcome.ok) { + refusal = outcome.error.message; + } }); + + // Refused before the provider named anything for the second element: the + // issuance the first one opened closed when that element finished. + expect(refusal).toContain("already resolved its session"); + expect(requests).toHaveLength(1); + // One session was confirmed, so one identity was retained — the refused + // element contributed no mapping of its own. + expect(recorded).toEqual([`session:${requests[0]}`]); }); }); diff --git a/packages/agent/tests/agent-components.test.ts b/packages/agent/tests/agent-components.test.ts index 292ad70f8..d3f721c6e 100644 --- a/packages/agent/tests/agent-components.test.ts +++ b/packages/agent/tests/agent-components.test.ts @@ -7,7 +7,7 @@ * `installAgentComponents`'s `rootProvider` seam — no real provider, * subprocess, or provider selection. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { InMemoryStream } from "@executablemd/durable-streams"; import { ensure, scoped } from "effection"; @@ -28,7 +28,9 @@ import type { AgentProviderFactory } from "../src/provider-api.ts"; import { installAgentComponents } from "../src/components.ts"; import { Component } from "../../core/src/component-api.ts"; import type { Json } from "@executablemd/core"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +const OWNER = "packages/agent/tests/agent-components.test.ts"; interface StubResponse { status?: "completed" | "failed" | "cancelled"; @@ -103,7 +105,11 @@ function createStubStream( sessionKey: `stub:${typeof options?.session === "string" ? options.session : "default"}`, cwd: "/stub", }; - const call: StubPromptCall = { content, agent, sessionKey: session.sessionKey }; + const call: StubPromptCall = { + content, + agent, + sessionKey: session.sessionKey, + }; if (options?.timeout !== undefined) { call.timeout = options.timeout; } @@ -147,7 +153,7 @@ function* installStub(stub: Stub): Operation { function* runDoc( doc: string, - stream: InMemoryStream, + providedStream?: InMemoryStream, dir = path.join(os.tmpdir(), `xmd-ac-test-${randomUUID()}`), ): Operation<{ output: string; result: Result }> { yield* ensureDir(dir); @@ -156,6 +162,7 @@ function* runDoc( const docPath = path.join(dir, "doc.md"); yield* writeTextFile(docPath, doc); + const stream = providedStream ?? (yield* useJournalStream(docPath)); // `` names durable work after its own invocation, so the host // declares it to the execution rather than registering it. const execution = yield* executeInstalled({ path: docPath, stream }, [ @@ -177,17 +184,13 @@ function agentCase( name: string, body: (run: (doc: string) => ReturnType) => Operation, ): void { - it(name, () => { - const dir = path.join(os.tmpdir(), `xmd-ac-test-${randomUUID()}`); - return captureTestJournal( - { owner: "packages/agent/tests/agent-components.test.ts", suite: SUITE, name }, - path.join(dir, "doc.md"), - (stream) => body((doc) => runDoc(doc, stream, dir)), - ); + it(name, function* () { + yield* body((doc) => runDoc(doc)); }); } describe(SUITE, () => { + beforeEach(journalBeforeEach(OWNER)); agentCase( "AC1: wrapper always sends rendered children, untrimmed", function* (runFresh) { @@ -358,7 +361,11 @@ describe(SUITE, () => { function* (runFresh) { const stub = createStubProvider((content) => { if (content.includes("first-fail")) { - return { status: "failed", stopReason: "max_tokens", deltas: ["partial-one "] }; + return { + status: "failed", + stopReason: "max_tokens", + deltas: ["partial-one "], + }; } if (content.includes("second-fail")) { return { status: "failed", stopReason: "refusal", deltas: [] }; @@ -399,7 +406,10 @@ describe(SUITE, () => { ); agentCase("AC8: throwOnError aborts the document immediately", function* (runFresh) { - const stub = createStubProvider(() => ({ status: "failed", stopReason: "refusal" })); + const stub = createStubProvider(() => ({ + status: "failed", + stopReason: "refusal", + })); yield* installStub(stub); const { output, result } = yield* runFresh( '\n\nnever reached\n', @@ -532,7 +542,11 @@ describe(SUITE, () => { yield* stream.append({ type: "yield", coroutineId: "root.0", - description: { type: "agent_prompt", name: "prompt:doc.md:1:1#0", input: "a" }, + description: { + type: "agent_prompt", + name: "prompt:doc.md:1:1#0", + input: "a", + }, result: { status: "ok", value: { @@ -550,7 +564,11 @@ describe(SUITE, () => { yield* stream.append({ type: "yield", coroutineId: "root.1", - description: { type: "agent_prompt", name: "prompt:doc.md:2:1#0", input: "b" }, + description: { + type: "agent_prompt", + name: "prompt:doc.md:2:1#0", + input: "b", + }, result: { status: "ok", value: { diff --git a/packages/cli/tests/repl-execution.test.ts b/packages/cli/tests/repl-execution.test.ts index 9d3ef1335..ebfdc7497 100644 --- a/packages/cli/tests/repl-execution.test.ts +++ b/packages/cli/tests/repl-execution.test.ts @@ -29,7 +29,7 @@ import { join } from "node:path"; import process from "node:process"; import { Writable } from "node:stream"; import { partitionEntrySegments, ROOT_IMPORT } from "../src/repl/entries.ts"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; import type { DeliverySink } from "../src/stdout-delivery.ts"; import { useTempDirectory } from "@executablemd/test-support/temp"; diff --git a/packages/cli/tests/repl-model.test.ts b/packages/cli/tests/repl-model.test.ts index ab73ba344..01e71df20 100644 --- a/packages/cli/tests/repl-model.test.ts +++ b/packages/cli/tests/repl-model.test.ts @@ -19,7 +19,7 @@ import { * binding in a scope that never published it. */ -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { Component, useTempFileCompiler } from "@executablemd/core"; diff --git a/packages/cli/tests/run-composition.test.ts b/packages/cli/tests/run-composition.test.ts index ace09a8a7..586e82b89 100644 --- a/packages/cli/tests/run-composition.test.ts +++ b/packages/cli/tests/run-composition.test.ts @@ -18,7 +18,7 @@ * still describes and resolves the whole language. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { scoped, suspend, type Operation } from "effection"; import { InMemoryStream } from "@executablemd/durable-streams"; @@ -27,6 +27,7 @@ import { API, Service, useHostFiles } from "@executablemd/runtime"; import type { RuntimeFetchResponse } from "@executablemd/runtime"; import { ensureDir, exists, readdir, readTextFile, writeTextFile } from "@effectionx/fs"; import { runCli } from "@executablemd/test-support/launch"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; import { useTempDirectory } from "@executablemd/test-support/temp"; import { join } from "node:path"; import { COMPOSITION_REGISTRATIONS } from "@executablemd/git"; @@ -35,9 +36,6 @@ import { installPlugins } from "../src/plugin-host.ts"; import type { CommandPlugins } from "../src/plugin-host.ts"; import { BUNDLED_PLUGIN } from "../src/run-profile.ts"; import { DEFAULT_REPOSITORY_ROOT } from "../src/run-repositories.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; - -const journalCase = journalCases("packages/cli/tests/run-composition.test.ts"); /** Every element an author can write that needs a repository provider. */ const OPERATIONS: readonly { readonly name: string; readonly source: string }[] = [ @@ -131,6 +129,7 @@ function* gitProfile(command: string): Operation { } describe("ORC1 — describing the vocabulary reaches nothing", () => { + beforeEach(journalBeforeEach("packages/cli/tests/run-composition.test.ts")); it("builds the catalog without a subprocess, a service, a request or a lock", function* () { const managed = yield* useTempDirectory("xmd-orc1-managed-"); const reached: string[] = []; @@ -190,25 +189,22 @@ describe("ORC1 — describing the vocabulary reaches nothing", () => { expect(yield* readdir(managed)).toEqual([]); }); - journalCase( - "leaves every repository operation unprovided after inspection", - "", - function* (stream) { - // Registering the declarations installs no provider: the Apis still answer - // with their own defaults, which is what a catalog is allowed to leave - // behind. - // Assembled rather than bootstrapped: the vocabulary belongs to the - // bundled Plugin, and installing it is what a run profile does. Installing - // it still installs no provider, which is the claim. - yield* gitProfile("run"); - const failure = yield* raisedValue( - collect(yield* execute({ ...inlineSource(``), stream })), - ); - // `` has no lexical Repository, so the first thing it asks for - // is the ambient one — and that is the Api reporting absence. - expect(String(failure)).toContain("no Repository composition provider is installed"); - }, - ); + it("leaves every repository operation unprovided after inspection", function* () { + const stream = yield* useJournalStream(""); + // Registering the declarations installs no provider: the Apis still answer + // with their own defaults, which is what a catalog is allowed to leave + // behind. + // Assembled rather than bootstrapped: the vocabulary belongs to the + // bundled Plugin, and installing it is what a run profile does. Installing + // it still installs no provider, which is the claim. + yield* gitProfile("run"); + const failure = yield* raisedValue( + collect(yield* execute({ ...inlineSource(``), stream })), + ); + // `` has no lexical Repository, so the first thing it asks for + // is the ambient one — and that is the Api reporting absence. + expect(String(failure)).toContain("no Repository composition provider is installed"); + }); }); describe("ORC2 — one language, described everywhere and operated somewhere", () => { diff --git a/packages/cli/tests/support/repl-observations.ts b/packages/cli/tests/support/repl-observations.ts index f2dbd02eb..314669bad 100644 --- a/packages/cli/tests/support/repl-observations.ts +++ b/packages/cli/tests/support/repl-observations.ts @@ -42,7 +42,7 @@ import { useCompositionComponents } from "@executablemd/git"; import { useReplRepositoryRecording } from "../../src/repl/repositories/deno/provider.ts"; import { admitReplRepositoryHistory } from "../../src/repl/repositories/admission.ts"; import type { ObservationFixture } from "../../../git/tests/support/observations.ts"; -import { captureTestJournal } from "../../../test-support/failure-journal.ts"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; import type { JournalCase } from "../../src/test-diagnostics.ts"; /** What the repository handle beneath one entry was asked to do. */ diff --git a/packages/core/tests/each.test.ts b/packages/core/tests/each.test.ts index c3ec7923f..1e50fa470 100644 --- a/packages/core/tests/each.test.ts +++ b/packages/core/tests/each.test.ts @@ -1,4 +1,4 @@ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { describe, it, beforeAll, beforeEach } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; import { scoped } from "effection"; @@ -11,9 +11,10 @@ import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import type { CodeBlockResult, Segment } from "../src/types.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; - -const journalCase = journalCases("packages/core/tests/each.test.ts"); +import { + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; interface EachRun { segments: Segment[]; @@ -219,33 +220,32 @@ describe("Tier Each — native iteration directive", () => { describe("Tier Each — eval in the loop body", () => { beforeAll(() => useTempFileCompiler()); - journalCase( - "E18: a body eval block reads the current item binding", - "test.md", - function* (stream) { - yield* useStubFs({ - "test.md": [ - "```js eval", - "const items = [{ name: 'alpha' }, { name: 'beta' }];", - "```", - '', - "```js eval", - "output('got:' + item.name);", - "```", - "", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("got:alpha"); - expect(output).toContain("got:beta"); - expect(output).not.toContain("ERROR"); - }, - ); - - journalCase("E19: renders a table row per item end to end", "test.md", function* (stream) { + beforeEach(journalBeforeEach("packages/core/tests/each.test.ts")); + it("E18: a body eval block reads the current item binding", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "test.md": [ + "```js eval", + "const items = [{ name: 'alpha' }, { name: 'beta' }];", + "```", + '', + "```js eval", + "output('got:' + item.name);", + "```", + "", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).toContain("got:alpha"); + expect(output).toContain("got:beta"); + expect(output).not.toContain("ERROR"); + }); + + it("E19: renders a table row per item end to end", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": [ "```js eval", @@ -265,60 +265,54 @@ describe("Tier Each — eval in the loop body", () => { expect(output).not.toContain("ERROR"); }); - journalCase( - "E22: each iteration's eval env is fresh — body bindings do not carry over", - "test.md", - function* (stream) { - yield* useStubFs({ - "test.md": [ - "```js eval", - "const items = [1, 2];", - "```", - '', - "```js eval", - "output('iter' + n + ':' + ('carry' in env ? env.carry : 'absent'));", - "env.carry = 'from-' + n;", - "```", - "", - "```js eval", - "output('final:' + (typeof carry === 'undefined' ? 'absent' : carry));", - "```", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("iter1:absent"); - expect(output).toContain("iter2:absent"); - expect(output).toContain("final:absent"); - expect(output).not.toContain("ERROR"); - }, - ); - - journalCase( - "E21: an projected through resolves caller bindings", - "test.md", - function* (stream) { - yield* useStubFs({ - "components/Wrap.md": "", - "test.md": [ - "```js eval", - "const items = [{ n: 1 }, { n: 2 }];", - "const label = 'L';", - "```", - "", - '[{label}:{item.n}]', - "", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("[L:1]"); - expect(output).toContain("[L:2]"); - expect(output).not.toContain("ERROR"); - }, - ); + it("E22: each iteration's eval env is fresh — body bindings do not carry over", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "test.md": [ + "```js eval", + "const items = [1, 2];", + "```", + '', + "```js eval", + "output('iter' + n + ':' + ('carry' in env ? env.carry : 'absent'));", + "env.carry = 'from-' + n;", + "```", + "", + "```js eval", + "output('final:' + (typeof carry === 'undefined' ? 'absent' : carry));", + "```", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).toContain("iter1:absent"); + expect(output).toContain("iter2:absent"); + expect(output).toContain("final:absent"); + expect(output).not.toContain("ERROR"); + }); + + it("E21: an projected through resolves caller bindings", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "components/Wrap.md": "", + "test.md": [ + "```js eval", + "const items = [{ n: 1 }, { n: 2 }];", + "const label = 'L';", + "```", + "", + '[{label}:{item.n}]', + "", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).toContain("[L:1]"); + expect(output).toContain("[L:2]"); + expect(output).not.toContain("ERROR"); + }); }); diff --git a/packages/core/tests/ephemeral-service.test.ts b/packages/core/tests/ephemeral-service.test.ts index f83cf8132..97f90594b 100644 --- a/packages/core/tests/ephemeral-service.test.ts +++ b/packages/core/tests/ephemeral-service.test.ts @@ -1,4 +1,4 @@ -import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; +import { beforeAll, beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { resource, scoped, type Operation } from "effection"; import { @@ -17,9 +17,10 @@ import { collect } from "../src/collect.ts"; import { execute } from "../src/execute.ts"; import { LiveBindingCollisionError } from "../src/live-env.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; - -const journalCase = journalCases("packages/core/tests/ephemeral-service.test.ts"); +import { + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; const SAMPLE = `--- meta: @@ -110,6 +111,7 @@ function serialized(stream: InMemoryStream): string { } describe("ephemeral eval and attached-service bindings", () => { + beforeEach(journalBeforeEach("packages/core/tests/ephemeral-service.test.ts")); beforeAll(() => useTempFileCompiler()); it("reconstructs live bindings without exposing them to durable consumers", function* () { @@ -551,13 +553,11 @@ const shared = "must-not-execute"; ).toHaveLength(1); }); - journalCase( - "allows a later ephemeral eval to update an existing live binding", - "doc.md", - function* (stream) { - yield* useStubFs({ - "doc.md": `\n`, - "components/Provider.md": `--- + it("allows a later ephemeral eval to update an existing live binding", function* () { + const stream = yield* useJournalStream("doc.md"); + yield* useStubFs({ + "doc.md": `\n`, + "components/Provider.md": `--- meta: componentName: Provider --- @@ -579,22 +579,21 @@ yield* Sample.around({ `, - "components/Sample.md": SAMPLE, - }); + "components/Sample.md": SAMPLE, + }); - const output = String( - yield* scoped(function* () { - return yield* collect( - yield* execute({ - path: "doc.md", - stream, - includes: ["components", "."], - }), - ); - }), - ); + const output = String( + yield* scoped(function* () { + return yield* collect( + yield* execute({ + path: "doc.md", + stream, + includes: ["components", "."], + }), + ); + }), + ); - expect(output).toContain("second"); - }, - ); + expect(output).toContain("second"); + }); }); diff --git a/packages/core/tests/generated-xmd-root.test.ts b/packages/core/tests/generated-xmd-root.test.ts index 3939cf2de..915166c09 100644 --- a/packages/core/tests/generated-xmd-root.test.ts +++ b/packages/core/tests/generated-xmd-root.test.ts @@ -28,8 +28,12 @@ * stays readable and resumable as what it is. */ -import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; +import { beforeAll, describe, it, beforeEach } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; +import { + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; import { Err, Ok, scoped } from "effection"; import type { Operation, Result } from "effection"; import { API } from "@executablemd/runtime"; @@ -52,7 +56,7 @@ import { Documentation } from "../src/documentation-api.ts"; import { Execution } from "../src/execute.ts"; import { retainedSource } from "../src/root-source.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; -import { heldHostCases, HELD_ROOT_PATH, withHeldHost } from "./support/held-host.ts"; +import { HELD_ROOT_PATH, withHeldHost } from "./support/held-host.ts"; // Imported from the module rather than the host surface: there is no public // siteless evaluator, and these pinned factories are core-internal — the same // import main's own generated-xmd tier uses. @@ -73,7 +77,6 @@ import type { } from "../src/generated-xmd.ts"; import type { FunctionComponent, FunctionComponentDefinition, Json } from "../src/types.ts"; -const heldHostCase = heldHostCases("packages/core/tests/generated-xmd-root.test.ts"); const ROOT_PATH = "repl/sidekick.md"; const ROOT_SOURCE = "The host evaluated a generated root.\n"; @@ -1768,60 +1771,59 @@ describe("993 generated nested roots", () => { }); describe("1178 generated root references and scope", () => { + beforeEach(journalBeforeEach("packages/core/tests/generated-xmd-root.test.ts")); beforeAll(() => useTempFileCompiler()); - heldHostCase( - "keeps root engine availability local across sibling roots and restores enclosing installed syntax", - function* (stream) { - const calls: string[] = []; - const names = ["If", "Output", "Content", "Return", "Banner", "BannerLine"]; - const named = ``; - const candidate = request(`${named}1178-ROOT`); - const run = yield* withHeldHost( - { - stream, - installations: [candidate.installation, syntaxInstallation(calls)], - root: { path: HELD_ROOT_PATH, source: `${named}` }, - }, - function* (held) { - const first = yield* held.execution.evaluateGeneratedXmdRoot(candidate.root); - if (!first.ok) { - throw first.error; - } - expect(first.value.output).toContain("1178-ROOT"); - for (const name of ["If", "Output"]) { - expectAvailability(first.value.output, name, true); - } - for (const name of ["Content", "Return", "Banner", "BannerLine"]) { - expectAvailability(first.value.output, name, false); - } - const second = yield* held.execution.evaluateGeneratedXmdRoot({ - id: "turn-2", - source: `{before}1178-CUT{reference}1178-CUT{after}`, - }); - if (!second.ok) { - throw second.error; - } - const [before, reference, after] = second.value.output.split("1178-CUT"); - expect(before).toBe(baseline1178["root-base.txt"]); - expect(after).toBe(baseline1178["root-base.txt"]); - expectAvailability(reference, "If", true); - const third = yield* held.execution.evaluateGeneratedXmdRoot({ - id: "turn-3", - source: "", - }); - if (!third.ok) { - throw third.error; - } - expect(third.value.output).toBe(baseline1178["root-base.txt"]); - }, - ); - if (!run.completion.ok) { - throw run.completion.error; - } - for (const name of names) { - expectAvailability(String(run.completion.value), name, true); - } - expect(calls).toEqual([]); - }, - ); + it("keeps root engine availability local across sibling roots and restores enclosing installed syntax", function* () { + const stream = yield* useJournalStream(HELD_ROOT_PATH); + const calls: string[] = []; + const names = ["If", "Output", "Content", "Return", "Banner", "BannerLine"]; + const named = ``; + const candidate = request(`${named}1178-ROOT`); + const run = yield* withHeldHost( + { + stream, + installations: [candidate.installation, syntaxInstallation(calls)], + root: { path: HELD_ROOT_PATH, source: `${named}` }, + }, + function* (held) { + const first = yield* held.execution.evaluateGeneratedXmdRoot(candidate.root); + if (!first.ok) { + throw first.error; + } + expect(first.value.output).toContain("1178-ROOT"); + for (const name of ["If", "Output"]) { + expectAvailability(first.value.output, name, true); + } + for (const name of ["Content", "Return", "Banner", "BannerLine"]) { + expectAvailability(first.value.output, name, false); + } + const second = yield* held.execution.evaluateGeneratedXmdRoot({ + id: "turn-2", + source: `{before}1178-CUT{reference}1178-CUT{after}`, + }); + if (!second.ok) { + throw second.error; + } + const [before, reference, after] = second.value.output.split("1178-CUT"); + expect(before).toBe(baseline1178["root-base.txt"]); + expect(after).toBe(baseline1178["root-base.txt"]); + expectAvailability(reference, "If", true); + const third = yield* held.execution.evaluateGeneratedXmdRoot({ + id: "turn-3", + source: "", + }); + if (!third.ok) { + throw third.error; + } + expect(third.value.output).toBe(baseline1178["root-base.txt"]); + }, + ); + if (!run.completion.ok) { + throw run.completion.error; + } + for (const name of names) { + expectAvailability(String(run.completion.value), name, true); + } + expect(calls).toEqual([]); + }); }); diff --git a/packages/core/tests/host-generated-root.test.ts b/packages/core/tests/host-generated-root.test.ts index c1a2d780e..0f152099b 100644 --- a/packages/core/tests/host-generated-root.test.ts +++ b/packages/core/tests/host-generated-root.test.ts @@ -31,7 +31,7 @@ import { Component } from "../mod.ts"; * work it started has finished unwinding. */ -import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; +import { beforeAll, beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { ensure, race, scoped, sleep, spawn, suspend, useScope, withResolvers } from "effection"; import { forEach } from "@effectionx/stream-helpers"; @@ -56,7 +56,11 @@ import { join } from "node:path"; import { execPath } from "node:process"; import { fileURLToPath } from "node:url"; import { cliRuntime, runShell, shellQuote } from "@executablemd/test-support/launch"; -import { captureTestJournal } from "@executablemd/test-support/failure-journal"; +import { + captureTestJournal, + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; import { contributeDocumentation, Documentation } from "../src/documentation-api.ts"; import { registerComponents } from "../src/components/registration.ts"; @@ -682,6 +686,7 @@ function markerPackage( const MARKER_PROSE = "## Marker\n\nTHE MARKER CHAPTER.\n"; describe("Tier E3 — the documentation this execution captured", () => { + beforeEach(journalBeforeEach("packages/core/tests/host-generated-root.test.ts")); beforeAll(() => useTempFileCompiler()); it("E3a: a named lookup renders a package chapter the root does not admit", function* () { @@ -732,55 +737,53 @@ describe("Tier E3 — the documentation this execution captured", () => { expect(counted.reads).toBe(1); }); - heldHostCase( - "E3b: late Documentation middleware in the document's own scope changes nothing", - function* (stream) { - const counted = { reads: 0 }; - const invoked: string[] = []; - const answer: { lookup?: Result } = {}; - - // Installed in an `Execution.document` wrapper: after canonical capture and - // before the document workflow, which is the scope the evaluation runner is - // attached inside. A recollection there *would* encounter this, so the row - // distinguishes a captured reference from one rebuilt at evaluation time. - // Middleware installed only in a sibling caller scope could not. - yield* Execution.around({ - *document([claimed], next) { - yield* Documentation.around({ - *contributions([read], nextChapter) { - const held = yield* nextChapter(read); - return held.map((one) => - one.source.owner === "@executablemd/test" - ? { - ...one, - source: { ...one.source, text: "## Marker\n\nSUBSTITUTED LATE.\n" }, - } - : one, - ); - }, - }); - yield* next(claimed); - }, - }); + it("E3b: late Documentation middleware in the document's own scope changes nothing", function* () { + const stream = yield* useJournalStream(HELD_ROOT_PATH); + const counted = { reads: 0 }; + const invoked: string[] = []; + const answer: { lookup?: Result } = {}; - const run = yield* withHeldHost( - { stream, installations: [markerPackage(MARKER_PROSE, counted), selecting([probe()])] }, - function* (open) { - answer.lookup = yield* open.execution.evaluateGeneratedXmdRoot( - request('\n\n\n', invoked), - ); - }, - ); + // Installed in an `Execution.document` wrapper: after canonical capture and + // before the document workflow, which is the scope the evaluation runner is + // attached inside. A recollection there *would* encounter this, so the row + // distinguishes a captured reference from one rebuilt at evaluation time. + // Middleware installed only in a sibling caller scope could not. + yield* Execution.around({ + *document([claimed], next) { + yield* Documentation.around({ + *contributions([read], nextChapter) { + const held = yield* nextChapter(read); + return held.map((one) => + one.source.owner === "@executablemd/test" + ? { + ...one, + source: { ...one.source, text: "## Marker\n\nSUBSTITUTED LATE.\n" }, + } + : one, + ); + }, + }); + yield* next(claimed); + }, + }); - expect(run.completion.ok).toBe(true); - const described = rendered(answer.lookup ?? { ok: false, error: new Error("none") }).output; - expect(described).toContain("THE MARKER CHAPTER."); - expect(described).not.toContain("SUBSTITUTED LATE."); - // No new collection happened, which is the mechanism rather than the - // symptom: the count is the same before and after the evaluation. - expect(counted.reads).toBe(1); - }, - ); + const run = yield* withHeldHost( + { stream, installations: [markerPackage(MARKER_PROSE, counted), selecting([probe()])] }, + function* (open) { + answer.lookup = yield* open.execution.evaluateGeneratedXmdRoot( + request('\n\n\n', invoked), + ); + }, + ); + + expect(run.completion.ok).toBe(true); + const described = rendered(answer.lookup ?? { ok: false, error: new Error("none") }).output; + expect(described).toContain("THE MARKER CHAPTER."); + expect(described).not.toContain("SUBSTITUTED LATE."); + // No new collection happened, which is the mechanism rather than the + // symptom: the count is the same before and after the evaluation. + expect(counted.reads).toBe(1); + }); it("E3c: two live executions each keep the capture they made", function* () { const mine = { reads: 0 }; @@ -1794,6 +1797,7 @@ describe("Tier E8 — the captured selection holds canonical Syntax once", () => }); describe("Tier E9 — an availability refusal is the call's answer", () => { + beforeEach(journalBeforeEach("packages/core/tests/host-generated-root.test.ts")); beforeAll(() => useTempFileCompiler()); /** @@ -1880,31 +1884,29 @@ describe("Tier E9 — an availability refusal is the call's answer", () => { * evaluator lives in, so the four decided members and the class name the * request uses are checked at the published boundary. */ - heldHostCase( - "E9d: the public request is the four-member shape, typed through core/host", - function* (stream) { - const invoked: string[] = []; - const classes: readonly GeneratedEffectClass[] = ["read"]; - const asked: GeneratedXmdRequest = { - id: "turn-1", - source: "\n\n\n", - allow: classes, - position: { line: 1, column: 1, offset: 0 }, - }; - const answer: { value?: Result } = {}; - const run = yield* withHeldHost( - { stream, installations: [selecting([probe(invoked)])] }, - function* (open) { - answer.value = yield* open.execution.evaluateGeneratedXmdRoot(asked); - }, - ); + it("E9d: the public request is the four-member shape, typed through core/host", function* () { + const stream = yield* useJournalStream(HELD_ROOT_PATH); + const invoked: string[] = []; + const classes: readonly GeneratedEffectClass[] = ["read"]; + const asked: GeneratedXmdRequest = { + id: "turn-1", + source: "\n\n\n", + allow: classes, + position: { line: 1, column: 1, offset: 0 }, + }; + const answer: { value?: Result } = {}; + const run = yield* withHeldHost( + { stream, installations: [selecting([probe(invoked)])] }, + function* (open) { + answer.value = yield* open.execution.evaluateGeneratedXmdRoot(asked); + }, + ); - expect(run.completion.ok).toBe(true); - expect(rendered(answer.value ?? { ok: false, error: new Error("none") }).output).toContain( - "probed", - ); - }, - ); + expect(run.completion.ok).toBe(true); + expect(rendered(answer.value ?? { ok: false, error: new Error("none") }).output).toContain( + "probed", + ); + }); }); describe("R6 — generated ordinary identity remains execution-owned", () => { diff --git a/packages/core/tests/if.test.ts b/packages/core/tests/if.test.ts index a3cccc579..9616feccf 100644 --- a/packages/core/tests/if.test.ts +++ b/packages/core/tests/if.test.ts @@ -1,4 +1,4 @@ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeAll, beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; import { scoped } from "effection"; @@ -21,9 +21,10 @@ import { Sample } from "../src/sample-api.ts"; import { readTextFile } from "@effectionx/fs"; import type { ComponentElement, FunctionComponentDefinition, Segment } from "../src/types.ts"; import { asText } from "./helpers.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; - -const journalCase = journalCases("packages/core/tests/if.test.ts"); +import { + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; interface IfRun { segments: Segment[]; @@ -443,10 +444,11 @@ describe("Tier IF — printed errors carry source positions", () => { }); describe("Tier IF — document execution", () => { + beforeEach(journalBeforeEach("packages/core/tests/if.test.ts")); beforeAll(() => useTempFileCompiler()); it("IF39: only the selected branch reaches the journal", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": [ "```js eval", @@ -481,47 +483,41 @@ describe("Tier IF — document execution", () => { expect(evaluated).not.toContain("UNSELECTED_RAN"); }); - journalCase( - "IF40: an effect in the unselected branch never happens", - "test.md", - function* (stream) { - yield* useStubFs({ - "test.md": [ - "", - "```js eval", - "throw new Error('UNSELECTED_EFFECT');", - "```", - "selected", - "", - ].join("\n"), - }); - yield* useEchoExec(); + it("IF40: an effect in the unselected branch never happens", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "test.md": [ + "", + "```js eval", + "throw new Error('UNSELECTED_EFFECT');", + "```", + "selected", + "", + ].join("\n"), + }); + yield* useEchoExec(); - const output = yield* collect(yield* execute({ path: "test.md", stream })); + const output = yield* collect(yield* execute({ path: "test.md", stream })); - expect(output).toContain("selected"); - expect(output).not.toContain("UNSELECTED_EFFECT"); - expect(output).not.toContain("ERROR"); - }, - ); + expect(output).toContain("selected"); + expect(output).not.toContain("UNSELECTED_EFFECT"); + expect(output).not.toContain("ERROR"); + }); - journalCase( - "IF41: a component in the unselected branch is never imported", - "test.md", - function* (stream) { - yield* useStubFs({ - "components/Boom.md": "```js eval\noutput('BOOM_RAN');\n```\n", - "test.md": "alt", - }); - yield* useEchoExec(); + it("IF41: a component in the unselected branch is never imported", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "components/Boom.md": "```js eval\noutput('BOOM_RAN');\n```\n", + "test.md": "alt", + }); + yield* useEchoExec(); - const output = yield* collect(yield* execute({ path: "test.md", stream })); + const output = yield* collect(yield* execute({ path: "test.md", stream })); - expect(output).toContain("alt"); - expect(output).not.toContain("BOOM_RAN"); - expect(output).not.toContain("ERROR"); - }, - ); + expect(output).toContain("alt"); + expect(output).not.toContain("BOOM_RAN"); + expect(output).not.toContain("ERROR"); + }); it("IF42: selects the same branch from a restored binding on partial replay", function* () { const REPLAY_DOC = [ @@ -570,30 +566,27 @@ describe("Tier IF — document execution", () => { expect(evaluated).not.toContain("CLEAN_RAN"); }); - journalCase( - "IF43: an projected through resolves the caller's binding", - "test.md", - function* (stream) { - yield* useStubFs({ - "components/Wrap.md": "", - "test.md": [ - "```js eval", - "const ready = false;", - "```", - "", - "READYWAITING", - "", - ].join("\n"), - }); - yield* useEchoExec(); + it("IF43: an projected through resolves the caller's binding", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "components/Wrap.md": "", + "test.md": [ + "```js eval", + "const ready = false;", + "```", + "", + "READYWAITING", + "", + ].join("\n"), + }); + yield* useEchoExec(); - const output = yield* collect(yield* execute({ path: "test.md", stream })); + const output = yield* collect(yield* execute({ path: "test.md", stream })); - expect(output).toContain("WAITING"); - expect(output).not.toContain("READY"); - expect(output).not.toContain("ERROR"); - }, - ); + expect(output).toContain("WAITING"); + expect(output).not.toContain("READY"); + expect(output).not.toContain("ERROR"); + }); }); /** @@ -603,6 +596,7 @@ describe("Tier IF — document execution", () => { * with a selected control that proves the counter would have moved. */ describe("Tier IF — the unselected branch reaches no external mechanism", () => { + beforeEach(journalBeforeEach("packages/core/tests/if.test.ts")); beforeAll(() => useTempFileCompiler()); const PROBE_DOC = (condition: boolean) => @@ -643,7 +637,7 @@ describe("Tier IF — the unselected branch reaches no external mechanism", () = return scoped(function* () { const reads: string[] = []; const commands: string[] = []; - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); const files: Record = { "components/Probe.md": "PROBE_BODY", "test.md": PROBE_DOC(condition), diff --git a/packages/core/tests/let.test.ts b/packages/core/tests/let.test.ts index 1b42963d7..c92f9e376 100644 --- a/packages/core/tests/let.test.ts +++ b/packages/core/tests/let.test.ts @@ -7,7 +7,7 @@ * and a direct value reaches the binding by reference rather than through the * JSON boundary ordinary component props cross. */ -import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; +import { beforeAll, beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { scoped, suspend, withResolvers } from "effection"; import type { Operation, Result } from "effection"; @@ -23,9 +23,10 @@ import { Component } from "../src/component-api.ts"; import { scanSegments } from "../src/scanner.ts"; import { renderSegments } from "../src/render.ts"; import { execute, executeInstalled } from "../src/execute.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; - -const journalCase = journalCases("packages/core/tests/let.test.ts"); +import { + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; import type { HostExecution } from "../src/execute.ts"; import { inlineSource } from "../src/root-source.ts"; import { collect } from "../src/collect.ts"; @@ -455,51 +456,49 @@ describe("Tier LET — failure and overwrite", () => { }); describe("Tier LET — a whole document", () => { + beforeEach(journalBeforeEach("packages/core/tests/let.test.ts")); beforeAll(() => useTempFileCompiler()); - journalCase( - "LET10: a direct binding reaches prose, an expression prop, and a later eval", - "README.md", - function* (stream) { - yield* useStubFs({ - "README.md": [ - "```ts eval", - 'const settings = { title: "Release" };', - "```", - "", - '', - "", - '', - "", - "Prose reads {title}.", - "", - "", - "", - "```ts eval", - 'return alias === settings ? "identical" : "different";', - "```", - ].join("\n"), - "components/Note.md": [ - "---", - "props:", - " type: object", - " properties:", - " label:", - " type: string", - " additionalProperties: false", - "---", - "", - "Note says {props.label}.", - ].join("\n"), - }); + it("LET10: a direct binding reaches prose, an expression prop, and a later eval", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ + "README.md": [ + "```ts eval", + 'const settings = { title: "Release" };', + "```", + "", + '', + "", + '', + "", + "Prose reads {title}.", + "", + "", + "", + "```ts eval", + 'return alias === settings ? "identical" : "different";', + "```", + ].join("\n"), + "components/Note.md": [ + "---", + "props:", + " type: object", + " properties:", + " label:", + " type: string", + " additionalProperties: false", + "---", + "", + "Note says {props.label}.", + ].join("\n"), + }); - const output = asText(yield* collect(yield* execute({ path: "README.md", stream }))); + const output = asText(yield* collect(yield* execute({ path: "README.md", stream }))); - expect(output).toContain("Prose reads Release."); - expect(output).toContain("Note says Release."); - expect(output).toContain("identical"); - }, - ); + expect(output).toContain("Prose reads Release."); + expect(output).toContain("Note says Release."); + expect(output).toContain("identical"); + }); it("LET11: a partial replay rebinds by reference and records nothing of its own", function* () { const document = [ @@ -548,22 +547,19 @@ describe("Tier LET — a whole document", () => { ]); }); - journalCase( - "LET12: Let is the syntax; Capture is an ordinary component", - "README.md", - function* (stream) { - yield* useStubFs({ - "README.md": ['', "", ""].join("\n"), - "components/Let.md": "A REPOSITORY FILE RAN\n", - "components/Capture.md": "an ordinary component ran.\n", - }); + it("LET12: Let is the syntax; Capture is an ordinary component", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ + "README.md": ['', "", ""].join("\n"), + "components/Let.md": "A REPOSITORY FILE RAN\n", + "components/Capture.md": "an ordinary component ran.\n", + }); - const output = asText(yield* collect(yield* execute({ path: "README.md", stream }))); + const output = asText(yield* collect(yield* execute({ path: "README.md", stream }))); - expect(output).toContain("an ordinary component ran."); - expect(output).not.toContain("A REPOSITORY FILE RAN"); - }, - ); + expect(output).toContain("an ordinary component ran."); + expect(output).not.toContain("A REPOSITORY FILE RAN"); + }); }); /** @@ -723,10 +719,11 @@ function* readBindings(execution: HostExecution): Operation { return answer.value; } describe("Tier LET — the ordinary inventory, and which values are eligible", () => { + beforeEach(journalBeforeEach("packages/core/tests/let.test.ts")); beforeAll(() => useTempFileCompiler()); it("C9: ordinary execution records the exact LET11 inventory and no binding effect", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": EVERY_KIND }); // Every one of those values binds by reference and is usable here, and the @@ -738,32 +735,29 @@ describe("Tier LET — the ordinary inventory, and which values are eligible", ( expect(stream.snapshot().map(kindOf)).toEqual(["import_component", "eval", "close"]); }); - journalCase( - "C7: the eligible data is the JSON subset of the same document's root", - "README.md", - function* (stream) { - yield* useStubFs({ "README.md": EVERY_KIND }); - - const execution = yield* executeInstalled({ path: "README.md", stream }, []); - const settled = yield* execution; - expect(settled.ok).toBe(true); - - // The live bindings are unchanged — the document read every one of them - // above — and what crosses is decided by one strict JSON walk rather than - // by which construct produced the value. - const held = yield* readBindings(execution); - expect(held["nested"]).toEqual({ list: [1, { deep: true }], nothing: null }); - expect(held["nothing"]).toBe(null); - expect(held["rendered"]).toBe("plain text"); - // Omitted whole, never coerced: a value that is not plain JSON, a cycle, a - // sparse hole, a non-finite number, `undefined` and a function. - for (const omitted of ["notPlain", "cycle", "sparse", "infinite", "absent", "work"]) { - expect(Object.hasOwn(held, omitted)).toBe(false); - } - // `props` is each execution's own namespace and never crosses. - expect(Object.hasOwn(held, "props")).toBe(false); - }, - ); + it("C7: the eligible data is the JSON subset of the same document's root", function* () { + const stream = yield* useJournalStream("README.md"); + yield* useStubFs({ "README.md": EVERY_KIND }); + + const execution = yield* executeInstalled({ path: "README.md", stream }, []); + const settled = yield* execution; + expect(settled.ok).toBe(true); + + // The live bindings are unchanged — the document read every one of them + // above — and what crosses is decided by one strict JSON walk rather than + // by which construct produced the value. + const held = yield* readBindings(execution); + expect(held["nested"]).toEqual({ list: [1, { deep: true }], nothing: null }); + expect(held["nothing"]).toBe(null); + expect(held["rendered"]).toBe("plain text"); + // Omitted whole, never coerced: a value that is not plain JSON, a cycle, a + // sparse hole, a non-finite number, `undefined` and a function. + for (const omitted of ["notPlain", "cycle", "sparse", "infinite", "absent", "work"]) { + expect(Object.hasOwn(held, omitted)).toBe(false); + } + // `props` is each execution's own namespace and never crosses. + expect(Object.hasOwn(held, "props")).toBe(false); + }); }); describe("Tier LET — the computed root data an execution hands back", () => { beforeAll(() => useTempFileCompiler()); diff --git a/packages/core/tests/loop.test.ts b/packages/core/tests/loop.test.ts index ae8c95a14..57e8e54cd 100644 --- a/packages/core/tests/loop.test.ts +++ b/packages/core/tests/loop.test.ts @@ -1,4 +1,4 @@ -import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; +import { beforeAll, beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; import { ensure, scoped, spawn, suspend, withResolvers } from "effection"; @@ -19,9 +19,10 @@ import { collect } from "../src/collect.ts"; import { printErrors } from "../src/component-failures.ts"; import type { ComponentElement, FunctionComponent, Segment } from "../src/types.ts"; import { asText } from "./helpers.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; - -const journalCase = journalCases("packages/core/tests/loop.test.ts"); +import { + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; interface LoopRun { segments: Segment[]; @@ -691,10 +692,11 @@ describe("Tier LOOP — printed errors carry source positions", () => { }); describe("Tier LOOP — document execution", () => { + beforeEach(journalBeforeEach("packages/core/tests/loop.test.ts")); beforeAll(() => useTempFileCompiler()); it("LOOP35: every iteration journals its own eval entry", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useHostFiles(); yield* useStubFs({ "test.md": ["", "", "```js eval", "output('RAN');", "```", "", ""].join( @@ -714,7 +716,7 @@ describe("Tier LOOP — document execution", () => { }); it("LOOP36: content skipped by writes no journal entry", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useHostFiles(); yield* useStubFs({ "test.md": [ @@ -747,27 +749,28 @@ describe("Tier LOOP — document execution", () => { expect(evalNames(stream.snapshot())).toHaveLength(1); }); - journalCase("LOOP37: a binding accumulates across iterations", "test.md", function* (stream) { + it("LOOP37: a binding accumulates across iterations", function* () { + const stream = yield* useJournalStream("test.md"); yield* useHostFiles(); yield* useStubFs({ - "test.md": [ - "```js eval", - "const marks = [];", - "```", - "", - "", - "```js eval", - "marks.push(marks.length);", - "```", - "", - "", - "", - "tally={marks.length}", - "", - "```js eval", - "output(`TALLY:${marks.join(',')}`);", - "```", - ].join("\n"), + "test.md": [ + "```js eval", + "const marks = [];", + "```", + "", + "", + "```js eval", + "marks.push(marks.length);", + "```", + "", + "", + "", + "tally={marks.length}", + "", + "```js eval", + "output(`TALLY:${marks.join(',')}`);", + "```", + ].join("\n"), }); yield* useEchoExec(); @@ -775,22 +778,19 @@ describe("Tier LOOP — document execution", () => { expect(output).toContain("TALLY:0,1,2,3"); }); - journalCase( - "LOOP38: a component in the loop body is imported once per iteration", - "test.md", - function* (stream) { - yield* useHostFiles(); - yield* useStubFs({ - "components/Step.md": "step\n", - "test.md": "", - }); - yield* useEchoExec(); + it("LOOP38: a component in the loop body is imported once per iteration", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useHostFiles(); + yield* useStubFs({ + "components/Step.md": "step\n", + "test.md": "", + }); + yield* useEchoExec(); - const output = asText(yield* collect(yield* execute({ path: "test.md", stream }))); - expect(output.match(/step/g)).toHaveLength(3); - expect(output).not.toContain("ERROR"); - }, - ); + const output = asText(yield* collect(yield* execute({ path: "test.md", stream }))); + expect(output.match(/step/g)).toHaveLength(3); + expect(output).not.toContain("ERROR"); + }); it("LOOP39: a truncated journal replays one iteration and runs the rest live", function* () { const DOC = [ @@ -847,39 +847,36 @@ describe("Tier LOOP — document execution", () => { ]); }); - journalCase( - "LOOP40: a chosen from a binding stops the document's loop", - "test.md", - function* (stream) { - yield* useHostFiles(); - yield* useStubFs({ - "test.md": [ - "```js eval", - "const seen = [];", - "```", - "", - "", - "```js eval", - "seen.push('iteration');", - "```", - "", - "", - "", - "", - "", - "", - "```js eval", - "output(`ITERATIONS:${seen.length}`);", - "```", - ].join("\n"), - }); - yield* useEchoExec(); + it("LOOP40: a chosen from a binding stops the document's loop", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useHostFiles(); + yield* useStubFs({ + "test.md": [ + "```js eval", + "const seen = [];", + "```", + "", + "", + "```js eval", + "seen.push('iteration');", + "```", + "", + "", + "", + "", + "", + "", + "```js eval", + "output(`ITERATIONS:${seen.length}`);", + "```", + ].join("\n"), + }); + yield* useEchoExec(); - const output = asText(yield* collect(yield* execute({ path: "test.md", stream }))); - expect(output).toContain("ITERATIONS:2"); - expect(output).not.toContain("ERROR"); - }, - ); + const output = asText(yield* collect(yield* execute({ path: "test.md", stream }))); + expect(output).toContain("ITERATIONS:2"); + expect(output).not.toContain("ERROR"); + }); }); /** diff --git a/packages/core/tests/sample-component.test.ts b/packages/core/tests/sample-component.test.ts index 29c63eacc..5e4a1ece8 100644 --- a/packages/core/tests/sample-component.test.ts +++ b/packages/core/tests/sample-component.test.ts @@ -12,7 +12,7 @@ * Uses stub providers (Sample.around middleware) to intercept Sample Api * calls and return canned responses for testing. */ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeAll, beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import { expect } from "@executablemd/test-support/expect"; import { InMemoryStream } from "@executablemd/durable-streams"; @@ -23,9 +23,10 @@ import { ensureDir, readTextFile, writeTextFile } from "@effectionx/fs"; import { useTempDirectory } from "@executablemd/test-support/temp"; import type { Operation } from "effection"; import * as path from "node:path"; -import { journalCases } from "../../test-support/journal-cases.ts"; - -const journalCase = journalCases("packages/core/tests/sample-component.test.ts"); +import { + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; function* writeFiles(dir: string, files: Record): Operation { for (const [filePath, content] of Object.entries(files)) { @@ -416,19 +417,21 @@ describe("Tier SC — Sample component", () => { }); describe("Tier EO — eval output() function", () => { + beforeEach(journalBeforeEach("packages/core/tests/sample-component.test.ts")); beforeAll(() => useTempFileCompiler()); // EO1: output() sets eval block output - journalCase("EO1: output() produces eval block output", "test.md", function* (stream) { + it("EO1: output() produces eval block output", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ - "test.md": '```js eval\noutput("hello from eval");\n```\n', + "test.md": '```js eval\noutput("hello from eval");\n```\n', }); yield* useEchoExec(); const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), + yield* execute({ + path: "test.md", + stream, + }), ); expect(output).toContain("hello from eval"); @@ -464,17 +467,18 @@ describe("Tier EO — eval output() function", () => { }); // EO3: eval block without output() still returns empty - journalCase("EO3: eval block without output() produces no output", "test.md", function* (stream) { + it("EO3: eval block without output() produces no output", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ - "test.md": "```js eval\nconst x = 42;\n```\nafter-eval", + "test.md": "```js eval\nconst x = 42;\n```\nafter-eval", }); yield* useEchoExec(); const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), + yield* execute({ + path: "test.md", + stream, + }), ); // No output from eval block — should see "after-eval" but no "42" @@ -483,34 +487,36 @@ describe("Tier EO — eval output() function", () => { }); // EO4: output() with multiline content - journalCase("EO4: output() with multiline content", "test.md", function* (stream) { + it("EO4: output() with multiline content", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ - "test.md": '```js eval\noutput("line1\\nline2\\nline3");\n```\n', + "test.md": '```js eval\noutput("line1\\nline2\\nline3");\n```\n', }); yield* useEchoExec(); const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), + yield* execute({ + path: "test.md", + stream, + }), ); expect(output).toContain("line1\nline2\nline3"); }); // EO5: output() converts non-string to string - journalCase("EO5: output() converts non-string to string", "test.md", function* (stream) { + it("EO5: output() converts non-string to string", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ - "test.md": "```js eval\noutput(12345);\n```\n", + "test.md": "```js eval\noutput(12345);\n```\n", }); yield* useEchoExec(); const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), + yield* execute({ + path: "test.md", + stream, + }), ); expect(output).toContain("12345"); @@ -518,41 +524,11 @@ describe("Tier EO — eval output() function", () => { }); describe("Tier RC — renderChildren and render closures", () => { + beforeEach(journalBeforeEach("packages/core/tests/sample-component.test.ts")); beforeAll(() => useTempFileCompiler()); // RC1: renderChildren() returns empty string for self-closing component - journalCase( - "RC1: renderChildren returns empty for self-closing component", - "test.md", - function* (stream) { - yield* useStubFs({ - "components/TestComp.md": [ - "---", - "meta:", - " componentName: TestComp", - "---", - "", - "```js eval", - "const result = yield* renderChildren();", - "output('children:[' + result + ']');", - "```", - ].join("\n"), - "test.md": "", - }); - yield* useEchoExec(); - - const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), - ); - - expect(output).toContain("children:[]"); - }, - ); - - // RC2: renderChildren() captures children text content - journalCase("RC2: renderChildren captures children text", "test.md", function* (stream) { + it("RC1: renderChildren returns empty for self-closing component", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "components/TestComp.md": [ "---", @@ -562,10 +538,10 @@ describe("Tier RC — renderChildren and render closures", () => { "", "```js eval", "const result = yield* renderChildren();", - "output('children:[' + result.trim() + ']');", + "output('children:[' + result + ']');", "```", ].join("\n"), - "test.md": ["", "", "hello from children", "", ""].join("\n"), + "test.md": "", }); yield* useEchoExec(); @@ -576,11 +552,69 @@ describe("Tier RC — renderChildren and render closures", () => { }), ); + expect(output).toContain("children:[]"); + }); + + // RC2: renderChildren() captures children text content + it("RC2: renderChildren captures children text", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "components/TestComp.md": [ + "---", + "meta:", + " componentName: TestComp", + "---", + "", + "```js eval", + "const result = yield* renderChildren();", + "output('children:[' + result.trim() + ']');", + "```", + ].join("\n"), + "test.md": ["", "", "hello from children", "", ""].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect( + yield* execute({ + path: "test.md", + stream, + }), + ); + expect(output).toContain("children:[hello from children]"); }); // RC3: render() expands arbitrary markdown string - journalCase("RC3: render() expands arbitrary markdown string", "test.md", function* (stream) { + it("RC3: render() expands arbitrary markdown string", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "components/TestComp.md": [ + "---", + "meta:", + " componentName: TestComp", + "---", + "", + "```js eval", + "const result = yield* render('arbitrary **markdown** content');", + "output('rendered:[' + result.trim() + ']');", + "```", + ].join("\n"), + "test.md": "", + }); + yield* useEchoExec(); + + const output = yield* collect( + yield* execute({ + path: "test.md", + stream, + }), + ); + + expect(output).toContain("rendered:[arbitrary **markdown** content]"); + }); + + it("RC4: renderChildren(override) is visible to children and wins over the caller value", function* () { + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "components/TestComp.md": [ "---", @@ -589,126 +623,89 @@ describe("Tier RC — renderChildren and render closures", () => { "---", "", "```js eval", - "const result = yield* render('arbitrary **markdown** content');", - "output('rendered:[' + result.trim() + ']');", + "const result = yield* renderChildren({ item: 'injected', shadow: 'override-wins' });", + "output('r:[' + result.trim() + ']');", "```", ].join("\n"), - "test.md": "", + "test.md": [ + "```js eval", + "const shadow = 'caller-value';", + "```", + "", + "text item={item} shadow={shadow}", + "```js eval", + "output('child-eval:' + shadow + '/' + item);", + "```", + "", + ].join("\n"), }); yield* useEchoExec(); - const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), - ); + const output = yield* collect(yield* execute({ path: "test.md", stream })); - expect(output).toContain("rendered:[arbitrary **markdown** content]"); + expect(output).toContain("text item=injected shadow=override-wins"); + expect(output).toContain("child-eval:override-wins/injected"); + expect(output).not.toContain("caller-value"); }); - journalCase( - "RC4: renderChildren(override) is visible to children and wins over the caller value", - "test.md", - function* (stream) { - yield* useStubFs({ - "components/TestComp.md": [ - "---", - "meta:", - " componentName: TestComp", - "---", - "", - "```js eval", - "const result = yield* renderChildren({ item: 'injected', shadow: 'override-wins' });", - "output('r:[' + result.trim() + ']');", - "```", - ].join("\n"), - "test.md": [ - "```js eval", - "const shadow = 'caller-value';", - "```", - "", - "text item={item} shadow={shadow}", - "```js eval", - "output('child-eval:' + shadow + '/' + item);", - "```", - "", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("text item=injected shadow=override-wins"); - expect(output).toContain("child-eval:override-wins/injected"); - expect(output).not.toContain("caller-value"); - }, - ); - - journalCase( - "RC5: renderChildren(override) does not leak into the caller env", - "test.md", - function* (stream) { - yield* useStubFs({ - "components/TestComp.md": [ - "---", - "meta:", - " componentName: TestComp", - "---", - "", - "```js eval", - "yield* renderChildren({ item: 'injected' });", - "```", - ].join("\n"), - "test.md": [ - "x={item}", - "```js eval", - "output('caller-after:' + (typeof item === 'undefined' ? 'undefined' : item));", - "```", - ].join("\n"), - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("caller-after:undefined"); - expect(output).not.toContain("ERROR"); - }, - ); - - journalCase( - "RC6: renderChildren(override) rejects null, arrays, primitives, and non-plain objects", - "test.md", - function* (stream) { - yield* useStubFs({ - "components/TestComp.md": [ - "---", - "meta:", - " componentName: TestComp", - "---", - "", - "```js eval", - "const bad = [null, [1, 2, 3], 42, 'str', new Date(), new Map()];", - "let caught = 0;", - "for (const value of bad) {", - " try {", - " yield* renderChildren(value);", - " } catch (e) {", - " if (e.message === 'renderChildren(override) requires a plain object.') caught++;", - " }", - "}", - "output('caught:' + caught + '/' + bad.length);", - "```", - ].join("\n"), - "test.md": "body", - }); - yield* useEchoExec(); - - const output = yield* collect(yield* execute({ path: "test.md", stream })); - - expect(output).toContain("caught:6/6"); - }, - ); + it("RC5: renderChildren(override) does not leak into the caller env", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "components/TestComp.md": [ + "---", + "meta:", + " componentName: TestComp", + "---", + "", + "```js eval", + "yield* renderChildren({ item: 'injected' });", + "```", + ].join("\n"), + "test.md": [ + "x={item}", + "```js eval", + "output('caller-after:' + (typeof item === 'undefined' ? 'undefined' : item));", + "```", + ].join("\n"), + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).toContain("caller-after:undefined"); + expect(output).not.toContain("ERROR"); + }); + + it("RC6: renderChildren(override) rejects null, arrays, primitives, and non-plain objects", function* () { + const stream = yield* useJournalStream("test.md"); + yield* useStubFs({ + "components/TestComp.md": [ + "---", + "meta:", + " componentName: TestComp", + "---", + "", + "```js eval", + "const bad = [null, [1, 2, 3], 42, 'str', new Date(), new Map()];", + "let caught = 0;", + "for (const value of bad) {", + " try {", + " yield* renderChildren(value);", + " } catch (e) {", + " if (e.message === 'renderChildren(override) requires a plain object.') caught++;", + " }", + "}", + "output('caught:' + caught + '/' + bad.length);", + "```", + ].join("\n"), + "test.md": "body", + }); + yield* useEchoExec(); + + const output = yield* collect(yield* execute({ path: "test.md", stream })); + + expect(output).toContain("caught:6/6"); + }); }); describe("Tier IN — Instruction component", () => { diff --git a/packages/core/tests/source-position.test.ts b/packages/core/tests/source-position.test.ts index 5cfdc8121..d5ba4e16c 100644 --- a/packages/core/tests/source-position.test.ts +++ b/packages/core/tests/source-position.test.ts @@ -1,4 +1,4 @@ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { useStubFs } from "@executablemd/runtime/test"; import { scanSegments } from "../src/scanner.ts"; @@ -8,9 +8,10 @@ import { registerComponents } from "../src/components/registration.ts"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import type { ComponentElement, SourcePosition } from "../src/types.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; - -const journalCase = journalCases("packages/core/tests/source-position.test.ts"); +import { + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; function componentsOf(segments: ReturnType): ComponentElement[] { const found: ComponentElement[] = []; @@ -24,6 +25,7 @@ function componentsOf(segments: ReturnType): ComponentEleme } describe("source positions", () => { + beforeEach(journalBeforeEach("packages/core/tests/source-position.test.ts")); it("attaches local positions without an origin", function* () { const [greeting] = componentsOf(scanSegments("line one\n\n")); expect(greeting?.position).toEqual({ path: undefined, offset: 9, line: 2, column: 1 }); @@ -53,100 +55,94 @@ describe("source positions", () => { }); }); - journalCase( - "computes original-file positions past frontmatter, immune to repeated body text", - "README.md", - function* (stream) { - // The frontmatter contains a copy of the body's first line: a content - // search for the body would false-match inside the frontmatter, so only - // the suffix computation yields the correct base. - const doc = [ - "---", - 'title: ""', - "note: intro line", - "---", - "intro line", - "", - "", - ].join("\n"); - - const positions: SourcePosition[] = []; - - yield* useStubFs({ "README.md": doc }); - // Registered rather than a repository file, so the probe can record what - // the engine tells a component about its own call site. That is the public - // guarantee now — `getExpansion()` — rather than what a claim was offered. - yield* registerComponents([ - { - name: "Probe", - origin: "test", - props: { type: "object", properties: {}, additionalProperties: false }, - *fn() { - const position = (yield* getExpansion()).position; - if (position) { - positions.push(position); - } - return "probed"; - }, - }, - ]); - - const output = yield* collect(yield* execute({ path: "README.md", stream })); - expect(output).toContain("probed"); - - const probeLine = doc.split("\n").indexOf("") + 1; - expect(positions).toEqual([ - { - path: "README.md", - offset: doc.indexOf("\n") + 1, - line: probeLine, - column: 1, - }, - ]); - }, - ); - - journalCase( - "positions imported-component invocations in the component's own file", - "README.md", - function* (stream) { - const readme = "\n"; - const wrapper = ["---", "title: Wrapper", "---", "before", "", ""].join("\n"); - - const positions: Array<{ name: string; position?: SourcePosition }> = []; - - yield* useStubFs({ - "README.md": readme, - "components/Wrapper.md": wrapper, - }); - yield* registerComponents([ - { - name: "Probe", - origin: "test", - props: { type: "object", properties: {}, additionalProperties: false }, - *fn() { - const metadata = yield* getExpansion(); - positions.push({ - name: metadata.name, - ...(metadata.position ? { position: metadata.position } : {}), - }); - return "probed"; - }, + it("computes original-file positions past frontmatter, immune to repeated body text", function* () { + const stream = yield* useJournalStream("README.md"); + // The frontmatter contains a copy of the body's first line: a content + // search for the body would false-match inside the frontmatter, so only + // the suffix computation yields the correct base. + const doc = [ + "---", + 'title: ""', + "note: intro line", + "---", + "intro line", + "", + "", + ].join("\n"); + + const positions: SourcePosition[] = []; + + yield* useStubFs({ "README.md": doc }); + // Registered rather than a repository file, so the probe can record what + // the engine tells a component about its own call site. That is the public + // guarantee now — `getExpansion()` — rather than what a claim was offered. + yield* registerComponents([ + { + name: "Probe", + origin: "test", + props: { type: "object", properties: {}, additionalProperties: false }, + *fn() { + const position = (yield* getExpansion()).position; + if (position) { + positions.push(position); + } + return "probed"; }, - ]); + }, + ]); - const output = yield* collect(yield* execute({ path: "README.md", stream })); - expect(output).toContain("probed"); + const output = yield* collect(yield* execute({ path: "README.md", stream })); + expect(output).toContain("probed"); - const probe = positions.find((p) => p.name === "Probe"); - expect(probe?.position).toEqual({ - path: "components/Wrapper.md", - offset: wrapper.indexOf(""), - line: 5, + const probeLine = doc.split("\n").indexOf("") + 1; + expect(positions).toEqual([ + { + path: "README.md", + offset: doc.indexOf("\n") + 1, + line: probeLine, column: 1, - }); - }, - ); + }, + ]); + }); + + it("positions imported-component invocations in the component's own file", function* () { + const stream = yield* useJournalStream("README.md"); + const readme = "\n"; + const wrapper = ["---", "title: Wrapper", "---", "before", "", ""].join("\n"); + + const positions: Array<{ name: string; position?: SourcePosition }> = []; + + yield* useStubFs({ + "README.md": readme, + "components/Wrapper.md": wrapper, + }); + yield* registerComponents([ + { + name: "Probe", + origin: "test", + props: { type: "object", properties: {}, additionalProperties: false }, + *fn() { + const metadata = yield* getExpansion(); + positions.push({ + name: metadata.name, + ...(metadata.position ? { position: metadata.position } : {}), + }); + return "probed"; + }, + }, + ]); + + const output = yield* collect(yield* execute({ path: "README.md", stream })); + expect(output).toContain("probed"); + + const probe = positions.find((p) => p.name === "Probe"); + expect(probe?.position).toEqual({ + path: "components/Wrapper.md", + offset: wrapper.indexOf(""), + line: 5, + column: 1, + }); + }); }); /** diff --git a/packages/core/tests/switch.test.ts b/packages/core/tests/switch.test.ts index 6ebf803c3..7fff5f637 100644 --- a/packages/core/tests/switch.test.ts +++ b/packages/core/tests/switch.test.ts @@ -1,4 +1,4 @@ -import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; +import { beforeAll, beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { scoped } from "effection"; import type { Operation } from "effection"; @@ -22,9 +22,10 @@ import type { SourceOrigin } from "../src/scanner.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import type { ComponentElement, FunctionComponentDefinition, Segment } from "../src/types.ts"; import { asText } from "./helpers.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; - -const journalCase = journalCases("packages/core/tests/switch.test.ts"); +import { + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; interface SwitchRun { segments: Segment[]; @@ -653,6 +654,7 @@ describe("Tier SWITCH / CASE — error observation", () => { * with a selected control that proves the counter would have moved. */ describe("Tier SWITCH / CASE — only the selected body works", () => { + beforeEach(journalBeforeEach("packages/core/tests/switch.test.ts")); beforeAll(() => useTempFileCompiler()); const PROBE_DOC = (selector: string) => @@ -693,7 +695,7 @@ describe("Tier SWITCH / CASE — only the selected body works", () => { return scoped(function* () { const reads: string[] = []; const commands: string[] = []; - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); const files: Record = { "components/Probe.md": "PROBE_BODY", "test.md": PROBE_DOC(selector), @@ -784,6 +786,7 @@ describe("Tier SWITCH / CASE — only the selected body works", () => { * Api itself. */ describe("Tier SWITCH / CASE — provider boundary", () => { + beforeEach(journalBeforeEach("packages/core/tests/switch.test.ts")); beforeAll(() => useTempFileCompiler()); function runSampleProbe(selector: string): Operation<{ calls: string[]; output: string }> { @@ -812,9 +815,8 @@ describe("Tier SWITCH / CASE — provider boundary", () => { }, }); - const output = asText( - yield* collect(yield* execute({ path: "test.md", stream: new InMemoryStream() })), - ); + const stream = yield* useJournalStream("test.md"); + const output = asText(yield* collect(yield* execute({ path: "test.md", stream }))); return { calls, output }; }); } @@ -834,64 +836,59 @@ describe("Tier SWITCH / CASE — provider boundary", () => { }); describe("Tier SWITCH / CASE — document execution", () => { + beforeEach(journalBeforeEach("packages/core/tests/switch.test.ts")); beforeAll(() => useTempFileCompiler()); - journalCase( - "SW55: a in the selected case satisfies the value body that owns it", - "test.md", - function* (journal) { - yield* useStubFs({ - "Verdict.md": [ - "---", - "returns:", - " type: string", - "---", - "", - '', - '', - '', - "", - "", - ].join("\n"), - "test.md": '\n\nGot {v}\n', - }); - yield* useEchoExec(); + it("SW55: a in the selected case satisfies the value body that owns it", function* () { + const journal = yield* useJournalStream("test.md"); + yield* useStubFs({ + "Verdict.md": [ + "---", + "returns:", + " type: string", + "---", + "", + '', + '', + '', + "", + "", + ].join("\n"), + "test.md": '\n\nGot {v}\n', + }); + yield* useEchoExec(); - const output = asText(yield* collect(yield* execute({ path: "test.md", stream: journal }))); - expect(output).toContain("Got picked"); - expect(output).not.toContain("held"); - }, - ); + const output = asText(yield* collect(yield* execute({ path: "test.md", stream: journal }))); + expect(output).toContain("Got picked"); + expect(output).not.toContain("held"); + }); - journalCase( - "SW56: a projected through reads the caller's bindings", - "test.md", - function* (journal) { - yield* useStubFs({ - "components/Wrap.md": "", - "test.md": [ - "```js eval", - "const state = 'waiting';", - "```", - "", - "", - "READY", - "WAITING", - "", - "", - ].join("\n"), - }); - yield* useEchoExec(); + it("SW56: a projected through reads the caller's bindings", function* () { + const journal = yield* useJournalStream("test.md"); + yield* useStubFs({ + "components/Wrap.md": "", + "test.md": [ + "```js eval", + "const state = 'waiting';", + "```", + "", + "", + "READY", + "WAITING", + "", + "", + ].join("\n"), + }); + yield* useEchoExec(); - const output = asText(yield* collect(yield* execute({ path: "test.md", stream: journal }))); - expect(output).toContain("WAITING"); - expect(output).not.toContain("READY"); - expect(output).not.toContain("ERROR"); - }, - ); + const output = asText(yield* collect(yield* execute({ path: "test.md", stream: journal }))); + expect(output).toContain("WAITING"); + expect(output).not.toContain("READY"); + expect(output).not.toContain("ERROR"); + }); it("SW57: only the selected case reaches the journal", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": [ "```js eval", diff --git a/packages/core/tests/validation-integration.test.ts b/packages/core/tests/validation-integration.test.ts index 4094e4198..59c8812fc 100644 --- a/packages/core/tests/validation-integration.test.ts +++ b/packages/core/tests/validation-integration.test.ts @@ -1,4 +1,4 @@ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { scoped } from "effection"; import type { Operation } from "effection"; @@ -12,9 +12,10 @@ import { asText } from "./helpers.ts"; import { parseFrontmatter } from "../src/frontmatter.ts"; import { compilePropsSchema } from "../src/validate.ts"; import type { ComponentDefinition, EvalEnv, Json, Segment } from "../src/types.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; - -const journalCase = journalCases("packages/core/tests/validation-integration.test.ts"); +import { + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; function markdownComponent(name: string, props: Record): ComponentDefinition { return { @@ -103,18 +104,48 @@ describe("prop-validation error segment", () => { // A full `inputs` schema parses — `inputs` lands in meta — so the failure // surfaces later, when a caller passes a prop the component does not declare. describe("`inputs` is not a compatibility alias for `props`", () => { - journalCase("a component declaring `inputs` declares no props", "README.md", function* (journal) { + beforeEach(journalBeforeEach("packages/core/tests/validation-integration.test.ts")); + it("a component declaring `inputs` declares no props", function* () { + const journal = yield* useJournalStream("README.md"); yield* useStubFs({ - "README.md": '\n', - "Legacy.md": [ + "README.md": '\n', + "Legacy.md": [ + "---", + "inputs:", + " type: object", + " properties:", + " name: { type: string }", + " additionalProperties: false", + "---", + "Hello, {props.name}", + "", + ].join("\n"), + }); + let message = ""; + try { + message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); + } catch (error) { + message = error instanceof Error ? error.message : String(error); + } + expect(message).toContain("Prop validation failed"); + }); +}); + +describe("definition-loading rejects invalid props schemas", () => { + beforeEach(journalBeforeEach("packages/core/tests/validation-integration.test.ts")); + it("rejects an async schema at the Markdown load boundary", function* () { + const journal = yield* useJournalStream("README.md"); + yield* useStubFs({ + "README.md": "\n", + "Bad.md": [ "---", - "inputs:", + "props:", + " $async: true", " type: object", - " properties:", - " name: { type: string }", + " properties: {}", " additionalProperties: false", "---", - "Hello, {props.name}", + "body", "", ].join("\n"), }); @@ -124,86 +155,50 @@ describe("`inputs` is not a compatibility alias for `props`", () => { } catch (error) { message = error instanceof Error ? error.message : String(error); } - expect(message).toContain("Prop validation failed"); + expect(message).toContain("async"); }); -}); - -describe("definition-loading rejects invalid props schemas", () => { - journalCase( - "rejects an async schema at the Markdown load boundary", - "README.md", - function* (journal) { - yield* useStubFs({ - "README.md": "\n", - "Bad.md": [ - "---", - "props:", - " $async: true", - " type: object", - " properties: {}", - " additionalProperties: false", - "---", - "body", - "", - ].join("\n"), - }); - let message = ""; - try { - message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); - } catch (error) { - message = error instanceof Error ? error.message : String(error); - } - expect(message).toContain("async"); - }, - ); - journalCase( - "rejects a reserved-name schema at the Markdown load boundary", - "README.md", - function* (journal) { - yield* useStubFs({ - "README.md": "\n", - "Bad.md": [ - "---", - "props:", - " type: object", - " properties:", - " slot: { type: string }", - " additionalProperties: false", - "---", - "body", - "", - ].join("\n"), - }); - let message = ""; - try { - message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); - } catch (error) { - message = error instanceof Error ? error.message : String(error); - } - expect(message).toContain("reserved"); - }, - ); + it("rejects a reserved-name schema at the Markdown load boundary", function* () { + const journal = yield* useJournalStream("README.md"); + yield* useStubFs({ + "README.md": "\n", + "Bad.md": [ + "---", + "props:", + " type: object", + " properties:", + " slot: { type: string }", + " additionalProperties: false", + "---", + "body", + "", + ].join("\n"), + }); + let message = ""; + try { + message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); + } catch (error) { + message = error instanceof Error ? error.message : String(error); + } + expect(message).toContain("reserved"); + }); - journalCase( - "rejects a reserved name declared through a props map", - "README.md", - function* (journal) { - yield* useStubFs({ - "README.md": "\n", - "Bad.md": ["---", "props:", " slot: { type: string }", "---", "body", ""].join("\n"), - }); - let message = ""; - try { - message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); - } catch (error) { - message = error instanceof Error ? error.message : String(error); - } - // Normalization runs first, so the contract still sees `slot` under - // `properties`. - expect(message).toContain("reserved"); - }, - ); + it("rejects a reserved name declared through a props map", function* () { + const journal = yield* useJournalStream("README.md"); + yield* useStubFs({ + "README.md": "\n", + "Bad.md": ["---", "props:", " slot: { type: string }", "---", "body", ""].join("\n"), + }); + let message = ""; + try { + message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); + } catch (error) { + message = error instanceof Error ? error.message : String(error); + } + // Normalization runs first, so the contract still sees `slot` under + // `properties`. + expect(message).toContain("reserved"); + }); }); describe("the root-object contract applies to the normalized schema", () => { diff --git a/packages/git/tests/issue-markdown.test.ts b/packages/git/tests/issue-markdown.test.ts index 41ee6ce19..8ae85a758 100644 --- a/packages/git/tests/issue-markdown.test.ts +++ b/packages/git/tests/issue-markdown.test.ts @@ -20,7 +20,7 @@ import type { Operation } from "effection"; import { SOURCE_POSITION_FIELD } from "@executablemd/core"; import { issueYields, runScenario, useScenarioFixture } from "./support/issue-scenario.ts"; import type { ScenarioObservation } from "./support/issue-scenario.ts"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; /** The scenarios, in the order a reader meets them. */ const SCENARIOS = [ diff --git a/packages/git/tests/support/run-composition.ts b/packages/git/tests/support/run-composition.ts index 328a415e9..6114f9e11 100644 --- a/packages/git/tests/support/run-composition.ts +++ b/packages/git/tests/support/run-composition.ts @@ -26,7 +26,7 @@ import { InMemoryStream } from "@executablemd/durable-streams"; import type { Json } from "@executablemd/durable-streams"; import { useTempDirectory } from "@executablemd/test-support/temp"; import { it } from "@executablemd/test-support/bdd"; -import { captureTestJournal } from "../../../test-support/failure-journal.ts"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; import { useCompositionComponents } from "../../src/composition/installation.ts"; import { RepositoryContext } from "../../src/composition/context.ts"; import type { RepositorySelection } from "../../src/composition/selection.ts"; diff --git a/packages/test-agent/tests/command-lazy.test.ts b/packages/test-agent/tests/command-lazy.test.ts index 42c2197e7..99b7c359a 100644 --- a/packages/test-agent/tests/command-lazy.test.ts +++ b/packages/test-agent/tests/command-lazy.test.ts @@ -5,7 +5,7 @@ * even where no entrypoint installed a command adapter. These suites install a * deliberately throwing adapter and assert what does and does not reach it. */ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { ensure, scoped } from "effection"; import type { Operation } from "effection"; @@ -16,7 +16,7 @@ import { ensureDir, rm, writeTextFile } from "@effectionx/fs"; import { API } from "@executablemd/runtime"; import { execute, useTempFileCompiler } from "@executablemd/core"; import { installAgentComponents } from "@executablemd/agent/host"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; import { useTesting } from "@executablemd/testing"; import type { TestResult } from "@executablemd/testing"; import { installTestAgentComponents } from "../src/components.ts"; diff --git a/packages/test-agent/tests/session-configuration.test.ts b/packages/test-agent/tests/session-configuration.test.ts index 745b70a3a..f56a82e95 100644 --- a/packages/test-agent/tests/session-configuration.test.ts +++ b/packages/test-agent/tests/session-configuration.test.ts @@ -17,7 +17,7 @@ import type { Operation, Result } from "effection"; import * as path from "node:path"; import process from "node:process"; import { Writable } from "node:stream"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; import type { DeliverySink } from "../../cli/src/stdout-delivery.ts"; import { installTestAgentComponents } from "../src/components.ts"; import { NativeSessionObserver } from "../src/controller.ts"; diff --git a/packages/testing/tests/agent-composition.test.ts b/packages/testing/tests/agent-composition.test.ts index 4fd87023f..2262f0d64 100644 --- a/packages/testing/tests/agent-composition.test.ts +++ b/packages/testing/tests/agent-composition.test.ts @@ -5,9 +5,9 @@ * outside a passing must still surface as the agent aggregate. * Lives in the testing package because core cannot depend on testing. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; import { ensure } from "effection"; import type { Result, Stream } from "effection"; import { ensureDir, rm, writeTextFile } from "@effectionx/fs"; @@ -60,6 +60,7 @@ function stubStream( } describe("agent + testing composition", () => { + beforeEach(journalBeforeEach("packages/testing/tests/agent-composition.test.ts")); const name = "both component sets decorate one execution"; it(name, function* () { const calls: string[] = []; @@ -115,29 +116,24 @@ describe("agent + testing composition", () => { ); // `` names durable work after its own invocation, so the host // declares it to the execution rather than registering it. - yield* captureTestJournal( - { owner: "packages/testing/tests/agent-composition.test.ts", name }, - docPath, - function* (stream) { - const execution = yield* executeInstalled({ path: docPath, stream }, [ - { components: agentIdentityComponents() }, - ]); - const subscription = yield* execution.output; - let next = yield* subscription.next(); - while (!next.done) { - next = yield* subscription.next(); - } - const result: Result = yield* execution; + const stream = yield* useJournalStream(docPath); + const execution = yield* executeInstalled({ path: docPath, stream }, [ + { components: agentIdentityComponents() }, + ]); + const subscription = yield* execution.output; + let next = yield* subscription.next(); + while (!next.done) { + next = yield* subscription.next(); + } + const result: Result = yield* execution; - expect(calls).toEqual(["good", "bad"]); - expect(result.ok).toBe(false); - if (!result.ok) { - expect(result.error).toBeInstanceOf(AggregateError); - if (result.error instanceof AggregateError) { - expect(result.error.message).toBe("1 agent prompt(s) failed"); - } - } - }, - ); + expect(calls).toEqual(["good", "bad"]); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error).toBeInstanceOf(AggregateError); + if (result.error instanceof AggregateError) { + expect(result.error.message).toBe("1 agent prompt(s) failed"); + } + } }); }); diff --git a/packages/testing/tests/assert-throws.test.ts b/packages/testing/tests/assert-throws.test.ts index ebe44fc56..c7a7a0275 100644 --- a/packages/testing/tests/assert-throws.test.ts +++ b/packages/testing/tests/assert-throws.test.ts @@ -1,13 +1,14 @@ -import { describe } from "@executablemd/test-support/bdd"; +import { beforeEach, describe } from "@executablemd/test-support/bdd"; import { scoped } from "effection"; import { expect } from "@executablemd/test-support/expect"; import { AssertionError } from "node:assert/strict"; import { Component } from "@executablemd/core"; import type { ComponentElement } from "@executablemd/core"; import { createTestHandlers } from "../src/handlers.ts"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { documentCases, failureOf } from "./helpers.ts"; -const documentCase = documentCases("packages/testing/tests/assert-throws.test.ts"); +const documentCase = documentCases(); const STRICT = [ "---", @@ -30,6 +31,7 @@ function inTest(body: string): Record { } describe("", () => { + beforeEach(journalBeforeEach("packages/testing/tests/assert-throws.test.ts")); documentCase("passes when the body raises an error matching a substring", function* (runFresh) { const run = yield* runFresh( inTest(''), diff --git a/packages/testing/tests/assertions.test.ts b/packages/testing/tests/assertions.test.ts index 36df03a15..26afd1c16 100644 --- a/packages/testing/tests/assertions.test.ts +++ b/packages/testing/tests/assertions.test.ts @@ -1,13 +1,15 @@ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { Component, useTempFileCompiler } from "@executablemd/core"; import { expect } from "@executablemd/test-support/expect"; import { AssertionError } from "node:assert/strict"; import { TestFailureError } from "../src/test-api.ts"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { documentCases, failureOf } from "./helpers.ts"; -const documentCase = documentCases("packages/testing/tests/assertions.test.ts"); +const documentCase = documentCases(); describe("assertion components", () => { + beforeEach(journalBeforeEach("packages/testing/tests/assertions.test.ts")); beforeAll(() => useTempFileCompiler()); documentCase("passing assertions in testing mode emit diagnostics", function* (runFresh) { const run = yield* runFresh( diff --git a/packages/testing/tests/boundary-contract.test.ts b/packages/testing/tests/boundary-contract.test.ts index b06ad186f..d94812e00 100644 --- a/packages/testing/tests/boundary-contract.test.ts +++ b/packages/testing/tests/boundary-contract.test.ts @@ -4,14 +4,16 @@ * the report from one that aborts the boundary and loses it. */ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "@executablemd/core"; import { expect } from "@executablemd/test-support/expect"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { documentCases } from "./helpers.ts"; -const documentCase = documentCases("packages/testing/tests/boundary-contract.test.ts"); +const documentCase = documentCases(); describe("Tier TC — the boundary", () => { + beforeEach(journalBeforeEach("packages/testing/tests/boundary-contract.test.ts")); beforeAll(() => useTempFileCompiler()); // The gap found while planning PR 3: `` does not intercept raises — @@ -75,6 +77,7 @@ describe("Tier TC — the boundary", () => { }); describe("Tier TC — what a keeps when it fails", () => { + beforeEach(journalBeforeEach("packages/testing/tests/boundary-contract.test.ts")); beforeAll(() => useTempFileCompiler()); // The behavior `content()` cannot express: a test that fails partway still diff --git a/packages/testing/tests/execution-harness.test.ts b/packages/testing/tests/execution-harness.test.ts index ef14cfae9..47b6d4dd1 100644 --- a/packages/testing/tests/execution-harness.test.ts +++ b/packages/testing/tests/execution-harness.test.ts @@ -20,7 +20,7 @@ import { useTempDirectory } from "@executablemd/test-support/temp"; import { join } from "node:path"; import process from "node:process"; import { Writable } from "node:stream"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; import type { DeliverySink } from "../../cli/src/stdout-delivery.ts"; import { readRecords } from "../../cli/src/repl/journal-records.ts"; import { useStubFs } from "@executablemd/runtime/test"; diff --git a/packages/testing/tests/helpers.ts b/packages/testing/tests/helpers.ts index d5669be17..de1d9d150 100644 --- a/packages/testing/tests/helpers.ts +++ b/packages/testing/tests/helpers.ts @@ -8,12 +8,11 @@ import { scoped } from "effection"; import type { Operation, Result } from "effection"; import { forEach } from "@effectionx/stream-helpers"; -import { InMemoryStream } from "@executablemd/durable-streams"; import type { DurableStream } from "@executablemd/durable-streams"; import { useStubFs } from "@executablemd/runtime/test"; import { execute } from "@executablemd/core"; import { it } from "@executablemd/test-support/bdd"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { captureTestJournal, useJournalStream } from "@executablemd/test-support/failure-journal"; import { useTesting } from "../src/use-testing.ts"; import { installHandlers, installTestingComponents } from "../src/components.ts"; import { activateTesting } from "../src/activation.ts"; @@ -48,9 +47,9 @@ type RunFreshDoc = ( options?: Omit, ) => Operation; -export function documentCases(owner: string) { +export function documentCases() { return (name: string, body: (run: RunFreshDoc) => Operation): void => { - it(name, () => withFreshDocument(owner, name, body)); + it(name, () => body((files, options) => runDoc(files, options))); }; } @@ -97,9 +96,10 @@ export function* runDoc( // Plain `execute()`: containment is core's, so a host attaches nothing to // get it (#441). + const path = options.path ?? "README.md"; const execution = yield* execute({ - path: options.path ?? "README.md", - stream: options.stream ?? new InMemoryStream(), + path, + stream: options.stream ?? (yield* useJournalStream(path)), }); const chunks: string[] = []; diff --git a/packages/testing/tests/test-component.test.ts b/packages/testing/tests/test-component.test.ts index 5ee77a0a6..bd2c9c669 100644 --- a/packages/testing/tests/test-component.test.ts +++ b/packages/testing/tests/test-component.test.ts @@ -6,7 +6,7 @@ * being made. */ -import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; +import { beforeAll, beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { ensure, scoped } from "effection"; import type { Operation } from "effection"; @@ -18,12 +18,14 @@ import { Component, registerComponents, useTempFileCompiler } from "@executablem import type { ErrorSegment } from "@executablemd/core"; import { createTestHandlers } from "../src/handlers.ts"; import { absorbTestFailure, Staging } from "../src/test-component.ts"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { documentCases, runDoc, withFreshDocument } from "./helpers.ts"; const OWNER = "packages/testing/tests/test-component.test.ts"; -const documentCase = documentCases(OWNER); +const documentCase = documentCases(); describe(" as a function component", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); /** An eval block whose retained work fails while the test is dismantled. */ diff --git a/packages/testing/tests/testing-boundary.test.ts b/packages/testing/tests/testing-boundary.test.ts index e88029219..3c7f377c7 100644 --- a/packages/testing/tests/testing-boundary.test.ts +++ b/packages/testing/tests/testing-boundary.test.ts @@ -8,15 +8,16 @@ * component replace `` entirely. */ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { scoped } from "effection"; import type { Operation } from "effection"; import { InMemoryStream } from "@executablemd/durable-streams"; import { registerComponents, useTempFileCompiler } from "@executablemd/core"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { documentCases, runDoc } from "./helpers.ts"; -const documentCase = documentCases("packages/testing/tests/testing-boundary.test.ts"); +const documentCase = documentCases(); import type { DocRun } from "./helpers.ts"; /** Every journalled boundary entry, by the name it recorded. */ @@ -125,6 +126,7 @@ describe("Tier TB — a boundary that never finished", () => { }); describe("Tier TB — a repository component replaces ", () => { + beforeEach(journalBeforeEach("packages/testing/tests/testing-boundary.test.ts")); beforeAll(() => useTempFileCompiler()); documentCase( diff --git a/packages/testing/tests/testing-mode.test.ts b/packages/testing/tests/testing-mode.test.ts index 06e586f0e..3093d9d7b 100644 --- a/packages/testing/tests/testing-mode.test.ts +++ b/packages/testing/tests/testing-mode.test.ts @@ -1,4 +1,4 @@ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "@executablemd/core"; import { expect } from "@executablemd/test-support/expect"; import { scoped, sleep } from "effection"; @@ -7,11 +7,13 @@ import { API } from "@executablemd/runtime"; import { useFailingExec } from "@executablemd/runtime/test"; import { TestFailureError } from "../src/test-api.ts"; import { createTestHandlers } from "../src/handlers.ts"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { documentCases, failureOf, runDoc } from "./helpers.ts"; -const documentCase = documentCases("packages/testing/tests/testing-mode.test.ts"); +const documentCase = documentCases(); describe("testing mode", () => { + beforeEach(journalBeforeEach("packages/testing/tests/testing-mode.test.ts")); beforeAll(() => useTempFileCompiler()); documentCase("skips entirely during regular execution", function* (runFresh) { const execCalls: string[] = []; diff --git a/packages/testing/tests/use-testing.test.ts b/packages/testing/tests/use-testing.test.ts index a2099053f..35fc75541 100644 --- a/packages/testing/tests/use-testing.test.ts +++ b/packages/testing/tests/use-testing.test.ts @@ -1,4 +1,4 @@ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/bdd"; import { useTempFileCompiler } from "@executablemd/core"; import { expect } from "@executablemd/test-support/expect"; import { when } from "@effectionx/converge"; @@ -15,11 +15,12 @@ import { execute, Execution, registerComponents } from "@executablemd/core"; import type { Json } from "@executablemd/core"; import { Test, TestFailureError, testing } from "../src/test-api.ts"; import { useTesting } from "../src/use-testing.ts"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { documentCases, failureOf, runDoc, withFreshDocument } from "./helpers.ts"; import { journalCases } from "../../test-support/journal-cases.ts"; const OWNER = "packages/testing/tests/use-testing.test.ts"; -const documentCase = documentCases(OWNER); +const documentCase = documentCases(); const journalCase = journalCases(OWNER); function* drain( @@ -35,6 +36,7 @@ function* drain( } describe("useTesting composition", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); const journalWitness = "attributes a fresh document when its caller fails after execution"; diff --git a/packages/web/tests/component-support.ts b/packages/web/tests/component-support.ts index 2e3a5a59c..6eef07c61 100644 --- a/packages/web/tests/component-support.ts +++ b/packages/web/tests/component-support.ts @@ -19,7 +19,7 @@ import { mkdtemp, realpath } from "node:fs/promises"; import process from "node:process"; import { basename, join } from "node:path"; import { rm, writeTextFile } from "@effectionx/fs"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; import type { JournalCase } from "../../cli/src/test-diagnostics.ts"; import { FormAssets } from "../src/assets.ts"; diff --git a/packages/web/tests/document.test.ts b/packages/web/tests/document.test.ts index 501d6e185..2d6a2a743 100644 --- a/packages/web/tests/document.test.ts +++ b/packages/web/tests/document.test.ts @@ -1,9 +1,9 @@ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { scoped } from "effection"; import { readTextFile } from "@effectionx/fs"; import { collect, execute, registerComponents, useTempFileCompiler } from "@executablemd/core"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; import { useTesting } from "@executablemd/testing"; import { fileURLToPath } from "node:url"; @@ -24,60 +24,56 @@ const DOCUMENT = fileURLToPath(new URL("../src/WebForm.test.md", import.meta.url * is not coming. */ describe("WebForm.test.md", () => { + beforeEach(journalBeforeEach("packages/web/tests/document.test.ts")); const name = "passes with an installed responder answering its forms"; - it(name, () => - captureTestJournal( - { owner: "packages/web/tests/document.test.ts", name }, - "WebForm.test.md", - function* (stream) { - const source = yield* readTextFile(DOCUMENT); + it(name, function* () { + const source = yield* readTextFile(DOCUMENT); + const stream = yield* useJournalStream("WebForm.test.md"); - const outcome = yield* scoped(function* () { - yield* useTempFileCompiler(); - yield* useStubFs({ "WebForm.test.md": source }); + const outcome = yield* scoped(function* () { + yield* useTempFileCompiler(); + yield* useStubFs({ "WebForm.test.md": source }); - yield* FormAssets.around({ - // deno-lint-ignore require-yield - *assets() { - return { clientJs: "/* fixture */", themeCss: "/* fixture */" }; - }, - }); - yield* FormOpener.around({ - // deno-lint-ignore require-yield - *open() {}, - }); - yield* FormResponder.around({ - *respond([url]) { - yield* submitForm(url, { decision: "approve", note: "looks right" }); - }, - }); + yield* FormAssets.around({ + // deno-lint-ignore require-yield + *assets() { + return { clientJs: "/* fixture */", themeCss: "/* fixture */" }; + }, + }); + yield* FormOpener.around({ + // deno-lint-ignore require-yield + *open() {}, + }); + yield* FormResponder.around({ + *respond([url]) { + yield* submitForm(url, { decision: "approve", note: "looks right" }); + }, + }); - yield* useTesting(); - yield* registerComponents([ - { - name: "WebForm", - origin: "@executablemd/web", - fn: WebForm, - props: WEB_FORM_PROPS, - returns: WEB_FORM_RETURNS, - }, - ]); + yield* useTesting(); + yield* registerComponents([ + { + name: "WebForm", + origin: "@executablemd/web", + fn: WebForm, + props: WEB_FORM_PROPS, + returns: WEB_FORM_RETURNS, + }, + ]); - const execution = yield* execute({ - path: "WebForm.test.md", - stream, - }); + const execution = yield* execute({ + path: "WebForm.test.md", + stream, + }); - try { - yield* collect(execution); - return { ok: true, message: "" }; - } catch (error) { - return { ok: false, message: error instanceof Error ? error.message : String(error) }; - } - }); + try { + yield* collect(execution); + return { ok: true, message: "" }; + } catch (error) { + return { ok: false, message: error instanceof Error ? error.message : String(error) }; + } + }); - expect(outcome).toEqual({ ok: true, message: "" }); - }, - ), - ); + expect(outcome).toEqual({ ok: true, message: "" }); + }); }); From 3cf19ef8b367bf405d2b044806b074d415d3e968 Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Mon, 16 Nov 2026 22:15:48 -0500 Subject: [PATCH 05/11] Complete shared journal imports in #1356 --- packages/core/tests/streaming-emission.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/tests/streaming-emission.test.ts b/packages/core/tests/streaming-emission.test.ts index 826e4fc9b..c2cd79b85 100644 --- a/packages/core/tests/streaming-emission.test.ts +++ b/packages/core/tests/streaming-emission.test.ts @@ -14,7 +14,7 @@ import type { Operation } from "effection"; import { join } from "node:path"; import process from "node:process"; import { Writable } from "node:stream"; -import { captureTestJournal } from "../../test-support/failure-journal.ts"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; import type { DeliverySink } from "../../cli/src/stdout-delivery.ts"; import { readRecords } from "../../cli/src/repl/journal-records.ts"; import { forEach } from "@effectionx/stream-helpers"; From 55c53bda5e9fa99ec8cfd694559812e994bd1d2e Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Mon, 16 Nov 2026 22:30:29 -0500 Subject: [PATCH 06/11] Integrate Luna formatting correction for #1356 --- packages/core/tests/each.test.ts | 5 +- packages/core/tests/ephemeral-service.test.ts | 5 +- .../core/tests/generated-xmd-root.test.ts | 6 +- packages/core/tests/if.test.ts | 5 +- packages/core/tests/let.test.ts | 5 +- packages/core/tests/loop.test.ts | 41 +++---- packages/core/tests/sample-component.test.ts | 109 +++++++++--------- packages/core/tests/source-position.test.ts | 5 +- packages/core/tests/switch.test.ts | 5 +- .../core/tests/validation-integration.test.ts | 33 +++--- 10 files changed, 94 insertions(+), 125 deletions(-) diff --git a/packages/core/tests/each.test.ts b/packages/core/tests/each.test.ts index 1e50fa470..f6e3dba5f 100644 --- a/packages/core/tests/each.test.ts +++ b/packages/core/tests/each.test.ts @@ -11,10 +11,7 @@ import { useStubFs, useEchoExec } from "@executablemd/runtime/test"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import type { CodeBlockResult, Segment } from "../src/types.ts"; -import { - journalBeforeEach, - useJournalStream, -} from "@executablemd/test-support/failure-journal"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; interface EachRun { segments: Segment[]; diff --git a/packages/core/tests/ephemeral-service.test.ts b/packages/core/tests/ephemeral-service.test.ts index 97f90594b..990f2ca32 100644 --- a/packages/core/tests/ephemeral-service.test.ts +++ b/packages/core/tests/ephemeral-service.test.ts @@ -17,10 +17,7 @@ import { collect } from "../src/collect.ts"; import { execute } from "../src/execute.ts"; import { LiveBindingCollisionError } from "../src/live-env.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; -import { - journalBeforeEach, - useJournalStream, -} from "@executablemd/test-support/failure-journal"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; const SAMPLE = `--- meta: diff --git a/packages/core/tests/generated-xmd-root.test.ts b/packages/core/tests/generated-xmd-root.test.ts index 915166c09..460fe7b9f 100644 --- a/packages/core/tests/generated-xmd-root.test.ts +++ b/packages/core/tests/generated-xmd-root.test.ts @@ -30,10 +30,7 @@ import { beforeAll, describe, it, beforeEach } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; -import { - journalBeforeEach, - useJournalStream, -} from "@executablemd/test-support/failure-journal"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; import { Err, Ok, scoped } from "effection"; import type { Operation, Result } from "effection"; import { API } from "@executablemd/runtime"; @@ -77,7 +74,6 @@ import type { } from "../src/generated-xmd.ts"; import type { FunctionComponent, FunctionComponentDefinition, Json } from "../src/types.ts"; - const ROOT_PATH = "repl/sidekick.md"; const ROOT_SOURCE = "The host evaluated a generated root.\n"; const URL_ONE = "https://api.example.test/one"; diff --git a/packages/core/tests/if.test.ts b/packages/core/tests/if.test.ts index 9616feccf..f4b233952 100644 --- a/packages/core/tests/if.test.ts +++ b/packages/core/tests/if.test.ts @@ -21,10 +21,7 @@ import { Sample } from "../src/sample-api.ts"; import { readTextFile } from "@effectionx/fs"; import type { ComponentElement, FunctionComponentDefinition, Segment } from "../src/types.ts"; import { asText } from "./helpers.ts"; -import { - journalBeforeEach, - useJournalStream, -} from "@executablemd/test-support/failure-journal"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; interface IfRun { segments: Segment[]; diff --git a/packages/core/tests/let.test.ts b/packages/core/tests/let.test.ts index c92f9e376..91a64ca53 100644 --- a/packages/core/tests/let.test.ts +++ b/packages/core/tests/let.test.ts @@ -23,10 +23,7 @@ import { Component } from "../src/component-api.ts"; import { scanSegments } from "../src/scanner.ts"; import { renderSegments } from "../src/render.ts"; import { execute, executeInstalled } from "../src/execute.ts"; -import { - journalBeforeEach, - useJournalStream, -} from "@executablemd/test-support/failure-journal"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; import type { HostExecution } from "../src/execute.ts"; import { inlineSource } from "../src/root-source.ts"; import { collect } from "../src/collect.ts"; diff --git a/packages/core/tests/loop.test.ts b/packages/core/tests/loop.test.ts index 57e8e54cd..fdee4eba7 100644 --- a/packages/core/tests/loop.test.ts +++ b/packages/core/tests/loop.test.ts @@ -19,10 +19,7 @@ import { collect } from "../src/collect.ts"; import { printErrors } from "../src/component-failures.ts"; import type { ComponentElement, FunctionComponent, Segment } from "../src/types.ts"; import { asText } from "./helpers.ts"; -import { - journalBeforeEach, - useJournalStream, -} from "@executablemd/test-support/failure-journal"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; interface LoopRun { segments: Segment[]; @@ -753,24 +750,24 @@ describe("Tier LOOP — document execution", () => { const stream = yield* useJournalStream("test.md"); yield* useHostFiles(); yield* useStubFs({ - "test.md": [ - "```js eval", - "const marks = [];", - "```", - "", - "", - "```js eval", - "marks.push(marks.length);", - "```", - "", - "", - "", - "tally={marks.length}", - "", - "```js eval", - "output(`TALLY:${marks.join(',')}`);", - "```", - ].join("\n"), + "test.md": [ + "```js eval", + "const marks = [];", + "```", + "", + "", + "```js eval", + "marks.push(marks.length);", + "```", + "", + "", + "", + "tally={marks.length}", + "", + "```js eval", + "output(`TALLY:${marks.join(',')}`);", + "```", + ].join("\n"), }); yield* useEchoExec(); diff --git a/packages/core/tests/sample-component.test.ts b/packages/core/tests/sample-component.test.ts index 5e4a1ece8..e71af1dab 100644 --- a/packages/core/tests/sample-component.test.ts +++ b/packages/core/tests/sample-component.test.ts @@ -23,10 +23,7 @@ import { ensureDir, readTextFile, writeTextFile } from "@effectionx/fs"; import { useTempDirectory } from "@executablemd/test-support/temp"; import type { Operation } from "effection"; import * as path from "node:path"; -import { - journalBeforeEach, - useJournalStream, -} from "@executablemd/test-support/failure-journal"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; function* writeFiles(dir: string, files: Record): Operation { for (const [filePath, content] of Object.entries(files)) { @@ -423,15 +420,15 @@ describe("Tier EO — eval output() function", () => { it("EO1: output() produces eval block output", function* () { const stream = yield* useJournalStream("test.md"); yield* useStubFs({ - "test.md": '```js eval\noutput("hello from eval");\n```\n', + "test.md": '```js eval\noutput("hello from eval");\n```\n', }); yield* useEchoExec(); const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), + yield* execute({ + path: "test.md", + stream, + }), ); expect(output).toContain("hello from eval"); @@ -470,15 +467,15 @@ describe("Tier EO — eval output() function", () => { it("EO3: eval block without output() produces no output", function* () { const stream = yield* useJournalStream("test.md"); yield* useStubFs({ - "test.md": "```js eval\nconst x = 42;\n```\nafter-eval", + "test.md": "```js eval\nconst x = 42;\n```\nafter-eval", }); yield* useEchoExec(); const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), + yield* execute({ + path: "test.md", + stream, + }), ); // No output from eval block — should see "after-eval" but no "42" @@ -490,15 +487,15 @@ describe("Tier EO — eval output() function", () => { it("EO4: output() with multiline content", function* () { const stream = yield* useJournalStream("test.md"); yield* useStubFs({ - "test.md": '```js eval\noutput("line1\\nline2\\nline3");\n```\n', + "test.md": '```js eval\noutput("line1\\nline2\\nline3");\n```\n', }); yield* useEchoExec(); const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), + yield* execute({ + path: "test.md", + stream, + }), ); expect(output).toContain("line1\nline2\nline3"); @@ -508,15 +505,15 @@ describe("Tier EO — eval output() function", () => { it("EO5: output() converts non-string to string", function* () { const stream = yield* useJournalStream("test.md"); yield* useStubFs({ - "test.md": "```js eval\noutput(12345);\n```\n", + "test.md": "```js eval\noutput(12345);\n```\n", }); yield* useEchoExec(); const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), + yield* execute({ + path: "test.md", + stream, + }), ); expect(output).toContain("12345"); @@ -559,26 +556,26 @@ describe("Tier RC — renderChildren and render closures", () => { it("RC2: renderChildren captures children text", function* () { const stream = yield* useJournalStream("test.md"); yield* useStubFs({ - "components/TestComp.md": [ - "---", - "meta:", - " componentName: TestComp", - "---", - "", - "```js eval", - "const result = yield* renderChildren();", - "output('children:[' + result.trim() + ']');", - "```", - ].join("\n"), - "test.md": ["", "", "hello from children", "", ""].join("\n"), + "components/TestComp.md": [ + "---", + "meta:", + " componentName: TestComp", + "---", + "", + "```js eval", + "const result = yield* renderChildren();", + "output('children:[' + result.trim() + ']');", + "```", + ].join("\n"), + "test.md": ["", "", "hello from children", "", ""].join("\n"), }); yield* useEchoExec(); const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), + yield* execute({ + path: "test.md", + stream, + }), ); expect(output).toContain("children:[hello from children]"); @@ -588,26 +585,26 @@ describe("Tier RC — renderChildren and render closures", () => { it("RC3: render() expands arbitrary markdown string", function* () { const stream = yield* useJournalStream("test.md"); yield* useStubFs({ - "components/TestComp.md": [ - "---", - "meta:", - " componentName: TestComp", - "---", - "", - "```js eval", - "const result = yield* render('arbitrary **markdown** content');", - "output('rendered:[' + result.trim() + ']');", - "```", - ].join("\n"), - "test.md": "", + "components/TestComp.md": [ + "---", + "meta:", + " componentName: TestComp", + "---", + "", + "```js eval", + "const result = yield* render('arbitrary **markdown** content');", + "output('rendered:[' + result.trim() + ']');", + "```", + ].join("\n"), + "test.md": "", }); yield* useEchoExec(); const output = yield* collect( - yield* execute({ - path: "test.md", - stream, - }), + yield* execute({ + path: "test.md", + stream, + }), ); expect(output).toContain("rendered:[arbitrary **markdown** content]"); diff --git a/packages/core/tests/source-position.test.ts b/packages/core/tests/source-position.test.ts index d5ba4e16c..27496de6e 100644 --- a/packages/core/tests/source-position.test.ts +++ b/packages/core/tests/source-position.test.ts @@ -8,10 +8,7 @@ import { registerComponents } from "../src/components/registration.ts"; import { execute } from "../src/execute.ts"; import { collect } from "../src/collect.ts"; import type { ComponentElement, SourcePosition } from "../src/types.ts"; -import { - journalBeforeEach, - useJournalStream, -} from "@executablemd/test-support/failure-journal"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; function componentsOf(segments: ReturnType): ComponentElement[] { const found: ComponentElement[] = []; diff --git a/packages/core/tests/switch.test.ts b/packages/core/tests/switch.test.ts index 7fff5f637..9299cfbf8 100644 --- a/packages/core/tests/switch.test.ts +++ b/packages/core/tests/switch.test.ts @@ -22,10 +22,7 @@ import type { SourceOrigin } from "../src/scanner.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; import type { ComponentElement, FunctionComponentDefinition, Segment } from "../src/types.ts"; import { asText } from "./helpers.ts"; -import { - journalBeforeEach, - useJournalStream, -} from "@executablemd/test-support/failure-journal"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; interface SwitchRun { segments: Segment[]; diff --git a/packages/core/tests/validation-integration.test.ts b/packages/core/tests/validation-integration.test.ts index 59c8812fc..f46d1bc95 100644 --- a/packages/core/tests/validation-integration.test.ts +++ b/packages/core/tests/validation-integration.test.ts @@ -12,10 +12,7 @@ import { asText } from "./helpers.ts"; import { parseFrontmatter } from "../src/frontmatter.ts"; import { compilePropsSchema } from "../src/validate.ts"; import type { ComponentDefinition, EvalEnv, Json, Segment } from "../src/types.ts"; -import { - journalBeforeEach, - useJournalStream, -} from "@executablemd/test-support/failure-journal"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; function markdownComponent(name: string, props: Record): ComponentDefinition { return { @@ -108,24 +105,24 @@ describe("`inputs` is not a compatibility alias for `props`", () => { it("a component declaring `inputs` declares no props", function* () { const journal = yield* useJournalStream("README.md"); yield* useStubFs({ - "README.md": '\n', - "Legacy.md": [ - "---", - "inputs:", - " type: object", - " properties:", - " name: { type: string }", - " additionalProperties: false", - "---", - "Hello, {props.name}", - "", - ].join("\n"), + "README.md": '\n', + "Legacy.md": [ + "---", + "inputs:", + " type: object", + " properties:", + " name: { type: string }", + " additionalProperties: false", + "---", + "Hello, {props.name}", + "", + ].join("\n"), }); let message = ""; try { - message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); + message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); } catch (error) { - message = error instanceof Error ? error.message : String(error); + message = error instanceof Error ? error.message : String(error); } expect(message).toContain("Prop validation failed"); }); From 848cceee055f138b5e79d65711b12c346d2da0ff Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Tue, 17 Nov 2026 00:06:46 -0500 Subject: [PATCH 07/11] Restore direct test declarations in Agent, REPL and Web --- packages/agent/tests/agent-components.test.ts | 276 ++++----- packages/cli/tests/repl-execution.test.ts | 558 +++++++++--------- packages/web/tests/component.test.ts | 158 +++-- 3 files changed, 511 insertions(+), 481 deletions(-) diff --git a/packages/agent/tests/agent-components.test.ts b/packages/agent/tests/agent-components.test.ts index d3f721c6e..831701690 100644 --- a/packages/agent/tests/agent-components.test.ts +++ b/packages/agent/tests/agent-components.test.ts @@ -180,48 +180,34 @@ function* runDoc( const SUITE = "Tier AC — agent components"; -function agentCase( - name: string, - body: (run: (doc: string) => ReturnType) => Operation, -): void { - it(name, function* () { - yield* body((doc) => runDoc(doc)); - }); -} - describe(SUITE, () => { beforeEach(journalBeforeEach(OWNER)); - agentCase( - "AC1: wrapper always sends rendered children, untrimmed", - function* (runFresh) { - const stub = createStubProvider(); - yield* installStub(stub); - const { output, result } = yield* runFresh( - '\nSay hello\n\n', - ); - expect(result.ok).toBe(true); - expect(stub.promptCalls.length).toBe(1); - const sent = stub.promptCalls[0]!.content; - expect(sent).toContain("Say hello"); - expect(sent).not.toBe("fallback"); - expect(sent).not.toBe(sent.trim()); - expect(output).toContain("[stub-agent:stub:default:"); - }, - ); + it("AC1: wrapper always sends rendered children, untrimmed", function* () { + const stub = createStubProvider(); + yield* installStub(stub); + const { output, result } = yield* runDoc('\nSay hello\n\n'); + expect(result.ok).toBe(true); + expect(stub.promptCalls.length).toBe(1); + const sent = stub.promptCalls[0]!.content; + expect(sent).toContain("Say hello"); + expect(sent).not.toBe("fallback"); + expect(sent).not.toBe(sent.trim()); + expect(output).toContain("[stub-agent:stub:default:"); + }); - agentCase("AC2: empty wrapper children win over the text prop", function* (runFresh) { + it("AC2: empty wrapper children win over the text prop", function* () { const stub = createStubProvider(); yield* installStub(stub); - const { result } = yield* runFresh('\n'); + const { result } = yield* runDoc('\n'); expect(result.ok).toBe(true); expect(stub.promptCalls.length).toBe(1); expect(stub.promptCalls[0]!.content).toBe(""); }); - agentCase("AC3: self-closing falls back to the text prop", function* (runFresh) { + it("AC3: self-closing falls back to the text prop", function* () { const stub = createStubProvider(); yield* installStub(stub); - const { output, result } = yield* runFresh('\n'); + const { output, result } = yield* runDoc('\n'); expect(result.ok).toBe(true); expect(stub.promptCalls[0]!.content).toBe("hello"); expect(output).toContain("[stub-agent:stub:default:hello]"); @@ -231,10 +217,10 @@ describe(SUITE, () => { // function component: it names the component and lists the offending prop, // where the claimed handler built the same account behind a ` ` // prefix of its own. - agentCase("AC19: the removed prompt prop is rejected as an unknown prop", function* (runFresh) { + it("AC19: the removed prompt prop is rejected as an unknown prop", function* () { const stub = createStubProvider(); yield* installStub(stub); - const { result } = yield* runFresh('\n'); + const { result } = yield* runDoc('\n'); expect(result.ok).toBe(false); expect(stub.promptCalls.length).toBe(0); const reported = result.ok ? "" : result.error.message; @@ -242,28 +228,23 @@ describe(SUITE, () => { expect(reported).toContain("prompt"); }); - agentCase( - "AC20: a component's own validation printed error is observed once", - function* (runFresh) { - const observed: string[] = []; - const stub = createStubProvider(); - yield* installStub(stub); - yield* Component.around({ - *raise([error], next) { - observed.push(error.message); - return yield* next(error); - }, - }); - // Written in a region that prints, because what this pins is that the - // diagnostic is observed once and rendered once — which needs it rendered. - const { output } = yield* runFresh( - '\n\n\n', - ); - expect(observed).toHaveLength(1); - expect(observed[0]).toContain("Prop validation failed for "); - expect(output.match(//g)).toHaveLength(1); - }, - ); + it("AC20: a component's own validation printed error is observed once", function* () { + const observed: string[] = []; + const stub = createStubProvider(); + yield* installStub(stub); + yield* Component.around({ + *raise([error], next) { + observed.push(error.message); + return yield* next(error); + }, + }); + // Written in a region that prints, because what this pins is that the + // diagnostic is observed once and rendered once — which needs it rendered. + const { output } = yield* runDoc('\n\n\n'); + expect(observed).toHaveLength(1); + expect(observed[0]).toContain("Prop validation failed for "); + expect(output.match(//g)).toHaveLength(1); + }); // The composition the whole component exists for, black box: a value bound by // ``, rendered by ``, consumed and retained by ``. Import @@ -297,10 +278,10 @@ describe(SUITE, () => { } }); - agentCase("AC4: as binding captures the response instead of emitting it", function* (runFresh) { + it("AC4: as binding captures the response instead of emitting it", function* () { const stub = createStubProvider(); yield* installStub(stub); - const { output, result } = yield* runFresh( + const { output, result } = yield* runDoc( '\n\nCaptured: {answer}\n', ); expect(result.ok).toBe(true); @@ -308,110 +289,101 @@ describe(SUITE, () => { expect(output.indexOf("[stub-agent")).toBe(output.lastIndexOf("[stub-agent")); }); - agentCase( - "AC5: agent and session scopes compose; per-prompt props override", - function* (runFresh) { - const stub = createStubProvider(); - yield* installStub(stub); - const doc = [ - '', - ' ', - ' ', - " ", - "", - "", - '', - "", - ].join("\n"); - const { result } = yield* runFresh(doc); - expect(result.ok).toBe(true); - expect(stub.promptCalls.length).toBe(2); - expect(stub.promptCalls[0]).toMatchObject({ - content: "one", - agent: "agent-two", - sessionKey: "stub:review", - }); - expect(stub.promptCalls[1]).toMatchObject({ - content: "two", - agent: "agent-three", - sessionKey: "stub:named", - timeout: 500, - }); - }, - ); - - agentCase( - "AC6: self-closing and validate without output", - function* (runFresh) { - const stub = createStubProvider(); - yield* installStub(stub); - const { output, result } = yield* runFresh( - '\n\nplain text\n', - ); - expect(result.ok).toBe(true); - expect(stub.agentProbes).toContain("probe-me"); - expect(stub.promptCalls.length).toBe(0); - expect(output).toContain("plain text"); - expect(output).not.toContain("probe-me"); - }, - ); - - agentCase( - "AC7: failed prompts render partial text, later content runs, completion aggregates in order", - function* (runFresh) { - const stub = createStubProvider((content) => { - if (content.includes("first-fail")) { - return { - status: "failed", - stopReason: "max_tokens", - deltas: ["partial-one "], - }; - } - if (content.includes("second-fail")) { - return { status: "failed", stopReason: "refusal", deltas: [] }; - } - return {}; - }); - yield* installStub(stub); - const doc = [ - '', - "", - '', - "", - '', - "", - "after all prompts", - "", - ].join("\n"); - const { output, result } = yield* runFresh(doc); - expect(output).toContain("partial-one"); - expect(output).toContain("after all prompts"); - expect(result.ok).toBe(false); - if (!result.ok) { - expect(result.error).toBeInstanceOf(AggregateError); - if (result.error instanceof AggregateError) { - expect(result.error.message).toBe("2 agent prompt(s) failed"); - const [first, second] = result.error.errors; - expect(first).toBeInstanceOf(AgentPromptError); - expect(second).toBeInstanceOf(AgentPromptError); - if (first instanceof AgentPromptError && second instanceof AgentPromptError) { - expect(first.stopReason).toBe("max_tokens"); - expect(first.agent).toBe("stub-agent"); - expect(first.sessionKey).toBe("stub:default"); - expect(second.stopReason).toBe("refusal"); - } + it("AC5: agent and session scopes compose; per-prompt props override", function* () { + const stub = createStubProvider(); + yield* installStub(stub); + const doc = [ + '', + ' ', + ' ', + " ", + "", + "", + '', + "", + ].join("\n"); + const { result } = yield* runDoc(doc); + expect(result.ok).toBe(true); + expect(stub.promptCalls.length).toBe(2); + expect(stub.promptCalls[0]).toMatchObject({ + content: "one", + agent: "agent-two", + sessionKey: "stub:review", + }); + expect(stub.promptCalls[1]).toMatchObject({ + content: "two", + agent: "agent-three", + sessionKey: "stub:named", + timeout: 500, + }); + }); + + it("AC6: self-closing and validate without output", function* () { + const stub = createStubProvider(); + yield* installStub(stub); + const { output, result } = yield* runDoc( + '\n\nplain text\n', + ); + expect(result.ok).toBe(true); + expect(stub.agentProbes).toContain("probe-me"); + expect(stub.promptCalls.length).toBe(0); + expect(output).toContain("plain text"); + expect(output).not.toContain("probe-me"); + }); + + it("AC7: failed prompts render partial text, later content runs, completion aggregates in order", function* () { + const stub = createStubProvider((content) => { + if (content.includes("first-fail")) { + return { + status: "failed", + stopReason: "max_tokens", + deltas: ["partial-one "], + }; + } + if (content.includes("second-fail")) { + return { status: "failed", stopReason: "refusal", deltas: [] }; + } + return {}; + }); + yield* installStub(stub); + const doc = [ + '', + "", + '', + "", + '', + "", + "after all prompts", + "", + ].join("\n"); + const { output, result } = yield* runDoc(doc); + expect(output).toContain("partial-one"); + expect(output).toContain("after all prompts"); + expect(result.ok).toBe(false); + if (!result.ok) { + expect(result.error).toBeInstanceOf(AggregateError); + if (result.error instanceof AggregateError) { + expect(result.error.message).toBe("2 agent prompt(s) failed"); + const [first, second] = result.error.errors; + expect(first).toBeInstanceOf(AgentPromptError); + expect(second).toBeInstanceOf(AgentPromptError); + if (first instanceof AgentPromptError && second instanceof AgentPromptError) { + expect(first.stopReason).toBe("max_tokens"); + expect(first.agent).toBe("stub-agent"); + expect(first.sessionKey).toBe("stub:default"); + expect(second.stopReason).toBe("refusal"); } } - }, - ); + } + }); - agentCase("AC8: throwOnError aborts the document immediately", function* (runFresh) { + it("AC8: throwOnError aborts the document immediately", function* () { const stub = createStubProvider(() => ({ status: "failed", stopReason: "refusal", })); yield* installStub(stub); - const { output, result } = yield* runFresh( + const { output, result } = yield* runDoc( '\n\nnever reached\n', ); expect(result.ok).toBe(false); diff --git a/packages/cli/tests/repl-execution.test.ts b/packages/cli/tests/repl-execution.test.ts index ebfdc7497..9ed0feceb 100644 --- a/packages/cli/tests/repl-execution.test.ts +++ b/packages/cli/tests/repl-execution.test.ts @@ -265,12 +265,6 @@ function captureReferenceEntry( ); } -function referenceCases(suite: string) { - return (name: string, body: (holder: ReplExecution) => Operation): void => { - it(name, () => captureReferenceEntry(name, body, process.stderr, suite)); - }; -} - function admitReferenceEntry( name: string, check?: (holder: ReplExecution) => Operation, @@ -328,8 +322,6 @@ function admitReferenceEntry( describe(SUBMIT_SUITE, () => { beforeAll(() => useTempFileCompiler()); - const referenceCase = referenceCases(SUBMIT_SUITE); - const admission = "X1: admits the exact source, binds, generates, asks, and settles"; it(admission, () => admitReferenceEntry(admission)); @@ -475,68 +467,74 @@ describe(SUBMIT_SUITE, () => { expect(lines).toHaveLength(1); }); - referenceCase( - "X1: the generated fragment is shown before it is admitted, and follows the binding", - function* (holder) { - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - (yield* nextQuestion(session)).submit({ decision: "decline" }); - yield* session.join(); - - const generated = session.model.entries[0]?.scope.generated[0]; - const fragment = session.model.entries[0]?.scope.scopes.find( - (scope) => scope.kind === "generated", - ); - expect(generated?.decision).toBe("admitted"); - // The value the durable evaluation published is what the fragment says, - // which is what makes it deterministic rather than merely repeatable. - expect(fragment?.source).toBe(''); - expect(session.model.terminal?.output).toContain("Decision: decline"); - }, - ); - - referenceCase( - "X1: shows the generated source before admitting it, then admits and evaluates it", - function* (holder) { - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - const fragment = ''; - - // Stepped rather than inspected at the end: a terminal model shows that - // both things happened and says nothing about which came first. Pausing at - // each boundary is what lets the order itself be observed. - controlling(session).pause(); - let shownBeforeAdmission = false; - let admitted = false; - for (let step = 0; step < 500 && session.live && !admitted; step++) { - yield* pausedOrDone(session); - if (!session.live) { - break; - } - admitted = counted(yield* readAll(holder), "generated_xmd") > 0; - if (!admitted && session.overlay.output.includes(fragment)) { - shownBeforeAdmission = true; - } - controlling(session).resume(); + it("X1: the generated fragment is shown before it is admitted, and follows the binding", () => + captureReferenceEntry( + "X1: the generated fragment is shown before it is admitted, and follows the binding", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + (yield* nextQuestion(session)).submit({ decision: "decline" }); + yield* session.join(); + + const generated = session.model.entries[0]?.scope.generated[0]; + const fragment = session.model.entries[0]?.scope.scopes.find( + (scope) => scope.kind === "generated", + ); + expect(generated?.decision).toBe("admitted"); + // The value the durable evaluation published is what the fragment says, + // which is what makes it deterministic rather than merely repeatable. + expect(fragment?.source).toBe(''); + expect(session.model.terminal?.output).toContain("Decision: decline"); + }, + process.stderr, + SUBMIT_SUITE, + )); + + it("X1: shows the generated source before admitting it, then admits and evaluates it", () => + captureReferenceEntry( + "X1: shows the generated source before admitting it, then admits and evaluates it", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + const fragment = ''; + + // Stepped rather than inspected at the end: a terminal model shows that + // both things happened and says nothing about which came first. Pausing at + // each boundary is what lets the order itself be observed. controlling(session).pause(); - } - - expect(shownBeforeAdmission).toBe(true); - expect(admitted).toBe(true); + let shownBeforeAdmission = false; + let admitted = false; + for (let step = 0; step < 500 && session.live && !admitted; step++) { + yield* pausedOrDone(session); + if (!session.live) { + break; + } + admitted = counted(yield* readAll(holder), "generated_xmd") > 0; + if (!admitted && session.overlay.output.includes(fragment)) { + shownBeforeAdmission = true; + } + controlling(session).resume(); + controlling(session).pause(); + } - controlling(session).resume(); - (yield* nextQuestion(session)).submit({ decision: "approve" }); - yield* session.join(); + expect(shownBeforeAdmission).toBe(true); + expect(admitted).toBe(true); - // The admitted fragment then ran where it was written: its rendering - // follows the source it was rendered from. - const settled = session.model.terminal?.output ?? ""; - expect(settled.indexOf(fragment)).toBeGreaterThan(-1); - expect(settled.indexOf('"title": "Ship the REPL"')).toBeGreaterThan( - settled.indexOf(fragment), - ); - }, - ); + controlling(session).resume(); + (yield* nextQuestion(session)).submit({ decision: "approve" }); + yield* session.join(); + + // The admitted fragment then ran where it was written: its rendering + // follows the source it was rendered from. + const settled = session.model.terminal?.output ?? ""; + expect(settled.indexOf(fragment)).toBeGreaterThan(-1); + expect(settled.indexOf('"title": "Ship the REPL"')).toBeGreaterThan( + settled.indexOf(fragment), + ); + }, + process.stderr, + SUBMIT_SUITE, + )); it("X1: an entry this environment cannot run leaves the history empty", function* () { const holder = execution(); @@ -551,33 +549,36 @@ describe(SUBMIT_SUITE, () => { expect(yield* holder.stream.readAll()).toEqual([]); }); - referenceCase( - "X1: a choice the form does not offer answers nothing and leaves the question open", - function* (holder) { - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - - const question = yield* nextQuestion(session); - // Not an offered value, so the schema rejects it: the question stays open and - // nothing is recorded. - expect(question.submit({ decision: "maybe" }).kind).toBe("invalid"); - yield* sleep(10); - - // An incomplete interaction, not a rejected answer. The provider never - // returned, so there is nothing for core to judge and nothing to record. - // The other case — a provider that really hands back a value the schema - // rejects — settles the durable operation `err` with no answer, and E1 - // proves that in `packages/core/tests/elicit-component.test.ts`. This case - // does not prove that one. - expect(session.elicitation.pending).toBe(question); - expect(counted(yield* readAll(holder), "elicit")).toBe(0); - expect(session.model.settled).toBe(false); - - question.submit({ decision: "approve" }); - yield* session.join(); - expect(counted(yield* readAll(holder), "elicit")).toBe(1); - }, - ); + it("X1: a choice the form does not offer answers nothing and leaves the question open", () => + captureReferenceEntry( + "X1: a choice the form does not offer answers nothing and leaves the question open", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + + const question = yield* nextQuestion(session); + // Not an offered value, so the schema rejects it: the question stays open and + // nothing is recorded. + expect(question.submit({ decision: "maybe" }).kind).toBe("invalid"); + yield* sleep(10); + + // An incomplete interaction, not a rejected answer. The provider never + // returned, so there is nothing for core to judge and nothing to record. + // The other case — a provider that really hands back a value the schema + // rejects — settles the durable operation `err` with no answer, and E1 + // proves that in `packages/core/tests/elicit-component.test.ts`. This case + // does not prove that one. + expect(session.elicitation.pending).toBe(question); + expect(counted(yield* readAll(holder), "elicit")).toBe(0); + expect(session.model.settled).toBe(false); + + question.submit({ decision: "approve" }); + yield* session.join(); + expect(counted(yield* readAll(holder), "elicit")).toBe(1); + }, + process.stderr, + SUBMIT_SUITE, + )); it("X1: an entry is refused while the entry before it has not settled", function* () { // The reference history without its root close: an entry that never reached @@ -598,7 +599,6 @@ describe(SUBMIT_SUITE, () => { describe("REPL execution: reopening one history", () => { beforeAll(() => useTempFileCompiler()); - const referenceCase = referenceCases("REPL execution: reopening one history"); it("X2: replays what was recorded and goes live at the first unrecorded step", function* () { const settled = yield* referenceEvents(); @@ -631,28 +631,31 @@ describe("REPL execution: reopening one history", () => { expect(session.model.terminal?.output).toContain("Decision: approve"); }); - referenceCase( - "X2: the same counters do register the work a first run really performs", - function* (holder) { - const source = yield* referenceSource(); - const performed = yield* countPerformed(); - const asked = yield* countAsked(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - - (yield* nextQuestion(session)).submit({ decision: "approve" }); - yield* session.join(); - - // The positive control for every count the negatives rely on. Without it, a - // counter that never increments for any reason at all would read as proof - // of replay, or of a provider that was never reached. - expect( - performed.reads.filter((path) => path.endsWith("Checklist.md")).length, - ).toBeGreaterThan(0); - expect(performed.compiles).toBeGreaterThan(0); - expect(asked.calls).toBe(1); - expect(session.elicitation.asked).toBe(1); - }, - ); + it("X2: the same counters do register the work a first run really performs", () => + captureReferenceEntry( + "X2: the same counters do register the work a first run really performs", + function* (holder) { + const source = yield* referenceSource(); + const performed = yield* countPerformed(); + const asked = yield* countAsked(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + + (yield* nextQuestion(session)).submit({ decision: "approve" }); + yield* session.join(); + + // The positive control for every count the negatives rely on. Without it, a + // counter that never increments for any reason at all would read as proof + // of replay, or of a provider that was never reached. + expect( + performed.reads.filter((path) => path.endsWith("Checklist.md")).length, + ).toBeGreaterThan(0); + expect(performed.compiles).toBeGreaterThan(0); + expect(asked.calls).toBe(1); + expect(session.elicitation.asked).toBe(1); + }, + process.stderr, + "REPL execution: reopening one history", + )); it("X2: a recorded answer is restored without the provider being reached", function* () { const settled = yield* referenceEvents(); @@ -814,140 +817,159 @@ describe("REPL execution: reopening one history", () => { describe("REPL execution: pausing expansion", () => { beforeAll(() => useTempFileCompiler()); - const referenceCase = referenceCases("REPL execution: pausing expansion"); - - referenceCase( - "P1: stops at every boundary it crosses and continues past each one", - function* (holder) { - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - - // Held for the length of the test. The session stops *offering* the - // capability once nothing is expanding, which is what the cold control - // below is about; the controller this run owned is still the thing that - // released its holds. - const controller = controlling(session); - controller.pause(); - let stops = 0; - while (session.live && stops < 500) { - yield* pausedOrDone(session); - if (!session.live) { - break; + it("P1: stops at every boundary it crosses and continues past each one", () => + captureReferenceEntry( + "P1: stops at every boundary it crosses and continues past each one", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + + // Held for the length of the test. The session stops *offering* the + // capability once nothing is expanding, which is what the cold control + // below is about; the controller this run owned is still the thing that + // released its holds. + const controller = controlling(session); + controller.pause(); + let stops = 0; + while (session.live && stops < 500) { + yield* pausedOrDone(session); + if (!session.live) { + break; + } + expect(session.expansion.state).toBe("paused"); + stops++; + controller.resume(); + // Re-armed in the same turn as the release, so the continuation this + // released stops at the next boundary rather than running to the end. + controller.pause(); } - expect(session.expansion.state).toBe("paused"); - stops++; controller.resume(); - // Re-armed in the same turn as the release, so the continuation this - // released stops at the next boundary rather than running to the end. - controller.pause(); - } - controller.resume(); - yield* session.join(); - - expect(stops).toBeGreaterThan(5); - expect(controller.released).toBe(stops); - expect(session.model.settled).toBe(true); - expect(session.model.terminal?.output).toContain("Decision: approve"); - }, - ); - - referenceCase( - "P1: the Journal advances after pause is requested and before it takes effect", - function* (holder) { - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - const controller = controlling(session); - - // A question is work that has already started: the walk is suspended - // inside the provider, not held by the controller, so requesting a pause - // here cannot take effect until that operation finishes. - const question = yield* nextQuestion(session); - const before = (yield* readAll(holder)).length; + yield* session.join(); - controller.pause(); - expect(session.expansion.state).toBe("pausing"); - - // Answering lets the already-started operation settle. Its record reaches - // the Journal while the pause request is outstanding, which is the whole - // point: the expansion position and the Journal head are two positions. - question.submit({ decision: "approve" }); - yield* pausedOrDone(session); + expect(stops).toBeGreaterThan(5); + expect(controller.released).toBe(stops); + expect(session.model.settled).toBe(true); + expect(session.model.terminal?.output).toContain("Decision: approve"); + }, + process.stderr, + "REPL execution: pausing expansion", + )); + + it("P1: the Journal advances after pause is requested and before it takes effect", () => + captureReferenceEntry( + "P1: the Journal advances after pause is requested and before it takes effect", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + const controller = controlling(session); + + // A question is work that has already started: the walk is suspended + // inside the provider, not held by the controller, so requesting a pause + // here cannot take effect until that operation finishes. + const question = yield* nextQuestion(session); + const before = (yield* readAll(holder)).length; - const atPause = (yield* readAll(holder)).length; - expect(session.expansion.state).toBe("paused"); - expect(atPause).toBeGreaterThan(before); + controller.pause(); + expect(session.expansion.state).toBe("pausing"); - // And once every walk is satisfied it goes no farther on its own. - yield* sleep(25); - expect((yield* readAll(holder)).length).toBe(atPause); - expect(session.expansion.state).toBe("paused"); + // Answering lets the already-started operation settle. Its record reaches + // the Journal while the pause request is outstanding, which is the whole + // point: the expansion position and the Journal head are two positions. + question.submit({ decision: "approve" }); + yield* pausedOrDone(session); - // Only Continue moves it, and then it finishes. - controller.resume(); - yield* session.join(); - expect(session.model.settled).toBe(true); - expect((yield* readAll(holder)).length).toBeGreaterThan(atPause); - }, - ); + const atPause = (yield* readAll(holder)).length; + expect(session.expansion.state).toBe("paused"); + expect(atPause).toBeGreaterThan(before); - referenceCase("P1: continuing twice releases nothing the second time", function* (holder) { - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); + // And once every walk is satisfied it goes no farther on its own. + yield* sleep(25); + expect((yield* readAll(holder)).length).toBe(atPause); + expect(session.expansion.state).toBe("paused"); - controlling(session).pause(); - yield* pausedOrDone(session); - const held = controlling(session).released; - controlling(session).resume(); - const afterFirst = controlling(session).released; - controlling(session).resume(); + // Only Continue moves it, and then it finishes. + controller.resume(); + yield* session.join(); + expect(session.model.settled).toBe(true); + expect((yield* readAll(holder)).length).toBeGreaterThan(atPause); + }, + process.stderr, + "REPL execution: pausing expansion", + )); - expect(afterFirst).toBeGreaterThan(held); - expect(controlling(session).released).toBe(afterFirst); + it("P1: continuing twice releases nothing the second time", () => + captureReferenceEntry( + "P1: continuing twice releases nothing the second time", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); - (yield* nextQuestion(session)).submit({ decision: "approve" }); - yield* session.join(); - }); + controlling(session).pause(); + yield* pausedOrDone(session); + const held = controlling(session).released; + controlling(session).resume(); + const afterFirst = controlling(session).released; + controlling(session).resume(); - referenceCase( - "P1: a paused expansion is stopped, not cancelled, and the run is still there", - function* (holder) { - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); + expect(afterFirst).toBeGreaterThan(held); + expect(controlling(session).released).toBe(afterFirst); - controlling(session).pause(); - yield* pausedOrDone(session); - const frozen = (yield* readAll(holder)).length; - yield* sleep(25); + (yield* nextQuestion(session)).submit({ decision: "approve" }); + yield* session.join(); + }, + process.stderr, + "REPL execution: pausing expansion", + )); - expect(session.expansion.state).toBe("paused"); - expect(session.live).toBe(true); - expect((yield* readAll(holder)).length).toBe(frozen); + it("P1: a paused expansion is stopped, not cancelled, and the run is still there", () => + captureReferenceEntry( + "P1: a paused expansion is stopped, not cancelled, and the run is still there", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); - controlling(session).resume(); - (yield* nextQuestion(session)).submit({ decision: "approve" }); - yield* session.join(); - expect(session.model.settled).toBe(true); - }, - ); + controlling(session).pause(); + yield* pausedOrDone(session); + const frozen = (yield* readAll(holder)).length; + yield* sleep(25); - referenceCase("P1: tearing the owner down releases nothing that was held", function* (holder) { - const source = yield* referenceSource(); - let held = -1; + expect(session.expansion.state).toBe("paused"); + expect(session.live).toBe(true); + expect((yield* readAll(holder)).length).toBe(frozen); - const controller = yield* scoped(function* () { - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - controlling(session).pause(); - yield* pausedOrDone(session); - expect(session.expansion.state).toBe("paused"); - held = (yield* readAll(holder)).length; - return controlling(session); - }); + controlling(session).resume(); + (yield* nextQuestion(session)).submit({ decision: "approve" }); + yield* session.join(); + expect(session.model.settled).toBe(true); + }, + process.stderr, + "REPL execution: pausing expansion", + )); + + it("P1: tearing the owner down releases nothing that was held", () => + captureReferenceEntry( + "P1: tearing the owner down releases nothing that was held", + function* (holder) { + const source = yield* referenceSource(); + let held = -1; + + const controller = yield* scoped(function* () { + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + controlling(session).pause(); + yield* pausedOrDone(session); + expect(session.expansion.state).toBe("paused"); + held = (yield* readAll(holder)).length; + return controlling(session); + }); - // The scope is gone: every hold unwound through its own discard, so nothing - // was resumed on the way out and the history stopped where it was held. - expect(controller.released).toBe(0); - expect((yield* readAll(holder)).length).toBe(held); - }); + // The scope is gone: every hold unwound through its own discard, so nothing + // was resumed on the way out and the history stopped where it was held. + expect(controller.released).toBe(0); + expect((yield* readAll(holder)).length).toBe(held); + }, + process.stderr, + "REPL execution: pausing expansion", + )); it("P1: a reconstructed session offers no Continue at all", function* () { const holder = execution(yield* referenceEvents()); @@ -975,7 +997,6 @@ describe("REPL execution: pausing expansion", () => { describe("REPL execution: the boundary inventory", () => { beforeAll(() => useTempFileCompiler()); - const referenceCase = referenceCases("REPL execution: the boundary inventory"); it("P1: every member of every wrapped Api is a walk, a gate, or declared not to be one", function* () { const partitioned = (partition: BoundaryPartition): string[] => @@ -986,38 +1007,41 @@ describe("REPL execution: the boundary inventory", () => { expect(partitioned(OUTPUT_BOUNDARIES)).toEqual(Object.keys(DocumentOutput.operations).sort()); }); - referenceCase( - "P1: the reference entry crosses the boundaries this controller accounts for", - function* (holder) { - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - // The controller, not a snapshot of its crossings: `crossings` answers - // with what has been crossed *so far*, and so far is nothing yet. - const controller = controlling(session); - (yield* nextQuestion(session)).submit({ decision: "approve" }); - yield* session.join(); - const crossings = controller.crossings; - - const controlled = [ - ...COMPONENT_BOUNDARIES.walks, - ...COMPONENT_BOUNDARIES.gates, - ...EXECUTION_BOUNDARIES.walks, - ...OUTPUT_BOUNDARIES.gates, - HOST_WALK, - ]; - const crossed = [...crossings.keys()].sort(); - for (const boundary of crossed) { - expect(controlled).toContain(boundary); - } - // The ones this document is written to reach. A boundary disappearing - // from it is what this list is here to notice. - expect(crossed).toContain("document"); - expect(crossed).toContain("importComponent"); - expect(crossed).toContain("applyModifiers"); - expect(crossed).toContain("output"); - expect(crossings.get("document")).toBe(1); - }, - ); + it("P1: the reference entry crosses the boundaries this controller accounts for", () => + captureReferenceEntry( + "P1: the reference entry crosses the boundaries this controller accounts for", + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); + // The controller, not a snapshot of its crossings: `crossings` answers + // with what has been crossed *so far*, and so far is nothing yet. + const controller = controlling(session); + (yield* nextQuestion(session)).submit({ decision: "approve" }); + yield* session.join(); + const crossings = controller.crossings; + + const controlled = [ + ...COMPONENT_BOUNDARIES.walks, + ...COMPONENT_BOUNDARIES.gates, + ...EXECUTION_BOUNDARIES.walks, + ...OUTPUT_BOUNDARIES.gates, + HOST_WALK, + ]; + const crossed = [...crossings.keys()].sort(); + for (const boundary of crossed) { + expect(controlled).toContain(boundary); + } + // The ones this document is written to reach. A boundary disappearing + // from it is what this list is here to notice. + expect(crossed).toContain("document"); + expect(crossed).toContain("importComponent"); + expect(crossed).toContain("applyModifiers"); + expect(crossed).toContain("output"); + expect(crossings.get("document")).toBe(1); + }, + process.stderr, + "REPL execution: the boundary inventory", + )); }); describe("REPL execution: one execution's file", () => { diff --git a/packages/web/tests/component.test.ts b/packages/web/tests/component.test.ts index 2c1d3f170..f8249871f 100644 --- a/packages/web/tests/component.test.ts +++ b/packages/web/tests/component.test.ts @@ -29,12 +29,13 @@ type RunFreshForm = ( const OWNER = "packages/web/tests/component.test.ts"; -function formCase(name: string, body: (run: RunFreshForm) => Operation): void { - it(name, () => - body((source, options, check) => - withWebFormDocument({ owner: OWNER, name }, source, options, check), - ), - ); +function runFreshForm( + name: string, + source: string, + options: Omit, + check: (run: DocRun) => void | Operation, +): Operation { + return withWebFormDocument({ owner: OWNER, name }, source, options, check); } const SCHEMA_BLOCK = [ @@ -101,8 +102,9 @@ describe("WebForm: what a document gets", () => { expect(records).not.toContain("127.0.0.1"); }); - formCase("binds the validated response and renders nothing", function* (runFresh) { - yield* runFresh( + it("binds the validated response and renders nothing", function* () { + yield* runFreshForm( + "binds the validated response and renders nothing", [ document("# Review required\n\nDecide."), "```js eval", @@ -121,8 +123,9 @@ describe("WebForm: what a document gets", () => { ); }); - formCase("is answered through its own live server", function* (runFresh) { - yield* runFresh( + it("is answered through its own live server", function* () { + yield* runFreshForm( + "is answered through its own live server", document("Decide."), { answer: { decision: "reject" }, @@ -141,14 +144,19 @@ describe("WebForm: what a document gets", () => { ); }); - formCase("prints the URL a person can use", function* (runFresh) { - yield* runFresh(document("Decide."), { answer: { decision: "approve" } }, function (run) { - const url = run.effects.opened[0]; - expect(run.effects.printed.some((line) => line.includes(url))).toBe(true); - }); + it("prints the URL a person can use", function* () { + yield* runFreshForm( + "prints the URL a person can use", + document("Decide."), + { answer: { decision: "approve" } }, + function (run) { + const url = run.effects.opened[0]; + expect(run.effects.printed.some((line) => line.includes(url))).toBe(true); + }, + ); }); - formCase("resolves an expression-valued uiSchema through core", function* (runFresh) { + it("resolves an expression-valued uiSchema through core", function* () { const source = [ "```js eval", `const reviewSchema = ${JSON.stringify(REVIEW_SCHEMA)};`, @@ -164,10 +172,15 @@ describe("WebForm: what a document gets", () => { "```", ].join("\n"); - yield* runFresh(source, { answer: { decision: "approve" } }, function (run) { - expect(run.completion.ok).toBe(true); - expect(run.output).toContain("approve"); - }); + yield* runFreshForm( + "resolves an expression-valued uiSchema through core", + source, + { answer: { decision: "approve" } }, + function (run) { + expect(run.completion.ok).toBe(true); + expect(run.output).toContain("approve"); + }, + ); }); }); @@ -184,7 +197,7 @@ describe("WebForm: nothing happens before the question is ready", () => { * this asserts is the consequence — the diagnostic reached the document and * nothing was served. */ - formCase("causes no live effect when its content fails", function* (runFresh) { + it("causes no live effect when its content fails", function* () { const source = [ SCHEMA_BLOCK, "", @@ -195,22 +208,27 @@ describe("WebForm: nothing happens before the question is ready", () => { "", ].join("\n"); - yield* runFresh(source, { answer: { decision: "approve" } }, function* (run) { - // Nothing recovers the body's failure, so the run reports it. - expect(run.completion.ok).toBe(false); - expect(run.completion.ok ? "" : run.completion.error.message).toContain("the body failed"); - expect(run.effects.served).toBe(0); - expect(run.effects.opened).toEqual([]); - expect(run.effects.responded).toEqual([]); - expect(run.effects.printed).toEqual([]); - // No entry at all, not merely no successful one: a durable operation that - // began and failed would leave an `err` event, and that is still work done - // for a question nobody was ever asked. - expect(yield* journalEvents(run.stream, "web_form")).toEqual([]); - }); + yield* runFreshForm( + "causes no live effect when its content fails", + source, + { answer: { decision: "approve" } }, + function* (run) { + // Nothing recovers the body's failure, so the run reports it. + expect(run.completion.ok).toBe(false); + expect(run.completion.ok ? "" : run.completion.error.message).toContain("the body failed"); + expect(run.effects.served).toBe(0); + expect(run.effects.opened).toEqual([]); + expect(run.effects.responded).toEqual([]); + expect(run.effects.printed).toEqual([]); + // No entry at all, not merely no successful one: a durable operation that + // began and failed would leave an `err` event, and that is still work done + // for a question nobody was ever asked. + expect(yield* journalEvents(run.stream, "web_form")).toEqual([]); + }, + ); }); - formCase("causes no live effect when the schema cannot be used", function* (runFresh) { + it("causes no live effect when the schema cannot be used", function* () { const unusable: [string, string][] = [ ["not JSON", 'x'], ["not an object", 'x'], @@ -230,26 +248,32 @@ describe("WebForm: nothing happens before the question is ready", () => { ]; for (const [label, source] of unusable) { - yield* runFresh(source, { answer: { decision: "approve" } }, function* (run) { - expect({ label, ok: run.completion.ok }).toEqual({ label, ok: false }); - expect({ label, served: run.effects.served }).toEqual({ label, served: 0 }); - expect({ label, opened: run.effects.opened }).toEqual({ label, opened: [] }); - expect({ label, responded: run.effects.responded }).toEqual({ label, responded: [] }); - expect({ label, printed: run.effects.printed }).toEqual({ label, printed: [] }); - // Every durable yield, not the successful ones: an operation that began and - // failed leaves an `err` event, and that is still work done for a question - // nobody was ever asked. - expect({ label, events: yield* journalEvents(run.stream, "web_form") }).toEqual({ - label, - events: [], - }); - }); + yield* runFreshForm( + "causes no live effect when the schema cannot be used", + source, + { answer: { decision: "approve" } }, + function* (run) { + expect({ label, ok: run.completion.ok }).toEqual({ label, ok: false }); + expect({ label, served: run.effects.served }).toEqual({ label, served: 0 }); + expect({ label, opened: run.effects.opened }).toEqual({ label, opened: [] }); + expect({ label, responded: run.effects.responded }).toEqual({ label, responded: [] }); + expect({ label, printed: run.effects.printed }).toEqual({ label, printed: [] }); + // Every durable yield, not the successful ones: an operation that began and + // failed leaves an `err` event, and that is still work done for a question + // nobody was ever asked. + expect({ label, events: yield* journalEvents(run.stream, "web_form") }).toEqual({ + label, + events: [], + }); + }, + ); } }); /** Unmarked, so #251's default applies: the failure is the document's. */ - formCase("fails the document rather than printing", function* (runFresh) { - yield* runFresh( + it("fails the document rather than printing", function* () { + yield* runFreshForm( + "fails the document rather than printing", 'Decide.\n\nafter\n', { answer: { decision: "approve" } }, function (run) { @@ -392,20 +416,25 @@ describe("WebForm: a repository file outranks the registration", () => { * Real files rather than a stubbed filesystem: a `.ts` component is `import()`ed * from a real path, so a stub can resolve its name and then fail to load it. */ - formCase("prefers a repository WebForm.md", function* (runFresh) { + it("prefers a repository WebForm.md", function* () { const dir = yield* useLocalFixture(); yield* writeFile(join(dir, "WebForm.md"), "the repository's own form\n"); const doc = join(dir, "README.md"); yield* writeFile(doc, "\n"); - yield* runFresh(doc, { realFiles: true, includes: [dir] }, function (run) { - expect(run.output).toContain("the repository's own form"); - // The registered default never ran, so nothing was served. - expect(run.effects.served).toBe(0); - }); + yield* runFreshForm( + "prefers a repository WebForm.md", + doc, + { realFiles: true, includes: [dir] }, + function (run) { + expect(run.output).toContain("the repository's own form"); + // The registered default never ran, so nothing was served. + expect(run.effects.served).toBe(0); + }, + ); }); - formCase("prefers a repository WebForm.ts", function* (runFresh) { + it("prefers a repository WebForm.ts", function* () { yield* useComponentModuleLoader(); const dir = yield* useLocalFixture(); yield* writeFile( @@ -417,9 +446,14 @@ describe("WebForm: a repository file outranks the registration", () => { const doc = join(dir, "README.md"); yield* writeFile(doc, "\n"); - yield* runFresh(doc, { realFiles: true, includes: [dir] }, function (run) { - expect(run.output).toContain("the repository's own function"); - expect(run.effects.served).toBe(0); - }); + yield* runFreshForm( + "prefers a repository WebForm.ts", + doc, + { realFiles: true, includes: [dir] }, + function (run) { + expect(run.output).toContain("the repository's own function"); + expect(run.effects.served).toBe(0); + }, + ); }); }); From f1ce2144b0252ee7a86983c903b09c788ef99f8a Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Tue, 17 Nov 2026 00:09:22 -0500 Subject: [PATCH 08/11] Keep ordinary test declarations in two Testing suites --- packages/testing/tests/assertions.test.ts | 263 +++++++++----------- packages/testing/tests/testing-mode.test.ts | 251 +++++++++---------- 2 files changed, 236 insertions(+), 278 deletions(-) diff --git a/packages/testing/tests/assertions.test.ts b/packages/testing/tests/assertions.test.ts index 26afd1c16..0b4a89012 100644 --- a/packages/testing/tests/assertions.test.ts +++ b/packages/testing/tests/assertions.test.ts @@ -4,15 +4,13 @@ import { expect } from "@executablemd/test-support/expect"; import { AssertionError } from "node:assert/strict"; import { TestFailureError } from "../src/test-api.ts"; import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; -import { documentCases, failureOf } from "./helpers.ts"; - -const documentCase = documentCases(); +import { failureOf, runDoc } from "./helpers.ts"; describe("assertion components", () => { beforeEach(journalBeforeEach("packages/testing/tests/assertions.test.ts")); beforeAll(() => useTempFileCompiler()); - documentCase("passing assertions in testing mode emit diagnostics", function* (runFresh) { - const run = yield* runFresh( + it("passing assertions in testing mode emit diagnostics", function* () { + const run = yield* runDoc( { "README.md": '\n', @@ -24,7 +22,7 @@ describe("assertion components", () => { expect(run.results).toEqual([{ status: "pass", name: "eq", location: "README.md:1:10" }]); }); - documentCase("a validation diagnostic is observed once and rendered once", function* (runFresh) { + it("a validation diagnostic is observed once and rendered once", function* () { const observed: string[] = []; yield* Component.around({ *raise([error], next) { @@ -34,7 +32,7 @@ describe("assertion components", () => { }); // Written in a region that prints: rendering the diagnostic once is half of // what this pins, and reading it needs the document to render it. - const run = yield* runFresh({ + const run = yield* runDoc({ "README.md": "\n\n\n", }); @@ -46,21 +44,18 @@ describe("assertion components", () => { expect(run.output.match(/must NOT have additional properties/g)).toHaveLength(1); }); - documentCase( - "assertions outside a test pass silently during regular execution", - function* (runFresh) { - const run = yield* runFresh({ - "README.md": "before\n\nafter\n", - }); - expect(run.completion.ok).toBe(true); - expect(run.output).not.toContain("AssertEquals"); - expect(run.output).toContain("before"); - expect(run.output).toContain("after"); - }, - ); + it("assertions outside a test pass silently during regular execution", function* () { + const run = yield* runDoc({ + "README.md": "before\n\nafter\n", + }); + expect(run.completion.ok).toBe(true); + expect(run.output).not.toContain("AssertEquals"); + expect(run.output).toContain("before"); + expect(run.output).toContain("after"); + }); - documentCase("assertions outside a test emit diagnostics with verbose", function* (runFresh) { - const run = yield* runFresh( + it("assertions outside a test emit diagnostics with verbose", function* () { + const run = yield* runDoc( { "README.md": "\n" }, { verbose: true }, ); @@ -68,8 +63,8 @@ describe("assertion components", () => { expect(run.output).toContain("**AssertEquals** passed"); }); - documentCase("a failed assertion outside a test aborts the document", function* (runFresh) { - const run = yield* runFresh({ + it("a failed assertion outside a test aborts the document", function* () { + const run = yield* runDoc({ "README.md": "before\n\nnever\n", }); const error = failureOf(run); @@ -79,20 +74,17 @@ describe("assertion components", () => { expect(run.output).not.toContain("AssertEquals"); }); - documentCase( - "a failed assertion outside a test keeps its diagnostic with verbose", - function* (runFresh) { - const run = yield* runFresh( - { "README.md": "before\n\nnever\n" }, - { verbose: true }, - ); - expect(failureOf(run)).toBeInstanceOf(AssertionError); - expect(run.output).toContain("**AssertEquals** failed"); - expect(run.output).toContain("before"); - }, - ); + it("a failed assertion outside a test keeps its diagnostic with verbose", function* () { + const run = yield* runDoc( + { "README.md": "before\n\nnever\n" }, + { verbose: true }, + ); + expect(failureOf(run)).toBeInstanceOf(AssertionError); + expect(run.output).toContain("**AssertEquals** failed"); + expect(run.output).toContain("before"); + }); - documentCase("expected children behave like trimming", function* (runFresh) { + it("expected children behave like trimming", function* () { const doc = [ "", '', @@ -104,45 +96,45 @@ describe("assertion components", () => { "", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results.map((r) => r.status)).toEqual(["pass"]); }); - documentCase("rejects both expected prop and expected children", function* (runFresh) { + it("rejects both expected prop and expected children", function* () { const doc = "1\n"; - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(failureOf(run)).toBeInstanceOf(TestFailureError); expect(run.results[0]?.status).toBe("fail"); expect(run.results[0]?.error?.kind).toBe("error"); expect(run.results[0]?.error?.message).toContain("not both"); }); - documentCase("rejects expected children on unary and numeric assertions", function* (runFresh) { + it("rejects expected children on unary and numeric assertions", function* () { const doc = "x\n"; - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results[0]?.error?.message).toContain("expected children"); }); - documentCase("match assertions require a real RegExp", function* (runFresh) { + it("match assertions require a real RegExp", function* () { const doc = '\n'; - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results[0]?.status).toBe("fail"); expect(run.results[0]?.error?.message).toContain("RegExp"); }); - documentCase("match assertions accept a RegExp expression", function* (runFresh) { + it("match assertions accept a RegExp expression", function* () { const doc = '\n'; - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results[0]?.status).toBe("pass"); }); - documentCase("unknown props are rejected per kind", function* (runFresh) { + it("unknown props are rejected per kind", function* () { const doc = "\n"; - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); // `actual` is a capture for the binary kinds but not for unary-truthy, so // never declares it and the schema rejects it like any other // undeclared prop. @@ -150,13 +142,13 @@ describe("assertion components", () => { expect(run.results[0]?.error?.message).toContain("must NOT have additional properties"); }); - documentCase("missing required props are rejected", function* (runFresh) { + it("missing required props are rejected", function* () { const doc = "\n"; - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results[0]?.error?.message).toContain('"expected"'); }); - documentCase("assertion expressions see live bindings from eval blocks", function* (runFresh) { + it("assertion expressions see live bindings from eval blocks", function* () { const doc = [ "", "```js eval", @@ -166,31 +158,28 @@ describe("assertion components", () => { "", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results[0]?.status).toBe("pass"); }); - documentCase( - "assertion expressions see caller bindings through projection", - function* (runFresh) { - const doc = [ - "```js eval", - 'const fromCaller = "outer-value";', - "```", - "", - '', - "", - "", - ].join("\n"); - const wrap = "projected: \n"; - const run = yield* runFresh({ "README.md": doc, "components/Wrap.md": wrap }); - expect(run.completion.ok).toBe(true); - expect(run.results[0]?.status).toBe("pass"); - }, - ); + it("assertion expressions see caller bindings through projection", function* () { + const doc = [ + "```js eval", + 'const fromCaller = "outer-value";', + "```", + "", + '', + "", + "", + ].join("\n"); + const wrap = "projected: \n"; + const run = yield* runDoc({ "README.md": doc, "components/Wrap.md": wrap }); + expect(run.completion.ok).toBe(true); + expect(run.results[0]?.status).toBe("pass"); + }); - documentCase("formatter-visible toJSON/toString cannot change the outcome", function* (runFresh) { + it("formatter-visible toJSON/toString cannot change the outcome", function* () { const doc = [ "", "```js eval", @@ -200,13 +189,13 @@ describe("assertion components", () => { "", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results[0]?.status).toBe("pass"); expect(run.output).toContain("unformattable"); }); - documentCase("a non-string msg is rejected by type check, never formatted", function* (runFresh) { + it("a non-string msg is rejected by type check, never formatted", function* () { // If msg were formatted before the assertion, the hostile toJSON or // toString would throw and replace the validation outcome. const doc = [ @@ -215,7 +204,7 @@ describe("assertion components", () => { "", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(failureOf(run)).toBeInstanceOf(TestFailureError); expect(run.results[0]?.status).toBe("fail"); expect(run.results[0]?.error?.kind).toBe("error"); @@ -224,48 +213,42 @@ describe("assertion components", () => { expect(run.results[0]?.error?.message).not.toContain("hostile-string"); }); - documentCase( - "a throwing getter read at format time cannot change the outcome", - function* (runFresh) { - const doc = [ - "", - "", - "", - "", - ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); - expect(run.completion.ok).toBe(true); - expect(run.results[0]?.status).toBe("pass"); - expect(run.output).toContain("[object Object]"); - }, - ); + it("a throwing getter read at format time cannot change the outcome", function* () { + const doc = [ + "", + "", + "", + "", + ].join("\n"); + const run = yield* runDoc({ "README.md": doc }); + expect(run.completion.ok).toBe(true); + expect(run.results[0]?.status).toBe("pass"); + expect(run.output).toContain("[object Object]"); + }); // §5b: `as` and `slot` are the engine's, consumed before validation, so an // assertion accepts them like any other component rather than rejecting them // from a hand-written allowed-list. - documentCase( - "accepts `as` on a value assertion, binding its diagnostic text", - function* (runFresh) { - const doc = [ - "", - '', - '', - '', - "", - "", - "", - ].join("\n"); + it("accepts `as` on a value assertion, binding its diagnostic text", function* () { + const doc = [ + "", + '', + '', + '', + "", + "", + "", + ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); - expect(run.results[0]?.status).toBe("pass"); - }, - ); + expect(run.results[0]?.status).toBe("pass"); + }); // A registration is a default, so a repository file of the same name wins. // The local component declares a props schema: a markdown component without // one accepts no props at all, and would be rejected before it could answer. - documentCase("a repository AssertEquals overrides the registered default", function* (runFresh) { + it("a repository AssertEquals overrides the registered default", function* () { const local = [ "---", "props:", @@ -279,7 +262,7 @@ describe("assertion components", () => { "", ].join("\n"); - const run = yield* runFresh({ + const run = yield* runDoc({ "README.md": [ "", '', @@ -296,49 +279,41 @@ describe("assertion components", () => { // Parity with the handler this replaced: an operand that throws is reported // by the assertion that owns it, not as the invocation's own failure. - documentCase( - "reports an operand expression that throws as its own diagnostic", - function* (runFresh) { - const doc = [ - "", - '', - ' { throw new Error("operand exploded"); })()} expected={1} />', - "", - "", - "", - ].join("\n"); + it("reports an operand expression that throws as its own diagnostic", function* () { + const doc = [ + "", + '', + ' { throw new Error("operand exploded"); })()} expected={1} />', + "", + "", + "", + ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); - expect(run.results[0]?.status).toBe("fail"); - expect(run.results[0]?.error?.message).toContain( - 'failed to evaluate the "actual" expression', - ); - expect(run.results[0]?.error?.message).toContain("operand exploded"); - }, - ); + expect(run.results[0]?.status).toBe("fail"); + expect(run.results[0]?.error?.message).toContain('failed to evaluate the "actual" expression'); + expect(run.results[0]?.error?.message).toContain("operand exploded"); + }); // `undefined` cannot survive the JSON gate, so before captures this could not // be written at all — the engine rejected the prop before the assertion ran. - documentCase( - " fails on undefined rather than rejecting the prop", - function* (runFresh) { - const doc = [ - "", - '', - "", - "", - "", - "", - ].join("\n"); + it(" fails on undefined rather than rejecting the prop", function* () { + const doc = [ + "", + '', + "", + "", + "", + "", + ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); - // The assertion's own comparison failed — not the engine refusing the prop, - // which is what happened before `actual` became a capture. - expect(run.results[0]?.status).toBe("fail"); - expect(run.results[0]?.error?.kind).toBe("assertion"); - expect(run.results[0]?.error?.message).not.toContain("non-serializable"); - }, - ); + // The assertion's own comparison failed — not the engine refusing the prop, + // which is what happened before `actual` became a capture. + expect(run.results[0]?.status).toBe("fail"); + expect(run.results[0]?.error?.kind).toBe("assertion"); + expect(run.results[0]?.error?.message).not.toContain("non-serializable"); + }); }); diff --git a/packages/testing/tests/testing-mode.test.ts b/packages/testing/tests/testing-mode.test.ts index 3093d9d7b..4e1757bd6 100644 --- a/packages/testing/tests/testing-mode.test.ts +++ b/packages/testing/tests/testing-mode.test.ts @@ -8,14 +8,12 @@ import { useFailingExec } from "@executablemd/runtime/test"; import { TestFailureError } from "../src/test-api.ts"; import { createTestHandlers } from "../src/handlers.ts"; import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; -import { documentCases, failureOf, runDoc } from "./helpers.ts"; - -const documentCase = documentCases(); +import { failureOf, runDoc } from "./helpers.ts"; describe("testing mode", () => { beforeEach(journalBeforeEach("packages/testing/tests/testing-mode.test.ts")); beforeAll(() => useTempFileCompiler()); - documentCase("skips entirely during regular execution", function* (runFresh) { + it("skips entirely during regular execution", function* () { const execCalls: string[] = []; yield* API.Process.around({ // deno-lint-ignore require-yield @@ -35,7 +33,7 @@ describe("testing mode", () => { "after", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.output).toContain("before"); expect(run.output).toContain("after"); @@ -44,49 +42,49 @@ describe("testing mode", () => { expect(run.results).toEqual([]); }); - documentCase("explicit runs its subtree during a regular run", function* (runFresh) { + it("explicit runs its subtree during a regular run", function* () { const doc = '\n'; - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results.map((r) => [r.name, r.status])).toEqual([["t", "pass"]]); expect(run.boundaries).toEqual([{ tests: 1, failed: 0 }]); }); - documentCase("a failing boundary rejects an ordinary run", function* (runFresh) { + it("a failing boundary rejects an ordinary run", function* () { const doc = "\n"; - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(failureOf(run)).toBeInstanceOf(TestFailureError); expect(run.output).toContain("Test"); }); - documentCase("an empty boundary rejects an ordinary run", function* (runFresh) { - const run = yield* runFresh({ "README.md": "no tests here\n" }); + it("an empty boundary rejects an ordinary run", function* () { + const run = yield* runDoc({ "README.md": "no tests here\n" }); expect(failureOf(run)).toBeInstanceOf(TestFailureError); expect(run.boundaries).toEqual([{ tests: 0, failed: 0 }]); }); - documentCase("root activation runs tests without ", function* (runFresh) { + it("root activation runs tests without ", function* () { const doc = '\n'; - const run = yield* runFresh({ "README.md": doc }, { testing: true }); + const run = yield* runDoc({ "README.md": doc }, { testing: true }); expect(run.completion.ok).toBe(true); expect(run.results.map((r) => [r.name, r.status])).toEqual([["rooted", "pass"]]); }); - documentCase("root activation with zero tests fails", function* (runFresh) { - const run = yield* runFresh({ "README.md": "just text\n" }, { testing: true }); + it("root activation with zero tests fails", function* () { + const run = yield* runDoc({ "README.md": "just text\n" }, { testing: true }); const error = failureOf(run); expect(error).toBeInstanceOf(TestFailureError); expect(error?.message).toContain("no tests were discovered"); }); - documentCase("nested delegates results outward", function* (runFresh) { + it("nested delegates results outward", function* () { const doc = [ "", "", "", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.completion.ok).toBe(true); // Inner boundary reports first; the outer boundary sees the delegated // result, so neither boundary is empty. @@ -97,7 +95,7 @@ describe("testing mode", () => { expect(run.results).toHaveLength(1); }); - documentCase("a failing exec block fails the test; later tests run", function* (runFresh) { + it("a failing exec block fails the test; later tests run", function* () { yield* useFailingExec(3, "command exploded"); const doc = [ "", @@ -110,7 +108,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(failureOf(run)).toBeInstanceOf(TestFailureError); expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ ["broken", "fail", "error"], @@ -118,25 +116,22 @@ describe("testing mode", () => { ]); }); - documentCase( - "an unresolvable component import fails the test; later tests run", - function* (runFresh) { - const doc = [ - "", - '', - '', - "", - "", - ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); - expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ - ["missing", "fail", "error"], - ["fine", "pass", undefined], - ]); - }, - ); + it("an unresolvable component import fails the test; later tests run", function* () { + const doc = [ + "", + '', + '', + "", + "", + ].join("\n"); + const run = yield* runDoc({ "README.md": doc }); + expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ + ["missing", "fail", "error"], + ["fine", "pass", undefined], + ]); + }); - documentCase("nested fails the enclosing test; later tests run", function* (runFresh) { + it("nested fails the enclosing test; later tests run", function* () { const doc = [ "", '', @@ -144,7 +139,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ ["outer", "fail", "error"], ["fine", "pass", undefined], @@ -152,44 +147,38 @@ describe("testing mode", () => { expect(run.results[0]?.error?.message).toContain("Nested "); }); - documentCase( - "an error raised inside a component-owned region fails the test", - function* (runFresh) { - const comp = "\nregion start\n\n\n"; - const doc = [ - "", - '', - '', - "", - "", - ].join("\n"); - const run = yield* runFresh({ "README.md": doc, "components/Regioned.md": comp }); - expect(run.results.map((r) => [r.name, r.status])).toEqual([ - ["output-region", "fail"], - ["fine", "pass"], - ]); - }, - ); + it("an error raised inside a component-owned region fails the test", function* () { + const comp = "\nregion start\n\n\n"; + const doc = [ + "", + '', + '', + "", + "", + ].join("\n"); + const run = yield* runDoc({ "README.md": doc, "components/Regioned.md": comp }); + expect(run.results.map((r) => [r.name, r.status])).toEqual([ + ["output-region", "fail"], + ["fine", "pass"], + ]); + }); - documentCase( - "a completed failing test inside a documentation region stays contained", - function* (runFresh) { - const comp = "\n\n\n"; - const doc = [ - "", - '', - "", - "", - ].join("\n"); - const run = yield* runFresh({ "README.md": doc, "components/Regioned.md": comp }); - // The document expands fully — the failure surfaces as a testing - // outcome, not an expansion abort. - expect(failureOf(run)).toBeInstanceOf(TestFailureError); - expect(run.results.map((r) => [r.name, r.status])).toEqual([["contained", "fail"]]); - }, - ); + it("a completed failing test inside a documentation region stays contained", function* () { + const comp = "\n\n\n"; + const doc = [ + "", + '', + "", + "", + ].join("\n"); + const run = yield* runDoc({ "README.md": doc, "components/Regioned.md": comp }); + // The document expands fully — the failure surfaces as a testing + // outcome, not an expansion abort. + expect(failureOf(run)).toBeInstanceOf(TestFailureError); + expect(run.results.map((r) => [r.name, r.status])).toEqual([["contained", "fail"]]); + }); - documentCase("bindings written in one test are invisible to the next", function* (runFresh) { + it("bindings written in one test are invisible to the next", function* () { const doc = [ "```js eval", "const inherited = 7;", @@ -209,35 +198,32 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results.map((r) => r.status)).toEqual(["pass", "pass"]); }); - documentCase( - "a projected through sees caller bindings in its eval blocks", - function* (runFresh) { - const doc = [ - "```js eval", - 'const callerValue = "from-caller";', - "```", - "", - '', - "```js eval", - 'const copied = callerValue + "!";', - "```", - '', - "", - "", - "", - ].join("\n"); - const run = yield* runFresh({ "README.md": doc, "components/Wrap.md": "\n" }); - expect(run.completion.ok).toBe(true); - expect(run.results.map((r) => [r.name, r.status])).toEqual([["projected", "pass"]]); - }, - ); + it("a projected through sees caller bindings in its eval blocks", function* () { + const doc = [ + "```js eval", + 'const callerValue = "from-caller";', + "```", + "", + '', + "```js eval", + 'const copied = callerValue + "!";', + "```", + '', + "", + "", + "", + ].join("\n"); + const run = yield* runDoc({ "README.md": doc, "components/Wrap.md": "\n" }); + expect(run.completion.ok).toBe(true); + expect(run.results.map((r) => [r.name, r.status])).toEqual([["projected", "pass"]]); + }); - documentCase("captures persist for the immediately following assertion", function* (runFresh) { + it("captures persist for the immediately following assertion", function* () { const doc = [ "", 'Hello World', @@ -245,40 +231,37 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.completion.ok).toBe(true); expect(run.results.map((r) => r.status)).toEqual(["pass"]); }); - documentCase( - "effects spawned in one test are torn down before the next begins", - function* (runFresh) { - Reflect.deleteProperty(globalThis, "__testingLeaseAlive"); - const doc = [ - "", - '', - "```js persist eval", - "globalThis.__testingLeaseAlive = true;", - "yield* spawn(function* () { try { yield* suspend(); } finally { globalThis.__testingLeaseAlive = false; } });", - "```", - "", - "", - '', - "", - "", - "", - "", - ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); - expect(run.completion.ok).toBe(true); - expect(run.results.map((r) => [r.name, r.status])).toEqual([ - ["spawner", "pass"], - ["observer", "pass"], - ]); - }, - ); + it("effects spawned in one test are torn down before the next begins", function* () { + Reflect.deleteProperty(globalThis, "__testingLeaseAlive"); + const doc = [ + "", + '', + "```js persist eval", + "globalThis.__testingLeaseAlive = true;", + "yield* spawn(function* () { try { yield* suspend(); } finally { globalThis.__testingLeaseAlive = false; } });", + "```", + "", + "", + '', + "", + "", + "", + "", + ].join("\n"); + const run = yield* runDoc({ "README.md": doc }); + expect(run.completion.ok).toBe(true); + expect(run.results.map((r) => [r.name, r.status])).toEqual([ + ["spawner", "pass"], + ["observer", "pass"], + ]); + }); - documentCase("output before a failure is kept, followed by the diagnostic", function* (runFresh) { + it("output before a failure is kept, followed by the diagnostic", function* () { const doc = [ "", "first line", @@ -287,7 +270,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.output).toContain("first line"); expect(run.output).toContain("**Assert** failed"); expect(run.output).not.toContain("never rendered"); @@ -296,7 +279,7 @@ describe("testing mode", () => { expect(firstAt).toBeLessThan(diagnosticAt); }); - documentCase("a hanging test times out, is torn down, and later tests run", function* (runFresh) { + it("a hanging test times out, is torn down, and later tests run", function* () { yield* API.Process.around({ *exec(_args, _next) { yield* sleep(10_000); @@ -314,7 +297,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runFresh( + const run = yield* runDoc( { "README.md": doc }, { handlers: createTestHandlers({ timeoutMs: 100 }) }, ); @@ -325,7 +308,7 @@ describe("testing mode", () => { ]); }); - documentCase("unnamed tests are identified by source location", function* (runFresh) { + it("unnamed tests are identified by source location", function* () { const doc = [ "---", "title: Located", @@ -335,7 +318,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results[0]?.location).toBe("README.md:5:1"); expect(run.output).toContain("test at README.md:5:1"); }); @@ -347,7 +330,7 @@ describe("testing mode", () => { * exactly as it would be anywhere else a document did not authorize one * (#441). */ - documentCase("a repository Test of the same name contains nothing", function* (runFresh) { + it("a repository Test of the same name contains nothing", function* () { yield* useFailingExec(3, "command exploded"); const doc = [ "", @@ -360,7 +343,7 @@ describe("testing mode", () => { "", "", ].join("\n"); - const run = yield* runFresh({ + const run = yield* runDoc({ "README.md": doc, // Chosen ahead of the package's, and a different function object. "Test.md": "\n", From 36597a81af745d4b521abc4ef37da46fe3d82601 Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Tue, 17 Nov 2026 00:11:34 -0500 Subject: [PATCH 09/11] Integrate direct declarations in Testing and Workflow --- packages/test-support/journal-cases.ts | 15 - packages/testing/tests/assert-throws.test.ts | 141 ++++--- .../testing/tests/boundary-contract.test.ts | 177 +++++---- packages/testing/tests/test-component.test.ts | 238 ++++++------ .../testing/tests/testing-boundary.test.ts | 45 +-- packages/testing/tests/use-testing.test.ts | 228 ++++++------ .../tests/generated-observations.test.ts | 347 ++++++++---------- 7 files changed, 561 insertions(+), 630 deletions(-) delete mode 100644 packages/test-support/journal-cases.ts diff --git a/packages/test-support/journal-cases.ts b/packages/test-support/journal-cases.ts deleted file mode 100644 index 2fa110cfb..000000000 --- a/packages/test-support/journal-cases.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { it } from "./bdd.ts"; -import { captureTestJournal } from "./failure-journal.ts"; -import type { InMemoryStream } from "@executablemd/durable-streams"; -import type { Operation } from "effection"; - -/** Register a case that owns one fresh guarded document execution. */ -export function journalCases(owner: string) { - return ( - name: string, - source: string, - body: (stream: InMemoryStream) => Operation, - ): void => { - it(name, () => captureTestJournal({ owner, name }, source, body)); - }; -} diff --git a/packages/testing/tests/assert-throws.test.ts b/packages/testing/tests/assert-throws.test.ts index c7a7a0275..7a6e335af 100644 --- a/packages/testing/tests/assert-throws.test.ts +++ b/packages/testing/tests/assert-throws.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { scoped } from "effection"; import { expect } from "@executablemd/test-support/expect"; import { AssertionError } from "node:assert/strict"; @@ -6,9 +6,7 @@ import { Component } from "@executablemd/core"; import type { ComponentElement } from "@executablemd/core"; import { createTestHandlers } from "../src/handlers.ts"; import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; -import { documentCases, failureOf } from "./helpers.ts"; - -const documentCase = documentCases(); +import { failureOf, runDoc } from "./helpers.ts"; const STRICT = [ "---", @@ -32,43 +30,43 @@ function inTest(body: string): Record { describe("", () => { beforeEach(journalBeforeEach("packages/testing/tests/assert-throws.test.ts")); - documentCase("passes when the body raises an error matching a substring", function* (runFresh) { - const run = yield* runFresh( + it("passes when the body raises an error matching a substring", function* () { + const run = yield* runDoc( inTest(''), ); expect(run.completion.ok).toBe(true); expect(run.results[0]?.status).toBe("pass"); }); - documentCase("passes when the body raises an error matching a RegExp", function* (runFresh) { - const run = yield* runFresh( + it("passes when the body raises an error matching a RegExp", function* () { + const run = yield* runDoc( inTest(''), ); expect(run.results[0]?.status).toBe("pass"); }); - documentCase("fails when the body raises no error", function* (runFresh) { - const run = yield* runFresh( + it("fails when the body raises no error", function* () { + const run = yield* runDoc( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); }); - documentCase("fails when the raised error does not match", function* (runFresh) { - const run = yield* runFresh( + it("fails when the raised error does not match", function* () { + const run = yield* runDoc( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); }); - documentCase("rejects a missing message prop", function* (runFresh) { - const run = yield* runFresh(inTest('')); + it("rejects a missing message prop", function* () { + const run = yield* runDoc(inTest('')); expect(run.results[0]?.status).toBe("fail"); expect(run.output).toContain('requires a "message"'); }); - documentCase("rejects an unknown prop", function* (runFresh) { - const run = yield* runFresh( + it("rejects an unknown prop", function* () { + const run = yield* runDoc( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); @@ -78,8 +76,8 @@ describe("", () => { expect(run.results[0]?.error?.message).toContain("must NOT have additional properties"); }); - documentCase("rejects an expression-valued as", function* (runFresh) { - const run = yield* runFresh( + it("rejects an expression-valued as", function* () { + const run = yield* runDoc( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); @@ -87,24 +85,24 @@ describe("", () => { expect(run.results[0]?.error?.message).toContain("must be a string literal"); }); - documentCase("rejects an invalid as identifier", function* (runFresh) { - const run = yield* runFresh( + it("rejects an invalid as identifier", function* () { + const run = yield* runDoc( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); expect(run.output).toContain("identifier"); }); - documentCase("rejects a message that evaluates to an unsupported type", function* (runFresh) { - const run = yield* runFresh( + it("rejects a message that evaluates to an unsupported type", function* () { + const run = yield* runDoc( inTest(''), ); expect(run.results[0]?.status).toBe("fail"); expect(run.results[0]?.error?.message).toContain("string or a RegExp"); }); - documentCase("binds the complete caught error (incl. cause) via as", function* (runFresh) { - const run = yield* runFresh( + it("binds the complete caught error (incl. cause) via as", function* () { + const run = yield* runDoc( inTest( [ '', @@ -116,41 +114,35 @@ describe("", () => { expect(run.results.every((r) => r.status === "pass")).toBe(true); }); - documentCase( - "re-propagates a non-raise throw (a failing assertion in the body)", - function* (runFresh) { - const run = yield* runFresh( - inTest( - '', - ), - ); - expect(run.results[0]?.status).toBe("fail"); - expect(run.results[0]?.error).toMatchObject({ - kind: "assertion", - actual: "1", - expected: "2", - }); - expect(run.output).toContain("**AssertEquals** failed"); - expect(run.output).not.toContain("**AssertThrows** failed"); - }, - ); - - documentCase( - "stops expanding body children after the first raised error, rendering nothing", - function* (runFresh) { - const run = yield* runFresh({ - "Strict.md": STRICT, - "README.md": - 'SHOULD-NOT-APPEAR\n', - }); - expect(run.completion.ok).toBe(true); - expect(run.output).not.toContain("SHOULD-NOT-APPEAR"); - expect(run.output).not.toContain("must be number"); - }, - ); - - documentCase("swallows silently outside a during regular execution", function* (runFresh) { - const run = yield* runFresh({ + it("re-propagates a non-raise throw (a failing assertion in the body)", function* () { + const run = yield* runDoc( + inTest( + '', + ), + ); + expect(run.results[0]?.status).toBe("fail"); + expect(run.results[0]?.error).toMatchObject({ + kind: "assertion", + actual: "1", + expected: "2", + }); + expect(run.output).toContain("**AssertEquals** failed"); + expect(run.output).not.toContain("**AssertThrows** failed"); + }); + + it("stops expanding body children after the first raised error, rendering nothing", function* () { + const run = yield* runDoc({ + "Strict.md": STRICT, + "README.md": + 'SHOULD-NOT-APPEAR\n', + }); + expect(run.completion.ok).toBe(true); + expect(run.output).not.toContain("SHOULD-NOT-APPEAR"); + expect(run.output).not.toContain("must be number"); + }); + + it("swallows silently outside a during regular execution", function* () { + const run = yield* runDoc({ "Strict.md": STRICT, "README.md": 'before\n\nafter\n', @@ -165,8 +157,8 @@ describe("", () => { // to bind, and no other channel preserves a durable rendered segment — so a // passing renders nothing, even with --verbose. Failure // behavior is unchanged, which the cases above pin. - documentCase("emits no pass diagnostic, even with verbose", function* (runFresh) { - const run = yield* runFresh( + it("emits no pass diagnostic, even with verbose", function* () { + const run = yield* runDoc( { "Strict.md": STRICT, "README.md": '\n', @@ -177,19 +169,16 @@ describe("", () => { expect(run.output).not.toContain("**AssertThrows** passed"); }); - documentCase( - "aborts the document outside a when no error is raised, with a visible diagnostic", - function* (runFresh) { - const run = yield* runFresh( - { - "Strict.md": STRICT, - "README.md": 'before\n\nnever\n', - }, - { verbose: true }, - ); - expect(failureOf(run)).toBeInstanceOf(AssertionError); - expect(run.output).toContain("**AssertThrows** failed"); - expect(run.output).not.toContain("never"); - }, - ); + it("aborts the document outside a when no error is raised, with a visible diagnostic", function* () { + const run = yield* runDoc( + { + "Strict.md": STRICT, + "README.md": 'before\n\nnever\n', + }, + { verbose: true }, + ); + expect(failureOf(run)).toBeInstanceOf(AssertionError); + expect(run.output).toContain("**AssertThrows** failed"); + expect(run.output).not.toContain("never"); + }); }); diff --git a/packages/testing/tests/boundary-contract.test.ts b/packages/testing/tests/boundary-contract.test.ts index d94812e00..e3a8546be 100644 --- a/packages/testing/tests/boundary-contract.test.ts +++ b/packages/testing/tests/boundary-contract.test.ts @@ -8,9 +8,7 @@ import { beforeEach, describe, it, beforeAll } from "@executablemd/test-support/ import { useTempFileCompiler } from "@executablemd/core"; import { expect } from "@executablemd/test-support/expect"; import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; -import { documentCases } from "./helpers.ts"; - -const documentCase = documentCases(); +import { runDoc } from "./helpers.ts"; describe("Tier TC — the boundary", () => { beforeEach(journalBeforeEach("packages/testing/tests/boundary-contract.test.ts")); @@ -20,36 +18,33 @@ describe("Tier TC — the boundary", () => { // that is ``'s job — so an error beside a test renders where it was // written and the boundary still reports. Projecting the body through // `content()` instead would abort the boundary and lose both. - documentCase( - "TC1: an error beside a test renders inline, and the boundary still reports", - function* (runFresh) { - // The region prints, which is what makes "renders inline and the boundary - // still reports" a thing the document asked for. - const doc = [ - "", - "", - "", - "", - "", - '', - "", - "", - "", - "", - ].join("\n"); - - const run = yield* runFresh({ "README.md": doc }); - - // The stray error is rendered, not swallowed and not fatal… - expect(run.output).toContain("Failed to import component Missing"); - // …the test beside it still ran… - expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); - // …and the boundary still reported its outcome. - expect(run.boundaries).toEqual([{ tests: 1, failed: 0 }]); - }, - ); - - documentCase("TC2: text around a test is preserved in order", function* (runFresh) { + it("TC1: an error beside a test renders inline, and the boundary still reports", function* () { + // The region prints, which is what makes "renders inline and the boundary + // still reports" a thing the document asked for. + const doc = [ + "", + "", + "", + "", + "", + '', + "", + "", + "", + "", + ].join("\n"); + + const run = yield* runDoc({ "README.md": doc }); + + // The stray error is rendered, not swallowed and not fatal… + expect(run.output).toContain("Failed to import component Missing"); + // …the test beside it still ran… + expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); + // …and the boundary still reported its outcome. + expect(run.boundaries).toEqual([{ tests: 1, failed: 0 }]); + }); + + it("TC2: text around a test is preserved in order", function* () { const doc = [ "", "", @@ -63,14 +58,14 @@ describe("Tier TC — the boundary", () => { "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.output.indexOf("BEFORE")).toBeGreaterThan(-1); expect(run.output.indexOf("AFTER")).toBeGreaterThan(run.output.indexOf("BEFORE")); }); - documentCase("TC3: an empty boundary reports zero tests", function* (runFresh) { - const run = yield* runFresh({ "README.md": "\n\nnothing here\n\n\n" }); + it("TC3: an empty boundary reports zero tests", function* () { + const run = yield* runDoc({ "README.md": "\n\nnothing here\n\n\n" }); expect(run.boundaries).toEqual([{ tests: 0, failed: 0 }]); }); @@ -82,36 +77,33 @@ describe("Tier TC — what a keeps when it fails", () => { // The behavior `content()` cannot express: a test that fails partway still // shows what it rendered up to that point, then its diagnostics. - documentCase( - "TC4: output produced before a failure survives, and precedes the diagnostic", - function* (runFresh) { - const doc = [ - "", - '', - "", - "RENDERED FIRST", - "", - "", - "", - "NEVER REACHED", - "", - "", - "", - "", - ].join("\n"); - - const run = yield* runFresh({ "README.md": doc }); - - expect(run.results.map((entry) => entry.status)).toEqual(["fail"]); - expect(run.output).toContain("RENDERED FIRST"); - // The body stopped where it failed. - expect(run.output).not.toContain("NEVER REACHED"); - // The diagnostic follows what was rendered, rather than replacing it. - expect(run.output.indexOf("RENDERED FIRST")).toBeLessThan(run.output.indexOf("partial")); - }, - ); - - documentCase("TC5: exactly one result is recorded for a failing test", function* (runFresh) { + it("TC4: output produced before a failure survives, and precedes the diagnostic", function* () { + const doc = [ + "", + '', + "", + "RENDERED FIRST", + "", + "", + "", + "NEVER REACHED", + "", + "", + "", + "", + ].join("\n"); + + const run = yield* runDoc({ "README.md": doc }); + + expect(run.results.map((entry) => entry.status)).toEqual(["fail"]); + expect(run.output).toContain("RENDERED FIRST"); + // The body stopped where it failed. + expect(run.output).not.toContain("NEVER REACHED"); + // The diagnostic follows what was rendered, rather than replacing it. + expect(run.output.indexOf("RENDERED FIRST")).toBeLessThan(run.output.indexOf("partial")); + }); + + it("TC5: exactly one result is recorded for a failing test", function* () { const doc = [ "", '', @@ -119,34 +111,31 @@ describe("Tier TC — what a keeps when it fails", () => { "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results).toHaveLength(1); expect(run.boundaries).toEqual([{ tests: 1, failed: 1 }]); }); - documentCase( - "TC6: a failing test returns only text — no error segment escapes it", - function* (runFresh) { - const doc = [ - "", - '', - "", - "AFTER", - "", - "", - ].join("\n"); - - const run = yield* runFresh({ "README.md": doc }); - - expect(run.results.map((entry) => entry.status)).toEqual(["fail"]); - // The failure is contained in the test's own report; the boundary carries on. - expect(run.output).toContain("AFTER"); - expect(run.boundaries).toEqual([{ tests: 1, failed: 1 }]); - }, - ); - - documentCase("TC7: a later test runs after an earlier one fails", function* (runFresh) { + it("TC6: a failing test returns only text — no error segment escapes it", function* () { + const doc = [ + "", + '', + "", + "AFTER", + "", + "", + ].join("\n"); + + const run = yield* runDoc({ "README.md": doc }); + + expect(run.results.map((entry) => entry.status)).toEqual(["fail"]); + // The failure is contained in the test's own report; the boundary carries on. + expect(run.output).toContain("AFTER"); + expect(run.boundaries).toEqual([{ tests: 1, failed: 1 }]); + }); + + it("TC7: a later test runs after an earlier one fails", function* () { const doc = [ "", '', @@ -155,13 +144,13 @@ describe("Tier TC — what a keeps when it fails", () => { "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results.map((entry) => entry.name)).toEqual(["first", "second"]); expect(run.results.map((entry) => entry.status)).toEqual(["fail", "pass"]); }); - documentCase("TC8: a nested fails the enclosing test, and only it", function* (runFresh) { + it("TC8: a nested fails the enclosing test, and only it", function* () { const doc = [ "", '', @@ -171,14 +160,14 @@ describe("Tier TC — what a keeps when it fails", () => { "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results).toHaveLength(1); expect(run.results[0]?.name).toBe("outer"); expect(run.results[0]?.status).toBe("fail"); }); - documentCase("TC9: bindings a test captures do not escape it", function* (runFresh) { + it("TC9: bindings a test captures do not escape it", function* () { const doc = [ "", '', @@ -193,7 +182,7 @@ describe("Tier TC — what a keeps when it fails", () => { "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); // The binding did not leak into the document after the boundary. diff --git a/packages/testing/tests/test-component.test.ts b/packages/testing/tests/test-component.test.ts index bd2c9c669..02b6264a1 100644 --- a/packages/testing/tests/test-component.test.ts +++ b/packages/testing/tests/test-component.test.ts @@ -19,10 +19,9 @@ import type { ErrorSegment } from "@executablemd/core"; import { createTestHandlers } from "../src/handlers.ts"; import { absorbTestFailure, Staging } from "../src/test-component.ts"; import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; -import { documentCases, runDoc, withFreshDocument } from "./helpers.ts"; +import { runDoc, withFreshDocument } from "./helpers.ts"; const OWNER = "packages/testing/tests/test-component.test.ts"; -const documentCase = documentCases(); describe(" as a function component", () => { beforeEach(journalBeforeEach(OWNER)); @@ -34,59 +33,56 @@ describe(" as a function component", () => { 'yield* spawn(function* () { try { yield* suspend(); } finally { throw new Error("cleanup exploded"); } });', "```", ].join("\n"); - documentCase( - "keeps Try release safety local to the canonical failed Test report", - function* (runFresh) { - let released = 0; - let replacement = 0; - let finalized = 0; - yield* registerComponents([ - { - name: "BrokenTestRelease", - origin: "test-interaction", - props: { type: "object" }, - *fn() { - yield* ensure(() => { - released++; - throw new Error("test release failed"); - }); - return "body"; - }, + it("keeps Try release safety local to the canonical failed Test report", function* () { + let released = 0; + let replacement = 0; + let finalized = 0; + yield* registerComponents([ + { + name: "BrokenTestRelease", + origin: "test-interaction", + props: { type: "object" }, + *fn() { + yield* ensure(() => { + released++; + throw new Error("test release failed"); + }); + return "body"; }, - { - name: "TestReplacement", - origin: "test-interaction", - props: { type: "object" }, - *fn() { - replacement++; - return "WRONG"; - }, + }, + { + name: "TestReplacement", + origin: "test-interaction", + props: { type: "object" }, + *fn() { + replacement++; + return "WRONG"; }, - { - name: "TestFinally", - origin: "test-interaction", - props: { type: "object" }, - *fn() { - finalized++; - return "WRONG"; - }, + }, + { + name: "TestFinally", + origin: "test-interaction", + props: { type: "object" }, + *fn() { + finalized++; + return "WRONG"; }, - ]); - const run = yield* runFresh({ - "README.md": - '', - }); - expect(released).toBe(1); - expect(replacement).toBe(0); - expect(finalized).toBe(0); - expect(run.results.map((result) => [result.name, result.status])).toEqual([ - ["broken", "fail"], - ["fine", "pass"], - ]); - expect(run.completion.ok).toBe(false); - expect(run.output).not.toContain("WRONG"); - }, - ); + }, + ]); + const run = yield* runDoc({ + "README.md": + '', + }); + expect(released).toBe(1); + expect(replacement).toBe(0); + expect(finalized).toBe(0); + expect(run.results.map((result) => [result.name, result.status])).toEqual([ + ["broken", "fail"], + ["fine", "pass"], + ]); + expect(run.completion.ok).toBe(false); + expect(run.output).not.toContain("WRONG"); + }); it("keeps actual Files infrastructure failure globally terminal inside canonical Test", function* () { let fatal: unknown; @@ -148,7 +144,7 @@ describe(" as a function component", () => { // diagnostic before the test claims it. That must not cost the property the // interception exists for: a raise the test does NOT claim still fails it, // even when other middleware observed the segment first. - documentCase("fails the test on a raise an outer observer already saw", function* (runFresh) { + it("fails the test on a raise an outer observer already saw", function* () { const observed: ErrorSegment[] = []; const doc = [ "", @@ -178,7 +174,7 @@ describe(" as a function component", () => { return yield* next(segment); }, }); - return yield* runFresh({ "README.md": doc, "Strict.md": strict }); + return yield* runDoc({ "README.md": doc, "Strict.md": strict }); }); // The observer saw it, and the test still failed because of it. @@ -190,7 +186,7 @@ describe(" as a function component", () => { // Content a component projects is the CALLER's markdown, so `{meta.x}` in it // resolves against the expansion that wrote it, which projection has to be // handed. - documentCase("resolves the document's frontmatter inside a test body", function* (runFresh) { + it("resolves the document's frontmatter inside a test body", function* () { const doc = [ "---", "title: Executable MDX", @@ -205,7 +201,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results[0]?.status).toBe("pass"); expect(run.completion.ok).toBe(true); @@ -215,33 +211,30 @@ describe(" as a function component", () => { // own eval scope, so its cleanup failing IS the test's invocation teardown. // That arrives at the session boundary after has returned, and has to // land on that test's staged result. - documentCase( - "records a teardown failure as that test's outcome, and fails only it", - function* (runFresh) { - const doc = [ - "", - '', - FAILING_CLEANUP, - "", - "", - '', - "", - "", - ].join("\n"); - - const run = yield* runFresh({ "README.md": doc }); - - expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ - ["tears", "fail", "teardown"], - ["fine", "pass", undefined], - ]); - expect(run.results[0]?.error?.message).toContain("cleanup exploded"); - }, - ); + it("records a teardown failure as that test's outcome, and fails only it", function* () { + const doc = [ + "", + '', + FAILING_CLEANUP, + "", + "", + '', + "", + "", + ].join("\n"); + + const run = yield* runDoc({ "README.md": doc }); + + expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ + ["tears", "fail", "teardown"], + ["fine", "pass", undefined], + ]); + expect(run.results[0]?.error?.message).toContain("cleanup exploded"); + }); // The body's account is the more useful one, so a test that already failed // keeps its own classification when teardown fails too. - documentCase("keeps the body's classification when teardown also fails", function* (runFresh) { + it("keeps the body's classification when teardown also fails", function* () { const doc = [ "", '', @@ -252,7 +245,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results[0]?.status).toBe("fail"); expect(run.results[0]?.error?.kind).toBe("assertion"); @@ -260,38 +253,33 @@ describe(" as a function component", () => { // A component failing beside a test is not the test's outcome: the session // layer delegates anything that is not a invocation. - documentCase( - "does not turn a neighbouring component's failure into a test result", - function* (runFresh) { - const doc = ["", '', "", ""].join( - "\n", - ); - - const run = yield* scoped(function* () { - yield* registerComponents([ - { - name: "Wilting", - origin: "test", - props: { type: "object", properties: {}, additionalProperties: false }, - *fn() { - yield* ensure(function* () { - throw new Error("component cleanup exploded"); - }); - return ""; - }, + it("does not turn a neighbouring component's failure into a test result", function* () { + const doc = ["", '', "", ""].join("\n"); + + const run = yield* scoped(function* () { + yield* registerComponents([ + { + name: "Wilting", + origin: "test", + props: { type: "object", properties: {}, additionalProperties: false }, + *fn() { + yield* ensure(function* () { + throw new Error("component cleanup exploded"); + }); + return ""; }, - ]); - return yield* runFresh({ "README.md": doc }); - }); + }, + ]); + return yield* runDoc({ "README.md": doc }); + }); - // One result, for the test that contained it — classified as an ordinary - // error, because the component's teardown is the component's, not the - // test's. - expect(run.results).toHaveLength(1); - expect(run.results[0]?.name).toBe("only"); - expect(run.results[0]?.error?.kind).toBe("error"); - }, - ); + // One result, for the test that contained it — classified as an ordinary + // error, because the component's teardown is the component's, not the + // test's. + expect(run.results).toHaveLength(1); + expect(run.results[0]?.name).toBe("only"); + expect(run.results[0]?.error?.kind).toBe("error"); + }); /** Every journalled entry of `type`, by the name it recorded, in order. */ function* entriesOf(stream: InMemoryStream, type: string): Operation { @@ -381,7 +369,7 @@ describe(" as a function component", () => { // A timed-out body was halted mid-flight, so there is no partial output to // keep — the report is the diagnostic alone (§5.3). - documentCase("renders only the diagnostic when a test times out", function* (runFresh) { + it("renders only the diagnostic when a test times out", function* () { const doc = [ "", '', @@ -395,7 +383,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runFresh( + const run = yield* runDoc( { "README.md": doc }, { handlers: createTestHandlers({ timeoutMs: 60 }) }, ); @@ -407,7 +395,7 @@ describe(" as a function component", () => { // `timeout=` is the test's own bound: a body the installed default would cut // short finishes inside the duration the element declares (§5.3). - documentCase("lets a declared timeout outlive a shorter installed default", function* (runFresh) { + it("lets a declared timeout outlive a shorter installed default", function* () { const doc = [ "", '', @@ -420,7 +408,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runFresh( + const run = yield* runDoc( { "README.md": doc }, { handlers: createTestHandlers({ timeoutMs: 60 }) }, ); @@ -430,7 +418,7 @@ describe(" as a function component", () => { // The declared bound also cuts, and the diagnostic names the duration the // element declared rather than the default it replaced. - documentCase("times out at a declared timeout below the default", function* (runFresh) { + it("times out at a declared timeout below the default", function* () { const doc = [ "", '', @@ -442,7 +430,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results[0]?.error?.kind).toBe("timeout"); expect(run.results[0]?.error?.message).toContain("0.06 seconds"); @@ -450,7 +438,7 @@ describe(" as a function component", () => { // A malformed duration is refused where it was written, and where it was // written is one test — the ones after it still run. - documentCase("fails only the declaring test on a malformed timeout", function* (runFresh) { + it("fails only the declaring test on a malformed timeout", function* () { const doc = [ "", 'inside', @@ -459,7 +447,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results).toHaveLength(2); expect(run.results[0]?.status).toBe("fail"); @@ -469,7 +457,7 @@ describe(" as a function component", () => { // A teardown-failed test had already returned, so the engine replaces its // output with the boundary's diagnostic — no body text survives (§5.4). - documentCase("renders only the boundary diagnostic when teardown fails", function* (runFresh) { + it("renders only the boundary diagnostic when teardown fails", function* () { const doc = [ "", '', @@ -481,7 +469,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results[0]?.error?.kind).toBe("teardown"); expect(run.output).not.toContain("BODY TEXT"); @@ -493,13 +481,13 @@ describe(" as a function component", () => { // element that renders nothing — so the comparison is against the element // removed, not against the lines it sat between removed too (§5.2). const skippedCase = "renders nothing of itself when skipped"; - documentCase(skippedCase, function* (runFresh) { + it(skippedCase, function* () { const withTest = ["before", "", 'inside', "", "after", ""].join( "\n", ); const withoutTest = ["before", "", "", "", "after", ""].join("\n"); - const skipped = yield* runFresh({ "README.md": withTest }, { testing: false }); + const skipped = yield* runDoc({ "README.md": withTest }, { testing: false }); yield* withFreshDocument(OWNER, skippedCase, function* (runPlain) { const plain = yield* runPlain({ "README.md": withoutTest }, { testing: false }); expect(skipped.output).not.toContain("inside"); @@ -509,7 +497,7 @@ describe(" as a function component", () => { // A nested is invalid, and what that means is the ENCLOSING test's // recorded outcome — not the shape of anything it returned. - documentCase("fails the enclosing test on nesting, recorded as an error", function* (runFresh) { + it("fails the enclosing test on nesting, recorded as an error", function* () { const doc = [ "", '', @@ -517,7 +505,7 @@ describe(" as a function component", () => { "", ].join("\n"); - const run = yield* runFresh({ "README.md": doc }); + const run = yield* runDoc({ "README.md": doc }); expect(run.results.map((r) => [r.name, r.status, r.error?.kind])).toEqual([ ["outer", "fail", "error"], diff --git a/packages/testing/tests/testing-boundary.test.ts b/packages/testing/tests/testing-boundary.test.ts index 3c7f377c7..59f5c39c4 100644 --- a/packages/testing/tests/testing-boundary.test.ts +++ b/packages/testing/tests/testing-boundary.test.ts @@ -15,9 +15,7 @@ import type { Operation } from "effection"; import { InMemoryStream } from "@executablemd/durable-streams"; import { registerComponents, useTempFileCompiler } from "@executablemd/core"; import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; -import { documentCases, runDoc } from "./helpers.ts"; - -const documentCase = documentCases(); +import { runDoc } from "./helpers.ts"; import type { DocRun } from "./helpers.ts"; /** Every journalled boundary entry, by the name it recorded. */ @@ -129,26 +127,23 @@ describe("Tier TB — a repository component replaces ", () => { beforeEach(journalBeforeEach("packages/testing/tests/testing-boundary.test.ts")); beforeAll(() => useTempFileCompiler()); - documentCase( - "TB4: a local Testing component is selected, and the package's never runs", - function* (runFresh) { - const run = yield* runFresh({ - "README.md": [ - "", - '', - "", - "", - ].join("\n"), - "components/Testing.md": "LOCAL TESTING BOUNDARY\n", - }); - - // The repository component answered… - expect(run.output).toContain("LOCAL TESTING BOUNDARY"); - // …so the package implementation never activated testing: the `` - // inside stayed inert, recorded no result, and reported no boundary. - expect(run.results).toEqual([]); - expect(run.boundaries).toEqual([]); - expect(run.output).not.toContain("would-run"); - }, - ); + it("TB4: a local Testing component is selected, and the package's never runs", function* () { + const run = yield* runDoc({ + "README.md": [ + "", + '', + "", + "", + ].join("\n"), + "components/Testing.md": "LOCAL TESTING BOUNDARY\n", + }); + + // The repository component answered… + expect(run.output).toContain("LOCAL TESTING BOUNDARY"); + // …so the package implementation never activated testing: the `` + // inside stayed inert, recorded no result, and reported no boundary. + expect(run.results).toEqual([]); + expect(run.boundaries).toEqual([]); + expect(run.output).not.toContain("would-run"); + }); }); diff --git a/packages/testing/tests/use-testing.test.ts b/packages/testing/tests/use-testing.test.ts index 35fc75541..587beff6a 100644 --- a/packages/testing/tests/use-testing.test.ts +++ b/packages/testing/tests/use-testing.test.ts @@ -15,13 +15,10 @@ import { execute, Execution, registerComponents } from "@executablemd/core"; import type { Json } from "@executablemd/core"; import { Test, TestFailureError, testing } from "../src/test-api.ts"; import { useTesting } from "../src/use-testing.ts"; -import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; -import { documentCases, failureOf, runDoc, withFreshDocument } from "./helpers.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; +import { captureTestJournal, journalBeforeEach } from "@executablemd/test-support/failure-journal"; +import { failureOf, runDoc, withFreshDocument } from "./helpers.ts"; const OWNER = "packages/testing/tests/use-testing.test.ts"; -const documentCase = documentCases(); -const journalCase = journalCases(OWNER); function* drain( subscription: Subscription, @@ -92,42 +89,50 @@ describe("useTesting composition", () => { }); }); - journalCase( - "composes around core execute and returns session results", - "README.md", - function* (stream) { - const outcome = yield* scoped(function* () { - yield* useStubFs({ - "README.md": '\n', + it("composes around core execute and returns session results", function* () { + yield* captureTestJournal( + { owner: OWNER, name: "composes around core execute and returns session results" }, + "README.md", + function* (stream) { + const outcome = yield* scoped(function* () { + yield* useStubFs({ + "README.md": '\n', + }); + const tests = yield* useTesting(); + const execution = yield* execute({ path: "README.md", stream }); + const result = yield* execution; + const results = yield* tests.results; + return { result, results }; }); - const tests = yield* useTesting(); - const execution = yield* execute({ path: "README.md", stream }); - const result = yield* execution; - const results = yield* tests.results; - return { result, results }; - }); - expect(outcome.result.ok).toBe(true); - expect(outcome.results.map((r) => [r.name, r.status])).toEqual([["one", "pass"]]); - expect(Object.isFrozen(outcome.results)).toBe(true); - }, - ); + expect(outcome.result.ok).toBe(true); + expect(outcome.results.map((r) => [r.name, r.status])).toEqual([["one", "pass"]]); + expect(Object.isFrozen(outcome.results)).toBe(true); + }, + ); + }); - journalCase("keeps results available after a testing failure", "README.md", function* (stream) { - const outcome = yield* scoped(function* () { - yield* useStubFs({ - "README.md": '\n', - }); - const tests = yield* useTesting(); - const execution = yield* execute({ path: "README.md", stream }); - const result = yield* execution; - const results = yield* tests.results; - return { result, results }; - }); - expect(outcome.result.ok).toBe(false); - if (!outcome.result.ok) { - expect(outcome.result.error).toBeInstanceOf(TestFailureError); - } - expect(outcome.results.map((r) => [r.name, r.status])).toEqual([["bad", "fail"]]); + it("keeps results available after a testing failure", function* () { + yield* captureTestJournal( + { owner: OWNER, name: "keeps results available after a testing failure" }, + "README.md", + function* (stream) { + const outcome = yield* scoped(function* () { + yield* useStubFs({ + "README.md": '\n', + }); + const tests = yield* useTesting(); + const execution = yield* execute({ path: "README.md", stream }); + const result = yield* execution; + const results = yield* tests.results; + return { result, results }; + }); + expect(outcome.result.ok).toBe(false); + if (!outcome.result.ok) { + expect(outcome.result.error).toBeInstanceOf(TestFailureError); + } + expect(outcome.results.map((r) => [r.name, r.status])).toEqual([["bad", "fail"]]); + }, + ); }); it("preserves a core Err unchanged", function* () { @@ -181,7 +186,7 @@ describe("useTesting composition", () => { expect(error?.message).toContain("one session per execution scope"); }); - documentCase("removes all session middleware with its scope", function* (runFresh) { + it("removes all session middleware with its scope", function* () { yield* scoped(function* () { yield* useStubFs({ "README.md": "\n" }); yield* useTesting(); @@ -193,7 +198,7 @@ describe("useTesting composition", () => { // Outside the session scope: activation, collectors, and policy are gone. expect(yield* testing).toBe(false); expect(yield* Test.operations.results()).toEqual([]); - const second = yield* runFresh({ "README.md": "\n" }); + const second = yield* runDoc({ "README.md": "\n" }); expect(second.completion.ok).toBe(true); expect(second.output).not.toContain("Assert"); expect(second.results).toEqual([]); @@ -273,77 +278,84 @@ describe("useTesting composition", () => { expect(thrown?.message).toBe("pre-handle setup exploded"); }); - journalCase( - "a late subscriber still receives all chunks and the close value", - "README.md", - function* (stream) { - const { late, value } = yield* scoped(function* () { - yield* useStubFs({ "README.md": "hello world\n" }); - yield* useTesting(); - const execution = yield* execute({ - path: "README.md", - stream, - }); - const result = yield* execution; - const drained = yield* scoped(function* () { - return yield* drain(yield* execution.output); + it("a late subscriber still receives all chunks and the close value", function* () { + yield* captureTestJournal( + { owner: OWNER, name: "a late subscriber still receives all chunks and the close value" }, + "README.md", + function* (stream) { + const { late, value } = yield* scoped(function* () { + yield* useStubFs({ "README.md": "hello world\n" }); + yield* useTesting(); + const execution = yield* execute({ + path: "README.md", + stream, + }); + const result = yield* execution; + const drained = yield* scoped(function* () { + return yield* drain(yield* execution.output); + }); + return { late: drained, value: result }; }); - return { late: drained, value: result }; - }); - // No tests in the doc — the session rejects, but output is intact. - expect(value.ok).toBe(false); - expect(late.close).toContain("hello world"); - expect(late.chunks.join("")).toContain("hello world"); - }, - ); + // No tests in the doc — the session rejects, but output is intact. + expect(value.ok).toBe(false); + expect(late.close).toContain("hello world"); + expect(late.chunks.join("")).toContain("hello world"); + }, + ); + }); - journalCase( - "an early caller-scope halt tears down the document and leased eval scope", - "README.md", - function* (stream) { - Reflect.deleteProperty(globalThis, "__useTestingHaltMarker"); - yield* API.Process.around({ - *exec(_args, _next) { - yield* sleep(10_000); - return { exitCode: 0, stdout: "", stderr: "" }; - }, - }); - const doc = [ - "", - '', - "```js persist eval", - "globalThis.__useTestingHaltMarker = true;", - "yield* spawn(function* () { try { yield* suspend(); } finally { globalThis.__useTestingHaltMarker = false; } });", - "```", - "```bash exec", - "sleep 600", - "```", - "", - "", - "", - ].join("\n"); - yield* useStubFs({ "README.md": doc }); + it("an early caller-scope halt tears down the document and leased eval scope", function* () { + yield* captureTestJournal( + { + owner: OWNER, + name: "an early caller-scope halt tears down the document and leased eval scope", + }, + "README.md", + function* (stream) { + Reflect.deleteProperty(globalThis, "__useTestingHaltMarker"); + yield* API.Process.around({ + *exec(_args, _next) { + yield* sleep(10_000); + return { exitCode: 0, stdout: "", stderr: "" }; + }, + }); + const doc = [ + "", + '', + "```js persist eval", + "globalThis.__useTestingHaltMarker = true;", + "yield* spawn(function* () { try { yield* suspend(); } finally { globalThis.__useTestingHaltMarker = false; } });", + "```", + "```bash exec", + "sleep 600", + "```", + "", + "", + "", + ].join("\n"); + yield* useStubFs({ "README.md": doc }); - yield* scoped(function* () { - yield* useTesting(); - const execution = yield* execute({ path: "README.md", stream }); - yield* spawn(function* () { - yield* scoped(function* () { - yield* drain(yield* execution.output); + yield* scoped(function* () { + yield* useTesting(); + const execution = yield* execute({ path: "README.md", stream }); + yield* spawn(function* () { + yield* scoped(function* () { + yield* drain(yield* execution.output); + }); }); + // Converge on the document having run the eval block and spawned its + // effect, then leave the scope — halting everything mid-test. A fixed + // sleep races variable startup cost (#335). + yield* when( + function* () { + return Reflect.get(globalThis, "__useTestingHaltMarker") === true; + }, + { timeout: 10_000 }, + ); }); - // Converge on the document having run the eval block and spawned its - // effect, then leave the scope — halting everything mid-test. A fixed - // sleep races variable startup cost (#335). - yield* when( - function* () { - return Reflect.get(globalThis, "__useTestingHaltMarker") === true; - }, - { timeout: 10_000 }, - ); - }); - expect(Reflect.get(globalThis, "__useTestingHaltMarker")).toBe(false); - }, - ); + expect(Reflect.get(globalThis, "__useTestingHaltMarker")).toBe(false); + }, + ); + }); }); diff --git a/packages/workflow/tests/generated-observations.test.ts b/packages/workflow/tests/generated-observations.test.ts index 2e442e02e..5176b4df3 100644 --- a/packages/workflow/tests/generated-observations.test.ts +++ b/packages/workflow/tests/generated-observations.test.ts @@ -14,7 +14,7 @@ * and canonical execution reads that answer once before public policy. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { ensure, resource, scoped, sleep, useScope, withResolvers } from "effection"; import type { Operation } from "effection"; @@ -26,6 +26,7 @@ import type { DurableEvent, Json } from "@executablemd/durable-streams"; import { retainedSource, SOURCE_POSITION_FIELD } from "@executablemd/core"; import { executeInstalled } from "@executablemd/core/host"; import { collect } from "@executablemd/core"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; import { useWorkflowGenerated, workflowGeneratedSelection, @@ -33,10 +34,9 @@ import { import type { GeneratedEvaluationOptions } from "../src/deno/workspace/evaluate.ts"; import { recordedFiles, useRecordedFiles } from "../../core/tests/support/fragment-files.ts"; import type { RecordedFiles } from "../../core/tests/support/fragment-files.ts"; -import { journalCases } from "../../test-support/journal-cases.ts"; const ROOT_PATH = "workflows/agent.md"; -const journalCase = journalCases("packages/workflow/tests/generated-observations.test.ts"); +const OWNER = "packages/workflow/tests/generated-observations.test.ts"; const URL_ONE = "https://api.example.test/one"; const URL_TWO = "https://api.example.test/two"; const NOTE = "the retained note\n"; @@ -256,6 +256,7 @@ function statuses(events: readonly DurableEvent[]): string[] { } describe("Tier WGX — the selection a workflow run offers", () => { + beforeEach(journalBeforeEach(OWNER)); it("WGX1: the generic selection is File's two spellings and the deletion", function* () { // A generic Workflow host offers exactly this. `` and `` are // names a host chooses, so a run that chose neither has neither — which is @@ -268,145 +269,125 @@ describe("Tier WGX — the selection a workflow run offers", () => { yield* useScope(); }); - journalCase( - "WGX2: a marker-4 admission holds the selection and nothing live", - ROOT_PATH, - function* (stream) { - const files = recordedFiles({ "notes.md": NOTE }); - - const attempt = yield* run(evaluating(`\n`), { files }, stream); - - expect(attempt.failure).toBe(undefined); - expect(attempt.output).toContain("the retained note"); - const policy = policyOf(admissions(attempt.events)[0]); - // The closed shape this build writes: the marker, the selected classes and - // the rows. No Workspace basis and no request inventory — what a run stands - // on and what a request may reach are both live state, and a grant compared - // against a copy of either would refuse a run for its own progress. - expect(Object.keys(policy ?? {}).sort()).toEqual(["allow", "allowed", "version"]); - expect(policy?.version).toBe(4); - expect(policy?.allow).toEqual(["read"]); - expect(policy?.allowed).toEqual([ - { - name: "Json", - form: "self-closing", - identity: "https://executable.md/components/json", - kind: "capability", - effect: "composition", - }, - { - name: "File", - form: "self-closing", - identity: "https://executable.md/components/file/read", - kind: "component-answer", - effect: "read", - }, - ]); - }, - ); + it("WGX2: a marker-4 admission holds the selection and nothing live", function* () { + const stream = yield* useJournalStream(ROOT_PATH); + const files = recordedFiles({ "notes.md": NOTE }); - journalCase( - "WGX3: a fragment that asks for nothing is admitted and retains its source", - ROOT_PATH, - function* (stream) { - const attempt = yield* run(evaluating("nothing to observe here.\n"), {}, stream); + const attempt = yield* run(evaluating(`\n`), { files }, stream); + + expect(attempt.failure).toBe(undefined); + expect(attempt.output).toContain("the retained note"); + const policy = policyOf(admissions(attempt.events)[0]); + // The closed shape this build writes: the marker, the selected classes and + // the rows. No Workspace basis and no request inventory — what a run stands + // on and what a request may reach are both live state, and a grant compared + // against a copy of either would refuse a run for its own progress. + expect(Object.keys(policy ?? {}).sort()).toEqual(["allow", "allowed", "version"]); + expect(policy?.version).toBe(4); + expect(policy?.allow).toEqual(["read"]); + expect(policy?.allowed).toEqual([ + { + name: "Json", + form: "self-closing", + identity: "https://executable.md/components/json", + kind: "capability", + effect: "composition", + }, + { + name: "File", + form: "self-closing", + identity: "https://executable.md/components/file/read", + kind: "component-answer", + effect: "read", + }, + ]); + }); - expect(attempt.failure).toBe(undefined); - expect(attempt.output).toContain("nothing to observe here."); - expect(admissions(attempt.events)).toHaveLength(1); - expect(reads(attempt.events)).toHaveLength(0); - }, - ); + it("WGX3: a fragment that asks for nothing is admitted and retains its source", function* () { + const stream = yield* useJournalStream(ROOT_PATH); + const attempt = yield* run(evaluating("nothing to observe here.\n"), {}, stream); - journalCase( - "WGX4: a document with no `` makes no admission at all", - ROOT_PATH, - function* (stream) { - // The untaken half of the previous row: offering a selection is not - // evaluating anything, so a run that never writes the element appends no - // admission and resolves nothing on its behalf. - const attempt = yield* run("The run wrote no fragment.\n", {}, stream); - - expect(attempt.failure).toBe(undefined); - expect(attempt.output).toContain("The run wrote no fragment."); - expect(admissions(attempt.events)).toHaveLength(0); - }, - ); + expect(attempt.failure).toBe(undefined); + expect(attempt.output).toContain("nothing to observe here."); + expect(admissions(attempt.events)).toHaveLength(1); + expect(reads(attempt.events)).toHaveLength(0); + }); - journalCase( - "WGX5: a name this host did not offer is refused before any effect", - ROOT_PATH, - function* (stream) { - const files = recordedFiles({ "notes.md": NOTE }); - - // `` is a core default this host did not choose. A generic workflow - // run therefore has no search at all, and the refusal lands before the read - // beside it in the same fragment. - const attempt = yield* run( - evaluating(`\n\n`), - { files }, - stream, - ); + it("WGX4: a document with no `` makes no admission at all", function* () { + const stream = yield* useJournalStream(ROOT_PATH); + // The untaken half of the previous row: offering a selection is not + // evaluating anything, so a run that never writes the element appends no + // admission and resolves nothing on its behalf. + const attempt = yield* run("The run wrote no fragment.\n", {}, stream); - expect(attempt.failure).toContain("did not admit"); - expect(files.performed).toEqual([]); - expect(admissions(attempt.events).length).toBeLessThanOrEqual(1); - expect(reads(attempt.events)).toHaveLength(0); - }, - ); + expect(attempt.failure).toBe(undefined); + expect(attempt.output).toContain("The run wrote no fragment."); + expect(admissions(attempt.events)).toHaveLength(0); + }); + + it("WGX5: a name this host did not offer is refused before any effect", function* () { + const stream = yield* useJournalStream(ROOT_PATH); + const files = recordedFiles({ "notes.md": NOTE }); + + // `` is a core default this host did not choose. A generic workflow + // run therefore has no search at all, and the refusal lands before the read + // beside it in the same fragment. + const attempt = yield* run( + evaluating(`\n\n`), + { files }, + stream, + ); + + expect(attempt.failure).toContain("did not admit"); + expect(files.performed).toEqual([]); + expect(admissions(attempt.events).length).toBeLessThanOrEqual(1); + expect(reads(attempt.events)).toHaveLength(0); + }); }); describe("Tier WGX — what an admitted request may reach is live", () => { - journalCase( - "WGX6: a host that bounds no request offers `` not at all", - ROOT_PATH, - function* (stream) { - const transport = yield* useTransport(); + beforeEach(journalBeforeEach(OWNER)); + it("WGX6: a host that bounds no request offers `` not at all", function* () { + const stream = yield* useJournalStream(ROOT_PATH); + const transport = yield* useTransport(); - const attempt = yield* run(evaluating(`\n`), {}, stream); + const attempt = yield* run(evaluating(`\n`), {}, stream); - expect(attempt.failure).toContain("did not admit"); - expect(transport.performed).toHaveLength(0); - }, - ); + expect(attempt.failure).toContain("did not admit"); + expect(transport.performed).toHaveLength(0); + }); - journalCase( - "WGX7: a bounded destination performs once and retains one observation", - ROOT_PATH, - function* (stream) { - const transport = yield* useTransport(); + it("WGX7: a bounded destination performs once and retains one observation", function* () { + const stream = yield* useJournalStream(ROOT_PATH); + const transport = yield* useTransport(); - const attempt = yield* run( - evaluating(`\n\n`), - { generated: { requests: [{ url: URL_ONE }] } }, - stream, - ); + const attempt = yield* run( + evaluating(`\n\n`), + { generated: { requests: [{ url: URL_ONE }] } }, + stream, + ); - expect(attempt.failure).toBe(undefined); - expect(transport.performed.map((call) => call.url)).toEqual([URL_ONE]); - expect(fetches(attempt.events)).toHaveLength(1); - }, - ); + expect(attempt.failure).toBe(undefined); + expect(transport.performed.map((call) => call.url)).toEqual([URL_ONE]); + expect(fetches(attempt.events)).toHaveLength(1); + }); - journalCase( - "WGX8: a destination outside the bound reaches no transport", - ROOT_PATH, - function* (stream) { - const transport = yield* useTransport(); - - // The name is offered — this host bounds a request — and the destination is - // not. So the refusal is the live terminal's, asked immediately before - // transport, and nothing is sent. - const attempt = yield* run( - evaluating(`\n\n`), - { generated: { requests: [{ url: URL_ONE }] } }, - stream, - ); + it("WGX8: a destination outside the bound reaches no transport", function* () { + const stream = yield* useJournalStream(ROOT_PATH); + const transport = yield* useTransport(); - expect(attempt.failure).toContain("not permitted in this scope"); - expect(transport.performed).toHaveLength(0); - }, - ); + // The name is offered — this host bounds a request — and the destination is + // not. So the refusal is the live terminal's, asked immediately before + // transport, and nothing is sent. + const attempt = yield* run( + evaluating(`\n\n`), + { generated: { requests: [{ url: URL_ONE }] } }, + stream, + ); + + expect(attempt.failure).toContain("not permitted in this scope"); + expect(transport.performed).toHaveLength(0); + }); it("WGX9: a bound this host stated cannot be moved by editing what it was given", function* () { const transport = yield* useTransport(); @@ -508,70 +489,62 @@ describe("Tier WGX — what an admitted request may reach is live", () => { expect(transport.performed).toHaveLength(0); }); - journalCase( - "WGX15: the request allowance is read once, and `` comes from that reading", - ROOT_PATH, - function* (stream) { - const server = yield* useLoopback(); - let reads = 0; - // An ordinary accessor-backed options object, which is all a host has to be - // for this to matter: the first reading states no bound and a later one - // states one. Whichever this host meant, the name a fragment may write and - // the policy deciding what it may reach have to come from one reading. - const changing: GeneratedEvaluationOptions = { - get requests() { - reads++; - return reads === 1 ? [] : [{ url: `${server.origin}/admitted` }]; - }, - }; + it("WGX15: the request allowance is read once, and `` comes from that reading", function* () { + const stream = yield* useJournalStream(ROOT_PATH); + const server = yield* useLoopback(); + let reads = 0; + // An ordinary accessor-backed options object, which is all a host has to be + // for this to matter: the first reading states no bound and a later one + // states one. Whichever this host meant, the name a fragment may write and + // the policy deciding what it may reach have to come from one reading. + const changing: GeneratedEvaluationOptions = { + get requests() { + reads++; + return reads === 1 ? [] : [{ url: `${server.origin}/admitted` }]; + }, + }; - const attempt = yield* run( - evaluating( - `\n\n`, - ), - { generated: changing }, - stream, - ); + const attempt = yield* run( + evaluating(`\n\n`), + { generated: changing }, + stream, + ); - // Asked first, because it is the consequence: the destination the fragment - // names is one no reading of this host's list ever admitted, and a - // selection taken from a second reading would send it under no policy at - // all. - expect(server.asked).toEqual([]); - // The retained reading bounded nothing, so `` was never offered: the - // fragment is refused at admission, by name, and retains no effect. - expect(attempt.failure).toContain("did not admit"); - expect(fetches(attempt.events)).toEqual([]); - // One reading for the whole setup. A second one is what would let the - // selection and the policy disagree about the same host statement. - expect(reads).toBe(1); - }, - ); + // Asked first, because it is the consequence: the destination the fragment + // names is one no reading of this host's list ever admitted, and a + // selection taken from a second reading would send it under no policy at + // all. + expect(server.asked).toEqual([]); + // The retained reading bounded nothing, so `` was never offered: the + // fragment is refused at admission, by name, and retains no effect. + expect(attempt.failure).toContain("did not admit"); + expect(fetches(attempt.events)).toEqual([]); + // One reading for the whole setup. A second one is what would let the + // selection and the policy disagree about the same host statement. + expect(reads).toBe(1); + }); - journalCase( - "WGX10: an enclosing restriction still refuses what this host bounded", - ROOT_PATH, - function* (stream) { - const transport = yield* useTransport(); - - const attempt = yield* scoped(function* () { - // Narrower than the host, installed outside it. A restriction delegates - // rather than answering `true`, so the enclosing refusal is never asked - // to delegate and the bound cannot widen it. - yield* Permissions.around({ fetch: () => false }); - return yield* run( - evaluating(`\n\n`), - { - generated: { requests: [{ url: URL_ONE }] }, - }, - stream, - ); - }); + it("WGX10: an enclosing restriction still refuses what this host bounded", function* () { + const stream = yield* useJournalStream(ROOT_PATH); + const transport = yield* useTransport(); - expect(attempt.failure).toContain("not permitted in this scope"); - expect(transport.performed).toHaveLength(0); - }, - ); + const attempt = yield* scoped(function* () { + // Narrower than the host, installed outside it. A restriction delegates + // rather than answering `true`, so the enclosing refusal is never asked + // to delegate and the bound cannot widen it. + yield* Permissions.around({ fetch: () => false }); + return yield* run( + evaluating(`\n\n`), + { + generated: { requests: [{ url: URL_ONE }] }, + }, + stream, + ); + }); + + expect(attempt.failure).toContain("not permitted in this scope"); + expect(transport.performed).toHaveLength(0); + }); }); describe("Tier WGX — a completed effect restores from its record", () => { From 321e082913c11059242eccc9dc6fd5fc0e0bfb5a Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Tue, 17 Nov 2026 00:12:19 -0500 Subject: [PATCH 10/11] Remove unused testing case registrar --- packages/testing/tests/helpers.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/testing/tests/helpers.ts b/packages/testing/tests/helpers.ts index de1d9d150..3ad0e6b61 100644 --- a/packages/testing/tests/helpers.ts +++ b/packages/testing/tests/helpers.ts @@ -11,7 +11,6 @@ import { forEach } from "@effectionx/stream-helpers"; import type { DurableStream } from "@executablemd/durable-streams"; import { useStubFs } from "@executablemd/runtime/test"; import { execute } from "@executablemd/core"; -import { it } from "@executablemd/test-support/bdd"; import { captureTestJournal, useJournalStream } from "@executablemd/test-support/failure-journal"; import { useTesting } from "../src/use-testing.ts"; import { installHandlers, installTestingComponents } from "../src/components.ts"; @@ -47,12 +46,6 @@ type RunFreshDoc = ( options?: Omit, ) => Operation; -export function documentCases() { - return (name: string, body: (run: RunFreshDoc) => Operation): void => { - it(name, () => body((files, options) => runDoc(files, options))); - }; -} - export function withFreshDocument( owner: string, name: string, From abacea6c30ab87ad900cb5b0e2df7722d58f20b1 Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Tue, 17 Nov 2026 00:19:51 -0500 Subject: [PATCH 11/11] Restore direct test declarations throughout the base journal PR --- .../tests/component-invocation-hooks.test.ts | 287 +++--- .../core/tests/host-generated-root.test.ts | 369 ++++---- packages/core/tests/support/held-host.ts | 13 +- .../git/tests/git-observations-deno.test.ts | 216 ++--- packages/git/tests/git-observations.test.ts | 858 +++++++++--------- .../git/tests/run-composition-ambient.test.ts | 620 +++++++------ .../git/tests/run-composition-managed.test.ts | 295 +++--- .../git/tests/run-composition-remote.test.ts | 158 ++-- packages/git/tests/support/run-composition.ts | 14 +- 9 files changed, 1405 insertions(+), 1425 deletions(-) diff --git a/packages/core/tests/component-invocation-hooks.test.ts b/packages/core/tests/component-invocation-hooks.test.ts index 8360c357b..ce12a620f 100644 --- a/packages/core/tests/component-invocation-hooks.test.ts +++ b/packages/core/tests/component-invocation-hooks.test.ts @@ -1,7 +1,8 @@ import { capture } from "../src/component-api.ts"; import { Elicitation } from "../src/elicitation-api.ts"; import { API } from "@executablemd/runtime"; -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; import { expect } from "@executablemd/test-support/expect"; import { ensure, sleep, spawn, suspend, withResolvers } from "effection"; import type { Operation, Result } from "effection"; @@ -20,9 +21,7 @@ import type { IdentityComponent, } from "../host.ts"; import { retainedSource } from "../src/root-source.ts"; -import { heldHostCases, useHeldHost } from "./support/held-host.ts"; - -const heldHostCase = heldHostCases("packages/core/tests/component-invocation-hooks.test.ts"); +import { useHeldHost } from "./support/held-host.ts"; const props = { type: "object", properties: {}, additionalProperties: false }; function declaration(body: () => Operation, name = "Observed"): IdentityComponent { @@ -41,153 +40,149 @@ function* text(): Operation { } describe("Installation-owned component observation", () => { - heldHostCase( - "R4a/R6: preserves dual-form dispatch, generated identity and accepted values", - function* (stream) { - const events: string[] = []; - const observations: ComponentInvocationObservation[] = []; - const values: unknown[] = []; - const names: string[] = []; - let projected = 0; - const component: IdentityComponent = { - name: "Observed", - origin: "test://observation", - props: { - type: "object", - properties: { - nested: { - type: "object", - properties: { label: { type: "string", default: "default" } }, - additionalProperties: false, - }, + beforeEach(journalBeforeEach("packages/core/tests/component-invocation-hooks.test.ts")); + + it("R4a/R6: preserves dual-form dispatch, generated identity and accepted values", function* () { + const stream = yield* useJournalStream("hooks.md"); + const events: string[] = []; + const observations: ComponentInvocationObservation[] = []; + const values: unknown[] = []; + const names: string[] = []; + let projected = 0; + const component: IdentityComponent = { + name: "Observed", + origin: "test://observation", + props: { + type: "object", + properties: { + nested: { + type: "object", + properties: { label: { type: "string", default: "default" } }, + additionalProperties: false, }, - additionalProperties: false, - }, - captures: ["raw"], - returns: { - type: "object", - properties: { form: { type: "string" }, accepted: { type: "boolean", default: true } }, - required: ["form", "accepted"], - additionalProperties: false, - }, - forms: ["self-closing", "paired"], - generated: { - "self-closing": { identity: "https://executable.md/test/observed/read", effect: "read" }, - paired: { identity: "https://executable.md/test/observed/write", effect: "write" }, }, - factory(claim) { - function body(form: string) { - return function* ( - input: Record, - invocation: Parameters[0], - ) { - const name = yield* claim(invocation); - names.push(name); - if (form === "write") { - yield* content(); - } - const nested = input.nested; - if (nested !== null && typeof nested === "object" && !Array.isArray(nested)) { - nested.label = "changed"; - } - yield createDurableOperation({ type: "observed", name }, function* () { - events.push(form); - return form; - }); - return { form }; - }; - } - return formDispatcher({ - forms: "both", - "self-closing": body("read"), - paired: body("write"), - }); - }, - }; - const hooks: ComponentInvocationHooks = { - *before(observation) { - expect(projected).toBe(observations.length < 2 ? 0 : 1); - observations.push(observation); - expect(Object.isFrozen(observation)).toBe(true); - expect(Object.isFrozen(observation.props.nested)).toBe(true); - expect(observation.props).toEqual({ nested: { label: "default" } }); - yield* sleep(0); - }, - *after(observation, outcome, cancelled) { - expect(observations.includes(observation)).toBe(true); - expect(cancelled).toBe(false); - expect(observation.props).toEqual({ nested: { label: "default" } }); - if (outcome.ok) { - values.push(outcome.value); - } - yield* sleep(0); - }, - }; - const installation: ExecutionInstallation = { - components: [component], - componentInvocations: hooks, - *install() { - hooks.before = function* () { - throw new Error("late mutation"); + additionalProperties: false, + }, + captures: ["raw"], + returns: { + type: "object", + properties: { form: { type: "string" }, accepted: { type: "boolean", default: true } }, + required: ["form", "accepted"], + additionalProperties: false, + }, + forms: ["self-closing", "paired"], + generated: { + "self-closing": { identity: "https://executable.md/test/observed/read", effect: "read" }, + paired: { identity: "https://executable.md/test/observed/write", effect: "write" }, + }, + factory(claim) { + function body(form: string) { + return function* (input: Record, invocation: Parameters[0]) { + const name = yield* claim(invocation); + names.push(name); + if (form === "write") { + yield* content(); + } + const nested = input.nested; + if (nested !== null && typeof nested === "object" && !Array.isArray(nested)) { + nested.label = "changed"; + } + yield createDurableOperation({ type: "observed", name }, function* () { + events.push(form); + return form; + }); + return { form }; }; - hooks.after = hooks.before; - yield* registerComponents([ - { - name: "Projected", - origin: "test://projection", - props, - *fn() { - projected++; - yield* sleep(0); - return "content"; - }, + } + return formDispatcher({ + forms: "both", + "self-closing": body("read"), + paired: body("write"), + }); + }, + }; + const hooks: ComponentInvocationHooks = { + *before(observation) { + expect(projected).toBe(observations.length < 2 ? 0 : 1); + observations.push(observation); + expect(Object.isFrozen(observation)).toBe(true); + expect(Object.isFrozen(observation.props.nested)).toBe(true); + expect(observation.props).toEqual({ nested: { label: "default" } }); + yield* sleep(0); + }, + *after(observation, outcome, cancelled) { + expect(observations.includes(observation)).toBe(true); + expect(cancelled).toBe(false); + expect(observation.props).toEqual({ nested: { label: "default" } }); + if (outcome.ok) { + values.push(outcome.value); + } + yield* sleep(0); + }, + }; + const installation: ExecutionInstallation = { + components: [component], + componentInvocations: hooks, + *install() { + hooks.before = function* () { + throw new Error("late mutation"); + }; + hooks.after = hooks.before; + yield* registerComponents([ + { + name: "Projected", + origin: "test://projection", + props, + *fn() { + projected++; + yield* sleep(0); + return "content"; }, - ]); - yield* Component.around({ generated: () => ({ Observed: ["self-closing", "paired"] }) }); - }, - }; - const held = yield* useHeldHost({ - stream, - installations: [installation], - root: { - path: "hooks.md", - source: - '\n\n', - }, - inside: function* () { - const caller = yield* Component.operations.env; - expect(caller?.values.inputs).toEqual({}); - expect(caller?.values.read).toEqual({ form: "read", accepted: true }); - expect(caller?.values.write).toEqual({ form: "write", accepted: true }); - }, - }); - expect(events).toEqual(["read", "write"]); - const evaluated = yield* held.execution.evaluateGeneratedXmdRoot({ - id: "dual", + }, + ]); + yield* Component.around({ generated: () => ({ Observed: ["self-closing", "paired"] }) }); + }, + }; + const held = yield* useHeldHost({ + stream, + installations: [installation], + root: { + path: "hooks.md", source: - '\nbody\n', - allow: ["read", "write"], - }); - expect(evaluated.ok).toBe(true); - expect(events).toEqual(["read", "write", "read", "write"]); - expect(values).toEqual([ - { form: "read", accepted: true }, - { form: "write", accepted: true }, - { form: "read", accepted: true }, - { form: "write", accepted: true }, - ]); - expect(new Set(names).size).toBe(4); - expect(new Set(observations).size).toBe(4); - const refused = yield* held.execution.evaluateGeneratedXmdRoot({ - id: "readonly", - source: 'body', - allow: ["read"], - }); - expect(refused.ok).toBe(false); - expect(events).toHaveLength(4); - }, - "hooks.md", - ); + '\n\n', + }, + inside: function* () { + const caller = yield* Component.operations.env; + expect(caller?.values.inputs).toEqual({}); + expect(caller?.values.read).toEqual({ form: "read", accepted: true }); + expect(caller?.values.write).toEqual({ form: "write", accepted: true }); + }, + }); + expect(events).toEqual(["read", "write"]); + const evaluated = yield* held.execution.evaluateGeneratedXmdRoot({ + id: "dual", + source: + '\nbody\n', + allow: ["read", "write"], + }); + expect(evaluated.ok).toBe(true); + expect(events).toEqual(["read", "write", "read", "write"]); + expect(values).toEqual([ + { form: "read", accepted: true }, + { form: "write", accepted: true }, + { form: "read", accepted: true }, + { form: "write", accepted: true }, + ]); + expect(new Set(names).size).toBe(4); + expect(new Set(observations).size).toBe(4); + const refused = yield* held.execution.evaluateGeneratedXmdRoot({ + id: "readonly", + source: 'body', + allow: ["read"], + }); + expect(refused.ok).toBe(false); + expect(events).toHaveLength(4); + }); it("R4b: independently optional callbacks and invalid inputs", function* () { for (const mode of ["none", "before", "after"]) { diff --git a/packages/core/tests/host-generated-root.test.ts b/packages/core/tests/host-generated-root.test.ts index 0f152099b..a5b26cf71 100644 --- a/packages/core/tests/host-generated-root.test.ts +++ b/packages/core/tests/host-generated-root.test.ts @@ -68,7 +68,7 @@ import { durabilityFailure } from "../src/errors.ts"; import { Execution, execute } from "../src/execute.ts"; import { retainedSource } from "../src/root-source.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; -import { heldHostCases, HELD_ROOT_PATH, useHeldHost, withHeldHost } from "./support/held-host.ts"; +import { HELD_ROOT_PATH, useHeldHost, withHeldHost } from "./support/held-host.ts"; import * as hostModule from "../host.ts"; import { executeInstalled, HostExecutionError } from "../host.ts"; // The request and the class it names are taken from the trusted host @@ -87,7 +87,6 @@ import type { FunctionComponent } from "../src/types.ts"; import { formDispatcher } from "../src/invocation-identity.ts"; const OWNER = "packages/core/tests/host-generated-root.test.ts"; -const heldHostCase = heldHostCases(OWNER); describe("held host failure journal", () => { const name = "saves the held root after cleanup and an enclosing assertion failure"; @@ -1091,71 +1090,76 @@ describe("Tier E5 — cancelling joins before anything else starts", () => { ).toBe(false); }); - heldHostCase( - "E5b: closing the execution joins a held evaluation before closure completes", - function* (stream) { - const order: string[] = []; - const reached = withResolvers(); - const release = withResolvers(); - const saved: { execution?: HostExecution } = {}; - const interrupted: { answer?: Result } = {}; - const invoked: string[] = []; - - yield* scoped(function* () { - const open = yield* useHeldHost({ - stream, - // The latch is resolved by the document element's own cleanup, which - // unwinds before the evaluation runner does. So the evaluation's - // cleanup is genuinely held at the moment closure begins, and it is the - // closing that frees it rather than this case pre-arranging the order. - *inside(): Operation { - yield* ensure(() => { - release.resolve(); - }); - }, - installations: [selecting([...declared(), holding(reached, release.operation, order)])], - }); - saved.execution = open.execution; - const running = yield* spawn(() => - open.execution.evaluateGeneratedXmdRoot({ - id: "turn-1", - source: "\n\n\n", - }), - ); - yield* reached.operation; - // Closing the execution: halting the handle's consumer takes the - // invocation down with it. - const closing = yield* spawn(function* () { - yield* open.watched.halt(); - order.push("closed"); + it("E5b: closing the execution joins a held evaluation before closure completes", () => + captureTestJournal( + { + owner: OWNER, + name: "E5b: closing the execution joins a held evaluation before closure completes", + }, + HELD_ROOT_PATH, + function* (stream) { + const order: string[] = []; + const reached = withResolvers(); + const release = withResolvers(); + const saved: { execution?: HostExecution } = {}; + const interrupted: { answer?: Result } = {}; + const invoked: string[] = []; + + yield* scoped(function* () { + const open = yield* useHeldHost({ + stream, + // The latch is resolved by the document element's own cleanup, which + // unwinds before the evaluation runner does. So the evaluation's + // cleanup is genuinely held at the moment closure begins, and it is the + // closing that frees it rather than this case pre-arranging the order. + *inside(): Operation { + yield* ensure(() => { + release.resolve(); + }); + }, + installations: [selecting([...declared(), holding(reached, release.operation, order)])], + }); + saved.execution = open.execution; + const running = yield* spawn(() => + open.execution.evaluateGeneratedXmdRoot({ + id: "turn-1", + source: "\n\n\n", + }), + ); + yield* reached.operation; + // Closing the execution: halting the handle's consumer takes the + // invocation down with it. + const closing = yield* spawn(function* () { + yield* open.watched.halt(); + order.push("closed"); + }); + yield* closing; + interrupted.answer = yield* running; }); - yield* closing; - interrupted.answer = yield* running; - }); - // The held cleanup joined, and only then did closure complete. - expect(order).toEqual(["evaluation cleanup", "closed"]); - // The caller was told the execution closed rather than left waiting on an - // answer nothing was left to produce. - expect(interrupted.answer?.ok).toBe(false); - expect(interrupted.answer?.ok === false && interrupted.answer.error).toBeInstanceOf( - HostExecutionError, - ); - expect(interrupted.answer?.ok === false && interrupted.answer.error.message).toContain( - "closed while it was evaluating", - ); + // The held cleanup joined, and only then did closure complete. + expect(order).toEqual(["evaluation cleanup", "closed"]); + // The caller was told the execution closed rather than left waiting on an + // answer nothing was left to produce. + expect(interrupted.answer?.ok).toBe(false); + expect(interrupted.answer?.ok === false && interrupted.answer.error).toBeInstanceOf( + HostExecutionError, + ); + expect(interrupted.answer?.ok === false && interrupted.answer.error.message).toContain( + "closed while it was evaluating", + ); - const execution = saved.execution; - if (execution === undefined) { - throw new Error("the case never held an execution"); - } - const after = yield* execution.evaluateGeneratedXmdRoot( - request("\n\n\n", invoked), - ); - expect(after.ok).toBe(false); - expect(invoked).toEqual([]); - }, - ); + const execution = saved.execution; + if (execution === undefined) { + throw new Error("the case never held an execution"); + } + const after = yield* execution.evaluateGeneratedXmdRoot( + request("\n\n\n", invoked), + ); + expect(after.ok).toBe(false); + expect(invoked).toEqual([]); + }, + )); }); describe("Tier E6 — reopening without replaying a response", () => { @@ -1419,32 +1423,37 @@ function failingMain(): NonNullable[number] describe("Tier E7 — which failures stay the run's", () => { beforeAll(() => useTempFileCompiler()); - heldHostCase( - "E7a: an ordinary control failure is a Result carrying the real partial render", - function* (stream) { - const invoked: string[] = []; - const answer: { failed?: Result } = {}; - - const run = yield* withHeldHost( - { stream, installations: [selecting(declared(invoked))] }, - function* (open) { - answer.failed = yield* open.execution.evaluateGeneratedXmdRoot({ - id: "turn-1", - source: "\nbefore the failure\n\n\n\n", - }); - }, - ); + it("E7a: an ordinary control failure is a Result carrying the real partial render", () => + captureTestJournal( + { + owner: OWNER, + name: "E7a: an ordinary control failure is a Result carrying the real partial render", + }, + HELD_ROOT_PATH, + function* (stream) { + const invoked: string[] = []; + const answer: { failed?: Result } = {}; + + const run = yield* withHeldHost( + { stream, installations: [selecting(declared(invoked))] }, + function* (open) { + answer.failed = yield* open.execution.evaluateGeneratedXmdRoot({ + id: "turn-1", + source: "\nbefore the failure\n\n\n\n", + }); + }, + ); - // The request's own failure, so the execution is untouched by it. - expect(run.completion.ok).toBe(true); - expect(answer.failed?.ok).toBe(false); - const error: (Error & { partial?: GeneratedXmdRootResult }) | undefined = - answer.failed?.ok === false ? answer.failed.error : undefined; - expect(error?.message).toContain("the control refused this request"); - expect(error?.partial?.output).toContain("before the failure"); - expect(invoked).toEqual(["boomed"]); - }, - ); + // The request's own failure, so the execution is untouched by it. + expect(run.completion.ok).toBe(true); + expect(answer.failed?.ok).toBe(false); + const error: (Error & { partial?: GeneratedXmdRootResult }) | undefined = + answer.failed?.ok === false ? answer.failed.error : undefined; + expect(error?.message).toContain("the control refused this request"); + expect(error?.partial?.output).toContain("before the failure"); + expect(invoked).toEqual(["boomed"]); + }, + )); it("E7b: a persistence failure stays itself, and the run does not succeed", function* () { const stream = new InMemoryStream(); @@ -1487,32 +1496,34 @@ describe("Tier E7 — which failures stay the run's", () => { expect(invoked).toEqual([]); }); - heldHostCase( - "E7c: a teardown failure stays itself, and the run does not succeed", - function* (stream) { - const caught: { raised?: unknown; completion?: Result } = {}; + it("E7c: a teardown failure stays itself, and the run does not succeed", () => + captureTestJournal( + { owner: OWNER, name: "E7c: a teardown failure stays itself, and the run does not succeed" }, + HELD_ROOT_PATH, + function* (stream) { + const caught: { raised?: unknown; completion?: Result } = {}; - yield* scoped(function* () { - const open = yield* useHeldHost({ stream, installations: [selecting(declared())] }); - try { - yield* open.execution.evaluateGeneratedXmdRoot({ - id: "turn-1", - source: "\n\n\n", - }); - } catch (error) { - caught.raised = error; - } - open.release(); - caught.completion = yield* open.watched; - }); + yield* scoped(function* () { + const open = yield* useHeldHost({ stream, installations: [selecting(declared())] }); + try { + yield* open.execution.evaluateGeneratedXmdRoot({ + id: "turn-1", + source: "\n\n\n", + }); + } catch (error) { + caught.raised = error; + } + open.release(); + caught.completion = yield* open.watched; + }); - // Work the root owned did not finish unwinding, so a rendering reported - // beside it would describe a root still running. - expect(caught.raised).not.toBe(undefined); - expect(String((caught.raised as Error).message)).toContain("cleanup"); - expect(caught.completion?.ok).toBe(false); - }, - ); + // Work the root owned did not finish unwinding, so a rendering reported + // beside it would describe a root still running. + expect(caught.raised).not.toBe(undefined); + expect(String((caught.raised as Error).message)).toContain("cleanup"); + expect(caught.completion?.ok).toBe(false); + }, + )); it("E7d: a fatal evaluation outranks the ordinary failure the document went on to have", function* () { const stream = new InMemoryStream(); @@ -1613,69 +1624,81 @@ describe("Tier E7 — which failures stay the run's", () => { expect(run.completion.ok).toBe(false); }); - heldHostCase( - "E7f: an authoritative evaluation failure of no classified kind is still the run's", - function* (stream) { - const caught: { raised?: unknown } = {}; - - // A failed evaluation teardown is neither a durability nor a Files - // failure, so the classified tiers do not reach it — and the document - // fails afterwards for an ordinary reason of its own. Which of the two the - // completion reports is decided by occurrence, and the evaluation's came - // first. - const run = yield* withHeldHost( - { - stream, - root: { path: "host/ranking.md", source: "\n\n" }, - installations: [{ components: [failingMain()] }, selecting([probe(), failingCleanup()])], - }, - function* (open) { - try { - yield* open.execution.evaluateGeneratedXmdRoot({ - id: "turn-1", - source: "\n\n\n", - }); - } catch (error) { - caught.raised = error; - } - }, - ); + it("E7f: an authoritative evaluation failure of no classified kind is still the run's", () => + captureTestJournal( + { + owner: OWNER, + name: "E7f: an authoritative evaluation failure of no classified kind is still the run's", + }, + "host/ranking.md", + function* (stream) { + const caught: { raised?: unknown } = {}; + + // A failed evaluation teardown is neither a durability nor a Files + // failure, so the classified tiers do not reach it — and the document + // fails afterwards for an ordinary reason of its own. Which of the two the + // completion reports is decided by occurrence, and the evaluation's came + // first. + const run = yield* withHeldHost( + { + stream, + root: { path: "host/ranking.md", source: "\n\n" }, + installations: [ + { components: [failingMain()] }, + selecting([probe(), failingCleanup()]), + ], + }, + function* (open) { + try { + yield* open.execution.evaluateGeneratedXmdRoot({ + id: "turn-1", + source: "\n\n\n", + }); + } catch (error) { + caught.raised = error; + } + }, + ); - // The object the caller was handed, not a rebuilt one carrying its words. - expect(caught.raised).toBeInstanceOf(Error); - expect(run.completion.ok).toBe(false); - expect(run.completion.ok ? undefined : run.completion.error).toBe(caught.raised); - }, - "host/ranking.md", - ); + // The object the caller was handed, not a rebuilt one carrying its words. + expect(caught.raised).toBeInstanceOf(Error); + expect(run.completion.ok).toBe(false); + expect(run.completion.ok ? undefined : run.completion.error).toBe(caught.raised); + }, + )); - heldHostCase( - "E7g: the same failure is the run's when the document succeeded — by identity", - function* (stream) { - const caught: { raised?: unknown } = {}; - - // The control for E7f. E7c already states that such a run does not report - // success; what this adds is that the completion is the *same* failure, so - // E7f's row is about ranking rather than about reporting one at all. - const run = yield* withHeldHost( - { stream, installations: [selecting(declared())] }, - function* (open) { - try { - yield* open.execution.evaluateGeneratedXmdRoot({ - id: "turn-1", - source: "\n\n\n", - }); - } catch (error) { - caught.raised = error; - } - }, - ); + it("E7g: the same failure is the run's when the document succeeded — by identity", () => + captureTestJournal( + { + owner: OWNER, + name: "E7g: the same failure is the run's when the document succeeded — by identity", + }, + HELD_ROOT_PATH, + function* (stream) { + const caught: { raised?: unknown } = {}; + + // The control for E7f. E7c already states that such a run does not report + // success; what this adds is that the completion is the *same* failure, so + // E7f's row is about ranking rather than about reporting one at all. + const run = yield* withHeldHost( + { stream, installations: [selecting(declared())] }, + function* (open) { + try { + yield* open.execution.evaluateGeneratedXmdRoot({ + id: "turn-1", + source: "\n\n\n", + }); + } catch (error) { + caught.raised = error; + } + }, + ); - expect(caught.raised).toBeInstanceOf(Error); - expect(run.completion.ok).toBe(false); - expect(run.completion.ok ? undefined : run.completion.error).toBe(caught.raised); - }, - ); + expect(caught.raised).toBeInstanceOf(Error); + expect(run.completion.ok).toBe(false); + expect(run.completion.ok ? undefined : run.completion.error).toBe(caught.raised); + }, + )); }); /** diff --git a/packages/core/tests/support/held-host.ts b/packages/core/tests/support/held-host.ts index f369f086c..76170d65a 100644 --- a/packages/core/tests/support/held-host.ts +++ b/packages/core/tests/support/held-host.ts @@ -24,8 +24,7 @@ import { executeInstalled } from "../../host.ts"; import type { ExecutionInstallation, HostExecution } from "../../host.ts"; import { retainedSource } from "../../src/root-source.ts"; import type { Json } from "../../src/types.ts"; -import { it } from "@executablemd/test-support/bdd"; -import { captureTestJournal, useJournalStream } from "@executablemd/test-support/failure-journal"; +import { useJournalStream } from "@executablemd/test-support/failure-journal"; const NO_PROPS = { type: "object", properties: {}, additionalProperties: false } as const; @@ -36,16 +35,6 @@ const HOLD_COMPONENT = "Held"; export const HELD_ROOT_PATH = "host/held.md"; const HELD_ROOT_SOURCE = `<${HOLD_COMPONENT} />\n`; -export function heldHostCases(owner: string) { - return ( - name: string, - body: (stream: InMemoryStream) => Operation, - source = HELD_ROOT_PATH, - ): void => { - it(name, () => captureTestJournal({ owner, name }, source, body)); - }; -} - /** The document a held execution runs, for a case that needs its own. */ export interface HeldRoot { readonly path: string; diff --git a/packages/git/tests/git-observations-deno.test.ts b/packages/git/tests/git-observations-deno.test.ts index 58d4ede7a..888085186 100644 --- a/packages/git/tests/git-observations-deno.test.ts +++ b/packages/git/tests/git-observations-deno.test.ts @@ -25,7 +25,8 @@ * began. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { expect } from "@executablemd/test-support/expect"; import { scoped, sleep, spawn, until, withResolvers } from "effection"; import type { Operation } from "effection"; @@ -36,16 +37,15 @@ import process from "node:process"; import type { Json } from "@executablemd/durable-streams"; import { capturingProbe, writeFixtureFile } from "./support/observations.ts"; import { - ordinaryDocumentCases, useHostCheckout, useManagedRoot, useOriginlessCheckout, + useOrdinaryDocumentRunner, } from "./support/run-composition.ts"; import { useBareRemote } from "./support/git-remotes.ts"; import { REMOTE } from "./support/run-composition-tier.ts"; import { denoRepositoryHost } from "../src/deno/composition/host.ts"; -const ordinaryCase = ordinaryDocumentCases("packages/git/tests/git-observations-deno.test.ts"); import type { GitBytes, GitInvocation, @@ -70,61 +70,61 @@ function paths(value: Json | undefined): string[] { } describe("GO9 — which checkout answers, through a managed selection", () => { - ordinaryCase( - "reads the ambient checkout, a lexical Repository, a Worktree and a Dir inside one", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const ambient = yield* useHostCheckout(remote.locator); - yield* writeFixtureFile(ambient.root, "only-here.txt", "ambient\n"); - ambient.run("add", "--", "only-here.txt"); - ambient.run("commit", "-m", "ambient only"); - const seen: Json[] = []; + beforeEach(journalBeforeEach("packages/git/tests/git-observations-deno.test.ts")); - yield* runFresh( - [ - '', - "", - "", - "", - ``, - "", - '', - "", - "", - "", - '', - "", - '', - "", - "", - "", - '', - "", - '', - "", - "", - "", - "", - "", - "", - "", - "", - "", - ].join("\n"), - { root, cwd: ambient.root, components: [capturingProbe(seen)] }, - ); + it("reads the ambient checkout, a lexical Repository, a Worktree and a Dir inside one", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const ambient = yield* useHostCheckout(remote.locator); + yield* writeFixtureFile(ambient.root, "only-here.txt", "ambient\n"); + ambient.run("add", "--", "only-here.txt"); + ambient.run("commit", "-m", "ambient only"); + const seen: Json[] = []; - // The ambient checkout holds a path the managed clone never received, which - // is how a read that answered from the wrong one would say so. - expect(paths(seen[0])).toContain("only-here.txt"); - expect(paths(seen[1])).toEqual(["which.txt"]); - expect(object(seen[2]).changes).toEqual([]); - // A `` beneath a Worktree stays inside that checkout, and the answer - // is still relative to its repository root. - expect(paths(seen[3])).toEqual(["which.txt"]); - }, - ); + yield* runFresh( + [ + '', + "", + "", + "", + ``, + "", + '', + "", + "", + "", + '', + "", + '', + "", + "", + "", + '', + "", + '', + "", + "", + "", + "", + "", + "", + "", + "", + "", + ].join("\n"), + { root, cwd: ambient.root, components: [capturingProbe(seen)] }, + ); + + // The ambient checkout holds a path the managed clone never received, which + // is how a read that answered from the wrong one would say so. + expect(paths(seen[0])).toContain("only-here.txt"); + expect(paths(seen[1])).toEqual(["which.txt"]); + expect(object(seen[2]).changes).toEqual([]); + // A `` beneath a Worktree stays inside that checkout, and the answer + // is still relative to its repository root. + expect(paths(seen[3])).toEqual(["which.txt"]); + }); }); /** What a watched `git` child reported about its own lifetime. */ @@ -227,60 +227,60 @@ function watchingHost( } describe("GO9 — what an interrupted native read leaves behind", () => { - ordinaryCase( - "joins the read's own child and removes its scratch before settling", - function* (runFresh) { - const checkout = yield* useOriginlessCheckout(); - const root = yield* useManagedRoot(); - yield* writeFixtureFile(checkout.root, "src/a.ts", "export const a = 1;\n"); - checkout.run("add", "--", "src/a.ts"); - checkout.run("commit", "-m", "a"); - yield* writeFixtureFile(checkout.root, "src/a.ts", "export const a = 11;\n"); + beforeEach(journalBeforeEach("packages/git/tests/git-observations-deno.test.ts")); - const watched = watchChildren(); - const scratch: string[] = []; - const marker = join(checkout.home, "read-ran"); - const script = join(checkout.home, "waiting-fsmonitor"); - yield* waitingScript(script, marker); + it("joins the read's own child and removes its scratch before settling", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const checkout = yield* useOriginlessCheckout(); + const root = yield* useManagedRoot(); + yield* writeFixtureFile(checkout.root, "src/a.ts", "export const a = 1;\n"); + checkout.run("add", "--", "src/a.ts"); + checkout.run("commit", "-m", "a"); + yield* writeFixtureFile(checkout.root, "src/a.ts", "export const a = 11;\n"); - // The comparison refreshes the index before it answers, and a file-system - // monitor is consulted while it does. This one reports that it ran and then - // waits, holding the pipes the parent inherited — which is what makes the - // cut land on a real child rather than between commands. - const intercept = (invocation: GitInvocation): Partial | undefined => - invocation.args.includes("diff") - ? { - args: ["-c", `core.fsmonitor=${script}`, ...invocation.args], - observe: watched.observe, - } - : undefined; + const watched = watchChildren(); + const scratch: string[] = []; + const marker = join(checkout.home, "read-ran"); + const script = join(checkout.home, "waiting-fsmonitor"); + yield* waitingScript(script, marker); - yield* scoped(function* () { - const task = yield* spawn(() => - runFresh( - [ - '', - 'sentinel', - ].join("\n"), - { root, cwd: checkout.root, host: watchingHost(scratch, intercept) }, - ), - ); - // The marker says the descendant really ran and is holding the pipes. - yield* appears(marker); - yield* watched.spawned; - yield* task.halt(); - }); + // The comparison refreshes the index before it answers, and a file-system + // monitor is consulted while it does. This one reports that it ran and then + // waits, holding the pipes the parent inherited — which is what makes the + // cut land on a real child rather than between commands. + const intercept = (invocation: GitInvocation): Partial | undefined => + invocation.args.includes("diff") + ? { + args: ["-c", `core.fsmonitor=${script}`, ...invocation.args], + observe: watched.observe, + } + : undefined; - // The child is gone and both its pipes have ended; the scratch the read - // owned is gone with it; and nothing after the read ran. - expect(watched.observed() > 0).toBe(true); - expect(watched.closed()).toBe(true); - expect(watched.alive()).toBe(false); - expect(scratch.length > 0).toBe(true); - for (const directory of scratch) { - expect(yield* exists(directory)).toBe(false); - } - expect(yield* exists(join(checkout.root, "sentinel.md"))).toBe(false); - }, - ); + yield* scoped(function* () { + const task = yield* spawn(() => + runFresh( + [ + '', + 'sentinel', + ].join("\n"), + { root, cwd: checkout.root, host: watchingHost(scratch, intercept) }, + ), + ); + // The marker says the descendant really ran and is holding the pipes. + yield* appears(marker); + yield* watched.spawned; + yield* task.halt(); + }); + + // The child is gone and both its pipes have ended; the scratch the read + // owned is gone with it; and nothing after the read ran. + expect(watched.observed() > 0).toBe(true); + expect(watched.closed()).toBe(true); + expect(watched.alive()).toBe(false); + expect(scratch.length > 0).toBe(true); + for (const directory of scratch) { + expect(yield* exists(directory)).toBe(false); + } + expect(yield* exists(join(checkout.root, "sentinel.md"))).toBe(false); + }); }); diff --git a/packages/git/tests/git-observations.test.ts b/packages/git/tests/git-observations.test.ts index 802fad39e..ede443923 100644 --- a/packages/git/tests/git-observations.test.ts +++ b/packages/git/tests/git-observations.test.ts @@ -9,7 +9,8 @@ * repository can answer. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { expect } from "@executablemd/test-support/expect"; import type { Json } from "@executablemd/durable-streams"; import { InMemoryStream } from "@executablemd/durable-streams"; @@ -56,7 +57,6 @@ import { writeFixtureFile, } from "./support/observations.ts"; import { - ordinaryDocumentCases, commonDirectoryOf, countingOrdinaryHost, raised, @@ -64,6 +64,7 @@ import { subcommands, useHostCheckout, useManagedRoot, + useOrdinaryDocumentRunner, } from "./support/run-composition.ts"; import { useBareRemote } from "./support/git-remotes.ts"; import { REMOTE } from "./support/run-composition-tier.ts"; @@ -97,7 +98,6 @@ import { denoRepositoryHost } from "../src/deno/composition/host.ts"; import type { GitInvocation, RepositoryHost } from "../src/deno/composition/host.ts"; import { performGitObservation } from "../src/deno/composition/observations.ts"; -const ordinaryCase = ordinaryDocumentCases("packages/git/tests/git-observations.test.ts"); import type { ObservationPlace } from "../src/deno/composition/observations.ts"; /** @@ -390,7 +390,10 @@ function paths(value: Json | undefined): string[] { } describe("GO1 — discovery addresses the chosen repository", () => { - ordinaryCase("lists the tracked index paths and nothing else", function* (runFresh) { + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); + + it("lists the tracked index paths and nothing else", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const fixture = yield* useObservationFixture(); const seen: Json[] = []; @@ -420,57 +423,56 @@ describe("GO1 — discovery addresses the chosen repository", () => { }); describe("GO2 — discovery excludes what the index does not hold", () => { - ordinaryCase( - "excludes untracked files, a nested checkout's contents and a sibling checkout", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - yield* writeFixtureFile(fixture.root, "untracked.md", "not tracked\n"); - const seen: Json[] = []; - - yield* runFresh( - ['', "", "", ""].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); - const listed = paths(seen[0]); - expect(listed).not.toContain("untracked.md"); - // The tracked gitlink is one path. Nothing beneath it, and nothing in the - // untracked checkout beside it, is a path this repository's index holds. - expect(listed).toContain("nested"); - expect(listed.filter((path) => path.startsWith("nested/"))).toEqual([]); - expect(listed.filter((path) => path.startsWith("scratch-repo"))).toEqual([]); - }, - ); + it("excludes untracked files, a nested checkout's contents and a sibling checkout", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + yield* writeFixtureFile(fixture.root, "untracked.md", "not tracked\n"); + const seen: Json[] = []; - ordinaryCase( - "matches prefix literally and answers root-relative paths from a subdirectory", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const seen: Json[] = []; + yield* runFresh( + ['', "", "", ""].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - yield* runFresh( - [ - '', - "", - '', - "", - "", - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + const listed = paths(seen[0]); + expect(listed).not.toContain("untracked.md"); + // The tracked gitlink is one path. Nothing beneath it, and nothing in the + // untracked checkout beside it, is a path this repository's index holds. + expect(listed).toContain("nested"); + expect(listed.filter((path) => path.startsWith("nested/"))).toEqual([]); + expect(listed.filter((path) => path.startsWith("scratch-repo"))).toEqual([]); + }); - // An incomplete final segment is a prefix, and the answer is relative to - // the repository root rather than to the directory the element was in. - expect(paths(seen[0])).toEqual(["src/a.ts", "src/added.ts"]); - }, - ); + it("matches prefix literally and answers root-relative paths from a subdirectory", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const seen: Json[] = []; - ordinaryCase("reads a prefix holding glob characters as those characters", function* (runFresh) { + yield* runFresh( + [ + '', + "", + '', + "", + "", + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); + + // An incomplete final segment is a prefix, and the answer is relative to + // the repository root rather than to the directory the element was in. + expect(paths(seen[0])).toEqual(["src/a.ts", "src/added.ts"]); + }); + + it("reads a prefix holding glob characters as those characters", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const fixture = yield* useObservationFixture(); const seen: Json[] = []; @@ -490,7 +492,10 @@ describe("GO2 — discovery excludes what the index does not hold", () => { }); describe("GO3 — the three comparisons", () => { - ordinaryCase("compares two commits with every status the fixture produces", function* (runFresh) { + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); + + it("compares two commits with every status the fixture produces", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const fixture = yield* useObservationFixture(); const seen: Json[] = []; @@ -523,41 +528,39 @@ describe("GO3 — the three comparisons", () => { expect(result.next).toBe(null); }); - ordinaryCase( - "compares the index with the working tree, and HEAD with the index", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - yield* writeFixtureFile(fixture.root, "src/a.ts", "export const a = 2;\n"); - yield* writeFixtureFile(fixture.root, "staged.ts", "export const staged = true;\n"); - fixture.run("add", "--", "staged.ts"); - yield* writeFixtureFile(fixture.root, "untracked.md", "not tracked\n"); - const seen: Json[] = []; - - yield* runFresh( - [ - '', - "", - "", - "", - '', - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + it("compares the index with the working tree, and HEAD with the index", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + yield* writeFixtureFile(fixture.root, "src/a.ts", "export const a = 2;\n"); + yield* writeFixtureFile(fixture.root, "staged.ts", "export const staged = true;\n"); + fixture.run("add", "--", "staged.ts"); + yield* writeFixtureFile(fixture.root, "untracked.md", "not tracked\n"); + const seen: Json[] = []; - const unstaged = object(seen[0]); - expect(unstaged.comparison).toEqual({ compare: "unstaged" }); - // The untracked file is not a tracked change, so it is not here. - expect(unstaged.changes).toEqual([{ path: "src/a.ts", status: "M", previousPath: null }]); + yield* runFresh( + [ + '', + "", + "", + "", + '', + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - const staged = object(seen[1]); - expect(staged.comparison).toEqual({ compare: "staged", base: fixture.c2 }); - expect(staged.changes).toEqual([{ path: "staged.ts", status: "A", previousPath: null }]); - }, - ); + const unstaged = object(seen[0]); + expect(unstaged.comparison).toEqual({ compare: "unstaged" }); + // The untracked file is not a tracked change, so it is not here. + expect(unstaged.changes).toEqual([{ path: "src/a.ts", status: "M", previousPath: null }]); + + const staged = object(seen[1]); + expect(staged.comparison).toEqual({ compare: "staged", base: fixture.c2 }); + expect(staged.changes).toEqual([{ path: "staged.ts", status: "A", previousPath: null }]); + }); // Driven at the native performer rather than through a document, because no // installed provider can select an unborn repository: a selection's identity @@ -589,118 +592,115 @@ describe("GO3 — the three comparisons", () => { }); describe("GO4 — the patch a comparison produces", () => { - ordinaryCase( - "answers the comparison's own unified text, with Git's binary and mode metadata", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const seen: Json[] = []; - - yield* runFresh( - [ - ``, - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); - const result = object(seen[0]); - const patch = String(result.text); - expect(result.comparison).toEqual({ compare: "commits", base: fixture.c1, head: fixture.c2 }); - // Every shape the fixture produces, as patch text rather than as a summary. - expect(patch).toContain("rename from doc/orig.md"); - expect(patch).toContain("rename to doc/renamed.md"); - expect(patch).toContain("Binary files a/src/bin.dat and b/src/bin.dat differ"); - expect(patch).toContain("old mode 100644"); - expect(patch).toContain("new mode 100755"); - expect(patch).toContain("new file mode 100644"); - expect(patch).toContain("deleted file mode 100644"); - expect(patch).toContain("Subproject commit"); - expect(result.offset).toBe(0); - expect(result.length).toBe([...patch].length); - expect(result.next).toBe(null); - }, - ); + it("answers the comparison's own unified text, with Git's binary and mode metadata", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const seen: Json[] = []; - ordinaryCase( - "narrows to literal files and directories, matching a rename on either side", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const seen: Json[] = []; + yield* runFresh( + [ + ``, + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - yield* runFresh( - [ - ``, - "", - "", - "", - ``, - "", - "", - "", - ``, - "", - "", - "", - ``, - "", - "", - "", - ``, - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + const result = object(seen[0]); + const patch = String(result.text); + expect(result.comparison).toEqual({ compare: "commits", base: fixture.c1, head: fixture.c2 }); + // Every shape the fixture produces, as patch text rather than as a summary. + expect(patch).toContain("rename from doc/orig.md"); + expect(patch).toContain("rename to doc/renamed.md"); + expect(patch).toContain("Binary files a/src/bin.dat and b/src/bin.dat differ"); + expect(patch).toContain("old mode 100644"); + expect(patch).toContain("new mode 100755"); + expect(patch).toContain("new file mode 100644"); + expect(patch).toContain("deleted file mode 100644"); + expect(patch).toContain("Subproject commit"); + expect(result.offset).toBe(0); + expect(result.length).toBe([...patch].length); + expect(result.next).toBe(null); + }); - expect(object(seen[0]).changes).toEqual([ - { path: "src/added.ts", status: "A", previousPath: null }, - { path: "src/bin.dat", status: "M", previousPath: null }, - ]); - // The old path selects the whole rename record, not half of it. - expect(object(seen[1]).changes).toEqual([ - { path: "doc/renamed.md", status: "R", previousPath: "doc/orig.md" }, - ]); - expect(String(object(seen[2]).text)).toContain("rename from doc/orig.md"); - // An empty selector is a thing a document says, and it selects nothing. - expect(object(seen[3]).changes).toEqual([]); - expect(object(seen[4]).text).toBe(""); - expect(object(seen[4]).length).toBe(0); - expect(object(seen[4]).next).toBe(null); - }, - ); + it("narrows to literal files and directories, matching a rename on either side", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const seen: Json[] = []; - ordinaryCase( - "keeps a tracked gitlink's moved pointer without reading inside it", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const seen: Json[] = []; + yield* runFresh( + [ + ``, + "", + "", + "", + ``, + "", + "", + "", + ``, + "", + "", + "", + ``, + "", + "", + "", + ``, + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - yield* runFresh( - [ - ``, - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + expect(object(seen[0]).changes).toEqual([ + { path: "src/added.ts", status: "A", previousPath: null }, + { path: "src/bin.dat", status: "M", previousPath: null }, + ]); + // The old path selects the whole rename record, not half of it. + expect(object(seen[1]).changes).toEqual([ + { path: "doc/renamed.md", status: "R", previousPath: "doc/orig.md" }, + ]); + expect(String(object(seen[2]).text)).toContain("rename from doc/orig.md"); + // An empty selector is a thing a document says, and it selects nothing. + expect(object(seen[3]).changes).toEqual([]); + expect(object(seen[4]).text).toBe(""); + expect(object(seen[4]).length).toBe(0); + expect(object(seen[4]).next).toBe(null); + }); - const patch = String(object(seen[0]).text); - expect(patch).toContain(`-Subproject commit ${fixture.gitlink.before}`); - expect(patch).toContain(`+Subproject commit ${fixture.gitlink.after}`); - // One path and one pointer. Nothing beneath the gitlink is read, so the - // nested repository's own file never appears. - expect(patch).not.toContain("inner.txt"); - }, - ); + it("keeps a tracked gitlink's moved pointer without reading inside it", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const seen: Json[] = []; - ordinaryCase("collapses an unmerged path to one record", function* (runFresh) { + yield* runFresh( + [ + ``, + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); + + const patch = String(object(seen[0]).text); + expect(patch).toContain(`-Subproject commit ${fixture.gitlink.before}`); + expect(patch).toContain(`+Subproject commit ${fixture.gitlink.after}`); + // One path and one pointer. Nothing beneath the gitlink is read, so the + // nested repository's own file never appears. + expect(patch).not.toContain("inner.txt"); + }); + + it("collapses an unmerged path to one record", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const conflicted = yield* useConflictedCheckout(); const seen: Json[] = []; @@ -722,6 +722,7 @@ describe("GO4 — the patch a comparison produces", () => { }); describe("GO5 — pages are bounded, complete, and about one observation", () => { + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); it("defaults to 100 records, follows every page once, and honors a larger limit", function* () { const root = yield* useManagedRoot(); const fixture = yield* useGeneratedFixture(1002); @@ -770,7 +771,8 @@ describe("GO5 — pages are bounded, complete, and about one observation", () => expect(object(whole[0]).next).toBe(null); }); - ordinaryCase("pages changes at a limit of one and at the default", function* (runFresh) { + it("pages changes at a limit of one and at the default", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const fixture = yield* useGeneratedFixture(1002); for (const path of fixture.paths) { @@ -911,7 +913,10 @@ describe("GO5 — pages are bounded, complete, and about one observation", () => }); describe("GO6 — a changed observation cannot be paged over", () => { - ordinaryCase("refuses a file listing whose paths changed between pages", function* (runFresh) { + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); + + it("refuses a file listing whose paths changed between pages", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const fixture = yield* useGeneratedFixture(150); @@ -948,123 +953,117 @@ describe("GO6 — a changed observation cannot be paged over", () => { // editing a path already reported as modified leaves the normalized listing // identical, and a read that restarted for that would refuse a page it could // honestly answer. So this adds a record the first page never described. - ordinaryCase( - "refuses a changes listing whose records changed between pages", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useGeneratedFixture(150); - for (const path of fixture.paths.slice(0, 120)) { - yield* writeFixtureFile(fixture.root, path, "changed\n"); - } - - const raised_ = yield* raised( - runFresh( - [ - '', - "", - "", - "", - '', - "", - ].join("\n"), - { - root, - cwd: fixture.root, - components: [ - actionProbe("Mutates", function* () { - yield* writeFixtureFile(fixture.root, fixture.paths[149] ?? "", "changed too\n"); - }), - ], - }, - ), - ); - - expect(reasonOf(raised_)).toBe("stale-continuation"); - expect(messageOf(raised_)).toBe( - "The Git observation changed. Restart without cursor.", - ); - }, - ); - - ordinaryCase( - "refuses a patch whose text changed between windows, and names offset zero", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useGeneratedFixture(150); - for (const path of fixture.paths) { - yield* writeFixtureFile(fixture.root, path, "changed\n"); - } + it("refuses a changes listing whose records changed between pages", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useGeneratedFixture(150); + for (const path of fixture.paths.slice(0, 120)) { + yield* writeFixtureFile(fixture.root, path, "changed\n"); + } - const raised_ = yield* raised( - runFresh( - [ - '', - "", - "", - "", - '', - "", - ].join("\n"), - { - root, - cwd: fixture.root, - components: [ - actionProbe("Mutates", function* () { - yield* writeFixtureFile(fixture.root, fixture.paths[0] ?? "", "changed again\n"); - }), - ], - }, - ), - ); + const raised_ = yield* raised( + runFresh( + [ + '', + "", + "", + "", + '', + "", + ].join("\n"), + { + root, + cwd: fixture.root, + components: [ + actionProbe("Mutates", function* () { + yield* writeFixtureFile(fixture.root, fixture.paths[149] ?? "", "changed too\n"); + }), + ], + }, + ), + ); - expect(reasonOf(raised_)).toBe("stale-continuation"); - expect(messageOf(raised_)).toBe( - "The Git observation changed. Restart without cursor and offset={0}.", - ); - }, - ); + expect(reasonOf(raised_)).toBe("stale-continuation"); + expect(messageOf(raised_)).toBe( + "The Git observation changed. Restart without cursor.", + ); + }); - ordinaryCase( - "follows a continuation unchanged when the repository changed elsewhere", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useGeneratedFixture(150); - const seen: Json[] = []; + it("refuses a patch whose text changed between windows, and names offset zero", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useGeneratedFixture(150); + for (const path of fixture.paths) { + yield* writeFixtureFile(fixture.root, path, "changed\n"); + } - yield* runFresh( + const raised_ = yield* raised( + runFresh( [ - '', + '', "", "", "", - '', - "", - "", + '', "", ].join("\n"), { root, cwd: fixture.root, components: [ - capturingProbe(seen), actionProbe("Mutates", function* () { - // Outside the selected observation: an untracked file changes no - // tracked path, so this page is still a page of the same answer. - yield* writeFixtureFile(fixture.root, "untracked-elsewhere.txt", "elsewhere\n"); + yield* writeFixtureFile(fixture.root, fixture.paths[0] ?? "", "changed again\n"); }), ], }, - ); + ), + ); - expect(paths(seen[0])).toEqual([ - "gen/file-00005.txt", - "gen/file-00006.txt", - "gen/file-00007.txt", - "gen/file-00008.txt", - "gen/file-00009.txt", - ]); - }, - ); + expect(reasonOf(raised_)).toBe("stale-continuation"); + expect(messageOf(raised_)).toBe( + "The Git observation changed. Restart without cursor and offset={0}.", + ); + }); + + it("follows a continuation unchanged when the repository changed elsewhere", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useGeneratedFixture(150); + const seen: Json[] = []; + + yield* runFresh( + [ + '', + "", + "", + "", + '', + "", + "", + "", + ].join("\n"), + { + root, + cwd: fixture.root, + components: [ + capturingProbe(seen), + actionProbe("Mutates", function* () { + // Outside the selected observation: an untracked file changes no + // tracked path, so this page is still a page of the same answer. + yield* writeFixtureFile(fixture.root, "untracked-elsewhere.txt", "elsewhere\n"); + }), + ], + }, + ); + + expect(paths(seen[0])).toEqual([ + "gen/file-00005.txt", + "gen/file-00006.txt", + "gen/file-00007.txt", + "gen/file-00008.txt", + "gen/file-00009.txt", + ]); + }); it("refuses a cursor that is malformed, for another read, or for another checkout", function* () { const root = yield* useManagedRoot(); @@ -1117,6 +1116,7 @@ describe("GO6 — a changed observation cannot be paged over", () => { }); describe("GO7 — a file is read as one commit holds it", () => { + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); it("windows the committed text, stays on its commit, and reassembles it", function* () { const root = yield* useManagedRoot(); const fixture = yield* useFileFixture(); @@ -1186,58 +1186,56 @@ describe("GO7 — a file is read as one commit holds it", () => { expect(windows.join("")).toBe(LONG_BLOB); }); - ordinaryCase( - "answers a middle window, an empty window past the end, and an empty file", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useFileFixture(); - const seen: Json[] = []; - const total = [...LONG_BLOB].length; + it("answers a middle window, an empty window past the end, and an empty file", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useFileFixture(); + const seen: Json[] = []; + const total = [...LONG_BLOB].length; - yield* runFresh( - [ - ``, - "", - "", - "", - ``, - "", - "", - "", - ``, - "", - "", - "", - ``, - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + yield* runFresh( + [ + ``, + "", + "", + "", + ``, + "", + "", + "", + ``, + "", + "", + "", + ``, + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - expect(object(seen[0]).text).toBe("aaaaa"); - expect(object(seen[0]).next).toEqual({ - commit: fixture.f1, - path: "big.txt", - offset: 15, - limit: 5, - }); - - for (const past of [object(seen[1]), object(seen[2])]) { - expect(past.text).toBe(""); - expect(past.length).toBe(total); - expect(past.next).toBe(null); - } - expect(object(seen[1]).offset).toBe(total); - expect(object(seen[2]).offset).toBe(total + 500); + expect(object(seen[0]).text).toBe("aaaaa"); + expect(object(seen[0]).next).toEqual({ + commit: fixture.f1, + path: "big.txt", + offset: 15, + limit: 5, + }); - const empty = object(seen[3]); - expect(empty.text).toBe(""); - expect(empty.length).toBe(0); - expect(empty.next).toBe(null); - }, - ); + for (const past of [object(seen[1]), object(seen[2])]) { + expect(past.text).toBe(""); + expect(past.length).toBe(total); + expect(past.next).toBe(null); + } + expect(object(seen[1]).offset).toBe(total); + expect(object(seen[2]).offset).toBe(total + 500); + + const empty = object(seen[3]); + expect(empty.text).toBe(""); + expect(empty.length).toBe(0); + expect(empty.next).toBe(null); + }); it("refuses an unusable revision, an absent path, a tree, a gitlink and a nontext blob", function* () { const root = yield* useManagedRoot(); @@ -1301,7 +1299,8 @@ describe("GO7 — a file is read as one commit holds it", () => { } }); - ordinaryCase("still reads a directory's own child by its exact path", function* (runFresh) { + it("still reads a directory's own child by its exact path", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const fixture = yield* useFileFixture(); const seen: Json[] = []; @@ -1322,31 +1321,30 @@ describe("GO7 — a file is read as one commit holds it", () => { expect(object(seen[0]).text).toBe("inside\n"); }); - ordinaryCase( - "answers a symbolic link's own stored text rather than what it points at", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useFileFixture(); - const seen: Json[] = []; + it("answers a symbolic link's own stored text rather than what it points at", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useFileFixture(); + const seen: Json[] = []; - yield* runFresh( - [ - ``, - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + yield* runFresh( + [ + ``, + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); - // The link's target, as the blob holds it. Nothing followed it. - expect(object(seen[0]).text).toBe("big.txt"); - expect(object(seen[0]).length).toBe(7); - }, - ); + // The link's target, as the blob holds it. Nothing followed it. + expect(object(seen[0]).text).toBe("big.txt"); + expect(object(seen[0]).length).toBe(7); + }); }); describe("GO8 — a read runs no repository-defined program and changes nothing", () => { + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); it("leaves every sentinel unfired and the repository byte-identical", function* () { const root = yield* useManagedRoot(); const fixture = yield* useObservationFixture(); @@ -1397,48 +1395,46 @@ describe("GO8 — a read runs no repository-defined program and changes nothing" } }); - ordinaryCase( - "reads a linked checkout's own split index and leaves all three untouched", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const linked = yield* useLinkedWorktree(fixture); - fixture.run("update-index", "--split-index"); - linked.run("update-index", "--split-index"); - yield* writeFixtureFile(linked.root, "src/a.ts", "export const a = 9;\n"); + it("reads a linked checkout's own split index and leaves all three untouched", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const linked = yield* useLinkedWorktree(fixture); + fixture.run("update-index", "--split-index"); + linked.run("update-index", "--split-index"); + yield* writeFixtureFile(linked.root, "src/a.ts", "export const a = 9;\n"); - const indexes = yield* indexFingerprints(fixture, linked); - const seen: Json[] = []; + const indexes = yield* indexFingerprints(fixture, linked); + const seen: Json[] = []; - yield* runFresh( - [ - '', - "", - "", - "", - '', - "", - "", - "", - '', - "", - "", - "", - ].join("\n"), - { root, cwd: linked.root, components: [capturingProbe(seen)] }, - ); + yield* runFresh( + [ + '', + "", + "", + "", + '', + "", + "", + "", + '', + "", + "", + "", + ].join("\n"), + { root, cwd: linked.root, components: [capturingProbe(seen)] }, + ); - // The linked checkout's own index answered: its working tree holds the - // edit, and the primary checkout's does not. - expect(paths(seen[0])).toContain("src/a.ts"); - expect(object(seen[1]).changes).toEqual([ - { path: "src/a.ts", status: "M", previousPath: null }, - ]); - expect(String(object(seen[2]).text)).toContain("+export const a = 9;"); + // The linked checkout's own index answered: its working tree holds the + // edit, and the primary checkout's does not. + expect(paths(seen[0])).toContain("src/a.ts"); + expect(object(seen[1]).changes).toEqual([ + { path: "src/a.ts", status: "M", previousPath: null }, + ]); + expect(String(object(seen[2]).text)).toContain("+export const a = 9;"); - expect(yield* indexFingerprints(fixture, linked)).toEqual(indexes); - }, - ); + expect(yield* indexFingerprints(fixture, linked)).toEqual(indexes); + }); }); describe("GO9 — which checkout answers, and what cannot choose one", () => { @@ -1521,42 +1517,42 @@ describe("GO9 — which checkout answers, and what cannot choose one", () => { }); describe("GO10 — the comparison sees an edit the index's own times would hide", () => { - ordinaryCase( - "reports a modification whose size and recorded time match the index entry", - function* (runFresh) { - const root = yield* useManagedRoot(); - const fixture = yield* useObservationFixture(); - const indexPath = join(fixture.root, ".git", "index"); - const indexTimes = yield* lstat(indexPath); - // The same length, and the index's own modification time. This is exactly - // the state Git calls racily clean: the cached stat data matches, so the - // only way to see the change is to compare content — and the only reason - // Git does is that the entry is no older than the index holding it. A - // private copy stamped with the current time destroys that relationship. - yield* writeFixtureFile(fixture.root, "src/a.ts", "export const a = 7;\n"); - yield* until(utimes(join(fixture.root, "src", "a.ts"), indexTimes.mtime, indexTimes.mtime)); - const seen: Json[] = []; - - yield* runFresh( - [ - '', - "", - "", - "", - '', - "", - "", - "", - ].join("\n"), - { root, cwd: fixture.root, components: [capturingProbe(seen)] }, - ); + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); - expect(object(seen[0]).changes).toEqual([ - { path: "src/a.ts", status: "M", previousPath: null }, - ]); - expect(String(object(seen[1]).text)).toContain("+export const a = 7;"); - }, - ); + it("reports a modification whose size and recorded time match the index entry", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const fixture = yield* useObservationFixture(); + const indexPath = join(fixture.root, ".git", "index"); + const indexTimes = yield* lstat(indexPath); + // The same length, and the index's own modification time. This is exactly + // the state Git calls racily clean: the cached stat data matches, so the + // only way to see the change is to compare content — and the only reason + // Git does is that the entry is no older than the index holding it. A + // private copy stamped with the current time destroys that relationship. + yield* writeFixtureFile(fixture.root, "src/a.ts", "export const a = 7;\n"); + yield* until(utimes(join(fixture.root, "src", "a.ts"), indexTimes.mtime, indexTimes.mtime)); + const seen: Json[] = []; + + yield* runFresh( + [ + '', + "", + "", + "", + '', + "", + "", + "", + ].join("\n"), + { root, cwd: fixture.root, components: [capturingProbe(seen)] }, + ); + + expect(object(seen[0]).changes).toEqual([ + { path: "src/a.ts", status: "M", previousPath: null }, + ]); + expect(String(object(seen[1]).text)).toContain("+export const a = 7;"); + }); }); describe("GO11 — the authored contract, before a provider is reached", () => { diff --git a/packages/git/tests/run-composition-ambient.test.ts b/packages/git/tests/run-composition-ambient.test.ts index e14f4b930..f6ffbbeae 100644 --- a/packages/git/tests/run-composition-ambient.test.ts +++ b/packages/git/tests/run-composition-ambient.test.ts @@ -14,7 +14,8 @@ import { Permissions } from "@executablemd/runtime"; * `~/.xmd/repositories`. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { join } from "node:path"; import { symlink } from "node:fs/promises"; import { API, cwd } from "@executablemd/runtime"; @@ -33,7 +34,6 @@ import { UnresolvedGitIdentityError } from "../src/deno/run-composition/errors.t import { useBareRemote } from "./support/git-remotes.ts"; import { gitHubSource } from "../src/deno/composition/github.ts"; import { - ordinaryDocumentCases, causedBy, commonDirectoryOf, countingOrdinaryHost, @@ -42,6 +42,7 @@ import { subcommands, raised, runOrdinaryDocument, + useOrdinaryDocumentRunner, recordingAccess, useHostCheckout, useManagedRoot, @@ -55,32 +56,31 @@ import { reviewRoutes, } from "./support/run-composition-tier.ts"; -const ordinaryCase = ordinaryDocumentCases("packages/git/tests/run-composition-ambient.test.ts"); +const OWNER = "packages/git/tests/run-composition-ambient.test.ts"; describe("ORC3 — the ambient primary checkout", () => { - ordinaryCase( - "switches, stages and commits in the repository the command was run in", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + beforeEach(journalBeforeEach(OWNER)); + it("switches, stages and commits in the repository the command was run in", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - yield* runFresh( - [ - ``, - `ordinary`, - ``, - ``, - ].join("\n"), - { root, cwd: checkout.root }, - ); + yield* runFresh( + [ + ``, + `ordinary`, + ``, + ``, + ].join("\n"), + { root, cwd: checkout.root }, + ); - // The person's own checkout moved, and it is what a later `git` sees. - expect(checkout.run("rev-parse", "--abbrev-ref", "HEAD")).toBe("feature"); - expect(checkout.run("log", "-1", "--pretty=%s")).toBe("Write notes"); - expect(checkout.run("show", "--pretty=", "--name-only", "HEAD")).toContain("notes.md"); - }, - ); + // The person's own checkout moved, and it is what a later `git` sees. + expect(checkout.run("rev-parse", "--abbrev-ref", "HEAD")).toBe("feature"); + expect(checkout.run("log", "-1", "--pretty=%s")).toBe("Write notes"); + expect(checkout.run("show", "--pretty=", "--name-only", "HEAD")).toContain("notes.md"); + }); it("refuses every root element that needs a repository outside a Git checkout", function* () { const root = yield* useManagedRoot(); @@ -119,38 +119,38 @@ describe("ORC3 — the ambient primary checkout", () => { }); describe("ORC4 — the ambient linked worktree", () => { - ordinaryCase( - "follows the common directory for identity and the worktree root for work", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const primary = yield* useHostCheckout(remote.locator); - // A linked worktree made by hand, exactly as a person would. - const linked = `${primary.root}-linked`; - primary.run("worktree", "add", "-b", "sidecar", linked); - - const before = primary.run("rev-parse", "HEAD"); - - yield* scoped(function* () { - yield* runFresh( - [ - `here`, - ``, - ``, - ].join("\n"), - // The command is run *in the linked worktree*. - { root, cwd: linked }, - ); - }); - - // The worktree advanced; the primary checkout did not. - expect(primary.run("rev-parse", "HEAD")).toBe(before); - expect(primary.run("log", "-1", "--pretty=%s", "sidecar")).toBe("In the worktree"); - }, - ); + beforeEach(journalBeforeEach(OWNER)); + it("follows the common directory for identity and the worktree root for work", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const primary = yield* useHostCheckout(remote.locator); + // A linked worktree made by hand, exactly as a person would. + const linked = `${primary.root}-linked`; + primary.run("worktree", "add", "-b", "sidecar", linked); + + const before = primary.run("rev-parse", "HEAD"); + + yield* scoped(function* () { + yield* runFresh( + [ + `here`, + ``, + ``, + ].join("\n"), + // The command is run *in the linked worktree*. + { root, cwd: linked }, + ); + }); + + // The worktree advanced; the primary checkout did not. + expect(primary.run("rev-parse", "HEAD")).toBe(before); + expect(primary.run("log", "-1", "--pretty=%s", "sidecar")).toBe("In the worktree"); + }); }); describe("ORC5 — origin is not local admission", () => { + beforeEach(journalBeforeEach(OWNER)); it("does local work with no origin, and refuses to publish before reaching anything", function* () { yield* Permissions.around( { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, @@ -205,28 +205,26 @@ describe("ORC5 — origin is not local admission", () => { } }); - ordinaryCase( - "opens a session and transports when there is an origin, so the counters can fail", - function* (runFresh) { - // The same counters, on a repository that *does* have an origin. Without - // this, every assertion above would pass on a counter that can never be - // incremented — which is the one way "nothing was reached" lies. - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const counting = countingOrdinaryHost(); + it("opens a session and transports when there is an origin, so the counters can fail", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + // The same counters, on a repository that *does* have an origin. Without + // this, every assertion above would pass on a counter that can never be + // incremented — which is the one way "nothing was reached" lies. + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const counting = countingOrdinaryHost(); - yield* runFresh([``, ``].join("\n"), { - root, - cwd: checkout.root, - host: counting.host, - }); + yield* runFresh([``, ``].join("\n"), { + root, + cwd: checkout.root, + host: counting.host, + }); - expect(counting.counters.sessions).toEqual([remote.locator]); - expect(subcommands(counting.counters)).toContain("ls-remote"); - expect(subcommands(counting.counters)).toContain("push"); - }, - ); + expect(counting.counters.sessions).toEqual([remote.locator]); + expect(subcommands(counting.counters)).toContain("ls-remote"); + expect(subcommands(counting.counters)).toContain("push"); + }); it("reaches a Git host when one is configured, so those counters can fail too", function* () { const remote = yield* useBareRemote(REMOTE); @@ -253,49 +251,49 @@ describe("ORC5 — origin is not local admission", () => { }); describe("ORC6 — lexical working directories", () => { - ordinaryCase( - "restores the enclosing directory after a Worktree body and a Dir body", - function* (runFresh) { - yield* Permissions.around( - { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, - { at: "min" }, - ); - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + beforeEach(journalBeforeEach(OWNER)); + it("restores the enclosing directory after a Worktree body and a Dir body", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + yield* Permissions.around( + { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, + { at: "min" }, + ); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - // A relative `` path resolves against the contextual working - // directory, so where each one lands is where the document was standing. - const common = commonDirectoryOf(checkout); - yield* runFresh( - [ - `outer`, - ``, - ``, - `within`, - "", - "", - `bound`, - "", - `after`, - ].join("\n"), - { root, cwd: checkout.root }, - ); + // A relative `` path resolves against the contextual working + // directory, so where each one lands is where the document was standing. + const common = commonDirectoryOf(checkout); + yield* runFresh( + [ + `outer`, + ``, + ``, + `within`, + "", + "", + `bound`, + "", + `after`, + ].join("\n"), + { root, cwd: checkout.root }, + ); - const lexical = worktreeSlotOf(root, common, "lexical"); - const bound = worktreeSlotOf(root, common, "inner"); - expect(yield* exists(`${checkout.root}/outer.md`)).toBe(true); - // Each body observed its own checkout. - expect(yield* exists(`${lexical.checkout}/within.md`)).toBe(true); - expect(yield* exists(`${bound.checkout}/bound.md`)).toBe(true); - // Restored: the sibling after both is back in the enclosing directory. - expect(yield* exists(`${checkout.root}/after.md`)).toBe(true); - expect(yield* exists(`${lexical.checkout}/after.md`)).toBe(false); - expect(yield* exists(`${bound.checkout}/after.md`)).toBe(false); - }, - ); + const lexical = worktreeSlotOf(root, common, "lexical"); + const bound = worktreeSlotOf(root, common, "inner"); + expect(yield* exists(`${checkout.root}/outer.md`)).toBe(true); + // Each body observed its own checkout. + expect(yield* exists(`${lexical.checkout}/within.md`)).toBe(true); + expect(yield* exists(`${bound.checkout}/bound.md`)).toBe(true); + // Restored: the sibling after both is back in the enclosing directory. + expect(yield* exists(`${checkout.root}/after.md`)).toBe(true); + expect(yield* exists(`${lexical.checkout}/after.md`)).toBe(false); + expect(yield* exists(`${bound.checkout}/after.md`)).toBe(false); + }); - ordinaryCase("restores the enclosing directory when the body fails", function* (runFresh) { + it("restores the enclosing directory when the body fails", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const remote = yield* useBareRemote(REMOTE); const root = yield* useManagedRoot(); const checkout = yield* useHostCheckout(remote.locator); @@ -354,31 +352,30 @@ describe("ORC6 — lexical working directories", () => { }); describe("ORC13 — live local Git", () => { - ordinaryCase( - "makes real, non-transactional changes and claims no rollback on failure", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + beforeEach(journalBeforeEach(OWNER)); + it("makes real, non-transactional changes and claims no rollback on failure", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - const failure = yield* raised( - runFresh( - [ - ``, - `staged before the failure`, - ``, - ``, - ].join("\n"), - { root, cwd: checkout.root }, - ), - ); - expect(failure).toBeInstanceOf(Error); + const failure = yield* raised( + runFresh( + [ + ``, + `staged before the failure`, + ``, + ``, + ].join("\n"), + { root, cwd: checkout.root }, + ), + ); + expect(failure).toBeInstanceOf(Error); - // The switch and the first Add really happened, and nothing took them back. - expect(checkout.run("rev-parse", "--abbrev-ref", "HEAD")).toBe("partway"); - expect(checkout.run("diff", "--cached", "--name-only")).toContain("staged.md"); - }, - ); + // The switch and the first Add really happened, and nothing took them back. + expect(checkout.run("rev-parse", "--abbrev-ref", "HEAD")).toBe("partway"); + expect(checkout.run("diff", "--cached", "--name-only")).toContain("staged.md"); + }); it("keeps what a cancelled document had already done, and claims no rollback", function* () { const remote = yield* useBareRemote(REMOTE); @@ -403,7 +400,8 @@ describe("ORC13 — live local Git", () => { expect(checkout.run("log", "-1", "--pretty=%s")).not.toBe("never reached"); }); - ordinaryCase("commits as the invoking user, not as the workflow identity", function* (runFresh) { + it("commits as the invoking user, not as the workflow identity", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const remote = yield* useBareRemote(REMOTE); const root = yield* useManagedRoot(); const checkout = yield* useHostCheckout(remote.locator); @@ -427,94 +425,88 @@ describe("ORC13 — live local Git", () => { expect(checkout.run("log", "-1", "--pretty=%an")).not.toBe("Executable.md workflow"); }); - ordinaryCase( - "takes author and committer separately when the host resolves them apart", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - - yield* runFresh( - [ - `pair`, - ``, - ``, - ].join("\n"), - { - root, - cwd: checkout.root, - identity: statedIdentity( - "Ada Lovelace 1 +0000", - "Grace Hopper 1 +0000", - ), - }, - ); - - expect(checkout.run("log", "-1", "--pretty=%an|%ae|%cn|%ce")).toBe( - "Ada Lovelace|ada@example.test|Grace Hopper|grace@example.test", - ); - }, - ); - - ordinaryCase( - "refuses to commit when the host cannot say who the commit is by", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const before = checkout.run("rev-parse", "HEAD"); + it("takes author and committer separately when the host resolves them apart", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - const failure = yield* raised( - runFresh( - [ - `orphan`, - ``, - ``, - ].join("\n"), - { root, cwd: checkout.root, identity: statedIdentity(undefined) }, + yield* runFresh( + [ + `pair`, + ``, + ``, + ].join("\n"), + { + root, + cwd: checkout.root, + identity: statedIdentity( + "Ada Lovelace 1 +0000", + "Grace Hopper 1 +0000", ), - ); + }, + ); - expect(failure).toBeInstanceOf(UnresolvedGitIdentityError); - expect(String(failure)).toContain("git config --global user.name"); - // Nothing was committed, and no identity was substituted. - expect(checkout.run("rev-parse", "HEAD")).toBe(before); - // The staging that came before it still happened: this refuses the commit, - // not the document that led to it. - expect(checkout.run("diff", "--cached", "--name-only")).toContain("orphan.md"); - }, - ); + expect(checkout.run("log", "-1", "--pretty=%an|%ae|%cn|%ce")).toBe( + "Ada Lovelace|ada@example.test|Grace Hopper|grace@example.test", + ); + }); - ordinaryCase( - "leaves every other component usable when no identity resolves", - function* (runFresh) { - yield* Permissions.around( - { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, - { at: "min" }, - ); - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + it("refuses to commit when the host cannot say who the commit is by", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const before = checkout.run("rev-parse", "HEAD"); - // Repository, Worktree, Dir, Switch and Add all work: none of them writes a - // commit object, so none of them needs to know who anybody is. - const rendered = yield* runFresh( + const failure = yield* raised( + runFresh( [ - ``, - ``, - "", - ``, - `fine`, - ``, - "", - "", - "ran", + `orphan`, + ``, + ``, ].join("\n"), { root, cwd: checkout.root, identity: statedIdentity(undefined) }, - ); - expect(String(rendered)).toContain("ran"); - }, - ); + ), + ); + + expect(failure).toBeInstanceOf(UnresolvedGitIdentityError); + expect(String(failure)).toContain("git config --global user.name"); + // Nothing was committed, and no identity was substituted. + expect(checkout.run("rev-parse", "HEAD")).toBe(before); + // The staging that came before it still happened: this refuses the commit, + // not the document that led to it. + expect(checkout.run("diff", "--cached", "--name-only")).toContain("orphan.md"); + }); + + it("leaves every other component usable when no identity resolves", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + yield* Permissions.around( + { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, + { at: "min" }, + ); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + + // Repository, Worktree, Dir, Switch and Add all work: none of them writes a + // commit object, so none of them needs to know who anybody is. + const rendered = yield* runFresh( + [ + ``, + ``, + "", + ``, + `fine`, + ``, + "", + "", + "ran", + ].join("\n"), + { root, cwd: checkout.root, identity: statedIdentity(undefined) }, + ); + expect(String(rendered)).toContain("ran"); + }); it("keeps hooks, monitors, signing and repository helpers disabled", function* () { const remote = yield* useBareRemote(REMOTE); @@ -597,81 +589,81 @@ function selectionProbe(seen: (RepositorySelection | undefined)[]): ComponentReg } describe("ORC6 — Dir makes the directory it names", () => { + beforeEach(journalBeforeEach(OWNER)); // ORC6: the ordering claim and the two path rules, in one document. - ordinaryCase( - "ORC6: creates missing parents, keeps an absolute path, and finishes before content", - function* (runFresh) { - yield* Permissions.around( - { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, - { at: "min" }, - ); - const root = yield* useManagedRoot(); - const checkout = (yield* useOriginlessCheckout()).root; - const elsewhere = yield* useTempDirectory("xmd-orc6-absolute-"); - - // The ordering probe. A nested `` proves nothing about ordering, - // because a write creates its own parents recursively — it would land - // whether or not the ensure had finished. This runs *first* inside the - // region and records what it finds: whether the directory is already there, - // and what the contextual working directory is at that moment. - const observed: { exists: boolean; cwd: string }[] = []; - const probe: ComponentRegistration = { - name: "Observes", - origin: "test://observes", - props: { type: "object", properties: {}, additionalProperties: false }, - *fn(): Operation { - const here = yield* cwd(); - observed.push({ exists: yield* exists(here), cwd: here }); - return ""; - }, - }; + it("ORC6: creates missing parents, keeps an absolute path, and finishes before content", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + yield* Permissions.around( + { files: ([operation], next) => (operation === "absolute" ? true : next(operation)) }, + { at: "min" }, + ); + const root = yield* useManagedRoot(); + const checkout = (yield* useOriginlessCheckout()).root; + const elsewhere = yield* useTempDirectory("xmd-orc6-absolute-"); + + // The ordering probe. A nested `` proves nothing about ordering, + // because a write creates its own parents recursively — it would land + // whether or not the ensure had finished. This runs *first* inside the + // region and records what it finds: whether the directory is already there, + // and what the contextual working directory is at that moment. + const observed: { exists: boolean; cwd: string }[] = []; + const probe: ComponentRegistration = { + name: "Observes", + origin: "test://observes", + props: { type: "object", properties: {}, additionalProperties: false }, + *fn(): Operation { + const here = yield* cwd(); + observed.push({ exists: yield* exists(here), cwd: here }); + return ""; + }, + }; - yield* runFresh( - [ - '', - "", - "", - "", - 'landed', - "", - "", - "", - ``, - "", - "", - "", - 'also landed', - "", - "", - "", - ].join("\n"), - { root, cwd: checkout, components: [probe] }, - ); + yield* runFresh( + [ + '', + "", + "", + "", + 'landed', + "", + "", + "", + ``, + "", + "", + "", + 'also landed', + "", + "", + "", + ].join("\n"), + { root, cwd: checkout, components: [probe] }, + ); - // Before any other content ran, the directory already existed and was the - // contextual working directory. That is the ordering claim, observed rather - // than inferred. - expect(observed).toHaveLength(2); - expect(observed[0]).toEqual({ exists: true, cwd: join(checkout, "made", "deep") }); - expect(observed[1]).toEqual({ - exists: true, - cwd: join(elsewhere, "written", "here"), - }); - - // Every missing parent, and the file that landed after them. - expect(yield* readTextFile(join(checkout, "made", "deep", "inside.md"))).toBe("landed"); - // The absolute target names exactly that place — not a rebase beneath cwd. - expect(yield* readTextFile(join(elsewhere, "written", "here", "outside.md"))).toBe( - "also landed", - ); - expect(yield* exists(join(checkout, elsewhere.replace(/^\//, "")))).toBe(false); - }, - ); + // Before any other content ran, the directory already existed and was the + // contextual working directory. That is the ordering claim, observed rather + // than inferred. + expect(observed).toHaveLength(2); + expect(observed[0]).toEqual({ exists: true, cwd: join(checkout, "made", "deep") }); + expect(observed[1]).toEqual({ + exists: true, + cwd: join(elsewhere, "written", "here"), + }); + + // Every missing parent, and the file that landed after them. + expect(yield* readTextFile(join(checkout, "made", "deep", "inside.md"))).toBe("landed"); + // The absolute target names exactly that place — not a rebase beneath cwd. + expect(yield* readTextFile(join(elsewhere, "written", "here", "outside.md"))).toBe( + "also landed", + ); + expect(yield* exists(join(checkout, elsewhere.replace(/^\//, "")))).toBe(false); + }); // ORC6a: an existing directory is adopted. Asserted on the bytes and on the // entry list, because a provider that removed and recreated it would pass a // test that only checked the directory exists. - ordinaryCase("ORC6a: an existing directory keeps its contents", function* (runFresh) { + it("ORC6a: an existing directory keeps its contents", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const checkout = (yield* useOriginlessCheckout()).root; yield* ensureDir(join(checkout, "kept", "sub")); @@ -821,38 +813,36 @@ describe("ORC6 — Dir makes the directory it names", () => { // ORC6d: the operation is about a directory. The Repository selection in // scope, and every member of its identity, is the same inside the region and // after it. - ordinaryCase( - "ORC6d: Repository selection and identity are unchanged by Dir", - function* (runFresh) { - const root = yield* useManagedRoot(); - const remote = yield* useBareRemote(REMOTE); - const checkout = (yield* useHostCheckout(remote.locator)).root; - const seen: (RepositorySelection | undefined)[] = []; + it("ORC6d: Repository selection and identity are unchanged by Dir", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const root = yield* useManagedRoot(); + const remote = yield* useBareRemote(REMOTE); + const checkout = (yield* useHostCheckout(remote.locator)).root; + const seen: (RepositorySelection | undefined)[] = []; - yield* runFresh( - [ - '', - '', - "", - '', - "", - "", - "", - '', - "", - ].join("\n"), - { root, cwd: checkout, components: [selectionProbe(seen)] }, - ); + yield* runFresh( + [ + '', + '', + "", + '', + "", + "", + "", + '', + "", + ].join("\n"), + { root, cwd: checkout, components: [selectionProbe(seen)] }, + ); - expect(seen).toHaveLength(3); - // The whole selection, member by member: identifier, name, checkout path - // and every member of the identity. A Dir that had re-selected anything - // would differ in one of them. - expect(seen[0]).toBeDefined(); - expect(seen[1]).toEqual(seen[0]); - expect(seen[2]).toEqual(seen[0]); - }, - ); + expect(seen).toHaveLength(3); + // The whole selection, member by member: identifier, name, checkout path + // and every member of the identity. A Dir that had re-selected anything + // would differ in one of them. + expect(seen[0]).toBeDefined(); + expect(seen[1]).toEqual(seen[0]); + expect(seen[2]).toEqual(seen[0]); + }); // ORC6e: creation is persistent. A directory made inside a region whose // content then fails is still there, and so is one made in a run that was diff --git a/packages/git/tests/run-composition-managed.test.ts b/packages/git/tests/run-composition-managed.test.ts index ff03522da..50eaf005e 100644 --- a/packages/git/tests/run-composition-managed.test.ts +++ b/packages/git/tests/run-composition-managed.test.ts @@ -12,7 +12,8 @@ * suite's own — no test ever touches the user's `~/.xmd/repositories`. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { expect } from "@executablemd/test-support/expect"; import { scoped, type Operation } from "effection"; import { exists, readdir, readTextFile, rm, writeTextFile } from "@effectionx/fs"; @@ -30,7 +31,6 @@ import { spawn, withResolvers } from "effection"; import { registerComponents } from "@executablemd/core"; import type { ChildOutcome } from "./support/run-composition-child.ts"; import { - ordinaryDocumentCases, causedBy, commonDirectoryOf, countingOrdinaryHost, @@ -42,6 +42,7 @@ import { readSidecar, repositorySlotOf, runOrdinaryDocument, + useOrdinaryDocumentRunner, gateComponent, useHostCheckout, useManagedRoot, @@ -51,97 +52,91 @@ import { } from "./support/run-composition.ts"; import { CHILD, REMOTE, entriesOf, isManagedRefusal } from "./support/run-composition-tier.ts"; -const ordinaryCase = ordinaryDocumentCases("packages/git/tests/run-composition-managed.test.ts"); - describe("ORC8 — managed checkouts are persistent", () => { - ordinaryCase( - "leaves the checkout, its metadata and its working files after the run", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const common = commonDirectoryOf(checkout); - const slot = worktreeSlotOf(root, common, "kept"); - - yield* runFresh( - [ - ``, - `unfinished`, - "", - ].join("\n"), - { root, cwd: checkout.root }, - ); + beforeEach(journalBeforeEach("packages/git/tests/run-composition-managed.test.ts")); - expect(yield* exists(slot.checkout)).toBe(true); - expect(yield* exists(`${slot.checkout}/draft.md`)).toBe(true); - const sidecar = yield* readSidecar(slot); - expect(sidecar).toMatchObject({ - kind: "worktree", - version: 1, - name: "kept", - requestedBranch: "kept", - requestedBase: null, - owner: common, - }); - }, - ); - - ordinaryCase( - "leaves a managed Repository, its metadata and its files after the run", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const slot = repositorySlotOf(root, remote.locator, "project"); - - yield* runFresh( - [ - ``, - `unfinished`, - "", - ].join("\n"), - { root, cwd: checkout.root }, - ); + it("leaves the checkout, its metadata and its working files after the run", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const common = commonDirectoryOf(checkout); + const slot = worktreeSlotOf(root, common, "kept"); - expect(yield* exists(`${slot.checkout}/draft.md`)).toBe(true); - expect(yield* readSidecar(slot)).toMatchObject({ - kind: "repository", - version: 1, - name: "project", - locator: remote.locator, - requestedBase: null, - }); - }, - ); - - ordinaryCase( - "issues no Git or delete command for a checkout while tearing down", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const counting = countingOrdinaryHost(); - - yield* runFresh( - [ - ``, - ``, - ].join("\n"), - { root, cwd: checkout.root, host: counting.host }, - ); + yield* runFresh( + [ + ``, + `unfinished`, + "", + ].join("\n"), + { root, cwd: checkout.root }, + ); - // Nothing that could undo a checkout ever ran — not while the document was - // expanding, and not on the way out. - const issued = subcommands(counting.counters); - for (const undoing of ["reset", "clean", "restore", "prune", "gc", "fetch"]) { - expect(`${undoing} ${issued.includes(undoing)}`).toBe(`${undoing} false`); - } - expect(counting.counters.commands.some((args) => args.includes("--force"))).toBe(false); - expect( - counting.counters.commands.some((args) => args[0] === "worktree" && args[1] === "remove"), - ).toBe(false); - }, - ); + expect(yield* exists(slot.checkout)).toBe(true); + expect(yield* exists(`${slot.checkout}/draft.md`)).toBe(true); + const sidecar = yield* readSidecar(slot); + expect(sidecar).toMatchObject({ + kind: "worktree", + version: 1, + name: "kept", + requestedBranch: "kept", + requestedBase: null, + owner: common, + }); + }); + + it("leaves a managed Repository, its metadata and its files after the run", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const slot = repositorySlotOf(root, remote.locator, "project"); + + yield* runFresh( + [ + ``, + `unfinished`, + "", + ].join("\n"), + { root, cwd: checkout.root }, + ); + + expect(yield* exists(`${slot.checkout}/draft.md`)).toBe(true); + expect(yield* readSidecar(slot)).toMatchObject({ + kind: "repository", + version: 1, + name: "project", + locator: remote.locator, + requestedBase: null, + }); + }); + + it("issues no Git or delete command for a checkout while tearing down", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const counting = countingOrdinaryHost(); + + yield* runFresh( + [ + ``, + ``, + ].join("\n"), + { root, cwd: checkout.root, host: counting.host }, + ); + + // Nothing that could undo a checkout ever ran — not while the document was + // expanding, and not on the way out. + const issued = subcommands(counting.counters); + for (const undoing of ["reset", "clean", "restore", "prune", "gc", "fetch"]) { + expect(`${undoing} ${issued.includes(undoing)}`).toBe(`${undoing} false`); + } + expect(counting.counters.commands.some((args) => args.includes("--force"))).toBe(false); + expect( + counting.counters.commands.some((args) => args[0] === "worktree" && args[1] === "remove"), + ).toBe(false); + }); it("keeps both kinds of checkout after a cancellation", function* () { const remote = yield* useBareRemote(REMOTE); @@ -168,72 +163,68 @@ describe("ORC8 — managed checkouts are persistent", () => { expect(yield* exists(`${worktree.checkout}/in-flight.md`)).toBe(true); }); - ordinaryCase( - "keeps a managed Repository after an authored failure inside its body", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const slot = repositorySlotOf(root, remote.locator, "surviving"); + it("keeps a managed Repository after an authored failure inside its body", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const slot = repositorySlotOf(root, remote.locator, "surviving"); - yield* raised( - runFresh( - [ - ``, - `written before the failure`, - ``, - ``, - ``, - "", - ].join("\n"), - { root, cwd: checkout.root }, - ), - ); + yield* raised( + runFresh( + [ + ``, + `written before the failure`, + ``, + ``, + ``, + "", + ].join("\n"), + { root, cwd: checkout.root }, + ), + ); - // The path, the sidecar, the Git state and the working file all survive. - expect(yield* exists(slot.checkout)).toBe(true); - expect(yield* readSidecar(slot)).toMatchObject({ - kind: "repository", - version: 1, - name: "surviving", - locator: remote.locator, - }); - expect(yield* readTextFile(`${slot.checkout}/kept.md`)).toBe("written before the failure"); - // The branch the document switched to and the staging it did are both still - // there: nothing rolled back, and nothing was cleaned up on the way out. - expect(git(["rev-parse", "--abbrev-ref", "HEAD"], slot.checkout, checkout.home)).toBe( - "in-progress", - ); - expect(git(["diff", "--cached", "--name-only"], slot.checkout, checkout.home)).toContain( - "kept.md", - ); - }, - ); - - ordinaryCase( - "keeps the checkout after an authored failure inside the Worktree body", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); - const slot = worktreeSlotOf(root, commonDirectoryOf(checkout), "survivor"); - - yield* raised( - runFresh( - [ - ``, - `written before the failure`, - ``, - "", - ].join("\n"), - { root, cwd: checkout.root }, - ), - ); + // The path, the sidecar, the Git state and the working file all survive. + expect(yield* exists(slot.checkout)).toBe(true); + expect(yield* readSidecar(slot)).toMatchObject({ + kind: "repository", + version: 1, + name: "surviving", + locator: remote.locator, + }); + expect(yield* readTextFile(`${slot.checkout}/kept.md`)).toBe("written before the failure"); + // The branch the document switched to and the staging it did are both still + // there: nothing rolled back, and nothing was cleaned up on the way out. + expect(git(["rev-parse", "--abbrev-ref", "HEAD"], slot.checkout, checkout.home)).toBe( + "in-progress", + ); + expect(git(["diff", "--cached", "--name-only"], slot.checkout, checkout.home)).toContain( + "kept.md", + ); + }); - expect(yield* exists(`${slot.checkout}/kept.md`)).toBe(true); - expect(yield* readSidecar(slot)).toMatchObject({ kind: "worktree" }); - }, - ); + it("keeps the checkout after an authored failure inside the Worktree body", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); + const slot = worktreeSlotOf(root, commonDirectoryOf(checkout), "survivor"); + + yield* raised( + runFresh( + [ + ``, + `written before the failure`, + ``, + "", + ].join("\n"), + { root, cwd: checkout.root }, + ), + ); + + expect(yield* exists(`${slot.checkout}/kept.md`)).toBe(true); + expect(yield* readSidecar(slot)).toMatchObject({ kind: "worktree" }); + }); }); describe("ORC9 — compatible reuse", () => { diff --git a/packages/git/tests/run-composition-remote.test.ts b/packages/git/tests/run-composition-remote.test.ts index 07ecc9366..af16589b1 100644 --- a/packages/git/tests/run-composition-remote.test.ts +++ b/packages/git/tests/run-composition-remote.test.ts @@ -13,7 +13,8 @@ import { Permissions } from "@executablemd/runtime"; * refused before one is. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { expect } from "@executablemd/test-support/expect"; import { type Operation } from "effection"; import { admitLivePushEvidence } from "../src/deno/run-composition/operations.ts"; @@ -33,7 +34,6 @@ import { patches, } from "./support/github.ts"; import { - ordinaryDocumentCases, causedBy, countingOrdinaryHost, fingerprintTree, @@ -41,6 +41,7 @@ import { subcommands, raised, runOrdinaryDocument, + useOrdinaryDocumentRunner, recordingAccess, rewritingHost, statedIdentity, @@ -57,39 +58,38 @@ import { isAdmissionFailure, } from "./support/run-composition-tier.ts"; -const ordinaryCase = ordinaryDocumentCases("packages/git/tests/run-composition-remote.test.ts"); +const OWNER = "packages/git/tests/run-composition-remote.test.ts"; describe("ORC14 — live Push evidence", () => { - ordinaryCase( - "records a performed publication and lets exactly that head be authorized", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + beforeEach(journalBeforeEach(OWNER)); + it("records a performed publication and lets exactly that head be authorized", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - const failure = yield* raised( - runFresh( - [ - ``, - `published`, - ``, - ``, - ``, - ``, - ].join("\n"), - { root, cwd: checkout.root }, - ), - ); + const failure = yield* raised( + runFresh( + [ + ``, + `published`, + ``, + ``, + ``, + ``, + ].join("\n"), + { root, cwd: checkout.root }, + ), + ); - // The branch really is at the remote, at the commit this execution made. - expect(remoteBranch(remote, "published")).toBe(checkout.run("rev-parse", "HEAD")); - // And the pull request got past the evidence gate: what stopped it is the - // adapter declining a locator that is not a github.com repository, which is - // the step *after* the local authorization this criterion is about. - expect(String(failure)).toContain("only for repositories on github.com"); - expect(String(failure)).not.toContain("holds no successful result"); - }, - ); + // The branch really is at the remote, at the commit this execution made. + expect(remoteBranch(remote, "published")).toBe(checkout.run("rev-parse", "HEAD")); + // And the pull request got past the evidence gate: what stopped it is the + // adapter declining a locator that is not a github.com repository, which is + // the step *after* the local authorization this criterion is about. + expect(String(failure)).toContain("only for repositories on github.com"); + expect(String(failure)).not.toContain("holds no successful result"); + }); it("records an already-equal publication the same way it records a performed one", function* () { const remote = yield* useBareRemote(REMOTE); @@ -231,7 +231,8 @@ describe("ORC14 — live Push evidence", () => { expect((stale as LivePushEvidenceError).reason).toBe("conflicting-push-evidence"); }); - ordinaryCase("lets the latest publication of a destination decide", function* (runFresh) { + it("lets the latest publication of a destination decide", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const remote = yield* useBareRemote(REMOTE); const root = yield* useManagedRoot(); const checkout = yield* useHostCheckout(remote.locator); @@ -264,55 +265,52 @@ describe("ORC14 — live Push evidence", () => { expect(remoteBranch(remote, "moving")).toBe(checkout.run("rev-parse", "HEAD~1")); }); - ordinaryCase( - "authorizes at the second publication's commit, not the first's", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + it("authorizes at the second publication's commit, not the first's", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - const failure = yield* raised( - runFresh( - [ - ``, - `one`, - ``, - ``, - ``, - `two`, - ``, - ``, - ``, - ``, - ].join("\n"), - { root, cwd: checkout.root }, - ), - ); - // Past the gate: the latest publication names the head the pull request - // would open from. - expect(String(failure)).toContain("only for repositories on github.com"); - expect(remoteBranch(remote, "latest")).toBe(checkout.run("rev-parse", "HEAD")); - }, - ); + const failure = yield* raised( + runFresh( + [ + ``, + `one`, + ``, + ``, + ``, + `two`, + ``, + ``, + ``, + ``, + ].join("\n"), + { root, cwd: checkout.root }, + ), + ); + // Past the gate: the latest publication names the head the pull request + // would open from. + expect(String(failure)).toContain("only for repositories on github.com"); + expect(remoteBranch(remote, "latest")).toBe(checkout.run("rev-parse", "HEAD")); + }); }); describe("ORC15 — evidence cannot cross runs", () => { - ordinaryCase( - "refuses a PullRequest in an execution that published nothing", - function* (runFresh) { - const remote = yield* useBareRemote(REMOTE); - const root = yield* useManagedRoot(); - const checkout = yield* useHostCheckout(remote.locator); + beforeEach(journalBeforeEach(OWNER)); + it("refuses a PullRequest in an execution that published nothing", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); + const remote = yield* useBareRemote(REMOTE); + const root = yield* useManagedRoot(); + const checkout = yield* useHostCheckout(remote.locator); - const failure = yield* raised( - runFresh(``, { - root, - cwd: checkout.root, - }), - ); - expect(String(failure)).toContain("holds no successful result"); - }, - ); + const failure = yield* raised( + runFresh(``, { + root, + cwd: checkout.root, + }), + ); + expect(String(failure)).toContain("holds no successful result"); + }); it("does not let one execution's real publication authorize the next", function* () { const remote = yield* useBareRemote(REMOTE); @@ -487,6 +485,7 @@ describe("ORC15 — evidence cannot cross runs", () => { }); describe("ORC16 — live Issues", () => { + beforeEach(journalBeforeEach(OWNER)); it("reads and files through the configured transport, keyed to this execution", function* () { const remote = yield* useBareRemote(REMOTE); const store = gitHubStore({ @@ -566,7 +565,8 @@ describe("ORC16 — live Issues", () => { expect(store.requests).toHaveLength(0); }); - ordinaryCase("installs no matching provider when nothing is configured", function* (runFresh) { + it("installs no matching provider when nothing is configured", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const remote = yield* useBareRemote(REMOTE); const root = yield* useManagedRoot(); const checkout = yield* useHostCheckout(remote.locator); @@ -984,7 +984,9 @@ describe("checkout admission is the whole repository identity", () => { * at the end is the ordinary provider's own. */ describe("ORC18 — the authored Git vocabulary", () => { - ordinaryCase("reaches Repository and Git middleware, and still publishes", function* (runFresh) { + beforeEach(journalBeforeEach(OWNER)); + it("reaches Repository and Git middleware, and still publishes", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const remote = yield* useBareRemote(REMOTE); const root = yield* useManagedRoot(); const here = yield* useHostCheckout(remote.locator); diff --git a/packages/git/tests/support/run-composition.ts b/packages/git/tests/support/run-composition.ts index 6114f9e11..98f735299 100644 --- a/packages/git/tests/support/run-composition.ts +++ b/packages/git/tests/support/run-composition.ts @@ -25,8 +25,7 @@ import { API, useHostFiles } from "@executablemd/runtime"; import { InMemoryStream } from "@executablemd/durable-streams"; import type { Json } from "@executablemd/durable-streams"; import { useTempDirectory } from "@executablemd/test-support/temp"; -import { it } from "@executablemd/test-support/bdd"; -import { captureTestJournal } from "@executablemd/test-support/failure-journal"; +import { useJournalStream } from "@executablemd/test-support/failure-journal"; import { useCompositionComponents } from "../../src/composition/installation.ts"; import { RepositoryContext } from "../../src/composition/context.ts"; import type { RepositorySelection } from "../../src/composition/selection.ts"; @@ -282,14 +281,9 @@ export function runOrdinaryDocument( type RunOrdinary = (source: string, options: RunOptions) => Operation; -export function ordinaryDocumentCases(owner: string) { - return (name: string, body: (run: RunOrdinary) => Operation): void => { - it(name, () => - captureTestJournal({ owner, name }, "", (stream) => - body((source, options) => runOrdinaryDocument(source, options, stream)), - ), - ); - }; +export function* useOrdinaryDocumentRunner(): Operation { + const stream = yield* useJournalStream(""); + return (source, options) => runOrdinaryDocument(source, options, stream); } /** What a suite reads back about one managed slot. */