inbox.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { describe, expect, it } from 'vitest'
  2. import { Inbox } from '../src/inbox.ts'
  3. function resolverPair() {
  4. let r!: () => void
  5. const p = new Promise<void>((resolve) => { r = resolve })
  6. return { promise: p, resolve: r }
  7. }
  8. describe('Inbox', () => {
  9. it('dequeues one queued message at a time in FIFO order', () => {
  10. const inbox = new Inbox()
  11. inbox.enqueue({ content: [{ type: 'text', text: 'first' }], source: { kind: 'user' } })
  12. inbox.enqueue({ content: [{ type: 'text', text: 'second' }], source: { kind: 'user' } })
  13. expect(inbox.hasQueued).toBe(true)
  14. expect(inbox.dequeueQueued()?.content[0]).toMatchObject({ text: 'first' })
  15. expect(inbox.hasQueued).toBe(true)
  16. expect(inbox.dequeueQueued()?.content[0]).toMatchObject({ text: 'second' })
  17. expect(inbox.hasQueued).toBe(false)
  18. expect(inbox.dequeueQueued()).toBeUndefined()
  19. })
  20. it('pushes and drains steering messages separately from queued', () => {
  21. const inbox = new Inbox()
  22. inbox.steer({ content: [{ type: 'text', text: 'steer' }], source: { kind: 'user' } })
  23. expect(inbox.hasQueued).toBe(false)
  24. expect(inbox.hasSteering).toBe(true)
  25. const steering = inbox.drainSteering()
  26. expect(steering).toHaveLength(1)
  27. expect(inbox.hasSteering).toBe(false)
  28. })
  29. it('waitForQueued returns immediately when a queued message is already present', async () => {
  30. const inbox = new Inbox()
  31. inbox.enqueue({ content: [{ type: 'text', text: 'ready' }], source: { kind: 'user' } })
  32. const started = Date.now()
  33. await inbox.waitForQueued(new Promise(() => {})) // never-resolving cancel
  34. expect(Date.now() - started).toBeLessThan(50)
  35. })
  36. it('waitForQueued resolves when a message is enqueued', async () => {
  37. const inbox = new Inbox()
  38. const waiter = inbox.waitForQueued(new Promise(() => {})) // never-resolving cancel
  39. // enqueue after starting the wait
  40. setTimeout(() => { inbox.enqueue({ content: [{ type: 'text', text: 'wake' }], source: { kind: 'user' } }) }, 5)
  41. await waiter
  42. })
  43. it('waitForQueued resolves when the cancel promise resolves', async () => {
  44. const inbox = new Inbox()
  45. const { promise, resolve } = resolverPair()
  46. const waiter = inbox.waitForQueued(promise)
  47. resolve()
  48. await waiter
  49. })
  50. it('waitForQueued overwrites the previous wakeup callback (only the latest waiter is notified)', async () => {
  51. const inbox = new Inbox()
  52. const { promise: p1, resolve: r1 } = resolverPair()
  53. void inbox.waitForQueued(new Promise(() => {})) // first call, never resolved
  54. void inbox.waitForQueued(p1) // second call overwrites wakeup
  55. // Cancelling the latest waiter clears the shared callback; enqueue must neither
  56. // wake the stale waiter nor fail on the cleared callback.
  57. r1()
  58. await p1
  59. inbox.enqueue({ content: [{ type: 'text', text: 'hey' }], source: { kind: 'user' } })
  60. })
  61. it('clears wakeup in finally handler when enqueue resolves', async () => {
  62. const inbox = new Inbox()
  63. void inbox.waitForQueued(new Promise(() => {})) // never-resolving cancel
  64. // The wakeup is set. Now trigger it via enqueue → wakeup() calls resolve,
  65. // promise resolves, finally clears wakeup because wakeup === resolve.
  66. inbox.enqueue({ content: [{ type: 'text', text: 'wake' }], source: { kind: 'user' } })
  67. // No explicit await needed — enqueue is synchronous, and the microtask
  68. // (finally) runs. The key coverage hit is finally with wakeup === resolve.
  69. })
  70. it('finally handler does not clear wakeup when a different waiter overwrote it', async () => {
  71. // A stale waiter's finally must not clear the replacement waiter.
  72. const inbox = new Inbox()
  73. const { promise: c1, resolve: r1 } = resolverPair()
  74. void inbox.waitForQueued(c1) // wakeup = resolve1, c1.then(resolve1)
  75. void inbox.waitForQueued(new Promise(() => {})) // wakeup = resolve2, cancel never resolves
  76. r1()
  77. await c1
  78. // The replacement remains registered and is resolved by enqueue.
  79. inbox.enqueue({ content: [{ type: 'text', text: 'hey' }], source: { kind: 'user' } })
  80. })
  81. })