diff --git a/packages/agent/tests/agent-function-components.test.ts b/packages/agent/tests/agent-function-components.test.ts index b8e6e75ea..d3ad9c6fe 100644 --- a/packages/agent/tests/agent-function-components.test.ts +++ b/packages/agent/tests/agent-function-components.test.ts @@ -8,7 +8,9 @@ * what a document gets. */ -import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +import { beforeAll, beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { InMemoryStream, serializeDurableEvent } from "@executablemd/durable-streams"; import type { DurableEvent } from "@executablemd/durable-streams"; @@ -373,6 +375,7 @@ interface RunOptions { function* runDoc( doc: string, options: RunOptions = {}, + sharedJournal = false, ): Operation<{ output: string; result: Result; trace: Trace; events: DurableEvent[] }> { const trace: Trace = options.trace ?? { prompts: [], agentLookups: [], timeouts: [] }; const dir = path.join(os.tmpdir(), `xmd-af-test-${randomUUID()}`); @@ -427,7 +430,7 @@ function* runDoc( // `` names durable work after its own invocation, so the host // declares it to the execution rather than registering it. - const stream = new InMemoryStream(); + const stream = sharedJournal ? yield* useJournalStream(docPath) : new InMemoryStream(); const execution = yield* executeInstalled( { path: docPath, @@ -451,11 +454,18 @@ function* runDoc( }); } +const OWNER = "packages/agent/tests/agent-function-components.test.ts"; + +function* run(doc: string, options?: RunOptions): ReturnType { + return yield* runDoc(doc, options, true); +} + describe("Tier AF — the engine owns props", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); it("AF1: an expression prop resolves, where the claimed handler rejected it", function* () { - const { output, result } = yield* runDoc( + const { output, result } = yield* run( ["```js eval", 'const who = "hello";', "```", "", "", ""].join("\n"), ); @@ -464,7 +474,7 @@ describe("Tier AF — the engine owns props", () => { }); it("AF2: a boolean expression prop resolves", function* () { - const { result } = yield* runDoc( + const { result } = yield* run( [ "```js eval", "const strict = false;", @@ -480,7 +490,7 @@ describe("Tier AF — the engine owns props", () => { it("AF3: an expression resolving to the wrong type is a schema printed error, and nothing runs", function* () { const trace: Trace = { prompts: [], agentLookups: [], timeouts: [] }; - const { result } = yield* runDoc( + const { result } = yield* run( ["```js eval", "const who = 42;", "```", "", "", ""].join("\n"), { trace }, ); @@ -496,7 +506,7 @@ describe("Tier AF — the engine owns props", () => { it("AF4: an unknown prop is rejected before the component performs anything", function* () { const trace: Trace = { prompts: [], agentLookups: [], timeouts: [] }; - const { output, result } = yield* runDoc( + const { output, result } = yield* run( 'body\n', { trace }, ); @@ -511,6 +521,7 @@ describe("Tier AF — the engine owns props", () => { }); describe("Tier AF — the engine owns `as`", () => { + beforeEach(journalBeforeEach(OWNER)); it("AF: live child input notices contribute neither Prompt capture nor durable Prompt fields", function* () { let delivered = 0; const notices: AgentPromptEvent[] = [ @@ -570,7 +581,7 @@ describe("Tier AF — the engine owns `as`", () => { }); it("AF5: the returned string is captured once and not also emitted", function* () { - const { output, result } = yield* runDoc('\n\nGot: {answer}\n'); + const { output, result } = yield* run('\n\nGot: {answer}\n'); expect(result.ok).toBe(true); expect(output).toContain("Got: [hi]"); @@ -579,7 +590,7 @@ describe("Tier AF — the engine owns `as`", () => { it("AF6: an invalid `as` prevents every component effect", function* () { const trace: Trace = { prompts: [], agentLookups: [], timeouts: [] }; - const { result } = yield* runDoc('\n', { trace }); + const { result } = yield* run('\n', { trace }); expect(result.ok).toBe(false); expect(result.ok ? "" : result.error.message).toContain('Prop "as" on '); @@ -589,9 +600,10 @@ describe("Tier AF — the engine owns `as`", () => { }); describe("Tier AF — a prompt does nothing before its content renders", () => { + beforeEach(journalBeforeEach(OWNER)); it("AF7: a failing wrapper performs no lookup, no prompt and no journal entry", function* () { const trace: Trace = { prompts: [], agentLookups: [], timeouts: [] }; - const { result } = yield* runDoc("\n\n\n", { trace }); + const { result } = yield* run("\n\n\n", { trace }); expect(result.ok).toBe(false); expect(result.ok ? "" : result.error.message).toContain("Failed to import component Missing"); @@ -601,15 +613,16 @@ describe("Tier AF — a prompt does nothing before its content renders", () => { it("AF8: an empty wrapper still overrides the text prop", function* () { const trace: Trace = { prompts: [], agentLookups: [], timeouts: [] }; - yield* runDoc('\n', { trace }); + yield* run('\n', { trace }); expect(trace.prompts).toEqual([""]); }); }); describe("Tier AF — failures that end the document", () => { + beforeEach(journalBeforeEach(OWNER)); it("AF9: an unavailable agent stops the document, nested in another wrapper", function* () { - const { output, result } = yield* runDoc( + const { output, result } = yield* run( [ "BEFORE", "", @@ -630,7 +643,7 @@ describe("Tier AF — failures that end the document", () => { }); it("AF10: a throwOnError prompt inside still ends the document", function* () { - const { output, result } = yield* runDoc( + const { output, result } = yield* run( [ "BEFORE", "", @@ -652,7 +665,7 @@ describe("Tier AF — failures that end the document", () => { }); it("AF11: the same holds two wrappers deep", function* () { - const { output, result } = yield* runDoc( + const { output, result } = yield* run( [ "BEFORE", "", @@ -674,7 +687,7 @@ describe("Tier AF — failures that end the document", () => { }); it("AF12: it survives a repository component projecting the prompt as content", function* () { - const { output, result } = yield* runDoc( + const { output, result } = yield* run( [ "BEFORE", "", @@ -698,6 +711,7 @@ describe("Tier AF — failures that end the document", () => { }); describe("Tier AF — registered defaults a document can replace", () => { + beforeEach(journalBeforeEach(OWNER)); const names = ["AgentProvider", "Agent", "Prompt", "ApproveAll", "AskPermission"]; it("AF24: a Session hands the exact value it was issued to every nested Prompt", function* () { @@ -706,7 +720,7 @@ describe("Tier AF — registered defaults a document can replace", () => { // object — a provider decides whether a session may be acted on by // identity, and a rebuilt look-alike was issued by nobody. const trace: Trace = { prompts: [], agentLookups: [], timeouts: [] }; - const { result } = yield* runDoc( + const { result } = yield* run( [ '', ' ', @@ -728,7 +742,7 @@ describe("Tier AF — registered defaults a document can replace", () => { it("AF25: a self-closing Session resolves one and renders nothing", function* () { const trace: Trace = { prompts: [], agentLookups: [], timeouts: [] }; - const { output, result } = yield* runDoc( + const { output, result } = yield* run( ["BEFORE", "", '', "", "AFTER", ""].join("\n"), { trace }, ); @@ -786,8 +800,9 @@ describe("Tier AF — registered defaults a document can replace", () => { }); describe("Tier AF — the scoped prompt-failure policy", () => { + beforeEach(journalBeforeEach(OWNER)); it("AF16: without a policy a failing prompt is collected, not fatal", function* () { - const { output, result } = yield* runDoc('\n\nAFTER\n', { fail: true }); + const { output, result } = yield* run('\n\nAFTER\n', { fail: true }); expect(result.ok).toBe(false); // aggregated at completion, as always expect(output).toContain("AFTER"); // but the document ran on @@ -795,7 +810,7 @@ describe("Tier AF — the scoped prompt-failure policy", () => { it("AF17: a policy that says yes ends the document at the prompt", function* () { let asked = 0; - const { output } = yield* runDoc('\n\nAFTER\n', { + const { output } = yield* run('\n\nAFTER\n', { fail: true, // deno-lint-ignore require-yield policy: function* () { @@ -810,7 +825,7 @@ describe("Tier AF — the scoped prompt-failure policy", () => { it("AF18: an explicit throwOnError wins without consulting the policy", function* () { let asked = 0; - const { output } = yield* runDoc('\n\nAFTER\n', { + const { output } = yield* run('\n\nAFTER\n', { fail: true, // deno-lint-ignore require-yield policy: function* () { @@ -824,7 +839,7 @@ describe("Tier AF — the scoped prompt-failure policy", () => { }); it("AF19: a policy that says no leaves the prompt collected", function* () { - const { output } = yield* runDoc('\n\nAFTER\n', { + const { output } = yield* run('\n\nAFTER\n', { fail: true, // deno-lint-ignore require-yield policy: function* () { @@ -837,7 +852,7 @@ describe("Tier AF — the scoped prompt-failure policy", () => { it("AF20: a repository Prompt never consults the policy", function* () { let asked = 0; - const { output } = yield* runDoc("\n\nAFTER\n", { + const { output } = yield* run("\n\nAFTER\n", { fail: true, files: { "Prompt.md": "LOCAL\n" }, // deno-lint-ignore require-yield @@ -854,30 +869,31 @@ describe("Tier AF — the scoped prompt-failure policy", () => { }); describe("Tier AF — prompt timeouts", () => { + beforeEach(journalBeforeEach(OWNER)); it("AF21: a prompt nobody bounded carries no timeout", function* () { - const { trace } = yield* runDoc('\n'); + const { trace } = yield* run('\n'); expect(trace.timeouts).toEqual([undefined]); }); it("AF22a: a prompt inherits the subtree default an installs", function* () { - const { trace } = yield* runDoc('\n', { promptTimeout: 500 }); + const { trace } = yield* run('\n', { promptTimeout: 500 }); expect(trace.timeouts).toEqual([500]); }); it("AF22b: a prompt's own timeout outranks the inherited one", function* () { - const { trace } = yield* runDoc('\n', { + const { trace } = yield* run('\n', { promptTimeout: 500, }); expect(trace.timeouts).toEqual([250]); }); it("AF22: a prompt's own timeout is the duration it declares", function* () { - const { trace } = yield* runDoc('\n'); + const { trace } = yield* run('\n'); expect(trace.timeouts).toEqual([250]); }); it("AF23: a malformed prompt timeout refuses instead of prompting", function* () { - const { result, trace } = yield* runDoc('\n'); + const { result, trace } = yield* run('\n'); expect(result.ok).toBe(false); expect(result.ok === false && result.error.message).toContain("must be a duration"); expect(trace.timeouts).toEqual([]); diff --git a/packages/agent/tests/agent-stack.test.ts b/packages/agent/tests/agent-stack.test.ts index ffd2b307e..166db5f72 100644 --- a/packages/agent/tests/agent-stack.test.ts +++ b/packages/agent/tests/agent-stack.test.ts @@ -6,15 +6,16 @@ * provider factories — no subprocess, no real provider. CLI precedence is * not established here; it is asserted at the CLI boundary. */ -import { describe, it } from "@executablemd/test-support/bdd"; + +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; +import { useTempDirectory } from "@executablemd/test-support/temp"; import { ensure, scoped } from "effection"; import type { Operation, Result } from "effection"; -import { InMemoryStream } from "@executablemd/durable-streams"; +import type { InMemoryStream } from "@executablemd/durable-streams"; import { ensureDir, rm, writeTextFile } from "@effectionx/fs"; -import { randomUUID } from "node:crypto"; import * as path from "node:path"; -import * as os from "node:os"; import { execute } from "../../core/src/execute.ts"; import { registerComponents } from "../../core/src/components/registration.ts"; import { Agent } from "../src/agent-api.ts"; @@ -55,11 +56,14 @@ function recordingFactory(seen: AgentProviderOptions[]): AgentProviderFactory { }; } +const OWNER = "packages/agent/tests/agent-stack.test.ts"; + function* runDoc( doc: string, install: () => Operation, + stream: InMemoryStream, + dir: string, ): Operation<{ output: string; result: Result }> { - const dir = path.join(os.tmpdir(), `xmd-ag-${randomUUID()}`); yield* ensureDir(dir); return yield* scoped(function* () { yield* ensure(() => rm(dir, { recursive: true, force: true })); @@ -67,7 +71,7 @@ function* runDoc( const docPath = path.join(dir, "doc.md"); yield* writeTextFile(docPath, doc); yield* install(); - const execution = yield* execute({ path: docPath, stream: new InMemoryStream() }); + const execution = yield* execute({ path: docPath, stream }); const subscription = yield* execution.output; let next = yield* subscription.next(); while (!next.done) { @@ -129,6 +133,7 @@ const PROVIDER_OPTIONS: AgentProviderOptions = { }; describe("Tier AG — provider registry and permission policies", () => { + beforeEach(journalBeforeEach(OWNER)); it("AG1: a registered name installs and receives the options; an unknown name refuses", function* () { const seen: AgentProviderOptions[] = []; yield* registerAgentProvider("stub", recordingFactory(seen)); @@ -212,6 +217,8 @@ describe("Tier AG — provider registry and permission policies", () => { }); it("AG7: approves inside its body under an enclosing deny", function* () { + const directory = yield* useTempDirectory("xmd-ag-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const outcomes: string[] = []; const { result } = yield* runDoc( ["", "", "", "", "", "", "", ""].join("\n"), @@ -219,12 +226,16 @@ describe("Tier AG — provider registry and permission policies", () => { yield* installAskComponent(outcomes); yield* installAgentComponents(); }, + stream, + directory, ); expect(result.ok).toBe(true); expect(outcomes).toEqual(["reject-once", "allow-once", "reject-once"]); }); it("AG8: denies inside its body under an enclosing approve-all", function* () { + const directory = yield* useTempDirectory("xmd-ag-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const outcomes: string[] = []; const { result } = yield* runDoc( ["", "", "", "", "", "", "", ""].join( @@ -235,25 +246,36 @@ describe("Tier AG — provider registry and permission policies", () => { yield* installApproveAll(); yield* installAgentComponents(); }, + stream, + directory, ); expect(result.ok).toBe(true); expect(outcomes).toEqual(["allow-once", "reject-once", "allow-once"]); }); it("AG9: installAgentComponents seeds the default agent and permission mode", function* () { + const directory = yield* useTempDirectory("xmd-ag-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const seen: AgentProviderOptions[] = []; - const { result } = yield* runDoc('\n', function* () { - yield* registerAgentProvider("rec", recordingFactory(seen)); - yield* installAgentComponents({ - defaultAgent: "seeded-agent", - permissionMode: "approve-reads", - }); - }); + const { result } = yield* runDoc( + '\n', + function* () { + yield* registerAgentProvider("rec", recordingFactory(seen)); + yield* installAgentComponents({ + defaultAgent: "seeded-agent", + permissionMode: "approve-reads", + }); + }, + stream, + directory, + ); expect(result.ok).toBe(true); expect(seen).toEqual([{ defaultAgent: "seeded-agent", permissionMode: "approve-reads" }]); }); it("AG10: overrides inside its body and restores after", function* () { + const directory = yield* useTempDirectory("xmd-ag-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const seen: AgentProviderOptions[] = []; const { result } = yield* runDoc( [ @@ -270,6 +292,8 @@ describe("Tier AG — provider registry and permission policies", () => { permissionMode: "deny-all", }); }, + stream, + directory, ); expect(result.ok).toBe(true); expect(seen.map((options) => options.defaultAgent)).toEqual(["scoped-agent", "seeded-agent"]); @@ -278,6 +302,8 @@ describe("Tier AG — provider registry and permission policies", () => { }); it("AG11: an unknown fails the execution; no body or later content renders", function* () { + const directory = yield* useTempDirectory("xmd-ag-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const seen: AgentProviderOptions[] = []; const { output, result } = yield* runDoc( [ @@ -291,6 +317,8 @@ describe("Tier AG — provider registry and permission policies", () => { yield* registerAgentProvider("rec", recordingFactory(seen)); yield* installAgentComponents({ defaultAgent: "seeded-agent" }); }, + stream, + directory, ); expect(result.ok).toBe(false); expect(output).not.toContain("INSIDE_MARKER"); @@ -299,6 +327,8 @@ describe("Tier AG — provider registry and permission policies", () => { }); it("AG12: without any default agent fails before its body expands", function* () { + const directory = yield* useTempDirectory("xmd-ag-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const seen: AgentProviderOptions[] = []; const { output, result } = yield* runDoc( ['', "INSIDE_MARKER", "", ""].join("\n"), @@ -306,6 +336,8 @@ describe("Tier AG — provider registry and permission policies", () => { yield* registerAgentProvider("rec", recordingFactory(seen)); yield* installAgentComponents(); }, + stream, + directory, ); expect(result.ok).toBe(false); const message = result.ok ? "" : result.error.message; diff --git a/packages/agent/tests/agent-usage.test.ts b/packages/agent/tests/agent-usage.test.ts index 7195db337..c2b527db5 100644 --- a/packages/agent/tests/agent-usage.test.ts +++ b/packages/agent/tests/agent-usage.test.ts @@ -7,8 +7,11 @@ * path, replay without a provider, and direct calls that retain nothing. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; +import { useTempDirectory } from "@executablemd/test-support/temp"; import { InMemoryStream } from "@executablemd/durable-streams"; import type { DurableEvent } from "@executablemd/durable-streams"; import { ensure, scoped, spawn, withResolvers } from "effection"; @@ -261,6 +264,8 @@ function always(plan: TurnPlan): () => TurnPlan { return () => ({ ...plan }); } +const OWNER = "packages/agent/tests/agent-usage.test.ts"; + function* runDocument( doc: string, stream: InMemoryStream, @@ -270,8 +275,8 @@ function* runDocument( direct?: boolean; scanner?: () => SecretScanner; } = {}, + dir = path.join(os.tmpdir(), `xmd-pu-test-${randomUUID()}`), ): Operation<{ result: Result; events: DurableEvent[] }> { - const dir = path.join(os.tmpdir(), `xmd-pu-test-${randomUUID()}`); yield* ensureDir(dir); return yield* scoped(function* () { yield* ensure(() => rm(dir, { recursive: true, force: true })); @@ -359,6 +364,7 @@ function* consume(stream: Stream): Operation { } describe("Tier PU — subscription-local receiver", () => { + beforeEach(journalBeforeEach(OWNER)); it("PU-B1: a sealed destination ignores reports; an unreadable report keeps only its provenance", function* () { const destination = promptUsageDestination(); destination.report(versioned("first")); @@ -436,6 +442,8 @@ describe("Tier PU — subscription-local receiver", () => { }); it("PU-B3: a direct Agent.prompt() gets an ephemeral receiver and lends nothing to the next Prompt", function* () { + const directory = yield* useTempDirectory("xmd-pu-test-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const trace = newTrace(); const receivers: (PromptUsageReporter | undefined)[] = []; const plans: TurnPlan[] = [ @@ -451,9 +459,10 @@ describe("Tier PU — subscription-local receiver", () => { ]; const { result, events } = yield* runDocument( "\n\nsilent\n\nmeasured\n", - new InMemoryStream(), + stream, stubFactory(trace, () => plans.shift() ?? {}), { direct: true }, + directory, ); expect(result.ok).toBe(true); expect(receivers.length).toBe(3); @@ -468,12 +477,15 @@ describe("Tier PU — subscription-local receiver", () => { }); it("PU-B4: a registered provider's delivered coordinator keeps the optional receiver", function* () { + const directory = yield* useTempDirectory("xmd-pu-test-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const trace = newTrace(); const { result, events } = yield* runDocument( '\n\none\n\n\n', - new InMemoryStream(), + stream, stubFactory(trace, always({ usage: () => versioned("registered") })), { register: true }, + directory, ); expect(result.ok).toBe(true); expect(trace.acquisitions).toBe(1); @@ -484,6 +496,7 @@ describe("Tier PU — subscription-local receiver", () => { }); describe("Tier PU — canonical retention and replay", () => { + beforeEach(journalBeforeEach(OWNER)); it("PU-P1: an authored Prompt retains usage, and a completed replay restores it without a turn", function* () { const stream = new InMemoryStream(); const live = newTrace(); @@ -512,6 +525,8 @@ describe("Tier PU — canonical retention and replay", () => { }); it("PU-P2: an acknowledged failed turn keeps what it reported, and no checkpoint", function* () { + const directory = yield* useTempDirectory("xmd-pu-test-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const failing = sampleUsage("failed"); const partial: PromptUsage = { ...failing, @@ -523,8 +538,10 @@ describe("Tier PU — canonical retention and replay", () => { }; const { events } = yield* runDocument( "fails\n", - new InMemoryStream(), + stream, stubFactory(newTrace(), always({ status: "failed", usage: () => partial })), + {}, + directory, ); const record = parsePromptRecord(promptValues(events)[0]); expect(record?.status).toBe("failed"); diff --git a/packages/agent/tests/root-provider.test.ts b/packages/agent/tests/root-provider.test.ts index 43c21991b..3d2bd2051 100644 --- a/packages/agent/tests/root-provider.test.ts +++ b/packages/agent/tests/root-provider.test.ts @@ -7,15 +7,16 @@ * fake provider is an Effection resource whose finalizer is driven by scope * teardown — never a manual teardown call or a Promise. */ -import { describe, it } from "@executablemd/test-support/bdd"; + +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; +import { useTempDirectory } from "@executablemd/test-support/temp"; import { ensure, Ok, scoped } from "effection"; import type { Operation, Result, Stream } from "effection"; -import { InMemoryStream } from "@executablemd/durable-streams"; +import type { InMemoryStream } from "@executablemd/durable-streams"; import { ensureDir, rm, writeTextFile } from "@effectionx/fs"; -import { randomUUID } from "node:crypto"; import * as path from "node:path"; -import * as os from "node:os"; import { execute } from "../../core/src/execute.ts"; import { Agent } from "../src/agent-api.ts"; import { sessionOf } from "../src/session-use.ts"; @@ -130,15 +131,20 @@ function stubStream( }; } -function* runDoc(doc: string): Operation<{ output: string; result: Result }> { - const dir = path.join(os.tmpdir(), `xmd-rp-test-${randomUUID()}`); +const OWNER = "packages/agent/tests/root-provider.test.ts"; + +function* runDoc( + doc: string, + stream: InMemoryStream, + dir: string, +): Operation<{ output: string; result: Result }> { 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 execution = yield* execute({ path: docPath, stream: new InMemoryStream() }); + const execution = yield* execute({ path: docPath, stream }); const subscription = yield* execution.output; let next = yield* subscription.next(); while (!next.done) { @@ -152,10 +158,13 @@ function* runDoc(doc: string): Operation<{ output: string; result: Result const OPTIONS: AgentProviderOptions = { defaultAgent: "a", permissionMode: "deny-all" }; describe("Tier RP — root-provider lifecycle", () => { + beforeEach(journalBeforeEach(OWNER)); it("RP1: provider is active during execution; its finalizer runs once, before completion settles", function* () { + const directory = yield* useTempDirectory("xmd-rp-test-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const { factory, state } = createProvider(); yield* installAgentComponents({ rootProvider: { factory, options: OPTIONS } }); - const { output, result } = yield* runDoc('\n'); + const { output, result } = yield* runDoc('\n', stream, directory); expect(result.ok).toBe(true); expect(output).toContain("[hi]"); expect(state.activeDuringPrompt).toBe(true); @@ -165,10 +174,12 @@ describe("Tier RP — root-provider lifecycle", () => { }); it("RP2: a teardown failure still closes rendered output but makes completion an Err", function* () { + const directory = yield* useTempDirectory("xmd-rp-test-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const boom = new Error("teardown boom"); const { factory } = createProvider({ teardownError: boom }); yield* installAgentComponents({ rootProvider: { factory, options: OPTIONS } }); - const { output, result } = yield* runDoc('\n'); + const { output, result } = yield* runDoc('\n', stream, directory); expect(output).toContain("[hi]"); expect(result.ok).toBe(false); if (!result.ok) { @@ -181,6 +192,8 @@ describe("Tier RP — root-provider lifecycle", () => { // the caller receives is the failure the document earned, because a completion // policy adds and never replaces. it("RP5: a failed document keeps its own failure even when cleanup also fails", function* () { + const directory = yield* useTempDirectory("xmd-rp-test-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const boom = new Error("teardown boom"); const { factory, state } = createProvider({ teardownError: boom, extraFinalizer: true }); yield* installAgentComponents({ rootProvider: { factory, options: OPTIONS } }); @@ -190,6 +203,8 @@ describe("Tier RP — root-provider lifecycle", () => { // having failed. const { result } = yield* runDoc( ["", "", "```bash exec", "exit 3", "```", "", "", ""].join("\n"), + stream, + directory, ); expect(result.ok).toBe(false); @@ -204,6 +219,8 @@ describe("Tier RP — root-provider lifecycle", () => { }); it("RP3: a prompt failure plus a teardown failure aggregate — primary first, every cleanup runs", function* () { + const directory = yield* useTempDirectory("xmd-rp-test-"); + const stream = yield* useJournalStream(path.join(directory, "doc.md")); const boom = new Error("teardown boom"); const { factory, state } = createProvider({ teardownError: boom, @@ -211,7 +228,7 @@ describe("Tier RP — root-provider lifecycle", () => { fail: true, }); yield* installAgentComponents({ rootProvider: { factory, options: OPTIONS } }); - const { result } = yield* runDoc('\n'); + const { result } = yield* runDoc('\n', stream, directory); expect(result.ok).toBe(false); if (!result.ok) { expect(result.error).toBeInstanceOf(AggregateError); diff --git a/packages/cli/src/test-diagnostics.ts b/packages/cli/src/test-diagnostics.ts index 83d79797c..acf72adbe 100644 --- a/packages/cli/src/test-diagnostics.ts +++ b/packages/cli/src/test-diagnostics.ts @@ -60,7 +60,7 @@ export interface TestDiagnostics { * the file each execution saves is named by its own position in this * command, so two executions never share one. */ - execution(kind: "root" | "child", source: string): ExecutionDiagnostics; + execution(kind: "root" | "child", source: string, profile?: string): ExecutionDiagnostics; /** The first failure to deliver a progress line, or `Ok` when none failed. */ delivery(): Result; /** Counts recorded by root documents. Nested execution results are evidence. */ @@ -160,12 +160,12 @@ function testDiagnosticsAt( const counts = { root: 0, child: 0 }; const results = { pass: 0, fail: 0 }; yield* provide({ - execution(kind, source) { + execution(kind, source, profile) { const ordinal = ++counts[kind]; const stem = join(directory, `${kind}-${ordinal}`); const file = `${stem}.jsonl`; const displayedSource = shown(source); - const named = `${kind} ${ordinal} ${displayedSource}`; + const named = `${kind} ${ordinal} ${displayedSource}${profile === undefined ? "" : ` (${profile})`}`; const context = kind === "root" ? "" : `${named}: `; return { observe: () => @@ -195,6 +195,7 @@ function testDiagnosticsAt( kind, ordinal, source: displayedSource, + profile, }) + "\n", ); } diff --git a/packages/cli/tests/agent-adapters.test.ts b/packages/cli/tests/agent-adapters.test.ts index ab4267277..0271c0e86 100644 --- a/packages/cli/tests/agent-adapters.test.ts +++ b/packages/cli/tests/agent-adapters.test.ts @@ -1,3 +1,4 @@ +import { Writable } from "node:stream"; import { mkdtemp, chmod, realpath } from "node:fs/promises"; import { cliBase } from "@executablemd/test-support/launch"; import { fileURLToPath } from "node:url"; @@ -19,14 +20,15 @@ import { fileURLToPath } from "node:url"; import { describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; -import { exists, rm, writeTextFile } from "@effectionx/fs"; +import { exists, readdir, readTextFile, rm, writeTextFile } from "@effectionx/fs"; +import { useTempDirectory } from "@executablemd/test-support/temp"; import { randomUUID } from "node:crypto"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { API, exec, useQuietProcessOutput } from "@executablemd/runtime"; import { createEmbeddedAdapters } from "@executablemd/acp/embedded-adapters"; import type { EmbeddedAdapters } from "@executablemd/acp/embedded-adapters"; -import { useScope, scoped, ensure, until } from "effection"; +import { useScope, scoped, ensure, lift, until } from "effection"; import type { Operation } from "effection"; import { @@ -39,6 +41,13 @@ import { planWriterDependencies } from "../src/plan-writer-profile.ts"; import type { PlanWriterProviderInputs } from "../src/plan-writer-profile.ts"; import { runPlan } from "../src/plan.ts"; import { AGENT, createPlanHarness, useWorkingDirectory } from "./support/plan-harness.ts"; +import { ASSEMBLY_PROFILES, ASSEMBLY_SOURCE } from "./support/assembly-profiles.ts"; +import { + checkSubprocessJournals, + parseSubprocessJournal, + readSubprocessSnapshot, +} from "../../test-support/subprocess-journal.ts"; +import type { SubprocessJournal } from "../../test-support/subprocess-journal.ts"; /** The two agents this build carries a patched snapshot for. */ const EMBEDDED = ["codex", "claude"] as const; @@ -250,7 +259,28 @@ describe("Tier AE — embedded adapters on the run and plan paths", () => { }); }); -function* assemblyInputs(agent: "claude" | "codex"): Operation { +const ASSEMBLY_OWNER = "packages/cli/tests/agent-adapters.test.ts"; +const ASSEMBLY_SUITE = "HC host selection across command assemblies"; + +function checkEnvironment(environmentPreserved: unknown): void { + expect(environmentPreserved).toBe(true); +} + +function assemblyJournals(value: unknown): SubprocessJournal[] { + return ASSEMBLY_PROFILES.map((profile, index) => ({ + source: ASSEMBLY_SOURCE, + profile, + snapshot: readSubprocessSnapshot(value, index), + })); +} + +function* assemblyInputs( + agent: "claude" | "codex", + name: string, + verify = checkEnvironment, + inspect?: (journals: readonly SubprocessJournal[]) => void, + options: { oversizedPlan?: boolean; sink?: Writable } = {}, +): Operation { const root = yield* until(realpath(yield* until(mkdtemp(join(tmpdir(), "xmd assemblies "))))); yield* ensure(() => rm(root, { recursive: true, force: true })); const host = join(root, agent); @@ -265,6 +295,8 @@ function* assemblyInputs(agent: "claude" | "codex"): Operation { fileURLToPath(new URL("./fixtures/agent-assemblies.ts", import.meta.url)), root, agent, + "--capture-journal", + ...(options.oversizedPlan ? ["--oversized-plan"] : []), ], env: { [variable]: host }, }); @@ -272,32 +304,147 @@ function* assemblyInputs(agent: "claude" | "codex"): Operation { if (result.exitCode !== 0) { throw new Error(result.stderr); } - const { results: rows, environmentPreserved } = JSON.parse(result.stdout); - expect(environmentPreserved).toBe(true); - expect(rows.map((row: { profile: string }) => row.profile)).toEqual([ - "run", - "plan", - "repl", - "custom", - "other", - ]); - for (const row of rows) { - expect(row.inputs).toHaveLength(2); - for (const input of row.inputs) { - if (["custom", "other"].includes(row.profile)) { - expect(input.environment).toBeUndefined(); - } else { - expect(input.environment).toEqual({ [variable]: host }); - expect(input.command).toContain(root); - expect(input.command).not.toContain("npx"); - } - } + let output; + try { + output = JSON.parse(result.stdout); + } catch { + throw new Error("the assembly fixture result is invalid JSON"); } + const { results: rows, environmentPreserved, journals: rawJournals } = output; + const journals = assemblyJournals(rawJournals); + yield* checkSubprocessJournals( + { owner: ASSEMBLY_OWNER, suite: ASSEMBLY_SUITE, name }, + journals, + lift(() => { + inspect?.(journals); + verify(environmentPreserved); + expect(rows.map((row: { profile: string }) => row.profile)).toEqual(ASSEMBLY_PROFILES); + for (const row of rows) { + expect(row.inputs).toHaveLength(2); + for (const input of row.inputs) { + if (["custom", "other"].includes(row.profile)) { + expect(input.environment).toBeUndefined(); + } else { + expect(input.environment).toEqual({ [variable]: host }); + expect(input.command).toContain(root); + expect(input.command).not.toContain("npx"); + } + } + } + }), + options.sink, + ); } -describe("HC host selection across command assemblies", () => { - it("HC3/HC4: run, Plan and REPL deliver the host selection to probe and live runtimes", () => - assemblyInputs("claude")); - it("H3: run, Plan and REPL deliver the host Codex selection to probe and live runtimes", () => - assemblyInputs("codex")); +describe(ASSEMBLY_SUITE, () => { + const claude = + "HC3/HC4: run, Plan and REPL deliver the host selection to probe and live runtimes"; + const codex = + "H3: run, Plan and REPL deliver the host Codex selection to probe and live runtimes"; + it(claude, () => assemblyInputs("claude", claude)); + it(codex, () => assemblyInputs("codex", codex)); + + for (const oversizedPlan of [false, true]) { + const witness = oversizedPlan + ? "keeps sibling journals when one joined assembly profile exceeds its budget" + : "attributes each assembly profile when the joined parent assertion fails"; + it(witness, function* () { + const messages: string[] = []; + const sink = new Writable({ + write(chunk, _encoding, callback) { + messages.push(String(chunk)); + callback(); + }, + }); + const root = yield* useTempDirectory("xmd-assembly-journals-"); + function run( + artifacts: string, + verify: typeof checkEnvironment, + inspect?: Parameters[3], + ) { + return scoped(function* () { + yield* API.Env.around({ + *env([name], next) { + return name === "XMD_TEST_ARTIFACTS" ? artifacts : yield* next(name); + }, + }); + yield* assemblyInputs("claude", witness, verify, inspect, { oversizedPlan, sink }); + }); + } + + const passed = join(root, "passed", ".xmd", "test-artifacts"); + yield* run(passed, checkEnvironment, (journals) => { + expect(journals).toHaveLength(ASSEMBLY_PROFILES.length); + }); + expect(yield* exists(passed)).toBe(false); + expect(messages).toEqual([]); + + const failed = join(root, "failed", ".xmd", "test-artifacts"); + let assertion: unknown; + let caught: unknown; + let received: readonly SubprocessJournal[] = []; + try { + yield* run( + failed, + (value) => { + checkEnvironment(value); + try { + checkEnvironment(false); + } catch (error) { + assertion = error; + throw error; + } + }, + (journals) => { + received = journals; + }, + ); + } catch (error) { + caught = error; + } + expect(assertion).toBeInstanceOf(Error); + expect(caught).toBe(assertion); + expect(received).toHaveLength(ASSEMBLY_PROFILES.length); + const invocations = yield* readdir(failed); + expect(invocations).toHaveLength(1); + const directory = join(failed, invocations[0]); + const saved = received.flatMap(({ profile, snapshot }, index) => + typeof snapshot === "string" ? [] : [{ profile, events: snapshot, ordinal: index + 1 }], + ); + expect(saved.map(({ profile }) => profile)).toEqual( + ASSEMBLY_PROFILES.filter((profile) => !oversizedPlan || profile !== "plan"), + ); + const omissions = messages.filter((message) => message.includes("journal not saved")); + expect(omissions).toEqual( + oversizedPlan + ? [ + "[xmd test] journal not saved for child 2 assembly.md (plan): snapshot exceeds journal budget\n", + ] + : [], + ); + const expectedFiles = saved.flatMap(({ ordinal }) => [ + `child-${ordinal}.case.json`, + `child-${ordinal}.jsonl`, + ]); + expect((yield* readdir(directory)).sort()).toEqual(expectedFiles.sort()); + for (const journal of saved) { + const { ordinal, profile } = journal; + const stem = join(directory, `child-${ordinal}`); + expect(JSON.parse(yield* readTextFile(`${stem}.case.json`))).toEqual({ + owner: ASSEMBLY_OWNER, + suite: ASSEMBLY_SUITE, + name: witness, + kind: "child", + ordinal, + source: ASSEMBLY_SOURCE, + profile, + }); + const events = parseSubprocessJournal( + (yield* readTextFile(`${stem}.jsonl`)).trimEnd().split("\n"), + ); + expect(events).toEqual(journal.events); + expect(events.at(-1)).toMatchObject({ type: "close", coroutineId: "root" }); + } + }); + } }); diff --git a/packages/cli/tests/fixtures/agent-assemblies.ts b/packages/cli/tests/fixtures/agent-assemblies.ts index 0d8bdeee8..b6b6993fa 100644 --- a/packages/cli/tests/fixtures/agent-assemblies.ts +++ b/packages/cli/tests/fixtures/agent-assemblies.ts @@ -1,4 +1,4 @@ -import { InMemoryStream } from "@executablemd/durable-streams"; +import { InMemoryStream, serializeDurableEvent } from "@executablemd/durable-streams"; import { rm } from "@effectionx/fs"; import { main, scoped, useScope } from "effection"; import { useHostFiles } from "@executablemd/runtime"; @@ -14,8 +14,12 @@ import { planWriterDependencies } from "../../src/plan-writer-profile.ts"; import { assembleReplProfile } from "../../src/repl-profile.ts"; import { NO_PLUGINS } from "../../src/plugin-host.ts"; import { createFakeAcp, makeStore, makeRegistry } from "../support/fake-acp.ts"; +import { boundedJournalResult } from "../../../test-support/subprocess-journal.ts"; +import { ASSEMBLY_PROFILES, ASSEMBLY_SOURCE } from "../support/assembly-profiles.ts"; + const root = process.argv[2]!; const agent = process.argv[3] === "codex" ? "codex" : "claude"; +const capture = process.argv[4] === "--capture-journal"; const variable = agent === "codex" ? "CODEX_PATH" : "CLAUDE_CODE_EXECUTABLE"; const environment = { ...process.env }; main(function* () { @@ -35,9 +39,16 @@ main(function* () { command: ReturnType; }[]; }[] = []; - for (const profile of ["run", "plan", "repl", "custom", "other"]) { + const journals: string[][] = []; + for (const profile of ASSEMBLY_PROFILES) { const fake = createFakeAcp(); - fake.script({ reply: "assembled" }); + fake.script({ + reply: + capture && process.argv[5] === "--oversized-plan" && profile === "plan" + ? "assembled ".repeat(2_000) + : "assembled", + }); + const stream = new InMemoryStream(); yield* scoped(function* () { const host = yield* useScope(); if (profile === "custom") { @@ -70,8 +81,8 @@ main(function* () { yield* collect( yield* executeInstalled( { - ...retainedSource("assembly.md", "assembly"), - stream: new InMemoryStream(), + ...retainedSource(ASSEMBLY_SOURCE, "assembly"), + stream, }, [], ), @@ -84,9 +95,13 @@ main(function* () { })), }); }); + if (capture) { + journals.push((yield* stream.readAll()).map(serializeDurableEvent)); + } } const environmentPreserved = [ ...new Set([...Object.keys(environment), ...Object.keys(process.env)]), ].every((key) => environment[key] === process.env[key]); - console.log(JSON.stringify({ results, environmentPreserved })); + const result = { results, environmentPreserved }; + console.log(capture ? boundedJournalResult(result, journals) : JSON.stringify(result)); }); diff --git a/packages/cli/tests/prompt-usage-hosts.test.ts b/packages/cli/tests/prompt-usage-hosts.test.ts index 72f239071..f2d576134 100644 --- a/packages/cli/tests/prompt-usage-hosts.test.ts +++ b/packages/cli/tests/prompt-usage-hosts.test.ts @@ -9,7 +9,9 @@ * actually retains and reads back, not that a dependency was constructed. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { InMemoryStream } from "@executablemd/durable-streams"; import type { DurableEvent, DurableStream } from "@executablemd/durable-streams"; @@ -44,6 +46,9 @@ import { records, useHarness } from "./fixtures/repl/issue-883/sidekick-harness. import { parseDurableEvent } from "@executablemd/durable-streams"; import { sleep } from "effection"; +const OWNER = "packages/cli/tests/prompt-usage-hosts.test.ts"; +const PROMPT_SOURCE = "hosts.md"; + const HOST: PromptUsageHost = { host: "deno-source", xmd: { version: "0.0.0-hosts" } }; const RETAINED = { @@ -110,7 +115,7 @@ function usageIn(events: readonly DurableEvent[]): (PromptUsage | undefined)[] { function* promptOnce(stream: DurableStream): Operation { yield* collect( yield* executeInstalled( - { ...retainedSource("hosts.md", "measure me"), stream }, + { ...retainedSource(PROMPT_SOURCE, "measure me"), stream }, [{ components: agentIdentityComponents() }], ), ); @@ -132,12 +137,13 @@ function expectRetained(usage: PromptUsage | undefined, adapterVersion: string): } describe("Tier HU — Prompt usage through the host assemblies", () => { + beforeEach(journalBeforeEach(OWNER)); it("HU1: an ordinary run's agent stack retains usage in the run's own history", function* () { + const stream = yield* useJournalStream(PROMPT_SOURCE); yield* useHostFiles(); const root = yield* useRoot(); const fake = createFakeAcp(); fake.script({ reply: "ok", usage: RETAINED }); - const stream = new InMemoryStream(); yield* scoped(function* () { const stack = stackAt(root); yield* installAgentProviderStack(stack, leaf(stack, fake)); @@ -148,11 +154,11 @@ describe("Tier HU — Prompt usage through the host assemblies", () => { }); it("HU2: the Plan writer's dependencies retain usage the same way", function* () { + const stream = yield* useJournalStream(PROMPT_SOURCE); yield* useHostFiles(); const root = yield* useRoot(); const fake = createFakeAcp(); fake.script({ reply: "ok", usage: RETAINED }); - const stream = new InMemoryStream(); yield* scoped(function* () { const stack = stackAt(root); const host = yield* useScope(); @@ -166,11 +172,11 @@ describe("Tier HU — Prompt usage through the host assemblies", () => { }); it("HU3: the REPL profile, through its headless wrapper, retains usage", function* () { + const stream = yield* useJournalStream(PROMPT_SOURCE); yield* useHostFiles(); const root = yield* useRoot(); const fake = createFakeAcp(); fake.script({ reply: "ok", usage: RETAINED }); - const stream = new InMemoryStream(); yield* scoped(function* () { const stack = stackAt(root); yield* assembleReplProfile(stack, NO_PLUGINS, { @@ -251,11 +257,11 @@ describe("Tier HU — Prompt usage through the host assemblies", () => { }); it("HU6: a host that states no usage facts collects none, and runs the same turn", function* () { + const stream = yield* useJournalStream(PROMPT_SOURCE); yield* useHostFiles(); const root = yield* useRoot(); const fake = createFakeAcp(); fake.script({ reply: "ok", usage: RETAINED }); - const stream = new InMemoryStream(); yield* scoped(function* () { const stack: AgentStack = { ...stackAt(root), diff --git a/packages/cli/tests/repl-execution.test.ts b/packages/cli/tests/repl-execution.test.ts index 9ed0feceb..79aba34dc 100644 --- a/packages/cli/tests/repl-execution.test.ts +++ b/packages/cli/tests/repl-execution.test.ts @@ -15,7 +15,7 @@ import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; -import { useTempFileCompiler } from "@executablemd/core"; +import { INLINE_SOURCE_PATH, useTempFileCompiler } from "@executablemd/core"; import { InMemoryStream, serializeDurableEvent } from "@executablemd/durable-streams"; import type { DurableEvent } from "@executablemd/durable-streams"; import { createContext, ensure, race, scoped, sleep, spawn, until } from "effection"; @@ -244,10 +244,20 @@ function captureReferenceEntry( body: (holder: ReplExecution) => Operation, sink: DeliverySink = process.stderr, suite = SUBMIT_SUITE, +): Operation { + return captureEntry(suite, name, REFERENCE_PATH, body, sink); +} + +function captureEntry( + suite: string, + name: string, + source: string, + body: (holder: ReplExecution) => Operation, + sink: DeliverySink = process.stderr, ): Operation { return captureTestJournal( { owner: OWNER, suite, name }, - REFERENCE_PATH, + source, (stream) => body({ id: "kf39sla2", stream }), { sink, @@ -257,7 +267,7 @@ function captureReferenceEntry( throw partition.error; } if (partition.value.length > 1) { - throw new Error("reference execution holds more than one admitted entry"); + throw new Error("fixture execution holds more than one admitted entry"); } return partition.value[0]?.events ?? []; }, @@ -1161,7 +1171,9 @@ describe("REPL execution: one execution's file", () => { }); }); -describe("REPL execution: the selection each entry reads", () => { +const SELECTION_SUITE = "REPL execution: the selection each entry reads"; + +describe(SELECTION_SUITE, () => { beforeAll(() => useTempFileCompiler()); /** @@ -1173,30 +1185,34 @@ describe("REPL execution: the selection each entry reads", () => { */ const DESCRIBING = '\n'; - it("X1: the session's selection is the entry's default, and a host's own is not replaced", function* () { + const selection = + "X1: the session's selection is the entry's default, and a host's own is not replaced"; + it(selection, function* () { // Control: no host states a selection, so the entry runs under the one the // command supplies and the fragment is admitted. - const ordinary = opened( - yield* submitReplEntry({ ...options(execution()), source: DESCRIBING }), - ); - const settled = yield* ordinary.join(); - expect(settled.ok).toBe(true); - expect(ordinary.model.terminal?.status).toBe("ok"); + yield* captureEntry(SELECTION_SUITE, selection, INLINE_SOURCE_PATH, function* (holder) { + const ordinary = opened(yield* submitReplEntry({ ...options(holder), source: DESCRIBING })); + const settled = yield* ordinary.join(); + expect(settled.ok).toBe(true); + expect(ordinary.model.terminal?.status).toBe("ok"); + }); // The same entry, where a host installation states its own. It is defined, // narrower, and nearer than the session's default — so it is what core // reads, and the same fragment is refused for the name it writes. - const narrowed = opened( - yield* submitReplEntry({ - ...options(execution()), - source: DESCRIBING, - installations: [selecting({ Glob: ["self-closing"] })], - }), - ); - const refused = yield* narrowed.join(); - expect(refused.ok).toBe(false); - expect(refused.ok ? "" : refused.error.message).toContain("did not admit"); - expect(narrowed.model.terminal?.status).toBe("err"); + yield* captureEntry(SELECTION_SUITE, selection, INLINE_SOURCE_PATH, function* (holder) { + const narrowed = opened( + yield* submitReplEntry({ + ...options(holder), + source: DESCRIBING, + installations: [selecting({ Glob: ["self-closing"] })], + }), + ); + const refused = yield* narrowed.join(); + expect(refused.ok).toBe(false); + expect(refused.ok ? "" : refused.error.message).toContain("did not admit"); + expect(narrowed.model.terminal?.status).toBe("err"); + }); }); it("X1: a host's defined empty selection refuses the entry before its body", function* () { diff --git a/packages/cli/tests/support/assembly-profiles.ts b/packages/cli/tests/support/assembly-profiles.ts new file mode 100644 index 000000000..7a73b967b --- /dev/null +++ b/packages/cli/tests/support/assembly-profiles.ts @@ -0,0 +1,2 @@ +export const ASSEMBLY_PROFILES = ["run", "plan", "repl", "custom", "other"] as const; +export const ASSEMBLY_SOURCE = "assembly.md"; diff --git a/packages/cli/tests/upgrade-output.test.ts b/packages/cli/tests/upgrade-output.test.ts index 7604375e0..74bc4b984 100644 --- a/packages/cli/tests/upgrade-output.test.ts +++ b/packages/cli/tests/upgrade-output.test.ts @@ -11,7 +11,7 @@ * runner and the document's shape, not the host: Tier UH owns the real lock, * bytes and rename. */ -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 { readTextFile } from "@effectionx/fs"; import { fileURLToPath } from "node:url"; @@ -31,9 +31,17 @@ import { UPGRADE_REPLACE_RETURNS, UPGRADE_VERIFY_RETURNS, } from "../src/compiled-upgrade.ts"; -import { runUpgrade } from "../src/upgrade.ts"; +import { runUpgrade, UPGRADE_COMMAND_IDENTITY } from "../src/upgrade.ts"; import type { UpgradeAssembly, UpgradeConsumer } from "../src/upgrade.ts"; +import { + captureTestJournal, + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; + +const OWNER = "packages/cli/tests/upgrade-output.test.ts"; + const INSTALLED = "0.10.2"; const RELEASE = "v0.11.0"; const EXECUTABLE = "/usr/local/bin/xmd"; @@ -147,7 +155,7 @@ function assemblyFor(phases: Phases): UpgradeAssembly { }; } -function install(consume: UpgradeConsumer, phases: Phases = {}) { +function install(stream: InMemoryStream, consume: UpgradeConsumer, phases: Phases = {}) { return runUpgrade({ command: { requestedTag: null, @@ -156,15 +164,17 @@ function install(consume: UpgradeConsumer, phases: Phases = {}) { allowPrerelease: false, }, assembly: assemblyFor(phases), - stream: new InMemoryStream(), + stream, consume, }); } describe("Tier UO — the upgrade transcript", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); it("UO1: each milestone reaches the reader before the next phase begins", function* () { + const stream = yield* useJournalStream(UPGRADE_COMMAND_IDENTITY); // The decisive case. A run that buffered everything and wrote it at the end // produces byte-identical output, so the only way to tell streaming from // buffering is to look at what has already arrived while a later phase has @@ -175,6 +185,7 @@ describe("Tier UO — the upgrade transcript", () => { let atReplace = ""; const outcome = yield* install( + stream, // deno-lint-ignore require-yield function* (chunk) { arrived.push(chunk); @@ -217,41 +228,53 @@ describe("Tier UO — the upgrade transcript", () => { expect(soFar().endsWith("\n\n")).toBe(false); }); - it("UO2: every consumer drains the same stream, and nothing is printed twice", function* () { + const identical = "UO2: every consumer drains the same stream, and nothing is printed twice"; + it(identical, function* () { + const stream = yield* useJournalStream(UPGRADE_COMMAND_IDENTITY); // The three shapes the command supports: a terminal writing as it goes, a // pipe writing once at the end, and a test observing. All read the same // `execution.output`, so the bytes cannot differ between them. const streamed: string[] = []; const first = yield* install( + stream, // deno-lint-ignore require-yield function* (chunk) { streamed.push(chunk); }, ); - const buffered: string[] = []; - const second = yield* install( - // deno-lint-ignore require-yield - function* (chunk) { - buffered.push(chunk); - }, - ); + yield* captureTestJournal( + { owner: OWNER, name: identical }, + UPGRADE_COMMAND_IDENTITY, + function* (otherStream) { + const buffered: string[] = []; + const second = yield* install( + otherStream, + // deno-lint-ignore require-yield + function* (chunk) { + buffered.push(chunk); + }, + ); - expect(first.ok).toBe(true); - expect(second.ok).toBe(true); - expect(streamed.join("")).toBe(buffered.join("")); - // More than one chunk, or the progressive claim above would be vacuous. - expect(streamed.length).toBeGreaterThan(1); + expect(first.ok).toBe(true); + expect(second.ok).toBe(true); + expect(streamed.join("")).toBe(buffered.join("")); + // More than one chunk, or the progressive claim above would be vacuous. + expect(streamed.length).toBeGreaterThan(1); - // The completion value is never handed back for printing: a text root's - // value is the text the consumer already has, and returning it would invite - // the caller to say everything twice. - expect(first.ok ? first.value : "unreachable").toBe(undefined); + // The completion value is never handed back for printing: a text root's + // value is the text the consumer already has, and returning it would invite + // the caller to say everything twice. + expect(first.ok ? first.value : "unreachable").toBe(undefined); + }, + ); }); it("UO3: a consumer that fails cancels the run and reports rather than hanging", function* () { + const stream = yield* useJournalStream(UPGRADE_COMMAND_IDENTITY); let replaced = false; const outcome = yield* install( + stream, // deno-lint-ignore require-yield function* (chunk) { if (chunk.includes("Downloaded binary:")) { diff --git a/packages/core/tests/answers-component.test.ts b/packages/core/tests/answers-component.test.ts index 0713bdb90..87250e00e 100644 --- a/packages/core/tests/answers-component.test.ts +++ b/packages/core/tests/answers-component.test.ts @@ -15,7 +15,9 @@ * observable. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { ensure, resource, scoped, until } from "effection"; import type { Operation, Result } from "effection"; @@ -41,6 +43,8 @@ const SCHEMA = '{"type":"object","properties":{"decision":{"type":"string"}},"required":["decision"],' + '"additionalProperties":false}'; +const OWNER = "packages/core/tests/answers-component.test.ts"; + function useWorkspace(): Operation { return resource(function* (provide) { const root = yield* until(realpath(yield* until(mkdtemp(join(tmpdir(), "answers-test-"))))); @@ -178,8 +182,10 @@ function reported(result: Run): string { } describe("Answers: choosing by template", () => { + beforeEach(journalBeforeEach(OWNER)); it("answers a nested component's elicitation with no host provider installed", function* () { const workspace = yield* useWorkspace(); + const stream = yield* useJournalStream(join(workspace, "doc.md")); yield* writeGate(workspace); const result = yield* run( @@ -192,6 +198,7 @@ describe("Answers: choosing by template", () => { "", "", ].join("\n"), + { stream }, ); expect(result.failure).toBe(undefined); @@ -200,7 +207,7 @@ describe("Answers: choosing by template", () => { it("matches anything when the matcher has no template", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -214,6 +221,7 @@ describe("Answers: choosing by template", () => { "", "", ].join("\n"), + { stream }, ); expect(result.output).toContain("Got: catch-all"); @@ -221,7 +229,7 @@ describe("Answers: choosing by template", () => { it("reads a multiline template from children", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -237,6 +245,7 @@ describe("Answers: choosing by template", () => { "", "", ].join("\n"), + { stream }, ); expect(result.output).toContain("Got: deployed"); @@ -245,7 +254,7 @@ describe("Answers: choosing by template", () => { /** `{?name}` constrains position but carries nothing into the answer. */ it("uses a wildcard hole to constrain without binding", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -259,6 +268,7 @@ describe("Answers: choosing by template", () => { "", "", ].join("\n"), + { stream }, ); expect(result.output).toContain("Got: yes"); @@ -266,7 +276,7 @@ describe("Answers: choosing by template", () => { it("does not match when the literal text around a hole differs", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -278,6 +288,7 @@ describe("Answers: choosing by template", () => { "", "", ].join("\n"), + { stream }, ); expect(result.failure).toContain("no matcher for this elicitation"); @@ -286,7 +297,7 @@ describe("Answers: choosing by template", () => { /** A `{binding}` in the prop form reaches the engine intact and constrains. */ it("interpolates a binding in the template prop", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -304,6 +315,7 @@ describe("Answers: choosing by template", () => { "", "", ].join("\n"), + { stream }, ); expect(result.output).toContain("Got: bound"); @@ -311,7 +323,7 @@ describe("Answers: choosing by template", () => { it("does not match when the interpolated binding differs", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -327,6 +339,7 @@ describe("Answers: choosing by template", () => { "", "", ].join("\n"), + { stream }, ); expect(result.failure).toContain("no matcher for this elicitation"); @@ -335,6 +348,7 @@ describe("Answers: choosing by template", () => { /** A string answer is JSON text like any other, so it is written quoted. */ it("round-trips a string answer written as quoted JSON", function* () { const workspace = yield* useWorkspace(); + const stream = yield* useJournalStream(join(workspace, "doc.md")); const stringSchema = '{"type":"string"}'; const result = yield* run( @@ -353,6 +367,7 @@ describe("Answers: choosing by template", () => { "", "", ].join("\n"), + { stream }, ); expect(result.failure).toBe(undefined); @@ -361,7 +376,7 @@ describe("Answers: choosing by template", () => { it("reads a value written as captured JSON text", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -375,6 +390,7 @@ describe("Answers: choosing by template", () => { "", "", ].join("\n"), + { stream }, ); expect(result.output).toContain("Got: from-text"); @@ -382,13 +398,14 @@ describe("Answers: choosing by template", () => { }); describe("Answers: selection", () => { + beforeEach(journalBeforeEach(OWNER)); /** * First declared wins, and the consequence is the point: a broad template * above a narrow one shadows it permanently. */ it("takes the first declared matching matcher", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -403,6 +420,7 @@ describe("Answers: selection", () => { "", "", ].join("\n"), + { stream }, ); expect(result.output).toContain("Got: broad"); @@ -410,7 +428,7 @@ describe("Answers: selection", () => { it("lets a narrower matcher win when it is declared first", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -426,6 +444,7 @@ describe("Answers: selection", () => { "", "", ].join("\n"), + { stream }, ); expect(result.output).toContain("Got: narrow then broad"); @@ -434,7 +453,7 @@ describe("Answers: selection", () => { /** A matcher is not consumed by answering: it answers every match it sees. */ it("reuses one matcher across several elicitations", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -450,6 +469,7 @@ describe("Answers: selection", () => { "", "", ].join("\n"), + { stream }, ); expect(result.failure).toBe(undefined); @@ -458,7 +478,7 @@ describe("Answers: selection", () => { it("does not mind a matcher that never fires", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -473,6 +493,7 @@ describe("Answers: selection", () => { "", "", ].join("\n"), + { stream }, ); expect(result.failure).toBe(undefined); @@ -481,7 +502,7 @@ describe("Answers: selection", () => { it("still judges a matched value against the asking component's schema", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -493,6 +514,7 @@ describe("Answers: selection", () => { "", "", ].join("\n"), + { stream }, ); expect(result.failure).toContain(""); @@ -501,9 +523,10 @@ describe("Answers: selection", () => { }); describe("Answers: unmatched elicitations", () => { + beforeEach(journalBeforeEach(OWNER)); it("names the message and every template tried", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -516,6 +539,7 @@ describe("Answers: unmatched elicitations", () => { "", "", ].join("\n"), + { stream }, ); expect(result.failure).toContain("Something nobody wrote a matcher for"); @@ -525,7 +549,7 @@ describe("Answers: unmatched elicitations", () => { it("passes an unmatched elicitation outward when delegate is set", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -540,8 +564,8 @@ describe("Answers: unmatched elicitations", () => { "", "", ].join("\n"), - // deno-lint-ignore require-yield { + stream, outer: function* () { return { decision: "from-host" }; }, @@ -560,7 +584,7 @@ describe("Answers: unmatched elicitations", () => { it("does not reach the outer provider without delegate", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -572,8 +596,8 @@ describe("Answers: unmatched elicitations", () => { "", "", ].join("\n"), - // deno-lint-ignore require-yield { + stream, outer: function* () { return { decision: "from-host" }; }, @@ -586,9 +610,10 @@ describe("Answers: unmatched elicitations", () => { }); describe("Answers: nesting", () => { + beforeEach(journalBeforeEach(OWNER)); it("lets the nearest region answer first", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -606,6 +631,7 @@ describe("Answers: nesting", () => { "", "", ].join("\n"), + { stream }, ); expect(result.output).toContain("Got: inner"); @@ -613,7 +639,7 @@ describe("Answers: nesting", () => { it("reaches the enclosing region when the inner one delegates", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -632,6 +658,7 @@ describe("Answers: nesting", () => { "", "", ].join("\n"), + { stream }, ); expect(result.failure).toBe(undefined); @@ -640,7 +667,7 @@ describe("Answers: nesting", () => { it("stops at the inner region when it does not delegate", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -656,6 +683,7 @@ describe("Answers: nesting", () => { "", "", ].join("\n"), + { stream }, ); expect(result.failure).toContain("no matcher for this elicitation"); @@ -664,7 +692,7 @@ describe("Answers: nesting", () => { /** The region ends with its body, so a later sibling is not answered by it. */ it("stops answering once its body is over", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, SCHEMA_BLOCK + @@ -678,6 +706,7 @@ describe("Answers: nesting", () => { elicits("b", "Outside?"), "", ].join("\n"), + { stream }, ); expect(result.failure).toContain("no elicitation provider configured"); @@ -685,12 +714,14 @@ describe("Answers: nesting", () => { }); describe("Answers: configuration printed errors", () => { + beforeEach(journalBeforeEach(OWNER)); it("refuses an outside an ", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, '\n', + { stream }, ); expect(reported(result)).toContain(" must be a direct child of "); @@ -717,7 +748,7 @@ describe("Answers: configuration printed errors", () => { it("refuses both template forms at once", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, [ @@ -728,6 +759,7 @@ describe("Answers: configuration printed errors", () => { "", "", ].join("\n"), + { stream }, ); expect(reported(result)).toContain( @@ -737,7 +769,7 @@ describe("Answers: configuration printed errors", () => { it("refuses a template that cannot be parsed", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, [ @@ -748,6 +780,7 @@ describe("Answers: configuration printed errors", () => { "", "", ].join("\n"), + { stream }, ); expect(reported(result)).toContain("adjacent capture holes"); @@ -755,10 +788,11 @@ describe("Answers: configuration printed errors", () => { it("refuses a matcher with no value", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, ["", '', "", "body", "", ""].join("\n"), + { stream }, ); expect(reported(result)).toContain('requires a "value" prop'); @@ -771,7 +805,7 @@ describe("Answers: configuration printed errors", () => { */ it("refuses a template written as an expression", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, [ @@ -786,6 +820,7 @@ describe("Answers: configuration printed errors", () => { "", "", ].join("\n"), + { stream }, ); expect(reported(result)).toContain("template must be a literal string prop"); @@ -794,7 +829,7 @@ describe("Answers: configuration printed errors", () => { it("refuses a value that is not JSON", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, [ @@ -805,6 +840,7 @@ describe("Answers: configuration printed errors", () => { "", "", ].join("\n"), + { stream }, ); expect(reported(result)).toContain("value text is not JSON"); @@ -817,7 +853,7 @@ describe("Answers: configuration printed errors", () => { */ it("points a bare string value at the JSON-quoted spelling", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, [ @@ -828,6 +864,7 @@ describe("Answers: configuration printed errors", () => { "", "", ].join("\n"), + { stream }, ); expect(reported(result)).toContain("captured JSON text"); @@ -840,7 +877,7 @@ describe("Answers: configuration printed errors", () => { */ it("does not expand the body when a matcher is malformed", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, [ @@ -851,6 +888,7 @@ describe("Answers: configuration printed errors", () => { "", "", ].join("\n"), + { stream }, ); expect(reported(result)).not.toContain("a body nobody should see"); @@ -858,6 +896,7 @@ describe("Answers: configuration printed errors", () => { }); describe("Answers: reservation", () => { + beforeEach(journalBeforeEach(OWNER)); /** * Repository files named after both constructs, in the directory `run()` * searches — so the only thing keeping them out of the document is the names @@ -875,6 +914,7 @@ describe("Answers: reservation", () => { it("does not let a repository Answers.md stand in for the construct", function* () { const workspace = yield* useWorkspace(); + const stream = yield* useJournalStream(join(workspace, "doc.md")); yield* writeStandIns(workspace); const result = yield* run( @@ -890,6 +930,7 @@ describe("Answers: reservation", () => { "", "", ].join("\n"), + { stream }, ); expect(result.failure).toBe(undefined); @@ -934,11 +975,13 @@ describe("Answers: reservation", () => { it("does not let a repository Answer.md rescue a stray matcher", function* () { const workspace = yield* useWorkspace(); + const stream = yield* useJournalStream(join(workspace, "doc.md")); yield* writeStandIns(workspace); const result = yield* run( workspace, '\n', + { stream }, ); // A reserved name resolves to nothing else wherever it is written, so the diff --git a/packages/core/tests/elicit-component.test.ts b/packages/core/tests/elicit-component.test.ts index 23df48b2b..7593464ae 100644 --- a/packages/core/tests/elicit-component.test.ts +++ b/packages/core/tests/elicit-component.test.ts @@ -10,7 +10,9 @@ * contextual boundary a host uses, so what passes here is what a host gets. */ -import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +import { beforeEach, beforeAll, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { ensure, race, resource, scoped, sleep, suspend, until } from "effection"; import type { Operation } from "effection"; @@ -29,6 +31,8 @@ import type { ElicitationRequest } from "../src/elicitation-api.ts"; import { execute } from "../src/execute.ts"; import { useTempFileCompiler } from "../src/temp-file-compiler.ts"; +const OWNER = "packages/core/tests/elicit-component.test.ts"; + function useWorkspace(): Operation { return resource(function* (provide) { const root = yield* until(realpath(yield* until(mkdtemp(join(tmpdir(), "elicit-test-"))))); @@ -114,12 +118,16 @@ function run( } /** Execute with no provider installed at all. */ -function runWithoutProvider(workspace: string, source: string): Operation { +function runWithoutProvider( + workspace: string, + source: string, + stream: InMemoryStream, +): Operation { return scoped(function* () { const path = join(workspace, "doc.md"); yield* writeTextFile(path, source); try { - const output = yield* collect(yield* execute({ path, stream: new InMemoryStream() })); + const output = yield* collect(yield* execute({ path, stream })); return { output: String(output), requests: [] }; } catch (error) { return { @@ -161,15 +169,17 @@ function document(body: string, schema: string = DECISION_SCHEMA): string { } describe("Elicit: the question", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); it("hands the provider the rendered message and the compiled schema, and nothing else", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, document("Approve the plan?"), constant({ decision: "approve" }), + stream, ); expect(result.failure).toBe(undefined); @@ -182,11 +192,12 @@ describe("Elicit: the question", () => { it("renders nothing of its own", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, document("Approve the plan?"), constant({ decision: "approve" }), + stream, ); expect(result.output).not.toContain("Approve the plan?"); @@ -194,6 +205,7 @@ describe("Elicit: the question", () => { it("accepts a schema as captured JSON text", function* () { const workspace = yield* useWorkspace(); + const stream = yield* useJournalStream(join(workspace, "doc.md")); const source = [ "```js eval", `const schemaText = ${JSON.stringify(DECISION_SCHEMA)};`, @@ -205,7 +217,7 @@ describe("Elicit: the question", () => { "", ].join("\n"); - const result = yield* run(workspace, source, constant({ decision: "reject" })); + const result = yield* run(workspace, source, constant({ decision: "reject" }), stream); expect(result.failure).toBe(undefined); expect(result.requests[0].schema).toEqual(JSON.parse(DECISION_SCHEMA)); @@ -214,12 +226,13 @@ describe("Elicit: the question", () => { }); describe("Elicit: what it refuses", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); it("fails with no provider configured, before asking anything", function* () { const workspace = yield* useWorkspace(); - - const result = yield* runWithoutProvider(workspace, document("Approve?")); + const stream = yield* useJournalStream(join(workspace, "doc.md")); + const result = yield* runWithoutProvider(workspace, document("Approve?"), stream); expect(result.failure?.message).toContain("no elicitation provider configured"); }); @@ -232,6 +245,7 @@ describe("Elicit: what it refuses", () => { */ it("compiles the schema before the invocation content expands", function* () { const workspace = yield* useWorkspace(); + const stream = yield* useJournalStream(join(workspace, "doc.md")); const marker = join(workspace, "expanded.txt"); const source = [ "```js eval", @@ -248,7 +262,7 @@ describe("Elicit: what it refuses", () => { "", ].join("\n"); - const result = yield* run(workspace, source, constant({ decision: "approve" })); + const result = yield* run(workspace, source, constant({ decision: "approve" }), stream); expect(result.failure).toBeDefined(); expect(result.requests).toHaveLength(0); @@ -257,12 +271,14 @@ describe("Elicit: what it refuses", () => { it("refuses a schema declaring __proto__ as a name, naming the position", function* () { const workspace = yield* useWorkspace(); + const stream = yield* useJournalStream(join(workspace, "doc.md")); const schema = '{"type":"object","properties":{"__proto__":{"type":"string"}}}'; const result = yield* run( workspace, document("Approve?", schema), constant({ decision: "approve" }), + stream, ); expect(result.failure?.message).toContain("__proto__"); @@ -272,12 +288,14 @@ describe("Elicit: what it refuses", () => { it("refuses a reference that leaves the schema, naming the position", function* () { const workspace = yield* useWorkspace(); + const stream = yield* useJournalStream(join(workspace, "doc.md")); const schema = '{"type":"object","properties":{"decision":{"$ref":"other.json#/x"}}}'; const result = yield* run( workspace, document("Approve?", schema), constant({ decision: "approve" }), + stream, ); expect(result.failure?.message).toContain("other.json#/x"); @@ -287,11 +305,12 @@ describe("Elicit: what it refuses", () => { it("refuses a schema that is not an object", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, document("Approve?", "[1,2]"), constant({ decision: "approve" }), + stream, ); expect(result.failure?.message).toContain("schema must be a JSON Schema object"); @@ -300,11 +319,12 @@ describe("Elicit: what it refuses", () => { it("refuses schema text that is not JSON", function* () { const workspace = yield* useWorkspace(); + const stream = yield* useJournalStream(join(workspace, "doc.md")); const source = ['Approve?', ""].join( "\n", ); - const result = yield* run(workspace, source, constant({ decision: "approve" })); + const result = yield* run(workspace, source, constant({ decision: "approve" }), stream); expect(result.failure?.message).toContain("schema text is not JSON"); expect(result.requests).toHaveLength(0); @@ -312,12 +332,18 @@ describe("Elicit: what it refuses", () => { }); describe("Elicit: judging the answer", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); it("validates the provider's answer against the same schema", function* () { const workspace = yield* useWorkspace(); - - const result = yield* run(workspace, document("Approve?"), constant({ decision: "maybe" })); + const stream = yield* useJournalStream(join(workspace, "doc.md")); + const result = yield* run( + workspace, + document("Approve?"), + constant({ decision: "maybe" }), + stream, + ); expect(result.failure?.message).toContain("failed its schema"); expect(result.failure?.message).toContain("decision"); @@ -325,8 +351,8 @@ describe("Elicit: judging the answer", () => { it("fails once, without asking a second time", function* () { const workspace = yield* useWorkspace(); - - const result = yield* run(workspace, document("Approve?"), constant({ wrong: true })); + const stream = yield* useJournalStream(join(workspace, "doc.md")); + const result = yield* run(workspace, document("Approve?"), constant({ wrong: true }), stream); expect(result.failure).toBeDefined(); expect(result.requests).toHaveLength(1); @@ -368,8 +394,8 @@ describe("Elicit: judging the answer", () => { it("renders those issues into the printed error a document sees", function* () { const workspace = yield* useWorkspace(); - - const result = yield* run(workspace, document("Approve?"), constant({ decision: 7 })); + const stream = yield* useJournalStream(join(workspace, "doc.md")); + const result = yield* run(workspace, document("Approve?"), constant({ decision: 7 }), stream); expect(result.failure?.message).toContain(""); expect(result.failure?.message).toContain('"/decision" must be string'); @@ -382,14 +408,14 @@ describe("Elicit: judging the answer", () => { */ it("propagates a provider failure rather than swallowing it", function* () { const workspace = yield* useWorkspace(); - + const stream = yield* useJournalStream(join(workspace, "doc.md")); const result = yield* run( workspace, document("Approve?"), - // deno-lint-ignore require-yield function* () { throw new Error("the provider could not reach anyone"); }, + stream, ); expect(result.failure?.message).toBe("the provider could not reach anyone"); diff --git a/packages/core/tests/expression-props.test.ts b/packages/core/tests/expression-props.test.ts index e84a0d2c5..3c893e049 100644 --- a/packages/core/tests/expression-props.test.ts +++ b/packages/core/tests/expression-props.test.ts @@ -5,7 +5,9 @@ * parseExpressionValue changes, and integration with expansion/validation. */ -import { describe, it, beforeAll } from "@executablemd/test-support/bdd"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +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 { parseExpressionValue, scanSegments } from "../src/scanner.ts"; @@ -152,10 +154,14 @@ describe("Tier ES — parseExpressionValue", () => { // Tier EP — Expression prop evaluation (integration) // ═══════════════════════════════════════════════════════════════════════════ +const OWNER = "packages/core/tests/expression-props.test.ts"; + describe("Tier EP — Expression prop evaluation", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); it("EP1: bare identifier resolves from env", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Display.md": [ "---", @@ -174,7 +180,6 @@ describe("Tier EP — Expression prop evaluation", () => { "\n", ), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); // {props.data} will be interpolated — for objects, it uses toString expect(output).toContain("received:"); @@ -183,6 +188,7 @@ describe("Tier EP — Expression prop evaluation", () => { it("EP2: member expression resolves", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Show.md": [ "---", @@ -205,13 +211,13 @@ describe("Tier EP — Expression prop evaluation", () => { "", ].join("\n"), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("length=3"); }); it("EP3: comparison expression resolves", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Check.md": [ "---", @@ -234,13 +240,13 @@ describe("Tier EP — Expression prop evaluation", () => { '', ].join("\n"), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("active=true"); }); it("EP5: arithmetic expression", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Sum.md": [ "---", @@ -264,13 +270,13 @@ describe("Tier EP — Expression prop evaluation", () => { "", ].join("\n"), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("total=30"); }); it("EP6: JSON literal still resolves at scan time", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Num.md": [ "---", @@ -288,13 +294,13 @@ describe("Tier EP — Expression prop evaluation", () => { // No eval block — count={42} resolves at scan time "doc.md": "", }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("count=42"); }); it("EP11: string attribute unaffected", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Greet.md": [ "---", @@ -311,13 +317,13 @@ describe("Tier EP — Expression prop evaluation", () => { ].join("\n"), "doc.md": '', }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("hello world"); }); it("EP13: undefined binding → error", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Show.md": [ "---", @@ -333,7 +339,6 @@ describe("Tier EP — Expression prop evaluation", () => { ].join("\n"), "doc.md": ["```js eval", "const x = 1;", "```", "", ""].join("\n"), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("Failed to evaluate expression prop"); expect(output).toContain("nonexistent"); @@ -341,6 +346,7 @@ describe("Tier EP — Expression prop evaluation", () => { it("EP14: non-serializable result → error", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Show.md": [ "---", @@ -362,13 +368,13 @@ describe("Tier EP — Expression prop evaluation", () => { "", ].join("\n"), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("non-serializable"); }); it("EP15: no binding in env → reference error", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Show.md": [ "---", @@ -385,7 +391,6 @@ describe("Tier EP — Expression prop evaluation", () => { // No eval block defines someVar — ReferenceError at expansion time "doc.md": "", }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("Failed to evaluate expression prop"); expect(output).toContain("someVar"); @@ -393,6 +398,7 @@ describe("Tier EP — Expression prop evaluation", () => { it("EP16: syntax error in expression → error", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Show.md": [ "---", @@ -408,13 +414,13 @@ describe("Tier EP — Expression prop evaluation", () => { ].join("\n"), "doc.md": ["```js eval", "const a = 1;", "```", "", ""].join("\n"), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("Failed to evaluate expression prop"); }); it("EP17: mixed resolved and eval props", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Mixed.md": [ "---", @@ -443,7 +449,6 @@ describe("Tier EP — Expression prop evaluation", () => { '', ].join("\n"), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("count=42"); expect(output).toContain("data=result"); @@ -452,6 +457,7 @@ describe("Tier EP — Expression prop evaluation", () => { it("EP18: expression prop passes validation", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Typed.md": [ "---", @@ -468,7 +474,6 @@ describe("Tier EP — Expression prop evaluation", () => { ].join("\n"), "doc.md": ["```js eval", "const total = 5;", "```", "", ""].join("\n"), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("count=5"); expect(output).not.toContain("ERROR"); @@ -476,6 +481,7 @@ describe("Tier EP — Expression prop evaluation", () => { it("EP19: expression prop fails validation", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Typed.md": [ "---", @@ -494,13 +500,13 @@ describe("Tier EP — Expression prop evaluation", () => { "\n", ), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("must be number"); }); it("EP20: expression prop with slot", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Layout.md": [ "---", @@ -530,7 +536,6 @@ describe("Tier EP — Expression prop evaluation", () => { ].join("\n"), "doc.md": ["", '', ""].join("\n"), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("data=hello"); expect(output).not.toContain("ERROR"); @@ -575,6 +580,7 @@ describe("Tier EP — Expression prop evaluation", () => { it("EP22: nested component receives expression prop", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Outer.md": [ "---", @@ -604,13 +610,13 @@ describe("Tier EP — Expression prop evaluation", () => { ].join("\n"), "doc.md": "", }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("inner-data=from-outer"); }); it("EP23: children with expression props", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Parent.md": [ "---", @@ -640,7 +646,6 @@ describe("Tier EP — Expression prop evaluation", () => { ].join("\n"), "doc.md": ["", "", ""].join("\n"), }); - const stream = new InMemoryStream(); const output = yield* said(tmpDir, stream); expect(output).toContain("child-data=from-parent"); }); @@ -656,6 +661,7 @@ describe("Tier EP — Expression prop evaluation", () => { * boundary the two of them share. */ describe("Tier EU — a prop that evaluates to undefined", () => { + beforeEach(journalBeforeEach(OWNER)); beforeAll(() => useTempFileCompiler()); /** @@ -681,6 +687,7 @@ describe("Tier EU — a prop that evaluates to undefined", () => { it("EU1: a member that is not there is a prop that is not there", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Probe.md": probe([" value: {}"]), "doc.md": [ @@ -691,22 +698,24 @@ describe("Tier EU — a prop that evaluates to undefined", () => { "", ].join("\n"), }); - const output = yield* said(tmpDir, new InMemoryStream()); + const output = yield* said(tmpDir, stream); expect(output).toContain("own=false"); }); it("EU2: a literal `undefined` takes that same path", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Probe.md": probe([" value: {}"]), "doc.md": "", }); - const output = yield* said(tmpDir, new InMemoryStream()); + const output = yield* said(tmpDir, stream); expect(output).toContain("own=false"); }); it("EU3: a declared default answers the absence", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Probe.md": probe( [" value:", " type: string", ' default: "fallback"'], @@ -720,7 +729,7 @@ describe("Tier EU — a prop that evaluates to undefined", () => { "", ].join("\n"), }); - const output = yield* said(tmpDir, new InMemoryStream()); + const output = yield* said(tmpDir, stream); // Omission happens before validation, which is the only reason the schema // gets to supply its default. expect(output).toContain("own=true value=fallback"); @@ -728,6 +737,7 @@ describe("Tier EU — a prop that evaluates to undefined", () => { it("EU4: a required prop is missing, and the body does not run", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Probe.md": [ "---", @@ -749,7 +759,7 @@ describe("Tier EU — a prop that evaluates to undefined", () => { "", ].join("\n"), }); - const output = yield* said(tmpDir, new InMemoryStream()); + const output = yield* said(tmpDir, stream); expect(output).toContain("value"); expect(output).toContain("required"); expect(output).not.toContain("THE BODY RAN"); @@ -757,6 +767,7 @@ describe("Tier EU — a prop that evaluates to undefined", () => { it("EU5: `null` is a value the author wrote, not an absence", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Probe.md": probe( [" value:", ' type: ["string", "null"]'], @@ -772,13 +783,14 @@ describe("Tier EU — a prop that evaluates to undefined", () => { "", ].join("\n"), }); - const output = yield* said(tmpDir, new InMemoryStream()); + const output = yield* said(tmpDir, stream); expect(output).toContain("own=true value=null;"); expect(output).toContain("own=false value=undefined;"); }); it("EU6: an expression that throws is still the failure it was", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Probe.md": probe([" value: {}"]), "doc.md": [ @@ -789,13 +801,14 @@ describe("Tier EU — a prop that evaluates to undefined", () => { "", ].join("\n"), }); - const output = yield* said(tmpDir, new InMemoryStream()); + const output = yield* said(tmpDir, stream); expect(output).toContain("Failed to evaluate expression prop"); expect(output).toContain("no value here"); }); it("EU7: a nested `undefined` keeps its native JSON reading", function* () { const tmpDir = yield* useTempDirectory("expr-props-test-"); + const stream = yield* useJournalStream(path.join(tmpDir, "doc.md")); yield* writeFiles(tmpDir, { "components/Probe.md": probe( [" value:", " type: object"], @@ -809,7 +822,7 @@ describe("Tier EU — a prop that evaluates to undefined", () => { "", ].join("\n"), }); - const output = yield* said(tmpDir, new InMemoryStream()); + const output = yield* said(tmpDir, stream); // One `JSON.stringify` decides everything below the root, as it always // has: an object member goes, an array entry becomes `null`. expect(output).toContain('json={"b":[null,1],"c":2}'); diff --git a/packages/core/tests/fail-component.test.ts b/packages/core/tests/fail-component.test.ts index fef30901d..941e960e9 100644 --- a/packages/core/tests/fail-component.test.ts +++ b/packages/core/tests/fail-component.test.ts @@ -15,7 +15,9 @@ * sentinel message being nowhere in the failure. */ -import { describe, it } from "@executablemd/test-support/bdd"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; + +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { ensure, resource, scoped, until } from "effection"; import type { Operation, Result } from "effection"; @@ -54,6 +56,8 @@ interface Run { events: DurableEvent[]; } +const OWNER = "packages/core/tests/fail-component.test.ts"; + function useDir(): Operation { return resource(function* (provide) { const dir = yield* until(mkdtemp(join(tmpdir(), "xmd-fail-"))); @@ -172,8 +176,10 @@ const MISSING_RETURN = "The root document declares `returns` but produced no { + beforeEach(journalBeforeEach(OWNER)); it("FAIL1: ends a plain root with the authored message and runs no later sibling", function* () { const dir = yield* useDir(); + const stream = yield* useJournalStream(join(dir, "doc.md")); const result = yield* run( dir, [ @@ -184,6 +190,7 @@ describe("Tier FAIL — stopping a document deliberately", () => { '', "", ].join("\n"), + { stream }, ); // The authored sentence is the outcome, unprefixed and unclassified. @@ -197,6 +204,7 @@ describe("Tier FAIL — stopping a document deliberately", () => { it("FAIL2: fails a value root instead of reporting a missing ", function* () { const dir = yield* useDir(); + const stream = yield* useJournalStream(join(dir, "doc.md")); const result = yield* run( dir, [ @@ -206,6 +214,7 @@ describe("Tier FAIL — stopping a document deliberately", () => { '', "", ].join("\n"), + { stream }, ); const failure = failureOf(result); @@ -241,6 +250,7 @@ describe("Tier FAIL — stopping a document deliberately", () => { it("FAIL4: names loop exhaustion after the loop reached its own max", function* () { const dir = yield* useDir(); + const stream = yield* useJournalStream(join(dir, "doc.md")); const result = yield* run( dir, [ @@ -254,6 +264,7 @@ describe("Tier FAIL — stopping a document deliberately", () => { '', "", ].join("\n"), + { stream }, ); // The loop finished normally; the failure is the sibling chosen after it. @@ -267,6 +278,7 @@ describe("Tier FAIL — stopping a document deliberately", () => { it("FAIL5: is recovered by an authored in a text root, once", function* () { const dir = yield* useDir(); + const stream = yield* useJournalStream(join(dir, "doc.md")); const result = yield* run( dir, [ @@ -278,6 +290,7 @@ describe("Tier FAIL — stopping a document deliberately", () => { "tail", "", ].join("\n"), + { stream }, ); expect(result.outcome.ok).toBe(true); @@ -294,6 +307,7 @@ describe("Tier FAIL — stopping a document deliberately", () => { it("FAIL6: is not recovered by in a value root", function* () { const dir = yield* useDir(); + const stream = yield* useJournalStream(join(dir, "doc.md")); const result = yield* run( dir, [ @@ -305,6 +319,7 @@ describe("Tier FAIL — stopping a document deliberately", () => { '', "", ].join("\n"), + { stream }, ); const failure = failureOf(result); @@ -374,8 +389,10 @@ describe("Tier FAIL — invocations that never raise the authored message", () = }); describe("Tier FAIL — position, durability and precedence", () => { + beforeEach(journalBeforeEach(OWNER)); it("FAIL8: positions the failure at the opening tag it was written on", function* () { const dir = yield* useDir(); + const stream = yield* useJournalStream(join(dir, "doc.md")); const lines = [ "# Review", "", @@ -385,7 +402,7 @@ describe("Tier FAIL — position, durability and precedence", () => { "", ]; const source = lines.join("\n"); - const result = yield* run(dir, source); + const result = yield* run(dir, source, { stream }); const failure = offeredByFail(result); expect(failure.name).toBe("Fail"); diff --git a/packages/core/tests/glob-component.test.ts b/packages/core/tests/glob-component.test.ts index 444b7420a..9eca45423 100644 --- a/packages/core/tests/glob-component.test.ts +++ b/packages/core/tests/glob-component.test.ts @@ -13,7 +13,7 @@ * rather than following them. */ -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, race, resource, scoped, sleep, suspend, until } from "effection"; import type { Operation } from "effection"; @@ -22,6 +22,7 @@ import { API, useHostFiles } from "@executablemd/runtime"; import { InMemoryStream } from "@executablemd/durable-streams"; import type { Json } from "@executablemd/durable-streams"; import { execute } from "../src/execute.ts"; +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; import { collect } from "../src/collect.ts"; import { GlobError } from "../src/components/Glob.ts"; import { mkdir, mkdtemp, realpath, symlink } from "node:fs/promises"; @@ -66,18 +67,22 @@ function* useCwd(directory: string): Operation { ); } -function run(fixture: Fixture, source: string): Operation { - return runWith(fixture, source, new InMemoryStream()); +function fixtureDocument(fixture: Fixture): string { + return join(fixture.workspace, "..", "doc.md"); +} + +function run(fixture: Fixture, source: string, stream = new InMemoryStream()): Operation { + return runWith(fixture, source, stream); } /** * What one run said: its rendered text, or the diagnostic it reported. A * refusal nothing recovers is the run's own outcome. */ -function said(fixture: Fixture, source: string): Operation { +function said(fixture: Fixture, source: string, stream?: InMemoryStream): Operation { return (function* () { try { - return String(yield* run(fixture, source)); + return String(yield* run(fixture, source, stream)); } catch (error) { return error instanceof Error ? error.message : String(error); } @@ -101,7 +106,7 @@ function runWith( return scoped(function* () { // The document lives beside the workspace, not inside it, so it is never // itself a match — a `**/*` in a test means only what the test laid out. - const path = join(fixture.workspace, "..", "doc.md"); + const path = fixtureDocument(fixture); if (source !== undefined) { yield* writeTextFile(path, source); } @@ -202,7 +207,11 @@ const ADVERSARIAL: Array<{ name: string; error: () => unknown }> = [ }, ]; -describe("Tier GB — Glob component", () => { +const SUITE = "Tier GB — Glob component"; +const OWNER = "packages/core/tests/glob-component.test.ts"; + +describe(SUITE, () => { + beforeEach(journalBeforeEach(OWNER)); // GB1: props are validated like any component's — no name-specific check. it("GB1: include is required and undeclared props are rejected", function* () { const fixture = yield* useFixture(); @@ -218,8 +227,8 @@ describe("Tier GB — Glob component", () => { // that forgot to say anything, and the schema is where that is caught. it("GB2: an empty include list is rejected", function* () { const fixture = yield* useFixture(); - - const output = yield* said(fixture, ''); + const stream = yield* useJournalStream(fixtureDocument(fixture)); + const output = yield* said(fixture, '', stream); expect(output).toContain('"/include" must NOT have fewer than 1 items'); }); @@ -244,8 +253,8 @@ describe("Tier GB — Glob component", () => { // contract, not a rule of its own. it("GB4: invoking without as fails", function* () { const fixture = yield* useFixture(); - - const output = yield* said(fixture, ''); + const stream = yield* useJournalStream(fixtureDocument(fixture)); + const output = yield* said(fixture, '', stream); expect(output).toContain("must be invoked with `as`"); }); @@ -253,9 +262,10 @@ describe("Tier GB — Glob component", () => { // GB5: `exclude` is optional, and omitting it excludes nothing. it("GB5: exclude defaults to empty", function* () { const fixture = yield* useFixture(); + const stream = yield* useJournalStream(fixtureDocument(fixture)); yield* plant(fixture, ["a.md", "nested/b.md"]); - const output = text(yield* run(fixture, FIND_ALL)); + const output = text(yield* run(fixture, FIND_ALL, stream)); expect(output).toContain(found("a.md", "nested/b.md")); }); @@ -316,8 +326,8 @@ describe("Tier GB — Glob component", () => { // authoring mistake as an absolute one. it("GB8: an empty pattern fails", function* () { const fixture = yield* useFixture(); - - const output = text(yield* run(fixture, '')); + const stream = yield* useJournalStream(fixtureDocument(fixture)); + const output = text(yield* run(fixture, '', stream)); expect(output).toContain("empty pattern"); }); @@ -485,10 +495,11 @@ describe("Tier GB — Glob component", () => { // to judge a destination. it("GB15b: a directory symlink inside the workspace is not traversed either", function* () { const fixture = yield* useFixture(); + const stream = yield* useJournalStream(fixtureDocument(fixture)); yield* plant(fixture, ["real/a.md"]); yield* until(symlink(join(fixture.workspace, "real"), join(fixture.workspace, "alias"))); - const output = text(yield* run(fixture, FIND_ALL)); + const output = text(yield* run(fixture, FIND_ALL, stream)); expect(output).toContain(found("real/a.md")); expect(output).not.toContain("alias/"); @@ -498,11 +509,12 @@ describe("Tier GB — Glob component", () => { // unbounded walk if it were followed; the search finishes. it("GB15c: a symlink cycle does not hang the search", function* () { const fixture = yield* useFixture(); + const stream = yield* useJournalStream(fixtureDocument(fixture)); yield* plant(fixture, ["a.md"]); yield* until(symlink(fixture.workspace, join(fixture.workspace, "loop"))); const output = text( - yield* race([run(fixture, FIND_ALL), timeout("GB15c: the search did not finish")]), + yield* race([run(fixture, FIND_ALL, stream), timeout("GB15c: the search did not finish")]), ); expect(output).toContain(found("a.md")); @@ -512,10 +524,11 @@ describe("Tier GB — Glob component", () => { // when its destination is an ordinary file inside the workspace. it("GB16: a symlink to a file is not a result", function* () { const fixture = yield* useFixture(); + const stream = yield* useJournalStream(fixtureDocument(fixture)); yield* plant(fixture, ["a.md"]); yield* until(symlink(join(fixture.workspace, "a.md"), join(fixture.workspace, "link.md"))); - const output = text(yield* run(fixture, FIND_ALL)); + const output = text(yield* run(fixture, FIND_ALL, stream)); expect(output).toContain(found("a.md")); expect(output).not.toContain("link.md"); diff --git a/packages/core/tests/root-props.test.ts b/packages/core/tests/root-props.test.ts index cad7a8cc0..535dfdbdf 100644 --- a/packages/core/tests/root-props.test.ts +++ b/packages/core/tests/root-props.test.ts @@ -5,7 +5,9 @@ * validated and defaulted before any body effect, then available through * `{props.name}`, bare binding interpolation, and eval blocks. */ -import { describe, it } from "@executablemd/test-support/bdd"; + +import { journalBeforeEach, useJournalStream } from "@executablemd/test-support/failure-journal"; +import { beforeEach, describe, it } from "@executablemd/test-support/bdd"; import { expect } from "@executablemd/test-support/expect"; import { InMemoryStream } from "@executablemd/durable-streams"; import type { Json } from "@executablemd/durable-streams"; @@ -80,24 +82,38 @@ const NESTED_CONCISE = [ "", ].join("\n"); -function* runPath(path: string, props?: Record) { - const execution = yield* execute({ path, stream: new InMemoryStream(), props }); +function* runPath(path: string, props?: Record, stream = new InMemoryStream()) { + const execution = yield* execute({ path, stream, props }); const output = yield* forEach(function* () {}, execution.output); const result = yield* execution; return { output, result }; } -function* runDoc(files: Record, path: string, props?: Record) { +function* runDoc( + files: Record, + path: string, + props?: Record, + stream?: InMemoryStream, +) { yield* useStubFs(files); - return yield* runPath(path, props); + return yield* runPath(path, props, stream); } +const OWNER = "packages/core/tests/root-props.test.ts"; + describe("Tier RP — root document properties", () => { + beforeEach(journalBeforeEach(OWNER)); it("RP1: supplied props reach interpolation without bare prop bindings", function* () { - const { output, result } = yield* runDoc({ "hello.md": GREETING }, "hello.md", { - name: "Ada", - loud: true, - }); + const stream = yield* useJournalStream("hello.md"); + const { output, result } = yield* runDoc( + { "hello.md": GREETING }, + "hello.md", + { + name: "Ada", + loud: true, + }, + stream, + ); expect(result.ok).toBe(true); expect(output).toContain("Hello, Ada!"); expect(output).toContain("loud=true"); @@ -105,32 +121,52 @@ describe("Tier RP — root document properties", () => { }); it("RP2: schema defaults apply to the root", function* () { - const { output } = yield* runDoc({ "hello.md": GREETING }, "hello.md", { name: "Ada" }); + const stream = yield* useJournalStream("hello.md"); + const { output } = yield* runDoc({ "hello.md": GREETING }, "hello.md", { name: "Ada" }, stream); expect(output).toContain("loud=false"); }); it("RP3: a missing required property prevents every body effect", function* () { - const { output, result } = yield* runDoc({ "doc.md": SIDE_EFFECT }, "doc.md", {}); + const stream = yield* useJournalStream("doc.md"); + const { output, result } = yield* runDoc({ "doc.md": SIDE_EFFECT }, "doc.md", {}, stream); expect(result.ok).toBe(false); expect(output).not.toContain("SIDE_EFFECT_MARKER"); }); it("RP4: an invalid property prevents every body effect", function* () { - const { output, result } = yield* runDoc({ "doc.md": SIDE_EFFECT }, "doc.md", { name: 12 }); + const stream = yield* useJournalStream("doc.md"); + const { output, result } = yield* runDoc( + { "doc.md": SIDE_EFFECT }, + "doc.md", + { name: 12 }, + stream, + ); expect(result.ok).toBe(false); expect(output).not.toContain("SIDE_EFFECT_MARKER"); }); it("RP5: an undeclared property is rejected by the root schema", function* () { - const { result } = yield* runDoc({ "doc.md": SIDE_EFFECT }, "doc.md", { - name: "Ada", - nope: 1, - }); + const stream = yield* useJournalStream("doc.md"); + const { result } = yield* runDoc( + { "doc.md": SIDE_EFFECT }, + "doc.md", + { + name: "Ada", + nope: 1, + }, + stream, + ); expect(result.ok).toBe(false); }); it("RP6: a document without props keeps its existing behavior", function* () { - const { output, result } = yield* runDoc({ "plain.md": NO_PROPS }, "plain.md"); + const stream = yield* useJournalStream("plain.md"); + const { output, result } = yield* runDoc( + { "plain.md": NO_PROPS }, + "plain.md", + undefined, + stream, + ); expect(result.ok).toBe(true); expect(output).toContain("PLAIN_MARKER"); }); @@ -225,10 +261,17 @@ describe("Tier RI — document inspection", () => { }); describe("Tier RS — concise props declarations", () => { + beforeEach(journalBeforeEach(OWNER)); it("RS1: a concise root receives props and applies defaults", function* () { - const { output, result } = yield* runDoc({ "hello.md": GREETING_CONCISE }, "hello.md", { - name: "Ada", - }); + const stream = yield* useJournalStream("hello.md"); + const { output, result } = yield* runDoc( + { "hello.md": GREETING_CONCISE }, + "hello.md", + { + name: "Ada", + }, + stream, + ); expect(result.ok).toBe(true); expect(output).toContain("Hello, Ada!"); expect(output).toContain("loud=false"); @@ -243,21 +286,39 @@ describe("Tier RS — concise props declarations", () => { }); it("RS3: a missing required property prevents every body effect", function* () { - const { output, result } = yield* runDoc({ "hello.md": GREETING_CONCISE }, "hello.md", {}); + const stream = yield* useJournalStream("hello.md"); + const { output, result } = yield* runDoc( + { "hello.md": GREETING_CONCISE }, + "hello.md", + {}, + stream, + ); expect(result.ok).toBe(false); expect(output).not.toContain("Hello"); }); it("RS4: the implicit closed object rejects an undeclared property", function* () { - const { result } = yield* runDoc({ "hello.md": GREETING_CONCISE }, "hello.md", { - name: "Ada", - nope: 1, - }); + const stream = yield* useJournalStream("hello.md"); + const { result } = yield* runDoc( + { "hello.md": GREETING_CONCISE }, + "hello.md", + { + name: "Ada", + nope: 1, + }, + stream, + ); expect(result.ok).toBe(false); }); it("RS5: object defaults fill recursively", function* () { - const { output, result } = yield* runDoc({ "nested.md": NESTED_CONCISE }, "nested.md", {}); + const stream = yield* useJournalStream("nested.md"); + const { output, result } = yield* runDoc( + { "nested.md": NESTED_CONCISE }, + "nested.md", + {}, + stream, + ); expect(result.ok).toBe(true); expect(output).toContain("port=8080"); }); diff --git a/packages/git/tests/run-composition-managed.test.ts b/packages/git/tests/run-composition-managed.test.ts index 50eaf005e..09f070ce8 100644 --- a/packages/git/tests/run-composition-managed.test.ts +++ b/packages/git/tests/run-composition-managed.test.ts @@ -18,11 +18,13 @@ import { expect } from "@executablemd/test-support/expect"; import { scoped, type Operation } from "effection"; 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 { + checkSubprocessJournals, + parseSubprocessJournal, + readSubprocessSnapshot, +} from "../../test-support/subprocess-journal.ts"; import { join } from "node:path"; import { git, useBareRemote } from "./support/git-remotes.ts"; import { spawnSync } from "node:child_process"; @@ -529,24 +531,6 @@ describe("ORC11 — an interrupted creation", () => { 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( @@ -554,7 +538,7 @@ describe(ORC12_SUITE, () => { cwd: string, source: string, capture = false, - ): Operation & { events: DurableEvent[] }> { + ): Operation }> { const outcome = spawnSync( process.execPath, [ @@ -595,7 +579,7 @@ describe(ORC12_SUITE, () => { 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) : [], + snapshot: readSubprocessSnapshot("journals" in result ? result.journals : undefined, 0), }; } @@ -640,7 +624,7 @@ describe(ORC12_SUITE, () => { expect(refused.kind).toBe("refused"); expect(refused.reason).toBe("in-use"); expect(refused.message).toContain("another process is working in"); - expect(refused.events).toEqual([]); + expect(refused.snapshot).toBe("snapshot absent"); // And nothing under the slot moved. expect(yield* fingerprintTree(held.slot)).toEqual(bytes); @@ -693,22 +677,15 @@ describe(ORC12_SUITE, () => { }, }); } - 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); - } + // The real second process takes the same checkout. Its result is available after exit. + const later = yield* elsewhere(root, checkout.root, document, true); + yield* checkSubprocessJournals( + { owner: OWNER, suite: ORC12_SUITE, name }, + [{ source: "", snapshot: later.snapshot }], + function* () { expect(later.kind).toBe("selected"); expect(later.bound).toBe(slot.checkout); - yield* check(slot, later.events); + yield* check(slot, typeof later.snapshot === "string" ? [] : later.snapshot); }, ); }); @@ -760,7 +737,7 @@ describe(ORC12_SUITE, () => { source: "", }); const lines = (yield* readTextFile(join(directory, "child-1.jsonl"))).trimEnd().split("\n"); - const events = parseJournal(lines); + const events = parseSubprocessJournal(lines); expect(events).toEqual(received); expect(events.at(-1)).toMatchObject({ type: "close", coroutineId: "root" }); }); diff --git a/packages/git/tests/support/run-composition-child.ts b/packages/git/tests/support/run-composition-child.ts index 5cb9e57e5..2c78d7467 100644 --- a/packages/git/tests/support/run-composition-child.ts +++ b/packages/git/tests/support/run-composition-child.ts @@ -13,13 +13,13 @@ import { main } from "effection"; import process from "node:process"; -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"; import { useRunComposition } from "../../src/deno/run-composition/provider.ts"; import { ManagedCheckoutError } from "../../src/deno/run-composition/errors.ts"; +import { boundedJournalResult } from "../../../test-support/subprocess-journal.ts"; /** What the parent reads back off this process's stdout. */ export interface ChildOutcome { @@ -29,7 +29,6 @@ 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* () { @@ -65,15 +64,11 @@ await main(function* () { : { kind: "refused", reason: refusal.reason, message: refusal.message }; } - 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); + const line = + capture === "--capture-journal" + ? boundedJournalResult(outcome, [(yield* stream.readAll()).map(serializeDurableEvent)]) + : JSON.stringify(outcome); + process.stdout.write(`${line}\n`); }); /** The managed-checkout refusal in this error's chain, if there is one. */ diff --git a/packages/test-agent/tests/components.test.ts b/packages/test-agent/tests/components.test.ts index 263375092..6ce14f428 100644 --- a/packages/test-agent/tests/components.test.ts +++ b/packages/test-agent/tests/components.test.ts @@ -22,6 +22,7 @@ import type { AnswerConfiguration } from "@executablemd/core/host"; import { executeInstalled } from "@executablemd/core/host"; import { API } from "@executablemd/runtime"; import { InMemoryStream } from "@executablemd/durable-streams"; +import { captureTestJournal } from "@executablemd/test-support/failure-journal"; import { installTestingComponents, testHarnessInstallation, @@ -55,8 +56,26 @@ interface RunOptions { cwdRef?: { value: string; flipTo: string }; } -function* runDoc(files: Record, options?: RunOptions): Operation { - const dir = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); +const OWNER = "packages/test-agent/tests/components.test.ts"; + +function* captureFileCase( + name: string, + directory: string, + body: (stream: InMemoryStream, directory: string) => Operation, +): Operation { + return yield* captureTestJournal( + { owner: OWNER, name }, + path.join(directory, "doc.md"), + (stream) => body(stream, directory), + ); +} + +function* runDoc( + files: Record, + options?: RunOptions, + stream = new InMemoryStream(), + dir = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`), +): Operation { yield* ensureDir(dir); return yield* scoped(function* () { yield* ensure(() => rm(dir, { recursive: true, force: true })); @@ -102,7 +121,7 @@ function* runDoc(files: Record, options?: RunOptions): Operation const execution = yield* executeInstalled( { path: path.join(dir, "doc.md"), - stream: new InMemoryStream(), + stream, ...(options?.includes ? { includes: options.includes.map((d) => path.join(dir, d)) } : {}), @@ -174,94 +193,148 @@ describe("Tier TV — TestAgent components", { sanitizeOps: false, sanitizeResou }); it("fails the owning Test when actual prompt configuration violates a constraint", function* () { - const run = yield* runDoc({ - "hi.md": '\n\nwrong\n', - "doc.md": - '\n\n\nhi\n\n\n', - }); - expect(run.results.map((result) => result.status)).toEqual(["fail"]); - expect(run.output).toContain("Prompt configuration does not match"); + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "fails the owning Test when actual prompt configuration violates a constraint", + directory, + function* (stream, directory) { + const run = yield* runDoc( + { + "hi.md": '\n\nwrong\n', + "doc.md": + '\n\n\nhi\n\n\n', + }, + undefined, + stream, + directory, + ); + expect(run.results.map((result) => result.status)).toEqual(["fail"]); + expect(run.output).toContain("Prompt configuration does not match"); + }, + ); }); it("TV1: outside an active testing session is a configuration error", function* () { - const run = yield* runDoc( - { "doc.md": "\nbody\n\n" }, - { session: false }, - ); - // Nothing recovers the configuration error, so it is the run's outcome. - expect(run.result.ok).toBe(false); - expect(run.result.ok ? "" : run.result.error.message).toContain( - "valid only in an active testing session", + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV1: outside an active testing session is a configuration error", + directory, + function* (stream, directory) { + const run = yield* runDoc( + { "doc.md": "\nbody\n\n" }, + { session: false }, + stream, + directory, + ); + // Nothing recovers the configuration error, so it is the run's outcome. + expect(run.result.ok).toBe(false); + expect(run.result.ok ? "" : run.result.error.message).toContain( + "valid only in an active testing session", + ); + }, ); }); it("TV12: a configuration diagnostic is observed once and rendered once", function* () { - const observed: string[] = []; - yield* Component.around({ - *raise([error], next) { - observed.push(error.message); - return yield* next(error); + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV12: a configuration diagnostic is observed once and rendered once", + directory, + function* (stream, directory) { + const observed: string[] = []; + yield* Component.around({ + *raise([error], next) { + observed.push(error.message); + return yield* next(error); + }, + }); + // Written in a region that prints: what this pins is that the diagnostic + // is observed once and rendered once, which needs it rendered. + const run = yield* runDoc( + { "doc.md": "\n\nbody\n\n\n" }, + { session: false }, + stream, + directory, + ); + expect(observed).toHaveLength(1); + expect(observed[0]).toContain("valid only in an active testing session"); + expect(run.output.match(/valid only in an active testing session/g)).toHaveLength(1); }, - }); - // Written in a region that prints: what this pins is that the diagnostic - // is observed once and rendered once, which needs it rendered. - const run = yield* runDoc( - { "doc.md": "\n\nbody\n\n\n" }, - { session: false }, ); - expect(observed).toHaveLength(1); - expect(observed[0]).toContain("valid only in an active testing session"); - expect(run.output.match(/valid only in an active testing session/g)).toHaveLength(1); }); it("TV2: missing and duplicate mappings fail the owning test before the turn", function* () { - const run = yield* runDoc({ - "agents/hi.md": HI, - "doc.md": [ - "", - ' ', - ' ', - ' ', - ' ', - " ", - ' ', - ' ', - " ", - "", - "", - ].join("\n"), - }); - expect(run.results.map((entry) => entry.status)).toEqual(["fail", "fail"]); - // Both refusals name the agent and the session they are about, so a reader - // of a failing suite knows which mapping to write or remove. - expect(run.output).toContain( - 'No was found for agent "test" and session "unmapped".', - ); - expect(run.output).toContain( - 'More than one was declared for agent "test" and session "dup".', + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV2: missing and duplicate mappings fail the owning test before the turn", + directory, + function* (stream, directory) { + const run = yield* runDoc( + { + "agents/hi.md": HI, + "doc.md": [ + "", + ' ', + ' ', + ' ', + ' ', + " ", + ' ', + ' ', + " ", + "", + "", + ].join("\n"), + }, + undefined, + stream, + directory, + ); + expect(run.results.map((entry) => entry.status)).toEqual(["fail", "fail"]); + // Both refusals name the agent and the session they are about, so a reader + // of a failing suite knows which mapping to write or remove. + expect(run.output).toContain( + 'No was found for agent "test" and session "unmapped".', + ); + expect(run.output).toContain( + 'More than one was declared for agent "test" and session "dup".', + ); + }, ); }); it("TV3: each gets fresh state; a mismatch fails only its owning test", function* () { - const run = yield* runDoc({ - "agents/hi.md": HI, - "doc.md": [ - "", - ' ', - ' ', - ' ', - " ", - ' ', - ' ', - ' ', - " ", - "", - "", - ].join("\n"), - }); - expect(run.results.map((entry) => entry.status)).toEqual(["fail", "pass"]); - expect(run.output).toContain("expected template: hi"); - expect(run.output).toContain("actual prompt: wrong"); + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV3: each gets fresh state; a mismatch fails only its owning test", + directory, + function* (stream, directory) { + const run = yield* runDoc( + { + "agents/hi.md": HI, + "doc.md": [ + "", + ' ', + ' ', + ' ', + " ", + ' ', + ' ', + ' ', + " ", + "", + "", + ].join("\n"), + }, + undefined, + stream, + directory, + ); + expect(run.results.map((entry) => entry.status)).toEqual(["fail", "pass"]); + expect(run.output).toContain("expected template: hi"); + expect(run.output).toContain("actual prompt: wrong"); + }, + ); }); it("TV4: non-whitespace pre-matcher output and eval imports are configuration failures", function* () { @@ -376,172 +449,241 @@ describe("Tier TV — TestAgent components", { sanitizeOps: false, sanitizeResou }); it("TV6: default and named mappings resolve; the agent prop registers additional agents", function* () { - // One per mapping: each prompt spawns a worker, and under Node a - // spawn costs enough that three inside one brush the fixed - // 20-second test timeout on a loaded runner (#332). Splitting keeps every - // test far from the deadline and names the mapping that failed. - const run = yield* runDoc({ - "agents/default.md": '\n\ndefault-reply\n', - "agents/named.md": '\n\nnamed-reply\n', - "agents/extra.md": '\n\nextra-reply\n', - "doc.md": [ - "", - ' ', - ' ', - ' ', - ' ', - ' ', - ' ', - " ", - ' ', - ' ', - ' ', - " ", - ' ', - ' ', - ' ', - " ", - "", - "", - ].join("\n"), - }); - expect(run.results.map((entry) => entry.status)).toEqual(["pass", "pass", "pass"]); + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV6: default and named mappings resolve; the agent prop registers additional agents", + directory, + function* (stream, directory) { + // One per mapping: each prompt spawns a worker, and under Node a + // spawn costs enough that three inside one brush the fixed + // 20-second test timeout on a loaded runner (#332). Splitting keeps every + // test far from the deadline and names the mapping that failed. + const run = yield* runDoc( + { + "agents/default.md": '\n\ndefault-reply\n', + "agents/named.md": '\n\nnamed-reply\n', + "agents/extra.md": '\n\nextra-reply\n', + "doc.md": [ + "", + ' ', + ' ', + ' ', + ' ', + ' ', + ' ', + " ", + ' ', + ' ', + ' ', + " ", + ' ', + ' ', + ' ', + " ", + "", + "", + ].join("\n"), + }, + undefined, + stream, + directory, + ); + expect(run.results.map((entry) => entry.status)).toEqual(["pass", "pass", "pass"]); + }, + ); }); it("TV7: different working directories get independent scenario scenarios", function* () { - const cwdRef = { value: path.resolve("."), flipTo: os.tmpdir() }; - const run = yield* runDoc( - { - "agents/hi.md": HI, - "doc.md": [ - "", - ' ', - ' ', - ' ', - "", - "```bash exec silent", - "flip-cwd", - "```", - "", - ' ', - ' ', - ' ', - " ", - "", - "", - ].join("\n"), + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV7: different working directories get independent scenario scenarios", + directory, + function* (stream, directory) { + const cwdRef = { value: path.resolve("."), flipTo: os.tmpdir() }; + const run = yield* runDoc( + { + "agents/hi.md": HI, + "doc.md": [ + "", + ' ', + ' ', + ' ', + "", + "```bash exec silent", + "flip-cwd", + "```", + "", + ' ', + ' ', + ' ', + " ", + "", + "", + ].join("\n"), + }, + { cwdRef }, + stream, + directory, + ); + // The same stage-1 prompt matched twice: the flipped cwd allocated a + // fresh scenario instead of reusing (and exhausting) the first one. + expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); }, - { cwdRef }, ); - // The same stage-1 prompt matched twice: the flipped cwd allocated a - // fresh scenario instead of reusing (and exhausting) the first one. - expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); }); it("TV8: exhaustion fails the owning test; a suspended scenario tears down cleanly", function* () { - const run = yield* runDoc({ - "agents/hi.md": HI, - "agents/two.md": TWO_STAGES, - "doc.md": [ - "", - ' ', - ' ', - ' ', - ' ', - ' ', - " ", - ' ', - ' ', - ' ', - " ", - "", - "", - "after the agent scope", - "", - ].join("\n"), - }); - expect(run.results.map((entry) => entry.status)).toEqual(["fail", "pass"]); - expect(run.output).toContain("scenario exhausted"); - // Clean teardown: the suspended second scenario did not fail the - // document, and content after the boundary still rendered. - expect(run.output).toContain("after the agent scope"); - expect(run.result.ok).toBe(false); + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV8: exhaustion fails the owning test; a suspended scenario tears down cleanly", + directory, + function* (stream, directory) { + const run = yield* runDoc( + { + "agents/hi.md": HI, + "agents/two.md": TWO_STAGES, + "doc.md": [ + "", + ' ', + ' ', + ' ', + ' ', + ' ', + " ", + ' ', + ' ', + ' ', + " ", + "", + "", + "after the agent scope", + "", + ].join("\n"), + }, + undefined, + stream, + directory, + ); + expect(run.results.map((entry) => entry.status)).toEqual(["fail", "pass"]); + expect(run.output).toContain("scenario exhausted"); + // Clean teardown: the suspended second scenario did not fail the + // document, and content after the boundary still rendered. + expect(run.output).toContain("after the agent scope"); + expect(run.result.ok).toBe(false); + }, + ); }); it("TV9: the behavior document executes from the declaration snapshot", function* () { - const dirToken = `xmd-snap-${randomUUID()}`; - const run = yield* runDoc({ - "agents/hi.md": HI, - "doc.md": [ - "", - ' ', - "", - "```bash exec silent", - `rm __DIR__/agents/hi.md || true # ${dirToken}`, - "```", - "", - ' ', - ' ', - ' ', - " ", - "", - "", - ].join("\n"), - }); - expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV9: the behavior document executes from the declaration snapshot", + directory, + function* (stream, directory) { + const dirToken = `xmd-snap-${randomUUID()}`; + const run = yield* runDoc( + { + "agents/hi.md": HI, + "doc.md": [ + "", + ' ', + "", + "```bash exec silent", + `rm __DIR__/agents/hi.md || true # ${dirToken}`, + "```", + "", + ' ', + ' ', + ' ', + " ", + "", + "", + ].join("\n"), + }, + undefined, + stream, + directory, + ); + expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); + }, + ); }); it("TV10: / pin a resolved session that routes nested prompts", function* () { - const run = yield* runDoc({ - "agents/two.md": TWO_STAGES, - "doc.md": [ - "", - ' ', - ' ', - ' ', - ' ', - ' ', - ' ', - " ", - " ", - ' ', - ' ', - " ", - "", - "", - ].join("\n"), - }); - // Both prompts routed through the Session object pinned by - // to the same scenario, so stage 2 matched after stage 1. - expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV10: / pin a resolved session that routes nested prompts", + directory, + function* (stream, directory) { + const run = yield* runDoc( + { + "agents/two.md": TWO_STAGES, + "doc.md": [ + "", + ' ', + ' ', + ' ', + ' ', + ' ', + ' ', + " ", + " ", + ' ', + ' ', + " ", + "", + "", + ].join("\n"), + }, + undefined, + stream, + directory, + ); + // Both prompts routed through the Session object pinned by + // to the same scenario, so stage 2 matched after stage 1. + expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); + }, + ); }); it("TV11: a pinned session is rejected for an agent that did not create it", function* () { - const run = yield* runDoc({ - "agents/owner.md": '\n\nowner-reply\n', - "agents/other.md": '\n\nother-reply\n', - "doc.md": [ - "", - ' ', - ' ', - ' ', - ' ', - ' ', - ' ', - ' ', - " ", - " ", - " ", - " ", - "", - "", - ].join("\n"), - }); - expect(run.results.map((entry) => entry.status)).toEqual(["fail"]); - expect(run.output).toContain('agent "extra" does not match session'); - // Neither agent's scenario advanced: no turn ran on either. - expect(run.output).not.toContain("owner-reply"); - expect(run.output).not.toContain("other-reply"); + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV11: a pinned session is rejected for an agent that did not create it", + directory, + function* (stream, directory) { + const run = yield* runDoc( + { + "agents/owner.md": '\n\nowner-reply\n', + "agents/other.md": '\n\nother-reply\n', + "doc.md": [ + "", + ' ', + ' ', + ' ', + ' ', + ' ', + ' ', + ' ', + " ", + " ", + " ", + " ", + "", + "", + ].join("\n"), + }, + undefined, + stream, + directory, + ); + expect(run.results.map((entry) => entry.status)).toEqual(["fail"]); + expect(run.output).toContain('agent "extra" does not match session'); + // Neither agent's scenario advanced: no turn ran on either. + expect(run.output).not.toContain("owner-reply"); + expect(run.output).not.toContain("other-reply"); + }, + ); }); // The regression the scoped() removal fixes: content projected by @@ -550,20 +692,32 @@ describe("Tier TV — TestAgent components", { sanitizeOps: false, sanitizeResou // scoped() again turns every scenario into "valid only inside " // and every prompt into "Agent.agent() has no provider". it("TV13: a scenario in the body sees the session installed", function* () { - const run = yield* runDoc({ - "agents/hi.md": HI, - "doc.md": [ - "", - '', - '', - "", - "", - ].join("\n"), - }); + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV13: a scenario in the body sees the session installed", + directory, + function* (stream, directory) { + const run = yield* runDoc( + { + "agents/hi.md": HI, + "doc.md": [ + "", + '', + '', + "", + "", + ].join("\n"), + }, + undefined, + stream, + directory, + ); - expect(run.output).not.toContain("is valid only inside "); - expect(run.output).not.toContain("has no provider"); - expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); + expect(run.output).not.toContain("is valid only inside "); + expect(run.output).not.toContain("has no provider"); + expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); + }, + ); }); // TC1's analog: a settled diagnostic beside healthy scenarios renders inline @@ -572,26 +726,38 @@ describe("Tier TV — TestAgent components", { sanitizeOps: false, sanitizeResou // pins that the component's own check still owns that wording — the schema // accepts "" as a string, so only the fn can catch it. it("TV14: a scenario diagnostic renders inline beside a healthy one", function* () { - const run = yield* runDoc({ - "agents/hi.md": HI, - // The region prints, which is what lets the rest of the body run after - // the scenario's own diagnostic. - "doc.md": [ - "", - "", - '', - '', - "BODY TEXT", - '', - "", - "", - "", - ].join("\n"), - }); + const directory = path.join(os.tmpdir(), `xmd-tv-${randomUUID()}`); + yield* captureFileCase( + "TV14: a scenario diagnostic renders inline beside a healthy one", + directory, + function* (stream, directory) { + const run = yield* runDoc( + { + "agents/hi.md": HI, + // The region prints, which is what lets the rest of the body run after + // the scenario's own diagnostic. + "doc.md": [ + "", + "", + '', + '', + "BODY TEXT", + '', + "", + "", + "", + ].join("\n"), + }, + undefined, + stream, + directory, + ); - expect(run.output).toContain('requires a "src" prop'); - expect(run.output).toContain("BODY TEXT"); - expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); + expect(run.output).toContain('requires a "src" prop'); + expect(run.output).toContain("BODY TEXT"); + expect(run.results.map((entry) => entry.status)).toEqual(["pass"]); + }, + ); }); // A registration is a default, so a repository component of either name wins. diff --git a/packages/test-agent/tests/native-launch.test.ts b/packages/test-agent/tests/native-launch.test.ts index c8b1c64fa..12d1f8479 100644 --- a/packages/test-agent/tests/native-launch.test.ts +++ b/packages/test-agent/tests/native-launch.test.ts @@ -14,7 +14,8 @@ * asserts "no hidden prompt" by the answer that later prompt gets, not by an * absence. */ -import { beforeAll, describe, it } from "@executablemd/test-support/bdd"; +import { beforeAll, 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 { useTempFileCompiler } from "@executablemd/core"; import { agentIdentityComponents } from "@executablemd/agent/host"; @@ -60,12 +61,17 @@ interface RunOptions { hostLauncher?: (request: NativeLaunchRequest) => void; } -function* runDoc(doc: string, options: RunOptions = {}): Operation { - const dir = path.join(os.tmpdir(), `xmd-tl-${randomUUID()}`); +const owner = "packages/test-agent/tests/native-launch.test.ts"; + +function* runDoc( + doc: string, + options: RunOptions = {}, + dir = path.join(os.tmpdir(), `xmd-tl-${randomUUID()}`), +): Operation { yield* ensureDir(dir); const launches: NativeLaunchRequest[] = []; const sessions: NativeSessionReport[] = []; - const stream = options.stream ?? new InMemoryStream(); + const stream = options.stream ?? (yield* useJournalStream(path.join(dir, "doc.md"))); return yield* scoped(function* () { yield* ensure(() => rm(dir, { recursive: true, force: true })); yield* writeTextFile(path.join(dir, "scenario.md"), SCENARIO); @@ -189,6 +195,7 @@ describe( }, () => { beforeAll(() => useTempFileCompiler()); + beforeEach(journalBeforeEach(owner)); it("TL1: the rendered body installs as the agent's session instruction layer", function* () { const run = yield* runDoc(testDoc(LAUNCH_BODY)); diff --git a/packages/test-support/subprocess-journal.ts b/packages/test-support/subprocess-journal.ts new file mode 100644 index 000000000..8927fb44c --- /dev/null +++ b/packages/test-support/subprocess-journal.ts @@ -0,0 +1,129 @@ +import { Buffer } from "node:buffer"; +import process from "node:process"; +import { ensure, scoped } from "effection"; +import type { Operation } from "effection"; +import { parseDurableEvent } from "@executablemd/durable-streams"; +import type { DurableEvent } from "@executablemd/durable-streams"; +import { withFailureJournal } from "@executablemd/testing"; +import { useTestDiagnostics } from "../cli/src/test-diagnostics.ts"; +import type { DeliverySink } from "../cli/src/stdout-delivery.ts"; +import type { JournalCase } from "../cli/src/test-diagnostics.ts"; + +const MAX_ENVELOPE_BYTES = 64 * 1024; + +const MAX_SNAPSHOT_BYTES = 16 * 1024; +const OVERFLOW = "snapshot exceeds journal budget"; +const INVALID = "invalid snapshot"; +const MISSING = "snapshot absent"; + +type Snapshot = readonly DurableEvent[] | typeof OVERFLOW | typeof INVALID | typeof MISSING; + +function fitsEnvelope(text: string): boolean { + return Buffer.byteLength(text) + 1 <= MAX_ENVELOPE_BYTES; +} + +export function boundedJournalResult(result: object, snapshots: readonly string[][]): string { + // Reserve every omission slot before admitting any snapshot. + const journals: (string[] | typeof OVERFLOW)[] = snapshots.map(() => OVERFLOW); + const serialize = () => JSON.stringify({ ...result, journals }); + let envelope = serialize(); + if (!fitsEnvelope(envelope)) { + return JSON.stringify(result); + } + for (const [index, records] of snapshots.entries()) { + if (Buffer.byteLength(JSON.stringify(records)) > MAX_SNAPSHOT_BYTES) { + continue; + } + journals[index] = records; + const candidate = serialize(); + if (fitsEnvelope(candidate)) { + envelope = candidate; + } else { + journals[index] = OVERFLOW; + } + } + return envelope; +} + +export function readSubprocessSnapshot(value: unknown, index: number): Snapshot { + const entry: unknown = Array.isArray(value) ? value[index] : undefined; + if (entry === undefined) { + return MISSING; + } + if ( + entry === OVERFLOW || + (Array.isArray(entry) && Buffer.byteLength(JSON.stringify(entry)) > MAX_SNAPSHOT_BYTES) + ) { + return OVERFLOW; + } + const events = parseSubprocessJournal(entry); + return events.length === 0 ? INVALID : events; +} + +export function parseSubprocessJournal(value: unknown): DurableEvent[] { + if (!Array.isArray(value) || Buffer.byteLength(JSON.stringify(value)) > MAX_ENVELOPE_BYTES) { + 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; +} + +export interface SubprocessJournal { + readonly source: string; + readonly profile?: string; + readonly snapshot: Snapshot; +} + +/** Hold settled records from a joined synthetic child through its parent's assertions. */ +export function checkSubprocessJournals( + testCase: JournalCase, + journals: readonly SubprocessJournal[], + check: () => Operation, + sink: DeliverySink = process.stderr, +): Operation { + if (journals.length === 0) { + return check(); + } + return scoped(function* () { + const diagnostics = yield* useTestDiagnostics(sink, testCase); + const captures = journals.map(({ snapshot, source, profile }) => ({ + snapshot, + host: diagnostics.execution("child", source, profile).journal(() => false), + })); + let passed = false; + yield* ensure(function* () { + if (!passed) { + for (const { snapshot, host } of captures) { + if (typeof snapshot === "string") { + yield* host.reportSaveFailure(new Error(snapshot)); + } + } + } + }); + const run = captures.reduceRight<() => Operation>((next, { snapshot, host }) => { + if (typeof snapshot === "string") { + return next; + } + return () => + withFailureJournal(host, function* (buffer) { + for (const event of snapshot) { + yield* buffer.append(event); + } + return yield* next(); + }); + }, check); + const result = yield* scoped(run); + passed = true; + return result; + }); +} diff --git a/packages/test-support/tests/subprocess-journal.test.ts b/packages/test-support/tests/subprocess-journal.test.ts new file mode 100644 index 000000000..3096bddbc --- /dev/null +++ b/packages/test-support/tests/subprocess-journal.test.ts @@ -0,0 +1,55 @@ +import { Buffer } from "node:buffer"; +import { describe, it } from "../bdd.ts"; +import { expect } from "../expect.ts"; +import { serializeDurableEvent } from "@executablemd/durable-streams"; +import type { DurableEvent } from "@executablemd/durable-streams"; +import { boundedJournalResult, readSubprocessSnapshot } from "../subprocess-journal.ts"; + +const close: DurableEvent = { + type: "close", + coroutineId: "root", + result: { status: "ok", value: null }, +}; +const line = serializeDurableEvent(close); + +describe("private subprocess journal envelope", () => { + // deno-lint-ignore require-yield + it("preserves the original result when even the omission slots cannot fit", function* () { + const result = { kind: "selected", value: "é".repeat(32 * 1024) }; + const printed = JSON.parse(boundedJournalResult(result, [[line]])); + expect(printed).toEqual(result); + expect(readSubprocessSnapshot(printed.journals, 0)).toBe("snapshot absent"); + }); + + // deno-lint-ignore require-yield + it("keeps fixed slots and valid siblings across both byte limits", function* () { + const large = [ + serializeDurableEvent({ + ...close, + result: { status: "ok", value: "é".repeat(7 * 1024) }, + }), + ]; + const oversized = ["é".repeat(16 * 1024)]; + const printed = boundedJournalResult({ value: "é".repeat(12 * 1024) }, [ + large, + oversized, + large, + large, + [line], + ]); + expect(Buffer.byteLength(printed) + 1).toBeLessThanOrEqual(64 * 1024); + const { journals } = JSON.parse(printed); + expect(readSubprocessSnapshot(journals, 0)).toHaveLength(1); + expect(readSubprocessSnapshot(journals, 1)).toBe("snapshot exceeds journal budget"); + expect(readSubprocessSnapshot(journals, 2)).toHaveLength(1); + expect(readSubprocessSnapshot(journals, 3)).toBe("snapshot exceeds journal budget"); + expect(readSubprocessSnapshot(journals, 4)).toEqual([close]); + }); + + // deno-lint-ignore require-yield + it("rejects a partially valid snapshot without losing its sibling", function* () { + const { journals } = JSON.parse(boundedJournalResult({}, [[line, "malformed"], [line]])); + expect(readSubprocessSnapshot(journals, 0)).toBe("invalid snapshot"); + expect(readSubprocessSnapshot(journals, 1)).toEqual([close]); + }); +}); diff --git a/packages/testing/tests/use-testing.test.ts b/packages/testing/tests/use-testing.test.ts index 587beff6a..a65c1b46e 100644 --- a/packages/testing/tests/use-testing.test.ts +++ b/packages/testing/tests/use-testing.test.ts @@ -15,8 +15,12 @@ import { execute, Execution, registerComponents } from "@executablemd/core"; import type { Json } from "@executablemd/core"; import { Test, TestFailureError, testing } from "../src/test-api.ts"; import { useTesting } from "../src/use-testing.ts"; -import { captureTestJournal, journalBeforeEach } from "@executablemd/test-support/failure-journal"; import { failureOf, runDoc, withFreshDocument } from "./helpers.ts"; +import { + captureTestJournal, + journalBeforeEach, + useJournalStream, +} from "@executablemd/test-support/failure-journal"; const OWNER = "packages/testing/tests/use-testing.test.ts"; @@ -90,49 +94,39 @@ describe("useTesting composition", () => { }); 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); - }, - ); + const stream = yield* useJournalStream("README.md"); + 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* () { - 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"]]); - }, - ); + const stream = yield* useJournalStream("README.md"); + 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* () { @@ -150,6 +144,7 @@ describe("useTesting composition", () => { }); it("enforces one execute() call per session", function* () { + const stream = yield* useJournalStream("README.md"); // Results are cumulative across a session: without the guard, a // zero-test second document would succeed on the strength of the first // document's passing test. @@ -160,7 +155,7 @@ describe("useTesting composition", () => { "empty.md": "no tests here\n", }); yield* useTesting(); - const execution = yield* execute({ path: "README.md", stream: new InMemoryStream() }); + const execution = yield* execute({ path: "README.md", stream }); const result = yield* execution; try { yield* execute({ path: "empty.md", stream: new InMemoryStream() }); @@ -187,10 +182,11 @@ describe("useTesting composition", () => { }); it("removes all session middleware with its scope", function* () { + const stream = yield* useJournalStream("README.md"); yield* scoped(function* () { yield* useStubFs({ "README.md": "\n" }); yield* useTesting(); - const execution = yield* execute({ path: "README.md", stream: new InMemoryStream() }); + const execution = yield* execute({ path: "README.md", stream }); const result = yield* execution; expect(result.ok).toBe(true); }); @@ -208,48 +204,55 @@ describe("useTesting composition", () => { // enclosing scope, and each document gets a child scope with one session and // one execution. Wrapping a loop in a single session would instead make the // second document inherit the first document's outcomes. - it("runs sibling documents with shared fixtures and separate sessions", function* () { - const visited: string[] = []; - const outcome = yield* scoped(function* () { - yield* useStubFs({ - "first.md": '\n', - "second.md": '\n', - }); - yield* registerComponents([ - { - name: "Visit", - origin: "use-testing-fixture", - props: { - type: "object", - properties: { doc: { type: "string" } }, - additionalProperties: false, - }, - // deno-lint-ignore require-yield - *fn(props: Record): Operation { - visited.push(String(props.doc)); - return ""; - }, - }, - ]); + const siblings = "runs sibling documents with shared fixtures and separate sessions"; + it(siblings, function* () { + const label = { owner: OWNER, name: siblings }; + yield* captureTestJournal(label, "first.md", function* (firstStream) { + yield* captureTestJournal(label, "second.md", function* (secondStream) { + const visited: string[] = []; + const outcome = yield* scoped(function* () { + yield* useStubFs({ + "first.md": '\n', + "second.md": + '\n', + }); + yield* registerComponents([ + { + name: "Visit", + origin: "use-testing-fixture", + props: { + type: "object", + properties: { doc: { type: "string" } }, + additionalProperties: false, + }, + // deno-lint-ignore require-yield + *fn(props: Record): Operation { + visited.push(String(props.doc)); + return ""; + }, + }, + ]); - const first = yield* scoped(function* () { - const tests = yield* useTesting(); - const result = yield* yield* execute({ path: "first.md", stream: new InMemoryStream() }); - return { result, results: yield* tests.results }; - }); - const second = yield* scoped(function* () { - const tests = yield* useTesting(); - const result = yield* yield* execute({ path: "second.md", stream: new InMemoryStream() }); - return { result, results: yield* tests.results }; + const first = yield* scoped(function* () { + const tests = yield* useTesting(); + const result = yield* yield* execute({ path: "first.md", stream: firstStream }); + return { result, results: yield* tests.results }; + }); + const second = yield* scoped(function* () { + const tests = yield* useTesting(); + const result = yield* yield* execute({ path: "second.md", stream: secondStream }); + return { result, results: yield* tests.results }; + }); + return { first, second }; + }); + + expect(visited).toEqual(["first", "second"]); + expect(outcome.first.result.ok).toBe(false); + expect(outcome.first.results.map((r) => [r.name, r.status])).toEqual([["first", "fail"]]); + expect(outcome.second.result.ok).toBe(true); + expect(outcome.second.results.map((r) => [r.name, r.status])).toEqual([["second", "pass"]]); }); - return { first, second }; }); - - expect(visited).toEqual(["first", "second"]); - expect(outcome.first.result.ok).toBe(false); - expect(outcome.first.results.map((r) => [r.name, r.status])).toEqual([["first", "fail"]]); - expect(outcome.second.result.ok).toBe(true); - expect(outcome.second.results.map((r) => [r.name, r.status])).toEqual([["second", "pass"]]); }); it("completion returns Err instead of throwing for document failures", function* () { @@ -279,83 +282,70 @@ describe("useTesting composition", () => { }); it("a late subscriber still receives all chunks and the close value", function* () { - yield* captureTestJournal( - { owner: OWNER, name: "a late subscriber still receives all chunks and the close value" }, - "README.md", - function* (stream) { - const { late, value } = yield* scoped(function* () { - yield* useStubFs({ "README.md": "hello world\n" }); - yield* useTesting(); - const execution = yield* execute({ - path: "README.md", - stream, - }); - const result = yield* execution; - const drained = yield* scoped(function* () { - return yield* drain(yield* execution.output); - }); - return { late: drained, value: result }; - }); - // 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"); - }, - ); + const stream = yield* useJournalStream("README.md"); + 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* () { - yield* captureTestJournal( - { - owner: OWNER, - name: "an early caller-scope halt tears down the document and leased eval scope", + const stream = yield* useJournalStream("README.md"); + Reflect.deleteProperty(globalThis, "__useTestingHaltMarker"); + yield* API.Process.around({ + *exec(_args, _next) { + yield* sleep(10_000); + return { exitCode: 0, stdout: "", stderr: "" }; }, - "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 }); + }); + const doc = [ + "", + '', + "```js persist eval", + "globalThis.__useTestingHaltMarker = true;", + "yield* spawn(function* () { try { yield* suspend(); } finally { globalThis.__useTestingHaltMarker = false; } });", + "```", + "```bash exec", + "sleep 600", + "```", + "", + "", + "", + ].join("\n"); + yield* useStubFs({ "README.md": doc }); + yield* scoped(function* () { + yield* useTesting(); + const execution = yield* execute({ path: "README.md", stream }); + yield* spawn(function* () { yield* scoped(function* () { - yield* 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 }, - ); + 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 }, + ); + }); - expect(Reflect.get(globalThis, "__useTestingHaltMarker")).toBe(false); - }, - ); + expect(Reflect.get(globalThis, "__useTestingHaltMarker")).toBe(false); }); });