|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * objectstack#37863 — the drawer header's ☆ (ui ADR-0057 Amendment A5's |
| 6 | + * header: title, ☆, +, history, ⋯, ×). |
| 7 | + * |
| 8 | + * - Once the first send has created the thread, the header draws ☆ first |
| 9 | + * among its buttons, in the rail, the full window and the phone sheet. |
| 10 | + * Before it, there is nothing to keep and no ☆. |
| 11 | + * - ☆ adds the thread to the user's favourites as a `conversation` favourite |
| 12 | + * (its id, its agent by route name, the header's label, its `/ai…` address) |
| 13 | + * and is pressed while it is one; pressed again, it removes it. |
| 14 | + * - While the thread is a favourite, the stored label follows the label the |
| 15 | + * header reads; a label still being read writes nothing, and a thread that |
| 16 | + * is not a favourite is written nowhere. |
| 17 | + * |
| 18 | + * Harness: the chrome stand-ins of `ChatDock.test.tsx` (the default body |
| 19 | + * reports its actions and draws nothing) under the real `FavoritesProvider`, |
| 20 | + * signed out, so the store is the provider's local key. |
| 21 | + */ |
| 22 | +// AiChatPage reads a dropped spreadsheet through `import('@object-ui/plugin-grid')`; load it |
| 23 | +// here so collection pays that transform, not a clocked case (check-test-source-alias). |
| 24 | +import '@object-ui/plugin-grid'; |
| 25 | +import '@testing-library/jest-dom/vitest'; |
| 26 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 27 | +import { act, render, screen, fireEvent, waitFor, within } from '@testing-library/react'; |
| 28 | +import React from 'react'; |
| 29 | +import { ChatDockPanel, ChatDockMobileSheet, type ChatDockState } from '../ChatDock'; |
| 30 | +import type { ChatDockConversationActions } from '../chatDockTarget'; |
| 31 | +import { FavoritesProvider, type FavoriteItem } from '../../context/FavoritesProvider'; |
| 32 | + |
| 33 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 34 | + ...(await importOriginal<Record<string, unknown>>()), |
| 35 | + useObjectTranslation: () => ({ |
| 36 | + t: (key: string, options?: Record<string, unknown>) => String(options?.defaultValue ?? key), |
| 37 | + }), |
| 38 | +})); |
| 39 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 40 | + ...(await importOriginal<Record<string, unknown>>()), |
| 41 | + useAuth: () => ({ user: null, activeOrganization: null }), |
| 42 | +})); |
| 43 | +vi.mock('../../console/ai/AiChatPage', () => ({ |
| 44 | + ChatPane: () => null, |
| 45 | + resolveApiBase: (explicit?: string) => explicit ?? '/api/v1/ai', |
| 46 | +})); |
| 47 | +vi.mock('@object-ui/plugin-chatbot/core', async (importOriginal) => ({ |
| 48 | + ...(await importOriginal<typeof import('@object-ui/plugin-chatbot/core')>()), |
| 49 | + useAgents: () => ({ agents: [], isLoading: false, error: undefined }), |
| 50 | +})); |
| 51 | +vi.mock('../../hooks', () => ({ |
| 52 | + useChatConversation: () => ({ conversationId: undefined, initialMessages: [] }), |
| 53 | +})); |
| 54 | +vi.mock('../../hooks/useAiUsage', () => ({ |
| 55 | + useAiUsage: () => ({ usage: null, isLoading: false, error: undefined, refetch: () => {} }), |
| 56 | +})); |
| 57 | + |
| 58 | +// What the default body reports to the chrome; `report` re-reports it. |
| 59 | +const bodyState: { actions: ChatDockConversationActions | undefined; report?: (a: ChatDockConversationActions | undefined) => void } = { |
| 60 | + actions: undefined, |
| 61 | +}; |
| 62 | +vi.mock('../ChatDockConversation', async () => { |
| 63 | + const ReactActual = await import('react'); |
| 64 | + return { |
| 65 | + ChatDockConversation: (props: { onActionsChange?: (a: unknown) => void }) => { |
| 66 | + const { onActionsChange } = props; |
| 67 | + ReactActual.useEffect(() => { |
| 68 | + bodyState.report = (a) => onActionsChange?.(a); |
| 69 | + onActionsChange?.(bodyState.actions); |
| 70 | + }, [onActionsChange]); |
| 71 | + return <div data-testid="default-dock-body" />; |
| 72 | + }, |
| 73 | + }; |
| 74 | +}); |
| 75 | + |
| 76 | +function dockState(overrides: Partial<ChatDockState> = {}): ChatDockState { |
| 77 | + return { |
| 78 | + expanded: true, |
| 79 | + width: 420, |
| 80 | + dragging: false, |
| 81 | + fullWindow: false, |
| 82 | + setFullWindow: vi.fn(), |
| 83 | + toggle: vi.fn(), |
| 84 | + expand: vi.fn(), |
| 85 | + collapse: vi.fn(), |
| 86 | + onResizePointerDown: vi.fn(), |
| 87 | + ...overrides, |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +/** The provider's local key for a signed-out session. */ |
| 92 | +const STORE_KEY = 'objectui-favorites'; |
| 93 | +const stored = (): FavoriteItem[] => JSON.parse(localStorage.getItem(STORE_KEY) ?? '[]'); |
| 94 | + |
| 95 | +const SENT: ChatDockConversationActions = { |
| 96 | + conversationId: 'c1', |
| 97 | + showDebug: false, |
| 98 | + label: 'Quarterly revenue', |
| 99 | + startNew: () => {}, |
| 100 | + agent: 'ask', |
| 101 | +}; |
| 102 | + |
| 103 | +function headerControls(): string[] { |
| 104 | + return Array.from(screen.getByTestId('chat-dock-header').querySelectorAll('button')).map( |
| 105 | + (b) => b.getAttribute('aria-label') ?? b.textContent ?? '', |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +function renderRail(overrides: Partial<ChatDockState> = {}) { |
| 110 | + return render( |
| 111 | + <FavoritesProvider> |
| 112 | + <ChatDockPanel dock={dockState(overrides)} /> |
| 113 | + </FavoritesProvider>, |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +beforeEach(() => { |
| 118 | + localStorage.clear(); |
| 119 | +}); |
| 120 | +afterEach(() => { |
| 121 | + bodyState.actions = undefined; |
| 122 | + bodyState.report = undefined; |
| 123 | +}); |
| 124 | + |
| 125 | +describe('the drawer header ☆ (objectstack#37863)', () => { |
| 126 | + it('the rail: ☆ leads the buttons once the thread exists', async () => { |
| 127 | + bodyState.actions = SENT; |
| 128 | + renderRail(); |
| 129 | + await screen.findByTestId('default-dock-body'); |
| 130 | + expect(headerControls()).toEqual(['Add to favorites', 'New conversation', 'History', 'More actions', 'Close']); |
| 131 | + const star = screen.getByTestId('chat-dock-favorite'); |
| 132 | + expect(star).toHaveAttribute('aria-pressed', 'false'); |
| 133 | + expect(star.querySelector('svg.lucide-star')).not.toBeNull(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('before the first send there is no ☆', async () => { |
| 137 | + bodyState.actions = { showDebug: false, startNew: () => {}, agent: 'ask' }; |
| 138 | + renderRail(); |
| 139 | + await screen.findByTestId('default-dock-body'); |
| 140 | + expect(screen.queryByTestId('chat-dock-favorite')).toBeNull(); |
| 141 | + expect(headerControls()).toEqual(['New conversation', 'History', 'More actions', 'Close']); |
| 142 | + }); |
| 143 | + |
| 144 | + it('☆ stores the thread as a conversation favourite, pressed while it is one; pressed again, removes it', async () => { |
| 145 | + bodyState.actions = { ...SENT, agent: 'data_chat' }; |
| 146 | + renderRail(); |
| 147 | + await screen.findByTestId('default-dock-body'); |
| 148 | + fireEvent.click(screen.getByTestId('chat-dock-favorite')); |
| 149 | + expect(stored()).toEqual([ |
| 150 | + expect.objectContaining({ |
| 151 | + id: 'conversation:c1', |
| 152 | + type: 'conversation', |
| 153 | + conversationId: 'c1', |
| 154 | + agent: 'ask', |
| 155 | + label: 'Quarterly revenue', |
| 156 | + href: '/ai/ask/c1', |
| 157 | + }), |
| 158 | + ]); |
| 159 | + const star = screen.getByTestId('chat-dock-favorite'); |
| 160 | + expect(star).toHaveAttribute('aria-pressed', 'true'); |
| 161 | + expect(star).toHaveAccessibleName('Remove from favorites'); |
| 162 | + |
| 163 | + fireEvent.click(star); |
| 164 | + expect(stored()).toEqual([]); |
| 165 | + expect(screen.getByTestId('chat-dock-favorite')).toHaveAttribute('aria-pressed', 'false'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('starred while the label is still read, it is stored as "New conversation" and then takes the label', async () => { |
| 169 | + bodyState.actions = { ...SENT, label: undefined, labelPending: true }; |
| 170 | + renderRail(); |
| 171 | + await screen.findByTestId('default-dock-body'); |
| 172 | + fireEvent.click(screen.getByTestId('chat-dock-favorite')); |
| 173 | + expect(stored()[0].label).toBe('New conversation'); |
| 174 | + act(() => bodyState.report?.({ ...SENT, label: 'Quarterly revenue' })); |
| 175 | + await waitFor(() => expect(stored()[0].label).toBe('Quarterly revenue')); |
| 176 | + }); |
| 177 | + |
| 178 | + it('a favourite thread\'s stored label follows the header; a label still being read writes nothing', async () => { |
| 179 | + localStorage.setItem( |
| 180 | + STORE_KEY, |
| 181 | + JSON.stringify([ |
| 182 | + { id: 'conversation:c1', label: 'old words', href: '/ai/ask/c1', type: 'conversation', conversationId: 'c1', agent: 'ask', favoritedAt: '2026-11-27T00:00:00.000Z' }, |
| 183 | + ]), |
| 184 | + ); |
| 185 | + bodyState.actions = { ...SENT, label: undefined, labelPending: true }; |
| 186 | + renderRail(); |
| 187 | + await screen.findByTestId('default-dock-body'); |
| 188 | + expect(screen.getByTestId('chat-dock-favorite')).toHaveAttribute('aria-pressed', 'true'); |
| 189 | + expect(stored()[0].label).toBe('old words'); |
| 190 | + act(() => bodyState.report?.(SENT)); |
| 191 | + await waitFor(() => expect(stored()[0].label).toBe('Quarterly revenue')); |
| 192 | + }); |
| 193 | + |
| 194 | + it('control: a thread that is not a favourite is written nowhere', async () => { |
| 195 | + bodyState.actions = SENT; |
| 196 | + renderRail(); |
| 197 | + await screen.findByTestId('default-dock-body'); |
| 198 | + act(() => bodyState.report?.({ ...SENT, label: 'Renamed' })); |
| 199 | + expect(stored()).toEqual([]); |
| 200 | + }); |
| 201 | + |
| 202 | + it('the full window draws ☆ too', async () => { |
| 203 | + bodyState.actions = SENT; |
| 204 | + renderRail({ fullWindow: true }); |
| 205 | + await screen.findByTestId('default-dock-body'); |
| 206 | + expect(headerControls()).toEqual(['Add to favorites', 'New conversation', 'More actions', 'Close']); |
| 207 | + }); |
| 208 | + |
| 209 | + it('the phone sheet: ☆ first, finger-sized', async () => { |
| 210 | + bodyState.actions = SENT; |
| 211 | + render( |
| 212 | + <FavoritesProvider> |
| 213 | + <ChatDockMobileSheet open onOpenChange={() => {}} /> |
| 214 | + </FavoritesProvider>, |
| 215 | + ); |
| 216 | + await screen.findByTestId('default-dock-body'); |
| 217 | + const sheet = screen.getByTestId('chat-dock-mobile-sheet'); |
| 218 | + const names = Array.from(sheet.querySelectorAll('button')).map((b) => b.getAttribute('aria-label') ?? b.textContent?.trim() ?? ''); |
| 219 | + expect(names.slice(0, 4)).toEqual(['Add to favorites', 'New conversation', 'History', 'More actions']); |
| 220 | + const star = within(sheet).getByTestId('chat-dock-mobile-favorite'); |
| 221 | + expect(star).toHaveClass('h-9', 'w-9'); |
| 222 | + fireEvent.click(star); |
| 223 | + expect(stored().map((f) => f.id)).toEqual(['conversation:c1']); |
| 224 | + }); |
| 225 | +}); |
0 commit comments