core-menu.client.spec.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { describe, expect, it } from 'vitest'
  2. import type { MenuState, TriggerHit } from '../src/core/contract.ts'
  3. import { exactMatch, MENU_CLOSED, menuReduce, seedGroups } from '../src/core/menu.ts'
  4. const hit = (query = ''): TriggerHit => ({
  5. trigger: '/',
  6. query,
  7. quoted: false,
  8. position: 'leading',
  9. span: { start: 0, end: 1 + query.length, draftRev: 1 },
  10. })
  11. function open(sources: readonly string[], h: TriggerHit = hit()): MenuState {
  12. return menuReduce(seedGroups(MENU_CLOSED, sources.map(name => ({ name }))), { type: 'hit', hit: h })
  13. }
  14. const item = (name: string) => ({ name })
  15. describe('menuReduce hit', () => {
  16. it('opens a new generation with all groups pending', () => {
  17. const s = open(['command', 'skill'])
  18. expect(s.open).toBe(true)
  19. expect(s.generation).toBe(1)
  20. expect(s.groups).toEqual([
  21. { source: 'command', status: 'pending', items: [] },
  22. { source: 'skill', status: 'pending', items: [] },
  23. ])
  24. expect(s.highlight).toBeNull()
  25. })
  26. it('re-hit resets ready groups to pending under a bumped generation', () => {
  27. let s = open(['command'])
  28. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal')] })
  29. s = menuReduce(s, { type: 'hit', hit: hit('g') })
  30. expect(s.generation).toBe(2)
  31. expect(s.groups).toEqual([{ source: 'command', status: 'pending', items: [] }])
  32. expect(s.highlight).toBeNull()
  33. })
  34. it('preserves a hidden group title through re-hit and settlement', () => {
  35. let s = menuReduce(seedGroups(MENU_CLOSED, [{ name: 'reference', showGroupTitle: false }]), { type: 'hit', hit: hit() })
  36. expect(s.groups[0]).toMatchObject({ source: 'reference', showGroupTitle: false, status: 'pending' })
  37. s = menuReduce(s, { type: 'hit', hit: hit('r') })
  38. s = menuReduce(s, { type: 'source-settled', generation: 2, source: 'reference', items: [item('README.md')] })
  39. expect(s.groups[0]).toMatchObject({ source: 'reference', showGroupTitle: false, status: 'ready' })
  40. })
  41. it('null hit closes; closing an already-closed state is a no-op reference', () => {
  42. const s = open(['command'])
  43. const c = menuReduce(s, { type: 'hit', hit: null })
  44. expect(c.open).toBe(false)
  45. expect(c.groups).toEqual([])
  46. expect(menuReduce(c, { type: 'hit', hit: null })).toBe(c)
  47. })
  48. })
  49. describe('menuReduce source-settled', () => {
  50. it('marks the group ready and highlights the first item', () => {
  51. let s = open(['command', 'skill'])
  52. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  53. expect(s.groups[1]).toEqual({ source: 'skill', status: 'ready', items: [item('commit')] })
  54. expect(s.groups[0]!.status).toBe('pending')
  55. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  56. })
  57. it('keeps an existing valid highlight when a later group settles', () => {
  58. let s = open(['command', 'skill'])
  59. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  60. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal')] })
  61. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  62. })
  63. it('drops settlements from a stale generation by reference', () => {
  64. let s = open(['command'])
  65. s = menuReduce(s, { type: 'hit', hit: hit('g') }) // generation 2
  66. const next = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal')] })
  67. expect(next).toBe(s)
  68. })
  69. it('drops settlements while closed and for unknown sources by reference', () => {
  70. const closed = menuReduce(open(['command']), { type: 'close' })
  71. expect(menuReduce(closed, { type: 'source-settled', generation: 1, source: 'command', items: [] })).toBe(closed)
  72. const s = open(['command'])
  73. expect(menuReduce(s, { type: 'source-settled', generation: 1, source: 'ghost', items: [] })).toBe(s)
  74. })
  75. it('treats omitted items as empty', () => {
  76. let s = open(['command', 'skill'])
  77. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command' })
  78. expect(s.groups[0]).toEqual({ source: 'command', status: 'ready', items: [] })
  79. expect(s.open).toBe(true) // skill still pending
  80. })
  81. it('auto-closes when every group settles ready and empty', () => {
  82. let s = open(['command', 'skill'])
  83. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [] })
  84. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [] })
  85. expect(s.open).toBe(false)
  86. expect(s.groups).toEqual([])
  87. })
  88. it('stays open when one group is empty but another has items', () => {
  89. let s = open(['command', 'skill'])
  90. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [] })
  91. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  92. expect(s.open).toBe(true)
  93. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  94. })
  95. })
  96. describe('menuReduce source-failed', () => {
  97. it('silently removes the failed group', () => {
  98. let s = open(['command', 'skill'])
  99. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  100. s = menuReduce(s, { type: 'source-failed', generation: 1, source: 'command' })
  101. expect(s.groups.map(g => g.source)).toEqual(['skill'])
  102. expect(s.open).toBe(true)
  103. })
  104. it('closes when the last group fails', () => {
  105. let s = open(['command'])
  106. s = menuReduce(s, { type: 'source-failed', generation: 1, source: 'command' })
  107. expect(s.open).toBe(false)
  108. })
  109. it('closes when the surviving groups are all ready and empty', () => {
  110. let s = open(['command', 'skill'])
  111. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [] })
  112. s = menuReduce(s, { type: 'source-failed', generation: 1, source: 'command' })
  113. expect(s.open).toBe(false)
  114. })
  115. it('moves the highlight off the failed group', () => {
  116. let s = open(['command', 'skill'])
  117. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal')] })
  118. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  119. expect(s.highlight).toEqual({ source: 'command', index: 0 })
  120. s = menuReduce(s, { type: 'source-failed', generation: 1, source: 'command' })
  121. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  122. })
  123. it('drops stale-generation and unknown-source failures by reference', () => {
  124. const s = open(['command'])
  125. expect(menuReduce(s, { type: 'source-failed', generation: 0, source: 'command' })).toBe(s)
  126. expect(menuReduce(s, { type: 'source-failed', generation: 1, source: 'ghost' })).toBe(s)
  127. })
  128. })
  129. describe('menuReduce move', () => {
  130. /** Two ready groups: command [goal, model], skill [commit]. */
  131. function ready(): MenuState {
  132. let s = open(['command', 'skill'])
  133. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal'), item('model')] })
  134. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  135. return s
  136. }
  137. it('cycles forward across groups and wraps', () => {
  138. let s = ready()
  139. s = menuReduce(s, { type: 'move', dir: 1 })
  140. expect(s.highlight).toEqual({ source: 'command', index: 1 })
  141. s = menuReduce(s, { type: 'move', dir: 1 })
  142. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  143. s = menuReduce(s, { type: 'move', dir: 1 })
  144. expect(s.highlight).toEqual({ source: 'command', index: 0 })
  145. })
  146. it('cycles backward and wraps to the last item', () => {
  147. let s = ready()
  148. s = menuReduce(s, { type: 'move', dir: -1 })
  149. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  150. })
  151. it('skips pending groups', () => {
  152. let s = open(['command', 'skill'])
  153. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  154. s = menuReduce(s, { type: 'move', dir: 1 })
  155. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  156. })
  157. it('enters from null highlight at either end', () => {
  158. const base = { ...ready(), highlight: null }
  159. expect(menuReduce(base, { type: 'move', dir: 1 }).highlight).toEqual({ source: 'command', index: 0 })
  160. expect(menuReduce(base, { type: 'move', dir: -1 }).highlight).toEqual({ source: 'skill', index: 0 })
  161. })
  162. it('is a no-op reference when closed, without positions, or single-item', () => {
  163. const closed = menuReduce(ready(), { type: 'close' })
  164. expect(menuReduce(closed, { type: 'move', dir: 1 })).toBe(closed)
  165. const pending = open(['command'])
  166. expect(menuReduce(pending, { type: 'move', dir: 1 })).toBe(pending)
  167. let single = open(['command'])
  168. single = menuReduce(single, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal')] })
  169. expect(menuReduce(single, { type: 'move', dir: 1 })).toBe(single)
  170. })
  171. })
  172. describe('menuReduce close', () => {
  173. it('clears everything but keeps the generation for stale-drop', () => {
  174. let s = open(['command'])
  175. s = menuReduce(s, { type: 'close' })
  176. expect(s).toMatchObject({ open: false, hit: null, groups: [], highlight: null, generation: 1 })
  177. })
  178. })
  179. describe('exactMatch', () => {
  180. const groups: MenuState['groups'] = [
  181. { source: 'command', status: 'ready', items: [item('goal'), item('model')] },
  182. { source: 'skill', status: 'pending', items: [] },
  183. ]
  184. it('finds an exact name in a ready group', () => {
  185. expect(exactMatch(groups, 'command', 'model')).toEqual(item('model'))
  186. })
  187. it('returns null on name miss, non-ready group, and unknown source', () => {
  188. expect(exactMatch(groups, 'command', 'goa')).toBeNull()
  189. expect(exactMatch(groups, 'skill', 'commit')).toBeNull()
  190. expect(exactMatch(groups, 'ghost', 'goal')).toBeNull()
  191. })
  192. })