plugin-install.test.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { expect, spyOn, test } from "bun:test"
  2. import fs from "fs/promises"
  3. import path from "path"
  4. import { pathToFileURL } from "url"
  5. import { tmpdir } from "../../fixture/fixture"
  6. import { createTuiPluginApi } from "../../fixture/tui-plugin"
  7. import { TuiConfig } from "../../../src/config/tui"
  8. const { TuiPluginRuntime } = await import("../../../src/cli/cmd/tui/plugin/runtime")
  9. test("installs plugin without loading it", async () => {
  10. await using tmp = await tmpdir({
  11. init: async (dir) => {
  12. const file = path.join(dir, "install-plugin.ts")
  13. const spec = pathToFileURL(file).href
  14. const marker = path.join(dir, "install.txt")
  15. await Bun.write(
  16. path.join(dir, "package.json"),
  17. JSON.stringify(
  18. {
  19. name: "demo-install-plugin",
  20. type: "module",
  21. exports: {
  22. "./tui": {
  23. import: "./install-plugin.ts",
  24. config: { marker },
  25. },
  26. },
  27. },
  28. null,
  29. 2,
  30. ),
  31. )
  32. await Bun.write(
  33. file,
  34. `export default {
  35. id: "demo.install",
  36. tui: async (_api, options) => {
  37. if (!options?.marker) return
  38. await Bun.write(options.marker, "loaded")
  39. },
  40. }
  41. `,
  42. )
  43. return { spec, marker }
  44. },
  45. })
  46. process.env.OPENCODE_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  47. const cfg: Awaited<ReturnType<typeof TuiConfig.get>> = {
  48. plugin: [],
  49. plugin_records: undefined,
  50. }
  51. const get = spyOn(TuiConfig, "get").mockImplementation(async () => cfg)
  52. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  53. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  54. const api = createTuiPluginApi({
  55. state: {
  56. path: {
  57. state: path.join(tmp.path, "state.json"),
  58. config: path.join(tmp.path, "tui.json"),
  59. worktree: tmp.path,
  60. directory: tmp.path,
  61. },
  62. },
  63. })
  64. try {
  65. await TuiPluginRuntime.init(api)
  66. const out = await TuiPluginRuntime.installPlugin(tmp.extra.spec)
  67. expect(out).toMatchObject({
  68. ok: true,
  69. tui: true,
  70. })
  71. await expect(fs.readFile(tmp.extra.marker, "utf8")).rejects.toThrow()
  72. await expect(TuiPluginRuntime.addPlugin(tmp.extra.spec)).resolves.toBe(true)
  73. await expect(fs.readFile(tmp.extra.marker, "utf8")).resolves.toBe("loaded")
  74. } finally {
  75. await TuiPluginRuntime.dispose()
  76. cwd.mockRestore()
  77. get.mockRestore()
  78. wait.mockRestore()
  79. delete process.env.OPENCODE_PLUGIN_META_FILE
  80. }
  81. })