import { InfiniteData, QueryClient } from '@tanstack/react-query' import { buildTestQueryClient } from '../../../__utils__/query_client' import type { ApiCollection } from '../../../types' import type { MessageResource } from '../../../types/resources/message' import type { PersonResource } from '../../../types/resources/person' import { updateCacheReplyRoots } from '../messages_cache' const author = { id: 1, type: 'Person', name: 'A', avatar: null } as unknown as PersonResource const message = (id: string, overrides: Partial = {}): MessageResource => ({ type: 'Message', id, text: `msg ${id}`, html: '', createdAt: '2026-01-01T00:00:00Z', deletedAt: null, textEditedAt: null, mine: false, attachments: [], author, reactionCounts: [], replyCount: 0, replyRootId: null, messageType: 'message', ...overrides, }) as MessageResource const queryKey = ['messages'] const seedPages = (queryClient: QueryClient, messages: MessageResource[]) => { queryClient.setQueryData>>(queryKey, { pageParams: [{}], pages: [{ data: messages, links: {}, meta: { count: messages.length, totalCount: 1 } }], }) } const pageMessages = (queryClient: QueryClient) => queryClient.getQueryData>>(queryKey)!.pages[0].data describe('updateCacheReplyRoots', () => { it('merges a root update into every reply carrying that root', () => { const queryClient = buildTestQueryClient() const root = message('rootA', { replyRootId: 'rootA', text: 'before' }) seedPages(queryClient, [ message('03', { replyRootId: 'rootA', replyRoot: root }), message('02', { replyRootId: 'rootB', replyRoot: message('rootB', { text: 'other' }) }), message('01', { replyRootId: 'rootA', replyRoot: root }), ]) updateCacheReplyRoots( queryClient, queryKey, message('rootA', { replyRootId: 'rootA', text: 'after', deletedAt: '2026-01-02T00:00:00Z' }) ) const [newest, other, oldest] = pageMessages(queryClient) expect(newest.replyRoot).toMatchObject({ text: 'after', deletedAt: '2026-01-02T00:00:00Z' }) expect(oldest.replyRoot).toMatchObject({ text: 'after' }) expect(other.replyRoot?.text).toBe('other') }) it('ignores replies without an inline root', () => { const queryClient = buildTestQueryClient() seedPages(queryClient, [message('01', { replyRootId: 'rootA' })]) updateCacheReplyRoots(queryClient, queryKey, message('rootA', { replyRootId: 'rootA' })) expect(pageMessages(queryClient)[0].replyRoot).toBeUndefined() }) })