plugin-add.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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("adds tui plugin at runtime from spec", async () => {
  10. await using tmp = await tmpdir({
  11. init: async (dir) => {
  12. const file = path.join(dir, "add-plugin.ts")
  13. const spec = pathToFileURL(file).href
  14. const marker = path.join(dir, "add.txt")
  15. await Bun.write(
  16. file,
  17. `export default {
  18. id: "demo.add",
  19. tui: async () => {
  20. await Bun.write(${JSON.stringify(marker)}, "called")
  21. },
  22. }
  23. `,
  24. )
  25. return { spec, marker }
  26. },
  27. })
  28. process.env.OPENCODE_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  29. const get = spyOn(TuiConfig, "get").mockResolvedValue({
  30. plugin: [],
  31. plugin_meta: undefined,
  32. })
  33. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  34. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  35. try {
  36. await TuiPluginRuntime.init(createTuiPluginApi())
  37. await expect(TuiPluginRuntime.addPlugin(tmp.extra.spec)).resolves.toBe(true)
  38. await expect(fs.readFile(tmp.extra.marker, "utf8")).resolves.toBe("called")
  39. expect(TuiPluginRuntime.list().find((item) => item.id === "demo.add")).toEqual({
  40. id: "demo.add",
  41. source: "file",
  42. spec: tmp.extra.spec,
  43. target: tmp.extra.spec,
  44. enabled: true,
  45. active: true,
  46. })
  47. } finally {
  48. await TuiPluginRuntime.dispose()
  49. cwd.mockRestore()
  50. get.mockRestore()
  51. wait.mockRestore()
  52. delete process.env.OPENCODE_PLUGIN_META_FILE
  53. }
  54. })