core-menu.client.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /** Two ready groups: command [goal, model], skill [commit]. */
  16. function ready(): MenuState {
  17. let s = open(['command', 'skill'])
  18. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal'), item('model')] })
  19. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  20. return s
  21. }
  22. describe('menuReduce hit', () => {
  23. it('opens a new generation with all groups pending', () => {
  24. const s = open(['command', 'skill'])
  25. expect(s.open).toBe(true)
  26. expect(s.generation).toBe(1)
  27. expect(s.groups).toEqual([
  28. { source: 'command', status: 'pending', items: [] },
  29. { source: 'skill', status: 'pending', items: [] },
  30. ])
  31. expect(s.highlight).toBeNull()
  32. })
  33. it('re-hit resets ready groups to pending under a bumped generation', () => {
  34. let s = open(['command'])
  35. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal')] })
  36. s = menuReduce(s, { type: 'hit', hit: hit('g') })
  37. expect(s.generation).toBe(2)
  38. expect(s.groups).toEqual([{ source: 'command', status: 'pending', items: [] }])
  39. expect(s.highlight).toBeNull()
  40. })
  41. it('preserves a hidden group title through re-hit and settlement', () => {
  42. let s = menuReduce(seedGroups(MENU_CLOSED, [{ name: 'reference', showGroupTitle: false }]), { type: 'hit', hit: hit() })
  43. expect(s.groups[0]).toMatchObject({ source: 'reference', showGroupTitle: false, status: 'pending' })
  44. s = menuReduce(s, { type: 'hit', hit: hit('r') })
  45. s = menuReduce(s, { type: 'source-settled', generation: 2, source: 'reference', items: [item('README.md')] })
  46. expect(s.groups[0]).toMatchObject({ source: 'reference', showGroupTitle: false, status: 'ready' })
  47. })
  48. it('null hit closes; closing an already-closed state is a no-op reference', () => {
  49. const s = open(['command'])
  50. const c = menuReduce(s, { type: 'hit', hit: null })
  51. expect(c.open).toBe(false)
  52. expect(c.groups).toEqual([])
  53. expect(menuReduce(c, { type: 'hit', hit: null })).toBe(c)
  54. })
  55. })
  56. describe('menuReduce source-settled', () => {
  57. it('marks the group ready and highlights the first item', () => {
  58. let s = open(['command', 'skill'])
  59. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  60. expect(s.groups[1]).toEqual({ source: 'skill', status: 'ready', items: [item('commit')] })
  61. expect(s.groups[0]!.status).toBe('pending')
  62. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  63. })
  64. it('keeps an existing valid highlight when a later group settles', () => {
  65. let s = open(['command', 'skill'])
  66. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  67. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal')] })
  68. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  69. })
  70. it('drops settlements from a stale generation by reference', () => {
  71. let s = open(['command'])
  72. s = menuReduce(s, { type: 'hit', hit: hit('g') }) // generation 2
  73. const next = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal')] })
  74. expect(next).toBe(s)
  75. })
  76. it('drops settlements while closed and for unknown sources by reference', () => {
  77. const closed = menuReduce(open(['command']), { type: 'close' })
  78. expect(menuReduce(closed, { type: 'source-settled', generation: 1, source: 'command', items: [] })).toBe(closed)
  79. const s = open(['command'])
  80. expect(menuReduce(s, { type: 'source-settled', generation: 1, source: 'ghost', items: [] })).toBe(s)
  81. })
  82. it('treats omitted items as empty', () => {
  83. let s = open(['command', 'skill'])
  84. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command' })
  85. expect(s.groups[0]).toEqual({ source: 'command', status: 'ready', items: [] })
  86. expect(s.open).toBe(true) // skill still pending
  87. })
  88. it('auto-closes when every group settles ready and empty', () => {
  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: [] })
  92. expect(s.open).toBe(false)
  93. expect(s.groups).toEqual([])
  94. })
  95. it('stays open when one group is empty but another has items', () => {
  96. let s = open(['command', 'skill'])
  97. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [] })
  98. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  99. expect(s.open).toBe(true)
  100. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  101. })
  102. })
  103. describe('menuReduce source-failed', () => {
  104. it('silently removes the failed group', () => {
  105. let s = open(['command', 'skill'])
  106. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  107. s = menuReduce(s, { type: 'source-failed', generation: 1, source: 'command' })
  108. expect(s.groups.map(g => g.source)).toEqual(['skill'])
  109. expect(s.open).toBe(true)
  110. })
  111. it('closes when the last group fails', () => {
  112. let s = open(['command'])
  113. s = menuReduce(s, { type: 'source-failed', generation: 1, source: 'command' })
  114. expect(s.open).toBe(false)
  115. })
  116. it('closes when the surviving groups are all ready and empty', () => {
  117. let s = open(['command', 'skill'])
  118. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [] })
  119. s = menuReduce(s, { type: 'source-failed', generation: 1, source: 'command' })
  120. expect(s.open).toBe(false)
  121. })
  122. it('moves the highlight off the failed group', () => {
  123. let s = open(['command', 'skill'])
  124. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal')] })
  125. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'skill', items: [item('commit')] })
  126. expect(s.highlight).toEqual({ source: 'command', index: 0 })
  127. s = menuReduce(s, { type: 'source-failed', generation: 1, source: 'command' })
  128. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  129. })
  130. it('drops stale-generation and unknown-source failures by reference', () => {
  131. const s = open(['command'])
  132. expect(menuReduce(s, { type: 'source-failed', generation: 0, source: 'command' })).toBe(s)
  133. expect(menuReduce(s, { type: 'source-failed', generation: 1, source: 'ghost' })).toBe(s)
  134. })
  135. })
  136. describe('menuReduce move', () => {
  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 hover', () => {
  173. it('parks the highlight on the hovered ready item', () => {
  174. const s = menuReduce(ready(), { type: 'hover', source: 'skill', index: 0 })
  175. expect(s.highlight).toEqual({ source: 'skill', index: 0 })
  176. })
  177. it('is a no-op reference when closed, on invalid targets, and on the current highlight', () => {
  178. const closed = menuReduce(ready(), { type: 'close' })
  179. expect(menuReduce(closed, { type: 'hover', source: 'command', index: 0 })).toBe(closed)
  180. const s = ready()
  181. expect(menuReduce(s, { type: 'hover', source: 'ghost', index: 0 })).toBe(s)
  182. expect(menuReduce(s, { type: 'hover', source: 'command', index: 5 })).toBe(s)
  183. expect(menuReduce(s, { type: 'hover', source: 'command', index: 0 })).toBe(s)
  184. })
  185. it('ignores a hover into a still-pending group', () => {
  186. let s = open(['command', 'skill'])
  187. s = menuReduce(s, { type: 'source-settled', generation: 1, source: 'command', items: [item('goal')] })
  188. expect(menuReduce(s, { type: 'hover', source: 'skill', index: 0 })).toBe(s)
  189. })
  190. })
  191. describe('menuReduce close', () => {
  192. it('clears everything but keeps the generation for stale-drop', () => {
  193. let s = open(['command'])
  194. s = menuReduce(s, { type: 'close' })
  195. expect(s).toMatchObject({ open: false, hit: null, groups: [], highlight: null, generation: 1 })
  196. })
  197. })
  198. describe('exactMatch', () => {
  199. const groups: MenuState['groups'] = [
  200. { source: 'command', status: 'ready', items: [item('goal'), item('model')] },
  201. { source: 'skill', status: 'pending', items: [] },
  202. ]
  203. it('finds an exact name in a ready group', () => {
  204. expect(exactMatch(groups, 'command', 'model')).toEqual(item('model'))
  205. })
  206. it('returns null on name miss, non-ready group, and unknown source', () => {
  207. expect(exactMatch(groups, 'command', 'goa')).toBeNull()
  208. expect(exactMatch(groups, 'skill', 'commit')).toBeNull()
  209. expect(exactMatch(groups, 'ghost', 'goal')).toBeNull()
  210. })
  211. })