|
| 1 | +import { afterEach, expect, test } from "bun:test" |
| 2 | +import { existsSync } from "node:fs" |
| 3 | +import path from "node:path" |
| 4 | +import { pathToFileURL } from "node:url" |
| 5 | +import { bootstrap as cliBootstrap } from "../../src/cli/bootstrap" |
| 6 | +import { WithInstance } from "../../src/project/with-instance" |
| 7 | +import { InstanceRuntime } from "../../src/project/instance-runtime" |
| 8 | +import { disposeAllInstances, tmpdir } from "../fixture/fixture" |
| 9 | + |
| 10 | +// InstanceBootstrap must run before any code touches the instance — |
| 11 | +// originally tracked by PRs #25389 and #25449, now a permanent |
| 12 | +// invariant. The plugin config hook writes a marker file; the test |
| 13 | +// bodies deliberately avoid Plugin/config directly. The marker only |
| 14 | +// appears if InstanceBootstrap ran at the instance boundary. |
| 15 | +// |
| 16 | +// The Hono variant of this check lived alongside these tests and is |
| 17 | +// going away with the Hono backend. The boundaries below are backend- |
| 18 | +// agnostic and stay. |
| 19 | + |
| 20 | +afterEach(async () => { |
| 21 | + await disposeAllInstances() |
| 22 | +}) |
| 23 | + |
| 24 | +async function bootstrapFixture() { |
| 25 | + return tmpdir({ |
| 26 | + init: async (dir) => { |
| 27 | + const marker = path.join(dir, "config-hook-fired") |
| 28 | + const pluginFile = path.join(dir, "plugin.ts") |
| 29 | + await Bun.write( |
| 30 | + pluginFile, |
| 31 | + [ |
| 32 | + `const MARKER = ${JSON.stringify(marker)}`, |
| 33 | + "export default async () => ({", |
| 34 | + " config: async () => {", |
| 35 | + ' await Bun.write(MARKER, "ran")', |
| 36 | + " },", |
| 37 | + "})", |
| 38 | + "", |
| 39 | + ].join("\n"), |
| 40 | + ) |
| 41 | + await Bun.write( |
| 42 | + path.join(dir, "opencode.json"), |
| 43 | + JSON.stringify({ |
| 44 | + $schema: "https://opencode.ai/config.json", |
| 45 | + plugin: [pathToFileURL(pluginFile).href], |
| 46 | + }), |
| 47 | + ) |
| 48 | + return marker |
| 49 | + }, |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +test("WithInstance.provide runs InstanceBootstrap before fn", async () => { |
| 54 | + await using tmp = await bootstrapFixture() |
| 55 | + |
| 56 | + await WithInstance.provide({ |
| 57 | + directory: tmp.path, |
| 58 | + fn: async () => "ok", |
| 59 | + }) |
| 60 | + |
| 61 | + expect(existsSync(tmp.extra)).toBe(true) |
| 62 | +}) |
| 63 | + |
| 64 | +test("CLI bootstrap runs InstanceBootstrap before callback", async () => { |
| 65 | + await using tmp = await bootstrapFixture() |
| 66 | + |
| 67 | + await cliBootstrap(tmp.path, async () => "ok") |
| 68 | + |
| 69 | + expect(existsSync(tmp.extra)).toBe(true) |
| 70 | +}) |
| 71 | + |
| 72 | +test("InstanceRuntime.reloadInstance runs InstanceBootstrap", async () => { |
| 73 | + await using tmp = await bootstrapFixture() |
| 74 | + |
| 75 | + await InstanceRuntime.reloadInstance({ directory: tmp.path }) |
| 76 | + |
| 77 | + expect(existsSync(tmp.extra)).toBe(true) |
| 78 | +}) |
0 commit comments