Files
openchamber/packages/ui/src/stores/useInlineCommentDraftStore.terminal.test.ts
T
Bohdan Triapitsyn 85400459e9 perf: overhaul session loading, caching, and runtime isolation (#2360)
Improve OpenChamber responsiveness under large session workloads while fixing
cache, synchronization, and persistence correctness across runtimes, projects,
directories, and worktrees.

- prioritize selected and visible sessions during bootstrap and defer
  non-critical enrichment work
- reduce redundant message loading, event processing, store publication, and
  hidden sidebar work
- prevent stale session and message requests from overwriting newer
  authoritative state
- preserve existing data when authoritative fetches fail instead of treating
  failures as successful empty responses
- scope session materialization, messages, drafts, queues, todos, pins,
  permissions, folders, tabs, Git state, and pull request data by runtime and
  directory identity
- harden runtime switching, reconnect, cleanup, mutation reconciliation, and
  persisted-state ordering
- preserve live subagent Task linkage when metadata arrives after an older
  message request or while streaming parts are suspended
- coalesce overlapping tail refreshes without losing newer refresh demand
- improve cold-session loading by moving deferrable work out of the critical
  bootstrap path
- isolate URL authentication, mobile credentials, native secrets, and other
  runtime-owned state across endpoint changes
- bound long-lived caches and remove avoidable allocations from event and
  rendering hot paths
- limit virtualization to archive collections where it improves rendering
  without disrupting active sidebar layout
- stabilize session folders, pin ordering, expanded state, and persisted
  sidebar behavior
- open skill files through the same secure editor and outside-workspace grant
  flow used by file navigation, including worktree sessions
- expand regression coverage for stale completions, runtime collisions,
  reconnect behavior, persistence races, authoritative empty results, and
  subagent refresh ordering
- document the updated synchronization, cache ownership, performance, and
  runtime-isolation invariants
2026-07-21 20:52:20 +03:00

82 lines
3.9 KiB
TypeScript

import { afterEach, describe, expect, test } from 'bun:test';
import { useInlineCommentDraftStore } from './useInlineCommentDraftStore';
const selection = {
source: 'terminal' as const,
fileLabel: 'Terminal 1',
startLine: 4,
endLine: 5,
code: 'first\nsecond',
language: 'term-1',
text: '',
};
const target = { directory: '/repo', sessionKey: 'session-1' };
describe('terminal context drafts', () => {
afterEach(() => { useInlineCommentDraftStore.setState({ drafts: {}, touchedAt: {} }); });
test('persists snapshots by chat session and deduplicates identical selections', () => {
useInlineCommentDraftStore.getState().addDraft(target, selection);
useInlineCommentDraftStore.getState().addDraft(target, selection);
const drafts = useInlineCommentDraftStore.getState().getDrafts(target);
expect(drafts).toHaveLength(1);
expect({ ...drafts[0], id: undefined, createdAt: undefined }).toEqual({ ...selection, sessionKey: 'session-1', id: undefined, createdAt: undefined });
});
test('supports individual removal and ordered consume', () => {
useInlineCommentDraftStore.getState().addDraft(target, selection);
useInlineCommentDraftStore.getState().addDraft(target, { ...selection, startLine: 8, endLine: 8, code: 'third' });
const drafts = useInlineCommentDraftStore.getState().getDrafts(target);
useInlineCommentDraftStore.getState().removeDraft(target, drafts[0].id);
expect(useInlineCommentDraftStore.getState().consumeDrafts(target)).toHaveLength(1);
expect(useInlineCommentDraftStore.getState().getDrafts(target)).toEqual([]);
});
test('restores consumed drafts after a failed send without duplicating them', () => {
useInlineCommentDraftStore.getState().addDraft(target, selection);
const consumed = useInlineCommentDraftStore.getState().consumeDrafts(target);
useInlineCommentDraftStore.getState().restoreDrafts(target, consumed);
useInlineCommentDraftStore.getState().restoreDrafts(target, consumed);
expect(useInlineCommentDraftStore.getState().getDrafts(target)).toEqual(consumed);
});
test('isolates identical session IDs by normalized directory', () => {
const otherTarget = { directory: '/other', sessionKey: 'session-1' };
useInlineCommentDraftStore.getState().addDraft(target, selection);
useInlineCommentDraftStore.getState().addDraft(otherTarget, { ...selection, code: 'other' });
useInlineCommentDraftStore.getState().clearDrafts({ ...target, directory: '/repo/' });
expect(useInlineCommentDraftStore.getState().getDrafts(target)).toEqual([]);
expect(useInlineCommentDraftStore.getState().getDrafts(otherTarget)).toHaveLength(1);
});
test('returns a stable empty snapshot for absent draft buckets', () => {
const first = useInlineCommentDraftStore.getState().getDrafts(target);
const second = useInlineCommentDraftStore.getState().getDrafts(target);
expect(first).toBe(second);
});
test('updates one draft without serializing the complete envelope on the mutation path', () => {
useInlineCommentDraftStore.getState().addDraft(target, selection);
const draft = useInlineCommentDraftStore.getState().getDrafts(target)[0];
const originalStringify = JSON.stringify;
let envelopeSerializations = 0;
JSON.stringify = ((value: unknown, ...rest: unknown[]) => {
if (value && typeof value === 'object' && 'drafts' in value && 'touchedAt' in value) {
envelopeSerializations += 1;
}
return originalStringify(value, ...(rest as [Parameters<typeof JSON.stringify>[1], Parameters<typeof JSON.stringify>[2]]));
}) as typeof JSON.stringify;
try {
useInlineCommentDraftStore.getState().updateDraft(target, draft.id, { text: 'edited' });
expect(envelopeSerializations).toBe(0);
expect(useInlineCommentDraftStore.getState().getDrafts(target)[0]?.text).toBe('edited');
} finally {
JSON.stringify = originalStringify;
}
});
});