diff --git a/scripts/canary-run.ts b/scripts/canary-run.ts index 0fa9f8a7..a831a04f 100644 --- a/scripts/canary-run.ts +++ b/scripts/canary-run.ts @@ -122,7 +122,10 @@ export function canaryLaunchEnvironment( delete environment.TYPESAFE_API_KEY; delete environment.FLOW_EVAL_NATIVE_AUTH; for (const name of Object.keys(environment)) - if (name.startsWith("OPENCODE_") && name !== "OPENCODE_API_KEY") + if ( + (name.startsWith("OPENCODE_") && name !== "OPENCODE_API_KEY") || + name.startsWith("GIT_") + ) delete environment[name]; for (const name of [ "NODE_OPTIONS", @@ -354,6 +357,24 @@ async function launchCanary( await realpath(join(dirname(directory), CANARY_PLUGIN_DIRECTORY)), attachment.plugin.package, ]); + const entryRoots = new Map([ + [ + await realpath( + join(dirname(directory), CANARY_PLUGIN_DIRECTORY, "index.js"), + ), + await realpath(join(dirname(directory), CANARY_PLUGIN_DIRECTORY)), + ], + [ + await realpath( + join(dirname(directory), CANARY_PLUGIN_DIRECTORY, "dist/index.js"), + ), + await realpath(join(dirname(directory), CANARY_PLUGIN_DIRECTORY)), + ], + [ + await realpath(join(attachment.plugin.package, "index.ts")), + attachment.plugin.package, + ], + ]); for (;;) { signal.throwIfAborted(); if (Date.now() >= deadline) @@ -372,12 +393,8 @@ async function launchCanary( if (plugin.source.type !== "local") throw new Error("Unprepared canary plugin loaded."); const path = await realpath(plugin.source.path); - const base = expected.has(path) - ? path - : /[/\\]index\.(?:ts|js)$/.test(path) - ? dirname(path) - : path; - if (!expected.has(base) || plugin.state.status !== "active") + const base = expected.has(path) ? path : entryRoots.get(path); + if (!base || !expected.has(base) || plugin.state.status !== "active") throw new Error("Unprepared or failed canary plugin loaded."); active.add(base); } diff --git a/scripts/canary-workspace.ts b/scripts/canary-workspace.ts new file mode 100644 index 00000000..674f3f99 --- /dev/null +++ b/scripts/canary-workspace.ts @@ -0,0 +1,67 @@ +import { spawnSync } from "node:child_process"; +import { mkdir, realpath, writeFile } from "node:fs/promises"; +import { join } from "node:path"; + +function git(directory: string, args: readonly string[]): string { + const env = { ...process.env }; + for (const name of Object.keys(env)) { + if (name.startsWith("GIT_")) delete env[name]; + } + env.GIT_CONFIG_NOSYSTEM = "1"; + env.GIT_CONFIG_GLOBAL = process.platform === "win32" ? "NUL" : "/dev/null"; + const result = spawnSync( + "git", + [ + "-c", + `core.hooksPath=${join(directory, ".git", "disabled-hooks")}`, + "-C", + directory, + ...args, + ], + { env, encoding: "utf8", timeout: 10000, maxBuffer: 1024 * 1024 }, + ); + if (result.status !== 0 || typeof result.stdout !== "string") { + throw new Error("Canary fixture requires a committed Git workspace."); + } + return result.stdout.trim(); +} + +export async function assertCanaryWorkspace(directory: string): Promise { + try { + const expected = await realpath(directory); + if ( + (await realpath(git(expected, ["rev-parse", "--show-toplevel"]))) !== + expected + ) { + throw new Error("Workspace root differs."); + } + git(expected, ["rev-parse", "--verify", "HEAD^{commit}"]); + } catch { + throw new Error("Canary fixture requires a committed Git workspace."); + } +} + +export async function prepareCanaryWorkspace(directory: string): Promise { + const workspace = await realpath(directory); + await mkdir(join(workspace, ".git")); + await writeFile( + join(workspace, ".gitignore"), + ".flow/\n.opencode/\nnode_modules/\n", + { flag: "wx" }, + ); + git(workspace, ["init", "--initial-branch=main"]); + git(workspace, ["add", "--all"]); + git(workspace, [ + "-c", + "user.name=Flow Canary", + "-c", + "user.email=flow-canary@example.invalid", + "-c", + "commit.gpgsign=false", + "commit", + "--no-verify", + "-m", + "Prepare canary fixture", + ]); + await assertCanaryWorkspace(workspace); +} diff --git a/scripts/eval-canary.ts b/scripts/eval-canary.ts index a80022ee..2f654a09 100644 --- a/scripts/eval-canary.ts +++ b/scripts/eval-canary.ts @@ -50,6 +50,10 @@ import { } from "../src/domain/operation.js"; import { currentRun } from "../src/domain/session.js"; import { readWorkspaceTestReport } from "../src/infrastructure/fs/workspace-validation.js"; +import { + assertCanaryWorkspace, + prepareCanaryWorkspace, +} from "./canary-workspace.js"; export const CANARY_CHECKLIST_VERSION = "phase9-canary-v1"; export const CANARY_DERIVATION_VERSION = "canary-evidence-v1"; @@ -70,6 +74,11 @@ export const CANARY_CHECK_IDS = [ "closes-with-delivery", ] as const; export const CANARY_MAX_AGE_MS = 72 * 60 * 60 * 1_000; +export const CANARY_PLUGIN_LOADER = + 'export { default } from "./dist/index.js";\n'; +export const CANARY_PLUGIN_LOADER_SHA256 = sha256( + Buffer.from(CANARY_PLUGIN_LOADER), +); const checklist = { version: CANARY_CHECKLIST_VERSION, @@ -223,6 +232,7 @@ const NativeInstallationEvidenceSchema = sourcePackageSha256: DigestSchema, tuiEntrySha256: DigestSchema, pluginPackageSha256: DigestSchema, + loaderEntrySha256: z.literal(CANARY_PLUGIN_LOADER_SHA256), configSha256: DigestSchema, nativeAuth: NativeAuthInstallationSchema.optional(), }).strict(); @@ -1170,7 +1180,8 @@ function canaryPluginPackage( name: metadata.name, version: metadata.version, type: "module", - exports: { ".": "./index.js", "./tui": "./tui.js" }, + main: "./dist/index.js", + exports: { ".": "./dist/index.js", "./tui": "./dist/tui.js" }, dependencies: { ...metadata.dependencies, "@opencode/plugin": OPENCODE_HOST_VERSION, @@ -1267,6 +1278,7 @@ const NativePreparationSchema = z sourcePackageSha256: DigestSchema, tuiEntrySha256: DigestSchema, pluginPackageSha256: DigestSchema, + loaderEntrySha256: z.literal(CANARY_PLUGIN_LOADER_SHA256), configSha256: DigestSchema, nativeAuth: CanaryNativeAuthSchema.optional(), }) @@ -1528,7 +1540,7 @@ export async function prepareCanary(input: { } const artifact = input.expectedArtifact ?? inspectedArtifact; const fixture = join(input.outputDirectory, "fixture"); - await mkdir(join(input.outputDirectory, CANARY_PLUGIN_DIRECTORY), { + await mkdir(join(input.outputDirectory, CANARY_PLUGIN_DIRECTORY, "dist"), { recursive: true, }); const copiedArtifactPath = join(input.outputDirectory, "artifact.tgz"); @@ -1564,8 +1576,9 @@ export async function prepareCanary(input: { const pluginPackage = canaryPluginPackage(packageMetadata); const config = canaryConfig(); for (const [name, bytes] of [ - ["index.js", pluginEntry], - ["tui.js", tuiEntry], + ["index.js", Buffer.from(CANARY_PLUGIN_LOADER)], + ["dist/index.js", pluginEntry], + ["dist/tui.js", tuiEntry], ["package.json", pluginPackage], ] as const) await writeFile( @@ -1578,6 +1591,7 @@ export async function prepareCanary(input: { sourcePackageSha256: sha256(sourcePackage), tuiEntrySha256: sha256(tuiEntry), pluginPackageSha256: sha256(pluginPackage), + loaderEntrySha256: CANARY_PLUGIN_LOADER_SHA256, configSha256: sha256(config), }; if (input.nativeAuth) { @@ -1600,6 +1614,7 @@ export async function prepareCanary(input: { join(fixture, "README.md"), `# Flow exact-artifact canary\n\nArtifact: ${artifact.tarballSha256}\n\nRun every checklist item and export sanitized JSON session and transcript evidence.\n`, ); + await prepareCanaryWorkspace(fixture); const base = { schemaVersion: 2 as const, runtime, @@ -1675,7 +1690,7 @@ export async function measureCanaryInstallation( join( directory, prepared.schemaVersion === 2 - ? `${CANARY_PLUGIN_DIRECTORY}/index.js` + ? `${CANARY_PLUGIN_DIRECTORY}/dist/index.js` : "fixture/.opencode/plugins/flow.js", ), ), @@ -1690,21 +1705,25 @@ export async function measureCanaryInstallation( ); } if (prepared.schemaVersion === 2) { - const [sourcePackage, tuiEntry, pluginPackage, config] = await Promise.all([ - Promise.resolve( - extractArtifactFile( - join(directory, prepared.artifactFile), - "package/package.json", + await assertCanaryWorkspace(join(directory, "fixture")); + const [sourcePackage, tuiEntry, pluginPackage, config, loaderEntry] = + await Promise.all([ + Promise.resolve( + extractArtifactFile( + join(directory, prepared.artifactFile), + "package/package.json", + ), ), - ), - readFile(join(directory, CANARY_PLUGIN_DIRECTORY, "tui.js")), - readFile(join(directory, CANARY_PLUGIN_DIRECTORY, "package.json")), - readFile(join(directory, "fixture/opencode.json")), - ]); + readFile(join(directory, CANARY_PLUGIN_DIRECTORY, "dist/tui.js")), + readFile(join(directory, CANARY_PLUGIN_DIRECTORY, "package.json")), + readFile(join(directory, "fixture/opencode.json")), + readFile(join(directory, CANARY_PLUGIN_DIRECTORY, "index.js")), + ]); const measured = { sourcePackageSha256: sha256(sourcePackage), tuiEntrySha256: sha256(tuiEntry), pluginPackageSha256: sha256(pluginPackage), + loaderEntrySha256: sha256(loaderEntry), configSha256: sha256(config), }; const { @@ -1718,6 +1737,7 @@ export async function measureCanaryInstallation( if ( canonicalJson(measured) !== canonicalJson(expected) || !pluginPackage.equals(canaryPluginPackage(metadata)) || + !loaderEntry.equals(Buffer.from(CANARY_PLUGIN_LOADER)) || !config.equals(canaryConfig()) || sha256( extractArtifactFile( diff --git a/tests/canary-run.test.ts b/tests/canary-run.test.ts index 95d3d683..d5c9fc39 100644 --- a/tests/canary-run.test.ts +++ b/tests/canary-run.test.ts @@ -17,10 +17,13 @@ import { canaryProviderConfiguration, runPaidCanary, } from "../scripts/canary-run.js"; +import { prepareCanaryWorkspace } from "../scripts/canary-workspace.js"; import { artifactIdentitySha256, CANARY_CHECKLIST_SHA256, CANARY_CHECKLIST_VERSION, + CANARY_PLUGIN_LOADER, + CANARY_PLUGIN_LOADER_SHA256, preparedCanarySha256, } from "../scripts/eval-canary.js"; import { authorizePaidRun, paidRunStatus } from "../scripts/paid-budget.js"; @@ -42,7 +45,7 @@ async function fixture() { ); directories.push(directory); const pluginDirectory = join(directory, "fixture/.opencode/plugins/flow"); - await mkdir(pluginDirectory, { recursive: true }); + await mkdir(join(pluginDirectory, "dist"), { recursive: true }); const hash = (text: string | Uint8Array) => `sha256:${createHash("sha256").update(text).digest("hex")}`; const sourcePackage = JSON.stringify({ @@ -55,7 +58,8 @@ async function fixture() { name: "opencode-plugin-flow", version: "10.0.0", type: "module", - exports: { ".": "./index.js", "./tui": "./tui.js" }, + main: "./dist/index.js", + exports: { ".": "./dist/index.js", "./tui": "./dist/tui.js" }, dependencies: { "@opencode/plugin": "2.1.4" }, }); const config = canonicalJson({ @@ -73,8 +77,9 @@ async function fixture() { ["package/package.json", sourcePackage], ["package/dist/index.js", "plugin"], ["package/dist/tui.js", "tui"], - ["fixture/.opencode/plugins/flow/index.js", "plugin"], - ["fixture/.opencode/plugins/flow/tui.js", "tui"], + ["fixture/.opencode/plugins/flow/index.js", CANARY_PLUGIN_LOADER], + ["fixture/.opencode/plugins/flow/dist/index.js", "plugin"], + ["fixture/.opencode/plugins/flow/dist/tui.js", "tui"], ["fixture/.opencode/plugins/flow/package.json", pluginPackage], ["fixture/opencode.json", config], ] as const) @@ -109,6 +114,7 @@ async function fixture() { sourcePackageSha256: hash(sourcePackage), tuiEntrySha256: hash("tui"), pluginPackageSha256: hash(pluginPackage), + loaderEntrySha256: CANARY_PLUGIN_LOADER_SHA256, configSha256: hash(config), }, }; @@ -116,6 +122,7 @@ async function fixture() { join(directory, "prepared.json"), JSON.stringify({ ...base, sha256: preparedCanarySha256(base) }), ); + await prepareCanaryWorkspace(join(directory, "fixture")); await writeFile(join(directory, "prompt.txt"), "Run fixture"); await authorizePaidRun(directory, { schemaVersion: 1, @@ -219,7 +226,12 @@ test.skipIf(process.platform === "win32")( }, ); -for (const path of ["index.js", "tui.js", "package.json"] as const) +for (const path of [ + "index.js", + "dist/index.js", + "dist/tui.js", + "package.json", +] as const) test(`changed prepared ${path} fails before dispatch consumption`, async () => { const { directory, input } = await fixture(); await writeFile( @@ -350,3 +362,53 @@ test("canary provider configuration uses the native HTTP transport setting for t }, }); }); + +for (const state of ["absent", "without commit"] as const) + test(`canary refuses ${state} Git workspace before charging`, async () => { + const { directory, input } = await fixture(); + const workspace = join(directory, "fixture"); + await rm(join(workspace, ".git"), { recursive: true, force: true }); + if (state === "without commit") { + const child = Bun.spawn(["git", "init", "--initial-branch=main"], { + cwd: workspace, + stdout: "ignore", + stderr: "pipe", + }); + expect(await child.exited).toBe(0); + } + let launches = 0; + await expect( + runPaidCanary(input, async () => { + launches++; + return 0; + }), + ).rejects.toThrow("committed Git workspace"); + expect(launches).toBe(0); + expect((await paidRunStatus(directory)).consumed).toBe(0); + }); + +test("canary does not accept an ancestor Git repository as its fixture", async () => { + const { directory, input } = await fixture(); + await rm(join(directory, "fixture/.git"), { recursive: true }); + await prepareCanaryWorkspace(directory); + let launches = 0; + await expect( + runPaidCanary(input, async () => { + launches++; + return 0; + }), + ).rejects.toThrow("committed Git workspace"); + expect(launches).toBe(0); + expect((await paidRunStatus(directory)).consumed).toBe(0); +}); + +test("canary Git environment cannot redirect its prepared workspace", () => { + expect( + canaryLaunchEnvironment("/prepared/fixture", { + GIT_DIR: "/other/.git", + GIT_WORK_TREE: "/other", + GIT_CONFIG_GLOBAL: "/other/config", + FLOW_CANARY_SENTINEL: "kept", + }), + ).toEqual({ PWD: "/prepared/fixture", FLOW_CANARY_SENTINEL: "kept" }); +}); diff --git a/tests/canary-v2-live.test.ts b/tests/canary-v2-live.test.ts index f6890220..7ee4e94c 100644 --- a/tests/canary-v2-live.test.ts +++ b/tests/canary-v2-live.test.ts @@ -17,10 +17,13 @@ import { join } from "node:path"; import { canonicalJson } from "../evals/canonical-json.js"; import { unpackedManifestSha256 } from "../evals/provenance.js"; import { runPaidCanary } from "../scripts/canary-run.js"; +import { prepareCanaryWorkspace } from "../scripts/canary-workspace.js"; import { artifactIdentitySha256, CANARY_CHECKLIST_SHA256, CANARY_CHECKLIST_VERSION, + CANARY_PLUGIN_LOADER, + CANARY_PLUGIN_LOADER_SHA256, deriveCanaryResult, preparedCanarySha256, } from "../scripts/eval-canary.js"; @@ -189,7 +192,8 @@ export default Plugin.define({ id: "flow-canary-local-proof", async setup(ctx) { name: "opencode-plugin-flow", version: "10.0.0", type: "module", - exports: { ".": "./index.js", "./tui": "./tui.js" }, + main: "./dist/index.js", + exports: { ".": "./dist/index.js", "./tui": "./dist/tui.js" }, dependencies: { "@opencode/plugin": "2.1.4" }, }); const config = canonicalJson({ @@ -202,12 +206,14 @@ export default Plugin.define({ id: "flow-canary-local-proof", async setup(ctx) { update: "disable", share: "disabled", }); + await mkdir(join(directory, "dist"), { recursive: true }); for (const [path, bytes] of [ ["package/package.json", metadata], ["package/dist/index.js", entry], ["package/dist/tui.js", tui], - ["fixture/.opencode/plugins/flow/index.js", entry], - ["fixture/.opencode/plugins/flow/tui.js", tui], + ["fixture/.opencode/plugins/flow/index.js", CANARY_PLUGIN_LOADER], + ["fixture/.opencode/plugins/flow/dist/index.js", entry], + ["fixture/.opencode/plugins/flow/dist/tui.js", tui], ["fixture/.opencode/plugins/flow/package.json", pluginPackage], ["fixture/opencode.json", config], ] as const) @@ -248,11 +254,13 @@ export default Plugin.define({ id: "flow-canary-local-proof", async setup(ctx) { sourcePackageSha256: digest(metadata), tuiEntrySha256: digest(tui), pluginPackageSha256: digest(pluginPackage), + loaderEntrySha256: CANARY_PLUGIN_LOADER_SHA256, configSha256: digest(config), }, }; const prepared = { ...base, sha256: preparedCanarySha256(base) }; await writeFile(join(root, "prepared.json"), JSON.stringify(prepared)); + await prepareCanaryWorkspace(join(root, "fixture")); await writeFile(join(root, "prompt.txt"), "Run fixture"); await writeFile( join(root, "npm"), diff --git a/tests/canary-v2-native-auth.test.ts b/tests/canary-v2-native-auth.test.ts index 14e36d49..05fb7942 100644 --- a/tests/canary-v2-native-auth.test.ts +++ b/tests/canary-v2-native-auth.test.ts @@ -38,11 +38,14 @@ import { canaryPublicNativeEvidence, runPaidCanary, } from "../scripts/canary-run.js"; +import { prepareCanaryWorkspace } from "../scripts/canary-workspace.js"; import { artifactIdentitySha256, CANARY_CHECKLIST_SHA256, CANARY_CHECKLIST_VERSION, CANARY_NATIVE_AUTH_DIRECTORY, + CANARY_PLUGIN_LOADER, + CANARY_PLUGIN_LOADER_SHA256, CanaryAuthLaunchReceiptSchema, CanaryNativeAuthSchema, canaryAuthLaunchMatches, @@ -94,7 +97,7 @@ async function fixture( ); roots.push(root); const plugin = join(root, "fixture/.opencode/plugins/flow"); - await mkdir(plugin, { recursive: true }); + await mkdir(join(plugin, "dist"), { recursive: true }); await mkdir(join(root, "package/dist"), { recursive: true }); const entry = options.entry ?? "export default {};\n"; const sourcePackage = canonicalJson({ @@ -107,7 +110,8 @@ async function fixture( name: "opencode-plugin-flow", version: "10.0.0", type: "module", - exports: { ".": "./index.js", "./tui": "./tui.js" }, + main: "./dist/index.js", + exports: { ".": "./dist/index.js", "./tui": "./dist/tui.js" }, dependencies: { "@opencode/plugin": "2.1.4" }, }); const config = canonicalJson({ @@ -124,8 +128,9 @@ async function fixture( ["package/package.json", sourcePackage], ["package/dist/index.js", entry], ["package/dist/tui.js", "export default {};\n"], - ["fixture/.opencode/plugins/flow/index.js", entry], - ["fixture/.opencode/plugins/flow/tui.js", "export default {};\n"], + ["fixture/.opencode/plugins/flow/index.js", CANARY_PLUGIN_LOADER], + ["fixture/.opencode/plugins/flow/dist/index.js", entry], + ["fixture/.opencode/plugins/flow/dist/tui.js", "export default {};\n"], ["fixture/.opencode/plugins/flow/package.json", pluginPackage], ["fixture/opencode.json", config], ] as const) @@ -169,6 +174,7 @@ async function fixture( sourcePackageSha256: digest(sourcePackage), tuiEntrySha256: digest("export default {};\n"), pluginPackageSha256: digest(pluginPackage), + loaderEntrySha256: CANARY_PLUGIN_LOADER_SHA256, configSha256: digest(config), ...(nativeAuth ? { nativeAuth } : {}), }, @@ -178,6 +184,7 @@ async function fixture( sha256: preparedCanarySha256(base), }); await writeFile(join(root, "prepared.json"), canonicalJson(prepared)); + await prepareCanaryWorkspace(join(root, "fixture")); await writeFile(join(root, "prompt.txt"), "Local only"); const privateDirectory = join(root, "source"); await mkdir(privateDirectory, { mode: 0o700 }); diff --git a/tests/canary-v2-native.test.ts b/tests/canary-v2-native.test.ts index eb69a06e..4673329a 100644 --- a/tests/canary-v2-native.test.ts +++ b/tests/canary-v2-native.test.ts @@ -10,7 +10,10 @@ import type { SessionMessageAssistantTool, SessionTransferData, } from "@opencode/client"; +import { prepareCanaryWorkspace } from "../scripts/canary-workspace.js"; import { + CANARY_PLUGIN_LOADER, + CANARY_PLUGIN_LOADER_SHA256, canaryRecordIssue, deriveCanaryResult, } from "../scripts/eval-canary.js"; @@ -242,6 +245,7 @@ function derive(transcript: unknown = native(), session = flowSession()) { sourcePackageSha256: hash("manifest"), tuiEntrySha256: hash("tui"), pluginPackageSha256: hash("local-manifest"), + loaderEntrySha256: CANARY_PLUGIN_LOADER_SHA256, configSha256: hash("config"), }, session, @@ -595,7 +599,8 @@ async function preparedFixture(directory: string) { name: "opencode-plugin-flow", version: "10.0.0", type: "module", - exports: { ".": "./index.js", "./tui": "./tui.js" }, + main: "./dist/index.js", + exports: { ".": "./dist/index.js", "./tui": "./dist/tui.js" }, dependencies: { "@opencode/plugin": "2.1.4" }, }); const config = canonicalJson({ @@ -609,15 +614,16 @@ async function preparedFixture(directory: string) { share: "disabled", }); await mkdir(join(directory, "package/dist"), { recursive: true }); - await mkdir(join(directory, "fixture/.opencode/plugins/flow"), { + await mkdir(join(directory, "fixture/.opencode/plugins/flow/dist"), { recursive: true, }); for (const [path, bytes] of [ ["package/package.json", sourcePackage], ["package/dist/index.js", "plugin"], ["package/dist/tui.js", "tui"], - ["fixture/.opencode/plugins/flow/index.js", "plugin"], - ["fixture/.opencode/plugins/flow/tui.js", "tui"], + ["fixture/.opencode/plugins/flow/index.js", CANARY_PLUGIN_LOADER], + ["fixture/.opencode/plugins/flow/dist/index.js", "plugin"], + ["fixture/.opencode/plugins/flow/dist/tui.js", "tui"], ["fixture/.opencode/plugins/flow/package.json", pluginPackage], ["fixture/opencode.json", config], ] as const) @@ -654,9 +660,11 @@ async function preparedFixture(directory: string) { sourcePackageSha256: hash(sourcePackage), tuiEntrySha256: hash("tui"), pluginPackageSha256: hash(pluginPackage), + loaderEntrySha256: CANARY_PLUGIN_LOADER_SHA256, configSha256: hash(config), }, }; + await prepareCanaryWorkspace(join(directory, "fixture")); const prepared = { ...base, sha256: preparedCanarySha256(base) }; await writeFile(join(directory, "prepared.json"), JSON.stringify(prepared)); return prepared; @@ -748,7 +756,7 @@ test("native canary recording redacts, reopens offline, rejects mutations and re ); expect(await verify(result.record)).toContain("digest or size"); await writeFile( - join(directory, "fixture/.opencode/plugins/flow/tui.js"), + join(directory, "fixture/.opencode/plugins/flow/dist/tui.js"), "mutated", ); await expect(recordCanary(input)).rejects.toThrow( diff --git a/tests/eval-canary.test.ts b/tests/eval-canary.test.ts index 6835aa9a..272c1462 100644 --- a/tests/eval-canary.test.ts +++ b/tests/eval-canary.test.ts @@ -11,7 +11,8 @@ import { writeFile, } from "node:fs/promises"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { join, resolve } from "node:path"; +import { pathToFileURL } from "node:url"; import { RELEASE_HOST_POLICY, releaseReviewerModel, @@ -1105,14 +1106,14 @@ describe("canary preparation", () => { } }); - test("builds an exact local-plugin fixture from the tarball", async () => { + async function exactLocalPluginFixture(contract: string) { const root = await mkdtemp(join(tmpdir(), "flow-canary-package-")); const output = await mkdtemp(join(tmpdir(), "flow-canary-output-")); temporary.push(root, output); await mkdir(join(root, "dist"), { recursive: true }); await writeFile( join(root, "dist/index.js"), - "export default async () => ({});\n", + 'import { createRequire } from "node:module"; export default async () => ({ version: createRequire(import.meta.url)("../package.json").version });\n', ); await writeFile(join(root, "dist/tui.js"), "export default {};\n"); await writeFile( @@ -1159,9 +1160,49 @@ describe("canary preparation", () => { preparedAt: new Date("2026-08-25T00:00:00.000Z"), }); expect(prepared.releaseTag).toBe("v10.0.0"); + const fixture = join(output, "prepared/fixture"); + const config = JSON.parse( + await readFile(join(fixture, "opencode.json"), "utf8"), + ); + expect(await realpath(resolve(fixture, config.plugins[0].package))).toBe( + await realpath(join(fixture, ".opencode/plugins/flow")), + ); + + if (contract === "workspace") { + await run(["git", "rev-parse", "--verify", "HEAD"], fixture); + const child = Bun.spawn(["git", "rev-parse", "--show-toplevel"], { + cwd: fixture, + stdout: "pipe", + }); + expect(await child.exited).toBe(0); + expect((await new Response(child.stdout).text()).trim()).toBe( + await realpath(fixture), + ); + } else { + const resolve = Bun.spawn( + [ + "node", + "-e", + "console.log(require.resolve(process.argv[1]))", + join(fixture, ".opencode/plugins/flow"), + ], + { stdout: "pipe", stderr: "pipe" }, + ); + expect(await resolve.exited).toBe(0); + expect( + await realpath((await new Response(resolve.stdout).text()).trim()), + ).toBe( + await realpath(join(fixture, ".opencode/plugins/flow/dist/index.js")), + ); + const entry = await import( + pathToFileURL(join(fixture, ".opencode/plugins/flow/index.js")).href + ); + expect(await entry.default()).toEqual({ version: "10.0.0" }); + } + expect( await readFile( - join(output, "prepared/fixture/.opencode/plugins/flow/index.js"), + join(output, "prepared/fixture/.opencode/plugins/flow/dist/index.js"), "utf8", ), ).toContain("export default"); @@ -1179,7 +1220,8 @@ describe("canary preparation", () => { name: "opencode-plugin-flow", version: "10.0.0", type: "module", - exports: { ".": "./index.js", "./tui": "./tui.js" }, + main: "./dist/index.js", + exports: { ".": "./dist/index.js", "./tui": "./dist/tui.js" }, dependencies: { "@opencode/plugin": "2.1.4", zod: "4.4.2", @@ -1211,7 +1253,12 @@ describe("canary preparation", () => { "package.json", "protocol.ts", ]); - }); + } + + test.each(["workspace", "module metadata"])( + "builds an exact local-plugin fixture from the tarball: %s", + exactLocalPluginFixture, + ); }); test("9.6 canary rejects every wrong-role model including a second reviewer", async () => {