logs-query.test.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { fireEvent, screen, waitFor } from '@testing-library/react'
  2. import userEvent from '@testing-library/user-event'
  3. import dayjs from 'dayjs'
  4. import { beforeAll, describe, expect, test, vi } from 'vitest'
  5. import { LogsExplorerPage } from '@/pages/project/[ref]/logs/explorer/index'
  6. import { clickDropdown } from '@/tests/helpers'
  7. import { customRender as render } from '@/tests/lib/custom-render'
  8. import { routerMock } from '@/tests/lib/route-mock'
  9. const router = routerMock
  10. beforeAll(() => {
  11. vi.doMock('common', async (importOriginal: () => Promise<any>) => {
  12. const mod = await importOriginal()
  13. return {
  14. ...mod,
  15. IS_PLATFORM: true,
  16. useIsLoggedIn: vi.fn(),
  17. useParams: vi.fn(() => ({ ref: 'projectRef' })),
  18. }
  19. })
  20. vi.mock('@/lib/gotrue', () => ({
  21. auth: { onAuthStateChange: vi.fn() },
  22. }))
  23. })
  24. test.skip('can display log data', async () => {
  25. const { container } = render(<LogsExplorerPage dehydratedState={{}} />)
  26. let editor = container.querySelector('.monaco-editor')
  27. await waitFor(() => {
  28. editor = container.querySelector('.monaco-editor')
  29. expect(editor).toBeTruthy()
  30. })
  31. if (!editor) {
  32. throw new Error('editor not found')
  33. }
  34. await userEvent.type(editor, 'select \ncount(*) as my_count \nfrom edge_logs')
  35. await screen.findByText(/Save query/)
  36. const button = await screen.findByTitle('run-logs-query')
  37. await userEvent.click(button)
  38. const row = await screen.findByText(/timestamp/)
  39. await userEvent.click(row)
  40. await screen.findByText(/metadata/)
  41. await screen.findByText(/request/)
  42. })
  43. test('q= query param will populate the query input', async () => {
  44. router.query = { ...router.query, type: 'api', q: 'some_query' }
  45. render(<LogsExplorerPage dehydratedState={{}} />)
  46. })
  47. test('ite= and its= query param will populate the datepicker', async () => {
  48. const start = dayjs().subtract(1, 'day')
  49. const end = dayjs()
  50. router.query = {
  51. ...router.query,
  52. type: 'api',
  53. q: 'some_query',
  54. its: start.toISOString(),
  55. ite: end.toISOString(),
  56. }
  57. render(<LogsExplorerPage dehydratedState={{}} />)
  58. })
  59. test.skip('custom sql querying', async () => {
  60. const { container } = render(<LogsExplorerPage dehydratedState={{}} />)
  61. let editor = container.querySelector('.monaco-editor')
  62. if (!editor) {
  63. throw new Error('editor not found')
  64. }
  65. // type new query
  66. await userEvent.type(editor, 'select \ncount(*) as my_count \nfrom edge_logs')
  67. // run query by button
  68. await userEvent.click(await screen.findByText('Run'))
  69. // run query by editor
  70. await userEvent.type(editor, '\nlimit 123{ctrl}{enter}')
  71. await screen.findByText(/my_count/) //column header
  72. const rowValue = await screen.findByText(/12345/) // row value
  73. // clicking on the row value should not show log selection panel
  74. await userEvent.click(rowValue)
  75. await expect(screen.findByText(/Metadata/)).rejects.toThrow()
  76. // should not see chronological features
  77. await expect(screen.findByText(/Load older/)).rejects.toThrow()
  78. })
  79. test.skip('bug: can edit query after selecting a log', async () => {
  80. const { container } = render(<LogsExplorerPage dehydratedState={{}} />)
  81. // run default query
  82. await userEvent.click(await screen.findByText('Run'))
  83. const rowValue = await screen.findByText(/12345/) // row value
  84. // open up an show selection panel
  85. await userEvent.click(rowValue)
  86. await screen.findByText('Copy')
  87. // change the query
  88. let editor = container.querySelector('.monaco-editor')
  89. if (!editor) {
  90. throw new Error('editor not found')
  91. }
  92. // type new query
  93. await userEvent.click(editor)
  94. await userEvent.type(editor, ' something')
  95. await userEvent.type(editor, '\nsomething{ctrl}{enter}')
  96. await userEvent.click(await screen.findByText('Run'))
  97. // closes the selection panel
  98. await expect(screen.findByText('Copy')).rejects.toThrow()
  99. })
  100. test.skip('query warnings', async () => {
  101. router.query = {
  102. ...router.query,
  103. q: 'some_query',
  104. its: dayjs().subtract(10, 'days').toISOString(),
  105. ite: dayjs().toISOString(),
  106. }
  107. render(<LogsExplorerPage dehydratedState={{}} />)
  108. await screen.findByText('1 warning')
  109. })
  110. test('field reference', async () => {
  111. render(<LogsExplorerPage dehydratedState={{}} />)
  112. await userEvent.click(await screen.findByText('Field Reference'))
  113. await screen.findByText('metadata.request.cf.asOrganization')
  114. })
  115. describe.each(['free', 'pro', 'team', 'enterprise'])('upgrade modal for %s', (key) => {
  116. test.skip('based on query params', async () => {
  117. router.query = {
  118. ...router.query,
  119. q: 'some_query',
  120. its: dayjs().subtract(5, 'month').toISOString(),
  121. ite: dayjs().toISOString(),
  122. }
  123. render(<LogsExplorerPage dehydratedState={{}} />)
  124. await screen.findByText(/Log retention/) // assert modal title is present
  125. })
  126. test.skip('based on datepicker helpers', async () => {
  127. render(<LogsExplorerPage dehydratedState={{}} />)
  128. clickDropdown(screen.getByText('Last hour'))
  129. await waitFor(async () => {
  130. const option = await screen.findByText('Last 3 days')
  131. fireEvent.click(option)
  132. })
  133. // only Free Plan will show modal
  134. if (key === 'free') {
  135. await screen.findByText('Log retention') // assert modal title is present
  136. } else {
  137. await expect(screen.findByText('Log retention')).rejects.toThrow()
  138. }
  139. })
  140. })