plugin-loader-entrypoint.test.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. import { Npm } from "@kirincode-ai/core/npm"
  10. const { TuiPluginRuntime } = await import("../../../src/plugin/tui/runtime")
  11. test("loads npm tui plugin from package ./tui export", async () => {
  12. await using tmp = await tmpdir({
  13. init: async (dir) => {
  14. const mod = path.join(dir, "mods", "acme-plugin")
  15. const marker = path.join(dir, "tui-called.txt")
  16. await fs.mkdir(mod, { recursive: true })
  17. await Bun.write(
  18. path.join(mod, "package.json"),
  19. JSON.stringify({
  20. name: "acme-plugin",
  21. type: "module",
  22. exports: { ".": "./index.js", "./server": "./server.js", "./tui": "./tui.js" },
  23. }),
  24. )
  25. await Bun.write(path.join(mod, "index.js"), 'import "./main-throws.js"\nexport default {}\n')
  26. await Bun.write(path.join(mod, "main-throws.js"), 'throw new Error("main loaded")\n')
  27. await Bun.write(path.join(mod, "server.js"), "export default {}\n")
  28. await Bun.write(
  29. path.join(mod, "tui.js"),
  30. `export default {
  31. id: "demo.tui.export",
  32. tui: async (_api, options) => {
  33. if (!options?.marker) return
  34. await Bun.write(${JSON.stringify(marker)}, "called")
  35. },
  36. }
  37. `,
  38. )
  39. return { mod, marker, spec: "acme-plugin@1.0.0" }
  40. },
  41. })
  42. process.env.KIRINCODE_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  43. const config = createTuiResolvedConfig({
  44. plugin: [[tmp.extra.spec, { marker: tmp.extra.marker }]],
  45. plugin_origins: [
  46. {
  47. spec: [tmp.extra.spec, { marker: tmp.extra.marker }],
  48. scope: "local",
  49. source: path.join(tmp.path, "tui.json"),
  50. },
  51. ],
  52. })
  53. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  54. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  55. const install = spyOn(Npm, "add").mockResolvedValue({ directory: tmp.extra.mod, entrypoint: undefined })
  56. try {
  57. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config })
  58. await expect(fs.readFile(tmp.extra.marker, "utf8")).resolves.toBe("called")
  59. const hit = TuiPluginRuntime.list().find((item) => item.id === "demo.tui.export")
  60. expect(hit?.enabled).toBe(true)
  61. expect(hit?.active).toBe(true)
  62. expect(hit?.source).toBe("npm")
  63. } finally {
  64. await TuiPluginRuntime.dispose()
  65. install.mockRestore()
  66. cwd.mockRestore()
  67. wait.mockRestore()
  68. delete process.env.KIRINCODE_PLUGIN_META_FILE
  69. }
  70. })
  71. test("does not use npm package exports dot for tui entry", async () => {
  72. await using tmp = await tmpdir({
  73. init: async (dir) => {
  74. const mod = path.join(dir, "mods", "acme-plugin")
  75. const marker = path.join(dir, "dot-called.txt")
  76. await fs.mkdir(mod, { recursive: true })
  77. await Bun.write(
  78. path.join(mod, "package.json"),
  79. JSON.stringify({
  80. name: "acme-plugin",
  81. type: "module",
  82. exports: { ".": "./index.js" },
  83. }),
  84. )
  85. await Bun.write(
  86. path.join(mod, "index.js"),
  87. `export default {
  88. id: "demo.dot",
  89. tui: async () => {
  90. await Bun.write(${JSON.stringify(marker)}, "called")
  91. },
  92. }
  93. `,
  94. )
  95. return { mod, marker, spec: "acme-plugin@1.0.0" }
  96. },
  97. })
  98. process.env.KIRINCODE_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  99. const config = createTuiResolvedConfig({
  100. plugin: [tmp.extra.spec],
  101. plugin_origins: [
  102. {
  103. spec: tmp.extra.spec,
  104. scope: "local",
  105. source: path.join(tmp.path, "tui.json"),
  106. },
  107. ],
  108. })
  109. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  110. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  111. const install = spyOn(Npm, "add").mockResolvedValue({ directory: tmp.extra.mod, entrypoint: undefined })
  112. try {
  113. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config })
  114. await expect(fs.readFile(tmp.extra.marker, "utf8")).rejects.toThrow()
  115. expect(TuiPluginRuntime.list().some((item) => item.spec === tmp.extra.spec)).toBe(false)
  116. } finally {
  117. await TuiPluginRuntime.dispose()
  118. install.mockRestore()
  119. cwd.mockRestore()
  120. wait.mockRestore()
  121. delete process.env.KIRINCODE_PLUGIN_META_FILE
  122. }
  123. })
  124. test("rejects npm tui export that resolves outside plugin directory", async () => {
  125. await using tmp = await tmpdir({
  126. init: async (dir) => {
  127. const mod = path.join(dir, "mods", "acme-plugin")
  128. const outside = path.join(dir, "outside")
  129. const marker = path.join(dir, "outside-called.txt")
  130. await fs.mkdir(mod, { recursive: true })
  131. await fs.mkdir(outside, { recursive: true })
  132. await Bun.write(
  133. path.join(mod, "package.json"),
  134. JSON.stringify({
  135. name: "acme-plugin",
  136. type: "module",
  137. exports: { ".": "./index.js", "./tui": "./escape/tui.js" },
  138. }),
  139. )
  140. await Bun.write(path.join(mod, "index.js"), "export default {}\n")
  141. await Bun.write(
  142. path.join(outside, "tui.js"),
  143. `export default {
  144. id: "demo.outside",
  145. tui: async () => {
  146. await Bun.write(${JSON.stringify(marker)}, "outside")
  147. },
  148. }
  149. `,
  150. )
  151. await fs.symlink(outside, path.join(mod, "escape"), process.platform === "win32" ? "junction" : "dir")
  152. return { mod, marker, spec: "acme-plugin@1.0.0" }
  153. },
  154. })
  155. process.env.KIRINCODE_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  156. const config = createTuiResolvedConfig({
  157. plugin: [tmp.extra.spec],
  158. plugin_origins: [
  159. {
  160. spec: tmp.extra.spec,
  161. scope: "local",
  162. source: path.join(tmp.path, "tui.json"),
  163. },
  164. ],
  165. })
  166. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  167. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  168. const install = spyOn(Npm, "add").mockResolvedValue({ directory: tmp.extra.mod, entrypoint: undefined })
  169. try {
  170. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config })
  171. // plugin code never ran
  172. await expect(fs.readFile(tmp.extra.marker, "utf8")).rejects.toThrow()
  173. // plugin not listed
  174. expect(TuiPluginRuntime.list().some((item) => item.spec === tmp.extra.spec)).toBe(false)
  175. } finally {
  176. await TuiPluginRuntime.dispose()
  177. install.mockRestore()
  178. cwd.mockRestore()
  179. wait.mockRestore()
  180. delete process.env.KIRINCODE_PLUGIN_META_FILE
  181. }
  182. })
  183. test("rejects npm tui plugin that exports server and tui together", async () => {
  184. await using tmp = await tmpdir({
  185. init: async (dir) => {
  186. const mod = path.join(dir, "mods", "acme-plugin")
  187. const marker = path.join(dir, "mixed-called.txt")
  188. await fs.mkdir(mod, { recursive: true })
  189. await Bun.write(
  190. path.join(mod, "package.json"),
  191. JSON.stringify({
  192. name: "acme-plugin",
  193. type: "module",
  194. exports: { ".": "./index.js", "./tui": "./tui.js" },
  195. }),
  196. )
  197. await Bun.write(path.join(mod, "index.js"), "export default {}\n")
  198. await Bun.write(
  199. path.join(mod, "tui.js"),
  200. `export default {
  201. id: "demo.mixed",
  202. server: async () => ({}),
  203. tui: async () => {
  204. await Bun.write(${JSON.stringify(marker)}, "called")
  205. },
  206. }
  207. `,
  208. )
  209. return { mod, marker, spec: "acme-plugin@1.0.0" }
  210. },
  211. })
  212. process.env.KIRINCODE_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  213. const config = createTuiResolvedConfig({
  214. plugin: [tmp.extra.spec],
  215. plugin_origins: [
  216. {
  217. spec: tmp.extra.spec,
  218. scope: "local",
  219. source: path.join(tmp.path, "tui.json"),
  220. },
  221. ],
  222. })
  223. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  224. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  225. const install = spyOn(Npm, "add").mockResolvedValue({ directory: tmp.extra.mod, entrypoint: undefined })
  226. try {
  227. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config })
  228. await expect(fs.readFile(tmp.extra.marker, "utf8")).rejects.toThrow()
  229. expect(TuiPluginRuntime.list().some((item) => item.spec === tmp.extra.spec)).toBe(false)
  230. } finally {
  231. await TuiPluginRuntime.dispose()
  232. install.mockRestore()
  233. cwd.mockRestore()
  234. wait.mockRestore()
  235. delete process.env.KIRINCODE_PLUGIN_META_FILE
  236. }
  237. })
  238. test("does not use npm package main for tui entry", async () => {
  239. await using tmp = await tmpdir({
  240. init: async (dir) => {
  241. const mod = path.join(dir, "mods", "acme-plugin")
  242. const marker = path.join(dir, "main-called.txt")
  243. await fs.mkdir(mod, { recursive: true })
  244. await Bun.write(
  245. path.join(mod, "package.json"),
  246. JSON.stringify({
  247. name: "acme-plugin",
  248. type: "module",
  249. main: "./index.js",
  250. }),
  251. )
  252. await Bun.write(
  253. path.join(mod, "index.js"),
  254. `export default {
  255. id: "demo.main",
  256. tui: async () => {
  257. await Bun.write(${JSON.stringify(marker)}, "called")
  258. },
  259. }
  260. `,
  261. )
  262. return { mod, marker, spec: "acme-plugin@1.0.0" }
  263. },
  264. })
  265. process.env.KIRINCODE_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  266. const config = createTuiResolvedConfig({
  267. plugin: [tmp.extra.spec],
  268. plugin_origins: [
  269. {
  270. spec: tmp.extra.spec,
  271. scope: "local",
  272. source: path.join(tmp.path, "tui.json"),
  273. },
  274. ],
  275. })
  276. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  277. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  278. const install = spyOn(Npm, "add").mockResolvedValue({ directory: tmp.extra.mod, entrypoint: undefined })
  279. const warn = spyOn(console, "warn").mockImplementation(() => {})
  280. const error = spyOn(console, "error").mockImplementation(() => {})
  281. try {
  282. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config })
  283. await expect(fs.readFile(tmp.extra.marker, "utf8")).rejects.toThrow()
  284. expect(TuiPluginRuntime.list().some((item) => item.spec === tmp.extra.spec)).toBe(false)
  285. expect(error).not.toHaveBeenCalled()
  286. expect(warn.mock.calls.some((call) => String(call[0]).includes("tui plugin has no entrypoint"))).toBe(true)
  287. } finally {
  288. await TuiPluginRuntime.dispose()
  289. install.mockRestore()
  290. cwd.mockRestore()
  291. wait.mockRestore()
  292. warn.mockRestore()
  293. error.mockRestore()
  294. delete process.env.KIRINCODE_PLUGIN_META_FILE
  295. }
  296. })
  297. test("does not use directory package main for tui entry", async () => {
  298. await using tmp = await tmpdir({
  299. init: async (dir) => {
  300. const mod = path.join(dir, "mods", "dir-plugin")
  301. const spec = pathToFileURL(mod).href
  302. const marker = path.join(dir, "dir-main-called.txt")
  303. await fs.mkdir(mod, { recursive: true })
  304. await Bun.write(
  305. path.join(mod, "package.json"),
  306. JSON.stringify({
  307. name: "dir-plugin",
  308. type: "module",
  309. main: "./main.js",
  310. }),
  311. )
  312. await Bun.write(
  313. path.join(mod, "main.js"),
  314. `export default {
  315. id: "demo.dir.main",
  316. tui: async () => {
  317. await Bun.write(${JSON.stringify(marker)}, "called")
  318. },
  319. }
  320. `,
  321. )
  322. return { marker, spec }
  323. },
  324. })
  325. process.env.KIRINCODE_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  326. const config = createTuiResolvedConfig({
  327. plugin: [tmp.extra.spec],
  328. plugin_origins: [
  329. {
  330. spec: tmp.extra.spec,
  331. scope: "local",
  332. source: path.join(tmp.path, "tui.json"),
  333. },
  334. ],
  335. })
  336. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  337. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  338. try {
  339. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config })
  340. await expect(fs.readFile(tmp.extra.marker, "utf8")).rejects.toThrow()
  341. expect(TuiPluginRuntime.list().some((item) => item.spec === tmp.extra.spec)).toBe(false)
  342. } finally {
  343. await TuiPluginRuntime.dispose()
  344. cwd.mockRestore()
  345. wait.mockRestore()
  346. delete process.env.KIRINCODE_PLUGIN_META_FILE
  347. }
  348. })
  349. test("uses directory index fallback for tui when package.json is missing", async () => {
  350. await using tmp = await tmpdir({
  351. init: async (dir) => {
  352. const mod = path.join(dir, "mods", "dir-index")
  353. const spec = pathToFileURL(mod).href
  354. const marker = path.join(dir, "dir-index-called.txt")
  355. await fs.mkdir(mod, { recursive: true })
  356. await Bun.write(
  357. path.join(mod, "index.ts"),
  358. `export default {
  359. id: "demo.dir.index",
  360. tui: async () => {
  361. await Bun.write(${JSON.stringify(marker)}, "called")
  362. },
  363. }
  364. `,
  365. )
  366. return { marker, spec }
  367. },
  368. })
  369. process.env.KIRINCODE_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  370. const config = createTuiResolvedConfig({
  371. plugin: [tmp.extra.spec],
  372. plugin_origins: [
  373. {
  374. spec: tmp.extra.spec,
  375. scope: "local",
  376. source: path.join(tmp.path, "tui.json"),
  377. },
  378. ],
  379. })
  380. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  381. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  382. try {
  383. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config })
  384. await expect(fs.readFile(tmp.extra.marker, "utf8")).resolves.toBe("called")
  385. expect(TuiPluginRuntime.list().find((item) => item.id === "demo.dir.index")?.active).toBe(true)
  386. } finally {
  387. await TuiPluginRuntime.dispose()
  388. cwd.mockRestore()
  389. wait.mockRestore()
  390. delete process.env.KIRINCODE_PLUGIN_META_FILE
  391. }
  392. })
  393. test("uses npm package name when tui plugin id is omitted", async () => {
  394. await using tmp = await tmpdir({
  395. init: async (dir) => {
  396. const mod = path.join(dir, "mods", "acme-plugin")
  397. const marker = path.join(dir, "name-id-called.txt")
  398. await fs.mkdir(mod, { recursive: true })
  399. await Bun.write(
  400. path.join(mod, "package.json"),
  401. JSON.stringify({
  402. name: "acme-plugin",
  403. type: "module",
  404. exports: { ".": "./index.js", "./tui": "./tui.js" },
  405. }),
  406. )
  407. await Bun.write(path.join(mod, "index.js"), "export default {}\n")
  408. await Bun.write(
  409. path.join(mod, "tui.js"),
  410. `export default {
  411. tui: async (_api, options) => {
  412. if (!options?.marker) return
  413. await Bun.write(options.marker, "called")
  414. },
  415. }
  416. `,
  417. )
  418. return { mod, marker, spec: "acme-plugin@1.0.0" }
  419. },
  420. })
  421. process.env.KIRINCODE_PLUGIN_META_FILE = path.join(tmp.path, "plugin-meta.json")
  422. const config = createTuiResolvedConfig({
  423. plugin: [[tmp.extra.spec, { marker: tmp.extra.marker }]],
  424. plugin_origins: [
  425. {
  426. spec: [tmp.extra.spec, { marker: tmp.extra.marker }],
  427. scope: "local",
  428. source: path.join(tmp.path, "tui.json"),
  429. },
  430. ],
  431. })
  432. const wait = spyOn(TuiConfig, "waitForDependencies").mockResolvedValue()
  433. const cwd = spyOn(process, "cwd").mockImplementation(() => tmp.path)
  434. const install = spyOn(Npm, "add").mockResolvedValue({ directory: tmp.extra.mod, entrypoint: undefined })
  435. try {
  436. await TuiPluginRuntime.init({ api: createTuiPluginApi(), config })
  437. await expect(fs.readFile(tmp.extra.marker, "utf8")).resolves.toBe("called")
  438. expect(TuiPluginRuntime.list().find((item) => item.spec === tmp.extra.spec)?.id).toBe("acme-plugin")
  439. } finally {
  440. await TuiPluginRuntime.dispose()
  441. install.mockRestore()
  442. cwd.mockRestore()
  443. wait.mockRestore()
  444. delete process.env.KIRINCODE_PLUGIN_META_FILE
  445. }
  446. })