core-menu.client.spec.ts 9.9 KB

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