import { readFileSync } from "node:fs"; import { dirname, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { describe, expect, it } from "vitest"; import { getToolcraftDecisionDiagnosticSeverity, getToolcraftDecisionRule, getToolcraftDecisionRulesByArea, TOOLCRAFT_DECISION_CONTRACT, } from "./decision-contracts"; import { TOOLCRAFT_PERFORMANCE_VERIFICATION_POLICY_TEXT } from "./performance-verification-policy"; const contractsDir = dirname(fileURLToPath(import.meta.url)); const repoRoot = resolve(contractsDir, "../../../.."); function readRepoFile(pathFromRoot: string): string { return readFileSync(resolve(repoRoot, pathFromRoot), "utf8"); } describe("Toolcraft template decision contract", () => { it("classifies every rule by level, verdict, and enforcement", () => { expect(TOOLCRAFT_DECISION_CONTRACT.length).toBeGreaterThanOrEqual(12); for (const rule of TOOLCRAFT_DECISION_CONTRACT) { expect(rule.id).toMatch(/^[a-z0-9-]+$/); expect(rule.area).toMatch( /^(runtime-shell|canvas|panels|layers|timeline|controls|renderer|reference-analysis|reference-clone|acceptance|performance|persistence|workflow)$/, ); expect(rule.title.trim().length).toBeGreaterThan(0); expect(rule.currentConstraint.trim().length).toBeGreaterThan(0); expect(rule.desiredBehavior.trim().length).toBeGreaterThan(0); expect(rule.enforcement.length).toBeGreaterThan(0); } }); it("keeps canvas app UI out of canvasContent as an invariant", () => { const rule = getToolcraftDecisionRule("canvas-no-app-ui"); expect(rule?.level).toBe("invariant"); expect(rule?.verdict).toBe("move-to-validator"); expect(rule?.enforcement).toContain("browser-helper"); expect(rule?.desiredBehavior).toMatch(/product result/i); expect(rule?.desiredBehavior).toMatch(/uploaded or imported content is part/i); expect(rule?.desiredBehavior).toMatch(/fake sample output/i); expect(rule?.desiredBehavior).toMatch(/agent-invented preset modes/i); expect(rule?.desiredBehavior).toMatch(/worklog records that evidence/i); expect(rule?.currentConstraint).toMatch(/uploaded\/imported source-material flows/i); }); it("requires infinite canvas exports to use canonical scene bounds", () => { const rule = getToolcraftDecisionRule("infinity-canvas-scene-bounds"); expect(rule?.level).toBe("invariant"); expect(rule?.desiredBehavior).toMatch(/sceneBoundsProvider/i); expect(rule?.desiredBehavior).toMatch(/useToolcraftProductSceneFrame/i); expect(rule?.desiredBehavior).toMatch(/live infinite preview/i); expect(rule?.desiredBehavior).toMatch(/one exact state/i); expect(rule?.desiredBehavior).toMatch(/runtime-owned timestamp schedule/i); expect(rule?.desiredBehavior).toMatch(/finite canvas size/i); expect(rule?.enforcement).toContain("acceptance-validator"); expect(rule?.enforcement).toContain("browser-helper"); expect(rule?.enforcement).toContain("runtime"); }); it("keeps runtime shell bypass prevention in validators, not only docs", () => { const rule = getToolcraftDecisionRule("runtime-shell-required"); expect(rule?.level).toBe("invariant"); expect(rule?.desiredBehavior).toMatch(/schema controls/i); expect(rule?.desiredBehavior).toMatch(/canvasContent/i); expect(rule?.desiredBehavior).toMatch(/controlRenderers/i); expect(rule?.desiredBehavior).toMatch(/onPanelAction/i); expect(rule?.desiredBehavior).toMatch(/low-level runtime surfaces/i); expect(rule?.desiredBehavior).toMatch(/built-in control components directly/i); expect(rule?.enforcement).toContain("cli-integrity-check"); expect(rule?.enforcement).toContain("acceptance-validator"); }); it("keeps layers and timeline as heuristics until enabled", () => { expect(getToolcraftDecisionRule("layers-enable-only-when-needed")?.level).toBe( "heuristic", ); expect(getToolcraftDecisionRule("layers-enabled-behavior")?.level).toBe( "invariant", ); expect( getToolcraftDecisionRule("layers-enabled-behavior")?.desiredBehavior, ).toMatch(/selectedLayer\.\*[^.]*two-entity isolation/i); expect(getToolcraftDecisionRule("timeline-mode-choice")?.level).toBe("heuristic"); expect(getToolcraftDecisionRule("timeline-mode-choice")?.desiredBehavior).toMatch( /Animation Intent Inventory/, ); expect(getToolcraftDecisionRule("timeline-enabled-behavior")?.level).toBe( "invariant", ); }); it("derives blocking severity from the canonical rule level", () => { expect(getToolcraftDecisionDiagnosticSeverity("controls-layout-heuristics")).toBe( "warning", ); expect(getToolcraftDecisionDiagnosticSeverity("runtime-shell-required")).toBe( "error", ); expect(getToolcraftDecisionDiagnosticSeverity("renderer-technique-inventory")).toBe( "error", ); }); it("separates mandatory section inventory and component fit from layout heuristics", () => { expect(getToolcraftDecisionRule("controls-section-inventory-required")?.level).toBe( "invariant", ); expect(getToolcraftDecisionRule("controls-component-layout-invariants")?.level).toBe( "invariant", ); expect(getToolcraftDecisionRule("controls-layout-heuristics")?.level).toBe( "heuristic", ); }); it("requires artifact delivery to correspond exactly to explicit product export intent", () => { const rule = getToolcraftDecisionRule("output-export-required"); expect(rule?.level).toBe("invariant"); expect(rule?.desiredBehavior).toMatch(/productReadiness\.exportIntent/); expect(rule?.desiredBehavior).toMatch(/image[^.]*Toolcraft default/i); expect(rule?.desiredBehavior).toMatch(/video[^.]*explicit user request[^.]*evidence/i); expect(rule?.desiredBehavior).toMatch(/SVG[^.]*explicit user request[^.]*evidence/i); expect(rule?.desiredBehavior).toMatch( /animation, playback, keyframes, or timeline[^.]*never/i, ); expect(rule?.desiredBehavior).toMatch(/image[^.]*removed[^.]*explicit user[^.]*evidence/i); expect(rule?.desiredBehavior).toMatch( /intent[^.]*correspond exactly[^.]*typed schema actions, applicable settings sections, and artifact-specific acceptance coverage/i, ); expect(rule?.desiredBehavior).toMatch(/SVG[^.]*editable vector[^.]*never permits raster/i); }); it("makes renderer technology a default with escape hatches", () => { const rule = getToolcraftDecisionRule("renderer-technique-inventory"); expect(rule?.level).toBe("default"); expect(rule?.desiredBehavior).toMatch(/product output semantics/i); expect(rule?.enforcement).toContain("performance-validator"); }); it("requires typed spatial view intent before renderer implementation", () => { const rule = getToolcraftDecisionRule("renderer-view-interaction"); expect(rule?.level).toBe("invariant"); expect(rule?.desiredBehavior).toMatch(/typed viewInteraction intent/i); expect(rule?.desiredBehavior).toMatch(/defaults to orbit/i); expect(rule?.desiredBehavior).toMatch(/explicit user request or inspected reference/i); expect(rule?.enforcement).toContain("acceptance-validator"); }); it("keeps model appearance and presentation runtime-owned", () => { const rule = getToolcraftDecisionRule("model-appearance-presentation"); expect(rule?.level).toBe("invariant"); expect(rule?.desiredBehavior).toMatch(/standalone files, folders, and bounded ZIP/i); expect(rule?.desiredBehavior).toMatch(/first normalized root deterministically/i); expect(rule?.desiredBehavior).toMatch(/fallback material only when authored appearance is absent/i); expect(rule?.desiredBehavior).toMatch(/original source package remains durable and immutable/i); expect(rule?.desiredBehavior).toMatch(/modelPresentation: runtime/i); expect(rule?.desiredBehavior).toMatch(/checked consumer/i); expect(rule?.desiredBehavior).toMatch(/renderDefaultCanvasMedia controls generic image\/file/i); expect(rule?.desiredBehavior).toMatch(/orientationGizmo/i); expect(rule?.enforcement).toContain("runtime"); expect(rule?.enforcement).toContain("cli-integrity-check"); }); it("requires one evidence-backed surface owner per user operation", () => { const rule = getToolcraftDecisionRule("interaction-surface-ownership"); expect(rule?.level).toBe("invariant"); expect(rule?.desiredBehavior).toMatch(/typed interactionOwnership/i); expect(rule?.desiredBehavior).toMatch(/user request, inspected reference/i); expect(rule?.desiredBehavior).toMatch(/product usability/i); expect(rule?.desiredBehavior).toMatch(/complementary operations/i); expect(rule?.desiredBehavior).toMatch(/cannot be mirrored/i); expect(rule?.enforcement).toContain("acceptance-validator"); }); it("requires reference clone functionality to be inventoried and acceptance-backed", () => { const rule = getToolcraftDecisionRule("reference-clone-source-of-truth"); expect(rule?.level).toBe("invariant"); expect(rule?.desiredBehavior).toMatch(/referenceStudy evidence/i); expect(rule?.desiredBehavior).toMatch(/running the original or restoring it locally/i); expect(rule?.desiredBehavior).toMatch(/reference feature inventory/i); expect(rule?.desiredBehavior).toMatch(/feature-level behavior evidence/i); expect(rule?.desiredBehavior).toMatch(/user-visible and output-affecting feature/i); expect(rule?.desiredBehavior).toMatch(/Toolcraft implementation/i); expect(rule?.desiredBehavior).toMatch(/acceptance coverage/i); expect(rule?.desiredBehavior).toMatch(/explicit user approval/i); expect(rule?.enforcement).toContain("acceptance-validator"); }); it("requires video references to be studied as behavior before implementation", () => { const rule = getToolcraftDecisionRule("video-reference-analysis"); expect(rule?.level).toBe("invariant"); expect(rule?.area).toBe("reference-analysis"); expect(rule?.desiredBehavior).toMatch(/video, GIF, screen recording/i); expect(rule?.desiredBehavior).toMatch(/typed referenceInputs/i); expect(rule?.desiredBehavior).toMatch(/full source scan/i); expect(rule?.desiredBehavior).toMatch(/dense 12 FPS review/i); expect(rule?.desiredBehavior).toMatch(/classifies every detected event/i); expect(rule?.desiredBehavior).toMatch(/complete contiguous phases/i); expect(rule?.desiredBehavior).toMatch(/browser reference-parity/i); expect(rule?.enforcement).toContain("acceptance-validator"); expect(rule?.enforcement).toContain("starter-agents"); }); it("splits performance into workload and lightweight responsiveness coverage", () => { const rule = getToolcraftDecisionRule("performance-coverage-levels"); expect(rule?.level).toBe("invariant"); expect(rule?.desiredBehavior).toMatch(/workload/i); expect(rule?.desiredBehavior).toMatch(/responsiveness/i); expect(rule?.desiredBehavior).toMatch(/canvas drag, pan, pinch, zoom, and radar\/center/i); expect(rule?.desiredBehavior).toMatch(/playback state/i); expect(rule?.desiredBehavior).toContain( TOOLCRAFT_PERFORMANCE_VERIFICATION_POLICY_TEXT, ); expect(rule?.desiredBehavior).toMatch( /functional delivery plan with basis "initial"[\s\S]*complete functional proof[\s\S]*no measured performance/i, ); expect(rule?.desiredBehavior).toMatch( /proven product has no later functional delivery plan[\s\S]*feature-focused tests or browser inspection/i, ); expect(rule?.desiredBehavior).toMatch(/development fixture/i); expect(rule?.desiredBehavior).toMatch( /complete performance matrix[\s\S]*never launch it automatically/i, ); expect(rule?.desiredBehavior).toMatch( /cannot claim targeted or full performance evidence/i, ); expect(rule?.desiredBehavior).toMatch(/verify:delivery/); expect(rule?.desiredBehavior).toMatch(/first product version/i); expect(rule?.desiredBehavior).toMatch( /changed files, verification tier, ownership classification, affected pass or path ids, and touched subsystems never authorize measured performance/i, ); expect(rule?.desiredBehavior).toMatch(/ordinary-product-work/i); expect(rule?.desiredBehavior).toMatch( /complete performance matrix remains outside conversational delivery/i, ); expect(rule?.desiredBehavior).toMatch(/performance-iteration/i); expect(rule?.desiredBehavior).toMatch(/bounded targeted iteration/i); expect(rule?.desiredBehavior).toMatch( /only a localized complaint or a post-clarification targeted choice creates performance-iteration intent and one bounded iteration over exact canonical affected paths/iu, ); expect(rule?.desiredBehavior).toMatch( /classifier output establishes complaint authority only and never path localization/iu, ); expect(rule?.desiredBehavior).toMatch( /regardless of whether the classifier returned high-confidence performance-iteration or needs-agent-judgment, unresolved localization creates neither performance-iteration intent nor canonical path authority/iu, ); expect(rule?.desiredBehavior).not.toMatch( /one complaint maps to one exact authority-selected targeted performance iteration/iu, ); expect(rule?.desiredBehavior).not.toMatch( /direct and repeated performance complaints each start one bounded targeted iteration/iu, ); expect(rule?.desiredBehavior).toMatch( /localized complaint[\s\S]*without asking the user/iu, ); expect(rule?.desiredBehavior).toMatch( /ambiguous or unresolved localization[\s\S]*one user-facing clarification[\s\S]*targeted diagnosis or a complete review/iu, ); expect(rule?.desiredBehavior).toMatch( /repetition alone never expands scope or selects the complete performance matrix/i, ); expect(rule?.desiredBehavior).toMatch( /explicit natural-language request for the complete audit/i, ); expect(rule?.desiredBehavior).toMatch(/user does not need to know the command name/i); expect(rule?.desiredBehavior).toMatch(/focused evidence for the functionality being edited/i); expect(rule?.desiredBehavior).toMatch( /those changes never authorize an aggregate functional gate or measured performance/i, ); expect(rule?.desiredBehavior).toMatch( /renderScaleCoverage "selected-backing-pixels" for interaction and steady, plus playback when timeline is enabled/i, ); expect(rule?.desiredBehavior).toMatch( /every measured path[\s\S]*devicePixelRatio × 2[\s\S]*each measured phase/iu, ); expect(rule?.desiredBehavior).toMatch( /never downsample[\s\S]*clamp the selected render scale to pass a performance budget/i, ); expect(rule?.desiredBehavior).toMatch(/durable baseline/i); }); it("requires default local persistence with exact reload coverage", () => { const rule = getToolcraftDecisionRule("persistence-policy-explicit"); expect(rule?.desiredBehavior).toMatch(/enabled by default/i); expect(rule?.desiredBehavior).toMatch(/storage none.*recorded reason/i); expect(rule?.desiredBehavior).toMatch(/binary repository.*IndexedDB/i); expect(rule?.desiredBehavior).toMatch(/every resolved slice/i); expect(rule?.desiredBehavior).toMatch(/reloads the page/i); expect(rule?.desiredBehavior).toMatch(/settings import\/export is not a substitute/i); expect(rule?.enforcement).toContain("acceptance-validator"); expect(rule?.enforcement).toContain("browser-helper"); }); it("groups rules by area", () => { expect(getToolcraftDecisionRulesByArea("canvas").map((rule) => rule.id)).toContain( "canvas-no-app-ui", ); }); it("uses neutral workflow rule ids in public contract data", () => { const legacyWorkflowRuleId = ["ai", "workflow", "required"].join("-"); expect(getToolcraftDecisionRule("workflow-required")?.area).toBe("workflow"); expect(getToolcraftDecisionRule(legacyWorkflowRuleId)).toBeUndefined(); }); it("keeps public docs, local docs, and app assembly instructions synced with rule ids", () => { const docsSource = readRepoFile( "apps/website/src/content/docs/toolcraft-decision-contract.md", ); const rootAgentsSource = readRepoFile("AGENTS.md"); const starterAgentsSource = readRepoFile("starter/AGENTS.md"); const starterDecisionSource = readRepoFile("starter/docs/toolcraft/decision-contract.md"); const starterDocsCheckSource = readRepoFile("starter/scripts/check-toolcraft-docs.mjs"); for (const rule of TOOLCRAFT_DECISION_CONTRACT) { expect(docsSource, `Docs page must list decision rule "${rule.id}".`).toContain(rule.id); expect(rootAgentsSource, `Root AGENTS.md must list decision rule "${rule.id}".`).toContain( rule.id, ); expect( starterAgentsSource, `Starter AGENTS.md must list decision rule "${rule.id}" for generated apps.`, ).toContain(rule.id); expect( starterDecisionSource, `Starter local decision docs must list decision rule "${rule.id}".`, ).toContain(rule.id); } expect(starterDocsCheckSource).toContain("toolcraftDecisionRuleIds"); expect(starterDocsCheckSource).toContain( "getToolcraftContractFragmentFailures", ); }); it("keeps root AGENTS.md as a concise entry map instead of duplicated rule prose", () => { const rootAgentsSource = readRepoFile("AGENTS.md"); const rootAgentsLines = rootAgentsSource.trim().split(/\r?\n/); const forbiddenDuplicatedSectionHeadings = [ "Reference Runtime Clone Mode", "Always-On Canvas Capabilities", "Optional Surfaces", "Panel Rules", "Control Rules", "App Completion Bar", ]; const requiredEntryMapRefs = [ "starter/docs/toolcraft/assembly-workflow.md", "starter/docs/toolcraft/decision-contract.md", "starter/docs/toolcraft/schema-reference.md", "starter/docs/toolcraft/component-rules.md", "starter/docs/toolcraft/acceptance-testing.md", "starter/docs/toolcraft/performance.md", "starter/docs/toolcraft/renderer-technique.md", "starter/docs/toolcraft/custom-controls.md", "packages/toolcraft-runtime/src/contracts/decision-contracts.ts", "packages/toolcraft-runtime/src/contracts/component-contracts.ts", ]; expect(rootAgentsLines.length).toBeLessThanOrEqual(180); expect(rootAgentsSource).toContain("## Quick Entry Contract"); expect(rootAgentsSource).toContain("## Detailed References"); for (const heading of forbiddenDuplicatedSectionHeadings) { expect(rootAgentsSource, `Root AGENTS.md should link out instead of duplicating "${heading}".`).not.toContain( `## ${heading}`, ); } for (const ref of requiredEntryMapRefs) { expect(rootAgentsSource, `Root AGENTS.md should link to "${ref}".`).toContain(ref); } }); it("keeps required local Toolcraft docs available for generated apps", () => { const requiredLocalDocs = [ "README.md", "assembly-workflow.md", "decision-contract.md", "schema-reference.md", "acceptance-testing.md", "performance.md", "renderer-technique.md", "custom-controls.md", "component-rules.md", ]; for (const docName of requiredLocalDocs) { expect(() => readRepoFile(`starter/docs/toolcraft/${docName}`)).not.toThrow(); } }); });