|
| 1 | +import { expect } from "bun:test" |
| 2 | +import fs from "node:fs/promises" |
| 3 | +import path from "node:path" |
| 4 | +import { ConfigPluginSource } from "@opencode/core/config/plugin/source" |
| 5 | +import { Location } from "@opencode/core/location" |
| 6 | +import { AbsolutePath } from "@opencode/core/schema" |
| 7 | +import { Directory, Document, Info, type Entry } from "@opencode/schema/config" |
| 8 | +import { FSUtil } from "@opencode/util/fs-util" |
| 9 | +import { Effect, Logger } from "effect" |
| 10 | +import { tmpdirScoped } from "../fixture/tmpdir" |
| 11 | +import { testEffect } from "../lib/effect" |
| 12 | +import { PluginTestLayer } from "../plugin/fixture" |
| 13 | + |
| 14 | +const it = testEffect(PluginTestLayer) |
| 15 | + |
| 16 | +const entries = (plugins: string[]) => [new Document({ type: "document", info: new Info({ plugins }) })] |
| 17 | + |
| 18 | +const scanEntries = (fs: FSUtil.Interface, location: Location.Interface, entries: readonly Entry[]) => { |
| 19 | + const warnings: unknown[] = [] |
| 20 | + const logger = Logger.make(({ logLevel, message }) => { |
| 21 | + if (logLevel === "Warn") warnings.push(message) |
| 22 | + }) |
| 23 | + return ConfigPluginSource.scan(fs, location, entries).pipe( |
| 24 | + Effect.provide(Logger.layer([logger])), |
| 25 | + Effect.map((operations) => ({ operations, warnings })), |
| 26 | + ) |
| 27 | +} |
| 28 | + |
| 29 | +const scan = (fs: FSUtil.Interface, location: Location.Interface, plugins: string[]) => |
| 30 | + scanEntries(fs, location, entries(plugins)) |
| 31 | + |
| 32 | +it.live("warns when a plugin directory has no server, GUI, or TUI entrypoint", () => |
| 33 | + Effect.gen(function* () { |
| 34 | + const root = yield* tmpdirScoped() |
| 35 | + const directory = path.join(root.path, "empty-plugin") |
| 36 | + yield* Effect.promise(() => fs.mkdir(directory, { recursive: true })) |
| 37 | + const fsService = yield* FSUtil.Service |
| 38 | + const location = yield* Location.Service |
| 39 | + |
| 40 | + const result = yield* scan(fsService, location, [directory]) |
| 41 | + |
| 42 | + expect(result.operations).toEqual([]) |
| 43 | + expect(result.warnings).toContainEqual([ |
| 44 | + "plugin directory has no server, GUI, or TUI entrypoint", |
| 45 | + { target: directory }, |
| 46 | + ]) |
| 47 | + }), |
| 48 | +) |
| 49 | + |
| 50 | +// Windows requires Developer Mode or elevation for file symlinks; the containment path is covered on POSIX. |
| 51 | +const symlinkIt = process.platform === "win32" ? it.live.skip : it.live |
| 52 | + |
| 53 | +symlinkIt("warns when a plugin entrypoint resolves outside its directory", () => |
| 54 | + Effect.gen(function* () { |
| 55 | + const root = yield* tmpdirScoped() |
| 56 | + const directory = path.join(root.path, "plugin") |
| 57 | + const outside = path.join(root.path, "outside") |
| 58 | + yield* Effect.promise(async () => { |
| 59 | + await fs.mkdir(directory, { recursive: true }) |
| 60 | + await fs.mkdir(outside, { recursive: true }) |
| 61 | + await fs.writeFile(path.join(outside, "index.js"), "export default { id: 'probe', setup() {} }") |
| 62 | + await fs.symlink(path.join(outside, "index.js"), path.join(directory, "index.js")) |
| 63 | + }) |
| 64 | + const fsService = yield* FSUtil.Service |
| 65 | + const location = yield* Location.Service |
| 66 | + const resolvedRoot = yield* fsService.resolve(directory) |
| 67 | + const resolvedEntrypoint = yield* fsService.resolve(path.join(outside, "index.js")) |
| 68 | + |
| 69 | + const result = yield* scan(fsService, location, [directory]) |
| 70 | + |
| 71 | + expect(result.operations).toEqual([]) |
| 72 | + expect(result.warnings).toContainEqual([ |
| 73 | + "plugin entrypoint resolves outside plugin directory", |
| 74 | + { target: directory, root: resolvedRoot, entrypoint: resolvedEntrypoint }, |
| 75 | + ]) |
| 76 | + }), |
| 77 | +) |
| 78 | + |
| 79 | +it.live("keeps a plugin directory whose server entrypoint is contained", () => |
| 80 | + Effect.gen(function* () { |
| 81 | + const root = yield* tmpdirScoped() |
| 82 | + const directory = path.join(root.path, "plugin") |
| 83 | + yield* Effect.promise(async () => { |
| 84 | + await fs.mkdir(directory, { recursive: true }) |
| 85 | + await fs.writeFile(path.join(directory, "index.js"), "export default { id: 'probe', setup() {} }") |
| 86 | + }) |
| 87 | + const fsService = yield* FSUtil.Service |
| 88 | + const location = yield* Location.Service |
| 89 | + |
| 90 | + const result = yield* scan(fsService, location, [directory]) |
| 91 | + |
| 92 | + expect(result.operations.map((operation) => operation.target)).toEqual([directory]) |
| 93 | + expect(result.warnings).toEqual([]) |
| 94 | + }), |
| 95 | +) |
| 96 | + |
| 97 | +it.live("keeps a GUI-only plugin directory without warning", () => |
| 98 | + Effect.gen(function* () { |
| 99 | + const root = yield* tmpdirScoped() |
| 100 | + const directory = path.join(root.path, "gui-plugin") |
| 101 | + yield* Effect.promise(async () => { |
| 102 | + await fs.mkdir(directory, { recursive: true }) |
| 103 | + await fs.writeFile(path.join(directory, "gui-experimental.cjs"), "module.exports = {}") |
| 104 | + }) |
| 105 | + const fsService = yield* FSUtil.Service |
| 106 | + const location = yield* Location.Service |
| 107 | + |
| 108 | + const result = yield* scan(fsService, location, [directory]) |
| 109 | + |
| 110 | + expect(result.operations.map((operation) => operation.target)).toEqual([directory]) |
| 111 | + expect(result.warnings).toEqual([]) |
| 112 | + }), |
| 113 | +) |
| 114 | + |
| 115 | +it.live("silently skips a TUI-only plugin directory", () => |
| 116 | + Effect.gen(function* () { |
| 117 | + const root = yield* tmpdirScoped() |
| 118 | + const directory = path.join(root.path, "tui-plugin") |
| 119 | + yield* Effect.promise(async () => { |
| 120 | + await fs.mkdir(directory, { recursive: true }) |
| 121 | + await fs.writeFile(path.join(directory, "tui.ts"), "export default {}") |
| 122 | + }) |
| 123 | + const fsService = yield* FSUtil.Service |
| 124 | + const location = yield* Location.Service |
| 125 | + |
| 126 | + const result = yield* scan(fsService, location, [directory]) |
| 127 | + |
| 128 | + expect(result.operations).toEqual([]) |
| 129 | + expect(result.warnings).toEqual([]) |
| 130 | + }), |
| 131 | +) |
| 132 | + |
| 133 | +it.live("does not warn for auto-discovered helper directories", () => |
| 134 | + Effect.gen(function* () { |
| 135 | + const root = yield* tmpdirScoped() |
| 136 | + const helper = path.join(root.path, "plugin", "lib") |
| 137 | + yield* Effect.promise(() => fs.mkdir(helper, { recursive: true })) |
| 138 | + const fsService = yield* FSUtil.Service |
| 139 | + const location = yield* Location.Service |
| 140 | + |
| 141 | + const result = yield* scanEntries(fsService, location, [ |
| 142 | + new Directory({ type: "directory", path: AbsolutePath.make(root.path) }), |
| 143 | + ]) |
| 144 | + |
| 145 | + expect(result.operations).toEqual([]) |
| 146 | + expect(result.warnings).toEqual([]) |
| 147 | + }), |
| 148 | +) |
0 commit comments