|
| 1 | +import { expect, test } from "bun:test" |
| 2 | +import { mkdir, writeFile } from "node:fs/promises" |
| 3 | +import path from "node:path" |
| 4 | +import { Host } from "@opencode/plugin/host" |
| 5 | +import { configurationTarget } from "../src/commands/handlers/plugin/add" |
| 6 | +import { tmpdir } from "./fixture/tmpdir" |
| 7 | + |
| 8 | +const name = "@fixture/plugin" |
| 9 | +const parts = { |
| 10 | + server: { ".": "./index.js" }, |
| 11 | + tui: { "./tui": "./tui.js" }, |
| 12 | + gui: { "./gui-experimental": "./gui-experimental.cjs" }, |
| 13 | +} |
| 14 | + |
| 15 | +test.each([ |
| 16 | + { parts: ["gui"] as const, target: "server" }, |
| 17 | + { parts: ["tui", "gui"] as const, target: "server" }, |
| 18 | + { parts: ["server"] as const, target: "server" }, |
| 19 | + { parts: ["server", "tui", "gui"] as const, target: "server" }, |
| 20 | + { parts: ["tui"] as const, target: "tui" }, |
| 21 | + { parts: [] as const, target: undefined }, |
| 22 | +])("configures a package with $parts parts in the $target config", async (input) => { |
| 23 | + await using root = await tmpdir() |
| 24 | + const directory = path.join(root.path, "node_modules", name) |
| 25 | + const exports = Object.assign({}, ...input.parts.map((part) => parts[part])) |
| 26 | + await mkdir(directory, { recursive: true }) |
| 27 | + await Promise.all([ |
| 28 | + writeFile(path.join(directory, "package.json"), JSON.stringify({ name, exports })), |
| 29 | + ...Object.values(exports).map((file) => writeFile(path.join(directory, String(file)), "")), |
| 30 | + ]) |
| 31 | + |
| 32 | + expect(configurationTarget(Host.resolve({ directory, name }))).toBe(input.target) |
| 33 | +}) |
0 commit comments