plugin-toggle.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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("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 get = spyOn(TuiConfig, "get").mockResolvedValue({
  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)
  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. get.mockRestore()
  79. wait.mockRestore()
  80. delete process.env.KILO_PLUGIN_META_FILE
  81. }
  82. })
  83. test("kv plugin_enabled overrides tui config on startup", async () => {
  84. await using tmp = await tmpdir({
  85. init: async (dir) => {
  86. const file = path.join(dir, "startup-plugin.ts")
  87. const spec = pathToFileURL(file).href
  88. const marker = path.join(dir, "startup.txt")
  89. await Bun.write(
  90. file,
  91. `export default {
  92. id: "demo.startup",
  93. tui: async (_api, options) => {
  94. await Bun.write(options.marker, "on")
  95. },
  96. }
  97. `,
  98. )
  99. return {
  100. spec,
  101. marker,
  102. }
  103. },
  104. })
  105. process.env.KILO_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  106. const get = spyOn(TuiConfig, "get").mockResolvedValue({
  107. plugin: [[tmp.extra.spec, { marker: tmp.extra.marker }]],
  108. plugin_enabled: {
  109. "demo.startup": false,
  110. },
  111. plugin_origins: [
  112. {
  113. spec: [tmp.extra.spec, { marker: tmp.extra.marker }],
  114. scope: "local",
  115. source: path.join(tmp.path, "tui.json"),
  116. },
  117. ],
  118. })
  119. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  120. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  121. const api = createTuiPluginApi()
  122. api.kv.set("plugin_enabled", {
  123. "demo.startup": true,
  124. })
  125. try {
  126. await TuiPluginRuntime.init(api)
  127. await expect(fs.readFile(tmp.extra.marker, "utf8")).resolves.toBe("on")
  128. expect(TuiPluginRuntime.list().find((item) => item.id === "demo.startup")).toEqual({
  129. id: "demo.startup",
  130. source: "file",
  131. spec: tmp.extra.spec,
  132. target: tmp.extra.spec,
  133. enabled: true,
  134. active: true,
  135. })
  136. } finally {
  137. await TuiPluginRuntime.dispose()
  138. cwd.mockRestore()
  139. get.mockRestore()
  140. wait.mockRestore()
  141. delete process.env.KILO_PLUGIN_META_FILE
  142. }
  143. })