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