| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355 |
- import { act, createElement, type ReactElement } from "react";
- import { createRoot, type Root } from "react-dom/client";
- import { FileClockIcon } from "lucide-react";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- TID_V4_EDIT,
- TID_V4_EDIT_ATTACHMENT_REMOVE,
- TID_V4_EDIT_REWIND_WORKSPACE,
- TID_V4_FEEDBACK_DISLIKE,
- TID_V4_FEEDBACK_LIKE,
- testId,
- } from "@zcode/shared";
- import type { AssistantTextRow, ReasoningRow, UserInputRow } from "@zcode/shared/zcode-protocol-v4";
- import type { CodeCommentComposerAttachment } from "@/lib/codeCommentContext.js";
- import { DEFAULT_CODE_PREVIEW_SETTINGS } from "@/lib/codePreviewSettings.js";
- import type { ConversationSelectionReference } from "@/lib/conversationSelectionReference.js";
- import type { PptxElementReference } from "@/lib/pptxElementReference.js";
- import {
- buildPromptWithWebElementContexts,
- type WebElementContextComposerAttachment,
- } from "@/lib/webElementContext.js";
- import { ConversationRowView } from "@/v4/ConversationRowView.js";
- import type { ConversationRowRenderContext } from "@/v4/conversationRowContext.js";
- import { serializeComposerPromptContexts } from "@/v4/composer/composerPromptContexts.js";
- import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
- const {
- attachmentPropsById,
- attachmentRemovePropsByTestId,
- chatPromptEditorProps,
- fileRewindDialogProps,
- hoverCardContentProps,
- imagePreviewDialogProps,
- messageActionPropsByTestId,
- messageResponseProps,
- reasoningTriggerProps,
- } = vi.hoisted(() => ({
- attachmentPropsById: new Map<string, Record<string, unknown>>(),
- attachmentRemovePropsByTestId: new Map<string, Record<string, unknown>>(),
- chatPromptEditorProps: [] as Array<Record<string, unknown>>,
- fileRewindDialogProps: [] as Array<Record<string, unknown>>,
- hoverCardContentProps: [] as Array<Record<string, unknown>>,
- imagePreviewDialogProps: [] as Array<Record<string, unknown>>,
- messageActionPropsByTestId: new Map<string, Record<string, unknown>>(),
- messageResponseProps: [] as Array<Record<string, unknown>>,
- reasoningTriggerProps: [] as Array<Record<string, unknown>>,
- }));
- vi.mock("@/components/ai-elements/image-preview-dialog.js", () => ({
- ImagePreviewDialog: (props: Record<string, unknown>) => {
- imagePreviewDialogProps.push(props);
- return null;
- },
- }));
- vi.mock("@/prompt-editor/ChatPromptEditor.js", async () => {
- const React = await import("react");
- return {
- ChatPromptEditor: (props: Record<string, unknown>) => {
- chatPromptEditorProps.push(props);
- return React.createElement(
- "form",
- { "data-testid": "mock-v4-user-edit-editor" },
- props.topContent as React.ReactNode,
- );
- },
- };
- });
- vi.mock("@/v4/ConversationFileRewindDialog.js", () => ({
- ConversationFileRewindDialog: (props: Record<string, unknown>) => {
- fileRewindDialogProps.push(props);
- return null;
- },
- }));
- vi.mock("@/components/ai-elements/attachments.js", async () => {
- const React = await import("react");
- const RemoveContext = React.createContext<(() => void) | undefined>(undefined);
- const Passthrough = ({ children, ...props }: { children?: React.ReactNode }) =>
- React.createElement("div", props, children);
- const Attachment = ({
- children,
- onRemove,
- data,
- variant: _variant,
- ...props
- }: {
- children?: React.ReactNode;
- onRemove?: () => void;
- data?: { id?: string };
- variant?: string;
- }) => {
- if (data?.id) {
- attachmentPropsById.set(data.id, {
- ...props,
- data,
- onRemove,
- variant: _variant,
- });
- }
- return React.createElement(
- RemoveContext.Provider,
- { value: onRemove },
- React.createElement("div", props, children),
- );
- };
- const AttachmentRemove = ({ alwaysVisible, label, ...props }: Record<string, unknown>) => {
- const onRemove = React.useContext(RemoveContext);
- const testIdValue = props["data-testid"];
- if (typeof testIdValue === "string") {
- attachmentRemovePropsByTestId.set(testIdValue, {
- ...props,
- alwaysVisible,
- label,
- onClick: onRemove,
- });
- }
- return React.createElement("button", { ...props, onClick: onRemove }, label);
- };
- const Trigger = ({ children }: { children?: React.ReactNode }) =>
- React.createElement(React.Fragment, null, children);
- const HoverCard = ({ children }: { children?: React.ReactNode }) =>
- React.createElement(React.Fragment, null, children);
- const HoverCardContent = ({ children, ...props }: { children?: React.ReactNode }) => {
- hoverCardContentProps.push(props);
- return React.createElement("div", props, children);
- };
- const Span = (props: Record<string, unknown>) => React.createElement("span", props);
- return {
- Attachment,
- AttachmentHoverCard: HoverCard,
- AttachmentHoverCardContent: HoverCardContent,
- AttachmentHoverCardTrigger: Trigger,
- AttachmentInfo: Span,
- AttachmentPreview: Span,
- AttachmentRemove,
- Attachments: Passthrough,
- };
- });
- vi.mock("@/components/ai-elements/message.js", async () => {
- const React = await import("react");
- return {
- MessageAction: ({
- children,
- label,
- tooltip,
- ...props
- }: {
- children?: React.ReactNode;
- label?: string;
- tooltip?: string;
- } & Record<string, unknown>) => {
- const testIdValue = props["data-testid"];
- if (typeof testIdValue === "string") {
- messageActionPropsByTestId.set(testIdValue, {
- ...props,
- label,
- tooltip,
- });
- }
- return React.createElement(
- "button",
- props,
- children,
- React.createElement("span", null, label ?? tooltip),
- );
- },
- MessageActions: ({ children, ...props }: { children?: React.ReactNode }) =>
- React.createElement("div", props, children),
- MessageResponse: ({
- children,
- ...props
- }: {
- children?: React.ReactNode;
- } & Record<string, unknown>) => {
- messageResponseProps.push(props);
- return React.createElement("div", null, children);
- },
- };
- });
- vi.mock("@/components/ai-elements/reasoning.js", async () => {
- const React = await import("react");
- const Passthrough = ({ children }: { children?: React.ReactNode }) =>
- React.createElement("div", null, children);
- return {
- Reasoning: Passthrough,
- ReasoningContent: Passthrough,
- ReasoningTrigger: (props: Record<string, unknown>) => {
- reasoningTriggerProps.push(props);
- return React.createElement("div", null, props.children as React.ReactNode);
- },
- };
- });
- vi.mock("@/ToolCallBlocks.js", async () => {
- const React = await import("react");
- return {
- ToolCallBlock: () => React.createElement("div", { "data-testid": "mock-tool-call" }),
- };
- });
- function createMinimalElement(ownerDocument: Document, tagName = "div") {
- const element = {
- addEventListener: () => {},
- appendChild: (child: Node) => {
- Object.assign(child, { parentNode: element });
- element.childNodes.push(child);
- return child;
- },
- childNodes: [] as Node[],
- getAttribute: (name: string) => element.attributes.get(name) ?? null,
- insertBefore: (child: Node, beforeChild?: Node | null) => {
- Object.assign(child, { parentNode: element });
- const beforeIndex = beforeChild ? element.childNodes.indexOf(beforeChild) : -1;
- if (beforeIndex >= 0) {
- element.childNodes.splice(beforeIndex, 0, child);
- } else {
- element.childNodes.push(child);
- }
- return child;
- },
- attributes: new Map<string, string>(),
- nodeName: tagName.toUpperCase(),
- nodeType: 1,
- ownerDocument,
- parentNode: null as Node | null,
- removeAttribute: (name: string) => {
- element.attributes.delete(name);
- },
- removeChild: (child: Node) => {
- element.childNodes = element.childNodes.filter((item) => item !== child);
- Object.assign(child, { parentNode: null });
- return child;
- },
- removeEventListener: () => {},
- setAttribute: (name: string, value: string) => {
- element.attributes.set(name, String(value));
- },
- style: {},
- tagName: tagName.toUpperCase(),
- };
- return element as unknown as Element;
- }
- function installMinimalDom() {
- const documentMock = {
- addEventListener: () => {},
- createComment: (nodeValue: string) => ({
- nodeType: 8,
- nodeValue,
- parentNode: null,
- }),
- createElement: (tagName: string) =>
- createMinimalElement(documentMock as unknown as Document, tagName),
- createElementNS: (_namespace: string, tagName: string) =>
- createMinimalElement(documentMock as unknown as Document, tagName),
- createTextNode: (nodeValue: string) => ({
- nodeType: 3,
- nodeValue,
- parentNode: null,
- }),
- nodeType: 9,
- removeEventListener: () => {},
- } as unknown as Document & { defaultView?: unknown };
- const windowMock = {
- addEventListener: () => {},
- document: documentMock,
- HTMLIFrameElement: function HTMLIFrameElement() {},
- HTMLElement: function HTMLElement() {},
- Node: function Node() {},
- removeEventListener: () => {},
- requestAnimationFrame: (callback: FrameRequestCallback) => {
- callback(0);
- return 0;
- },
- };
- documentMock.defaultView = windowMock;
- Object.defineProperty(globalThis, "document", {
- configurable: true,
- value: documentMock,
- });
- Object.defineProperty(globalThis, "window", {
- configurable: true,
- value: windowMock,
- });
- Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
- configurable: true,
- value: true,
- });
- return createMinimalElement(documentMock, "div");
- }
- function collectText(node: unknown): string {
- if (!node || typeof node !== "object") return "";
- const candidate = node as {
- childNodes?: unknown[];
- nodeValue?: string;
- textContent?: string;
- };
- if (typeof candidate.nodeValue === "string") return candidate.nodeValue;
- if (candidate.childNodes && candidate.childNodes.length > 0) {
- return candidate.childNodes.map((child) => collectText(child)).join("");
- }
- return typeof candidate.textContent === "string" ? candidate.textContent : "";
- }
- function collectAttributeValues(node: unknown, attributeName: string): string[] {
- if (!node || typeof node !== "object") return [];
- const candidate = node as {
- attributes?: Map<string, string>;
- childNodes?: unknown[];
- getAttribute?: (name: string) => string | null;
- };
- const ownValue =
- typeof candidate.getAttribute === "function"
- ? candidate.getAttribute(attributeName)
- : (candidate.attributes?.get(attributeName) ?? null);
- const childValues =
- candidate.childNodes?.flatMap((child) => collectAttributeValues(child, attributeName)) ?? [];
- return ownValue ? [ownValue, ...childValues] : childValues;
- }
- function findElementByAttribute(
- node: unknown,
- attributeName: string,
- attributeValue: string,
- ): { childNodes?: unknown[]; parentNode?: unknown } | null {
- if (!node || typeof node !== "object") return null;
- const candidate = node as {
- childNodes?: unknown[];
- getAttribute?: (name: string) => string | null;
- parentNode?: unknown;
- };
- if (candidate.getAttribute?.(attributeName) === attributeValue) return candidate;
- for (const child of candidate.childNodes ?? []) {
- const match = findElementByAttribute(child, attributeName, attributeValue);
- if (match) return match;
- }
- return null;
- }
- const rowContext: ConversationRowRenderContext = {
- codePreviewSettings: DEFAULT_CODE_PREVIEW_SETTINGS,
- messageStreamShowReasoning: true,
- sessionId: "session-1",
- theme: "system",
- workspaceIdentity: "remote:workspace",
- workspacePath: "/workspace",
- };
- function userInputRow(overrides: Partial<UserInputRow> = {}): UserInputRow {
- return {
- rowId: 42,
- entityId: "message-user-42",
- turnId: "turn-1",
- createdAt: 1_700_000_000_000,
- createdAtSeq: 1,
- kind: "userInput",
- origin: "realUser",
- text: "我刚刚说了啥",
- attachments: [
- {
- bytes: 12,
- fileName: "note.txt",
- mime: "text/plain",
- ref: "/workspace/note.txt",
- },
- ],
- ...overrides,
- };
- }
- function assistantTextRow(overrides: Partial<AssistantTextRow> = {}): AssistantTextRow {
- return {
- rowId: 43,
- turnId: "turn-1",
- createdAt: 1_700_000_000_001,
- createdAtSeq: 2,
- kind: "assistantText",
- state: "streaming",
- text: "正在输出",
- ...overrides,
- };
- }
- function reasoningRow(overrides: Partial<ReasoningRow> = {}): ReasoningRow {
- return {
- rowId: 44,
- turnId: "turn-1",
- createdAt: 1_700_000_000_002,
- createdAtSeq: 3,
- kind: "reasoning",
- state: "streaming",
- text: "正在检查摘要接线",
- ...overrides,
- };
- }
- afterEach(() => {
- attachmentPropsById.clear();
- attachmentRemovePropsByTestId.clear();
- chatPromptEditorProps.length = 0;
- fileRewindDialogProps.length = 0;
- hoverCardContentProps.length = 0;
- imagePreviewDialogProps.length = 0;
- messageActionPropsByTestId.clear();
- messageResponseProps.length = 0;
- reasoningTriggerProps.length = 0;
- vi.restoreAllMocks();
- delete (globalThis as { document?: unknown }).document;
- delete (globalThis as { window?: unknown }).window;
- delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
- });
- describe("ConversationRowView reasoning summary", () => {
- it("opens topic history from the owning transport only after clicking the attachment", async () => {
- const readAttachment = vi.fn(async () => ({
- bytes: new TextEncoder().encode("Alice | ou_a\nOriginal history"),
- mediaType: "text/plain",
- }));
- const row = userInputRow({
- attachments: [
- {
- ref: "zcode-artifact://history",
- fileName: "topic-history.txt",
- mime: "text/plain",
- bytes: 30,
- sourceKind: "topic-history",
- messageCount: 2,
- },
- ],
- });
- const root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, { context: { ...rowContext, readAttachment }, row }),
- ),
- );
- });
- expect(readAttachment).not.toHaveBeenCalled();
- const props = attachmentPropsById.get(`${row.rowId}-0`)!;
- expect(props.onOpen).toBeTypeOf("function");
- await act(async () => {
- await (props.onOpen as () => Promise<void>)();
- });
- expect(readAttachment).toHaveBeenCalledWith(
- expect.objectContaining({ sessionId: "session-1", ref: "zcode-artifact://history" }),
- );
- await act(async () => root.unmount());
- });
- it("forwards streaming reasoning text to the collapsed summary trigger", async () => {
- const root: Root = createRoot(installMinimalDom());
- const row = reasoningRow();
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, { context: rowContext, row }),
- ),
- );
- });
- expect(reasoningTriggerProps).toHaveLength(1);
- expect(reasoningTriggerProps[0]?.streamingText).toBe(row.text);
- await act(async () => root.unmount());
- });
- });
- describe("ConversationRowView user query editing", () => {
- it.each([false, true])(
- "renders topic originals separately (mobile=%s)",
- async (compactForRemoteControl) => {
- const container = installMinimalDom();
- const root = createRoot(container);
- const row = userInputRow({
- text: "combined execution payload",
- botGroupSource: {
- provider: "feishu",
- botId: "bot",
- chatId: "chat",
- threadId: "topic",
- senderId: "b",
- senderName: "乙",
- messageId: "b",
- messages: [
- {
- messageId: "a",
- senderId: "a",
- senderName: "甲",
- text: "第一条需求",
- attachmentIndexes: [],
- conversationQuotes: [{ text: "根原文" }],
- },
- {
- messageId: "b",
- senderId: "b",
- senderName: "乙",
- text: "第二条补充",
- attachmentIndexes: [],
- },
- ],
- },
- });
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: { ...rowContext, compactForRemoteControl },
- row,
- }),
- ),
- );
- });
- const first = findElementByAttribute(container, "data-topic-message-id", "a");
- const second = findElementByAttribute(container, "data-topic-message-id", "b");
- expect(first).not.toBeNull();
- expect(second).not.toBeNull();
- expect(collectText(first)).toContain("第一条需求");
- expect(collectText(first)).toContain("甲");
- expect(collectText(second)).toContain("第二条补充");
- expect(collectText(second)).toContain("乙");
- expect(findElementByAttribute(first, "data-v4-user-input-quotes", "true")).not.toBeNull();
- expect(findElementByAttribute(second, "data-v4-user-input-quotes", "true")).toBeNull();
- expect(collectText(container)).not.toContain("combined execution payload");
- await act(async () => root.unmount());
- },
- );
- it("orders group sender, quote and attachments without an invented message bubble", async () => {
- const container = installMinimalDom();
- const root = createRoot(container);
- const row = userInputRow({
- text: "",
- conversationQuotes: [
- {
- text: "actual quoted request",
- senderName: "Ryan Bot",
- senderId: "ou_bot",
- sentAt: "2026-09-09T10:32:44+08:00",
- },
- ],
- botGroupSource: {
- provider: "feishu",
- botId: "bot",
- chatId: "chat",
- senderId: "member",
- senderName: "测试成员",
- messageId: "message",
- },
- });
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, { context: rowContext, row }),
- ),
- );
- });
- const source = findElementByAttribute(container, "data-v4-user-input-source", "true");
- const area = findElementByAttribute(container, "data-v4-user-input-attachments", "true");
- const quotes = findElementByAttribute(container, "data-v4-user-input-quotes", "true");
- expect(source).not.toBeNull();
- expect(quotes).not.toBeNull();
- const siblings = [...source!.parentNode.childNodes];
- expect(siblings.indexOf(source)).toBeLessThan(siblings.indexOf(area));
- expect([...area!.childNodes][0]).toBe(quotes);
- expect(findElementByAttribute(container, "data-v4-user-input-bubble", "true")).toBeNull();
- await act(async () => root.unmount());
- });
- it.each([
- {
- label: "scheduled automation prompt",
- sourceCommandId: "automation-1:1700000000000",
- visible: true,
- },
- {
- label: "manual automation prompt",
- sourceCommandId: "automation-1:manual:run-1",
- visible: true,
- },
- { label: "ordinary prompt", sourceCommandId: "command-1", visible: false },
- ])("shows the scheduled-task origin only for $label", async ({ sourceCommandId, visible }) => {
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- row: userInputRow({ sourceCommandId }),
- }),
- ),
- );
- });
- const origin = findElementByAttribute(
- container,
- "data-v4-user-input-automation-origin",
- "schedule",
- );
- expect(Boolean(origin)).toBe(visible);
- if (origin) {
- expect(collectText(origin)).toBe("由定时任务发送");
- expect(findElementByAttribute(origin, "data-cron-task-icon", "true")).not.toBeNull();
- }
- await act(async () => root.unmount());
- });
- it.each([
- {
- label: "attachments and text",
- text: "解释附件",
- attachments: userInputRow().attachments,
- bubble: true,
- },
- {
- label: "attachments only",
- text: "",
- attachments: userInputRow().attachments,
- bubble: false,
- },
- { label: "text only", text: "只有文字", attachments: [], bubble: true },
- ])("renders the user message layout for $label", async ({ text, attachments, bubble }) => {
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- row: userInputRow({ text, attachments }),
- }),
- ),
- );
- });
- const attachmentArea = findElementByAttribute(
- container,
- "data-v4-user-input-attachments",
- "true",
- );
- const messageBubble = findElementByAttribute(container, "data-v4-user-input-bubble", "true");
- expect(Boolean(attachmentArea)).toBe(attachments.length > 0);
- expect(Boolean(messageBubble)).toBe(bubble);
- if (messageBubble) {
- const bubbleClasses = collectAttributeValues(messageBubble, "class").join(" ");
- const bubbleClassTokens = bubbleClasses.split(/\s+/);
- expect(bubbleClasses).toContain("max-w-full");
- expect(bubbleClasses).toContain("@min-[624px]/conversation:max-w-xl");
- expect(bubbleClassTokens).not.toContain("w-fit");
- expect(bubbleClassTokens).not.toContain("w-full");
- expect(bubbleClassTokens).not.toContain("@md/conversation:max-w-xl");
- expect(bubbleClassTokens).not.toContain("@xl/conversation:max-w-xl");
- expect(bubbleClassTokens).not.toContain("@md/conversation:w-full");
- }
- if (attachmentArea && messageBubble) {
- expect(attachmentArea.parentNode).toBe(messageBubble.parentNode);
- const siblings = attachmentArea.parentNode as {
- childNodes?: unknown[];
- };
- expect(siblings.childNodes?.indexOf(attachmentArea)).toBeLessThan(
- siblings.childNodes?.indexOf(messageBubble) ?? -1,
- );
- }
- await act(async () => root.unmount());
- });
- it("renders sent attachments as surface pills with a file icon and filename", async () => {
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- row: userInputRow({ text: "" }),
- }),
- ),
- );
- });
- const pill = findElementByAttribute(container, "data-v4-user-input-attachment-pill", "true");
- expect(pill).not.toBeNull();
- const classes = collectAttributeValues(pill, "class").join(" ");
- expect(classes).toContain("rounded-full");
- expect(classes).toContain("bg-surface");
- expect(collectText(pill)).toBe("note.txt");
- expect(collectAttributeValues(pill, "src").some((src) => src.endsWith(".svg"))).toBe(true);
- await act(async () => root.unmount());
- });
- it("renders image thumbnails above ordered file and context pills", async () => {
- const context: WebElementContextComposerAttachment = {
- capturedAt: 1_700_000_000_000,
- id: "element-1",
- pageTitle: "Docs",
- pageUrl: "https://example.test/docs",
- tagName: "button",
- text: "Submit",
- workspacePath: "/workspace",
- };
- const readAttachment = vi.fn().mockResolvedValue({
- bytes: new Uint8Array([137, 80, 78, 71]),
- mediaType: "image/png",
- });
- vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:sent-thumbnail");
- vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
- const comment: CodeCommentComposerAttachment = {
- id: "comment-1",
- workspacePath: "/workspace",
- sourcePath: "/workspace/app.ts",
- sourceTitle: "app.ts",
- selectedText: "const value = 1",
- comment: "检查这里",
- startLine: 1,
- endLine: 1,
- };
- const pptxElement: PptxElementReference = {
- id: "pptx-1",
- workspacePath: "/workspace",
- sourcePath: "/workspace/deck.pptx",
- sourceTitle: "deck.pptx",
- sourceFingerprint: `sha256:${"a".repeat(64)}`,
- slideIndex: 0,
- slidePart: "ppt/slides/slide1.xml",
- nodeId: "7",
- nodeName: "Title",
- nodeType: "shape",
- bounds: { x: 0, y: 0, width: 100, height: 30 },
- zIndex: 1,
- capturedAt: 1,
- };
- const selection: ConversationSelectionReference = {
- id: "selection-1",
- sourceSessionId: "source-session",
- sourceRowId: 8,
- contentType: "assistant",
- text: "selected answer",
- };
- const row = userInputRow({
- text: serializeComposerPromptContexts("", {
- codeComments: [comment],
- conversationSelections: [selection],
- pptxElements: [pptxElement],
- webElements: [context],
- }),
- attachments: [
- {
- ref: "/workspace/shot.png",
- fileName: "shot.png",
- mime: "image/png",
- bytes: 4,
- },
- {
- ref: "/workspace/demo.mov",
- fileName: "demo.mov",
- mime: "video/quicktime",
- bytes: 8,
- },
- {
- ref: "/workspace/note.txt",
- fileName: "note.txt",
- mime: "text/plain",
- bytes: 4,
- },
- ],
- });
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: { ...rowContext, readAttachment },
- row,
- }),
- ),
- );
- await Promise.resolve();
- await Promise.resolve();
- });
- const attachmentArea = findElementByAttribute(
- container,
- "data-v4-user-input-attachments",
- "true",
- );
- const imageRow = findElementByAttribute(
- attachmentArea,
- "data-v4-user-input-media-attachments",
- "true",
- );
- const pillRow = findElementByAttribute(
- attachmentArea,
- "data-v4-user-input-attachment-pills",
- "true",
- );
- const image = findElementByAttribute(imageRow, "data-v4-user-input-media-attachment", "true");
- // findElementByAttribute 只返回首个匹配;media 行内还有视频卡,按子节点序取第二个。
- const mediaCards = [...(imageRow as { childNodes?: unknown[] }).childNodes!].filter(
- (child) =>
- typeof child === "object" &&
- child !== null &&
- "getAttribute" in child &&
- (child as { getAttribute(name: string): string | null }).getAttribute(
- "data-v4-user-input-media-attachment",
- ) === "true",
- );
- const video = mediaCards[1] ?? null;
- const file = findElementByAttribute(pillRow, "data-v4-user-input-attachment-pill", "true");
- const web = findElementByAttribute(pillRow, "aria-label", "1 个网页元素");
- const codeComment = findElementByAttribute(pillRow, "aria-label", "1 条评论");
- const pptx = findElementByAttribute(pillRow, "aria-label", "1 个幻灯片元素");
- const conversation = findElementByAttribute(pillRow, "aria-label", "1 条对话引用");
- expect(imageRow?.parentNode).toBe(attachmentArea);
- expect(pillRow?.parentNode).toBe(attachmentArea);
- expect(image?.parentNode).toBe(imageRow);
- expect(video?.parentNode).toBe(imageRow);
- expect(file?.parentNode).toBe(pillRow);
- // 媒体行内按添加顺序混排图片与视频;视频不进入文件 pill 行。
- const mediaChildren = (imageRow as { childNodes?: unknown[] }).childNodes ?? [];
- expect(mediaChildren.indexOf(image)).toBeLessThan(mediaChildren.indexOf(video));
- expect(web?.parentNode).toBe(pillRow);
- const areaChildren = (attachmentArea as { childNodes?: unknown[] }).childNodes ?? [];
- expect(areaChildren.indexOf(imageRow)).toBeLessThan(areaChildren.indexOf(pillRow));
- const pillChildren = (pillRow as { childNodes?: unknown[] }).childNodes ?? [];
- expect([
- pillChildren.indexOf(file),
- pillChildren.indexOf(codeComment),
- pillChildren.indexOf(web),
- pillChildren.indexOf(pptx),
- pillChildren.indexOf(conversation),
- ]).toEqual(
- [...pillChildren]
- .map((child, index) => ({ child, index }))
- .filter(({ child }) => [file, codeComment, web, pptx, conversation].includes(child))
- .map(({ index }) => index),
- );
- expect(attachmentPropsById.get("42-0")).toMatchObject({
- variant: "grid",
- data: { url: "blob:sent-thumbnail" },
- });
- // 可见消息行会读取已发送视频,卡片复用 Blob URL 由无控件 video 展示首帧。
- expect(attachmentPropsById.get("42-1")).toMatchObject({
- variant: "grid",
- data: { url: "blob:sent-thumbnail", filename: "demo.mov" },
- });
- expect(readAttachment).toHaveBeenCalledWith({
- sessionId: "session-1",
- ref: "/workspace/demo.mov",
- mediaType: "video/quicktime",
- target: { rowId: 42, entityId: "message-user-42" },
- attachmentIndex: 1,
- signal: expect.any(AbortSignal),
- });
- const imageClasses = collectAttributeValues(image, "class").join(" ");
- expect(imageClasses).toContain("size-20");
- expect(imageClasses).toContain("rounded-xl");
- expect(imageClasses).not.toMatch(/(?:^|\s)border(?:\s|$)/u);
- expect(imageClasses).toContain("after:border");
- expect(imageClasses).toContain("after:pointer-events-none");
- const videoClasses = collectAttributeValues(video, "class").join(" ");
- expect(videoClasses).toContain("size-20");
- expect(videoClasses).toContain("rounded-xl");
- await act(async () => root.unmount());
- });
- it("opens sent images with the shared Markdown preview gallery and leaves non-images static", async () => {
- const readAttachment = vi.fn().mockResolvedValue({
- bytes: new Uint8Array([137, 80, 78, 71]),
- mediaType: "image/png",
- });
- const createObjectUrl = vi
- .spyOn(URL, "createObjectURL")
- .mockReturnValueOnce("blob:first-preview")
- .mockReturnValueOnce("blob:second-preview");
- const revokeObjectUrl = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
- const row = userInputRow({
- attachments: [
- {
- ref: "/workspace/first.png",
- fileName: "first.png",
- mime: "image/png",
- bytes: 4,
- },
- {
- ref: "/workspace/second.png",
- fileName: "second.png",
- mime: "image/png",
- bytes: 4,
- },
- {
- ref: "/workspace/note.txt",
- fileName: "note.txt",
- mime: "text/plain",
- bytes: 4,
- },
- ],
- });
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: { ...rowContext, readAttachment },
- row,
- }),
- ),
- );
- });
- expect(attachmentPropsById.get("42-0")?.onOpen).toBeTypeOf("function");
- expect(attachmentPropsById.get("42-0")?.openLabel).toBe("打开图片预览");
- expect(attachmentPropsById.get("42-1")?.onOpen).toBeTypeOf("function");
- expect(attachmentPropsById.get("42-2")?.onOpen).toBeUndefined();
- const openPreview = attachmentPropsById.get("42-1")?.onOpen;
- expect(openPreview).toBeTypeOf("function");
- await act(async () => {
- (openPreview as () => void)();
- await Promise.resolve();
- await Promise.resolve();
- });
- expect(readAttachment).toHaveBeenCalledTimes(2);
- expect(createObjectUrl).toHaveBeenCalledTimes(2);
- expect(imagePreviewDialogProps.at(-1)).toMatchObject({
- open: true,
- initialIndex: 1,
- items: [
- { alt: "first.png", filename: "first.png", src: "blob:first-preview" },
- {
- alt: "second.png",
- filename: "second.png",
- src: "blob:second-preview",
- },
- ],
- });
- const onPreviewOpenChange = imagePreviewDialogProps.at(-1)?.onOpenChange;
- expect(onPreviewOpenChange).toBeTypeOf("function");
- await act(async () => {
- (onPreviewOpenChange as (open: boolean) => void)(false);
- });
- await act(async () => root.unmount());
- expect(revokeObjectUrl).toHaveBeenCalledWith("blob:first-preview");
- expect(revokeObjectUrl).toHaveBeenCalledWith("blob:second-preview");
- });
- it("opens sent videos in the shared mixed media gallery by exact row attachment", async () => {
- const readAttachment = vi.fn().mockResolvedValue({
- bytes: new Uint8Array([0, 0, 0, 24]),
- mediaType: "video/quicktime",
- });
- const createObjectUrl = vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:sent-preview");
- const revokeObjectUrl = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
- const row = userInputRow({
- attachments: [
- {
- ref: "/workspace/shot.png",
- fileName: "shot.png",
- mime: "image/png",
- bytes: 4,
- },
- {
- ref: "/workspace/demo.mov",
- fileName: "demo.mov",
- mime: "video/quicktime",
- bytes: 4,
- },
- {
- ref: "/workspace/note.txt",
- fileName: "note.txt",
- mime: "text/plain",
- bytes: 4,
- },
- ],
- });
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: { ...rowContext, readAttachment },
- row,
- }),
- ),
- );
- });
- expect(attachmentPropsById.get("42-0")?.onOpen).toBeTypeOf("function");
- expect(attachmentPropsById.get("42-0")?.openLabel).toBe("打开图片预览");
- expect(attachmentPropsById.get("42-1")?.onOpen).toBeTypeOf("function");
- expect(attachmentPropsById.get("42-1")?.openLabel).toBe("打开视频预览");
- expect(attachmentPropsById.get("42-2")?.onOpen).toBeUndefined();
- const openPreview = attachmentPropsById.get("42-1")?.onOpen;
- expect(openPreview).toBeTypeOf("function");
- await act(async () => {
- (openPreview as () => void)();
- await Promise.resolve();
- await Promise.resolve();
- });
- expect(readAttachment).toHaveBeenCalledWith({
- sessionId: "session-1",
- ref: "/workspace/demo.mov",
- mediaType: "video/quicktime",
- target: { rowId: 42, entityId: "message-user-42" },
- attachmentIndex: 1,
- signal: expect.any(AbortSignal),
- });
- // 挂载时 image/video 各读取一次;点击 video 复用首帧 Blob,不重复读取。
- expect(createObjectUrl).toHaveBeenCalledTimes(2);
- expect(imagePreviewDialogProps.at(-1)).toMatchObject({
- open: true,
- initialIndex: 1,
- items: [
- {
- alt: "shot.png",
- filename: "shot.png",
- mediaType: "image/png",
- src: expect.stringMatching(/^blob:/u),
- },
- {
- alt: "demo.mov",
- filename: "demo.mov",
- mediaType: "video/quicktime",
- src: "blob:sent-preview",
- },
- ],
- });
- const onPreviewOpenChange = imagePreviewDialogProps.at(-1)?.onOpenChange;
- expect(onPreviewOpenChange).toBeTypeOf("function");
- await act(async () => {
- (onPreviewOpenChange as (open: boolean) => void)(false);
- });
- // gallery 关闭后首帧仍在消息卡片使用,直到消息行卸载才统一释放。
- expect(revokeObjectUrl).not.toHaveBeenCalledWith("blob:sent-preview");
- await act(async () => root.unmount());
- expect(revokeObjectUrl).toHaveBeenCalledWith("blob:sent-preview");
- });
- it("loads a video when mixed-media gallery navigation selects it", async () => {
- let resolveVideoPreview!: (value: { bytes: Uint8Array; mediaType: string }) => void;
- let videoReads = 0;
- const readAttachment = vi.fn(({ ref }: { ref: string }) => {
- if (ref.endsWith("shot.png")) {
- return Promise.resolve({
- bytes: new Uint8Array([1, 2, 3]),
- mediaType: "image/png",
- });
- }
- videoReads += 1;
- if (videoReads === 1) {
- return Promise.reject(new Error("thumbnail prefetch failed"));
- }
- return new Promise<{ bytes: Uint8Array; mediaType: string }>((resolve) => {
- resolveVideoPreview = resolve;
- });
- });
- vi.spyOn(URL, "createObjectURL")
- .mockReturnValueOnce("blob:image-preview")
- .mockReturnValueOnce("blob:video-preview");
- vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
- const row = userInputRow({
- attachments: [
- {
- ref: "/workspace/shot.png",
- fileName: "shot.png",
- mime: "image/png",
- bytes: 3,
- },
- {
- ref: "/workspace/demo.mp4",
- fileName: "demo.mp4",
- mime: "video/mp4",
- bytes: 4,
- },
- ],
- });
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: { ...rowContext, readAttachment },
- row,
- }),
- ),
- );
- await Promise.resolve();
- await Promise.resolve();
- });
- const openImagePreview = attachmentPropsById.get("42-0")?.onOpen;
- expect(openImagePreview).toBeTypeOf("function");
- await act(async () => {
- (openImagePreview as () => void)();
- });
- const onActiveIndexChange = imagePreviewDialogProps.at(-1)?.onActiveIndexChange as
- | ((index: number) => void)
- | undefined;
- expect(onActiveIndexChange).toBeTypeOf("function");
- await act(async () => {
- onActiveIndexChange?.(1);
- await Promise.resolve();
- });
- expect(readAttachment).toHaveBeenLastCalledWith({
- sessionId: "session-1",
- ref: "/workspace/demo.mp4",
- mediaType: "video/mp4",
- target: { rowId: 42, entityId: "message-user-42" },
- attachmentIndex: 1,
- signal: expect.any(AbortSignal),
- });
- expect(imagePreviewDialogProps.at(-1)).toMatchObject({
- initialIndex: 1,
- items: [
- expect.objectContaining({ src: "blob:image-preview" }),
- expect.objectContaining({ loading: true, src: undefined }),
- ],
- });
- await act(async () => {
- resolveVideoPreview({
- bytes: new Uint8Array([0, 0, 0, 24]),
- mediaType: "video/mp4",
- });
- await Promise.resolve();
- await Promise.resolve();
- });
- expect(imagePreviewDialogProps.at(-1)).toMatchObject({
- items: [
- expect.objectContaining({ src: "blob:image-preview" }),
- expect.objectContaining({ loading: false, src: "blob:video-preview" }),
- ],
- });
- await act(async () => root.unmount());
- });
- it("opens a Desktop local media URL without creating or revoking a Blob URL", async () => {
- const readAttachment = vi.fn().mockResolvedValue({
- url: "zcode-media://local/desktop-video",
- mediaType: "video/mp4",
- });
- const createObjectUrl = vi.spyOn(URL, "createObjectURL");
- const revokeObjectUrl = vi.spyOn(URL, "revokeObjectURL");
- const row = userInputRow({
- attachments: [{ ref: "/tmp/demo.mp4", fileName: "demo.mp4", mime: "video/mp4", bytes: 1024 }],
- });
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: { ...rowContext, readAttachment },
- row,
- }),
- ),
- );
- await Promise.resolve();
- await Promise.resolve();
- });
- expect(readAttachment).toHaveBeenCalledWith({
- sessionId: "session-1",
- ref: "/tmp/demo.mp4",
- mediaType: "video/mp4",
- target: { rowId: row.rowId, entityId: row.entityId },
- attachmentIndex: 0,
- signal: expect.any(AbortSignal),
- });
- expect(attachmentPropsById.get(`${row.rowId}-0`)).toMatchObject({
- data: { url: "zcode-media://local/desktop-video" },
- });
- const openPreview = attachmentPropsById.get(`${row.rowId}-0`)?.onOpen;
- expect(openPreview).toBeTypeOf("function");
- await act(async () => {
- (openPreview as () => void)();
- await Promise.resolve();
- await Promise.resolve();
- });
- expect(imagePreviewDialogProps.at(-1)).toMatchObject({
- open: true,
- initialIndex: 0,
- items: [
- {
- alt: "demo.mp4",
- filename: "demo.mp4",
- mediaType: "video/mp4",
- src: "zcode-media://local/desktop-video",
- loading: false,
- error: false,
- },
- ],
- });
- expect(createObjectUrl).not.toHaveBeenCalled();
- const closePreview = imagePreviewDialogProps.at(-1)?.onOpenChange;
- expect(closePreview).toBeTypeOf("function");
- await act(async () => {
- (closePreview as (open: boolean) => void)(false);
- });
- expect(revokeObjectUrl).not.toHaveBeenCalled();
- await act(async () => root.unmount());
- });
- it("aborts an unfinished sent media read when the preview closes", async () => {
- let readSignal: AbortSignal | undefined;
- const readAttachment = vi.fn(
- (params: { signal?: AbortSignal }) =>
- new Promise<{ bytes: Uint8Array; mediaType: string }>(() => {
- readSignal = params.signal;
- }),
- );
- const row = userInputRow({
- attachments: [
- {
- ref: "/workspace/large.mp4",
- fileName: "large.mp4",
- mime: "video/mp4",
- bytes: 80,
- },
- ],
- });
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: { ...rowContext, readAttachment },
- row,
- }),
- ),
- );
- });
- const openPreview = attachmentPropsById.get("42-0")?.onOpen;
- expect(openPreview).toBeTypeOf("function");
- await act(async () => {
- (openPreview as () => void)();
- await Promise.resolve();
- });
- expect(readSignal).toBeDefined();
- expect(readSignal?.aborted).toBe(false);
- const replacedSignal = readSignal;
- await act(async () => {
- (openPreview as () => void)();
- await Promise.resolve();
- });
- expect(replacedSignal?.aborted).toBe(true);
- expect(readSignal).not.toBe(replacedSignal);
- expect(readSignal?.aborted).toBe(false);
- const closePreview = imagePreviewDialogProps.at(-1)?.onOpenChange;
- expect(closePreview).toBeTypeOf("function");
- await act(async () => {
- (closePreview as (open: boolean) => void)(false);
- });
- expect(readSignal?.aborted).toBe(true);
- const closedSignal = readSignal;
- await act(async () => {
- (openPreview as () => void)();
- await Promise.resolve();
- });
- expect(readSignal).not.toBe(closedSignal);
- expect(readSignal?.aborted).toBe(false);
- await act(async () => root.unmount());
- expect(readSignal?.aborted).toBe(true);
- });
- it("aborts unfinished visible-row media prefetch when the row unmounts", async () => {
- let prefetchSignal: AbortSignal | undefined;
- const readAttachment = vi.fn(
- (params: { signal?: AbortSignal }) =>
- new Promise<{ bytes: Uint8Array; mediaType: string }>(() => {
- prefetchSignal = params.signal;
- }),
- );
- const row = userInputRow({
- attachments: [
- {
- ref: "/workspace/large.mp4",
- fileName: "large.mp4",
- mime: "video/mp4",
- bytes: 80,
- },
- ],
- });
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: { ...rowContext, readAttachment },
- row,
- }),
- ),
- );
- await Promise.resolve();
- });
- expect(prefetchSignal).toBeDefined();
- expect(prefetchSignal?.aborted).toBe(false);
- await act(async () => root.unmount());
- expect(prefetchSignal?.aborted).toBe(true);
- });
- it("marks an unreadable sent image unavailable and removes its open action", async () => {
- const readAttachment = vi.fn().mockRejectedValue(new Error("gone"));
- vi.spyOn(console, "warn").mockImplementation(() => {});
- const row = userInputRow({
- attachments: [
- {
- ref: "/workspace/gone.png",
- fileName: "gone.png",
- mime: "image/png",
- bytes: 4,
- },
- ],
- });
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: { ...rowContext, readAttachment },
- row,
- }),
- ),
- );
- });
- expect(readAttachment).toHaveBeenCalledWith({
- sessionId: "session-1",
- ref: "/workspace/gone.png",
- signal: expect.any(AbortSignal),
- });
- expect(attachmentPropsById.get("42-0")?.onOpen).toBeUndefined();
- expect(attachmentPropsById.get("42-0")?.title).toBe("这张图片已无法预览。");
- await act(async () => root.unmount());
- });
- // 回归:v4 迁移后用户行复制/编辑按钮硬编码中文,未走 i18n;
- // en-US 下会显示「复制」「编辑」。文案必须跟随当前 locale。
- it.each([
- ["zh-CN", "复制", "编辑"],
- ["en-US", "Copy", "Edit"],
- ] as const)(
- "renders user row copy/edit action labels from locale %s",
- async (locale, copyLabel, editLabel) => {
- const row = userInputRow();
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: locale },
- createElement(ConversationRowView, {
- context: rowContext,
- onEdit: vi.fn(),
- row,
- }),
- ),
- );
- });
- const copyAction = messageActionPropsByTestId.get(`v4-copy-${row.rowId}`);
- expect(copyAction?.label).toBe(copyLabel);
- expect(copyAction?.tooltip).toBe(copyLabel);
- const editAction = messageActionPropsByTestId.get(testId(TID_V4_EDIT, String(row.rowId)));
- expect(editAction?.label).toBe(editLabel);
- expect(editAction?.tooltip).toBe(editLabel);
- await act(async () => {
- root.unmount();
- });
- },
- );
- it("keeps user message actions visible in compact remote control mode", async () => {
- const row = userInputRow();
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: { ...rowContext, compactForRemoteControl: true },
- row,
- onEdit: vi.fn(),
- }),
- ),
- );
- });
- const actionsClassName = collectAttributeValues(container, "class").find((value) =>
- value.includes("mt-1 opacity"),
- );
- expect(actionsClassName).toContain("opacity-100");
- expect(actionsClassName).not.toContain("opacity-0");
- await act(async () => {
- root.unmount();
- });
- });
- it("keeps assistant feedback local to the mounted action toolbar", async () => {
- const row = assistantTextRow({
- entityId: "message-assistant-43",
- state: "complete",
- });
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: { ...rowContext, compactForRemoteControl: true },
- row,
- onFeedbackChange: vi.fn(async () => true),
- }),
- ),
- );
- });
- const likeTestId = testId(TID_V4_FEEDBACK_LIKE, String(row.rowId));
- const dislikeTestId = testId(TID_V4_FEEDBACK_DISLIKE, String(row.rowId));
- expect(messageActionPropsByTestId.get(likeTestId)?.["aria-pressed"]).toBe(false);
- expect(messageActionPropsByTestId.get(dislikeTestId)?.["aria-pressed"]).toBe(false);
- expect(messageActionPropsByTestId.get(likeTestId)?.tooltip).toBeUndefined();
- const handleLike = messageActionPropsByTestId.get(likeTestId)?.onClick;
- expect(handleLike).toBeTypeOf("function");
- await act(async () => {
- (handleLike as () => void)();
- });
- expect(messageActionPropsByTestId.get(likeTestId)?.["aria-pressed"]).toBe(true);
- expect(messageActionPropsByTestId.get(dislikeTestId)?.["aria-pressed"]).toBe(false);
- const handleLikeAgain = messageActionPropsByTestId.get(likeTestId)?.onClick;
- expect(handleLikeAgain).toBeTypeOf("function");
- await act(async () => {
- (handleLikeAgain as () => void)();
- });
- expect(messageActionPropsByTestId.get(likeTestId)?.["aria-pressed"]).toBe(false);
- expect(messageActionPropsByTestId.get(dislikeTestId)?.["aria-pressed"]).toBe(false);
- const handleDislike = messageActionPropsByTestId.get(dislikeTestId)?.onClick;
- expect(handleDislike).toBeTypeOf("function");
- await act(async () => {
- (handleDislike as () => void)();
- });
- expect(messageActionPropsByTestId.get(likeTestId)?.["aria-pressed"]).toBe(false);
- expect(messageActionPropsByTestId.get(dislikeTestId)?.["aria-pressed"]).toBe(true);
- await act(async () => {
- root.unmount();
- });
- });
- it("does not append a visual cursor to streaming assistant text", async () => {
- const row = assistantTextRow();
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- row,
- }),
- ),
- );
- });
- expect(collectText(container)).toContain(row.text);
- const classValues = collectAttributeValues(container, "class");
- expect(classValues.join(" ")).not.toContain("animate-pulse");
- await act(async () => {
- root.unmount();
- });
- });
- it.each(["complete", "streaming"] as const)(
- "puts assistant %s text inside a real selectable DOM region",
- async (state) => {
- const row = assistantTextRow({ state });
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- row,
- }),
- ),
- );
- });
- expect(collectAttributeValues(container, "data-conversation-selectable")).toContain("true");
- // 回归根因:标记不能继续依赖不透传 data-* 的 MessageResponse。
- expect(messageResponseProps.at(-1)?.["data-conversation-selectable"]).toBeUndefined();
- await act(async () => {
- root.unmount();
- });
- },
- );
- it("保持显式 Markdown 文件处理器,仅为 Assistant 开启 citation 投影", async () => {
- const row = assistantTextRow({
- state: "complete",
- text: "See [README](./README.md) and https://example.test.",
- });
- const onOpenBrowserUrl = vi.fn();
- const onOpenCodeViewer = vi.fn();
- const onOpenFileLink = vi.fn();
- const readAttachment = vi.fn();
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: {
- ...rowContext,
- onOpenBrowserUrl,
- onOpenCodeViewer,
- onOpenFileLink,
- readAttachment,
- },
- row,
- }),
- ),
- );
- });
- expect(messageResponseProps).toHaveLength(1);
- expect(messageResponseProps[0]?.onOpenExternalUrl).toBe(onOpenBrowserUrl);
- expect(messageResponseProps[0]?.onOpenCodeViewer).toBe(onOpenCodeViewer);
- expect(messageResponseProps[0]?.onOpenFileLink).toBe(onOpenFileLink);
- expect(messageResponseProps[0]?.renderZCodeFileCitations).toBe(true);
- // 回归根因:artifact 图片读取能力必须由行级生产接线继续传入 Markdown。
- expect(messageResponseProps[0]?.sessionId).toBe("session-1");
- expect(messageResponseProps[0]?.readAttachment).toBe(readAttachment);
- await act(async () => {
- root.unmount();
- });
- });
- it("renders web element contexts as read-only chips while keeping the raw row text for actions", async () => {
- const context: WebElementContextComposerAttachment = {
- capturedAt: 1_700_000_000_000,
- id: "element-1",
- pageTitle: "Docs",
- pageUrl: "https://example.test/docs",
- role: "button",
- tagName: "button",
- text: "Submit",
- workspaceIdentity: "remote:workspace",
- workspacePath: "/workspace",
- };
- const rawText = buildPromptWithWebElementContexts("帮我看这个按钮", [context]);
- const row = userInputRow({
- text: rawText,
- });
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- onEdit: vi.fn(),
- row,
- }),
- ),
- );
- });
- const text = collectText(container);
- expect(text).toContain("帮我看这个按钮");
- expect(text).toContain("1 个网页元素");
- expect(text).not.toContain("# Web page elements:");
- const attachmentArea = findElementByAttribute(
- container,
- "data-v4-user-input-attachments",
- "true",
- );
- const messageBubble = findElementByAttribute(container, "data-v4-user-input-bubble", "true");
- expect(collectText(attachmentArea)).toContain("note.txt");
- expect(collectText(attachmentArea)).toContain("1 个网页元素");
- expect(collectText(messageBubble)).toBe("帮我看这个按钮");
- const fileAttachmentPill = findElementByAttribute(
- attachmentArea,
- "data-v4-user-input-attachment-pill",
- "true",
- );
- const webElementChip = findElementByAttribute(attachmentArea, "aria-label", "1 个网页元素");
- const pillRow = findElementByAttribute(
- attachmentArea,
- "data-v4-user-input-attachment-pills",
- "true",
- );
- expect(fileAttachmentPill?.parentNode).toBe(pillRow);
- expect(webElementChip?.parentNode).toBe(pillRow);
- expect(collectAttributeValues(webElementChip, "class").join(" ")).toContain("rounded-full");
- const copyAction = messageActionPropsByTestId.get(`v4-copy-${row.rowId}`);
- expect(copyAction).toBeDefined();
- expect(copyAction?.disabled).toBe(false);
- await act(async () => {
- root.unmount();
- });
- });
- it("right-aligns PPT and conversation-reference hover cards in sent user rows", async () => {
- const pptxElement: PptxElementReference = {
- id: "pptx-1",
- workspacePath: "/workspace",
- sourcePath: "/workspace/deck.pptx",
- sourceTitle: "deck.pptx",
- sourceFingerprint: `sha256:${"a".repeat(64)}`,
- slideIndex: 0,
- slidePart: "ppt/slides/slide1.xml",
- nodeId: "7",
- nodeName: "Title",
- nodeType: "shape",
- bounds: { x: 0, y: 0, width: 100, height: 30 },
- zIndex: 1,
- capturedAt: 1,
- };
- const selection: ConversationSelectionReference = {
- id: "selection-1",
- sourceSessionId: "source-session",
- sourceRowId: 8,
- contentType: "assistant",
- text: "selected answer",
- };
- const row = userInputRow({
- text: serializeComposerPromptContexts("请处理", {
- codeComments: [],
- conversationSelections: [selection],
- pptxElements: [pptxElement],
- webElements: [],
- }),
- });
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- onEdit: vi.fn(),
- row,
- }),
- ),
- );
- });
- expect(hoverCardContentProps).toHaveLength(4);
- expect(hoverCardContentProps.every((props) => props.align === "end")).toBe(true);
- await act(async () => {
- root.unmount();
- });
- });
- it("does not render a bubble when a user row only contains context references", async () => {
- const context: WebElementContextComposerAttachment = {
- capturedAt: 1_700_000_000_000,
- id: "element-only",
- pageTitle: "Docs",
- pageUrl: "https://example.test/docs",
- tagName: "main",
- text: "Content",
- workspaceIdentity: "remote:workspace",
- workspacePath: "/workspace",
- };
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- row: userInputRow({
- attachments: [],
- text: buildPromptWithWebElementContexts("", [context]),
- }),
- }),
- ),
- );
- });
- expect(
- findElementByAttribute(container, "data-v4-user-input-attachments", "true"),
- ).not.toBeNull();
- expect(findElementByAttribute(container, "data-v4-user-input-bubble", "true")).toBeNull();
- await act(async () => root.unmount());
- });
- it("renders an authoritative goal query without the slash", async () => {
- const row = userInputRow({
- attachments: [],
- text: "/goal 看一下这是个啥项目",
- });
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- row,
- }),
- ),
- );
- });
- expect(collectText(container)).toContain("goal 看一下这是个啥项目");
- expect(collectText(container)).not.toContain("/goal");
- expect(collectAttributeValues(container, "class").join(" ")).toContain("lucide-goal");
- expect(collectAttributeValues(container, "data-v4-user-input-command")).toContain("goal");
- await act(async () => {
- root.unmount();
- });
- });
- it("opens an inline editor and submits edited text with original attachments", async () => {
- const onEdit = vi.fn(async () => true);
- const row = userInputRow();
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- onEdit,
- row,
- }),
- ),
- );
- });
- const editAction = messageActionPropsByTestId.get(testId(TID_V4_EDIT, String(row.rowId)));
- expect(editAction).toBeDefined();
- const editOnClick = editAction?.onClick;
- expect(typeof editOnClick).toBe("function");
- await act(async () => {
- (editOnClick as () => void)();
- });
- expect(chatPromptEditorProps).toHaveLength(1);
- expect(chatPromptEditorProps[0]?.initialValue).toBe(row.text);
- const submitEdit = chatPromptEditorProps[0]?.onSubmit;
- expect(typeof submitEdit).toBe("function");
- await act(async () => {
- await (submitEdit as (value: string) => Promise<void>)("改过的问题");
- });
- expect(onEdit).toHaveBeenCalledWith(
- { rowId: row.rowId, entityId: row.entityId },
- "改过的问题",
- row.attachments,
- "preserve",
- );
- await act(async () => {
- root.unmount();
- });
- });
- it("renders edit attachments like the composer and submits the remaining refs in source order", async () => {
- const onEdit = vi.fn(async () => true);
- const row = userInputRow({
- text: "",
- attachments: [
- {
- bytes: 12,
- fileName: "first.txt",
- mime: "text/plain",
- ref: "/workspace/first.txt",
- },
- {
- bytes: 24,
- fileName: "second.png",
- mime: "image/png",
- ref: "/workspace/second.png",
- },
- {
- bytes: 48,
- fileName: "third.mov",
- mime: "video/quicktime",
- ref: "/workspace/third.mov",
- },
- ],
- });
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- onEdit,
- row,
- }),
- ),
- );
- });
- await act(async () => {
- (
- messageActionPropsByTestId.get(testId(TID_V4_EDIT, String(row.rowId)))?.onClick as
- | (() => void)
- | undefined
- )?.();
- });
- // 编辑态按「媒体优先、组内原序」排列:second.png、third.mov 为媒体卡,first.txt 为文件卡。
- expect(attachmentPropsById.get(`${row.rowId}-1`)).toMatchObject({
- className: expect.stringContaining("size-12"),
- variant: "grid",
- "data-v4-user-edit-attachment-kind": "image",
- });
- expect(attachmentPropsById.get(`${row.rowId}-2`)).toMatchObject({
- className: expect.stringContaining("size-12"),
- variant: "grid",
- "data-v4-user-edit-attachment-kind": "video",
- });
- expect(attachmentPropsById.get(`${row.rowId}-0`)).toMatchObject({
- className: expect.stringContaining("h-12"),
- variant: "inline",
- "data-v4-user-edit-attachment-kind": "file",
- });
- const firstRemoveId = testId(TID_V4_EDIT_ATTACHMENT_REMOVE, `${row.rowId}-0`);
- const secondRemoveId = testId(TID_V4_EDIT_ATTACHMENT_REMOVE, `${row.rowId}-1`);
- expect(attachmentRemovePropsByTestId.get(firstRemoveId)).toMatchObject({
- alwaysVisible: undefined,
- className: expect.stringContaining("size-3.5"),
- label: "移除附件",
- placement: "corner",
- variant: "default",
- });
- expect(attachmentRemovePropsByTestId.get(secondRemoveId)).toBeDefined();
- await act(async () => {
- (attachmentRemovePropsByTestId.get(firstRemoveId)!.onClick as () => void)();
- });
- expect(chatPromptEditorProps.at(-1)?.allowSubmitWhenEmpty).toBe(true);
- expect(chatPromptEditorProps.at(-1)?.submitDisabled).toBe(false);
- await act(async () => {
- await (chatPromptEditorProps.at(-1)!.onSubmit as (value: string) => Promise<void>)("");
- });
- expect(onEdit).toHaveBeenCalledWith(
- { rowId: row.rowId, entityId: row.entityId },
- "",
- [row.attachments?.[1], row.attachments?.[2]],
- "preserve",
- );
- await act(async () => root.unmount());
- });
- it("renders prompt contexts in the edit second row and keeps protocol text out of the editor", async () => {
- const onEdit = vi.fn(async () => true);
- const webContext: WebElementContextComposerAttachment = {
- capturedAt: 1_700_000_000_000,
- id: "edit-web-element",
- pageTitle: "Docs",
- pageUrl: "https://example.test/docs",
- selector: "main",
- tagName: "main",
- workspacePath: "/workspace",
- };
- const row = userInputRow({
- attachments: [],
- text: buildPromptWithWebElementContexts("改一下", [webContext]),
- });
- const container = installMinimalDom();
- const root: Root = createRoot(container);
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- onEdit,
- row,
- }),
- ),
- );
- });
- await act(async () => {
- (
- messageActionPropsByTestId.get(testId(TID_V4_EDIT, String(row.rowId)))?.onClick as
- | (() => void)
- | undefined
- )?.();
- });
- expect(chatPromptEditorProps.at(-1)?.initialValue).toBe("改一下");
- const contextRow = findElementByAttribute(
- container,
- "data-v4-user-edit-context-attachments-row",
- "true",
- );
- expect(contextRow).not.toBeNull();
- expect(collectText(contextRow)).toContain("1 个网页元素");
- await act(async () => {
- await (chatPromptEditorProps.at(-1)!.onSubmit as (value: string) => Promise<void>)("更新");
- });
- expect(onEdit).toHaveBeenCalledWith(
- { rowId: row.rowId, entityId: row.entityId },
- buildPromptWithWebElementContexts("更新", [webContext]),
- [],
- "preserve",
- );
- await act(async () => root.unmount());
- });
- it("rejects an edit only after both text and attachments become empty", async () => {
- const onEdit = vi.fn(async () => true);
- const row = userInputRow({ text: "" });
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- onEdit,
- row,
- }),
- ),
- );
- });
- await act(async () => {
- (
- messageActionPropsByTestId.get(testId(TID_V4_EDIT, String(row.rowId)))?.onClick as
- | (() => void)
- | undefined
- )?.();
- });
- expect(chatPromptEditorProps.at(-1)?.submitDisabled).toBe(false);
- await act(async () => {
- (
- attachmentRemovePropsByTestId.get(testId(TID_V4_EDIT_ATTACHMENT_REMOVE, `${row.rowId}-0`))!
- .onClick as () => void
- )();
- });
- expect(chatPromptEditorProps.at(-1)?.allowSubmitWhenEmpty).toBe(false);
- expect(chatPromptEditorProps.at(-1)?.submitDisabled).toBe(true);
- await act(async () => {
- await (chatPromptEditorProps.at(-1)!.onSubmit as (value: string) => Promise<void>)(" ");
- });
- expect(onEdit).not.toHaveBeenCalled();
- await act(async () => root.unmount());
- });
- it("restores the original attachment list after cancelling and reopening edit", async () => {
- const onEdit = vi.fn(async () => true);
- const row = userInputRow({
- attachments: [
- ...(userInputRow().attachments ?? []),
- {
- bytes: 24,
- fileName: "second.txt",
- mime: "text/plain",
- ref: "/workspace/second.txt",
- },
- ],
- });
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- onEdit,
- row,
- }),
- ),
- );
- });
- await act(async () => {
- (
- messageActionPropsByTestId.get(testId(TID_V4_EDIT, String(row.rowId)))?.onClick as
- | (() => void)
- | undefined
- )?.();
- });
- await act(async () => {
- (
- attachmentRemovePropsByTestId.get(testId(TID_V4_EDIT_ATTACHMENT_REMOVE, `${row.rowId}-0`))!
- .onClick as () => void
- )();
- (chatPromptEditorProps.at(-1)!.onCancel as () => void)();
- });
- await act(async () => {
- (
- messageActionPropsByTestId.get(testId(TID_V4_EDIT, String(row.rowId)))?.onClick as
- | (() => void)
- | undefined
- )?.();
- });
- await act(async () => {
- await (chatPromptEditorProps.at(-1)!.onSubmit as (value: string) => Promise<void>)(row.text);
- });
- expect(onEdit).toHaveBeenCalledWith(
- { rowId: row.rowId, entityId: row.entityId },
- row.text,
- row.attachments,
- "preserve",
- );
- await act(async () => root.unmount());
- });
- it("keeps removed refs after a failed ACK and leaves 15360+ pasted text in the editor", async () => {
- const onEdit = vi.fn(async () => false);
- const row = userInputRow({
- attachments: [
- ...(userInputRow().attachments ?? []),
- {
- bytes: 24,
- fileName: "second.txt",
- mime: "text/plain",
- ref: "/workspace/second.txt",
- },
- ],
- });
- const longText = "长".repeat(15_361);
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- onEdit,
- row,
- }),
- ),
- );
- });
- await act(async () => {
- (
- messageActionPropsByTestId.get(testId(TID_V4_EDIT, String(row.rowId)))?.onClick as
- | (() => void)
- | undefined
- )?.();
- });
- expect(chatPromptEditorProps.at(-1)?.onPaste).toBeUndefined();
- await act(async () => {
- (
- attachmentRemovePropsByTestId.get(testId(TID_V4_EDIT_ATTACHMENT_REMOVE, `${row.rowId}-0`))!
- .onClick as () => void
- )();
- (chatPromptEditorProps.at(-1)!.onChange as (value: string) => void)(longText);
- });
- await act(async () => {
- await (chatPromptEditorProps.at(-1)!.onSubmit as (value: string) => Promise<void>)(longText);
- });
- expect(onEdit).toHaveBeenCalledWith(
- { rowId: row.rowId, entityId: row.entityId },
- longText,
- [row.attachments?.[1]],
- "preserve",
- );
- expect(chatPromptEditorProps.at(-1)?.initialValue).toBe(row.text);
- expect(
- attachmentRemovePropsByTestId.has(testId(TID_V4_EDIT_ATTACHMENT_REMOVE, `${row.rowId}-0`)),
- ).toBe(true);
- await act(async () => root.unmount());
- });
- it("keeps the disabled file reset tooltip hoverable and explains why it is unavailable", async () => {
- const row = userInputRow();
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- editWorkspaceRewindAvailability: {
- enabled: false,
- reason: "noFiles",
- },
- onEdit: vi.fn(),
- row,
- }),
- ),
- );
- });
- const editAction = messageActionPropsByTestId.get(testId(TID_V4_EDIT, String(row.rowId)));
- const editOnClick = editAction?.onClick;
- expect(editOnClick).toBeTypeOf("function");
- await act(async () => {
- (editOnClick as () => void)();
- });
- const slot = chatPromptEditorProps.at(-1)?.betweenCancelAndSubmitAction as ReactElement<{
- description?: string;
- title: string;
- children: ReactElement<Record<string, unknown>>;
- }>;
- const disabledTooltipTrigger = slot.props.children;
- const rewindButton = disabledTooltipTrigger.props.children as ReactElement<
- Record<string, unknown>
- >;
- expect(slot.props.title).toBe("与文件一起重置");
- expect(slot.props.description).toBe("本轮没有可安全恢复的文件改动");
- expect(disabledTooltipTrigger.type).toBe("span");
- expect(disabledTooltipTrigger.props["data-disabled-tooltip-trigger"]).toBe("true");
- expect(rewindButton.props.disabled).toBe(true);
- expect(rewindButton.props.size).toBe("icon-md");
- expect((rewindButton.props.children as ReactElement).type).toBe(FileClockIcon);
- await act(async () => {
- root.unmount();
- });
- });
- it("keeps the file+conversation action between cancel and send and falls back after a blocked preview", async () => {
- const preview = {
- canApply: false,
- safeFiles: [],
- unsafeFiles: [
- {
- action: "restore" as const,
- operationCount: 1,
- path: "/workspace/note.txt",
- reason: "external_modified" as const,
- toolNames: ["Write"],
- },
- ],
- ignoredFiles: [],
- };
- const onEdit = vi
- .fn()
- .mockResolvedValueOnce({
- commandId: "edit-rewind-command",
- accepted: true,
- result: {
- type: "editUserQuery",
- disposition: "blocked",
- preview,
- reasonCode: "edit.workspaceRewindBlocked",
- },
- })
- .mockResolvedValueOnce({
- commandId: "edit-conversation-only-command",
- accepted: true,
- result: { type: "editUserQuery", disposition: "rewind" },
- });
- const row = userInputRow();
- const root: Root = createRoot(installMinimalDom());
- await act(async () => {
- root.render(
- createElement(
- ZCodeIntlProvider,
- { initialLocale: "zh-CN" },
- createElement(ConversationRowView, {
- context: rowContext,
- editWorkspaceRewindAvailability: {
- enabled: true,
- reason: "available",
- },
- onEdit,
- row,
- }),
- ),
- );
- });
- const editAction = messageActionPropsByTestId.get(testId(TID_V4_EDIT, String(row.rowId)));
- const editOnClick = editAction?.onClick;
- expect(editOnClick).toBeTypeOf("function");
- await act(async () => {
- (editOnClick as () => void)();
- });
- const slot = chatPromptEditorProps.at(-1)?.betweenCancelAndSubmitAction as ReactElement<{
- description?: string;
- title: string;
- children: ReactElement<Record<string, unknown>>;
- }>;
- const rewindButton = slot.props.children;
- expect(slot.props.title).toBe("与文件一起重置");
- expect(slot.props.description).toBeUndefined();
- expect(rewindButton.props["data-testid"]).toBe(
- testId(TID_V4_EDIT_REWIND_WORKSPACE, String(row.rowId)),
- );
- expect(rewindButton.props.disabled).toBe(false);
- expect(rewindButton.props.size).toBe("icon-md");
- expect(rewindButton.props.children).toMatchObject({
- type: FileClockIcon,
- props: { className: "size-4" },
- });
- await act(async () => {
- (rewindButton.props.onClick as () => void)();
- await Promise.resolve();
- });
- expect(onEdit).toHaveBeenNthCalledWith(
- 1,
- { rowId: row.rowId, entityId: row.entityId },
- row.text,
- row.attachments,
- "rewind",
- );
- expect(fileRewindDialogProps.at(-1)).toMatchObject({
- open: true,
- preview,
- variant: "editConflict",
- });
- const onConversationOnly = fileRewindDialogProps.at(-1)?.onConversationOnly;
- expect(onConversationOnly).toBeTypeOf("function");
- await act(async () => {
- (onConversationOnly as () => void)();
- await Promise.resolve();
- });
- expect(onEdit).toHaveBeenNthCalledWith(
- 2,
- { rowId: row.rowId, entityId: row.entityId },
- row.text,
- row.attachments,
- "preserve",
- );
- await act(async () => {
- root.unmount();
- });
- });
- });
|