|
| 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