|
| 1 | +import { expect, test } from "bun:test"; |
| 2 | +import type { RuntimeCapabilities } from "@intx/types/runtime-capabilities"; |
| 3 | +import type { WorkflowRole } from "@corbits-builder/schemas/installed-workflows"; |
| 4 | +import { createArtifactTools } from "./tools.ts"; |
| 5 | + |
| 6 | +const deployment = { |
| 7 | + source: { |
| 8 | + kind: "asset" as const, |
| 9 | + assetId: "asset", |
| 10 | + package: { format: "tarball" as const }, |
| 11 | + }, |
| 12 | + entry: "entry", |
| 13 | + pin: "@corbits-builder/writer-native@1.0.0", |
| 14 | + sourceOfferingIds: ["offering"], |
| 15 | + defaultSourceOfferingId: "offering", |
| 16 | +}; |
| 17 | + |
| 18 | +const sampleApproaches = { |
| 19 | + approaches: [ |
| 20 | + { |
| 21 | + id: "simple", |
| 22 | + name: "Simple", |
| 23 | + summary: "Client-side forecast view.", |
| 24 | + bestFor: "Fast launch", |
| 25 | + pros: ["Fast to build"], |
| 26 | + cons: ["No server cache"], |
| 27 | + effort: "low" as const, |
| 28 | + risks: ["Rate limits"], |
| 29 | + }, |
| 30 | + { |
| 31 | + id: "cached", |
| 32 | + name: "Cached", |
| 33 | + summary: "Server-cached forecast view.", |
| 34 | + bestFor: "Higher traffic", |
| 35 | + pros: ["Fewer upstream calls"], |
| 36 | + cons: ["More moving parts"], |
| 37 | + effort: "medium" as const, |
| 38 | + risks: ["Stale cache"], |
| 39 | + }, |
| 40 | + ], |
| 41 | + recommended: "simple", |
| 42 | +}; |
| 43 | + |
| 44 | +function harness(role: WorkflowRole) { |
| 45 | + const written: unknown[] = []; |
| 46 | + const fetch = async (path: string, init: RequestInit) => { |
| 47 | + if (path === "/api/workflow-project-scope") |
| 48 | + return Response.json({ |
| 49 | + tenantId: "tenant", |
| 50 | + projectId: "sb-project1", |
| 51 | + domain: "example.invalid", |
| 52 | + role, |
| 53 | + workflows: { |
| 54 | + writer: deployment, |
| 55 | + reviewer: deployment, |
| 56 | + coder: deployment, |
| 57 | + designer: deployment, |
| 58 | + }, |
| 59 | + openNetwork: false, |
| 60 | + build: { |
| 61 | + assetDirectory: "/tmp", |
| 62 | + browserCli: "/tmp/browser", |
| 63 | + childPath: "/tmp/child.ts", |
| 64 | + dependencyRoots: ["/tmp"], |
| 65 | + ripgrepPath: "/tmp/rg", |
| 66 | + runtimeVersion: "1.0.0", |
| 67 | + }, |
| 68 | + }); |
| 69 | + if (path.endsWith("/artifacts") && init.method === "POST") { |
| 70 | + const body = JSON.parse(String(init.body)) as { kind: string }; |
| 71 | + written.push(body); |
| 72 | + return Response.json({ |
| 73 | + artifact: { |
| 74 | + artifactId: "art-" + body.kind, |
| 75 | + kind: body.kind, |
| 76 | + version: written.length, |
| 77 | + sha256: "a".repeat(64), |
| 78 | + }, |
| 79 | + }); |
| 80 | + } |
| 81 | + return Response.json({ code: "unexpected_route" }, { status: 404 }); |
| 82 | + }; |
| 83 | + const capabilities = { |
| 84 | + resolve: () => ({ resolve: async () => ({ kind: "http", fetch }) }), |
| 85 | + } as unknown as RuntimeCapabilities; |
| 86 | + const instance = createArtifactTools(role)({ capabilities } as never); |
| 87 | + return { |
| 88 | + definitions: instance.definitions, |
| 89 | + written, |
| 90 | + write: (args: Record<string, unknown>) => |
| 91 | + instance.run( |
| 92 | + { id: "call", name: "write_artifact", arguments: args }, |
| 93 | + new AbortController().signal, |
| 94 | + ), |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +test("coordinator write_artifact exposes only problem_brief without approaches", async () => { |
| 99 | + const coordinator = harness("coordinator"); |
| 100 | + const definition = coordinator.definitions.find( |
| 101 | + (item) => item.name === "write_artifact", |
| 102 | + ); |
| 103 | + expect(definition).toBeDefined(); |
| 104 | + expect(definition?.description).not.toContain("approaches"); |
| 105 | + const properties = ( |
| 106 | + definition?.inputSchema as |
| 107 | + | { properties?: Record<string, unknown> } |
| 108 | + | undefined |
| 109 | + )?.properties; |
| 110 | + expect(properties?.["kind"]).toEqual({ const: "problem_brief" }); |
| 111 | + expect(properties?.["approaches"]).toBeUndefined(); |
| 112 | + |
| 113 | + const withApproaches = await coordinator.write({ |
| 114 | + kind: "problem_brief", |
| 115 | + title: "Problem Brief", |
| 116 | + content: "# Problem Brief\n\nWeather at a glance.", |
| 117 | + approaches: sampleApproaches, |
| 118 | + }); |
| 119 | + expect(withApproaches.isError).toBe(true); |
| 120 | + expect(withApproaches.content).toContain("approaches"); |
| 121 | + |
| 122 | + const wrongKind = await coordinator.write({ |
| 123 | + kind: "constraints", |
| 124 | + title: "Constraints", |
| 125 | + content: "# Constraints", |
| 126 | + }); |
| 127 | + expect(wrongKind.isError).toBe(true); |
| 128 | + expect(wrongKind.content).toBe( |
| 129 | + "brief_only: you write the problem brief; deploy_writer has a writer write the constraints", |
| 130 | + ); |
| 131 | + |
| 132 | + const valid = await coordinator.write({ |
| 133 | + kind: "problem_brief", |
| 134 | + title: "Problem Brief", |
| 135 | + content: "# Problem Brief\n\nWeather at a glance.", |
| 136 | + }); |
| 137 | + expect(valid.isError).toBeUndefined(); |
| 138 | + expect(coordinator.written).toHaveLength(1); |
| 139 | +}); |
| 140 | + |
| 141 | +test("writer write_artifact exposes only writer kinds and validates approaches", async () => { |
| 142 | + const writer = harness("writer"); |
| 143 | + const definition = writer.definitions.find( |
| 144 | + (item) => item.name === "write_artifact", |
| 145 | + ); |
| 146 | + const properties = ( |
| 147 | + definition?.inputSchema as |
| 148 | + | { |
| 149 | + properties?: { kind?: { enum?: string[] }; approaches?: unknown }; |
| 150 | + } |
| 151 | + | undefined |
| 152 | + )?.properties; |
| 153 | + expect(properties?.kind?.enum).toEqual([ |
| 154 | + "approaches", |
| 155 | + "build_plan", |
| 156 | + "constraints", |
| 157 | + "product_requirements", |
| 158 | + ]); |
| 159 | + expect(properties?.approaches).toBeDefined(); |
| 160 | + |
| 161 | + const briefAttempt = await writer.write({ |
| 162 | + kind: "problem_brief", |
| 163 | + title: "Problem Brief", |
| 164 | + content: "# Problem Brief", |
| 165 | + }); |
| 166 | + expect(briefAttempt.isError).toBe(true); |
| 167 | + expect(briefAttempt.content).toStartWith("invalid_arguments:"); |
| 168 | + |
| 169 | + const validApproaches = await writer.write({ |
| 170 | + kind: "approaches", |
| 171 | + title: "Approaches", |
| 172 | + content: "# Approaches", |
| 173 | + approaches: sampleApproaches, |
| 174 | + }); |
| 175 | + expect(validApproaches.isError).toBeUndefined(); |
| 176 | + expect(writer.written).toHaveLength(1); |
| 177 | +}); |
0 commit comments