Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

25.9 KB
Raw Blame
vi.mock('@queries/messages/subscription', () => ({
  useMessageSubscription: () => {},
}));

/** @vitest-environment jsdom */
import type {
  Message,
  MessageListItem,
  MessageParent,
  MessageThread,
} from '@service-storage/messages';
import { QueryObserver } from '@tanstack/query-core';
import { QueryClient } from '@tanstack/solid-query';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

let testQueryClient: QueryClient;
vi.mock('../../client', () => ({
  get queryClient() {
    return testQueryClient;
  },
}));
const mocks = vi.hoisted(() => ({ thread: vi.fn() }));
vi.mock('@service-storage/messages', async (importOriginal) => ({
  ...(await importOriginal<object>()),
  entityMessagesClient: { thread: mocks.thread },
}));

import { registerNonce } from '../../nonce';
import { MessageNonceKeys, messageKeys } from '../keys';
import {
  applyMessage,
  applyThreadState,
  handleMessageEvent,
  handleTimelineActivity,
} from '../sync';
import {
  getThreadRepliesQueryKey,
  threadRepliesQueryOptions,
} from '../thread-replies';
import {
  getMessageTimelineQueryKey,
  type MessageTimelineData,
} from '../timeline';
import { timelineMessages } from '../timeline-entries';
import { clearTypingIndicators, getTypingUsers } from '../typing';

const time = '2026-09-09T00:00:00Z';
const message = (
  parent: MessageParent,
  id: string,
  thread_id?: string
): Message => ({
  id,
  parent,
  thread_id,
  sender_id: 'macro|[email protected]',
  content: id,
  created_at: time,
  updated_at: time,
  mentions: [],
  attachments: [],
  reactions: [],
});
const state = {
  root_id: 'root',
  user_id: 'macro|[email protected]',
  created_at: time,
  updated_at: time,
  resolved: false,
};
const item = (parent: MessageParent): MessageListItem => ({
  ...message(parent, 'root'),
  state,
  thread: { reply_count: 0, latest_reply_at: null, preview: [] },
});
beforeEach(() => {
  testQueryClient = new QueryClient({
    defaultOptions: { queries: { retry: false } },
  });
});
afterEach(() => {
  testQueryClient.clear();
  clearTypingIndicators();
});

it('refetches an empty call after a first message races the initial thread fetch', async () => {
  const parent: MessageParent = { type: 'call', id: 'call-id' };
  const key = getThreadRepliesQueryKey(parent, parent.id);
  const root = message(parent, parent.id);
  const thread: MessageThread = {
    root,
    state: { ...state, root_id: root.id },
    replies: [],
  };
  let finishEmptyFetch!: (value: null) => void;
  const fetchThread = vi
    .fn()
    .mockImplementationOnce(
      () =>
        new Promise<null>((resolve) => {
          finishEmptyFetch = resolve;
        })
    )
    .mockResolvedValue(thread);
  const observer = new QueryObserver<MessageThread | null>(testQueryClient, {
    queryKey: key,
    queryFn: fetchThread,
  });
  const stop = observer.subscribe(() => {});

  handleMessageEvent({
    parent,
    actor: root.sender_id,
    change: {
      type: 'posted',
      message: root,
      mentions: [],
      notification_policy: 'Default',
    },
  });
  expect(fetchThread).toHaveBeenCalledTimes(1);
  finishEmptyFetch(null);

  await vi.waitFor(() => {
    expect(fetchThread).toHaveBeenCalledTimes(2);
    expect(testQueryClient.getQueryData(key)).toEqual(thread);
  });
  stop();
});

describe.each([
  'channel',
  'document',
  'initiative',
  'crm_company',
  'call',
] as const)('%s uses the shared live cache', (type) => {
  const parent: MessageParent = { type, id: 'source' };
  const other: MessageParent = {
    type: type === 'channel' ? 'document' : 'channel',
    id: 'source',
  };
  const timelineKey = () => getMessageTimelineQueryKey(parent);
  const threadKey = () => getThreadRepliesQueryKey(parent, 'root');
  function seed() {
    testQueryClient.setQueryData<MessageTimelineData>(timelineKey(), {
      pageParams: [null],
      pages: [
        {
          entries: [{ type: 'message', message: item(parent) }],
          next_cursor: null,
          previous_cursor: null,
        },
      ],
    });
    testQueryClient.setQueryData<MessageListItem[]>(
      messageKeys.messagesByIds(parent, ['root']).queryKey,
      [item(parent)]
    );
    testQueryClient.setQueryData<MessageThread>(threadKey(), {
      state,
      root: message(parent, 'root'),
      replies: [],
    });
    testQueryClient.setQueryData<MessageThread>(
      getThreadRepliesQueryKey(other, 'root'),
      { state, root: message(other, 'root'), replies: [] }
    );
  }
  it('reconciles replies, attachments, mentions, reactions, and edits across timeline and linked drawer', () => {
    seed();
    const reply = {
      ...message(parent, 'reply', 'root'),
      attachments: [
        {
          id: 'attachment',
          entity_id: 'doc',
          entity_type: 'document',
          created_at: time,
        },
      ],
    };
    applyMessage(reply, 'posted');
    const edit = {
      ...reply,
      content: 'edited',
      mentions: [{ entity_type: 'document', entity_id: 'doc' }],
      reactions: [{ emoji: '👍', users: ['macro|[email protected]'] }],
    };
    applyMessage(edit, 'edited');
    const root = timelineMessages(
      testQueryClient.getQueryData<MessageTimelineData>(timelineKey())!.pages[0]
    )[0];
    expect(root.thread.reply_count).toBe(1);
    expect(root.thread.preview).toEqual([expect.objectContaining(edit)]);
    expect(
      testQueryClient.getQueryData<MessageThread>(threadKey())!.replies
    ).toEqual([expect.objectContaining(edit)]);
    expect(
      testQueryClient.getQueryData<MessageListItem[]>(
        messageKeys.messagesByIds(parent, ['root']).queryKey
      )![0].thread.preview
    ).toEqual([expect.objectContaining(edit)]);
    expect(
      testQueryClient.getQueryData<MessageThread>(
        getThreadRepliesQueryKey(other, 'root')
      )!.replies
    ).toEqual([]);
  });
  it('preserves imported reply order when an older reply is edited', () => {
    seed();
    const first = {
      ...message(parent, 'first', 'root'),
      created_at: '2026-01-03T00:00:00Z',
    };
    const second = {
      ...message(parent, 'second', 'root'),
      created_at: '2026-01-01T00:00:00Z',
    };
    testQueryClient.setQueryData<MessageThread>(threadKey(), {
      state,
      root: message(parent, 'root'),
      replies: [first, second],
    });
    applyMessage({ ...first, content: 'edited historical reply' }, 'edited');
    expect(
      testQueryClient
        .getQueryData<MessageThread>(threadKey())!
        .replies.map((reply) => reply.id)
    ).toEqual(['first', 'second']);
  });
  it('does not count a reaction to an unseen preview reply as a new post', () => {
    seed();
    testQueryClient.removeQueries({ queryKey: threadKey() });
    const root = {
      ...item(parent),
      thread: {
        reply_count: 4,
        latest_reply_at: time,
        preview: ['first', 'second', 'third'].map((id) =>
          message(parent, id, 'root')
        ),
      },
    };
    testQueryClient.setQueryData<MessageTimelineData>(timelineKey(), {
      pageParams: [null],
      pages: [
        {
          entries: [{ type: 'message', message: root }],
          next_cursor: null,
          previous_cursor: null,
        },
      ],
    });
    testQueryClient.setQueryData<MessageListItem[]>(
      messageKeys.messagesByIds(parent, ['root']).queryKey,
      [root]
    );

    handleMessageEvent({
      parent,
      actor: 'macro|[email protected]',
      change: {
        type: 'reaction_changed',
        emoji: '👍',
        added: true,
        message: {
          ...message(parent, 'fourth', 'root'),
          reactions: [{ emoji: '👍', users: ['macro|[email protected]'] }],
        },
      },
    });

    expect(
      timelineMessages(
        testQueryClient.getQueryData<MessageTimelineData>(timelineKey())!
          .pages[0]
      )[0].thread
    ).toEqual(root.thread);
    expect(
      testQueryClient.getQueryData<MessageListItem[]>(
        messageKeys.messagesByIds(parent, ['root']).queryKey
      )![0].thread
    ).toEqual(root.thread);
    expect(testQueryClient.getQueryData(threadKey())).toBeUndefined();
  });
  it('does not insert an unseen edit or let a reaction snapshot overwrite content', () => {
    seed();
    applyMessage(message(parent, 'unseen', 'root'), 'edited');
    expect(
      testQueryClient.getQueryData<MessageThread>(threadKey())!.replies
    ).toEqual([]);

    const reply = message(parent, 'reply', 'root');
    applyMessage(reply, 'posted');
    applyMessage({ ...reply, content: 'new content' }, 'edited');
    applyMessage(
      {
        ...reply,
        reactions: [{ emoji: '👍', users: ['macro|[email protected]'] }],
      },
      'reaction_changed'
    );

    expect(
      testQueryClient.getQueryData<MessageThread>(threadKey())!.replies
    ).toEqual([
      expect.objectContaining({
        content: 'new content',
        reactions: [{ emoji: '👍', users: ['macro|[email protected]'] }],
      }),
    ]);
    expect(
      timelineMessages(
        testQueryClient.getQueryData<MessageTimelineData>(timelineKey())!
          .pages[0]
      )[0].thread.reply_count
    ).toBe(1);
  });
  it('updates the canonical root when only a linked thread is cached', () => {
    testQueryClient.setQueryData<MessageThread>(threadKey(), {
      state,
      root: message(parent, 'root'),
      replies: [message(parent, 'reply', 'root')],
    });
    applyMessage(
      { ...message(parent, 'root'), content: 'edited root' },
      'edited'
    );
    expect(
      testQueryClient.getQueryData<MessageThread>(threadKey())!.root.content
    ).toBe('edited root');
    applyMessage(
      { ...message(parent, 'root'), content: '', deleted_at: time },
      'message_deleted'
    );
    const thread = testQueryClient.getQueryData<MessageThread>(threadKey())!;
    expect(thread.root.deleted_at).toBe(time);
    expect(thread.replies).toHaveLength(1);
    expect(testQueryClient.getQueryData(timelineKey())).toBeUndefined();
  });
  it('keeps root tombstones with replies and propagates resolution and whole-thread deletion', () => {
    seed();
    applyMessage(message(parent, 'reply', 'root'), 'posted');
    applyMessage(
      {
        ...message(parent, 'root'),
        content: '',
        deleted_at: time,
      },
      'message_deleted'
    );
    expect(
      timelineMessages(
        testQueryClient.getQueryData<MessageTimelineData>(timelineKey())!
          .pages[0]
      )[0].deleted_at
    ).toBe(time);
    expect(
      testQueryClient.getQueryData<MessageThread>(threadKey())!.replies
    ).toHaveLength(1);
    applyThreadState(parent, { ...state, resolved: true });
    expect(
      testQueryClient.getQueryData<MessageThread>(threadKey())!.state.resolved
    ).toBe(true);
    applyThreadState(parent, { ...state, deleted_at: time });
    expect(
      timelineMessages(
        testQueryClient.getQueryData<MessageTimelineData>(timelineKey())!
          .pages[0]
      )
    ).toEqual(
      parent.type === 'document'
        ? [expect.objectContaining({ state: { ...state, deleted_at: time } })]
        : []
    );
  });
  it('inserts an external root post at the bottom of the conversation only', () => {
    seed();
    handleMessageEvent({
      parent,
      actor: 'macro|[email protected]',
      nonce: null,
      change: {
        type: 'posted',
        message: {
          ...message(parent, 'newer-root'),
          created_at: '2026-09-09T00:00:01Z',
        },
        mentions: [],
        notification_policy: 'Default',
      },
    });
    expect(
      timelineMessages(
        testQueryClient.getQueryData<MessageTimelineData>(timelineKey())!
          .pages[0]
      ).map((root) => root.id)
    ).toEqual(['newer-root', 'root']);

    testQueryClient.setQueryData<MessageTimelineData>(timelineKey(), {
      pageParams: [null],
      pages: [
        {
          entries: [{ type: 'message', message: item(parent) }],
          next_cursor: null,
          previous_cursor: { created_at: time, id: 'root' },
        },
      ],
    });
    applyMessage(message(parent, 'mid-conversation'), 'posted');
    expect(
      timelineMessages(
        testQueryClient.getQueryData<MessageTimelineData>(timelineKey())!
          .pages[0]
      ).map((root) => root.id)
    ).toEqual(['root']);
  });
  it(
    type !== 'channel'
      ? "loads a live root's thread state instead of refetching the timeline"
      : 'applies a live root without a metadata refetch',
    async () => {
      seed();
      mocks.thread.mockReset();
      const rootState = {
        ...state,
        root_id: 'newer-root',
        anchor:
          type === 'document'
            ? ({ type: 'markdown', mark_id: 'mark' } as const)
            : null,
      };
      // An agent's comment: the thread endpoint returns the same
      // bot_profile the timeline does, never a pre-derived sender.
      const agentRoot: Message = {
        ...message(parent, 'newer-root'),
        sender_id: 'bot|00000000-0000-0000-0000-000000000001',
        bot_profile: { name: 'Bingus', avatar_url: null },
        triggered_by: 'macro|[email protected]',
      };
      mocks.thread.mockResolvedValue({
        root: agentRoot,
        state: rootState,
        replies: [],
      });
      const invalidate = vi.spyOn(testQueryClient, 'invalidateQueries');
      handleMessageEvent({
        parent,
        actor: agentRoot.sender_id,
        nonce: null,
        change: {
          type: 'posted',
          message: agentRoot,
          mentions: [],
          notification_policy: 'Default',
        },
      });
      const newestRoot = () =>
        timelineMessages(
          testQueryClient.getQueryData<MessageTimelineData>(timelineKey())!
            .pages[0]
        )[0];
      const agentSender = expect.objectContaining({
        type: 'bot',
        name: 'Bingus',
        triggered_by: 'macro|[email protected]',
      });
      expect(newestRoot().sender).toEqual(agentSender);
      if (type !== 'channel') {
        expect(mocks.thread).toHaveBeenCalledWith(parent, 'newer-root');
        await vi.waitFor(() =>
          expect(
            testQueryClient.getQueryData<MessageThread>(
              getThreadRepliesQueryKey(parent, 'newer-root')
            )?.state
          ).toEqual(rootState)
        );
        expect(newestRoot().state.anchor).toEqual(rootState.anchor);
        const thread = testQueryClient.getQueryData<MessageThread>(
          getThreadRepliesQueryKey(parent, 'newer-root')
        );
        expect(thread?.state).toEqual(rootState);
        // The margin renders this cached root, so the agent must keep its
        // name here too rather than falling back to a generic "Bot".
        expect(thread?.root.sender).toEqual(agentSender);
      } else {
        expect(mocks.thread).not.toHaveBeenCalled();
        expect(newestRoot().id).toBe('newer-root');
        expect(newestRoot().state.anchor).toBeNull();
      }
      // Only the soft invalidation that leaves mounted timelines alone may run.
      for (const [filters] of invalidate.mock.calls) {
        if (type === 'call' && filters?.exact) {
          expect(filters.queryKey).toEqual(
            getThreadRepliesQueryKey(parent, parent.id)
          );
        } else {
          expect(filters?.refetchType).toBe('inactive');
        }
      }
    }
  );
  it('refetches a bottom page that is still loading when a root arrives', async () => {
    mocks.thread.mockReset();
    let resolveFetch: (data: MessageTimelineData) => void = () => {};
    const fetching = testQueryClient.fetchQuery({
      queryKey: timelineKey(),
      queryFn: () =>
        new Promise<MessageTimelineData>((resolve) => {
          resolveFetch = resolve;
        }),
    });
    const invalidate = vi.spyOn(testQueryClient, 'invalidateQueries');
    handleMessageEvent({
      parent,
      actor: 'macro|[email protected]',
      nonce: null,
      change: {
        type: 'posted',
        message: message(parent, 'racing-root'),
        mentions: [],
        notification_policy: 'Default',
      },
    });
    // The refetch waits for the in-flight page to settle rather than
    // deduping into it, so nothing is invalidated yet.
    expect(invalidate).not.toHaveBeenCalled();
    expect(mocks.thread).not.toHaveBeenCalled();
    resolveFetch({
      pageParams: [null],
      pages: [
        {
          entries: [{ type: 'message', message: item(parent) }],
          next_cursor: null,
          previous_cursor: null,
        },
      ],
    });
    await fetching.catch(() => undefined);
    await vi.waitFor(() =>
      expect(invalidate).toHaveBeenCalledWith({
        queryKey: timelineKey(),
        exact: true,
      })
    );
  });
  it("recovers the sender's own root posted before the bottom page loads", async () => {
    let resolveFetch: (data: MessageTimelineData) => void = () => {};
    const fetching = testQueryClient.fetchQuery({
      queryKey: timelineKey(),
      queryFn: () =>
        new Promise<MessageTimelineData>((resolve) => {
          resolveFetch = resolve;
        }),
    });
    registerNonce(MessageNonceKeys.MESSAGE, 'own-send');
    const invalidate = vi.spyOn(testQueryClient, 'invalidateQueries');
    handleMessageEvent({
      parent,
      actor: 'macro|[email protected]',
      nonce: 'own-send',
      change: {
        type: 'posted',
        message: message(parent, 'own-root'),
        mentions: [],
        notification_policy: 'Default',
      },
    });
    // The sender's own optimistic insert no-oped while the page was loading,
    // and the echo alone would be consumed by the nonce; the recovery fetch
    // still runs once the page settles.
    resolveFetch({
      pageParams: [null],
      pages: [{ entries: [], next_cursor: null, previous_cursor: null }],
    });
    await fetching.catch(() => undefined);
    await vi.waitFor(() =>
      expect(invalidate).toHaveBeenCalledWith({
        queryKey: timelineKey(),
        exact: true,
      })
    );
  });
  // An expanded thread whose first replies fetch is still in flight: the root
  // is in the timeline, but the thread's own query has not resolved yet.
  async function expandThreadWithPendingFetch() {
    testQueryClient.setQueryData<MessageTimelineData>(timelineKey(), {
      pageParams: [null],
      pages: [
        {
          entries: [{ type: 'message', message: item(parent) }],
          next_cursor: null,
          previous_cursor: null,
        },
      ],
    });
    mocks.thread.mockReset();
    let resolveFirst: (thread: MessageThread) => void = () => {};
    mocks.thread.mockImplementationOnce(
      () =>
        new Promise<MessageThread>((resolve) => {
          resolveFirst = resolve;
        })
    );
    const observer = new QueryObserver(
      testQueryClient,
      threadRepliesQueryOptions(parent, 'root')
    );
    const unsubscribe = observer.subscribe(() => {});
    await vi.waitFor(() => expect(mocks.thread).toHaveBeenCalledTimes(1));
    const threadReplies = () =>
      testQueryClient.getQueryData<MessageThread>(threadKey())?.replies;
    // The fetch settles predating whatever arrived while it was in flight.
    const settleEmpty = async () => {
      resolveFirst({ state, root: message(parent, 'root'), replies: [] });
      await vi.waitFor(() => expect(threadReplies()).toBeDefined());
      await Promise.resolve();
    };
    return { settleEmpty, threadReplies, unsubscribe };
  }

  it('keeps a live reply that arrives while its first replies fetch is in flight', async () => {
    const { settleEmpty, threadReplies, unsubscribe } =
      await expandThreadWithPendingFetch();

    // The preview-only insert drops the reply from the not-yet-cached thread;
    // it must be re-applied once the fetch settles rather than lost.
    applyMessage(message(parent, 'reply', 'root'), 'posted');
    await settleEmpty();

    expect(threadReplies()).toEqual([expect.objectContaining({ id: 'reply' })]);
    // Convergence needs no extra network round trip.
    expect(mocks.thread).toHaveBeenCalledTimes(1);

    // The timeline preview mirrors the reply while the thread settles.
    const root = timelineMessages(
      testQueryClient.getQueryData<MessageTimelineData>(timelineKey())!.pages[0]
    )[0];
    expect(root.thread.reply_count).toBe(1);
    expect(root.thread.preview).toEqual([
      expect.objectContaining({ id: 'reply' }),
    ]);
    unsubscribe();
  });

  it('applies the latest edit of a reply that arrives while the fetch is in flight', async () => {
    const { settleEmpty, threadReplies, unsubscribe } =
      await expandThreadWithPendingFetch();
    const reply = message(parent, 'reply', 'root');

    applyMessage(reply, 'posted');
    applyMessage({ ...reply, content: 'edited' }, 'edited');
    await settleEmpty();

    // The deferred re-apply reads the current preview state, not the stale
    // posted payload, so the edit wins.
    expect(threadReplies()).toEqual([
      expect.objectContaining({ id: 'reply', content: 'edited' }),
    ]);
    unsubscribe();
  });

  it('does not resurrect a reply deleted while the fetch is in flight', async () => {
    const { settleEmpty, threadReplies, unsubscribe } =
      await expandThreadWithPendingFetch();
    const reply = message(parent, 'reply', 'root');

    applyMessage(reply, 'posted');
    applyMessage(
      { ...reply, content: '', deleted_at: time },
      'message_deleted'
    );
    await settleEmpty();

    // The reply left the preview on delete, so the deferred re-apply skips it.
    expect(threadReplies()).toEqual([]);
    unsubscribe();
  });
  it('derives senders for a thread fetched on its own', async () => {
    mocks.thread.mockReset();
    const agent = 'bot|00000000-0000-0000-0000-000000000001';
    mocks.thread.mockResolvedValue({
      state,
      root: {
        ...message(parent, 'root'),
        sender_id: agent,
        bot_profile: { name: 'Bingus', avatar_url: null },
      },
      replies: [
        {
          ...message(parent, 'reply', 'root'),
          sender_id: agent,
          bot_profile: { name: 'Bingus', avatar_url: null },
          triggered_by: 'macro|[email protected]',
        },
        message(parent, 'human-reply', 'root'),
      ],
    });
    const thread = await testQueryClient.fetchQuery(
      threadRepliesQueryOptions(parent, 'root')
    );
    expect(thread.root.sender).toEqual(
      expect.objectContaining({ type: 'bot', name: 'Bingus' })
    );
    expect(thread.replies.map((reply) => reply.sender)).toEqual([
      expect.objectContaining({
        type: 'bot',
        name: 'Bingus',
        triggered_by: 'macro|[email protected]',
      }),
      { type: 'user', id: 'macro|[email protected]', triggered_by: undefined },
    ]);
  });
  it('skips the sender nonce and scopes ephemeral typing to the parent and root', () => {
    seed();
    registerNonce(MessageNonceKeys.MESSAGE, 'own-send');
    handleMessageEvent({
      parent,
      actor: 'macro|[email protected]',
      nonce: 'own-send',
      change: {
        type: 'posted',
        message: message(parent, 'reply', 'root'),
        mentions: [],
        notification_policy: 'Default',
      },
    });
    expect(
      testQueryClient.getQueryData<MessageThread>(threadKey())!.replies
    ).toEqual([]);
    handleMessageEvent(
      {
        parent,
        actor: 'macro|[email protected]',
        nonce: null,
        change: { type: 'typing', active: true, thread_id: null },
      },
      'macro|[email protected]'
    );
    expect([...getTypingUsers(parent)]).toEqual(['macro|[email protected]']);
    expect([...getTypingUsers(other)]).toEqual([]);
  });
});

describe('live timeline activity', () => {
  const parent: MessageParent = { type: 'channel', id: 'live-activity' };
  const root = (id: string, created_at: string) => ({
    type: 'message' as const,
    message: { ...item(parent), id, created_at },
  });
  const activity = {
    id: 'renamed',
    actor_id: 'macro|[email protected]',
    occurred_at: '2026-09-09T00:00:01Z',
    action: 'renamed',
    payload: { to: 'Planning' },
  };
  const keys = (key: readonly unknown[]) =>
    testQueryClient
      .getQueryData<MessageTimelineData>(key)!
      .pages.flatMap((page) =>
        page.entries.map((entry) =>
          entry.type === 'activity'
            ? `activity:${entry.activity.id}`
            : entry.message.id
        )
      );

  it('refetches a bottom page that is still loading when activity arrives', async () => {
    const latest = getMessageTimelineQueryKey(parent);
    let resolveFetch: (data: MessageTimelineData) => void = () => {};
    const fetching = testQueryClient.fetchQuery({
      queryKey: latest,
      queryFn: () =>
        new Promise<MessageTimelineData>((resolve) => {
          resolveFetch = resolve;
        }),
    });
    const invalidate = vi.spyOn(testQueryClient, 'invalidateQueries');
    handleTimelineActivity({
      parent,
      activities: [activity],
    });
    expect(invalidate).not.toHaveBeenCalled();
    resolveFetch({
      pageParams: [null],
      pages: [{ entries: [], next_cursor: null, previous_cursor: null }],
    });
    await fetching.catch(() => undefined);
    await vi.waitFor(() =>
      expect(invalidate).toHaveBeenCalledWith({ queryKey: latest, exact: true })
    );
  });

  it('places activity by position in windows that contain it', () => {
    const latest = getMessageTimelineQueryKey(parent);
    const history = getMessageTimelineQueryKey(parent, 'older');
    testQueryClient.setQueryData<MessageTimelineData>(latest, {
      pageParams: [null],
      pages: [
        {
          entries: [
            root('newer', '2026-09-09T00:00:02Z'),
            root('older', '2026-09-09T00:00:00Z'),
          ],
          next_cursor: null,
          previous_cursor: null,
        },
      ],
    });
    // A load-around window whose newest loaded row predates the activity.
    testQueryClient.setQueryData<MessageTimelineData>(history, {
      pageParams: [null],
      pages: [
        {
          entries: [root('older', '2026-09-09T00:00:00Z')],
          next_cursor: null,
          previous_cursor: { id: 'older', created_at: '2026-09-09T00:00:00Z' },
        },
      ],
    });

    handleTimelineActivity({ parent, activities: [activity] });
    handleTimelineActivity({ parent, activities: [activity] });

    expect(keys(latest)).toEqual(['newer', 'activity:renamed', 'older']);
    expect(keys(history)).toEqual(['older']);
  });
});