runtime.queue.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. import { describe, expect, test } from "bun:test"
  2. import { runPromptQueue as runPromptQueueBase, type QueueInput } from "../../src/mini/runtime.queue"
  3. import type { RunPrompt } from "../../src/mini/types"
  4. import { createFooterApiFixture } from "./fixture/footer-api"
  5. function runPromptQueue(input: Omit<QueueInput, "admit" | "settle"> & Partial<Pick<QueueInput, "admit" | "settle">>) {
  6. return runPromptQueueBase({
  7. admit: async () => {},
  8. settle: async () => {},
  9. ...input,
  10. })
  11. }
  12. describe("run runtime queue", () => {
  13. test("ignores empty prompts", async () => {
  14. const ui = createFooterApiFixture()
  15. let calls = 0
  16. const task = runPromptQueue({
  17. footer: ui.api,
  18. run: async () => {
  19. calls += 1
  20. },
  21. })
  22. ui.submit(" ")
  23. ui.api.close()
  24. await task
  25. expect(calls).toBe(0)
  26. })
  27. test("treats /exit as a close command", async () => {
  28. const ui = createFooterApiFixture()
  29. let calls = 0
  30. const task = runPromptQueue({
  31. footer: ui.api,
  32. run: async () => {
  33. calls += 1
  34. },
  35. })
  36. ui.submit("/exit")
  37. await task
  38. expect(calls).toBe(0)
  39. })
  40. test("treats /new as a local session command", async () => {
  41. const ui = createFooterApiFixture()
  42. const seen: string[] = []
  43. let created = 0
  44. const task = runPromptQueue({
  45. footer: ui.api,
  46. onNewSession: async () => {
  47. created += 1
  48. },
  49. run: async (input) => {
  50. seen.push(input.text)
  51. ui.api.close()
  52. },
  53. })
  54. ui.submit("/new")
  55. ui.submit("hello")
  56. await task
  57. expect(created).toBe(1)
  58. expect(seen).toEqual(["hello"])
  59. expect(ui.commits).toEqual([
  60. {
  61. kind: "user",
  62. text: "hello",
  63. phase: "start",
  64. source: "system",
  65. messageID: expect.any(String),
  66. },
  67. ])
  68. })
  69. test.each(["/compact", "/summarize"])("treats %s as a local compaction command", async (command) => {
  70. const ui = createFooterApiFixture()
  71. const seen: string[] = []
  72. let compacted = 0
  73. const task = runPromptQueue({
  74. footer: ui.api,
  75. onCompact: async () => {
  76. compacted += 1
  77. },
  78. run: async (input) => {
  79. seen.push(input.text)
  80. ui.api.close()
  81. },
  82. })
  83. ui.submit(command)
  84. ui.submit("hello")
  85. await task
  86. expect(compacted).toBe(1)
  87. expect(seen).toEqual(["hello"])
  88. expect(ui.commits.map((item) => item.text)).toEqual(["hello"])
  89. })
  90. test("keeps prompts submitted after an in-flight /compact behind the compaction barrier", async () => {
  91. const ui = createFooterApiFixture()
  92. const active = Promise.withResolvers<void>()
  93. const order: string[] = []
  94. const task = runPromptQueue({
  95. footer: ui.api,
  96. onCompact: async () => {
  97. order.push("compact")
  98. },
  99. admit: async (prompt) => {
  100. order.push(`admit:${prompt.text}`)
  101. },
  102. run: async (prompt) => {
  103. order.push(`run:${prompt.text}`)
  104. if (prompt.text === "first") await active.promise
  105. if (prompt.text === "later") ui.api.close()
  106. },
  107. })
  108. ui.submit("first")
  109. await Promise.resolve()
  110. ui.submit("/compact")
  111. ui.submit("later")
  112. await Promise.resolve()
  113. expect(order).toEqual(["run:first"])
  114. active.resolve()
  115. await task
  116. expect(order).toEqual(["run:first", "compact", "run:later"])
  117. })
  118. test("shell mode submits /exit as a shell command", async () => {
  119. const ui = createFooterApiFixture()
  120. const seen: RunPrompt[] = []
  121. const task = runPromptQueue({
  122. footer: ui.api,
  123. run: async (input) => {
  124. seen.push(input)
  125. ui.api.close()
  126. },
  127. })
  128. ui.submit("/exit", "shell")
  129. await task
  130. expect(seen).toEqual([{ text: "/exit", parts: [], mode: "shell" }])
  131. expect(ui.commits).toEqual([])
  132. })
  133. test("shell mode submits /new instead of creating a session", async () => {
  134. const ui = createFooterApiFixture()
  135. const seen: RunPrompt[] = []
  136. let created = 0
  137. const task = runPromptQueue({
  138. footer: ui.api,
  139. onNewSession: async () => {
  140. created += 1
  141. },
  142. run: async (input) => {
  143. seen.push(input)
  144. ui.api.close()
  145. },
  146. })
  147. ui.submit("/new", "shell")
  148. await task
  149. expect(created).toBe(0)
  150. expect(seen).toEqual([{ text: "/new", parts: [], mode: "shell" }])
  151. expect(ui.commits).toEqual([])
  152. })
  153. test("shell mode does not append a synthetic user row", async () => {
  154. const ui = createFooterApiFixture()
  155. const task = runPromptQueue({
  156. footer: ui.api,
  157. run: async () => {
  158. expect(ui.commits).toEqual([])
  159. ui.api.close()
  160. },
  161. })
  162. ui.submit("ls", "shell")
  163. await task
  164. })
  165. test("shell mode does not emit a turn duration summary", async () => {
  166. const ui = createFooterApiFixture()
  167. const task = runPromptQueue({
  168. footer: ui.api,
  169. run: async () => {
  170. ui.api.close()
  171. },
  172. })
  173. ui.submit("ls", "shell")
  174. await task
  175. expect(ui.events.some((event) => event.type === "turn.duration")).toBe(false)
  176. })
  177. test("preserves whitespace for initial input", async () => {
  178. const ui = createFooterApiFixture()
  179. const seen: string[] = []
  180. await runPromptQueue({
  181. footer: ui.api,
  182. initialInput: " hello ",
  183. run: async (input) => {
  184. seen.push(input.text)
  185. ui.api.close()
  186. },
  187. })
  188. expect(seen).toEqual([" hello "])
  189. expect(ui.commits).toEqual([
  190. {
  191. kind: "user",
  192. text: " hello ",
  193. phase: "start",
  194. source: "system",
  195. messageID: expect.any(String),
  196. },
  197. ])
  198. })
  199. test("durably admits in-flight follow-ups in submission order", async () => {
  200. const ui = createFooterApiFixture()
  201. const admitted: string[] = []
  202. const gate = Promise.withResolvers<void>()
  203. const task = runPromptQueue({
  204. footer: ui.api,
  205. run: async (input, _signal, onAdmitted) => {
  206. admitted.push(`${input.text}:steer`)
  207. onAdmitted()
  208. await gate.promise
  209. },
  210. admit: async (input) => {
  211. admitted.push(`${input.text}:queue`)
  212. },
  213. settle: async () => ui.api.close(),
  214. })
  215. ui.submit("one")
  216. ui.submit("two")
  217. ui.submit("three")
  218. while (admitted.length < 3) await Bun.sleep(0)
  219. expect(admitted).toEqual(["one:steer", "two:queue", "three:queue"])
  220. expect(ui.commits.map((item) => item.text)).toEqual(["one"])
  221. gate.resolve()
  222. await task
  223. })
  224. test("continues durable admission after one fails", async () => {
  225. const ui = createFooterApiFixture()
  226. const admitted: string[] = []
  227. const errors: string[] = []
  228. const gate = Promise.withResolvers<void>()
  229. const task = runPromptQueue({
  230. footer: ui.api,
  231. run: async (_input, _signal, admitted) => {
  232. admitted()
  233. await gate.promise
  234. },
  235. admit: async (input) => {
  236. if (input.text === "two") throw new Error("admission failed")
  237. admitted.push(input.text)
  238. },
  239. onAdmissionError: (_prompt, error) => {
  240. errors.push(error instanceof Error ? error.message : String(error))
  241. },
  242. settle: async () => ui.api.close(),
  243. })
  244. ui.submit("one")
  245. ui.submit("two")
  246. ui.submit("three")
  247. while (admitted.length === 0) await Bun.sleep(0)
  248. gate.resolve()
  249. await task
  250. expect(errors).toEqual(["admission failed"])
  251. expect(admitted).toEqual(["three"])
  252. })
  253. test("close aborts an in-flight durable admission", async () => {
  254. const ui = createFooterApiFixture()
  255. let admissionHit = false
  256. const admissionStarted = Promise.withResolvers<void>()
  257. const task = runPromptQueue({
  258. footer: ui.api,
  259. run: async (_input, signal, admitted) => {
  260. admitted()
  261. await new Promise<void>((resolve) => signal.addEventListener("abort", () => resolve(), { once: true }))
  262. },
  263. admit: async (_prompt, signal) => {
  264. admissionStarted.resolve()
  265. await new Promise<void>((resolve) => {
  266. if (signal.aborted) {
  267. admissionHit = true
  268. resolve()
  269. return
  270. }
  271. signal.addEventListener(
  272. "abort",
  273. () => {
  274. admissionHit = true
  275. resolve()
  276. },
  277. { once: true },
  278. )
  279. })
  280. },
  281. })
  282. ui.submit("one")
  283. await Promise.resolve()
  284. ui.submit("two")
  285. await admissionStarted.promise
  286. ui.api.close()
  287. await task
  288. expect(admissionHit).toBe(true)
  289. })
  290. test.each([
  291. ["session", undefined, false],
  292. ["shell", "shell", true],
  293. ] as const)("close handles an active %s turn", async (_name, mode, aborted) => {
  294. const ui = createFooterApiFixture()
  295. const started = Promise.withResolvers<AbortSignal>()
  296. const active = Promise.withResolvers<void>()
  297. const task = runPromptQueue({
  298. footer: ui.api,
  299. run: async (_input, signal) => {
  300. started.resolve(signal)
  301. await active.promise
  302. },
  303. })
  304. ui.submit("one", mode)
  305. const signal = await started.promise
  306. ui.api.close()
  307. await task
  308. expect(signal.aborted).toBe(aborted)
  309. active.resolve()
  310. })
  311. test("propagates run errors", async () => {
  312. const ui = createFooterApiFixture()
  313. const task = runPromptQueue({
  314. footer: ui.api,
  315. run: async () => {
  316. throw new Error("boom")
  317. },
  318. })
  319. ui.submit("one")
  320. await expect(task).rejects.toThrow("boom")
  321. })
  322. })