plugin-lifecycle.test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 { mockTuiRuntime } from "../../fixture/tui-runtime"
  8. const { TuiPluginRuntime } = await import("../../../src/plugin/tui/runtime")
  9. test("runs onDispose callbacks with aborted signal and is idempotent", 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, "marker.txt")
  15. await Bun.write(
  16. file,
  17. `export default {
  18. id: "demo.lifecycle",
  19. tui: async (api, options) => {
  20. api.event.on("event.test", () => {})
  21. api.route.register([{ name: "lifecycle.route", render: () => null }])
  22. api.lifecycle.onDispose(async () => {
  23. const prev = await Bun.file(options.marker).text().catch(() => "")
  24. await Bun.write(options.marker, prev + "custom\\n")
  25. })
  26. api.lifecycle.onDispose(async () => {
  27. const prev = await Bun.file(options.marker).text().catch(() => "")
  28. await Bun.write(options.marker, prev + "aborted:" + String(api.lifecycle.signal.aborted) + "\\n")
  29. })
  30. },
  31. }
  32. `,
  33. )
  34. return { spec, marker }
  35. },
  36. })
  37. const { config, restore } = mockTuiRuntime(tmp.path, [[tmp.extra.spec, { marker: tmp.extra.marker }]])
  38. try {
  39. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config })
  40. await TuiPluginRuntime.dispose()
  41. const marker = await fs.readFile(tmp.extra.marker, "utf8")
  42. expect(marker).toContain("custom")
  43. expect(marker).toContain("aborted:true")
  44. // second dispose is a no-op
  45. await TuiPluginRuntime.dispose()
  46. const after = await fs.readFile(tmp.extra.marker, "utf8")
  47. expect(after).toBe(marker)
  48. } finally {
  49. await TuiPluginRuntime.dispose()
  50. restore()
  51. }
  52. })
  53. test("rolls back failed plugin and continues loading next", async () => {
  54. await using tmp = await tmpdir({
  55. init: async (dir) => {
  56. const bad = path.join(dir, "bad-plugin.ts")
  57. const good = path.join(dir, "good-plugin.ts")
  58. const badSpec = pathToFileURL(bad).href
  59. const goodSpec = pathToFileURL(good).href
  60. const badMarker = path.join(dir, "bad-cleanup.txt")
  61. const goodMarker = path.join(dir, "good-called.txt")
  62. await Bun.write(
  63. bad,
  64. `export default {
  65. id: "demo.bad",
  66. tui: async (api, options) => {
  67. api.route.register([{ name: "bad.route", render: () => null }])
  68. api.lifecycle.onDispose(async () => {
  69. await Bun.write(options.bad_marker, "cleaned")
  70. })
  71. throw new Error("bad plugin")
  72. },
  73. }
  74. `,
  75. )
  76. await Bun.write(
  77. good,
  78. `export default {
  79. id: "demo.good",
  80. tui: async (_api, options) => {
  81. await Bun.write(options.good_marker, "called")
  82. },
  83. }
  84. `,
  85. )
  86. return { badSpec, goodSpec, badMarker, goodMarker }
  87. },
  88. })
  89. const { config, restore } = mockTuiRuntime(tmp.path, [
  90. [tmp.extra.badSpec, { bad_marker: tmp.extra.badMarker }],
  91. [tmp.extra.goodSpec, { good_marker: tmp.extra.goodMarker }],
  92. ])
  93. try {
  94. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config })
  95. // bad plugin's onDispose ran during rollback
  96. await expect(fs.readFile(tmp.extra.badMarker, "utf8")).resolves.toBe("cleaned")
  97. // good plugin still loaded
  98. await expect(fs.readFile(tmp.extra.goodMarker, "utf8")).resolves.toBe("called")
  99. } finally {
  100. await TuiPluginRuntime.dispose()
  101. restore()
  102. }
  103. })
  104. test("assigns sequential slot ids scoped to plugin", async () => {
  105. await using tmp = await tmpdir({
  106. init: async (dir) => {
  107. const file = path.join(dir, "slot-plugin.ts")
  108. const spec = pathToFileURL(file).href
  109. const marker = path.join(dir, "slot-setup.txt")
  110. await Bun.write(
  111. file,
  112. `import fs from "fs"
  113. const mark = (label) => {
  114. fs.appendFileSync(${JSON.stringify(marker)}, label + "\\n")
  115. }
  116. export default {
  117. id: "demo.slot",
  118. tui: async (api) => {
  119. const one = api.slots.register({
  120. id: 1,
  121. setup: () => { mark("one") },
  122. slots: { home_logo() { return null } },
  123. })
  124. const two = api.slots.register({
  125. id: 2,
  126. setup: () => { mark("two") },
  127. slots: { home_bottom() { return null } },
  128. })
  129. mark("id:" + one)
  130. mark("id:" + two)
  131. },
  132. }
  133. `,
  134. )
  135. return { spec, marker }
  136. },
  137. })
  138. const { config, restore } = mockTuiRuntime(tmp.path, [tmp.extra.spec])
  139. const err = spyOn(console, "error").mockImplementation(() => {})
  140. try {
  141. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config })
  142. const marker = await fs.readFile(tmp.extra.marker, "utf8")
  143. expect(marker).toContain("one")
  144. expect(marker).toContain("two")
  145. expect(marker).toContain("id:demo.slot")
  146. expect(marker).toContain("id:demo.slot:1")
  147. // no initialization failures
  148. const hit = err.mock.calls.find(
  149. (item) => typeof item[0] === "string" && item[0].includes("failed to initialize tui plugin"),
  150. )
  151. expect(hit).toBeUndefined()
  152. } finally {
  153. await TuiPluginRuntime.dispose()
  154. err.mockRestore()
  155. restore()
  156. }
  157. })
  158. test(
  159. "times out hanging plugin cleanup on dispose",
  160. async () => {
  161. await using tmp = await tmpdir({
  162. init: async (dir) => {
  163. const file = path.join(dir, "timeout-plugin.ts")
  164. const spec = pathToFileURL(file).href
  165. await Bun.write(
  166. file,
  167. `export default {
  168. id: "demo.timeout",
  169. tui: async (api) => {
  170. api.lifecycle.onDispose(() => new Promise(() => {}))
  171. },
  172. }
  173. `,
  174. )
  175. return { spec }
  176. },
  177. })
  178. const { config, restore } = mockTuiRuntime(tmp.path, [tmp.extra.spec])
  179. try {
  180. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config, disposeTimeoutMs: 25 })
  181. const done = await new Promise<string>((resolve) => {
  182. const timer = setTimeout(() => resolve("timeout"), 500)
  183. void TuiPluginRuntime.dispose().then(() => {
  184. clearTimeout(timer)
  185. resolve("done")
  186. })
  187. })
  188. expect(done).toBe("done")
  189. } finally {
  190. await TuiPluginRuntime.dispose()
  191. restore()
  192. }
  193. },
  194. { timeout: 15000 },
  195. )