Files
PaperClipAI/ui/src/lib/issue-chat-bootstrap.test.ts
T
DottaandClaude Opus 5.5 2e6850e745 perf(task-chat): open tasks from a preloaded cache and cold tasks in one request (#20592)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - People open tasks from task lists, the Inbox and the sidebar many
times each hour
> - A task's chat shows its first frame only after about seven separate
reads finish
> - #20355 added one batched `chat-bootstrap` read that returns all of
these reads. #20450 used it only for the sidebar's Recents
> - The hosted database is CPU-bound, so the UI must open tasks faster
and must also send fewer requests
> - This pull request uses the bootstrap for task lists, for open intent
on any task link, for cold opens and for a hard load of a task URL. It
shares one coordination module between all of these paths
> - The benefit is that a task opens from cache more often, a cold open
sends one chat request instead of about seven, and the first content
paint comes sooner

## Linked Issues or Issue Description

Refs #20355 (server read that this pull request consumes). Refs #20450
(Recents preload, now on the shared path).

**Is your feature request related to a problem?**
A click on a task in a task list waited for about seven separate chat
reads. The default task list (Coordinator) had no preload at all. A
press or a resting hover on a task link sent the same seven reads, and
the page then sent a second set. A hard load of a task URL started its
chat reads only after the JavaScript bundle and the company list loaded.
Sidebar project task links had no open-intent prefetch.

**Describe the solution you'd like**
Use the single `chat-bootstrap` read everywhere a task is about to open.
Let the page's gating reads share a bootstrap that is in flight instead
of sending their own requests. Keep the core issue read separate so the
header does not wait for the larger read.

**Describe alternatives you've considered**
Preload 30 rows per list in three requests (the first prototype). Each
task costs the server about fifty queries, and a resting hover warms any
other row before the click, so this change preloads 10 rows in one
request.

## What Changed

- `ui/src/lib/issue-chat-bootstrap.ts`: one module decides when to send
a bootstrap and lets page reads share it (`readFromIssueChatBootstrap`).
Seeding uses the existing `issueChatBootstrapCache` (replace-style reads
are refreshed when newer, merge-style reads are filled only when empty,
other companies are ignored). It skips tasks that are loading, loaded in
the last 30 seconds, or already fresh in cache, under the id or the
identifier. It never aborts a request in flight, so a click that
unmounts the list still uses the answer. Each request is bound to the
account that sent it; a request sent before the first session read is
bound to the first account the session reports, and a sign-out revokes
it. A page read shares a bootstrap only while its key holds no data (a
refresh after a write always reads the server), and waits at most 5
seconds for it. Every ref of a task (id and identifier) is tracked at
request start. Entries expire after 30 seconds.
- Task lists preload the chat of their top 10 rows in one request, after
the list's own reads settle and the browser is idle
(`usePreloadTaskChats`, `when-page-settles.ts`): the Coordinator task
list (`TaskList` with `preloadCompanyId`), the streamlined and legacy
issues lists and the streamlined Inbox. Recents now uses the same path,
so a task in both places loads once. A list that stays open preloads
each task at most once in five minutes: live events (#20563's lanes)
keep a preloaded task's cache current or mark it stale, and a stale key
is read from the server, never from an older bootstrap. All seeded keys
are per-task keys on the fast live lane, not the ambient lane.
- Open intent (press, click, resting hover or focus) on a task with no
chat history in cache warms it with one bootstrap request instead of six
separate reads (`prefetchIssueDetailForNavigation`). A task seen before
still refreshes its cached reads one by one.
- A cold open starts one bootstrap. The gating reads (recent comments,
activity, runs, input facts, interactions, attachments, live runs,
active run) wait for it instead of sending their own requests. The core
issue read stays independent.
- A hard load of a task URL starts `GET /api/issues/:ref/chat-bootstrap`
from the inline preload script, in parallel with the JavaScript bundle.
The page adopts that answer.
- Sidebar project task links (Coordinator sidebar) get the same hover,
press and focus warming as Recents and `IssueLinkQuicklook`.
- Tests: unit tests for the coordination module, Recents, `TaskList`
preload, sidebar warming and the cold-intent path. A new layout journey
checks that a task the Inbox preloaded reveals once at its latest
message without reading its chat again. Journeys that hold one separate
read now also hold or fail the bootstrap. The Inbox reveal journey held
a read that the chat no longer sends on open (it was flaky on master
too), so it now holds the recent comment window.
- The perf harness gains `PAPERCLIP_ISSUE_PERF_LIST_DWELL_MS` and the
README documents it.

## Verification

Measurement setup: production UI builds of master (`e01fe7f84a` and the
earlier base `8d2bf78323`) and of this branch, served by one local
server with `PAPERCLIP_UI_DEV_MIDDLEWARE=false` and embedded PostgreSQL.
Only `index.html` changed between variants (both asset sets were
present), so the server, database and seeded task were the same. Four
rounds of the issue-detail harness, with master and branch alternated in
each round: 20 pairs per S1 cell, 40 per S2 cell. The host was shared
with other agents (load average 16 to 43), so the spread is large.

Content paint (the task is readable), median paired delta, branch minus
master:

| Scenario | Profile | Pairs | Content paint | Branch faster in | API
requests before content (master / branch) |
|---|---|---:|---:|---:|---:|
| S1 preloaded click (dwell 1500 ms) | unthrottled | 20 | -45 ms | 15/20
| 25 / 18 |
| S1 preloaded click (dwell 1500 ms) | Fast 4G, 4x CPU | 20 | -23 ms |
13/20 | 26 / 18 |
| S1 click, no dwell | unthrottled | 20 | -51 ms | 15/20 | 25 / 18 |
| S1 click, no dwell | Fast 4G, 4x CPU | 20 | -21 ms | 12/20 | 26 / 18 |
| S2 cold deep link | unthrottled | 40 | -61 ms | 28/40 | 54 / 40 |
| S2 cold deep link | Fast 4G, 4x CPU | 40 | -129 ms | 29/40 | 47 / 39 |

Medians (p25 to p75), content paint in ms: S1 dwell unthrottled 295
(283-315) → 248 (217-276). S1 dwell Fast 4G 1133 (1114-1226) → 1093
(853-1176). S1 no dwell unthrottled 295 (277-328) → 260 (233-332). S1 no
dwell Fast 4G 984 (842-1196) → 923 (816-1110). S2 unthrottled 1002
(677-1235) → 896 (619-1280). S2 Fast 4G 4643 (4155-5061) → 4516
(4298-4894).

Header paint is neutral to slightly later in two cells: S1 dwell
unthrottled +39 ms (3/20 faster) and S2 Fast 4G +56 ms (17/40). In those
cells the header now paints in the same frame as the content. In the
first two rounds, the median gap from header to content fell from
105-157 ms to 29-74 ms unthrottled and from 557 ms to 298 ms for S2 on
Fast 4G. Other header cells are -27 to +1 ms.

The local server answers each read in a few milliseconds, so the network
wait that the preload removes is small here. The request count is the
stable result: a cold open sends one chat request instead of about
seven.

List page: the preload request starts after the first task row is in the
DOM (64 ms after it unthrottled, 200-280 ms after it on Fast 4G, page
clock). Time to the first visible row, 30-task list: unthrottled master
1863 ms vs branch 1563 ms (6 pairs, branch faster in 5). Fast 4G master
3626 ms vs branch 3727 ms (14 pairs, branch faster in 6, mixed signs).
The preload starts after the row paints, so it cannot delay it; the Fast
4G difference is noise. One 10-task bootstrap took 78-162 ms on the
server.

Checks run:
- `pnpm exec vitest run` for the bootstrap, cache, prefetch, Recents,
sidebar, quicklook, `TaskList`, issues list, Inbox, issue detail and
Coordinator tests: 18 files, 646 tests pass.
- `pnpm exec tsc -p ui/tsconfig.json --noEmit`: clean, except missing
generated Storybook catalog files that this change does not touch.
- `pnpm check:token-gates`: clean.
- `tests/e2e/task-loading-frame.spec.ts`,
`disconnect-resilience.spec.ts` and `connection-reviews.spec.ts`
(production build): 21 passed, 1 skipped.
- `tests/perf/issue-detail/layout-stability.spec.ts` and
`plan-startup.spec.ts` against the production build: all pass. The Inbox
reveal journey and the new preloaded journey passed 4 of 4 repeats each.
- `pnpm verify` asks for broader CI coverage because an e2e spec
changed.

## Risks

- More server work for list views: one bootstrap of up to 10 tasks per
list visit (about fifty queries per task), skipped for tasks already
loaded in the last 30 seconds. It runs only after the page's own reads
settle and the browser is idle, and only while the tab is visible.
- A resting hover on a never-opened task now sends one bootstrap instead
of about six reads. The server work per task is about the same; the
request count is lower.
- If a bootstrap fails, each page read falls back to its own request.
The fallback is slower than before for that one open (the reads start
when the page mounts, not at the press).
- The header can paint a few tens of milliseconds later in the same
frame as the content when everything is cached.
- An answer that arrives after a sign-out or account change is dropped.
An answer for another company is ignored.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude
Code with tool use (shell, file edits, Playwright runs).

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 14:26:27 -06:00

389 lines
21 KiB
TypeScript

// @vitest-environment jsdom
import { QueryClient } from "@tanstack/react-query";
import type { Issue, IssueRecentComments } from "@paperclipai/shared";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { EARLY_CHAT_BOOTSTRAP_GLOBAL } from "./issue-detail-preload-events";
import {
CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS,
CHAT_BOOTSTRAP_SHARE_DEADLINE_MS,
issueChatBootstrapEntryCountForTest,
loadIssueChatBootstraps,
readFromIssueChatBootstrap,
resetIssueChatBootstrapsForTest,
startIssueChatBootstrap,
type ChatBootstrapItem,
} from "./issue-chat-bootstrap";
import { queryKeys } from "./queryKeys";
import { ISSUE_CHAT_BOOTSTRAP_FRESH_MS } from "./issueChatBootstrapCache";
const chatBootstrap = vi.hoisted(() => vi.fn());
vi.mock("@/api/issues", () => ({ issuesApi: { chatBootstrap } }));
function idFor(number: number) {
return `00000000-0000-4000-8000-${String(number).padStart(12, "0")}`;
}
function item(ref: string, overrides: Partial<ChatBootstrapItem> = {}): ChatBootstrapItem {
const number = Number(ref.split("-").pop() ?? 1);
const comment = { id: `comment-${number}`, body: "Latest reply", createdAt: "2026-11-09T00:00:00Z" };
return {
ref,
issue: { id: idFor(number), identifier: `ABC-${number}`, companyId: "company-1", title: `Task ${ref}`, status: "done" } as unknown as Issue,
recentComments: { comments: [comment], latestCommentId: comment.id, hasMore: false } as unknown as IssueRecentComments,
commentInputFacts: [{ id: `fact-${number}` }] as never,
recentActivity: [{ id: `event-${number}` }] as never,
recentRuns: [{ runId: `run-${number}-b` }, { runId: `run-${number}-a` }] as never,
interactions: [],
attachments: [],
liveRuns: [],
activeRun: null,
...overrides,
};
}
function respondWith(refs: readonly string[]) {
return { items: refs.map((ref) => item(ref)) };
}
const requestedRefs = () => chatBootstrap.mock.calls.map((call) => (call[1] as string[]).join(","));
let client: QueryClient;
beforeEach(() => {
client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
resetIssueChatBootstrapsForTest();
chatBootstrap.mockReset();
chatBootstrap.mockImplementation(async (_companyId: string, refs: string[]) => respondWith(refs));
});
afterEach(() => {
client.clear();
vi.useRealTimers();
delete (window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL];
});
describe("loadIssueChatBootstraps", () => {
it("loads 30 tasks as three requests of ten, one at a time, and seeds the page's keys", async () => {
let concurrent = 0;
let maxConcurrent = 0;
chatBootstrap.mockImplementation(async (_companyId: string, refs: string[]) => {
maxConcurrent = Math.max(maxConcurrent, ++concurrent);
await Promise.resolve();
concurrent--;
return respondWith(refs);
});
const refs = Array.from({ length: 30 }, (_, index) => `ABC-${index + 1}`);
await loadIssueChatBootstraps(client, "company-1", refs);
expect(chatBootstrap.mock.calls.map((call) => (call[1] as string[]).length)).toEqual([10, 10, 10]);
expect(chatBootstrap.mock.calls[0]![0]).toBe("company-1");
expect(chatBootstrap.mock.calls[0]![1][0]).toBe("ABC-1");
expect(maxConcurrent).toBe(1);
expect(client.getQueryData([...queryKeys.issues.detail("ABC-30"), "core"])).toMatchObject({ identifier: "ABC-30" });
expect(client.getQueryData([...queryKeys.issues.detail(idFor(30)), "core"])).toMatchObject({ identifier: "ABC-30" });
});
it("skips tasks loaded recently and reloads them once they are stale", async () => {
vi.useFakeTimers({ toFake: ["Date"] });
await loadIssueChatBootstraps(client, "company-1", ["ABC-1", "ABC-2"]);
await loadIssueChatBootstraps(client, "company-1", ["ABC-2", "ABC-3"]);
expect(requestedRefs()).toEqual(["ABC-1,ABC-2", "ABC-3"]);
vi.setSystemTime(Date.now() + ISSUE_CHAT_BOOTSTRAP_FRESH_MS + 1);
await loadIssueChatBootstraps(client, "company-1", ["ABC-1"]);
expect(requestedRefs()).toEqual(["ABC-1,ABC-2", "ABC-3", "ABC-1"]);
});
it("does not load a task again under its other reference (a list by identifier, Recents by id)", async () => {
await loadIssueChatBootstraps(client, "company-1", ["ABC-4"]);
await loadIssueChatBootstraps(client, "company-1", [{ id: idFor(4), identifier: "ABC-4" }, { id: idFor(5), identifier: "ABC-5" }]);
expect(requestedRefs()).toEqual(["ABC-4", idFor(5)]);
});
it("does not wait for or repeat a request already in flight for the same task", async () => {
let answer!: (value: unknown) => void;
chatBootstrap.mockImplementationOnce(() => new Promise((resolve) => { answer = resolve; }));
const first = loadIssueChatBootstraps(client, "company-1", ["ABC-6"]);
await Promise.resolve();
await loadIssueChatBootstraps(client, "company-1", ["abc-6"]);
expect(chatBootstrap).toHaveBeenCalledOnce();
answer(respondWith(["ABC-6"]));
await first;
});
it("stops later requests when aborted, but keeps the one in flight usable by the page", async () => {
let answer!: (value: unknown) => void;
chatBootstrap.mockImplementationOnce(() => new Promise((resolve) => { answer = resolve; }));
const controller = new AbortController();
const refs = Array.from({ length: 12 }, (_, index) => `ABC-${index + 1}`);
const loading = loadIssueChatBootstraps(client, "company-1", refs, { signal: controller.signal });
await Promise.resolve();
// The click that opens a task unmounts the list and aborts its preload.
controller.abort();
const ownRead = vi.fn();
const interactions = readFromIssueChatBootstrap(client, ["unread", "ABC-3"], "ABC-3", (bootstrap) => bootstrap.recentActivity, ownRead);
answer(respondWith(refs.slice(0, 10)));
await loading;
await expect(interactions).resolves.toEqual([{ id: "event-3" }]);
expect(ownRead).not.toHaveBeenCalled();
expect(chatBootstrap).toHaveBeenCalledOnce();
expect(chatBootstrap.mock.calls[0]![2]).toBeUndefined();
});
it("lets a failed preload fall back to the page's own reads and be retried", async () => {
chatBootstrap.mockRejectedValueOnce(new Error("busy"));
await loadIssueChatBootstraps(client, "company-1", ["ABC-1"]);
const read = vi.fn().mockResolvedValue("from server");
await expect(readFromIssueChatBootstrap(client, ["unread", "ABC-1"], "ABC-1", (bootstrap) => bootstrap.interactions, read)).resolves.toBe("from server");
expect(read).toHaveBeenCalledOnce();
await loadIssueChatBootstraps(client, "company-1", ["ABC-1"]);
expect(chatBootstrap).toHaveBeenCalledTimes(2);
});
it("never shares or seeds a task that belongs to another company", async () => {
chatBootstrap.mockResolvedValueOnce({ items: [item("ABC-7", { issue: { ...item("ABC-7").issue, companyId: "company-2" } })] });
await loadIssueChatBootstraps(client, "company-1", ["ABC-7"]);
expect(client.getQueryData([...queryKeys.issues.detail("ABC-7"), "core"])).toBeUndefined();
const read = vi.fn().mockResolvedValue([]);
await readFromIssueChatBootstrap(client, ["unread", "ABC-7"], "ABC-7", (bootstrap) => bootstrap.interactions, read);
expect(read).toHaveBeenCalledOnce();
});
});
describe("readFromIssueChatBootstrap", () => {
it("lets a cold open's reads share one loading bootstrap instead of sending their own", async () => {
let answer!: (value: unknown) => void;
chatBootstrap.mockImplementationOnce(() => new Promise((resolve) => { answer = resolve; }));
void loadIssueChatBootstraps(client, "company-1", ["ABC-9"]);
await Promise.resolve();
const ownRead = vi.fn();
const interactions = readFromIssueChatBootstrap(client, ["unread", "abc-9"], "abc-9", (bootstrap) => bootstrap.interactions, ownRead);
const activity = readFromIssueChatBootstrap(client, ["unread", "ABC-9"], "ABC-9", (bootstrap) => bootstrap.recentActivity, ownRead);
answer(respondWith(["ABC-9"]));
await expect(interactions).resolves.toEqual([]);
await expect(activity).resolves.toEqual([{ id: "event-9" }]);
expect(ownRead).not.toHaveBeenCalled();
expect(chatBootstrap).toHaveBeenCalledOnce();
});
it("reads from the server when the bootstrap omits the task, is not loading, or is no longer new", async () => {
vi.useFakeTimers({ toFake: ["Date"] });
chatBootstrap.mockResolvedValueOnce({ items: [] });
await loadIssueChatBootstraps(client, "company-1", ["ABC-4"]);
const read = vi.fn().mockResolvedValue(["server"]);
await expect(readFromIssueChatBootstrap(client, ["unread", "ABC-4"], "ABC-4", (bootstrap) => bootstrap.attachments, read)).resolves.toEqual(["server"]);
await expect(readFromIssueChatBootstrap(client, ["unread", "ABC-5"], "ABC-5", (bootstrap) => bootstrap.attachments, read)).resolves.toEqual(["server"]);
await loadIssueChatBootstraps(client, "company-1", ["ABC-8"]);
// A later read of a loaded task is a refresh: it asks the server.
vi.setSystemTime(Date.now() + 5_000);
await expect(readFromIssueChatBootstrap(client, ["unread", "ABC-8"], "ABC-8", (bootstrap) => bootstrap.attachments, read)).resolves.toEqual(["server"]);
expect(read).toHaveBeenCalledTimes(3);
});
});
describe("startIssueChatBootstrap", () => {
it("adopts the hard load's early fetch instead of sending a request", async () => {
(window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL] = { ref: "abc-3", promise: Promise.resolve(item("ABC-3")) };
startIssueChatBootstrap(client, null, "ABC-3");
const ownRead = vi.fn();
await expect(readFromIssueChatBootstrap(client, ["unread", "ABC-3"], "ABC-3", (bootstrap) => bootstrap.recentActivity, ownRead)).resolves.toEqual([{ id: "event-3" }]);
expect(client.getQueryData([...queryKeys.issues.detail("ABC-3"), "core"])).toMatchObject({ identifier: "ABC-3" });
expect(chatBootstrap).not.toHaveBeenCalled();
expect(ownRead).not.toHaveBeenCalled();
expect((window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL]).toBeUndefined();
});
it("does not use an early answer for a task of another company than the page's", async () => {
(window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL] = { ref: "ABC-3", promise: Promise.resolve(item("ABC-3")) };
startIssueChatBootstrap(client, "company-2", "ABC-3");
const read = vi.fn().mockResolvedValue([]);
await readFromIssueChatBootstrap(client, ["unread", "ABC-3"], "ABC-3", (bootstrap) => bootstrap.recentActivity, read);
expect(read).toHaveBeenCalledOnce();
expect(client.getQueryData([...queryKeys.issues.detail("ABC-3"), "core"])).toBeUndefined();
});
it("falls back to the page's reads when the early fetch failed", async () => {
(window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL] = { ref: "ABC-3", promise: Promise.resolve(null) };
startIssueChatBootstrap(client, null, "ABC-3");
const read = vi.fn().mockResolvedValue([]);
await readFromIssueChatBootstrap(client, ["unread", "ABC-3"], "ABC-3", (bootstrap) => bootstrap.recentActivity, read);
expect(read).toHaveBeenCalledOnce();
});
it("ignores an early fetch for another task and loads this one through the company endpoint", async () => {
(window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL] = { ref: "ABC-1", promise: Promise.resolve(item("ABC-1")) };
startIssueChatBootstrap(client, "company-1", "ABC-2");
await Promise.resolve();
await expect(readFromIssueChatBootstrap(client, ["unread", "ABC-2"], "ABC-2", (bootstrap) => bootstrap.issue.identifier, vi.fn())).resolves.toBe("ABC-2");
expect(requestedRefs()).toEqual(["ABC-2"]);
});
});
describe("background preloads", () => {
it("do not preload a task again for five minutes while a list stays open, but an open still loads it", async () => {
vi.useFakeTimers({ toFake: ["Date"] });
await loadIssueChatBootstraps(client, "company-1", ["ABC-1", "ABC-2"], { background: true });
// Past the 30-second freshness: the list re-renders after a live reorder.
vi.setSystemTime(Date.now() + ISSUE_CHAT_BOOTSTRAP_FRESH_MS + 1);
await loadIssueChatBootstraps(client, "company-1", ["ABC-2", "ABC-1", "ABC-3"], { background: true });
expect(requestedRefs()).toEqual(["ABC-1,ABC-2", "ABC-3"]);
// Open intent is not a background preload.
await loadIssueChatBootstraps(client, "company-1", ["ABC-1"]);
expect(requestedRefs()).toEqual(["ABC-1,ABC-2", "ABC-3", "ABC-1"]);
vi.setSystemTime(Date.now() + CHAT_BOOTSTRAP_BACKGROUND_REPEAT_MS);
await loadIssueChatBootstraps(client, "company-1", ["ABC-2"], { background: true });
expect(requestedRefs()).toEqual(["ABC-1,ABC-2", "ABC-3", "ABC-1", "ABC-2"]);
});
it("leave a seeded key that a live event invalidated to the page's own read", async () => {
await loadIssueChatBootstraps(client, "company-1", ["ABC-4"], { background: true });
const key = queryKeys.issues.interactions("ABC-4");
// An event for this task (the fast live lane) marks the seeded key stale.
await client.invalidateQueries({ queryKey: key });
expect(client.getQueryState(key)?.isInvalidated).toBe(true);
const read = vi.fn().mockResolvedValue([{ id: "new" }]);
await expect(readFromIssueChatBootstrap(client, key, "ABC-4", (bootstrap) => bootstrap.interactions, read)).resolves.toEqual([{ id: "new" }]);
expect(read).toHaveBeenCalledOnce();
});
});
describe("safeguards", () => {
it("does not request a task again by its identifier while its id's request is in flight", async () => {
let answer!: (value: unknown) => void;
chatBootstrap.mockImplementationOnce(() => new Promise((resolve) => { answer = resolve; }));
// Recents asks by id (and knows the identifier); the list then asks by identifier.
const recents = loadIssueChatBootstraps(client, "company-1", [{ id: idFor(5), identifier: "ABC-5" }]);
await Promise.resolve();
await loadIssueChatBootstraps(client, "company-1", ["ABC-5"]);
expect(chatBootstrap).toHaveBeenCalledOnce();
answer({ items: [item("ABC-5", { ref: idFor(5) })] });
await recents;
});
it("stops waiting for a stalled bootstrap, so the page and its Retry read the server", async () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
chatBootstrap.mockImplementationOnce(() => new Promise(() => {}));
void loadIssueChatBootstraps(client, "company-1", ["ABC-6"]);
await Promise.resolve();
const read = vi.fn().mockResolvedValue(["server"]);
const first = readFromIssueChatBootstrap(client, ["unread"], "ABC-6", (bootstrap) => bootstrap.attachments, read);
await vi.advanceTimersByTimeAsync(CHAT_BOOTSTRAP_SHARE_DEADLINE_MS);
await expect(first).resolves.toEqual(["server"]);
// A retry does not join the stuck request again.
await expect(readFromIssueChatBootstrap(client, ["unread"], "ABC-6", (bootstrap) => bootstrap.attachments, read)).resolves.toEqual(["server"]);
expect(read).toHaveBeenCalledTimes(2);
});
it("never shares a bootstrap with a refresh of a key that already holds data", async () => {
let answer!: (value: unknown) => void;
chatBootstrap.mockImplementationOnce(() => new Promise((resolve) => { answer = resolve; }));
void loadIssueChatBootstraps(client, "company-1", ["ABC-9"]);
await Promise.resolve();
// An accepted confirmation was written and its key invalidated: the refresh
// must read the server, not the bootstrap's older pending interaction.
const key = queryKeys.issues.interactions("ABC-9");
client.setQueryData(key, [{ id: "accepted" }]);
const read = vi.fn().mockResolvedValue([{ id: "accepted" }]);
await expect(readFromIssueChatBootstrap(client, key, "ABC-9", (bootstrap) => bootstrap.interactions, read)).resolves.toEqual([{ id: "accepted" }]);
expect(read).toHaveBeenCalledOnce();
answer(respondWith(["ABC-9"]));
});
it("never gives one account's bootstrap to another account", async () => {
client.setQueryData(queryKeys.auth.session, { user: { id: "user-1" } });
await loadIssueChatBootstraps(client, "company-1", ["ABC-3"]);
client.clear();
client.setQueryData(queryKeys.auth.session, { user: { id: "user-2" } });
const read = vi.fn().mockResolvedValue(["server"]);
await expect(readFromIssueChatBootstrap(client, ["unread"], "ABC-3", (bootstrap) => bootstrap.attachments, read)).resolves.toEqual(["server"]);
expect(read).toHaveBeenCalledOnce();
// The new account loads the task for itself.
await loadIssueChatBootstraps(client, "company-1", ["ABC-3"]);
expect(requestedRefs()).toEqual(["ABC-3", "ABC-3"]);
});
it("drops an early answer when the account changes before it arrives", async () => {
client.setQueryData(queryKeys.auth.session, { user: { id: "user-1" } });
let answer!: (value: unknown) => void;
(window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL] = {
ref: "ABC-3", startedAt: Date.now(), promise: new Promise((resolve) => { answer = resolve; }),
};
startIssueChatBootstrap(client, null, "ABC-3");
client.setQueryData(queryKeys.auth.session, { user: { id: "user-2" } });
answer(item("ABC-3"));
const read = vi.fn().mockResolvedValue([]);
await readFromIssueChatBootstrap(client, ["unread"], "ABC-3", (bootstrap) => bootstrap.recentActivity, read);
expect(read).toHaveBeenCalledOnce();
expect(client.getQueryData([...queryKeys.issues.detail("ABC-3"), "core"])).toBeUndefined();
});
it("binds an early answer adopted before the session loaded to the first account that uses it", async () => {
(window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL] = { ref: "ABC-3", startedAt: Date.now(), promise: Promise.resolve(item("ABC-3")) };
// The page renders the task before its first session read finishes.
await startIssueChatBootstrap(client, null, "ABC-3");
client.setQueryData(queryKeys.auth.session, { user: { id: "user-1" } });
const ownRead = vi.fn();
await expect(readFromIssueChatBootstrap(client, ["unread"], "ABC-3", (bootstrap) => bootstrap.recentActivity, ownRead)).resolves.toEqual([{ id: "event-3" }]);
expect(ownRead).not.toHaveBeenCalled();
// Once used by user-1, another account cannot use it.
client.setQueryData(queryKeys.auth.session, { user: { id: "user-2" } });
const read = vi.fn().mockResolvedValue([]);
await readFromIssueChatBootstrap(client, ["unread"], "ABC-3", (bootstrap) => bootstrap.recentActivity, read);
expect(read).toHaveBeenCalledOnce();
});
it("never seeds a pre-session early answer after a sign-out, nor for the next account", async () => {
let answer!: (value: unknown) => void;
(window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL] = {
ref: "ABC-3", startedAt: Date.now(), promise: new Promise((resolve) => { answer = resolve; }),
};
// Adopted before the first session read finishes.
startIssueChatBootstrap(client, null, "ABC-3");
client.setQueryData(queryKeys.auth.session, { user: { id: "user-1" } });
// user-1 signs out, then user-2 signs in, before the answer arrives.
client.setQueryData(queryKeys.auth.session, null);
client.setQueryData(queryKeys.auth.session, { user: { id: "user-2" } });
answer(item("ABC-3"));
await Promise.resolve();
await Promise.resolve();
expect(client.getQueryData([...queryKeys.issues.detail("ABC-3"), "core"])).toBeUndefined();
const read = vi.fn().mockResolvedValue([]);
await readFromIssueChatBootstrap(client, ["unread"], "ABC-3", (bootstrap) => bootstrap.recentActivity, read);
expect(read).toHaveBeenCalledOnce();
});
it("revokes a pre-session early answer when the first session has no user", async () => {
(window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL] = { ref: "ABC-3", startedAt: Date.now(), promise: new Promise(() => {}) };
startIssueChatBootstrap(client, null, "ABC-3");
client.setQueryData(queryKeys.auth.session, null);
client.setQueryData(queryKeys.auth.session, { user: { id: "user-2" } });
const read = vi.fn().mockResolvedValue([]);
await readFromIssueChatBootstrap(client, ["unread"], "ABC-3", (bootstrap) => bootstrap.recentActivity, read);
expect(read).toHaveBeenCalledOnce();
});
it("stamps an early answer with its request time and ignores one that is already stale", async () => {
const startedAt = Date.now() - 5_000;
(window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL] = { ref: "ABC-3", startedAt, promise: Promise.resolve(item("ABC-3")) };
await startIssueChatBootstrap(client, null, "ABC-3");
expect(client.getQueryState([...queryKeys.issues.detail("ABC-3"), "core"])?.dataUpdatedAt).toBe(startedAt);
(window as unknown as Record<string, unknown>)[EARLY_CHAT_BOOTSTRAP_GLOBAL] = {
ref: "ABC-4", startedAt: Date.now() - ISSUE_CHAT_BOOTSTRAP_FRESH_MS - 1, promise: Promise.resolve(item("ABC-4")),
};
startIssueChatBootstrap(client, "company-1", "ABC-4");
await Promise.resolve();
// The page read the task itself instead of seeding the stale answer.
expect(requestedRefs()).toEqual(["ABC-4"]);
});
it("drops expired answers so they do not stay in memory", async () => {
vi.useFakeTimers({ toFake: ["Date"] });
await loadIssueChatBootstraps(client, "company-1", ["ABC-1"]);
await Promise.resolve();
vi.setSystemTime(Date.now() + ISSUE_CHAT_BOOTSTRAP_FRESH_MS + 1);
client.clear();
await loadIssueChatBootstraps(client, "company-1", ["ABC-2"]);
await Promise.resolve();
// Only ABC-2 remains, under its identifier and its id.
expect(issueChatBootstrapEntryCountForTest()).toBe(2);
});
});