install.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. import { describe, expect, test } from "bun:test"
  2. import fs from "fs/promises"
  3. import path from "path"
  4. import { parse as parseJsonc } from "jsonc-parser"
  5. import { Filesystem } from "../../src/util/filesystem"
  6. import { createPlugTask, type PlugCtx, type PlugDeps } from "../../src/cli/cmd/plug"
  7. import { tmpdir } from "../fixture/fixture"
  8. function deps(global: string, target: string | Error): PlugDeps {
  9. return {
  10. spinner: () => ({
  11. start() {},
  12. stop() {},
  13. }),
  14. log: {
  15. error() {},
  16. info() {},
  17. success() {},
  18. },
  19. resolve: async () => {
  20. if (target instanceof Error) throw target
  21. return target
  22. },
  23. readText: (file) => Filesystem.readText(file),
  24. write: async (file, text) => {
  25. await Filesystem.write(file, text)
  26. },
  27. exists: (file) => Filesystem.exists(file),
  28. files: (dir, name) => [path.join(dir, `${name}.jsonc`), path.join(dir, `${name}.json`)],
  29. global,
  30. }
  31. }
  32. function ctx(dir: string): PlugCtx {
  33. return {
  34. vcs: "git",
  35. worktree: dir,
  36. directory: dir,
  37. }
  38. }
  39. function ctxDir(dir: string, worktree: string): PlugCtx {
  40. return {
  41. vcs: "none",
  42. worktree,
  43. directory: dir,
  44. }
  45. }
  46. function ctxRoot(dir: string): PlugCtx {
  47. return {
  48. vcs: "git",
  49. worktree: "/",
  50. directory: dir,
  51. }
  52. }
  53. async function plugin(
  54. dir: string,
  55. kinds?: Array<"server" | "tui">,
  56. opts?: {
  57. server?: Record<string, unknown>
  58. tui?: Record<string, unknown>
  59. },
  60. themes?: string[],
  61. ) {
  62. const p = path.join(dir, "plugin")
  63. const server = kinds?.includes("server") ?? false
  64. const tui = kinds?.includes("tui") ?? false
  65. const exports: Record<string, unknown> = {}
  66. if (server) {
  67. exports["./server"] = opts?.server
  68. ? {
  69. import: "./server.js",
  70. config: opts.server,
  71. }
  72. : "./server.js"
  73. }
  74. if (tui) {
  75. exports["./tui"] = opts?.tui
  76. ? {
  77. import: "./tui.js",
  78. config: opts.tui,
  79. }
  80. : "./tui.js"
  81. }
  82. await fs.mkdir(p, { recursive: true })
  83. await Bun.write(
  84. path.join(p, "package.json"),
  85. JSON.stringify(
  86. {
  87. name: "acme",
  88. version: "1.0.0",
  89. ...(server ? { main: "./server.js" } : {}),
  90. ...(Object.keys(exports).length ? { exports } : {}),
  91. ...(themes?.length ? { "oc-themes": themes } : {}),
  92. },
  93. null,
  94. 2,
  95. ),
  96. )
  97. return p
  98. }
  99. async function read(file: string) {
  100. return Filesystem.readJson<{
  101. plugin?: unknown[]
  102. }>(file)
  103. }
  104. describe("plugin.install.task", () => {
  105. test("writes both server and tui config entries", async () => {
  106. await using tmp = await tmpdir()
  107. const target = await plugin(tmp.path, ["server", "tui"])
  108. const run = createPlugTask(
  109. {
  110. mod: "[email protected]",
  111. },
  112. deps(path.join(tmp.path, "global"), target),
  113. )
  114. const ok = await run(ctx(tmp.path))
  115. expect(ok).toBe(true)
  116. const server = await read(path.join(tmp.path, ".opencode", "opencode.jsonc"))
  117. const tui = await read(path.join(tmp.path, ".opencode", "tui.jsonc"))
  118. expect(server.plugin).toEqual(["[email protected]"])
  119. expect(tui.plugin).toEqual(["[email protected]"])
  120. })
  121. test("writes default options from exports config metadata", async () => {
  122. await using tmp = await tmpdir()
  123. const target = await plugin(tmp.path, ["server", "tui"], {
  124. server: { custom: true, other: false },
  125. tui: { compact: true },
  126. })
  127. const run = createPlugTask(
  128. {
  129. mod: "[email protected]",
  130. },
  131. deps(path.join(tmp.path, "global"), target),
  132. )
  133. const ok = await run(ctx(tmp.path))
  134. expect(ok).toBe(true)
  135. const server = await read(path.join(tmp.path, ".opencode", "opencode.jsonc"))
  136. const tui = await read(path.join(tmp.path, ".opencode", "tui.jsonc"))
  137. expect(server.plugin).toEqual([["[email protected]", { custom: true, other: false }]])
  138. expect(tui.plugin).toEqual([["[email protected]", { compact: true }]])
  139. })
  140. test("preserves JSONC comments when adding plugins to server and tui config", async () => {
  141. await using tmp = await tmpdir()
  142. const target = await plugin(tmp.path, ["server", "tui"])
  143. const cfg = path.join(tmp.path, ".opencode")
  144. const server = path.join(cfg, "opencode.jsonc")
  145. const tui = path.join(cfg, "tui.jsonc")
  146. await fs.mkdir(cfg, { recursive: true })
  147. await Bun.write(
  148. server,
  149. `{
  150. // server head
  151. "plugin": [
  152. // server keep
  153. "[email protected]"
  154. ],
  155. // server tail
  156. "model": "x"
  157. }
  158. `,
  159. )
  160. await Bun.write(
  161. tui,
  162. `{
  163. // tui head
  164. "plugin": [
  165. // tui keep
  166. "[email protected]"
  167. ],
  168. // tui tail
  169. "theme": "opencode"
  170. }
  171. `,
  172. )
  173. const run = createPlugTask(
  174. {
  175. mod: "[email protected]",
  176. },
  177. deps(path.join(tmp.path, "global"), target),
  178. )
  179. const ok = await run(ctx(tmp.path))
  180. expect(ok).toBe(true)
  181. const serverText = await fs.readFile(server, "utf8")
  182. const tuiText = await fs.readFile(tui, "utf8")
  183. expect(serverText).toContain("// server head")
  184. expect(serverText).toContain("// server keep")
  185. expect(serverText).toContain("// server tail")
  186. expect(tuiText).toContain("// tui head")
  187. expect(tuiText).toContain("// tui keep")
  188. expect(tuiText).toContain("// tui tail")
  189. const serverJson = parseJsonc(serverText) as { plugin?: unknown[] }
  190. const tuiJson = parseJsonc(tuiText) as { plugin?: unknown[] }
  191. expect(serverJson.plugin).toEqual(["[email protected]", "[email protected]"])
  192. expect(tuiJson.plugin).toEqual(["[email protected]", "[email protected]"])
  193. })
  194. test("preserves JSONC comments when force replacing plugin version", async () => {
  195. await using tmp = await tmpdir()
  196. const target = await plugin(tmp.path, ["server"])
  197. const cfg = path.join(tmp.path, ".opencode", "opencode.jsonc")
  198. await fs.mkdir(path.dirname(cfg), { recursive: true })
  199. await Bun.write(
  200. cfg,
  201. `{
  202. "plugin": [
  203. // keep this note
  204. "[email protected]"
  205. ]
  206. }
  207. `,
  208. )
  209. const run = createPlugTask(
  210. {
  211. mod: "[email protected]",
  212. force: true,
  213. },
  214. deps(path.join(tmp.path, "global"), target),
  215. )
  216. const ok = await run(ctx(tmp.path))
  217. expect(ok).toBe(true)
  218. const text = await fs.readFile(cfg, "utf8")
  219. expect(text).toContain("// keep this note")
  220. const json = parseJsonc(text) as { plugin?: unknown[] }
  221. expect(json.plugin).toEqual(["[email protected]"])
  222. })
  223. test("supports resolver target pointing to a file", async () => {
  224. await using tmp = await tmpdir()
  225. const target = await plugin(tmp.path, ["server"])
  226. const file = path.join(target, "index.js")
  227. await Bun.write(file, "export {}")
  228. const run = createPlugTask(
  229. {
  230. mod: "[email protected]",
  231. },
  232. deps(path.join(tmp.path, "global"), file),
  233. )
  234. const ok = await run(ctx(tmp.path))
  235. expect(ok).toBe(true)
  236. const server = await read(path.join(tmp.path, ".opencode", "opencode.jsonc"))
  237. expect(server.plugin).toEqual(["[email protected]"])
  238. })
  239. test("does not change configured package version without force", async () => {
  240. await using tmp = await tmpdir()
  241. const target = await plugin(tmp.path, ["server"])
  242. const cfg = path.join(tmp.path, ".opencode", "opencode.json")
  243. await fs.mkdir(path.dirname(cfg), { recursive: true })
  244. await Bun.write(cfg, JSON.stringify({ plugin: ["[email protected]"] }, null, 2))
  245. const run = createPlugTask(
  246. {
  247. mod: "[email protected]",
  248. },
  249. deps(path.join(tmp.path, "global"), target),
  250. )
  251. const ok = await run(ctx(tmp.path))
  252. expect(ok).toBe(true)
  253. const json = await read(cfg)
  254. expect(json.plugin).toEqual(["[email protected]"])
  255. })
  256. test("does not change scoped package version without force", async () => {
  257. await using tmp = await tmpdir()
  258. const target = await plugin(tmp.path, ["server"])
  259. const cfg = path.join(tmp.path, ".opencode", "opencode.json")
  260. await fs.mkdir(path.dirname(cfg), { recursive: true })
  261. await Bun.write(cfg, JSON.stringify({ plugin: ["@scope/[email protected]"] }, null, 2))
  262. const run = createPlugTask(
  263. {
  264. mod: "@scope/[email protected]",
  265. },
  266. deps(path.join(tmp.path, "global"), target),
  267. )
  268. const ok = await run(ctx(tmp.path))
  269. expect(ok).toBe(true)
  270. const json = await read(cfg)
  271. expect(json.plugin).toEqual(["@scope/[email protected]"])
  272. })
  273. test("keeps file plugin entries and still adds npm plugin", async () => {
  274. await using tmp = await tmpdir()
  275. const target = await plugin(tmp.path, ["server"])
  276. const cfg = path.join(tmp.path, ".opencode", "opencode.json")
  277. await fs.mkdir(path.dirname(cfg), { recursive: true })
  278. await Bun.write(cfg, JSON.stringify({ plugin: ["file:///tmp/acme.ts"] }, null, 2))
  279. const run = createPlugTask(
  280. {
  281. mod: "[email protected]",
  282. },
  283. deps(path.join(tmp.path, "global"), target),
  284. )
  285. const ok = await run(ctx(tmp.path))
  286. expect(ok).toBe(true)
  287. const json = await read(cfg)
  288. expect(json.plugin).toEqual(["file:///tmp/acme.ts", "[email protected]"])
  289. })
  290. test("force replaces configured package version and keeps tuple options", async () => {
  291. await using tmp = await tmpdir()
  292. const target = await plugin(tmp.path, ["server"])
  293. const cfg = path.join(tmp.path, ".opencode", "opencode.json")
  294. await fs.mkdir(path.dirname(cfg), { recursive: true })
  295. await Bun.write(
  296. cfg,
  297. JSON.stringify(
  298. {
  299. plugin: [["[email protected]", { mode: "safe" }], "[email protected]", "[email protected]"],
  300. },
  301. null,
  302. 2,
  303. ),
  304. )
  305. const run = createPlugTask(
  306. {
  307. mod: "[email protected]",
  308. force: true,
  309. },
  310. deps(path.join(tmp.path, "global"), target),
  311. )
  312. const ok = await run(ctx(tmp.path))
  313. expect(ok).toBe(true)
  314. const json = await read(cfg)
  315. expect(json.plugin).toEqual([["[email protected]", { mode: "safe" }], "[email protected]"])
  316. })
  317. test("writes to global scope when global flag is set", async () => {
  318. await using tmp = await tmpdir()
  319. const target = await plugin(tmp.path, ["server"])
  320. const global = path.join(tmp.path, "global")
  321. const run = createPlugTask(
  322. {
  323. mod: "[email protected]",
  324. global: true,
  325. },
  326. deps(global, target),
  327. )
  328. const ok = await run(ctx(tmp.path))
  329. expect(ok).toBe(true)
  330. expect(await Filesystem.exists(path.join(global, "opencode.jsonc"))).toBe(true)
  331. expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false)
  332. })
  333. test("writes local scope under directory when vcs is not git", async () => {
  334. await using tmp = await tmpdir()
  335. const target = await plugin(tmp.path, ["server"])
  336. const directory = path.join(tmp.path, "dir")
  337. const worktree = path.join(tmp.path, "worktree")
  338. await fs.mkdir(directory, { recursive: true })
  339. await fs.mkdir(worktree, { recursive: true })
  340. const run = createPlugTask(
  341. {
  342. mod: "[email protected]",
  343. },
  344. deps(path.join(tmp.path, "global"), target),
  345. )
  346. const ok = await run(ctxDir(directory, worktree))
  347. expect(ok).toBe(true)
  348. expect(await Filesystem.exists(path.join(directory, ".opencode", "opencode.jsonc"))).toBe(true)
  349. expect(await Filesystem.exists(path.join(worktree, ".opencode", "opencode.jsonc"))).toBe(false)
  350. })
  351. test("writes local scope under directory when worktree is root slash", async () => {
  352. await using tmp = await tmpdir()
  353. const target = await plugin(tmp.path, ["server"])
  354. const directory = path.join(tmp.path, "dir")
  355. await fs.mkdir(directory, { recursive: true })
  356. const run = createPlugTask(
  357. {
  358. mod: "[email protected]",
  359. },
  360. deps(path.join(tmp.path, "global"), target),
  361. )
  362. const ok = await run(ctxRoot(directory))
  363. expect(ok).toBe(true)
  364. expect(await Filesystem.exists(path.join(directory, ".opencode", "opencode.jsonc"))).toBe(true)
  365. })
  366. test("writes tui local scope under directory when worktree is root slash", async () => {
  367. await using tmp = await tmpdir()
  368. const target = await plugin(tmp.path, ["tui"])
  369. const directory = path.join(tmp.path, "dir")
  370. await fs.mkdir(directory, { recursive: true })
  371. const run = createPlugTask(
  372. {
  373. mod: "[email protected]",
  374. },
  375. deps(path.join(tmp.path, "global"), target),
  376. )
  377. const ok = await run(ctxRoot(directory))
  378. expect(ok).toBe(true)
  379. expect(await Filesystem.exists(path.join(directory, ".opencode", "tui.jsonc"))).toBe(true)
  380. })
  381. test("writes only tui config for tui-only plugins", async () => {
  382. await using tmp = await tmpdir()
  383. const target = await plugin(tmp.path, ["tui"])
  384. const run = createPlugTask(
  385. {
  386. mod: "[email protected]",
  387. },
  388. deps(path.join(tmp.path, "global"), target),
  389. )
  390. const ok = await run(ctx(tmp.path))
  391. expect(ok).toBe(true)
  392. expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "tui.jsonc"))).toBe(true)
  393. expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false)
  394. })
  395. test("writes tui config for oc-themes-only packages", async () => {
  396. await using tmp = await tmpdir()
  397. const target = await plugin(tmp.path, undefined, undefined, ["themes/forest.json"])
  398. await fs.mkdir(path.join(target, "themes"), { recursive: true })
  399. await Bun.write(path.join(target, "themes", "forest.json"), JSON.stringify({ theme: { text: "#fff" } }, null, 2))
  400. const run = createPlugTask(
  401. {
  402. mod: "[email protected]",
  403. },
  404. deps(path.join(tmp.path, "global"), target),
  405. )
  406. const ok = await run(ctx(tmp.path))
  407. expect(ok).toBe(true)
  408. expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "tui.jsonc"))).toBe(true)
  409. expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false)
  410. const tui = await read(path.join(tmp.path, ".opencode", "tui.jsonc"))
  411. expect(tui.plugin).toEqual(["[email protected]"])
  412. })
  413. test("returns false for oc-themes outside plugin directory", async () => {
  414. await using tmp = await tmpdir()
  415. const target = await plugin(tmp.path, undefined, undefined, ["../outside.json"])
  416. const run = createPlugTask(
  417. {
  418. mod: "[email protected]",
  419. },
  420. deps(path.join(tmp.path, "global"), target),
  421. )
  422. const ok = await run(ctx(tmp.path))
  423. expect(ok).toBe(false)
  424. expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "tui.jsonc"))).toBe(false)
  425. expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false)
  426. })
  427. test("force replaces version in both server and tui configs", async () => {
  428. await using tmp = await tmpdir()
  429. const target = await plugin(tmp.path, ["server", "tui"])
  430. const server = path.join(tmp.path, ".opencode", "opencode.json")
  431. const tui = path.join(tmp.path, ".opencode", "tui.json")
  432. await fs.mkdir(path.dirname(server), { recursive: true })
  433. await Bun.write(server, JSON.stringify({ plugin: ["[email protected]", "[email protected]"] }, null, 2))
  434. await Bun.write(tui, JSON.stringify({ plugin: [["[email protected]", { mode: "safe" }], "[email protected]"] }, null, 2))
  435. const run = createPlugTask(
  436. {
  437. mod: "[email protected]",
  438. force: true,
  439. },
  440. deps(path.join(tmp.path, "global"), target),
  441. )
  442. const ok = await run(ctx(tmp.path))
  443. expect(ok).toBe(true)
  444. const serverJson = await read(server)
  445. const tuiJson = await read(tui)
  446. expect(serverJson.plugin).toEqual(["[email protected]", "[email protected]"])
  447. expect(tuiJson.plugin).toEqual([["[email protected]", { mode: "safe" }], "[email protected]"])
  448. })
  449. test("returns false and keeps config unchanged for invalid JSONC", async () => {
  450. await using tmp = await tmpdir()
  451. const target = await plugin(tmp.path, ["server"])
  452. const cfg = path.join(tmp.path, ".opencode", "opencode.jsonc")
  453. await fs.mkdir(path.dirname(cfg), { recursive: true })
  454. const bad = '{"plugin": ["[email protected]",}'
  455. await Bun.write(cfg, bad)
  456. const run = createPlugTask(
  457. {
  458. mod: "[email protected]",
  459. },
  460. deps(path.join(tmp.path, "global"), target),
  461. )
  462. const ok = await run(ctx(tmp.path))
  463. expect(ok).toBe(false)
  464. expect(await fs.readFile(cfg, "utf8")).toBe(bad)
  465. })
  466. test("returns false when manifest declares no supported targets", async () => {
  467. await using tmp = await tmpdir()
  468. const target = await plugin(tmp.path)
  469. const run = createPlugTask(
  470. {
  471. mod: "[email protected]",
  472. },
  473. deps(path.join(tmp.path, "global"), target),
  474. )
  475. const ok = await run(ctx(tmp.path))
  476. expect(ok).toBe(false)
  477. expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false)
  478. expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "tui.jsonc"))).toBe(false)
  479. })
  480. test("returns false when manifest cannot be read", async () => {
  481. await using tmp = await tmpdir()
  482. const target = path.join(tmp.path, "plugin")
  483. await fs.mkdir(target, { recursive: true })
  484. const run = createPlugTask(
  485. {
  486. mod: "[email protected]",
  487. },
  488. deps(path.join(tmp.path, "global"), target),
  489. )
  490. const ok = await run(ctx(tmp.path))
  491. expect(ok).toBe(false)
  492. expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false)
  493. })
  494. test("returns false when install fails", async () => {
  495. await using tmp = await tmpdir()
  496. const run = createPlugTask(
  497. {
  498. mod: "[email protected]",
  499. },
  500. deps(path.join(tmp.path, "global"), new Error("boom")),
  501. )
  502. const ok = await run(ctx(tmp.path))
  503. expect(ok).toBe(false)
  504. expect(await Filesystem.exists(path.join(tmp.path, ".opencode", "opencode.jsonc"))).toBe(false)
  505. })
  506. })