plugin-toggle.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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("toggles plugin runtime state by exported id", async () => {
  10. await using tmp = await tmpdir({
  11. init: async (dir) => {
  12. const file = path.join(dir, "toggle-plugin.ts")
  13. const spec = pathToFileURL(file).href
  14. const marker = path.join(dir, "toggle.txt")
  15. await Bun.write(
  16. file,
  17. `export default {
  18. id: "demo.toggle",
  19. tui: async (api, options) => {
  20. const text = await Bun.file(options.marker).text().catch(() => "")
  21. await Bun.write(options.marker, text + "start\\n")
  22. api.lifecycle.onDispose(async () => {
  23. const next = await Bun.file(options.marker).text().catch(() => "")
  24. await Bun.write(options.marker, next + "stop\\n")
  25. })
  26. },
  27. }
  28. `,
  29. )
  30. return {
  31. spec,
  32. marker,
  33. }
  34. },
  35. })
  36. process.env.KILO_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  37. const config: TuiConfig.Info = {
  38. plugin: [[tmp.extra.spec, { marker: tmp.extra.marker }]],
  39. plugin_enabled: {
  40. "demo.toggle": false,
  41. },
  42. plugin_origins: [
  43. {
  44. spec: [tmp.extra.spec, { marker: tmp.extra.marker }],
  45. scope: "local",
  46. source: path.join(tmp.path, "tui.json"),
  47. },
  48. ],
  49. }
  50. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  51. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  52. const api = createTuiPluginApi()
  53. try {
  54. await TuiPluginRuntime.init({ api, config })
  55. await expect(fs.readFile(tmp.extra.marker, "utf8")).rejects.toThrow()
  56. expect(TuiPluginRuntime.list().find((item) => item.id === "demo.toggle")).toEqual({
  57. id: "demo.toggle",
  58. source: "file",
  59. spec: tmp.extra.spec,
  60. target: tmp.extra.spec,
  61. enabled: false,
  62. active: false,
  63. })
  64. await expect(TuiPluginRuntime.activatePlugin("demo.toggle")).resolves.toBe(true)
  65. await expect(fs.readFile(tmp.extra.marker, "utf8")).resolves.toBe("start\n")
  66. expect(api.kv.get("plugin_enabled", {})).toEqual({
  67. "demo.toggle": true,
  68. })
  69. await expect(TuiPluginRuntime.deactivatePlugin("demo.toggle")).resolves.toBe(true)
  70. await expect(fs.readFile(tmp.extra.marker, "utf8")).resolves.toBe("start\nstop\n")
  71. expect(api.kv.get("plugin_enabled", {})).toEqual({
  72. "demo.toggle": false,
  73. })
  74. await expect(TuiPluginRuntime.activatePlugin("missing.id")).resolves.toBe(false)
  75. } finally {
  76. await TuiPluginRuntime.dispose()
  77. cwd.mockRestore()
  78. wait.mockRestore()
  79. delete process.env.KILO_PLUGIN_META_FILE
  80. }
  81. })
  82. test("kv plugin_enabled overrides tui config on startup", async () => {
  83. await using tmp = await tmpdir({
  84. init: async (dir) => {
  85. const file = path.join(dir, "startup-plugin.ts")
  86. const spec = pathToFileURL(file).href
  87. const marker = path.join(dir, "startup.txt")
  88. await Bun.write(
  89. file,
  90. `export default {
  91. id: "demo.startup",
  92. tui: async (_api, options) => {
  93. await Bun.write(options.marker, "on")
  94. },
  95. }
  96. `,
  97. )
  98. return {
  99. spec,
  100. marker,
  101. }
  102. },
  103. })
  104. process.env.KILO_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  105. const config: TuiConfig.Info = {
  106. plugin: [[tmp.extra.spec, { marker: tmp.extra.marker }]],
  107. plugin_enabled: {
  108. "demo.startup": false,
  109. },
  110. plugin_origins: [
  111. {
  112. spec: [tmp.extra.spec, { marker: tmp.extra.marker }],
  113. scope: "local",
  114. source: path.join(tmp.path, "tui.json"),
  115. },
  116. ],
  117. }
  118. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  119. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  120. const api = createTuiPluginApi()
  121. api.kv.set("plugin_enabled", {
  122. "demo.startup": true,
  123. })
  124. try {
  125. await TuiPluginRuntime.init({ api, config })
  126. await expect(fs.readFile(tmp.extra.marker, "utf8")).resolves.toBe("on")
  127. expect(TuiPluginRuntime.list().find((item) => item.id === "demo.startup")).toEqual({
  128. id: "demo.startup",
  129. source: "file",
  130. spec: tmp.extra.spec,
  131. target: tmp.extra.spec,
  132. enabled: true,
  133. active: true,
  134. })
  135. } finally {
  136. await TuiPluginRuntime.dispose()
  137. cwd.mockRestore()
  138. wait.mockRestore()
  139. delete process.env.KILO_PLUGIN_META_FILE
  140. }
  141. })