|
| 1 | +/** |
| 2 | + * profile の plugin が **opencode の loader を通るか** を検査する。 |
| 3 | + * |
| 4 | + * ## 起点(2026-09-04、実 `opencode run` で反証) |
| 5 | + * |
| 6 | + * gate-fires.test.ts が 6 pass の状態で、実 CLI は |
| 7 | + * failed to load plugin .../guardrail.ts (The "paths[0]" property must be of type string, got object) |
| 8 | + * failed to load plugin .../team.ts (Plugin export is not a function) |
| 9 | + * を出し、`chat.params` の block は events.jsonl に**全期間で 0 件**だった。 |
| 10 | + * gate() の単体テストが緑でも、**gate() に到達する経路が死んでいれば強制は無い。** |
| 11 | + * |
| 12 | + * ## なぜ loader を直接 import せず判定を複製するか |
| 13 | + * |
| 14 | + * loader 本体(packages/opencode/src/plugin/shared.ts の readV1Plugin と |
| 15 | + * packages/opencode/src/plugin/index.ts の getLegacyPlugins)は `@/util/*` と |
| 16 | + * `@opencode-ai/core/npm` に依存し、guardrails の bun test からは解決できない。 |
| 17 | + * そこで判定だけを忠実に複製し、**複製がソースからずれたら落ちる pair 検査**を付ける |
| 18 | + * (ソースの決定的な 2 条件を文字列で照合する)。複製だけだと「テストは緑だが loader は |
| 19 | + * 変わっていた」を通してしまう。 |
| 20 | + * |
| 21 | + * ## 軸 |
| 22 | + * F1 v1 判定 default が object で server() を持つ → loader は named export に触れない |
| 23 | + * F2 legacy 経路 default が関数の module(修正前の形)は legacy に落ち、named export を |
| 24 | + * server(input) で呼んで throw する — これが修正前の red の再現 |
| 25 | + * F3 到達性 server(stubInput) が返す hooks に "chat.params" がある |
| 26 | + * PAIR 複製した判定条件がソースに実在する |
| 27 | + * |
| 28 | + * Run: bun test packages/guardrails/profile/plugins/plugin-loads.test.ts |
| 29 | + */ |
| 30 | +import { describe, expect, test } from "bun:test" |
| 31 | +import { existsSync, readFileSync } from "node:fs" |
| 32 | +import { join } from "node:path" |
| 33 | +import * as guardrailMod from "./guardrail" |
| 34 | +import * as teamMod from "./team" |
| 35 | + |
| 36 | +// ---- loader の判定を複製(packages/opencode/src/plugin/{shared,index}.ts) ---- |
| 37 | +type Mod = Record<string, unknown> |
| 38 | +type ServerFn = (input: unknown, opts?: unknown) => Promise<unknown> |
| 39 | +type V1 = { id?: unknown; server: ServerFn } |
| 40 | + |
| 41 | +const isRecord = (v: unknown): v is Record<string, unknown> => !!v && typeof v === "object" |
| 42 | +const isServerFn = (v: unknown): v is ServerFn => typeof v === "function" |
| 43 | +const isV1 = (v: unknown): v is V1 => isRecord(v) && isServerFn(v.server) |
| 44 | + |
| 45 | +/** shared.ts readV1Plugin(mod, spec, "server", "detect") の判定部 */ |
| 46 | +function readV1PluginDetect(mod: Mod): V1 | undefined { |
| 47 | + const value = mod.default |
| 48 | + if (!isRecord(value)) return undefined |
| 49 | + if (!("id" in value) && !("server" in value) && !("tui" in value)) return undefined |
| 50 | + if ("server" in value && value.server !== undefined && !isServerFn(value.server)) { |
| 51 | + throw new TypeError("invalid server export") |
| 52 | + } |
| 53 | + if (!isV1(value)) throw new TypeError("must default export an object with server()") |
| 54 | + return value |
| 55 | +} |
| 56 | + |
| 57 | +/** index.ts getServerPlugin / getLegacyPlugins の判定部 */ |
| 58 | +function getLegacyPlugins(mod: Mod): ServerFn[] { |
| 59 | + const seen = new Set<unknown>() |
| 60 | + const out: ServerFn[] = [] |
| 61 | + for (const entry of Object.values(mod)) { |
| 62 | + if (seen.has(entry)) continue |
| 63 | + seen.add(entry) |
| 64 | + if (isServerFn(entry)) { |
| 65 | + out.push(entry) |
| 66 | + continue |
| 67 | + } |
| 68 | + if (isRecord(entry) && isServerFn(entry.server)) { |
| 69 | + out.push(entry.server) |
| 70 | + continue |
| 71 | + } |
| 72 | + throw new TypeError("Plugin export is not a function") |
| 73 | + } |
| 74 | + return out |
| 75 | +} |
| 76 | + |
| 77 | +/** index.ts applyPlugin: v1 があればそれだけ、無ければ legacy で全 export を呼ぶ */ |
| 78 | +async function applyPluginLike(mod: Mod, input: unknown): Promise<unknown[]> { |
| 79 | + const v1 = readV1PluginDetect(mod) |
| 80 | + // Promise.resolve で包む: 型ガード越しの戻り値を oxlint が thenable と確定できないため。 |
| 81 | + // 実体は async 関数なので挙動は同じ。 |
| 82 | + if (v1) return [await Promise.resolve(v1.server(input, {}))] |
| 83 | + const hooks: unknown[] = [] |
| 84 | + for (const server of getLegacyPlugins(mod)) hooks.push(await Promise.resolve(server(input, {}))) |
| 85 | + return hooks |
| 86 | +} |
| 87 | + |
| 88 | +// ---- loader が実際に渡す入力に近い stub(client は触ったら落ちる Proxy) ---- |
| 89 | +const dir = process.cwd() |
| 90 | +const stubInput = { |
| 91 | + client: new Proxy( |
| 92 | + {}, |
| 93 | + { |
| 94 | + get(_t, p) { |
| 95 | + throw new Error(`plugin touched client.${String(p)} at load`) |
| 96 | + }, |
| 97 | + }, |
| 98 | + ), |
| 99 | + project: { id: "test" }, |
| 100 | + directory: dir, |
| 101 | + worktree: dir, |
| 102 | + serverUrl: new URL("http://localhost:0"), |
| 103 | + experimental_workspace: { register() {} }, |
| 104 | + $: undefined, |
| 105 | +} |
| 106 | + |
| 107 | +describe("profile plugin は opencode の loader を通る", () => { |
| 108 | + test("F1 guardrail: default は object で server() を持つ(v1 として認識される)", () => { |
| 109 | + const v1 = readV1PluginDetect(guardrailMod) |
| 110 | + expect(v1).toBeDefined() |
| 111 | + expect(v1?.id).toBe("aidd-guardrail") |
| 112 | + }) |
| 113 | + |
| 114 | + test("F1 team: default は object で server() を持つ", () => { |
| 115 | + const v1 = readV1PluginDetect(teamMod) |
| 116 | + expect(v1).toBeDefined() |
| 117 | + expect(v1?.id).toBe("aidd-team") |
| 118 | + }) |
| 119 | + |
| 120 | + test("F2 legacy 経路の再現: default が関数だと named export が server(input) で呼ばれて落ちる", async () => { |
| 121 | + // 修正前の guardrail.ts と同じ形を合成する: default は関数、named export に |
| 122 | + // 「string を要求する補助関数」がある。loader はこれを plugin と誤認して input を渡す。 |
| 123 | + const legacyShaped: Mod = { |
| 124 | + default: async () => ({}), |
| 125 | + ensureLocalOpencodeIgnored: async (worktree: string) => join(worktree, ".gitignore"), |
| 126 | + } |
| 127 | + expect(readV1PluginDetect(legacyShaped)).toBeUndefined() // v1 とは認識されない |
| 128 | + // bun test は返した Promise を待つ。await にすると oxlint が expect(...).rejects を |
| 129 | + // thenable と確定できず await-thenable を出すので return にする。 |
| 130 | + return expect(applyPluginLike(legacyShaped, stubInput)).rejects.toThrow(/paths\[0\]|must be of type string/) |
| 131 | + }) |
| 132 | + |
| 133 | + test("F2 legacy 経路の再現: 非関数の named export は 'Plugin export is not a function'", () => { |
| 134 | + const legacyShaped: Mod = { default: async () => ({}), DEFAULT_TIMEOUT_MS: 1000 } |
| 135 | + expect(() => getLegacyPlugins(legacyShaped)).toThrow("Plugin export is not a function") |
| 136 | + }) |
| 137 | + |
| 138 | + test("F3 到達性: guardrail.server(input) が返す hooks に chat.params がある", async () => { |
| 139 | + const [hooks] = await applyPluginLike(guardrailMod, stubInput) |
| 140 | + expect(isRecord(hooks)).toBe(true) |
| 141 | + if (!isRecord(hooks)) return |
| 142 | + expect(typeof hooks["chat.params"]).toBe("function") |
| 143 | + expect(typeof hooks["tool.execute.before"]).toBe("function") |
| 144 | + }) |
| 145 | + |
| 146 | + test("F3 統制: 現行の guardrail は named export を持つが、v1 なので loader はそれを呼ばない", async () => { |
| 147 | + // named export が残っていること自体は正しい(他の用途がある)。 |
| 148 | + // v1 判定が効いていれば applyPluginLike は default.server だけを呼び、例外は出ない。 |
| 149 | + const named = Object.keys(guardrailMod).filter((k) => k !== "default") |
| 150 | + expect(named.length).toBeGreaterThan(0) |
| 151 | + return expect(applyPluginLike(guardrailMod, stubInput)).resolves.toBeDefined() |
| 152 | + }) |
| 153 | +}) |
| 154 | + |
| 155 | +describe("PAIR: 複製した loader 判定がソースに実在する(ずれたら落ちる)", () => { |
| 156 | + const root = join(import.meta.dir, "..", "..", "..", "opencode", "src", "plugin") |
| 157 | + const shared = join(root, "shared.ts") |
| 158 | + const index = join(root, "index.ts") |
| 159 | + |
| 160 | + test("loader のソースが解決できる(無ければ pair は成立しない)", () => { |
| 161 | + expect(existsSync(shared)).toBe(true) |
| 162 | + expect(existsSync(index)).toBe(true) |
| 163 | + }) |
| 164 | + |
| 165 | + test("readV1Plugin: detect モードは id/server/tui の無い default を無視する", () => { |
| 166 | + const src = readFileSync(shared, "utf-8") |
| 167 | + expect(src).toContain(`if (mode === "detect" && !("id" in value) && !("server" in value) && !("tui" in value)) return`) |
| 168 | + }) |
| 169 | + |
| 170 | + test("getLegacyPlugins: 全 export を走査し、非関数なら throw する", () => { |
| 171 | + const src = readFileSync(index, "utf-8") |
| 172 | + expect(src).toContain("for (const entry of Object.values(mod))") |
| 173 | + expect(src).toContain(`throw new TypeError("Plugin export is not a function")`) |
| 174 | + }) |
| 175 | +}) |
0 commit comments