dsh-better-chat/tests/reader-in-chat-wiring.test.ts

106 lines
6.1 KiB
TypeScript

import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { test } from 'vitest';
/**
* The reader-in-chat wiring is mostly declarative (slot id, priority, child
* seats, hidden official row), so the cheap regression guard is reading the
* entry modules. Executable ledger behaviour lives in `chat-view-seat.test.ts`;
* this file guards the two files' agreed contract.
*/
const source = await readFile(new URL('../src/client/reader-plugin.tsx', import.meta.url), 'utf8');
const seat = await readFile(new URL('../src/client/chat-view-seat.ts', import.meta.url), 'utf8');
const reader = await readFile(new URL('../src/client/Reader.tsx', import.meta.url), 'utf8');
const flow = await readFile(new URL('../src/client/ChoreographedFlow.tsx', import.meta.url), 'utf8');
const official = await readFile(new URL('../src/client/OfficialContent.tsx', import.meta.url), 'utf8');
const blocks = await readFile(new URL('../src/client/Blocks.tsx', import.meta.url), 'utf8');
const readerCss = await readFile(new URL('../src/client/Reader.module.css', import.meta.url), 'utf8');
const markdown = await readFile(new URL('../src/client/markdown/MarkdownText.tsx', import.meta.url), 'utf8');
test('the reader occupies the built-in 对话 View at a shadowing priority', () => {
assert.match(source, /name: 'conversation\.view'/u);
assert.match(source, /id: CHAT_VIEW_ID/u);
assert.match(source, /priority: CHAT_VIEW_PRIORITY/u);
assert.match(source, /label: \(\) => '对话'/u);
assert.doesNotMatch(source, /id: 'reader'/u, 'the old 阅读 tab must not come back');
assert.doesNotMatch(source, /installReaderEntry/u, 'no forced View switch on open');
});
test('the official ChatView row stays live; only its tab id is hidden', () => {
assert.match(source, /retireHostedChatView/u);
assert.match(source, /yield retireHostedChatView\(/u);
assert.match(source, /yield installOfficialSlots\(ctx\)/u);
assert.match(seat, /entry\.options\.id = undefined/u);
assert.doesNotMatch(seat, /rows\.splice/u);
assert.match(source, /yield hideDuplicateChatTabs\(document\)/u);
assert.match(readerCss, /data-better-chat-duplicate-tab[^}]+display: none/u);
assert.match(seat, /ACTIVE\.set\(ledger, restore\)/u);
});
test('the reader bubble and actions stay right-aligned under the user message', () => {
assert.match(readerCss, /\.userCluster > \[data-slot\] > :not\(\.user, \.userImages, \.userActions\)/u);
assert.match(readerCss, /\.userActions \{[^}]*align-self: flex-end;[^}]*width: fit-content/u);
assert.doesNotMatch(readerCss, /\.userCluster > \[data-slot\] > \*/u);
});
test('reasoning bodies render through compact Markdown', () => {
assert.match(blocks, /data-reader-text-kind="reasoning"/u);
assert.match(blocks, /variant="compact"/u);
assert.match(markdown, /variant\?: 'body' \| 'compact'/u);
});
test('the optional user-image slot has a low-priority renderer', () => {
assert.match(source, /name: 'conversation\.chat\.user\.images',\s*priority: 1000,[\s\S]*?UserImageFallback/u);
assert.match(blocks, /loadImage=\{loadImage\}/u);
assert.match(blocks, /src=\{image\.preview\.url\}/u);
});
test('short thoughts, tools and folded process summaries keep visible cards', () => {
assert.doesNotMatch(readerCss, /\.reasonCard\[data-overflow=false\][^{}]*\{[^}]*background:\s*transparent/u);
assert.match(readerCss, /\.toolActivity, \.processCard \{[^}]*border:/u);
assert.match(readerCss, /\.disclosureButton:has\(\[data-reader-fold-summary\]\)[^{}]*\{[^}]*border:/u);
});
test('completed timing and record summary share one measured status lane', () => {
assert.match(reader, /<StickyLane kind="status"[^>]*>[\s\S]*?<GroupStatus[\s\S]*?<ClosedProcessSummary[\s\S]*?<\/StickyLane>/u);
assert.match(readerCss, /\.closedProcessSummary\.summaryRow \{[^}]*flex-wrap: nowrap/u);
});
test('process summary wraps between labels and keeps the last counter visible', () => {
const rules = [...readerCss.matchAll(/^\.foldSummary \{([^}]*)\}/gmu)].map(match => match[1]);
assert.equal(rules.length, 1);
assert.match(rules[0] ?? '', /flex-wrap: wrap/u);
assert.doesNotMatch(rules[0] ?? '', /overflow: hidden|text-overflow/u);
assert.match(readerCss, /\.foldSummaryPart \{[^}]*white-space: nowrap/u);
});
test('tool rendering reads a snapshot that survives the plugin context', () => {
assert.match(source, /const officialHostHome = createSnapshotStore<string \| undefined>\(undefined\)/u);
assert.match(source, /ctx\.on\('connection\/reset', updateHome\)/u);
assert.doesNotMatch(source, /getSnapshot:\s*\(\)\s*=>\s*ctx\.remote/u);
assert.match(official, /useSyncExternalStore\(official\.officialHostHome\.subscribe, official\.officialHostHome\.getSnapshot\)/u);
});
test('live summaries and visible work share a frame that is removed when folded', () => {
assert.match(flow, /row.kind === 'summary'\) return \{ row, key: row.key, hidden: false, process: true \}/u);
assert.match(flow, /hidden, process: processOpen && process/u);
assert.match(readerCss, /\.processViewport \{[^}]*max-height: min\(320px, 45vh\)/u);
assert.match(readerCss, /\.processScroll \{[^}]*overflow: auto/u);
assert.match(reader, /const expanded = !autoFold \|\| holdingSelection \|\| processExpanded\(expansionChoice, boundary\)/u);
});
test('all Reader attachment previews delegate the full reference to the host loader', () => {
assert.match(reader, /loadImage: props\.officialImageLoader/u);
assert.match(source, /uiConversation\.imageUrl\(sessionId, attachment\)/u);
assert.match(blocks, /loadImage\(attachment\)/u);
assert.doesNotMatch(source, /readAttachment\(attachment\.attachmentId\)/u);
});
test('the status lane joins the visible process frame and the execution request folds with it', () => {
assert.match(reader, /status=\{\(showStatusLane \|\| showClosedSummary\) && <StickyLane/u);
assert.match(flow, /groupProcessRows\(status \? \[header, \.\.\.cells\] : cells\)/u);
assert.match(flow, /const hidden = \(trigger && !processOpen\)/u);
assert.match(flow, /data-reader-process-frame>[\s\S]*?\{status\}[\s\S]*?data-reader-process-scroll/u);
assert.match(readerCss, /\[data-flow-process\]\[data-flow-summary\] \.disclosureButton \{[^}]*border: 0/u);
});