Repository navigation
fix(tickets): the drawer reads a file's link again when a read brought none CU-86agmkyyu #3350
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+811
−2
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4fcc312
fix(tickets): the drawer reads a file's link again when a read brough…
oleksandr-blip 8fc1a9b
fix(tickets): a file that lost its link again gets its re-reads again…
oleksandr-blip ef3e1ba
fix(tickets): a re-read for a file's link sends one request, not the …
oleksandr-blip ec6ec6d
fix(tickets): only the re-reads go without the client's retries CU-86…
oleksandr-blip 06b15b8
test(tickets): two drawers on one ticket share one run of re-reads CU…
oleksandr-blip f186dd7
refactor(tickets): the re-read budget is a pure function beside the h…
oleksandr-blip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
330 changes: 330 additions & 0 deletions
330
...rame-frontend-core/src/components/tickets/hooks/__tests__/use-ticket-engagements.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,330 @@ | ||
| import { notifyManager, QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
| import { act, renderHook } from '@testing-library/react'; | ||
| import type { ReactNode } from 'react'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('../../../../utils/embed-authed-fetch', async importOriginal => ({ | ||
| ...(await importOriginal<Record<string, unknown>>()), | ||
| embedAuthedFetch: vi.fn(), | ||
| })); | ||
| vi.mock('../../../../contexts/chat-runtime-context', async importOriginal => ({ | ||
| ...(await importOriginal<Record<string, unknown>>()), | ||
| useRequiredChatRuntime: () => ({ endpoints: {} }), | ||
| })); | ||
| vi.mock('../../../chat/hooks/use-chat-identity', () => ({ | ||
| useChatIdentity: () => ({ authTier: 'session', isLoading: false, user: { email: 'customer@example.com' } }), | ||
| })); | ||
| import { embedAuthedFetch } from '../../../../utils/embed-authed-fetch'; | ||
| import { type TicketEngagement, type TicketEngagementFile, useTicketEngagements } from '../use-ticket-engagements'; | ||
|
|
||
| const signedUrl = 'https://1.cdnp1.hubspotusercontent-na1.net/hubfs/1/screen.png?Expires=1&Signature=s'; | ||
|
|
||
| function file(id: string, url: string | null): TicketEngagementFile { | ||
| return { id, name: `${id}.png`, url, mime: url ? 'image/png' : null, size: null }; | ||
| } | ||
|
|
||
| function message(id: string, ...attachments: TicketEngagementFile[]): TicketEngagement { | ||
| return { | ||
| id, | ||
| body: 'Here is the screenshot', | ||
| authorId: null, | ||
| authorRole: 'customer', | ||
| authorName: null, | ||
| authorEmail: 'customer@example.com', | ||
| authorAvatarUrl: null, | ||
| createdAt: '2026-12-04T12:54:49Z', | ||
| attachments, | ||
| }; | ||
| } | ||
|
|
||
| const unlinked = [message('m1', file('229452971610', null))]; | ||
| const linked = [message('m1', file('229452971610', signedUrl))]; | ||
| // Beside a file that never gets its link, so the re-reads' budget outlives the other file's link. | ||
| const besideStuckFile = (url: string | null) => [ | ||
| message('m0', file('stuck', null)), | ||
| message('m1', file('229452971610', url)), | ||
| ]; | ||
|
|
||
| const fetchMock = vi.mocked(embedAuthedFetch); | ||
| const reply = (engagements: TicketEngagement[]) => | ||
| ({ ok: true, json: () => Promise.resolve({ engagements }) }) as unknown as Response; | ||
|
|
||
| function respondWith(...reads: TicketEngagement[][]) { | ||
| for (const engagements of reads) fetchMock.mockResolvedValueOnce(reply(engagements)); | ||
| fetchMock.mockResolvedValue(reply(reads[reads.length - 1] ?? [])); | ||
| } | ||
|
|
||
| // Queues a read that answers only when the returned function is called. | ||
| function heldRead() { | ||
| let answer!: (engagements: TicketEngagement[]) => void; | ||
| const response = new Promise<Response>(resolve => { | ||
| answer = engagements => resolve(reply(engagements)); | ||
| }); | ||
| fetchMock.mockImplementationOnce(() => response); | ||
| return answer; | ||
| } | ||
|
|
||
| function renderEngagements(client = new QueryClient({ defaultOptions: { queries: { retry: false } } })) { | ||
| const rendered = renderHook(() => useTicketEngagements('52119670862'), { | ||
| wrapper: ({ children }: { children: ReactNode }) => ( | ||
| <QueryClientProvider client={client}>{children}</QueryClientProvider> | ||
| ), | ||
| }); | ||
| return { ...rendered, client }; | ||
| } | ||
|
|
||
| // Runs the timers due within `ms`, then lets the reads they started reach the hook. | ||
| const advance = (ms: number) => | ||
| act(async () => { | ||
| await vi.advanceTimersByTimeAsync(ms); | ||
| await vi.advanceTimersByTimeAsync(0); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers(); | ||
| // react-query hands results to the hook on a `setTimeout(0)`, which fake timers run only once a millisecond has | ||
| // passed: a microtask keeps every read's result at the time the read finished. | ||
| notifyManager.setScheduler(queueMicrotask); | ||
| fetchMock.mockReset(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| notifyManager.setScheduler(callback => setTimeout(callback, 0)); | ||
| vi.useRealTimers(); | ||
| }); | ||
|
|
||
| describe('useTicketEngagements: a file the server could not link yet', () => { | ||
| it('reads the timeline again and shows the link the next read brings', async () => { | ||
| respondWith(unlinked, linked); | ||
| const { result } = renderEngagements(); | ||
| await advance(0); | ||
| expect(result.current.engagements[0]?.attachments[0]?.url).toBeNull(); | ||
|
|
||
| await advance(2_000); | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| expect(result.current.engagements[0]?.attachments[0]?.url).toBe(signedUrl); | ||
| }); | ||
|
|
||
| it('stops after three more reads when the link never comes', async () => { | ||
| respondWith(unlinked); | ||
| renderEngagements(); | ||
| await advance(0); | ||
|
|
||
| await advance(2_000); | ||
| await advance(4_000); | ||
| await advance(8_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(4); | ||
|
|
||
| await advance(60_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(4); | ||
| }); | ||
|
|
||
| it('waits longer before each further read', async () => { | ||
| respondWith(unlinked); | ||
| renderEngagements(); | ||
| await advance(0); | ||
|
|
||
| await advance(1_999); | ||
| expect(fetchMock).toHaveBeenCalledTimes(1); | ||
| await advance(1); | ||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| await advance(3_999); | ||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| await advance(1); | ||
| expect(fetchMock).toHaveBeenCalledTimes(3); | ||
| await advance(7_999); | ||
| expect(fetchMock).toHaveBeenCalledTimes(3); | ||
| await advance(1); | ||
| expect(fetchMock).toHaveBeenCalledTimes(4); | ||
| }); | ||
|
|
||
| it('does not read again when every file has its link', async () => { | ||
| respondWith([message('m0'), ...linked]); | ||
| renderEngagements(); | ||
| await advance(0); | ||
|
|
||
| await advance(60_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('reads again when one file among several messages has no link', async () => { | ||
| respondWith( | ||
| [message('m0'), message('m1', file('f1', signedUrl)), message('m2', file('f2', null))], | ||
| [message('m0'), message('m1', file('f1', signedUrl)), message('m2', file('f2', signedUrl))], | ||
| ); | ||
| renderEngagements(); | ||
| await advance(0); | ||
|
|
||
| await advance(2_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| await advance(60_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('reads again when one of a message’s files has no link', async () => { | ||
| respondWith([message('m1', file('f1', signedUrl), file('f2', null))]); | ||
| renderEngagements(); | ||
| await advance(0); | ||
|
|
||
| await advance(2_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it('stops when the reads after a file without a link keep failing', async () => { | ||
| respondWith(unlinked); | ||
| fetchMock.mockRejectedValue(new Error('network down')); | ||
| const { result } = renderEngagements(); | ||
| await advance(0); | ||
|
|
||
| await advance(2_000); | ||
| await advance(4_000); | ||
| await advance(8_000); | ||
| await advance(60_000); | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledTimes(4); | ||
| expect(result.current.engagements[0]?.attachments[0]?.url).toBeNull(); | ||
| }); | ||
|
|
||
| it('does not read again after a failed read that brought no timeline', async () => { | ||
| fetchMock.mockRejectedValue(new Error('network down')); | ||
| const { result } = renderEngagements(); | ||
| await advance(0); | ||
| expect(result.current.error).not.toBeNull(); | ||
|
|
||
| await advance(60_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('reads again when a later read brings the file without its link once more', async () => { | ||
| respondWith(unlinked, linked, unlinked, linked); | ||
| const { result } = renderEngagements(); | ||
| await advance(0); | ||
| await advance(2_000); | ||
| expect(result.current.engagements[0]?.attachments[0]?.url).toBe(signedUrl); | ||
|
|
||
| act(() => result.current.refetch()); | ||
| await advance(0); | ||
| expect(fetchMock).toHaveBeenCalledTimes(3); | ||
| expect(result.current.engagements[0]?.attachments[0]?.url).toBeNull(); | ||
|
|
||
| await advance(2_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(4); | ||
| expect(result.current.engagements[0]?.attachments[0]?.url).toBe(signedUrl); | ||
| }); | ||
|
|
||
| it('starts over for a new file without a link after the re-reads ran out', async () => { | ||
| respondWith(unlinked, unlinked, unlinked, unlinked, [...unlinked, message('m2', file('f2', null))]); | ||
| const { result } = renderEngagements(); | ||
| await advance(0); | ||
| await advance(2_000); | ||
| await advance(4_000); | ||
| await advance(8_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(4); | ||
|
|
||
| act(() => result.current.refetch()); | ||
| await advance(0); | ||
| expect(fetchMock).toHaveBeenCalledTimes(5); | ||
|
|
||
| await advance(2_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(6); | ||
| }); | ||
|
|
||
| it('does not start over when a later read brings the same file without a link', async () => { | ||
| respondWith(unlinked); | ||
| const { result } = renderEngagements(); | ||
| await advance(0); | ||
| await advance(2_000); | ||
| await advance(4_000); | ||
| await advance(8_000); | ||
|
|
||
| act(() => result.current.refetch()); | ||
| await advance(0); | ||
| expect(fetchMock).toHaveBeenCalledTimes(5); | ||
|
|
||
| await advance(60_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(5); | ||
| }); | ||
|
|
||
| it('starts over for a file that lost its link again after the re-reads ran out', async () => { | ||
| respondWith( | ||
| besideStuckFile(null), | ||
| besideStuckFile(signedUrl), | ||
| besideStuckFile(signedUrl), | ||
| besideStuckFile(signedUrl), | ||
| besideStuckFile(null), | ||
| ); | ||
| const { result } = renderEngagements(); | ||
| await advance(0); | ||
| await advance(2_000); | ||
| await advance(4_000); | ||
| await advance(8_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(4); | ||
|
|
||
| act(() => result.current.refetch()); | ||
| await advance(0); | ||
| expect(fetchMock).toHaveBeenCalledTimes(5); | ||
|
|
||
| await advance(2_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(6); | ||
| }); | ||
|
|
||
| it('does not start over for a file that lost its link again during the re-reads', async () => { | ||
| respondWith(besideStuckFile(null), besideStuckFile(signedUrl), besideStuckFile(signedUrl), besideStuckFile(null)); | ||
| renderEngagements(); | ||
| await advance(0); | ||
| await advance(2_000); | ||
| await advance(4_000); | ||
| await advance(8_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(4); | ||
|
|
||
| await advance(60_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(4); | ||
| }); | ||
|
|
||
| it('counts only the reads the timeline keeps, not one a newer read replaced', async () => { | ||
| fetchMock.mockResolvedValueOnce(reply(unlinked)); | ||
| const replaced = heldRead(); | ||
| const newer = heldRead(); | ||
| fetchMock.mockResolvedValue(reply(linked)); | ||
| const { result, client } = renderEngagements(); | ||
| await advance(0); | ||
| await advance(2_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
|
|
||
| // A stream event invalidates the ticket while the re-read is on its way: a newer read replaces it. | ||
| act(() => { | ||
| void client.invalidateQueries({ queryKey: ['ticket-engagements'] }); | ||
| }); | ||
| expect(fetchMock).toHaveBeenCalledTimes(3); | ||
| act(() => { | ||
| replaced(linked); | ||
| newer(unlinked); | ||
| }); | ||
| await advance(0); | ||
| expect(result.current.engagements[0]?.attachments[0]?.url).toBeNull(); | ||
|
|
||
| await advance(4_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(4); | ||
| expect(result.current.engagements[0]?.attachments[0]?.url).toBe(signedUrl); | ||
| }); | ||
|
|
||
| it('keeps reading again for a drawer reopened while the first read was on its way', async () => { | ||
| const first = heldRead(); | ||
| fetchMock.mockResolvedValue(reply(linked)); | ||
| const closed = renderEngagements(); | ||
| await advance(0); | ||
| closed.unmount(); | ||
|
|
||
| const { result } = renderEngagements(closed.client); | ||
| await advance(0); | ||
| expect(fetchMock).toHaveBeenCalledTimes(1); | ||
| act(() => first(unlinked)); | ||
| await advance(0); | ||
| expect(result.current.engagements[0]?.attachments[0]?.url).toBeNull(); | ||
|
|
||
| await advance(2_000); | ||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| expect(result.current.engagements[0]?.attachments[0]?.url).toBe(signedUrl); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.