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 { createTuiResolvedConfig } from "../../fixture/tui-runtime"
  8. import { TuiConfig } from "../../../src/config/tui"
  9. const { TuiPluginRuntime } = await import("../../../src/plugin/tui/runtime")
  10. test("installs plugin without loading it", async () => {
  11. await using tmp = await tmpdir({
  12. init: async (dir) => {
  13. const file = path.join(dir, "install-plugin.ts")
  14. const spec = pathToFileURL(file).href
  15. const marker = path.join(dir, "install.txt")
  16. await Bun.write(
  17. path.join(dir, "package.json"),
  18. JSON.stringify(
  19. {
  20. name: "demo-install-plugin",
  21. type: "module",
  22. exports: {
  23. "./tui": {
  24. import: "./install-plugin.ts",
  25. config: { marker },
  26. },
  27. },
  28. },
  29. null,
  30. 2,
  31. ),
  32. )
  33. await Bun.write(
  34. file,
  35. `export default {
  36. id: "demo.install",
  37. tui: async (_api, options) => {
  38. if (!options?.marker) return
  39. await Bun.write(options.marker, "loaded")
  40. },
  41. }
  42. `,
  43. )
  44. return { spec, marker }
  45. },
  46. })
  47. process.env.KIRINCODE_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  48. const config = createTuiResolvedConfig({
  49. plugin: [],
  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.KIRINCODE_PLUGIN_META_FILE
  78. }
  79. })