core-menu.client.spec.ts 9.3 KB

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