plugin-loader-pure.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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("skips external tui plugins in pure mode", async () => {
  10. await using tmp = await tmpdir({
  11. init: async (dir) => {
  12. const file = path.join(dir, "plugin.ts")
  13. const spec = pathToFileURL(file).href
  14. const marker = path.join(dir, "called.txt")
  15. const meta = path.join(dir, "plugin-meta.json")
  16. await Bun.write(
  17. file,
  18. `export default {
  19. id: "demo.pure",
  20. tui: async (_api, options) => {
  21. if (!options?.marker) return
  22. await Bun.write(options.marker, "called")
  23. },
  24. }
  25. `,
  26. )
  27. return { spec, marker, meta }
  28. },
  29. })
  30. const pure = process.env.KILO_PURE
  31. const meta = process.env.KILO_PLUGIN_META_FILE
  32. process.env.KILO_PURE = "1"
  33. process.env.KILO_PLUGIN_META_FILE = tmp.extra.meta
  34. const get = spyOn(TuiConfig, "get").mockResolvedValue({
  35. plugin: [[tmp.extra.spec, { marker: tmp.extra.marker }]],
  36. plugin_origins: [
  37. {
  38. spec: [tmp.extra.spec, { marker: tmp.extra.marker }],
  39. scope: "local",
  40. source: path.join(tmp.path, "tui.json"),
  41. },
  42. ],
  43. })
  44. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  45. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  46. try {
  47. await TuiPluginRuntime.init(createTuiPluginApi())
  48. await expect(fs.readFile(tmp.extra.marker, "utf8")).rejects.toThrow()
  49. } finally {
  50. await TuiPluginRuntime.dispose()
  51. cwd.mockRestore()
  52. get.mockRestore()
  53. wait.mockRestore()
  54. if (pure === undefined) {
  55. delete process.env.KILO_PURE
  56. } else {
  57. process.env.KILO_PURE = pure
  58. }
  59. if (meta === undefined) {
  60. delete process.env.KILO_PLUGIN_META_FILE
  61. } else {
  62. process.env.KILO_PLUGIN_META_FILE = meta
  63. }
  64. }
  65. })