|
| 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 | +}); |
0 commit comments