plugin-install.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/cli/cmd/tui/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.KILO_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  47. const config: TuiConfig.Info = {
  48. plugin: [],
  49. plugin_origins: undefined,
  50. }
  51. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  52. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  53. const api = createTuiPluginApi({
  54. state: {
  55. path: {
  56. state: path.join(tmp.path, "state.json"),
  57. config: path.join(tmp.path, "tui.json"),
  58. worktree: tmp.path,
  59. directory: tmp.path,
  60. },
  61. },
  62. })
  63. try {
  64. await TuiPluginRuntime.init({ api, config })
  65. const out = await TuiPluginRuntime.installPlugin(tmp.extra.spec)
  66. expect(out).toMatchObject({
  67. ok: true,
  68. tui: true,
  69. })
  70. await expect(fs.readFile(tmp.extra.marker, "utf8")).rejects.toThrow()
  71. await expect(TuiPluginRuntime.addPlugin(tmp.extra.spec)).resolves.toBe(true)
  72. await expect(fs.readFile(tmp.extra.marker, "utf8")).resolves.toBe("loaded")
  73. } finally {
  74. await TuiPluginRuntime.dispose()
  75. cwd.mockRestore()
  76. wait.mockRestore()
  77. delete process.env.KILO_PLUGIN_META_FILE
  78. }
  79. })