|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#35017 — where Home draws the "Connect an AI Assistant" card. |
| 5 | + * |
| 6 | + * On the real `HomePage`, with the card itself stood in by a marker (whether |
| 7 | + * it shows is `ConnectAssistantCard-30895.test.tsx`'s, unchanged): |
| 8 | + * |
| 9 | + * - with apps, the first screen reads greeting, My apps, My work, and only |
| 10 | + * then the card; with no My work section it still follows the apps; |
| 11 | + * - with no app yet, it follows the empty state's welcome, not above it. |
| 12 | + * |
| 13 | + * The harness is `HomePage.greetingAndApps-12601.test.tsx`'s: every source the |
| 14 | + * page reads is stubbed, `t` echoes the key. |
| 15 | + */ |
| 16 | + |
| 17 | +// HomePage reaches `import('@objectstack/lint')` (usePublishAllDrafts → |
| 18 | +// capabilityLint); pay that load at collection, not inside a case's timeout. |
| 19 | +import '@objectstack/lint'; |
| 20 | +import '@testing-library/jest-dom/vitest'; |
| 21 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 22 | +import { render, screen, cleanup } from '@testing-library/react'; |
| 23 | + |
| 24 | +const navigateMock = vi.fn(); |
| 25 | + |
| 26 | +const state = vi.hoisted(() => ({ |
| 27 | + apps: [] as Array<Record<string, unknown>>, |
| 28 | + readable: true, |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock('react-router-dom', async () => { |
| 32 | + const { createElement } = await import('react'); |
| 33 | + return { |
| 34 | + useNavigate: () => navigateMock, |
| 35 | + Link: ({ to, ...rest }: { to: string } & Record<string, unknown>) => |
| 36 | + createElement('a', { ...rest, href: to }), |
| 37 | + }; |
| 38 | +}); |
| 39 | + |
| 40 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 41 | + ...(await importOriginal<Record<string, unknown>>()), |
| 42 | + useObjectTranslation: () => ({ t: (key: string) => `«${key}»`, language: 'en' }), |
| 43 | + useObjectLabel: () => { |
| 44 | + const label = (x: { name?: string; label?: string }) => String(x?.label || x?.name || ''); |
| 45 | + return { |
| 46 | + appLabel: label, |
| 47 | + objectLabel: label, |
| 48 | + objectPluralLabel: label, |
| 49 | + dashboardLabel: label, |
| 50 | + pageLabel: label, |
| 51 | + reportLabel: label, |
| 52 | + viewLabel: (_object: string, _view: string, fallback?: string) => fallback ?? '', |
| 53 | + }; |
| 54 | + }, |
| 55 | +})); |
| 56 | + |
| 57 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 58 | + ...(await importOriginal<Record<string, unknown>>()), |
| 59 | + useAuth: () => ({ user: { id: 'u1', name: 'Dev Admin', email: 'dev.admin@example.com' } }), |
| 60 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 61 | +})); |
| 62 | + |
| 63 | +vi.mock('@object-ui/plugin-chatbot/core', async (importOriginal) => ({ |
| 64 | + ...(await importOriginal<typeof import('@object-ui/plugin-chatbot/core')>()), |
| 65 | + useAgents: () => ({ agents: [{ name: 'custom_helper' }] }), |
| 66 | + isAskAgent: () => false, |
| 67 | + agentHasCapability: () => false, |
| 68 | +})); |
| 69 | + |
| 70 | +vi.mock('../../../providers/MetadataProvider', () => ({ |
| 71 | + useMetadata: () => ({ apps: state.apps, objects: [], dashboards: [], pages: [], reports: [], loading: false }), |
| 72 | +})); |
| 73 | +vi.mock('../../../context/NavigationContext', () => ({ |
| 74 | + useNavigationContext: () => ({ currentAppName: undefined }), |
| 75 | +})); |
| 76 | +vi.mock('../../../hooks/useCanAuthorMetadata', () => ({ |
| 77 | + useCanAuthorMetadata: () => true, |
| 78 | + useCanReadPendingDrafts: () => true, |
| 79 | +})); |
| 80 | +// `readable: false` on both sources and no drafts row: My work draws nothing. |
| 81 | +vi.mock('../../../hooks/useHomeInbox', () => ({ |
| 82 | + useHomeInbox: () => ({ |
| 83 | + pendingApprovalsCount: 0, |
| 84 | + pendingApprovals: [], |
| 85 | + pendingApprovalsReadable: state.readable, |
| 86 | + notificationsReadable: state.readable, |
| 87 | + pendingApprovalsStatus: 'ready', |
| 88 | + notifications: [], |
| 89 | + unreadTopicCount: 0, |
| 90 | + notificationsStatus: 'ready', |
| 91 | + activity: { value: [], status: 'ready', readable: true }, |
| 92 | + }), |
| 93 | +})); |
| 94 | +vi.mock('../../../runtime-config', () => ({ |
| 95 | + getRuntimeConfig: () => ({ branding: { productName: 'ObjectStack' } }), |
| 96 | + isMarketplaceEnabled: () => true, |
| 97 | + isAiStudioEnabled: () => true, |
| 98 | +})); |
| 99 | +vi.mock('../../../hooks/useRecentItems', () => ({ useRecentItems: () => ({ recentItems: [] }) })); |
| 100 | +vi.mock('../../../hooks/useFavorites', () => ({ useFavorites: () => ({ favorites: [] }) })); |
| 101 | +vi.mock('../../../hooks/useAiSurface', () => ({ resolveAiApiBase: () => '' })); |
| 102 | +vi.mock('../../../preview/usePendingDrafts', () => ({ usePendingDrafts: () => ({ count: 0 }) })); |
| 103 | +vi.mock('../../../preview/usePublishAllDrafts', () => ({ |
| 104 | + usePublishAllDrafts: () => ({ publishAll: async () => ({ ok: true }), publishing: false }), |
| 105 | +})); |
| 106 | +vi.mock('../../../hooks/useInboxBell', () => ({ useInboxBell: () => ({ markRead: vi.fn() }) })); |
| 107 | +vi.mock('../../deployment-notice/DeploymentNotice', () => ({ DeploymentNotice: () => null })); |
| 108 | + |
| 109 | +// The card stood in by a marker that always draws, so only its place is read. |
| 110 | +vi.mock('../ConnectAssistantCard', async () => { |
| 111 | + const { createElement } = await import('react'); |
| 112 | + return { |
| 113 | + ConnectAssistantCard: ({ className }: { className?: string }) => |
| 114 | + createElement('section', { 'data-testid': 'home-connect-assistant', className }), |
| 115 | + }; |
| 116 | +}); |
| 117 | + |
| 118 | +import { HomePage } from '../HomePage'; |
| 119 | + |
| 120 | +const CRM = { name: 'crm', label: 'CRM', icon: 'Building2' }; |
| 121 | + |
| 122 | +beforeEach(() => { |
| 123 | + navigateMock.mockReset(); |
| 124 | + state.apps = [CRM]; |
| 125 | + state.readable = true; |
| 126 | + vi.stubGlobal('fetch', vi.fn(async () => ({ ok: false, status: 404, json: async () => ({}) }))); |
| 127 | +}); |
| 128 | + |
| 129 | +afterEach(() => { |
| 130 | + cleanup(); |
| 131 | + vi.unstubAllGlobals(); |
| 132 | +}); |
| 133 | + |
| 134 | +/** `a` comes before `b` in document order. */ |
| 135 | +function before(a: Element, b: Element): boolean { |
| 136 | + return Boolean(a.compareDocumentPosition(b) & Node.DOCUMENT_POSITION_FOLLOWING); |
| 137 | +} |
| 138 | + |
| 139 | +describe('with apps: greeting, My apps, My work, then the card (objectstack#35017)', () => { |
| 140 | + it('draws the card after the greeting, the apps strip and My work', () => { |
| 141 | + render(<HomePage />); |
| 142 | + const greeting = screen.getByRole('heading', { level: 1 }); |
| 143 | + const apps = screen.getByTestId('app-tile-crm'); |
| 144 | + const work = screen.getByTestId('home-my-work'); |
| 145 | + const card = screen.getByTestId('home-connect-assistant'); |
| 146 | + expect(before(greeting, apps)).toBe(true); |
| 147 | + expect(before(apps, work)).toBe(true); |
| 148 | + expect(before(work, card)).toBe(true); |
| 149 | + }); |
| 150 | + |
| 151 | + it('with no My work section, the card still follows the greeting and the apps', () => { |
| 152 | + state.readable = false; |
| 153 | + render(<HomePage />); |
| 154 | + expect(screen.queryByTestId('home-my-work')).toBeNull(); |
| 155 | + const card = screen.getByTestId('home-connect-assistant'); |
| 156 | + expect(before(screen.getByRole('heading', { level: 1 }), card)).toBe(true); |
| 157 | + expect(before(screen.getByTestId('app-tile-crm'), card)).toBe(true); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
| 161 | +describe('with no app yet: after the welcome (objectstack#35017)', () => { |
| 162 | + it('draws the card after the empty state, not above it', () => { |
| 163 | + state.apps = []; |
| 164 | + render(<HomePage />); |
| 165 | + const welcome = screen.getByText('«home.welcome»'); |
| 166 | + const card = screen.getByTestId('home-connect-assistant'); |
| 167 | + expect(before(welcome, card)).toBe(true); |
| 168 | + }); |
| 169 | +}); |
0 commit comments