getReplayEvent.spec.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import {BreadcrumbLevelType, BreadcrumbType, Crumb} from 'sentry/types/breadcrumbs';
  2. import {
  3. getNextReplayEvent,
  4. getPrevReplayEvent,
  5. } from 'sentry/utils/replays/getReplayEvent';
  6. const START_TIMESTAMP_SEC = 1651693622.951;
  7. const CURRENT_TIME_MS = 15000;
  8. function createCrumbs(): Crumb[] {
  9. return [
  10. {
  11. color: 'gray300',
  12. data: {url: 'https://dev.getsentry.net:7999/organizations/sentry/performance/'},
  13. description: 'Default',
  14. id: 0,
  15. level: BreadcrumbLevelType.INFO,
  16. message: 'Start recording',
  17. timestamp: '2022-05-11T22:41:32.002Z',
  18. type: BreadcrumbType.INIT,
  19. },
  20. {
  21. category: 'ui.click',
  22. color: 'purple300',
  23. data: undefined,
  24. description: 'User Action',
  25. event_id: null,
  26. id: 3,
  27. level: BreadcrumbLevelType.INFO,
  28. message: 'div.App > section.padding-b-2 > div.makeStyles-search-input-2 > input',
  29. timestamp: '2022-05-04T19:47:08.085000Z',
  30. type: BreadcrumbType.UI,
  31. },
  32. {
  33. category: 'ui.input',
  34. color: 'purple300',
  35. data: undefined,
  36. description: 'User Action',
  37. event_id: null,
  38. id: 4,
  39. level: BreadcrumbLevelType.INFO,
  40. message: 'div.App > section.padding-b-2 > div.makeStyles-search-input-2 > input',
  41. timestamp: '2022-05-04T19:47:11.086000Z',
  42. type: BreadcrumbType.UI,
  43. },
  44. {
  45. category: 'ui.click',
  46. color: 'purple300',
  47. data: undefined,
  48. description: 'User Action',
  49. event_id: null,
  50. id: 20,
  51. level: BreadcrumbLevelType.INFO,
  52. message: 'div.App > section.padding-b-2 > div.makeStyles-search-input-2 > input',
  53. timestamp: '2022-05-04T19:47:52.915000Z',
  54. type: BreadcrumbType.UI,
  55. },
  56. {
  57. category: 'navigation',
  58. color: 'green300',
  59. data: {
  60. from: '/organizations/sentry/user-feedback/?project=6380506',
  61. to: '/organizations/sentry/issues/',
  62. },
  63. description: 'Navigation',
  64. event_id: null,
  65. id: 166,
  66. level: BreadcrumbLevelType.INFO,
  67. message: undefined,
  68. timestamp: '2022-05-04T19:47:59.915000Z',
  69. type: BreadcrumbType.NAVIGATION,
  70. },
  71. ];
  72. }
  73. describe('getNextReplayEvent', () => {
  74. it('should return the next crumb', () => {
  75. const crumbs = createCrumbs();
  76. const results = getNextReplayEvent({
  77. items: crumbs,
  78. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  79. });
  80. expect(results?.id).toEqual(20);
  81. });
  82. it('should return the next crumb when the the list is not sorted', () => {
  83. const [one, two, three, four, five] = createCrumbs();
  84. const results = getNextReplayEvent({
  85. items: [one, four, five, three, two],
  86. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  87. });
  88. expect(results?.id).toEqual(20);
  89. });
  90. it('should return undefined when there are no crumbs', () => {
  91. const crumbs = [];
  92. const results = getNextReplayEvent({
  93. items: crumbs,
  94. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  95. });
  96. expect(results).toBeUndefined();
  97. });
  98. it('should return undefined when the timestamp is later than any crumbs', () => {
  99. const crumbs = createCrumbs();
  100. const results = getNextReplayEvent({
  101. items: crumbs,
  102. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + 99999999999,
  103. });
  104. expect(results).toBeUndefined();
  105. });
  106. it('should return the crumb after when a timestamp exactly matches', () => {
  107. const crumbs = createCrumbs();
  108. const exactCrumbTime = 8135;
  109. const results = getNextReplayEvent({
  110. items: crumbs,
  111. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + exactCrumbTime,
  112. });
  113. expect(results?.id).toEqual(20);
  114. });
  115. it('should return the crumb if timestamps exactly match and allowMatch is enabled', () => {
  116. const crumbs = createCrumbs();
  117. const exactCrumbTime = 8135;
  118. const results = getNextReplayEvent({
  119. items: crumbs,
  120. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + exactCrumbTime,
  121. });
  122. expect(results?.id).toEqual(20);
  123. });
  124. });
  125. describe('getPrevReplayEvent', () => {
  126. it('should return the previous crumb even if the timestamp is closer to the next crumb', () => {
  127. const crumbs = createCrumbs();
  128. const results = getPrevReplayEvent({
  129. items: crumbs,
  130. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  131. });
  132. expect(results?.id).toEqual(4);
  133. });
  134. it('should return the previous crumb when the list is not sorted', () => {
  135. const [one, two, three, four, five] = createCrumbs();
  136. const results = getPrevReplayEvent({
  137. items: [one, four, five, three, two],
  138. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  139. });
  140. expect(results?.id).toEqual(4);
  141. });
  142. it('should return undefined when there are no crumbs', () => {
  143. const crumbs = [];
  144. const results = getPrevReplayEvent({
  145. items: crumbs,
  146. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + CURRENT_TIME_MS,
  147. });
  148. expect(results).toBeUndefined();
  149. });
  150. it('should return undefined when the timestamp is earlier than any crumbs', () => {
  151. const crumbs = createCrumbs();
  152. const results = getPrevReplayEvent({
  153. items: crumbs,
  154. targetTimestampMs: START_TIMESTAMP_SEC * 1000 - CURRENT_TIME_MS,
  155. });
  156. expect(results).toBeUndefined();
  157. });
  158. it('should return the crumb previous when a crumbs timestamp exactly matches', () => {
  159. const crumbs = createCrumbs();
  160. const exactCrumbTime = 8135;
  161. const results = getPrevReplayEvent({
  162. items: crumbs,
  163. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + exactCrumbTime,
  164. });
  165. expect(results?.id).toEqual(3);
  166. });
  167. it('should return the crumb if timestamps exactly match and allowExact is enabled', () => {
  168. const crumbs = createCrumbs();
  169. const exactCrumbTime = 8135;
  170. const results = getPrevReplayEvent({
  171. items: crumbs,
  172. targetTimestampMs: START_TIMESTAMP_SEC * 1000 + exactCrumbTime,
  173. allowExact: true,
  174. });
  175. expect(results?.id).toEqual(4);
  176. });
  177. });