Skip to content

Commit 75f8603

Browse files
committed
test(plugin-chatbot): pin the iterate_or_new question, live and after reload
The mapper lifts the question from the envelope (plain proposal and other decisions lift nothing; no objects still asks); the card asks instead of offering Build it and each choice sends its answer; the question survives both reload paths (server history and cache fallback); the two answers follow the conversation's language and never read as approval to the cloud confirm gate. Claude-Session: https://claude.ai/code/session_01Utp6Ca9ZbSdPg2iuzBYA3d Co-authored-by: Claude <noreply@anthropic.com>
1 parent f691828 commit 75f8603

4 files changed

Lines changed: 441 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* objectstack#27480 — the extend-or-new question an `iterate_or_new` plan asks
5+
* survives a reload, on both reload paths this page has. Without that, a reload
6+
* turns the question back into an ordinary plan card with a "Build it" button:
7+
* the one-click duplicate the question exists to prevent, one refresh later.
8+
*
9+
* - server history: the stored tool result is merged back onto its call
10+
* (`toUIMessages`) and re-parsed by the same detector the live chat uses;
11+
* - cache fallback: the cache keeps a compact `output` rebuilt from the
12+
* derived plan (`sanitizeChatMessagesForCache`), so the question's keys
13+
* must be written back into it.
14+
*
15+
* Each path ends where the user does: the card rendered from the reloaded
16+
* messages, asserted for the question and for the absence of "Build it".
17+
*/
18+
import '@testing-library/jest-dom/vitest';
19+
import { afterEach, describe, expect, it, vi } from 'vitest';
20+
import { cleanup, render, screen } from '@testing-library/react';
21+
import React from 'react';
22+
import { ChatbotEnhanced, uiMessageToChatMessage } from '@object-ui/plugin-chatbot';
23+
import { hydratedMessagesToChatMessages } from '../AiChatPage';
24+
import { sanitizeChatMessagesForCache, toUIMessages } from '../../../hooks/useChatConversation';
25+
26+
const ITERATE_OR_NEW = {
27+
status: 'blueprint_proposed',
28+
decision: 'iterate_or_new',
29+
existingAppOverlap: { name: 'library', label: '图书馆' },
30+
summary: 'A reading-list app',
31+
blueprint: {
32+
objects: [{ name: 'book', label: 'Book', fields: [{ name: 'title' }, { name: 'author' }] }],
33+
},
34+
counts: { objects: 1, views: 0, dashboards: 0, seedData: 0 },
35+
questions: [],
36+
};
37+
38+
const LIVE = {
39+
id: 'a1',
40+
role: 'assistant' as const,
41+
parts: [
42+
{
43+
type: 'tool-propose_blueprint',
44+
toolCallId: 'plan-1',
45+
state: 'output-available' as const,
46+
input: {},
47+
output: ITERATE_OR_NEW,
48+
},
49+
],
50+
};
51+
52+
function renderCard(messages: React.ComponentProps<typeof ChatbotEnhanced>['messages']) {
53+
render(<ChatbotEnhanced messages={messages} onSendMessage={vi.fn()} />);
54+
}
55+
56+
function expectQuestionNotApproval() {
57+
expect(screen.getByTestId('proposed-plan-iterate-or-new-question').textContent).toContain('图书馆');
58+
expect(screen.getByTestId('proposed-plan-extend-existing')).toBeInTheDocument();
59+
expect(screen.getByTestId('proposed-plan-build-new')).toBeInTheDocument();
60+
expect(screen.queryByTestId('proposed-plan-approve')).toBeNull();
61+
expect(screen.queryByTestId('proposed-plan-fallback')).toBeNull();
62+
}
63+
64+
afterEach(cleanup);
65+
66+
describe('the iterate_or_new question survives a reload (objectstack#27480)', () => {
67+
it('server history: the stored result re-parses to the question', () => {
68+
const ui = toUIMessages([
69+
{
70+
id: 'a1',
71+
role: 'assistant',
72+
content: [{ type: 'tool-call', toolCallId: 'plan-1', toolName: 'propose_blueprint', input: {} }],
73+
},
74+
{
75+
role: 'tool',
76+
content: [
77+
{
78+
type: 'tool-result',
79+
toolCallId: 'plan-1',
80+
output: { type: 'text', value: JSON.stringify(ITERATE_OR_NEW) },
81+
},
82+
],
83+
},
84+
]);
85+
const messages = hydratedMessagesToChatMessages(ui);
86+
expect(messages[0]?.toolInvocations?.[0]?.proposedPlan?.iterateOrNew).toEqual({
87+
app: { name: 'library', label: '图书馆' },
88+
});
89+
renderCard(messages);
90+
expectQuestionNotApproval();
91+
});
92+
93+
it('cache fallback: the compact cached output keeps both keys the question is read from', () => {
94+
const live = uiMessageToChatMessage(LIVE);
95+
const persisted = JSON.parse(JSON.stringify(sanitizeChatMessagesForCache([live]))) as ReturnType<
96+
typeof sanitizeChatMessagesForCache
97+
>;
98+
const planPart = persisted[0]?.parts.find((p) => p.type === 'tool-propose_blueprint');
99+
expect(planPart?.output).toMatchObject({
100+
status: 'blueprint_proposed',
101+
decision: 'iterate_or_new',
102+
existingAppOverlap: { name: 'library', label: '图书馆' },
103+
});
104+
105+
// Both reads a cache-fallback reload performs: this page's hydration
106+
// mapper, and the live mapper `useObjectChat` re-runs over the same parts.
107+
for (const messages of [hydratedMessagesToChatMessages(persisted), [uiMessageToChatMessage(persisted[0])]]) {
108+
expect(messages[0]?.toolInvocations?.[0]?.proposedPlan?.iterateOrNew).toEqual({
109+
app: { name: 'library', label: '图书馆' },
110+
});
111+
cleanup();
112+
renderCard(messages);
113+
expectQuestionNotApproval();
114+
}
115+
});
116+
117+
it('cache fallback: a plain proposal is cached without the question keys (control)', () => {
118+
const plain = { ...ITERATE_OR_NEW, decision: undefined, existingAppOverlap: undefined };
119+
const live = uiMessageToChatMessage({ ...LIVE, parts: [{ ...LIVE.parts[0], output: plain }] });
120+
const persisted = sanitizeChatMessagesForCache([live]);
121+
const output = persisted[0]?.parts.find((p) => p.type === 'tool-propose_blueprint')?.output as Record<
122+
string,
123+
unknown
124+
>;
125+
expect(output.status).toBe('blueprint_proposed');
126+
expect('decision' in output).toBe(false);
127+
expect('existingAppOverlap' in output).toBe(false);
128+
renderCard(hydratedMessagesToChatMessages(persisted));
129+
expect(screen.getByTestId('proposed-plan-approve')).toBeInTheDocument();
130+
});
131+
});

‎ui/packages/app-shell/src/console/ai/__tests__/AiChatPage.planCardLocale.test.tsx‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,35 @@ describe('#3896 — outbound text follows the CONVERSATION, in both directions',
276276
);
277277
});
278278
});
279+
280+
/**
281+
* objectstack#27480 — the extend-or-new question an `iterate_or_new` plan asks
282+
* SENDS one of two answers, so they sit on the send side of the same line: the
283+
* conversation's language picks them, never the console's. The rows that can
284+
* discriminate are the ones where the two disagree.
285+
*/
286+
describe('objectstack#27480 — the extend-or-new answers follow the CONVERSATION', () => {
287+
function answers(props: Record<string, unknown>) {
288+
return {
289+
extend: (props.planExtendExistingMessage as (app: string) => string)('图书馆'),
290+
buildNew: props.planBuildNewAppMessage,
291+
};
292+
}
293+
294+
it.each(['en', 'zh', 'de'])('%s UI + Chinese thread: both answers are Chinese', (uiLocale) => {
295+
expect(answers(renderPane(uiLocale, ZH_THREAD))).toEqual({
296+
extend: '在「图书馆」上继续完善,不另建应用。',
297+
buildNew: '另建一个独立应用。',
298+
});
299+
});
300+
301+
it.each(['zh', 'en', 'de'])('%s UI + English thread: both answers are English', (uiLocale) => {
302+
const props = renderPane(uiLocale, EN_THREAD);
303+
expect(answers(props)).toEqual({
304+
extend: 'Keep improving "图书馆" instead of building a new app.',
305+
buildNew: 'Build a separate, new app.',
306+
});
307+
// Nothing Chinese leaves the console for an English thread but the app's own name.
308+
expect(CJK.test(String(props.planBuildNewAppMessage)), `outbound Chinese under ${uiLocale} UI`).toBe(false);
309+
});
310+
});
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
8+
* objectstack#27480 — a plan whose `decision` is `iterate_or_new` overlaps an
9+
* app the user already has. The card drew it as an ordinary plan with a
10+
* "Build it" button, so one click could build a duplicate of that app. It now
11+
* asks the extend-or-new question in place of the plan's actions: the question
12+
* names the existing app, and each of its two choices sends its answer.
13+
*
14+
* Every case feeds the wire shape through the real mapper, so what is pinned is
15+
* the envelope the producer sends, not a hand-built `proposedPlan`. The plain
16+
* proposal is the control: its "Build it" gate is untouched.
17+
*/
18+
import '@testing-library/jest-dom/vitest';
19+
import { describe, it, expect, vi, afterEach } from 'vitest';
20+
import { render, screen, fireEvent, cleanup } from '@testing-library/react';
21+
import { I18nProvider } from '@object-ui/i18n';
22+
import { ChatbotEnhanced } from '../ChatbotEnhanced';
23+
import { uiMessagesToChatMessages } from '../mapMessages';
24+
25+
type WireMessage = Parameters<typeof uiMessagesToChatMessages>[0][number];
26+
27+
const PLAIN = {
28+
status: 'blueprint_proposed',
29+
summary: 'A reading-list app',
30+
blueprint: {
31+
objects: [{ name: 'book', label: 'Book', fields: [{ name: 'title' }, { name: 'author' }] }],
32+
},
33+
counts: { objects: 1, views: 0, dashboards: 0, seedData: 0 },
34+
questions: [],
35+
};
36+
37+
const ITERATE_OR_NEW = {
38+
...PLAIN,
39+
decision: 'iterate_or_new',
40+
existingAppOverlap: { name: 'library', label: '图书馆' },
41+
};
42+
43+
function proposal(result: unknown): WireMessage[] {
44+
return [
45+
{ id: 'u1', role: 'user', parts: [{ type: 'text', text: 'Build me a library app' }] },
46+
{
47+
id: 'a1',
48+
role: 'assistant',
49+
parts: [
50+
{
51+
type: 'tool-propose_blueprint',
52+
toolCallId: 'plan-1',
53+
state: 'output-available',
54+
output: { type: 'text', value: JSON.stringify(result) },
55+
},
56+
],
57+
},
58+
] as WireMessage[];
59+
}
60+
61+
const messagesOf = (wire: WireMessage[]) => uiMessagesToChatMessages(wire, { isStreaming: false });
62+
63+
afterEach(cleanup);
64+
65+
describe('the iterate_or_new plan asks extend-or-new instead of offering Build it (objectstack#27480)', () => {
66+
it('renders the question naming the existing app, and no approve button', () => {
67+
render(<ChatbotEnhanced messages={messagesOf(proposal(ITERATE_OR_NEW))} onSendMessage={vi.fn()} />);
68+
expect(screen.getByTestId('proposed-plan-iterate-or-new-question').textContent).toContain('图书馆');
69+
expect(screen.queryByTestId('proposed-plan-approve')).toBeNull();
70+
expect(screen.queryByTestId('proposed-plan-adjust')).toBeNull();
71+
expect(screen.queryByTestId('proposed-plan-fallback')).toBeNull();
72+
expect(screen.queryByText('Build it')).toBeNull();
73+
// The plan's own rows stay, so the user sees what would be built either way.
74+
expect(screen.getByTestId('proposed-plan')).toBeInTheDocument();
75+
});
76+
77+
it('a plain proposal is unchanged: Build it, Adjust, and no question (control)', () => {
78+
render(<ChatbotEnhanced messages={messagesOf(proposal(PLAIN))} onSendMessage={vi.fn()} />);
79+
expect(screen.getByTestId('proposed-plan-approve')).toBeInTheDocument();
80+
expect(screen.getByTestId('proposed-plan-adjust')).toBeInTheDocument();
81+
expect(screen.queryByTestId('proposed-plan-iterate-or-new')).toBeNull();
82+
});
83+
84+
it('each choice sends its answer', () => {
85+
const onSendMessage = vi.fn();
86+
render(<ChatbotEnhanced messages={messagesOf(proposal(ITERATE_OR_NEW))} onSendMessage={onSendMessage} />);
87+
fireEvent.click(screen.getByTestId('proposed-plan-extend-existing'));
88+
expect(onSendMessage).toHaveBeenLastCalledWith('Keep improving "图书馆" instead of building a new app.');
89+
fireEvent.click(screen.getByTestId('proposed-plan-build-new'));
90+
expect(onSendMessage).toHaveBeenLastCalledWith('Build a separate, new app.');
91+
expect(onSendMessage).toHaveBeenCalledTimes(2);
92+
});
93+
94+
it("sends the host's outbound text when the host supplies it", () => {
95+
const onSendMessage = vi.fn();
96+
render(
97+
<ChatbotEnhanced
98+
messages={messagesOf(proposal(ITERATE_OR_NEW))}
99+
onSendMessage={onSendMessage}
100+
planExtendExistingMessage={(app) => `在「${app}」上继续完善,不另建应用。`}
101+
planBuildNewAppMessage="另建一个独立应用。"
102+
/>,
103+
);
104+
fireEvent.click(screen.getByTestId('proposed-plan-extend-existing'));
105+
expect(onSendMessage).toHaveBeenLastCalledWith('在「图书馆」上继续完善,不另建应用。');
106+
fireEvent.click(screen.getByTestId('proposed-plan-build-new'));
107+
expect(onSendMessage).toHaveBeenLastCalledWith('另建一个独立应用。');
108+
});
109+
110+
it('zh: the question and both choices read in Chinese and name the app', () => {
111+
render(
112+
<I18nProvider config={{ defaultLanguage: 'zh', detectBrowserLanguage: false }}>
113+
<ChatbotEnhanced messages={messagesOf(proposal(ITERATE_OR_NEW))} onSendMessage={vi.fn()} />
114+
</I18nProvider>,
115+
);
116+
expect(screen.getByTestId('proposed-plan-iterate-or-new-question').textContent).toContain('「图书馆」');
117+
expect(screen.getByTestId('proposed-plan-extend-existing').textContent).toBe('在「图书馆」上继续完善');
118+
expect(screen.getByTestId('proposed-plan-build-new').textContent).toBe('另建一个独立应用');
119+
});
120+
121+
it('still asks, and draws no fallback Build it, when the envelope names no object', () => {
122+
const noObjects = { ...ITERATE_OR_NEW, blueprint: { objects: [] } };
123+
render(<ChatbotEnhanced messages={messagesOf(proposal(noObjects))} onSendMessage={vi.fn()} />);
124+
expect(screen.getByTestId('proposed-plan-iterate-or-new')).toBeInTheDocument();
125+
expect(screen.queryByTestId('proposed-plan-fallback')).toBeNull();
126+
expect(screen.queryByTestId('proposed-plan-approve')).toBeNull();
127+
});
128+
129+
it("names the app by the host's label when the envelope carries only its name", () => {
130+
const nameOnly = { ...ITERATE_OR_NEW, existingAppOverlap: { name: 'library' } };
131+
render(
132+
<ChatbotEnhanced
133+
messages={messagesOf(proposal(nameOnly))}
134+
onSendMessage={vi.fn()}
135+
resolveAppLabel={(name) => (name === 'library' ? 'Library' : undefined)}
136+
/>,
137+
);
138+
expect(screen.getByTestId('proposed-plan-extend-existing').textContent).toBe('Keep improving "Library"');
139+
});
140+
141+
it('an overlap that names no app asks without a name, with a reply hint and no one-click choice', () => {
142+
const unnamed = { ...ITERATE_OR_NEW, existingAppOverlap: {} };
143+
render(<ChatbotEnhanced messages={messagesOf(proposal(unnamed))} onSendMessage={vi.fn()} />);
144+
expect(screen.getByTestId('proposed-plan-iterate-or-new-question').textContent).toContain(
145+
'an app you already have',
146+
);
147+
expect(screen.getByTestId('proposed-plan-iterate-or-new-hint')).toBeInTheDocument();
148+
expect(screen.queryByTestId('proposed-plan-iterate-or-new-choices')).toBeNull();
149+
expect(screen.queryByTestId('proposed-plan-approve')).toBeNull();
150+
});
151+
152+
it('a host that wires no message sending gets the question and a reply hint', () => {
153+
render(<ChatbotEnhanced messages={messagesOf(proposal(ITERATE_OR_NEW))} />);
154+
expect(screen.getByTestId('proposed-plan-iterate-or-new-hint')).toBeInTheDocument();
155+
expect(screen.queryByTestId('proposed-plan-iterate-or-new-choices')).toBeNull();
156+
});
157+
158+
it('once a build has run after it, the question stays and its choices go', () => {
159+
const wire: WireMessage[] = [
160+
...proposal(ITERATE_OR_NEW),
161+
{ id: 'u2', role: 'user', parts: [{ type: 'text', text: 'Build a separate, new app.' }] },
162+
{
163+
id: 'a2',
164+
role: 'assistant',
165+
parts: [
166+
{
167+
type: 'tool-apply_blueprint',
168+
toolCallId: 'build-1',
169+
state: 'output-available',
170+
output: { status: 'drafted', drafted: [{ type: 'object', name: 'book' }] },
171+
},
172+
],
173+
},
174+
] as WireMessage[];
175+
render(<ChatbotEnhanced messages={messagesOf(wire)} onSendMessage={vi.fn()} />);
176+
expect(screen.getByTestId('proposed-plan-iterate-or-new-question')).toBeInTheDocument();
177+
expect(screen.queryByTestId('proposed-plan-iterate-or-new-choices')).toBeNull();
178+
expect(screen.queryByTestId('proposed-plan-iterate-or-new-hint')).toBeNull();
179+
});
180+
});

0 commit comments

Comments
 (0)