Files
PaperClipAI/ui/src/lib/issue-chat-bootstrap.test.ts
DottaandClaude Opus 5.5 ee6922f705 perf(tasks): open recent tasks without waiting on a slow ten-task preload (#21939)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - A person opens a task from the sidebar's Recent Tasks, the Inbox or
the Tasks list, and the task chat shows its first frame from a
`chat-bootstrap` read
> - On staging, a task opened from Recent Tasks soon after a page load
took 15.3 s to show its chat. The same task from the Inbox took 0.19 s
> - Two defects cause this. Every task's bootstrap paid a fixed ~1.8 s
for its latest runs. Recent Tasks preloaded ten tasks in one request
(17.7 s), and an open waited for that whole request
> - Recent Tasks also warmed its tasks only once per page load. Unread
cache entries are dropped after five minutes, so its tasks were cold for
the rest of the session
> - This pull request makes the runs read use indexes, lets an open read
its own task instead of waiting for a ten-task preload, and re-warms
Recent Tasks on navigation
> - The benefit is that a task from Recent Tasks opens as fast as from a
task list

## Linked Issues or Issue Description

Refs #19818, Refs #16713 (earlier proposals of the same `runsForIssue`
rewrite; this PR adds a query-plan regression test and the client
fixes).

**What happened?**
On staging, a click on a Recent Tasks link about 6 s after a page load
took 15.3 s until the chat thread showed. The Inbox opened the same task
in 0.19 s.

**Steps to reproduce**
1. Load a page with a populated Recent Tasks sidebar.
2. Within a few seconds, click a recent task.
3. The chat stays on its loading state for many seconds.

**Expected behavior**
The task opens about as fast as from the Inbox or the Tasks list.

**Root cause (measured on staging)**
- `GET /issues/:id/runs?view=recent` took 1.7–1.9 s of server time for
tasks with 1 to 24 runs. Every other first-frame read took 80–350 ms.
The query filtered with `issue_id = $1 OR EXISTS (activity_log … run_id
= heartbeat_runs.id)`, so Postgres walked every run of the company.
- The bootstrap reads two tasks at a time, so the Recents preload of ten
tasks took 17.7 s. Each task in a batch settles only with the whole
request, and the page's reads wait for it (up to the 5 s share deadline,
then read again under load).
- `useRecentTaskOpenWarmup` remembered preloaded task ids for the life
of the page and never preloaded them again.

## What Changed

- `activityService.runsForIssue`: select the task's runs by `id IN (runs
with issue_id = task UNION run ids from the task's activity)`. Each half
uses its own index. The results are the same.
- `startIssueChatBootstrap`: when the only request for a task is a
background batch that is still loading, send a request for that task
alone. All refs of the task point to the new request. The later batch
answer only fills keys that are still empty or older.
- `loadIssueChatBootstraps` (background): a task whose answer did not
arrive is not held back for five minutes, so the next preload can retry
it.
- `useRecentTaskOpenWarmup`: use the background preload (five-minute
repeat, the same as the cache GC time) and run again after each
navigation (`navigationKey`). It no longer keeps a per-page-load set.
`SidebarRecentTasks` and `CoordinatorSidebar` pass the pathname.

## Verification

- `server/src/__tests__/issue-runs-read-cost.test.ts` (new): a company
with 20,003 runs, and a task with 3 runs. EXPLAIN ANALYZE of the runs
reads examined **40,011** run rows before the fix and **15** after
(red/green). The test also checks the run order and the run that is
linked only through activity.
- `ui/src/lib/issue-chat-bootstrap.test.ts`: a new test opens a task
while a ten-task preload is still loading. Before the fix it timed out
waiting on the batch. After the fix it reads its own request. A second
new test checks that a single-task request is still shared.
- `ui/src/hooks/useRecentTaskOpenWarmup.test.tsx`: a new test re-warms
Recents on a navigation after five minutes (red on the old hook).
Another new test sends nothing for navigations inside five minutes.
- Existing suites pass: activity service/routes, task cold open, agent
conversations (197 tests); sidebar, Recents, navigation prefetch, issue
detail cache, IssueDetail (341 tests).
- `tsc --noEmit` is clean for `server` and `ui` (apart from the
pre-existing Storybook index errors).
- `pnpm verify` exits 2 (an opaque server setup dependency), so CI
supplies the broad coverage.
- After deploy: on staging, time a Recent Tasks click a few seconds
after a cold load, and time `runs?view=recent`.

## Risks

- Low risk. The SQL change keeps the same filter. It changes only how
Postgres finds the candidate runs. The existing activity tests cover
retry, credential and conversation cases.
- An open during a background preload now sends one more bootstrap
request for that task. This happens only on an open or a resting hover,
never for each row.
- Recents now sends at most one preload per five minutes while the
person navigates. It sends nothing while the tab is idle or hidden.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use
(shell, browser measurement on staging, code execution).

## 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-16 15:26:22 -06:00

423 lines
24 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";
import { recentIssueActivityQueryKey } from "./issue-chat-history-queries";
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("opens a task on its own request instead of waiting for a slow preload of ten tasks", async () => {
// Recents' preload of ten tasks is still loading (each task costs the server time).
let answerBatch!: (value: unknown) => void;
chatBootstrap.mockImplementationOnce(() => new Promise((resolve) => { answerBatch = resolve; }));
const batchRefs = Array.from({ length: 10 }, (_, index) => ({ id: idFor(index + 1), identifier: `ABC-${index + 1}` }));
void loadIssueChatBootstraps(client, "company-1", batchRefs);
await Promise.resolve();
// The person opens one of them: the page reads by identifier, a later read by id.
startIssueChatBootstrap(client, "company-1", "ABC-4");
const ownRead = vi.fn();
const byIdentifier = readFromIssueChatBootstrap(client, ["unread", "ABC-4"], "ABC-4", (bootstrap) => bootstrap.recentActivity, ownRead);
const byId = readFromIssueChatBootstrap(client, ["unread", idFor(4)], idFor(4), (bootstrap) => bootstrap.issue.identifier, ownRead);
await expect(byIdentifier).resolves.toEqual([{ id: "event-4" }]);
await expect(byId).resolves.toBe("ABC-4");
expect(ownRead).not.toHaveBeenCalled();
expect(requestedRefs()).toEqual([batchRefs.map((task) => task.id).join(","), "ABC-4"]);
// The preload's later answer seeds the other tasks and does not replace the newer read.
answerBatch({ items: batchRefs.map((task) => item(task.id, { recentActivity: [{ id: "stale" }] as never })) });
await vi.waitFor(() => expect(client.getQueryData([...queryKeys.issues.detail("ABC-5"), "core"])).toMatchObject({ identifier: "ABC-5" }));
expect(client.getQueryData(recentIssueActivityQueryKey("ABC-4"))).toEqual([{ id: "event-4" }]);
});
it("still shares a loading request that was made for this task alone", async () => {
let answer!: (value: unknown) => void;
chatBootstrap.mockImplementationOnce(() => new Promise((resolve) => { answer = resolve; }));
startIssueChatBootstrap(client, "company-1", "ABC-7");
await Promise.resolve();
startIssueChatBootstrap(client, "company-1", "ABC-7");
answer(respondWith(["ABC-7"]));
await expect(readFromIssueChatBootstrap(client, ["unread", "ABC-7"], "ABC-7", (bootstrap) => bootstrap.issue.identifier, vi.fn())).resolves.toBe("ABC-7");
expect(requestedRefs()).toEqual(["ABC-7"]);
});
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);
});
});