| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- import assert from "node:assert/strict";
- import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises";
- import { tmpdir } from "node:os";
- import { dirname, join } from "node:path";
- import { after, test } from "node:test";
- const INSPECTOR_URL = new URL("../lib/conversation-session-formal-admission.mjs", import.meta.url);
- const temporaryRoots = [];
- after(async () => {
- await Promise.all(temporaryRoots.map((root) => rm(root, { force: true, recursive: true })));
- });
- async function inspect(root) {
- let module;
- try {
- module = await import(INSPECTOR_URL.href);
- } catch (error) {
- assert.fail(`formal admission inspector is unavailable: ${error.message}`);
- }
- return module.inspectConversationSessionFormalAdmission(root);
- }
- async function makeRoot() {
- const root = await mkdtemp(join(tmpdir(), "zcode-formal-admission-"));
- temporaryRoots.push(root);
- return root;
- }
- async function writeText(root, relativePath, value) {
- const absolutePath = join(root, relativePath);
- await mkdir(dirname(absolutePath), { recursive: true });
- await writeFile(absolutePath, value, "utf8");
- }
- async function writeJson(root, relativePath, value) {
- await writeText(root, relativePath, `${JSON.stringify(value, null, 2)}\n`);
- }
- function request(
- id,
- { kind = "main", source = "capture", syntheticReason, timingPolicy = "fast-text" } = {},
- ) {
- return {
- id,
- kind,
- source,
- timingPolicy,
- ...(syntheticReason === undefined ? {} : { syntheticReason }),
- };
- }
- function providerEntry(manifestRequest) {
- const { id, ...metadata } = manifestRequest;
- return {
- id,
- metadata,
- match: { method: "POST", pathIncludes: "/messages" },
- response: { statusCode: 200, text: { text: "ok" } },
- };
- }
- async function addFormalCase(
- root,
- caseName,
- {
- caseFixture,
- commonFixture = { version: 1, fixtures: [] },
- manifest,
- specSource = "it('runs', async () => {});\n",
- writeCaseFixture = true,
- writeCommonFixture = true,
- writeManifest = true,
- } = {},
- ) {
- const spec = `./test/e2e/conversation-session/${caseName}.test.ts`;
- const caseFixturePath = `./test/e2e/fixtures/upstream/conversation-session/${caseName}.json`;
- const defaultRequest = request(`${caseName}-request`);
- const defaultManifest = {
- version: 1,
- caseName,
- spec,
- providerFixtures: ["./test/e2e/fixtures/upstream/common.json", caseFixturePath],
- fileFixtures: [],
- requests: [defaultRequest],
- };
- const defaultCaseFixture = {
- version: 1,
- caseName,
- spec,
- fixtures: [providerEntry(defaultRequest)],
- };
- await writeText(
- root,
- `packages/desktop/test/e2e/conversation-session/${caseName}.test.ts`,
- specSource,
- );
- if (writeCommonFixture) {
- await writeJson(root, "packages/desktop/test/e2e/fixtures/upstream/common.json", commonFixture);
- }
- if (writeCaseFixture) {
- await writeJson(
- root,
- `packages/desktop/test/e2e/fixtures/upstream/conversation-session/${caseName}.json`,
- caseFixture ?? defaultCaseFixture,
- );
- }
- if (writeManifest) {
- const manifestValue = manifest ?? defaultManifest;
- const manifestPath = `packages/desktop/test/e2e/fixtures/cases/conversation-session/${caseName}.json`;
- if (typeof manifestValue === "string") {
- await writeText(root, manifestPath, manifestValue);
- } else {
- await writeJson(root, manifestPath, manifestValue);
- }
- }
- return { caseFixturePath, defaultManifest, defaultRequest, spec };
- }
- function codes(result, caseName) {
- return result.formal.rejected
- .find((item) => item.caseName === caseName)
- ?.reasons.map((reason) => reason.code);
- }
- test("admits a valid V4 formal case and permits compatible TID_CHAT_TOOL usage", async () => {
- const root = await makeRoot();
- await addFormalCase(root, "valid-v4", {
- specSource:
- "const selector = '[data-testid=\"TID_CHAT_TOOL_RESULT\"]';\n" +
- "it('runs', async () => selector);\n",
- });
- const result = await inspect(root);
- assert.deepEqual(result.counts, {
- formalAdmitted: 1,
- formalRejected: 0,
- legacyHarness: 0,
- legacyHarnessFormal: 0,
- legacyHarnessPending: 0,
- missingFixture: 0,
- missingManifest: 0,
- pending: 0,
- pendingFixtureRejected: 0,
- });
- assert.deepEqual(result.formal.admitted, [
- {
- caseName: "valid-v4",
- specPath: "packages/desktop/test/e2e/conversation-session/valid-v4.test.ts",
- },
- ]);
- });
- test("treats an omitted optional fileFixtures field as an empty list", async () => {
- const root = await makeRoot();
- const values = await addFormalCase(root, "no-file-fixtures");
- const { fileFixtures: _fileFixtures, ...manifest } = values.defaultManifest;
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/no-file-fixtures.json",
- manifest,
- );
- const result = await inspect(root);
- assert.equal(result.counts.formalAdmitted, 1);
- assert.equal(result.counts.formalRejected, 0);
- });
- test("accepts a runtime-created file fixture descriptor without requiring it on disk", async () => {
- const root = await makeRoot();
- const values = await addFormalCase(root, "runtime-file-fixture");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/runtime-file-fixture.json",
- {
- ...values.defaultManifest,
- fileFixtures: [
- {
- description: "provider tool creates this file during the replay",
- path: "/tmp/generated-by-provider.txt",
- source: "created-by-provider-tool",
- },
- ],
- },
- );
- const result = await inspect(root);
- assert.equal(result.counts.formalAdmitted, 1);
- assert.equal(result.counts.formalRejected, 0);
- });
- test("rejects exact legacy helper imports and prepareConversationE2E calls", async () => {
- const root = await makeRoot();
- await addFormalCase(root, "legacy-import", {
- specSource:
- "import { helper } from '../helpers/conversation-session.js';\n" +
- "it('runs', async () => helper);\n",
- });
- await addFormalCase(root, "legacy-prepare", {
- specSource: "it('runs', async () => prepareConversationE2E());\n",
- });
- const result = await inspect(root);
- assert.equal(result.counts.formalRejected, 2);
- assert.equal(result.counts.legacyHarnessFormal, 2);
- assert.ok(codes(result, "legacy-import").includes("LEGACY_HELPER_IMPORT"));
- assert.ok(codes(result, "legacy-prepare").includes("LEGACY_PREPARE_CALL"));
- });
- test("rejects missing manifests and missing case-local fixtures", async () => {
- const root = await makeRoot();
- await addFormalCase(root, "missing-manifest", { writeManifest: false });
- await addFormalCase(root, "missing-fixture", { writeCaseFixture: false });
- const result = await inspect(root);
- assert.equal(result.counts.missingManifest, 1);
- assert.equal(result.counts.missingFixture, 1);
- assert.ok(codes(result, "missing-manifest").includes("MISSING_CASE_MANIFEST"));
- assert.ok(codes(result, "missing-fixture").includes("MISSING_CASE_FIXTURE"));
- });
- test("rejects invalid manifest JSON, version, case identity, and spec path", async () => {
- const root = await makeRoot();
- await addFormalCase(root, "invalid-json", { manifest: "{not-json\n" });
- const wrongVersion = await addFormalCase(root, "wrong-version");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/wrong-version.json",
- { ...wrongVersion.defaultManifest, version: 2 },
- );
- const wrongIdentity = await addFormalCase(root, "wrong-identity");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/wrong-identity.json",
- { ...wrongIdentity.defaultManifest, caseName: "someone-else" },
- );
- const wrongSpec = await addFormalCase(root, "wrong-spec");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/wrong-spec.json",
- { ...wrongSpec.defaultManifest, spec: "./test/e2e/other.test.ts" },
- );
- const wrongFixtureSpec = await addFormalCase(root, "wrong-fixture-spec");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/upstream/conversation-session/wrong-fixture-spec.json",
- {
- version: 1,
- caseName: "wrong-fixture-spec",
- spec: "./test/e2e/other.test.ts",
- fixtures: [providerEntry(wrongFixtureSpec.defaultRequest)],
- },
- );
- const result = await inspect(root);
- assert.ok(codes(result, "invalid-json").includes("INVALID_MANIFEST_JSON"));
- assert.ok(codes(result, "wrong-version").includes("INVALID_MANIFEST_VERSION"));
- assert.ok(codes(result, "wrong-identity").includes("MANIFEST_CASE_NAME_MISMATCH"));
- assert.ok(codes(result, "wrong-spec").includes("MANIFEST_SPEC_PATH_MISMATCH"));
- assert.ok(codes(result, "wrong-fixture-spec").includes("CASE_FIXTURE_SPEC_PATH_MISMATCH"));
- });
- test("rejects providerRequestPolicy none when requests are declared", async () => {
- const root = await makeRoot();
- const values = await addFormalCase(root, "policy-none");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/policy-none.json",
- { ...values.defaultManifest, providerRequestPolicy: "none" },
- );
- const result = await inspect(root);
- assert.ok(codes(result, "policy-none").includes("PROVIDER_REQUEST_POLICY_NONE_WITH_REQUESTS"));
- });
- test("rejects synthetic requests without a non-empty synthetic reason", async () => {
- const root = await makeRoot();
- const values = await addFormalCase(root, "synthetic-no-reason");
- const syntheticRequest = request("synthetic-request", {
- source: "synthetic",
- });
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/synthetic-no-reason.json",
- { ...values.defaultManifest, requests: [syntheticRequest] },
- );
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/upstream/conversation-session/synthetic-no-reason.json",
- { version: 1, fixtures: [providerEntry(syntheticRequest)] },
- );
- const result = await inspect(root);
- assert.ok(codes(result, "synthetic-no-reason").includes("SYNTHETIC_REASON_MISSING"));
- });
- test("rejects case-local request ledger ID, order, and metadata mismatches", async () => {
- const root = await makeRoot();
- const idValues = await addFormalCase(root, "ledger-id");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/ledger-id.json",
- { ...idValues.defaultManifest, requests: [request("invented-id")] },
- );
- const orderValues = await addFormalCase(root, "ledger-order");
- const first = request("first");
- const second = request("second");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/ledger-order.json",
- { ...orderValues.defaultManifest, requests: [second, first] },
- );
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/upstream/conversation-session/ledger-order.json",
- { version: 1, fixtures: [providerEntry(first), providerEntry(second)] },
- );
- const metadataValues = await addFormalCase(root, "ledger-metadata");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/ledger-metadata.json",
- {
- ...metadataValues.defaultManifest,
- requests: [request("ledger-metadata-request", { timingPolicy: "fault-stream" })],
- },
- );
- const result = await inspect(root);
- assert.ok(codes(result, "ledger-id").includes("REQUEST_LEDGER_ID_MISMATCH"));
- assert.ok(codes(result, "ledger-order").includes("REQUEST_LEDGER_ORDER_MISMATCH"));
- assert.ok(codes(result, "ledger-metadata").includes("REQUEST_LEDGER_METADATA_MISMATCH"));
- });
- test("rejects omitted case fixtures and missing declared provider or file paths", async () => {
- const root = await makeRoot();
- const omitted = await addFormalCase(root, "omitted-case-fixture");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/omitted-case-fixture.json",
- {
- ...omitted.defaultManifest,
- providerFixtures: ["./test/e2e/fixtures/upstream/common.json"],
- },
- );
- const missingProvider = await addFormalCase(root, "missing-provider-path");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/missing-provider-path.json",
- {
- ...missingProvider.defaultManifest,
- providerFixtures: [
- ...missingProvider.defaultManifest.providerFixtures,
- "./test/e2e/fixtures/upstream/not-there.json",
- ],
- },
- );
- const missingFile = await addFormalCase(root, "missing-file-path");
- await writeJson(
- root,
- "packages/desktop/test/e2e/fixtures/cases/conversation-session/missing-file-path.json",
- {
- ...missingFile.defaultManifest,
- fileFixtures: ["./test/e2e/fixtures/fs/not-there/input.txt"],
- },
- );
- const result = await inspect(root);
- assert.ok(codes(result, "omitted-case-fixture").includes("MANIFEST_OMITS_CASE_FIXTURE"));
- assert.ok(codes(result, "missing-provider-path").includes("MISSING_PROVIDER_FIXTURE"));
- assert.ok(codes(result, "missing-file-path").includes("MISSING_FILE_FIXTURE"));
- });
- test("counts a pending legacy spec without rejecting formal admission", async () => {
- const root = await makeRoot();
- await writeText(
- root,
- "packages/desktop/test/e2e/conversation-session/manual-review/pending/legacy.test.ts",
- "import { prepareConversationE2E } from '../../../helpers/conversation-session.js';\n" +
- "it('runs', async () => prepareConversationE2E());\n",
- );
- const result = await inspect(root);
- assert.equal(result.counts.formalAdmitted, 0);
- assert.equal(result.counts.formalRejected, 0);
- assert.equal(result.counts.pending, 1);
- assert.equal(result.counts.pendingFixtureRejected, 0);
- assert.equal(result.counts.legacyHarness, 1);
- assert.equal(result.counts.legacyHarnessPending, 1);
- assert.deepEqual(result.formal.rejected, []);
- });
- test("checks pending fixture metadata against the canonical formal target", async () => {
- const root = await makeRoot();
- const caseName = "pending-contract";
- const pendingSpec = `./test/e2e/conversation-session/manual-review/pending/${caseName}.test.ts`;
- const canonicalSpec = `./test/e2e/conversation-session/${caseName}.test.ts`;
- await writeText(
- root,
- `packages/desktop/test/e2e/conversation-session/manual-review/pending/${caseName}.test.ts`,
- "it('runs', async () => {});\n",
- );
- await writeJson(
- root,
- `packages/desktop/test/e2e/fixtures/cases/conversation-session/${caseName}.json`,
- { version: 1, caseName, spec: canonicalSpec },
- );
- await writeJson(
- root,
- `packages/desktop/test/e2e/fixtures/upstream/conversation-session/${caseName}.json`,
- { version: 1, caseName, spec: pendingSpec, fixtures: [] },
- );
- const result = await inspect(root);
- assert.equal(result.counts.pendingFixtureRejected, 1);
- assert.deepEqual(
- result.pending[0].reasons.map((reason) => reason.code),
- ["PENDING_FIXTURE_FORMAL_TARGET_MISMATCH"],
- );
- assert.equal(result.pending[0].canonicalSpecPath.endsWith(`${caseName}.test.ts`), true);
- });
|