utils.spec.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { get } from 'lodash'
  2. import { buildChatItemTree, getThreadMessages } from '../utils'
  3. import type { ChatItemInTree } from '../types'
  4. import branchedTestMessages from './branchedTestMessages.json'
  5. import legacyTestMessages from './legacyTestMessages.json'
  6. import mixedTestMessages from './mixedTestMessages.json'
  7. import multiRootNodesMessages from './multiRootNodesMessages.json'
  8. import multiRootNodesWithLegacyTestMessages from './multiRootNodesWithLegacyTestMessages.json'
  9. import realWorldMessages from './realWorldMessages.json'
  10. import partialMessages from './partialMessages.json'
  11. function visitNode(tree: ChatItemInTree | ChatItemInTree[], path: string): ChatItemInTree {
  12. return get(tree, path)
  13. }
  14. describe('build chat item tree and get thread messages', () => {
  15. const tree1 = buildChatItemTree(branchedTestMessages as ChatItemInTree[])
  16. it('should build chat item tree1', () => {
  17. const a1 = visitNode(tree1, '0.children.0')
  18. expect(a1.id).toBe('1')
  19. expect(a1.children).toHaveLength(2)
  20. const a2 = visitNode(a1, 'children.0.children.0')
  21. expect(a2.id).toBe('2')
  22. expect(a2.siblingIndex).toBe(0)
  23. const a3 = visitNode(a2, 'children.0.children.0')
  24. expect(a3.id).toBe('3')
  25. const a4 = visitNode(a1, 'children.1.children.0')
  26. expect(a4.id).toBe('4')
  27. expect(a4.siblingIndex).toBe(1)
  28. })
  29. it('should get thread messages from tree1, using the last message as the target', () => {
  30. const threadChatItems1_1 = getThreadMessages(tree1)
  31. expect(threadChatItems1_1).toHaveLength(4)
  32. const q1 = visitNode(threadChatItems1_1, '0')
  33. const a1 = visitNode(threadChatItems1_1, '1')
  34. const q4 = visitNode(threadChatItems1_1, '2')
  35. const a4 = visitNode(threadChatItems1_1, '3')
  36. expect(q1.id).toBe('question-1')
  37. expect(a1.id).toBe('1')
  38. expect(q4.id).toBe('question-4')
  39. expect(a4.id).toBe('4')
  40. expect(a4.siblingCount).toBe(2)
  41. expect(a4.siblingIndex).toBe(1)
  42. })
  43. it('should get thread messages from tree1, using the message with id 3 as the target', () => {
  44. const threadChatItems1_2 = getThreadMessages(tree1, '3')
  45. expect(threadChatItems1_2).toHaveLength(6)
  46. const q1 = visitNode(threadChatItems1_2, '0')
  47. const a1 = visitNode(threadChatItems1_2, '1')
  48. const q2 = visitNode(threadChatItems1_2, '2')
  49. const a2 = visitNode(threadChatItems1_2, '3')
  50. const q3 = visitNode(threadChatItems1_2, '4')
  51. const a3 = visitNode(threadChatItems1_2, '5')
  52. expect(q1.id).toBe('question-1')
  53. expect(a1.id).toBe('1')
  54. expect(q2.id).toBe('question-2')
  55. expect(a2.id).toBe('2')
  56. expect(q3.id).toBe('question-3')
  57. expect(a3.id).toBe('3')
  58. expect(a2.siblingCount).toBe(2)
  59. expect(a2.siblingIndex).toBe(0)
  60. })
  61. const tree2 = buildChatItemTree(legacyTestMessages as ChatItemInTree[])
  62. it('should work with legacy chat items', () => {
  63. expect(tree2).toHaveLength(1)
  64. const q1 = visitNode(tree2, '0')
  65. const a1 = visitNode(q1, 'children.0')
  66. const q2 = visitNode(a1, 'children.0')
  67. const a2 = visitNode(q2, 'children.0')
  68. const q3 = visitNode(a2, 'children.0')
  69. const a3 = visitNode(q3, 'children.0')
  70. const q4 = visitNode(a3, 'children.0')
  71. const a4 = visitNode(q4, 'children.0')
  72. expect(q1.id).toBe('question-1')
  73. expect(a1.id).toBe('1')
  74. expect(q2.id).toBe('question-2')
  75. expect(a2.id).toBe('2')
  76. expect(q3.id).toBe('question-3')
  77. expect(a3.id).toBe('3')
  78. expect(q4.id).toBe('question-4')
  79. expect(a4.id).toBe('4')
  80. })
  81. it('should get thread messages from tree2, using the last message as the target', () => {
  82. const threadMessages2 = getThreadMessages(tree2)
  83. expect(threadMessages2).toHaveLength(8)
  84. const q1 = visitNode(threadMessages2, '0')
  85. const a1 = visitNode(threadMessages2, '1')
  86. const q2 = visitNode(threadMessages2, '2')
  87. const a2 = visitNode(threadMessages2, '3')
  88. const q3 = visitNode(threadMessages2, '4')
  89. const a3 = visitNode(threadMessages2, '5')
  90. const q4 = visitNode(threadMessages2, '6')
  91. const a4 = visitNode(threadMessages2, '7')
  92. expect(q1.id).toBe('question-1')
  93. expect(a1.id).toBe('1')
  94. expect(q2.id).toBe('question-2')
  95. expect(a2.id).toBe('2')
  96. expect(q3.id).toBe('question-3')
  97. expect(a3.id).toBe('3')
  98. expect(q4.id).toBe('question-4')
  99. expect(a4.id).toBe('4')
  100. expect(a1.siblingCount).toBe(1)
  101. expect(a1.siblingIndex).toBe(0)
  102. expect(a2.siblingCount).toBe(1)
  103. expect(a2.siblingIndex).toBe(0)
  104. expect(a3.siblingCount).toBe(1)
  105. expect(a3.siblingIndex).toBe(0)
  106. expect(a4.siblingCount).toBe(1)
  107. expect(a4.siblingIndex).toBe(0)
  108. })
  109. const tree3 = buildChatItemTree(mixedTestMessages as ChatItemInTree[])
  110. it('should build mixed chat items tree', () => {
  111. expect(tree3).toHaveLength(1)
  112. const a1 = visitNode(tree3, '0.children.0')
  113. expect(a1.id).toBe('1')
  114. expect(a1.children).toHaveLength(2)
  115. const a2 = visitNode(a1, 'children.0.children.0')
  116. expect(a2.id).toBe('2')
  117. expect(a2.siblingIndex).toBe(0)
  118. const a3 = visitNode(a2, 'children.0.children.0')
  119. expect(a3.id).toBe('3')
  120. const a4 = visitNode(a1, 'children.1.children.0')
  121. expect(a4.id).toBe('4')
  122. expect(a4.siblingIndex).toBe(1)
  123. })
  124. it('should get thread messages from tree3, using the last message as the target', () => {
  125. const threadMessages3_1 = getThreadMessages(tree3)
  126. expect(threadMessages3_1).toHaveLength(4)
  127. const q1 = visitNode(threadMessages3_1, '0')
  128. const a1 = visitNode(threadMessages3_1, '1')
  129. const q4 = visitNode(threadMessages3_1, '2')
  130. const a4 = visitNode(threadMessages3_1, '3')
  131. expect(q1.id).toBe('question-1')
  132. expect(a1.id).toBe('1')
  133. expect(q4.id).toBe('question-4')
  134. expect(a4.id).toBe('4')
  135. expect(a4.siblingCount).toBe(2)
  136. expect(a4.siblingIndex).toBe(1)
  137. })
  138. it('should get thread messages from tree3, using the message with id 3 as the target', () => {
  139. const threadMessages3_2 = getThreadMessages(tree3, '3')
  140. expect(threadMessages3_2).toHaveLength(6)
  141. const q1 = visitNode(threadMessages3_2, '0')
  142. const a1 = visitNode(threadMessages3_2, '1')
  143. const q2 = visitNode(threadMessages3_2, '2')
  144. const a2 = visitNode(threadMessages3_2, '3')
  145. const q3 = visitNode(threadMessages3_2, '4')
  146. const a3 = visitNode(threadMessages3_2, '5')
  147. expect(q1.id).toBe('question-1')
  148. expect(a1.id).toBe('1')
  149. expect(q2.id).toBe('question-2')
  150. expect(a2.id).toBe('2')
  151. expect(q3.id).toBe('question-3')
  152. expect(a3.id).toBe('3')
  153. expect(a2.siblingCount).toBe(2)
  154. expect(a2.siblingIndex).toBe(0)
  155. })
  156. const tree4 = buildChatItemTree(multiRootNodesMessages as ChatItemInTree[])
  157. it('should build multi root nodes chat items tree', () => {
  158. expect(tree4).toHaveLength(2)
  159. const a5 = visitNode(tree4, '1.children.0')
  160. expect(a5.id).toBe('5')
  161. expect(a5.siblingIndex).toBe(1)
  162. })
  163. it('should get thread messages from tree4, using the last message as the target', () => {
  164. const threadMessages4 = getThreadMessages(tree4)
  165. expect(threadMessages4).toHaveLength(2)
  166. const a1 = visitNode(threadMessages4, '0.children.0')
  167. expect(a1.id).toBe('5')
  168. })
  169. it('should get thread messages from tree4, using the message with id 2 as the target', () => {
  170. const threadMessages4_1 = getThreadMessages(tree4, '2')
  171. expect(threadMessages4_1).toHaveLength(6)
  172. const a1 = visitNode(threadMessages4_1, '1')
  173. expect(a1.id).toBe('1')
  174. const a2 = visitNode(threadMessages4_1, '3')
  175. expect(a2.id).toBe('2')
  176. const a3 = visitNode(threadMessages4_1, '5')
  177. expect(a3.id).toBe('3')
  178. })
  179. const tree5 = buildChatItemTree(multiRootNodesWithLegacyTestMessages as ChatItemInTree[])
  180. it('should work with multi root nodes chat items with legacy chat items', () => {
  181. expect(tree5).toHaveLength(2)
  182. const q5 = visitNode(tree5, '1')
  183. expect(q5.id).toBe('question-5')
  184. expect(q5.parentMessageId).toBe(null)
  185. const a5 = visitNode(q5, 'children.0')
  186. expect(a5.id).toBe('5')
  187. expect(a5.children).toHaveLength(0)
  188. })
  189. it('should get thread messages from tree5, using the last message as the target', () => {
  190. const threadMessages5 = getThreadMessages(tree5)
  191. expect(threadMessages5).toHaveLength(2)
  192. const q5 = visitNode(threadMessages5, '0')
  193. const a5 = visitNode(threadMessages5, '1')
  194. expect(q5.id).toBe('question-5')
  195. expect(a5.id).toBe('5')
  196. expect(a5.siblingCount).toBe(2)
  197. expect(a5.siblingIndex).toBe(1)
  198. })
  199. const tree6 = buildChatItemTree(realWorldMessages as ChatItemInTree[])
  200. it('should work with real world messages', () => {
  201. expect(tree6).toMatchSnapshot()
  202. })
  203. it ('should get thread messages from tree6, using the last message as target', () => {
  204. const threadMessages6_1 = getThreadMessages(tree6)
  205. expect(threadMessages6_1).toMatchSnapshot()
  206. })
  207. it ('should get thread messages from tree6, using specified message as target', () => {
  208. const threadMessages6_2 = getThreadMessages(tree6, 'ff4c2b43-48a5-47ad-9dc5-08b34ddba61b')
  209. expect(threadMessages6_2).toMatchSnapshot()
  210. })
  211. const partialMessages1 = (realWorldMessages as ChatItemInTree[]).slice(-10)
  212. const tree7 = buildChatItemTree(partialMessages1)
  213. it('should work with partial messages 1', () => {
  214. expect(tree7).toMatchSnapshot()
  215. })
  216. const partialMessages2 = (partialMessages as ChatItemInTree[])
  217. const tree8 = buildChatItemTree(partialMessages2)
  218. it('should work with partial messages 2', () => {
  219. expect(tree8).toMatchSnapshot()
  220. })
  221. })