query.spec.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { describe, expect, it } from 'vitest'
  2. import { SessionId } from '@deepseek-ai/dsh-session'
  3. import { SessionSearchCursor, type SessionQueryErrorCode } from '@deepseek-ai/dsh-session-query'
  4. import {
  5. buildEventWhere,
  6. buildSessionWhere,
  7. FTS_HIGHLIGHT_END,
  8. FTS_HIGHLIGHT_START,
  9. makeSnippet,
  10. normalizeEventRequest,
  11. normalizeSessionRequest,
  12. quoteFtsData,
  13. requestFingerprint,
  14. type NormalizedEventRequest,
  15. type NormalizedSessionRequest,
  16. } from '../src/query.ts'
  17. const limits = { defaultLimit: 2, maxLimit: 3 }
  18. function expectCode(code: SessionQueryErrorCode): Error {
  19. return expect.objectContaining({ code }) as Error
  20. }
  21. describe('SQLite search request normalization', () => {
  22. it('normalizes both scopes, defaults arrays and limits, and preserves cursors', () => {
  23. expect(normalizeSessionRequest({ query: ' alpha\n beta ' }, limits)).toEqual({
  24. query: 'alpha beta',
  25. sessionFilters: [],
  26. eventFilters: [],
  27. limit: 2,
  28. })
  29. expect(normalizeSessionRequest({
  30. query: 'needle',
  31. sessionFilters: [{ kind: 'availability', values: ['live'] }],
  32. eventFilters: [{ kind: 'surface', values: ['current'] }],
  33. limit: 3,
  34. cursor: SessionSearchCursor('next'),
  35. }, limits)).toEqual({
  36. query: 'needle',
  37. sessionFilters: [{ kind: 'availability', values: ['live'] }],
  38. eventFilters: [{ kind: 'surface', values: ['current'] }],
  39. limit: 3,
  40. cursor: SessionSearchCursor('next'),
  41. })
  42. expect(normalizeEventRequest({ sessionId: SessionId('s'), query: 'needle' }, limits)).toEqual({
  43. sessionId: SessionId('s'),
  44. query: 'needle',
  45. filters: [],
  46. limit: 2,
  47. })
  48. expect(normalizeEventRequest({
  49. sessionId: SessionId('s'),
  50. query: 'needle',
  51. filters: [{ kind: 'seq', from: 1 }],
  52. cursor: SessionSearchCursor('next'),
  53. }, limits)).toEqual({
  54. sessionId: SessionId('s'),
  55. query: 'needle',
  56. filters: [{ kind: 'seq', from: 1 }],
  57. limit: 2,
  58. cursor: SessionSearchCursor('next'),
  59. })
  60. })
  61. it('rejects non-text, blank, non-integer, non-positive, and oversized requests', () => {
  62. expect(() => normalizeSessionRequest({ query: 1 as never }, limits))
  63. .toThrow(expectCode('SESSION_QUERY_INVALID_QUERY'))
  64. expect(() => normalizeSessionRequest({ query: ' \n ' }, limits))
  65. .toThrow(expectCode('SESSION_QUERY_INVALID_QUERY'))
  66. expect(() => normalizeSessionRequest({ query: 'bad\0query' }, limits))
  67. .toThrow(expectCode('SESSION_QUERY_INVALID_QUERY'))
  68. expect(() => normalizeEventRequest({ sessionId: 1 as never, query: 'x' }, limits))
  69. .toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  70. expect(() => normalizeEventRequest({
  71. sessionId: SessionId('s'),
  72. query: 'x',
  73. cursor: 1 as never,
  74. }, limits)).toThrow(expectCode('SESSION_QUERY_INVALID_CURSOR'))
  75. expect(() => normalizeSessionRequest({
  76. query: 'x',
  77. eventFilters: [{ kind: 'text', text: 'x' } as never],
  78. }, limits)).toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  79. expect(() => normalizeSessionRequest({
  80. query: 'x',
  81. eventFilters: [{} as never],
  82. }, limits)).toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  83. for (const limit of [1.5, 0, 4]) {
  84. expect(() => normalizeEventRequest({ sessionId: SessionId('s'), query: 'x', limit }, limits))
  85. .toThrow(expectCode('SESSION_QUERY_INVALID_LIMIT'))
  86. }
  87. })
  88. it('materializes owned filter values during normalization', () => {
  89. const values = ['live'] as Array<'live' | 'persisted'>
  90. const filter = { kind: 'availability' as const, values }
  91. const request = { query: 'needle', sessionFilters: [filter] }
  92. const normalized = normalizeSessionRequest(request, limits)
  93. values[0] = 'persisted'
  94. request.sessionFilters.push({ kind: 'availability', values: ['persisted'] })
  95. expect(normalized.sessionFilters).toEqual([{ kind: 'availability', values: ['live'] }])
  96. })
  97. })
  98. describe('SQLite search predicate compilation', () => {
  99. it('compiles all logical-session clauses including empty and nullable values', () => {
  100. expect(buildSessionWhere([])).toEqual({ sql: '', params: [] })
  101. expect(buildSessionWhere([{ kind: 'id', values: [] }])).toEqual({ sql: '0', params: [] })
  102. expect(buildSessionWhere([{ kind: 'id', values: [SessionId('a'), SessionId('b')] }])).toEqual({
  103. sql: 'session_id IN (?, ?)',
  104. params: [SessionId('a'), SessionId('b')],
  105. })
  106. expect(buildSessionWhere([{ kind: 'cwd', values: [] }])).toEqual({ sql: '0', params: [] })
  107. expect(buildSessionWhere([{ kind: 'cwd', values: [null] }])).toEqual({
  108. sql: '(cwd IS NULL)',
  109. params: [],
  110. })
  111. expect(buildSessionWhere([{ kind: 'cwd', values: ['/a'] }])).toEqual({
  112. sql: '(cwd IN (?))',
  113. params: ['/a'],
  114. })
  115. expect(buildSessionWhere([{ kind: 'parent', values: [SessionId('p'), null] }])).toEqual({
  116. sql: '(parent_session IN (?) OR parent_session IS NULL)',
  117. params: [SessionId('p')],
  118. })
  119. expect(buildSessionWhere([
  120. { kind: 'created-at', from: 1, to: 2 },
  121. { kind: 'availability', values: [] },
  122. { kind: 'availability', values: ['live', 'live'] },
  123. { kind: 'availability', values: ['live', 'persisted'] },
  124. ])).toEqual({
  125. sql: 'CAST(created_at AS INTEGER) >= ? AND CAST(created_at AS INTEGER) <= ? AND 0 AND live = 1',
  126. params: [1, 2],
  127. })
  128. expect(buildSessionWhere([{ kind: 'created-at' }])).toEqual({ sql: '', params: [] })
  129. })
  130. it('compiles every event clause and empty lists', () => {
  131. expect(buildEventWhere([
  132. { kind: 'seq', from: 1 },
  133. { kind: 'time', to: 9 },
  134. { kind: 'type', values: ['user/message'] },
  135. { kind: 'surface', values: ['current', 'log-only'] },
  136. ])).toEqual({
  137. sql: 'CAST(seq AS INTEGER) >= ? AND CAST(time AS INTEGER) <= ? AND type IN (?) AND surface IN (?, ?)',
  138. params: [1, 9, 'user/message', 'current', 'log-only'],
  139. })
  140. expect(buildEventWhere([
  141. { kind: 'type', values: [] },
  142. { kind: 'surface', values: [] },
  143. ])).toEqual({ sql: '0 AND 0', params: [] })
  144. })
  145. it('rejects runtime-unknown filter discriminants in both SQL builders', () => {
  146. expect(() => buildSessionWhere([{ kind: 'future' } as never]))
  147. .toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  148. expect(() => buildEventWhere([{ kind: 'future' } as never]))
  149. .toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  150. expect(() => buildSessionWhere([{ kind: 'availability', values: ['future'] } as never]))
  151. .toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  152. expect(() => buildSessionWhere([{} as never]))
  153. .toThrow(expectCode('SESSION_QUERY_INVALID_FILTER'))
  154. })
  155. })
  156. describe('SQLite query identity and presentation', () => {
  157. it('quotes all caller MATCH syntax as data', () => {
  158. expect(quoteFtsData('say "needle" OR *')).toBe('"say ""needle"" OR *"')
  159. })
  160. it('canonicalizes request and filter ordering in both scopes', () => {
  161. const sessionA: NormalizedSessionRequest = {
  162. query: 'needle',
  163. limit: 2,
  164. sessionFilters: [
  165. { kind: 'cwd', values: ['/b', '/a'] },
  166. { kind: 'parent', values: [null, SessionId('p')] },
  167. { kind: 'id', values: [SessionId('same'), SessionId('same')] },
  168. { kind: 'created-at', from: 1 },
  169. ],
  170. eventFilters: [{ kind: 'time', to: 9 }],
  171. }
  172. const sessionB: NormalizedSessionRequest = {
  173. query: 'needle',
  174. limit: 2,
  175. sessionFilters: [
  176. { kind: 'created-at', from: 1 },
  177. { kind: 'id', values: [SessionId('same'), SessionId('same')] },
  178. { kind: 'parent', values: [SessionId('p'), null] },
  179. { kind: 'cwd', values: ['/a', '/b'] },
  180. ],
  181. eventFilters: [{ kind: 'time', to: 9 }],
  182. }
  183. expect(requestFingerprint(sessionA)).toBe(requestFingerprint(sessionB))
  184. const eventA: NormalizedEventRequest = {
  185. sessionId: SessionId('s'),
  186. query: 'needle',
  187. limit: 2,
  188. filters: [{ kind: 'seq' }, { kind: 'surface', values: ['shadowed', 'current'] }],
  189. }
  190. const eventB: NormalizedEventRequest = {
  191. sessionId: SessionId('s'),
  192. query: 'needle',
  193. limit: 2,
  194. filters: [{ kind: 'surface', values: ['current', 'shadowed'] }, { kind: 'seq' }],
  195. }
  196. expect(requestFingerprint(eventA)).toBe(requestFingerprint(eventB))
  197. expect(requestFingerprint(eventA)).not.toBe(requestFingerprint({ ...eventB, sessionId: SessionId('other') }))
  198. })
  199. it('normalizes, bounds, and positions snippets by Unicode code point', () => {
  200. expect(makeSnippet(' short\ntext ', 20)).toBe('short text')
  201. expect(makeSnippet(`abcde${FTS_HIGHLIGHT_START}f${FTS_HIGHLIGHT_END}`, 1)).toBe('…')
  202. expect(makeSnippet('abcdefghij', 5)).toBe('abcd…')
  203. expect(makeSnippet(`ab${FTS_HIGHLIGHT_START}c${FTS_HIGHLIGHT_END}defghij`, 5)).toBe('…bcd…')
  204. expect(makeSnippet(`abcde${FTS_HIGHLIGHT_START}f${FTS_HIGHLIGHT_END}`, 2)).toBe('a…')
  205. expect(makeSnippet(`abcde${FTS_HIGHLIGHT_START}f${FTS_HIGHLIGHT_END}`, 5)).toBe('…cdef')
  206. expect(makeSnippet(` x—${FTS_HIGHLIGHT_START}café${FTS_HIGHLIGHT_END}\n y `, 20))
  207. .toBe('x—café y')
  208. })
  209. })