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..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,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 { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; 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,9 @@ const DOC = [ ].join("\n"); describe("Tier XA — ACPX provider through the rootProvider seam", () => { - it("XA1: agent availability → session → normalized prompt → structured teardown", function* () { + beforeEach(journalBeforeEach(OWNER)); + 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,6 +41,7 @@ describe("Tier XA — ACPX provider through the rootProvider seam", () => { const docPath = path.join(dir, "doc.md"); yield* writeTextFile(docPath, DOC); + const stream = yield* useJournalStream(docPath); const result = yield* scoped(function* () { yield* useFlatWorld(CWD); yield* installAgentComponents({ @@ -53,7 +57,7 @@ describe("Tier XA — ACPX provider through the rootProvider seam", () => { // `` 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() }, [ + const execution = yield* executeInstalled({ path: docPath, stream }, [ { components: agentIdentityComponents() }, ]); const subscription = yield* execution.output; @@ -89,7 +93,10 @@ 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* () { + beforeEach(journalBeforeEach(OWNER)); + 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,6 +122,7 @@ describe("Tier WAP — same-named sibling Sessions", () => { ].join("\n"), ); + const stream = yield* useJournalStream(docPath); yield* scoped(function* () { yield* useFlatWorld(CWD); yield* installAgentComponents({ @@ -165,7 +173,7 @@ describe("Tier WAP — same-named sibling Sessions", () => { // `` 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() }, [ + const execution = yield* executeInstalled({ path: docPath, stream }, [ { components: agentIdentityComponents() }, ]); const subscription = yield* execution.output; @@ -194,7 +202,9 @@ describe("Tier WAP — same-named sibling Sessions", () => { 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,6 +231,7 @@ describe("Tier WAP — same-named sibling Sessions", () => { ].join("\n"), ); + const stream = yield* useJournalStream(docPath); yield* scoped(function* () { yield* useFlatWorld(CWD); yield* installAgentComponents({ @@ -267,7 +278,7 @@ describe("Tier WAP — same-named sibling Sessions", () => { // `` 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() }, [ + const execution = yield* executeInstalled({ path: docPath, stream }, [ { components: agentIdentityComponents() }, ]); const subscription = yield* execution.output; diff --git a/packages/agent/tests/agent-components.test.ts b/packages/agent/tests/agent-components.test.ts index b6a6eb06c..831701690 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,6 +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 { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +const OWNER = "packages/agent/tests/agent-components.test.ts"; interface StubResponse { status?: "completed" | "failed" | "cancelled"; @@ -102,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; } @@ -146,15 +153,16 @@ 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 }> { - 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 })); 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 }, [ @@ -170,14 +178,14 @@ function* runDoc( }); } -describe("Tier AC — agent components", () => { +const SUITE = "Tier AC — agent components"; + +describe(SUITE, () => { + beforeEach(journalBeforeEach(OWNER)); 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 { 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; @@ -190,7 +198,7 @@ describe("Tier AC — agent components", () => { it("AC2: empty wrapper children win over the text prop", function* () { const stub = createStubProvider(); yield* installStub(stub); - const { result } = yield* runDoc('\n', new InMemoryStream()); + const { result } = yield* runDoc('\n'); expect(result.ok).toBe(true); expect(stub.promptCalls.length).toBe(1); expect(stub.promptCalls[0]!.content).toBe(""); @@ -199,7 +207,7 @@ describe("Tier AC — agent components", () => { it("AC3: self-closing falls back to the text prop", function* () { const stub = createStubProvider(); yield* installStub(stub); - const { output, result } = yield* runDoc('\n', new InMemoryStream()); + 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]"); @@ -212,7 +220,7 @@ describe("Tier AC — agent components", () => { it("AC19: the removed prompt prop is rejected as an unknown prop", function* () { const stub = createStubProvider(); yield* installStub(stub); - const { result } = yield* runDoc('\n', new InMemoryStream()); + const { result } = yield* runDoc('\n'); expect(result.ok).toBe(false); expect(stub.promptCalls.length).toBe(0); const reported = result.ok ? "" : result.error.message; @@ -232,10 +240,7 @@ describe("Tier AC — agent components", () => { }); // 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(), - ); + 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); @@ -278,7 +283,6 @@ describe("Tier AC — agent components", () => { yield* installStub(stub); const { output, result } = yield* runDoc( '\n\nCaptured: {answer}\n', - new InMemoryStream(), ); expect(result.ok).toBe(true); expect(output).toContain("Captured: [stub-agent:stub:default:hi]"); @@ -298,7 +302,7 @@ describe("Tier AC — agent components", () => { '', "", ].join("\n"); - const { result } = yield* runDoc(doc, new InMemoryStream()); + const { result } = yield* runDoc(doc); expect(result.ok).toBe(true); expect(stub.promptCalls.length).toBe(2); expect(stub.promptCalls[0]).toMatchObject({ @@ -319,7 +323,6 @@ describe("Tier AC — agent components", () => { 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"); @@ -331,7 +334,11 @@ describe("Tier AC — agent components", () => { 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 "] }; + return { + status: "failed", + stopReason: "max_tokens", + deltas: ["partial-one "], + }; } if (content.includes("second-fail")) { return { status: "failed", stopReason: "refusal", deltas: [] }; @@ -349,7 +356,7 @@ describe("Tier AC — agent components", () => { "after all prompts", "", ].join("\n"); - const { output, result } = yield* runDoc(doc, new InMemoryStream()); + const { output, result } = yield* runDoc(doc); expect(output).toContain("partial-one"); expect(output).toContain("after all prompts"); expect(result.ok).toBe(false); @@ -371,11 +378,13 @@ describe("Tier AC — agent components", () => { }); it("AC8: throwOnError aborts the document immediately", function* () { - const stub = createStubProvider(() => ({ status: "failed", stopReason: "refusal" })); + const stub = createStubProvider(() => ({ + status: "failed", + stopReason: "refusal", + })); yield* installStub(stub); const { output, result } = yield* runDoc( '\n\nnever reached\n', - new InMemoryStream(), ); expect(result.ok).toBe(false); if (!result.ok) { @@ -505,7 +514,11 @@ describe("Tier AC — agent components", () => { 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: { @@ -523,7 +536,11 @@ describe("Tier AC — agent components", () => { 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/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..9ed0feceb 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 "@executablemd/test-support/failure-journal"; import type { DeliverySink } from "../src/stdout-delivery.ts"; import { useTempDirectory } from "@executablemd/test-support/temp"; @@ -237,93 +236,98 @@ 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 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 })); - - 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([]); + return captureReferenceEntry( + name, + function* (holder) { + const source = yield* referenceSource(); + const session = opened(yield* submitReplEntry({ ...options(holder), source })); - expect(question.submit({ decision: "approve" }).kind).toBe("answered"); - yield* session.join(); + 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 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); + 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, + ); } -describe("REPL execution: submitting one entry", () => { +describe(SUBMIT_SUITE, () => { beforeAll(() => useTempFileCompiler()); + const admission = "X1: admits the exact source, binds, generates, asks, and settles"; + it(admission, () => admitReferenceEntry(admission)); - it("X1: admits the exact source, binds, generates, asks, and settles", () => - admitReferenceEntry()); - - 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 +358,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 +392,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 +427,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 +450,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 +467,74 @@ 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(); - - 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: 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(); + 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(); + } - 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 = ''; - - // 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), + ); + }, + process.stderr, + SUBMIT_SUITE, + )); it("X1: an entry this environment cannot run leaves the history empty", function* () { const holder = execution(); @@ -515,31 +549,36 @@ 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 })); - - 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 @@ -592,26 +631,31 @@ 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 })); - - (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(); @@ -773,135 +817,159 @@ describe("REPL execution: reopening one history", () => { describe("REPL execution: pausing expansion", () => { beforeAll(() => useTempFileCompiler()); + 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(); + } + controller.resume(); + yield* session.join(); - 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 })); - - // 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(); - } - 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"); - }); - - 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); - - // 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"); - - // 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); - - // 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); - }); - - it("P1: continuing twice releases nothing the second time", function* () { - const holder = execution(); - const source = yield* referenceSource(); - const session = opened(yield* submitReplEntry({ ...options(holder), source })); - - controlling(session).pause(); - yield* pausedOrDone(session); - const held = controlling(session).released; - controlling(session).resume(); - const afterFirst = controlling(session).released; - controlling(session).resume(); - - expect(afterFirst).toBeGreaterThan(held); - expect(controlling(session).released).toBe(afterFirst); - - (yield* nextQuestion(session)).submit({ decision: "approve" }); - 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 })); - - 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); - - 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(); - 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); - }); + 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; + + 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); + + 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"); + + // 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", + )); + + 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 })); + + controlling(session).pause(); + yield* pausedOrDone(session); + const held = controlling(session).released; + controlling(session).resume(); + const afterFirst = controlling(session).released; + controlling(session).resume(); + + expect(afterFirst).toBeGreaterThan(held); + expect(controlling(session).released).toBe(afterFirst); + + (yield* nextQuestion(session)).submit({ decision: "approve" }); + yield* session.join(); + }, + process.stderr, + "REPL execution: pausing expansion", + )); + + 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).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); + + 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()); @@ -939,36 +1007,41 @@ 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); - }); + 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/cli/tests/repl-model.test.ts b/packages/cli/tests/repl-model.test.ts index 4ff20c653..01e71df20 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 "@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"; @@ -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..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"; @@ -128,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[] = []; @@ -188,6 +190,7 @@ describe("ORC1 — describing the vocabulary reaches nothing", () => { }); 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. @@ -196,7 +199,7 @@ describe("ORC1 — describing the vocabulary reaches nothing", () => { // it still installs no provider, which is the claim. yield* gitProfile("run"); const failure = yield* raisedValue( - collect(yield* execute({ ...inlineSource(``), stream: new InMemoryStream() })), + 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. diff --git a/packages/cli/tests/support/repl-observations.ts b/packages/cli/tests/support/repl-observations.ts index e55939b07..314669bad 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 "@executablemd/test-support/failure-journal"; +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..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"; @@ -39,7 +40,10 @@ function* text(): Operation { } describe("Installation-owned component observation", () => { + 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[] = []; @@ -140,6 +144,7 @@ describe("Installation-owned component observation", () => { }, }; const held = yield* useHeldHost({ + stream, installations: [installation], root: { path: "hooks.md", diff --git a/packages/core/tests/each.test.ts b/packages/core/tests/each.test.ts index 819dfd901..f6e3dba5f 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"; @@ -7,11 +7,11 @@ 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 { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; interface EachRun { segments: Segment[]; @@ -217,8 +217,9 @@ describe("Tier Each — native iteration directive", () => { describe("Tier Each — eval in the loop body", () => { beforeAll(() => useTempFileCompiler()); + beforeEach(journalBeforeEach("packages/core/tests/each.test.ts")); it("E18: a body eval block reads the current item binding", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": [ "```js eval", @@ -241,7 +242,7 @@ describe("Tier Each — eval in the loop body", () => { }); it("E19: renders a table row per item end to end", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": [ "```js eval", @@ -262,7 +263,7 @@ describe("Tier Each — eval in the loop body", () => { }); it("E22: each iteration's eval env is fresh — body bindings do not carry over", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": [ "```js eval", @@ -290,7 +291,7 @@ describe("Tier Each — eval in the loop body", () => { }); it("E21: an projected through resolves caller bindings", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "components/Wrap.md": "", "test.md": [ diff --git a/packages/core/tests/ephemeral-service.test.ts b/packages/core/tests/ephemeral-service.test.ts index 03de69a46..990f2ca32 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,6 +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"; const SAMPLE = `--- meta: @@ -107,6 +108,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* () { @@ -549,7 +551,7 @@ const shared = "must-not-execute"; }); it("allows a later ephemeral eval to update an existing live binding", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "doc.md": `\n`, "components/Provider.md": `--- diff --git a/packages/core/tests/eval-bindings.test.ts b/packages/core/tests/eval-bindings.test.ts index c11817c84..0c78999b6 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,12 +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 "@executablemd/test-support/failure-journal"; + +const OWNER = "packages/core/tests/eval-bindings.test.ts"; describe("Tier T5 — Binding environment", () => { + beforeEach(journalBeforeEach(OWNER)); 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(); + 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", @@ -47,7 +51,7 @@ 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(); + 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", @@ -69,7 +73,7 @@ describe("Tier T5 — Binding environment", () => { // T40: Empty block — no exports, no error it("T40: empty eval block — no exports, no error", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\n\n```\n", }); @@ -90,7 +94,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(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\nconst y = undeclaredVar + 1;\n```\n", }); @@ -104,7 +108,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(); + 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 c57dd6c42..c3860cabd 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, beforeEach, describe, it } 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 { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +const OWNER = "packages/core/tests/eval-error-mode.test.ts"; const PROJECTING_BLOCK = [ "```ts persist eval", @@ -26,12 +28,13 @@ 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 // 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(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "components/Wrap.md": [...PROJECTING_BLOCK, "", "", "done", ""].join("\n"), "doc.md": "\n\n", @@ -55,7 +58,7 @@ describe("Tier O — Eval scope hierarchy", () => { // 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(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "components/Wrap.md": ["", "", "", "done", ""].join("\n"), "doc.md": "\n\n", @@ -76,7 +79,7 @@ describe("Tier O — Eval scope hierarchy", () => { // 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(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "components/Wrap.md": ["", "", "", "done", ""].join("\n"), "doc.md": "\n\n", @@ -97,7 +100,7 @@ describe("Tier O — Eval scope hierarchy", () => { // 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(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "components/Value.md": [ "---", @@ -127,7 +130,7 @@ describe("Tier O — Eval scope hierarchy", () => { // 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(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "doc.md": [ "```js eval", @@ -154,7 +157,7 @@ describe("Tier O — Eval scope hierarchy", () => { // 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(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "components/Wrap.md": [ "```js persist eval", @@ -191,7 +194,7 @@ describe("Tier O — Eval scope hierarchy", () => { // 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(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "components/Wrap.md": [ "```ts eval", @@ -213,7 +216,7 @@ describe("Tier O — Eval scope hierarchy", () => { // 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(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "components/Wrap.md": ["", ...PROJECTING_BLOCK, ""].join("\n"), "doc.md": "\n\n", @@ -234,7 +237,7 @@ describe("Tier O — Eval scope hierarchy", () => { // 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(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "components/Wrap.md": [ "", @@ -261,7 +264,7 @@ describe("Tier O — Eval scope hierarchy", () => { // 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(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "components/Wrap.md": [ "```ts eval", @@ -296,7 +299,7 @@ describe("Tier O — Eval scope hierarchy", () => { // 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(); + const stream = yield* useJournalStream("doc.md"); yield* useStubFs({ "components/Wrap.md": [ "```ts eval", diff --git a/packages/core/tests/eval-return.test.ts b/packages/core/tests/eval-return.test.ts index 6db39cdbb..e2fc088cc 100644 --- a/packages/core/tests/eval-return.test.ts +++ b/packages/core/tests/eval-return.test.ts @@ -5,19 +5,23 @@ * 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 { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +const OWNER = "packages/core/tests/eval-return.test.ts"; describe("Eval block return value", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); // ER1: return string → rendered output it("ER1: return string → rendered output", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": '```js eval\nreturn "hello from eval";\n```\n', }); @@ -35,7 +39,7 @@ describe("Eval block return value", () => { // ER2: return number → coerced to string it("ER2: return number → coerced to string", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\nreturn 42;\n```\n", }); @@ -53,7 +57,7 @@ describe("Eval block return value", () => { // ER3: return null → no output it("ER3: return null → no output", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "before\n```js eval\nreturn null;\n```\nafter", }); @@ -72,7 +76,7 @@ describe("Eval block return value", () => { // ER4: return undefined → no output it("ER4: return undefined → no output", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "before\n```js eval\nreturn undefined;\n```\nafter", }); @@ -90,7 +94,7 @@ describe("Eval block return value", () => { // ER5: output() takes precedence over return it("ER5: output() takes precedence over return", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": '```js eval\noutput("explicit");\nreturn "ignored";\n```\n', }); @@ -109,7 +113,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(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": '```js persist eval\nreturn "persisted output";\n```\n', }); @@ -127,7 +131,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(); + 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 14f937f5a..ab20afc84 100644 --- a/packages/core/tests/eval-scope.test.ts +++ b/packages/core/tests/eval-scope.test.ts @@ -4,21 +4,24 @@ * 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 { 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 { 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 "@executablemd/test-support/failure-journal"; + +const OWNER = "packages/core/tests/eval-scope.test.ts"; describe("Tier T10 — eval-scope hierarchy", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); // T61: Document with eval blocks — scope created per document it("T61: eval blocks run within document scope", function* () { - const stream = new InMemoryStream(); + 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", }); @@ -41,7 +44,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(); + 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 4e01570d9..ae41c3cdf 100644 --- a/packages/core/tests/eval-timeout.test.ts +++ b/packages/core/tests/eval-timeout.test.ts @@ -7,21 +7,24 @@ * 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 { 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 "@executablemd/test-support/failure-journal"; + +const OWNER = "packages/core/tests/eval-timeout.test.ts"; describe("Tier T7 — timeout modifier", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); it("T49: block completes before timeout → success", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js timeout=30s eval\nconst x = 42;\n```\n", }); @@ -39,7 +42,7 @@ describe("Tier T7 — timeout modifier", () => { }); it("T50: a bare timeout boxes the block at the run's exec default", function* () { - const stream = new InMemoryStream(); + 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", @@ -52,7 +55,7 @@ describe("Tier T7 — timeout modifier", () => { }); it("T50a: a bare timeout with no exec default refuses instead of running unbounded", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js timeout eval\nconst x = 1;\n```\n", }); @@ -68,7 +71,7 @@ describe("Tier T7 — timeout modifier", () => { * 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(); + 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', @@ -82,7 +85,7 @@ describe("Tier T7 — timeout modifier", () => { }); it("T58: a declared duration boxes the block at what it declared", function* () { - const stream = new InMemoryStream(); + 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', diff --git a/packages/core/tests/execute.test.ts b/packages/core/tests/execute.test.ts index d051bdb0d..19649d447 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,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 { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +const OWNER = "packages/core/tests/execute.test.ts"; /** * A stand-in child that behaves like one: it writes its output to the display @@ -113,6 +116,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(); @@ -207,7 +211,7 @@ 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(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": "\n" }); yield* useStubExec(); @@ -283,8 +287,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* () { + const stream = yield* useJournalStream("README.md"); // 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", @@ -305,6 +310,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(); @@ -368,7 +374,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(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": ["```bash exec", "failing-command", "```"].join("\n") }); yield* useFailingExec(1, "command not found"); @@ -383,7 +389,7 @@ describe("Tier D — code execution and modifiers", () => { // 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(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": ["```bash exec", "echo partial; exit 1", "```"].join("\n"), }); @@ -598,7 +604,7 @@ 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(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": ["```bash frobnicate exec", "echo test", "```"].join("\n"), }); @@ -613,7 +619,7 @@ describe("Tier D — code execution and modifiers", () => { // D16: no terminal modifier → error it("D16: no terminal modifier — code block without exec/eval is passive text", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": ["```bash", "echo test", "```"].join("\n"), }); @@ -635,7 +641,7 @@ describe("Tier D — code execution and modifiers", () => { // D17: custom modifier registration it("D17: custom modifier registration — handler runs in chain", function* () { - const stream = new InMemoryStream(); + 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). @@ -676,9 +682,10 @@ describe("Tier D — code execution and modifiers", () => { // D19: modifier parsing — timeout=30s 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; - const stream = new InMemoryStream(); + yield* useStubFs({ "README.md": ["```bash timeout=30s exec", "echo test", "```"].join("\n"), }); @@ -702,6 +709,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(); @@ -797,7 +805,7 @@ describe("execute", () => { // E6: Props flow through expansion it("E6: validated props flow through expansion", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": '\n', "components/Greeting.md": [ @@ -830,7 +838,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(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": '\n', "components/Badge.md": ["---", "color: blue", "---", "", "badge"].join("\n"), @@ -876,7 +884,7 @@ describe("execute", () => { // Simple text document — no components, no exec it("simple text document — passthrough", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": "# Hello World\n\nThis is a test.\n" }); yield* useStubExec(); @@ -894,7 +902,7 @@ describe("execute", () => { // Default props applied it("default props applied when not provided", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": "\n", "components/Greeting.md": [ @@ -1036,7 +1044,7 @@ 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(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": "This is **bold\n\nmore text\n", "components/Greeting.md": "greeting\n", @@ -1191,7 +1199,7 @@ describe("execute", () => { // Content slot with exec it("Content slot + exec — both work together", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": ["", "```bash exec", "echo inside", "```", ""].join("\n"), "components/Wrapper.md": "BEFORE\n\nAFTER\n", @@ -1246,8 +1254,9 @@ describe("Component Api dispatch — journal shape", () => { }); describe("component-declared output — document workflow", () => { + beforeEach(journalBeforeEach(OWNER)); it("applies to an imported component invoked from a streaming root", function* () { - const stream = new InMemoryStream(); + 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", @@ -1260,7 +1269,7 @@ describe("component-declared output — document workflow", () => { }); it("applies to a root document the same way", function* () { - const stream = new InMemoryStream(); + 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"); @@ -1268,7 +1277,7 @@ describe("component-declared output — document workflow", () => { }); it("buffers a root with into a single emission after success", function* () { - const stream = new InMemoryStream(); + 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 }); @@ -1282,7 +1291,7 @@ describe("component-declared output — document workflow", () => { }); it("emits what a buffered root selected before its documentation failed", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": "\nSELECTED\n\n\n```bash exec\nfailing-command\n```\n", }); @@ -1303,7 +1312,7 @@ describe("component-declared output — document workflow", () => { }); it("keeps per-segment streaming for roots without ", function* () { - const stream = new InMemoryStream(); + 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). @@ -1321,7 +1330,7 @@ describe("component-declared output — document workflow", () => { }); it("emits no output event for an empty buffered root ", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": "Root docs.\n\n\n" }); const execution = yield* execute({ path: "README.md", stream }); 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..460fe7b9f 100644 --- a/packages/core/tests/generated-xmd-root.test.ts +++ b/packages/core/tests/generated-xmd-root.test.ts @@ -28,8 +28,9 @@ * 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"; @@ -1766,14 +1767,17 @@ describe("993 generated nested roots", () => { }); describe("1178 generated root references and scope", () => { + beforeEach(journalBeforeEach("packages/core/tests/generated-xmd-root.test.ts")); beforeAll(() => useTempFileCompiler()); 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}` }, }, diff --git a/packages/core/tests/host-generated-root.test.ts b/packages/core/tests/host-generated-root.test.ts index d235c195b..a5b26cf71 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"; @@ -46,8 +46,21 @@ 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, + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; import { contributeDocumentation, Documentation } from "../src/documentation-api.ts"; import { registerComponents } from "../src/components/registration.ts"; @@ -73,6 +86,163 @@ 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"; + +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[] = []; @@ -515,6 +685,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* () { @@ -566,6 +737,7 @@ describe("Tier E3 — the documentation this execution captured", () => { }); 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 } = {}; @@ -595,7 +767,7 @@ describe("Tier E3 — the documentation this execution captured", () => { }); const run = yield* withHeldHost( - { installations: [markerPackage(MARKER_PROSE, counted), selecting([probe()])] }, + { stream, installations: [markerPackage(MARKER_PROSE, counted), selecting([probe()])] }, function* (open) { answer.lookup = yield* open.execution.evaluateGeneratedXmdRoot( request('\n\n\n', invoked), @@ -918,67 +1090,76 @@ 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(); + 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)])], }); - }, - 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; - }); + 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; + }); - // 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 +1423,37 @@ 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 } = {}; - - 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", - }); + 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(); @@ -1307,29 +1496,34 @@ 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 } = {}; - - yield* scoped(function* () { - const open = yield* useHeldHost({ installations: [selecting(declared())] }); - try { - yield* open.execution.evaluateGeneratedXmdRoot({ - id: "turn-1", - source: "\n\n\n", + 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; }); - } 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 +1624,81 @@ 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( + it("E7f: an authoritative evaluation failure of no classified kind is still the run's", () => + captureTestJournal( { - root: { path: "host/ranking.md", source: "\n\n" }, - installations: [{ components: [failingMain()] }, selecting([probe(), failingCleanup()])], + owner: OWNER, + name: "E7f: an authoritative evaluation failure of no classified kind is still the run's", }, - 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", + 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); + }, + )); - // 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; - } - }); + 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); + }, + )); }); /** @@ -1603,6 +1820,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()); /** @@ -1690,6 +1908,7 @@ describe("Tier E9 — an availability refusal is the call's answer", () => { * request uses are checked at the published boundary. */ 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 = { @@ -1700,7 +1919,7 @@ describe("Tier E9 — an availability refusal is the call's answer", () => { }; const answer: { value?: Result } = {}; const run = yield* withHeldHost( - { installations: [selecting([probe(invoked)])] }, + { stream, installations: [selecting([probe(invoked)])] }, function* (open) { answer.value = yield* open.execution.evaluateGeneratedXmdRoot(asked); }, diff --git a/packages/core/tests/if.test.ts b/packages/core/tests/if.test.ts index 696e75d11..f4b233952 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,6 +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"; interface IfRun { segments: Segment[]; @@ -440,10 +441,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", @@ -479,7 +481,7 @@ describe("Tier IF — document execution", () => { }); it("IF40: an effect in the unselected branch never happens", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": [ "", @@ -500,7 +502,7 @@ describe("Tier IF — document execution", () => { }); it("IF41: a component in the unselected branch is never imported", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "components/Boom.md": "```js eval\noutput('BOOM_RAN');\n```\n", "test.md": "alt", @@ -562,7 +564,7 @@ describe("Tier IF — document execution", () => { }); it("IF43: an projected through resolves the caller's binding", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "components/Wrap.md": "", "test.md": [ @@ -591,6 +593,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) => @@ -631,7 +634,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 c6cd810ea..91a64ca53 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,6 +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 type { HostExecution } from "../src/execute.ts"; import { inlineSource } from "../src/root-source.ts"; import { collect } from "../src/collect.ts"; @@ -452,10 +453,11 @@ describe("Tier LET — failure and overwrite", () => { }); describe("Tier LET — a whole document", () => { + beforeEach(journalBeforeEach("packages/core/tests/let.test.ts")); beforeAll(() => useTempFileCompiler()); it("LET10: a direct binding reaches prose, an expression prop, and a later eval", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": [ "```ts eval", @@ -543,7 +545,7 @@ describe("Tier LET — a whole document", () => { }); it("LET12: Let is the syntax; Capture is an ordinary component", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": ['', "", ""].join("\n"), "components/Let.md": "A REPOSITORY FILE RAN\n", @@ -714,10 +716,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 @@ -730,7 +733,7 @@ describe("Tier LET — the ordinary inventory, and which values are eligible", ( }); it("C7: the eligible data is the JSON subset of the same document's root", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("README.md"); yield* useStubFs({ "README.md": EVERY_KIND }); const execution = yield* executeInstalled({ path: "README.md", stream }, []); diff --git a/packages/core/tests/loop.test.ts b/packages/core/tests/loop.test.ts index 08cac0564..fdee4eba7 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,6 +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"; interface LoopRun { segments: Segment[]; @@ -688,10 +689,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( @@ -711,7 +713,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": [ @@ -745,7 +747,7 @@ describe("Tier LOOP — document execution", () => { }); it("LOOP37: a binding accumulates across iterations", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useHostFiles(); yield* useStubFs({ "test.md": [ @@ -774,7 +776,7 @@ describe("Tier LOOP — document execution", () => { }); it("LOOP38: a component in the loop body is imported once per iteration", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useHostFiles(); yield* useStubFs({ "components/Step.md": "step\n", @@ -843,7 +845,7 @@ describe("Tier LOOP — document execution", () => { }); it("LOOP40: a chosen from a binding stops the document's loop", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useHostFiles(); yield* useStubFs({ "test.md": [ diff --git a/packages/core/tests/sample-component.test.ts b/packages/core/tests/sample-component.test.ts index 4dddc7f86..e71af1dab 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,6 +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"; function* writeFiles(dir: string, files: Record): Operation { for (const [filePath, content] of Object.entries(files)) { @@ -413,10 +414,11 @@ 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 it("EO1: output() produces eval block output", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": '```js eval\noutput("hello from eval");\n```\n', }); @@ -463,7 +465,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(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\nconst x = 42;\n```\nafter-eval", }); @@ -483,7 +485,7 @@ describe("Tier EO — eval output() function", () => { // EO4: output() with multiline content it("EO4: output() with multiline content", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": '```js eval\noutput("line1\\nline2\\nline3");\n```\n', }); @@ -501,7 +503,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(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "test.md": "```js eval\noutput(12345);\n```\n", }); @@ -519,10 +521,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 it("RC1: renderChildren returns empty for self-closing component", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "components/TestComp.md": [ "---", @@ -551,7 +554,7 @@ describe("Tier RC — renderChildren and render closures", () => { // RC2: renderChildren() captures children text content it("RC2: renderChildren captures children text", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "components/TestComp.md": [ "---", @@ -580,7 +583,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(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "components/TestComp.md": [ "---", @@ -608,7 +611,7 @@ describe("Tier RC — renderChildren and render closures", () => { }); it("RC4: renderChildren(override) is visible to children and wins over the caller value", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "components/TestComp.md": [ "---", @@ -643,7 +646,7 @@ describe("Tier RC — renderChildren and render closures", () => { }); it("RC5: renderChildren(override) does not leak into the caller env", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "components/TestComp.md": [ "---", @@ -671,7 +674,7 @@ describe("Tier RC — renderChildren and render closures", () => { }); it("RC6: renderChildren(override) rejects null, arrays, primitives, and non-plain objects", function* () { - const stream = new InMemoryStream(); + const stream = yield* useJournalStream("test.md"); yield* useStubFs({ "components/TestComp.md": [ "---", diff --git a/packages/core/tests/source-position.test.ts b/packages/core/tests/source-position.test.ts index 749d28486..27496de6e 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 { beforeEach, 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,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"; function componentsOf(segments: ReturnType): ComponentElement[] { const found: ComponentElement[] = []; @@ -22,6 +22,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 }); @@ -52,6 +53,7 @@ describe("source positions", () => { }); 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. @@ -66,7 +68,7 @@ describe("source positions", () => { ].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 @@ -101,11 +103,12 @@ describe("source positions", () => { }); 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 }> = []; - const stream = new InMemoryStream(); + yield* useStubFs({ "README.md": readme, "components/Wrapper.md": wrapper, diff --git a/packages/core/tests/streaming-emission.test.ts b/packages/core/tests/streaming-emission.test.ts index c5bc48267..c2cd79b85 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 "@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"; @@ -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..76170d65a 100644 --- a/packages/core/tests/support/held-host.ts +++ b/packages/core/tests/support/held-host.ts @@ -17,13 +17,14 @@ 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 { useJournalStream } from "@executablemd/test-support/failure-journal"; const NO_PROPS = { type: "object", properties: {}, additionalProperties: false } as const; @@ -112,8 +113,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..9299cfbf8 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,6 +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"; interface SwitchRun { segments: Segment[]; @@ -650,6 +651,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) => @@ -690,7 +692,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), @@ -781,6 +783,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 }> { @@ -809,9 +812,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 }; }); } @@ -831,9 +833,11 @@ describe("Tier SWITCH / CASE — provider boundary", () => { }); describe("Tier SWITCH / CASE — document execution", () => { + beforeEach(journalBeforeEach("packages/core/tests/switch.test.ts")); beforeAll(() => useTempFileCompiler()); 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": [ "---", @@ -851,14 +855,13 @@ describe("Tier SWITCH / CASE — document execution", () => { }); yield* useEchoExec(); - const output = asText( - yield* collect(yield* execute({ path: "test.md", stream: new InMemoryStream() })), - ); + 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* () { + const journal = yield* useJournalStream("test.md"); yield* useStubFs({ "components/Wrap.md": "", "test.md": [ @@ -875,16 +878,14 @@ describe("Tier SWITCH / CASE — document execution", () => { }); yield* useEchoExec(); - const output = asText( - yield* collect(yield* execute({ path: "test.md", stream: new InMemoryStream() })), - ); + 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 d37ec507b..f46d1bc95 100644 --- a/packages/core/tests/validation-integration.test.ts +++ b/packages/core/tests/validation-integration.test.ts @@ -1,8 +1,7 @@ -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"; -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,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"; function markdownComponent(name: string, props: Record): ComponentDefinition { return { @@ -101,7 +101,9 @@ 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`", () => { + 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": [ @@ -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,7 +129,9 @@ describe("`inputs` is not a compatibility alias for `props`", () => { }); 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": [ @@ -146,9 +148,7 @@ describe("definition-loading rejects invalid props schemas", () => { }); 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); } @@ -156,6 +156,7 @@ describe("definition-loading rejects invalid props schemas", () => { }); 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": [ @@ -172,9 +173,7 @@ describe("definition-loading rejects invalid props schemas", () => { }); 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); } @@ -182,15 +181,14 @@ describe("definition-loading rejects invalid props schemas", () => { }); 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: new InMemoryStream() })), - ); + message = asText(yield* collect(yield* execute({ path: "README.md", stream: journal }))); } catch (error) { message = error instanceof Error ? error.message : String(error); } diff --git a/packages/git/tests/git-observations-deno.test.ts b/packages/git/tests/git-observations-deno.test.ts index 67a29ba03..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,14 +37,15 @@ import process from "node:process"; import type { Json } from "@executablemd/durable-streams"; import { capturingProbe, writeFixtureFile } from "./support/observations.ts"; import { - runOrdinaryDocument, 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"; + import type { GitBytes, GitInvocation, @@ -68,7 +70,10 @@ function paths(value: Json | undefined): string[] { } describe("GO9 — which checkout answers, through a managed selection", () => { + beforeEach(journalBeforeEach("packages/git/tests/git-observations-deno.test.ts")); + 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); @@ -77,7 +82,7 @@ describe("GO9 — which checkout answers, through a managed selection", () => { ambient.run("commit", "-m", "ambient only"); const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ '', "", @@ -222,7 +227,10 @@ function watchingHost( } describe("GO9 — what an interrupted native read leaves behind", () => { + beforeEach(journalBeforeEach("packages/git/tests/git-observations-deno.test.ts")); + 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"); @@ -250,7 +258,7 @@ describe("GO9 — what an interrupted native read leaves behind", () => { yield* scoped(function* () { const task = yield* spawn(() => - runOrdinaryDocument( + runFresh( [ '', 'sentinel', diff --git a/packages/git/tests/git-observations.test.ts b/packages/git/tests/git-observations.test.ts index 78fb83332..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"; @@ -63,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"; @@ -95,6 +97,7 @@ 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"; + import type { ObservationPlace } from "../src/deno/composition/observations.ts"; /** @@ -387,12 +390,15 @@ function paths(value: Json | undefined): string[] { } describe("GO1 — discovery addresses the chosen repository", () => { + 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[] = []; - yield* runOrdinaryDocument( + yield* runFresh( ['', "", "", ""].join("\n"), { root, cwd: fixture.root, components: [capturingProbe(seen)] }, ); @@ -417,13 +423,16 @@ describe("GO1 — discovery addresses the chosen repository", () => { }); describe("GO2 — discovery excludes what the index does not hold", () => { + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); + 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[] = []; - yield* runOrdinaryDocument( + yield* runFresh( ['', "", "", ""].join("\n"), { root, cwd: fixture.root, components: [capturingProbe(seen)] }, ); @@ -438,11 +447,12 @@ describe("GO2 — discovery excludes what the index does not hold", () => { }); 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[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ '', "", @@ -462,11 +472,12 @@ describe("GO2 — discovery excludes what the index does not hold", () => { }); 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[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ ``, "", @@ -481,12 +492,15 @@ describe("GO2 — discovery excludes what the index does not hold", () => { }); describe("GO3 — the three comparisons", () => { + 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[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ ``, "", @@ -515,6 +529,7 @@ describe("GO3 — the three comparisons", () => { }); 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"); @@ -523,7 +538,7 @@ describe("GO3 — the three comparisons", () => { yield* writeFixtureFile(fixture.root, "untracked.md", "not tracked\n"); const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ '', "", @@ -577,12 +592,15 @@ describe("GO3 — the three comparisons", () => { }); describe("GO4 — the patch a comparison produces", () => { + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); + 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[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ ``, "", @@ -610,11 +628,12 @@ describe("GO4 — the patch a comparison produces", () => { }); 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[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ ``, "", @@ -657,11 +676,12 @@ describe("GO4 — the patch a comparison produces", () => { }); 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[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ ``, "", @@ -680,11 +700,12 @@ describe("GO4 — the patch a comparison produces", () => { }); it("collapses an unmerged path to one record", function* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const conflicted = yield* useConflictedCheckout(); const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ '', "", @@ -701,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); @@ -750,6 +772,7 @@ describe("GO5 — pages are bounded, complete, and about one observation", () => }); 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) { @@ -757,7 +780,7 @@ describe("GO5 — pages are bounded, complete, and about one observation", () => } const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ '', "", @@ -890,12 +913,15 @@ describe("GO5 — pages are bounded, complete, and about one observation", () => }); describe("GO6 — a changed observation cannot be paged over", () => { + 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); const raised_ = yield* raised( - runOrdinaryDocument( + runFresh( [ '', "", @@ -928,6 +954,7 @@ describe("GO6 — a changed observation cannot be paged over", () => { // 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 runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const fixture = yield* useGeneratedFixture(150); for (const path of fixture.paths.slice(0, 120)) { @@ -935,7 +962,7 @@ describe("GO6 — a changed observation cannot be paged over", () => { } const raised_ = yield* raised( - runOrdinaryDocument( + runFresh( [ '', "", @@ -963,6 +990,7 @@ describe("GO6 — a changed observation cannot be paged over", () => { }); 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) { @@ -970,7 +998,7 @@ describe("GO6 — a changed observation cannot be paged over", () => { } const raised_ = yield* raised( - runOrdinaryDocument( + runFresh( [ '', "", @@ -998,11 +1026,12 @@ describe("GO6 — a changed observation cannot be paged over", () => { }); 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* runOrdinaryDocument( + yield* runFresh( [ '', "", @@ -1087,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(); @@ -1157,12 +1187,13 @@ describe("GO7 — a file is read as one commit holds it", () => { }); 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* runOrdinaryDocument( + yield* runFresh( [ ``, "", @@ -1269,11 +1300,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* () { + const runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const fixture = yield* useFileFixture(); const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ ``, "", @@ -1290,11 +1322,12 @@ describe("GO7 — a file is read as one commit holds it", () => { }); 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* runOrdinaryDocument( + yield* runFresh( [ ``, "", @@ -1311,6 +1344,7 @@ describe("GO7 — a file is read as one commit holds it", () => { }); 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(); @@ -1362,6 +1396,7 @@ 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 runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const fixture = yield* useObservationFixture(); const linked = yield* useLinkedWorktree(fixture); @@ -1372,7 +1407,7 @@ describe("GO8 — a read runs no repository-defined program and changes nothing" const indexes = yield* indexFingerprints(fixture, linked); const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ '', "", @@ -1482,7 +1517,10 @@ describe("GO9 — which checkout answers, and what cannot choose one", () => { }); describe("GO10 — the comparison sees an edit the index's own times would hide", () => { + beforeEach(journalBeforeEach("packages/git/tests/git-observations.test.ts")); + 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"); @@ -1496,7 +1534,7 @@ describe("GO10 — the comparison sees an edit the index's own times would hide" yield* until(utimes(join(fixture.root, "src", "a.ts"), indexTimes.mtime, indexTimes.mtime)); const seen: Json[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ '', "", diff --git a/packages/git/tests/issue-markdown.test.ts b/packages/git/tests/issue-markdown.test.ts index 1a8e17db9..8ae85a758 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 "@executablemd/test-support/failure-journal"; /** 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..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"; @@ -41,6 +42,7 @@ import { subcommands, raised, runOrdinaryDocument, + useOrdinaryDocumentRunner, recordingAccess, useHostCheckout, useManagedRoot, @@ -54,13 +56,17 @@ import { reviewRoutes, } from "./support/run-composition-tier.ts"; +const OWNER = "packages/git/tests/run-composition-ambient.test.ts"; + describe("ORC3 — the ambient primary checkout", () => { + 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* runOrdinaryDocument( + yield* runFresh( [ ``, `ordinary`, @@ -113,7 +119,9 @@ describe("ORC3 — the ambient primary checkout", () => { }); describe("ORC4 — the ambient linked 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); @@ -124,7 +132,7 @@ describe("ORC4 — the ambient linked worktree", () => { const before = primary.run("rev-parse", "HEAD"); yield* scoped(function* () { - yield* runOrdinaryDocument( + yield* runFresh( [ `here`, ``, @@ -142,6 +150,7 @@ describe("ORC4 — the ambient linked 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)) }, @@ -197,6 +206,7 @@ describe("ORC5 — origin is not local admission", () => { }); 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. @@ -205,7 +215,7 @@ describe("ORC5 — origin is not local admission", () => { const checkout = yield* useHostCheckout(remote.locator); const counting = countingOrdinaryHost(); - yield* runOrdinaryDocument([``, ``].join("\n"), { + yield* runFresh([``, ``].join("\n"), { root, cwd: checkout.root, host: counting.host, @@ -241,7 +251,9 @@ describe("ORC5 — origin is not local admission", () => { }); describe("ORC6 — lexical working directories", () => { + 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" }, @@ -253,7 +265,7 @@ describe("ORC6 — lexical working directories", () => { // 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( + yield* runFresh( [ `outer`, ``, @@ -281,13 +293,14 @@ describe("ORC6 — lexical working directories", () => { }); 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); // 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,13 +352,15 @@ describe("ORC6 — lexical working directories", () => { }); describe("ORC13 — live local Git", () => { + 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( - runOrdinaryDocument( + runFresh( [ ``, `staged before the failure`, @@ -386,11 +401,12 @@ describe("ORC13 — live local Git", () => { }); 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); - yield* runOrdinaryDocument( + yield* runFresh( [ `mine`, ``, @@ -410,11 +426,12 @@ describe("ORC13 — live local Git", () => { }); 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); - yield* runOrdinaryDocument( + yield* runFresh( [ `pair`, ``, @@ -436,13 +453,14 @@ describe("ORC13 — live local Git", () => { }); 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"); const failure = yield* raised( - runOrdinaryDocument( + runFresh( [ `orphan`, ``, @@ -462,6 +480,7 @@ describe("ORC13 — live local Git", () => { }); 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" }, @@ -472,7 +491,7 @@ describe("ORC13 — live local Git", () => { // 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( + const rendered = yield* runFresh( [ ``, ``, @@ -570,8 +589,10 @@ 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. 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" }, @@ -597,7 +618,7 @@ describe("ORC6 — Dir makes the directory it names", () => { }, }; - yield* runOrdinaryDocument( + yield* runFresh( [ '', "", @@ -642,15 +663,16 @@ describe("ORC6 — Dir makes the directory it names", () => { // 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* () { + const runFresh = yield* useOrdinaryDocumentRunner(); 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", @@ -792,12 +814,13 @@ describe("ORC6 — Dir makes the directory it names", () => { // 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 runFresh = yield* useOrdinaryDocumentRunner(); const root = yield* useManagedRoot(); const remote = yield* useBareRemote(REMOTE); const checkout = (yield* useHostCheckout(remote.locator)).root; const seen: (RepositorySelection | undefined)[] = []; - yield* runOrdinaryDocument( + yield* runFresh( [ '', '', diff --git a/packages/git/tests/run-composition-managed.test.ts b/packages/git/tests/run-composition-managed.test.ts index e4c8ccd96..50eaf005e 100644 --- a/packages/git/tests/run-composition-managed.test.ts +++ b/packages/git/tests/run-composition-managed.test.ts @@ -12,10 +12,18 @@ * 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, 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"; @@ -34,23 +42,28 @@ import { readSidecar, repositorySlotOf, runOrdinaryDocument, + useOrdinaryDocumentRunner, gateComponent, useHostCheckout, 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", () => { + beforeEach(journalBeforeEach("packages/git/tests/run-composition-managed.test.ts")); + 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"); - yield* runOrdinaryDocument( + yield* runFresh( [ ``, `unfinished`, @@ -73,12 +86,13 @@ describe("ORC8 — managed checkouts are persistent", () => { }); 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* runOrdinaryDocument( + yield* runFresh( [ ``, `unfinished`, @@ -98,12 +112,13 @@ describe("ORC8 — managed checkouts are persistent", () => { }); 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* runOrdinaryDocument( + yield* runFresh( [ ``, ``, @@ -149,13 +164,14 @@ describe("ORC8 — managed checkouts are persistent", () => { }); 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( - runOrdinaryDocument( + runFresh( [ ``, `written before the failure`, @@ -188,13 +204,14 @@ describe("ORC8 — managed checkouts are persistent", () => { }); 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( - runOrdinaryDocument( + runFresh( [ ``, `written before the failure`, @@ -509,19 +526,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"); + } + 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 JSON.parse(printed) as ChildOutcome; + 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 +640,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 +666,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 +685,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..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"; @@ -40,6 +41,7 @@ import { subcommands, raised, runOrdinaryDocument, + useOrdinaryDocumentRunner, recordingAccess, rewritingHost, statedIdentity, @@ -56,14 +58,18 @@ import { isAdmissionFailure, } from "./support/run-composition-tier.ts"; +const OWNER = "packages/git/tests/run-composition-remote.test.ts"; + describe("ORC14 — live Push evidence", () => { + 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( - runOrdinaryDocument( + runFresh( [ ``, `published`, @@ -226,6 +232,7 @@ describe("ORC14 — live Push evidence", () => { }); 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); @@ -233,7 +240,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`, @@ -259,12 +266,13 @@ describe("ORC14 — live Push evidence", () => { }); 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( - runOrdinaryDocument( + runFresh( [ ``, `one`, @@ -288,13 +296,15 @@ describe("ORC14 — live Push evidence", () => { }); describe("ORC15 — evidence cannot cross runs", () => { + 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( - runOrdinaryDocument(``, { + runFresh(``, { root, cwd: checkout.root, }), @@ -475,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({ @@ -555,12 +566,13 @@ describe("ORC16 — live Issues", () => { }); 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); const failure = yield* raised( - runOrdinaryDocument(``, { + runFresh(``, { root, cwd: checkout.root, }), @@ -972,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", () => { + 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); @@ -980,7 +994,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..98f735299 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,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 { 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"; @@ -236,7 +237,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 +272,20 @@ 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* useOrdinaryDocumentRunner(): Operation { + const stream = yield* useJournalStream(""); + return (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..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 { InMemoryStream } from "@executablemd/durable-streams"; +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"; @@ -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..f56a82e95 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 "@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"; @@ -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/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..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 { InMemoryStream } from "@executablemd/durable-streams"; +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,7 +60,9 @@ function stubStream( } describe("agent + testing composition", () => { - it("both component sets decorate one execution", function* () { + beforeEach(journalBeforeEach("packages/testing/tests/agent-composition.test.ts")); + const name = "both component sets decorate one execution"; + it(name, function* () { const calls: string[] = []; yield* installTestingComponents(); yield* installAgentComponents({ @@ -114,7 +116,8 @@ 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() }, [ + const stream = yield* useJournalStream(docPath); + const execution = yield* executeInstalled({ path: docPath, stream }, [ { components: agentIdentityComponents() }, ]); const subscription = yield* execution.output; diff --git a/packages/testing/tests/assert-throws.test.ts b/packages/testing/tests/assert-throws.test.ts index 33a66e24c..7a6e335af 100644 --- a/packages/testing/tests/assert-throws.test.ts +++ b/packages/testing/tests/assert-throws.test.ts @@ -1,10 +1,11 @@ -import { describe, it } 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"; 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 { failureOf, runDoc } from "./helpers.ts"; const STRICT = [ @@ -28,6 +29,7 @@ function inTest(body: string): Record { } describe("", () => { + beforeEach(journalBeforeEach("packages/testing/tests/assert-throws.test.ts")); it("passes when the body raises an error matching a substring", function* () { const run = yield* runDoc( inTest(''), diff --git a/packages/testing/tests/assertions.test.ts b/packages/testing/tests/assertions.test.ts index 692a8e19b..0b4a89012 100644 --- a/packages/testing/tests/assertions.test.ts +++ b/packages/testing/tests/assertions.test.ts @@ -1,11 +1,13 @@ -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 { failureOf, runDoc } from "./helpers.ts"; describe("assertion components", () => { + beforeEach(journalBeforeEach("packages/testing/tests/assertions.test.ts")); beforeAll(() => useTempFileCompiler()); it("passing assertions in testing mode emit diagnostics", function* () { const run = yield* runDoc( diff --git a/packages/testing/tests/boundary-contract.test.ts b/packages/testing/tests/boundary-contract.test.ts index 789c406ea..e3a8546be 100644 --- a/packages/testing/tests/boundary-contract.test.ts +++ b/packages/testing/tests/boundary-contract.test.ts @@ -4,12 +4,14 @@ * 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 { runDoc } from "./helpers.ts"; 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 — @@ -70,6 +72,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 c9ca9af57..47b6d4dd1 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 "@executablemd/test-support/failure-journal"; 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..3ad0e6b61 100644 --- a/packages/testing/tests/helpers.ts +++ b/packages/testing/tests/helpers.ts @@ -8,10 +8,10 @@ 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 { 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"; @@ -41,6 +41,21 @@ export interface RunDocOptions { stream?: DurableStream; } +type RunFreshDoc = ( + files: Record, + options?: Omit, +) => Operation; + +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 = {}, @@ -74,9 +89,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 7e270d9f3..02b6264a1 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,9 +18,13 @@ 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 { journalBeforeEach } from "@executablemd/test-support/failure-journal"; +import { runDoc, withFreshDocument } from "./helpers.ts"; + +const OWNER = "packages/testing/tests/test-component.test.ts"; describe(" as a function component", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); /** An eval block whose retained work fails while the test is dismantled. */ @@ -476,17 +480,19 @@ 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"; + it(skippedCase, function* () { 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); + 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 diff --git a/packages/testing/tests/testing-boundary.test.ts b/packages/testing/tests/testing-boundary.test.ts index 6fb97d617..59f5c39c4 100644 --- a/packages/testing/tests/testing-boundary.test.ts +++ b/packages/testing/tests/testing-boundary.test.ts @@ -8,12 +8,13 @@ * 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 { runDoc } from "./helpers.ts"; import type { DocRun } from "./helpers.ts"; @@ -123,6 +124,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()); it("TB4: a local Testing component is selected, and the package's never runs", function* () { diff --git a/packages/testing/tests/testing-mode.test.ts b/packages/testing/tests/testing-mode.test.ts index ceafaf300..4e1757bd6 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,9 +7,11 @@ 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 { failureOf, runDoc } from "./helpers.ts"; describe("testing mode", () => { + beforeEach(journalBeforeEach("packages/testing/tests/testing-mode.test.ts")); beforeAll(() => useTempFileCompiler()); it("skips entirely during regular execution", function* () { const execCalls: string[] = []; diff --git a/packages/testing/tests/use-testing.test.ts b/packages/testing/tests/use-testing.test.ts index e10d38053..587beff6a 100644 --- a/packages/testing/tests/use-testing.test.ts +++ b/packages/testing/tests/use-testing.test.ts @@ -1,17 +1,24 @@ -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"; 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 { captureTestJournal, journalBeforeEach } from "@executablemd/test-support/failure-journal"; +import { failureOf, runDoc, withFreshDocument } from "./helpers.ts"; + +const OWNER = "packages/testing/tests/use-testing.test.ts"; function* drain( subscription: Subscription, @@ -26,39 +33,106 @@ function* drain( } describe("useTesting composition", () => { + beforeEach(journalBeforeEach(OWNER)); 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("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 }; + }); + 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* () { - 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 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"]]); + 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* () { @@ -205,68 +279,83 @@ describe("useTesting composition", () => { }); 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); - }); - 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"); + 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 }; + }); + // 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: "" }; + yield* captureTestJournal( + { + owner: OWNER, + name: "an early caller-scope halt tears down the document and leased eval scope", }, - }); - 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 }); + "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* 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..6eef07c61 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 "@executablemd/test-support/failure-journal"; +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..f8249871f 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,27 @@ 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 runFreshForm( + name: string, + source: string, + options: Omit, + check: (run: DocRun) => void | Operation, +): Operation { + return withWebFormDocument({ owner: OWNER, name }, source, options, check); +} const SCHEMA_BLOCK = [ "```js eval", @@ -26,8 +49,62 @@ function document(body: string, props = "schema={reviewSchema}"): string { } describe("WebForm: what a document gets", () => { + 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"); + }); + it("binds the validated response and renders nothing", function* () { - const run = yield* runWebFormDoc( + yield* runFreshForm( + "binds the validated response and renders nothing", [ document("# Review required\n\nDecide."), "```js eval", @@ -35,35 +112,48 @@ 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}\/$/); + yield* runFreshForm( + "is answered through its own live server", + 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); + 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); + }, + ); }); it("resolves an expression-valued uiSchema through core", function* () { @@ -82,10 +172,15 @@ 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* 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"); + }, + ); }); }); @@ -113,19 +208,24 @@ 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* 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([]); + }, + ); }); it("causes no live effect when the schema cannot be used", function* () { @@ -148,33 +248,40 @@ 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* 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. */ it("fails the document rather than printing", function* () { - const run = yield* runWebFormDoc( + yield* runFreshForm( + "fails the document rather than printing", '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); }); }); @@ -315,11 +422,16 @@ 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 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); + }, + ); }); it("prefers a repository WebForm.ts", function* () { @@ -334,9 +446,14 @@ 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* 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); + }, + ); }); }); diff --git a/packages/web/tests/document.test.ts b/packages/web/tests/document.test.ts index 6344c3d4e..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 { InMemoryStream } from "@executablemd/durable-streams"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; import { useTesting } from "@executablemd/testing"; import { fileURLToPath } from "node:url"; @@ -24,8 +24,11 @@ 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* () { + beforeEach(journalBeforeEach("packages/web/tests/document.test.ts")); + const name = "passes with an installed responder answering its forms"; + it(name, function* () { const source = yield* readTextFile(DOCUMENT); + const stream = yield* useJournalStream("WebForm.test.md"); const outcome = yield* scoped(function* () { yield* useTempFileCompiler(); @@ -60,7 +63,7 @@ describe("WebForm.test.md", () => { const execution = yield* execute({ path: "WebForm.test.md", - stream: new InMemoryStream(), + stream, }); try { diff --git a/packages/workflow/tests/generated-observations.test.ts b/packages/workflow/tests/generated-observations.test.ts index 314714347..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, @@ -35,6 +36,7 @@ import { recordedFiles, useRecordedFiles } from "../../core/tests/support/fragme import type { RecordedFiles } from "../../core/tests/support/fragment-files.ts"; const ROOT_PATH = "workflows/agent.md"; +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"; @@ -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); } @@ -253,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 @@ -266,9 +270,10 @@ describe("Tier WGX — the selection a workflow run offers", () => { }); 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 }); - const attempt = yield* run(evaluating(`\n`), { files }); + const attempt = yield* run(evaluating(`\n`), { files }, stream); expect(attempt.failure).toBe(undefined); expect(attempt.output).toContain("the retained note"); @@ -299,7 +304,8 @@ describe("Tier WGX — the selection a workflow run offers", () => { }); 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")); + const stream = yield* useJournalStream(ROOT_PATH); + const attempt = yield* run(evaluating("nothing to observe here.\n"), {}, stream); expect(attempt.failure).toBe(undefined); expect(attempt.output).toContain("nothing to observe here."); @@ -308,10 +314,11 @@ describe("Tier WGX — the selection a workflow run offers", () => { }); 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"); + 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."); @@ -319,6 +326,7 @@ describe("Tier WGX — the selection a workflow run offers", () => { }); 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 @@ -327,6 +335,7 @@ describe("Tier WGX — the selection a workflow run offers", () => { const attempt = yield* run( evaluating(`\n\n`), { files }, + stream, ); expect(attempt.failure).toContain("did not admit"); @@ -337,21 +346,25 @@ describe("Tier WGX — the selection a workflow run offers", () => { }); describe("Tier WGX — what an admitted request may reach is live", () => { + 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`)); + const attempt = yield* run(evaluating(`\n`), {}, stream); 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 stream = yield* useJournalStream(ROOT_PATH); const transport = yield* useTransport(); const attempt = yield* run( evaluating(`\n\n`), { generated: { requests: [{ url: URL_ONE }] } }, + stream, ); expect(attempt.failure).toBe(undefined); @@ -360,6 +373,7 @@ describe("Tier WGX — what an admitted request may reach is live", () => { }); it("WGX8: a destination outside the bound reaches no transport", function* () { + const stream = yield* useJournalStream(ROOT_PATH); const transport = yield* useTransport(); // The name is offered — this host bounds a request — and the destination is @@ -368,6 +382,7 @@ describe("Tier WGX — what an admitted request may reach is live", () => { const attempt = yield* run( evaluating(`\n\n`), { generated: { requests: [{ url: URL_ONE }] } }, + stream, ); expect(attempt.failure).toContain("not permitted in this scope"); @@ -475,6 +490,7 @@ describe("Tier WGX — what an admitted request may reach is live", () => { }); 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 @@ -491,6 +507,7 @@ describe("Tier WGX — what an admitted request may reach is live", () => { const attempt = yield* run( evaluating(`\n\n`), { generated: changing }, + stream, ); // Asked first, because it is the consequence: the destination the fragment @@ -508,6 +525,7 @@ describe("Tier WGX — what an admitted request may reach is live", () => { }); it("WGX10: an enclosing restriction still refuses what this host bounded", function* () { + const stream = yield* useJournalStream(ROOT_PATH); const transport = yield* useTransport(); const attempt = yield* scoped(function* () { @@ -515,9 +533,13 @@ describe("Tier WGX — what an admitted request may reach is live", () => { // 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 }] }, - }); + return yield* run( + evaluating(`\n\n`), + { + generated: { requests: [{ url: URL_ONE }] }, + }, + stream, + ); }); expect(attempt.failure).toContain("not permitted in this scope"); 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"],