|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#26838 — when a URL names no app the shell can resolve (a |
| 5 | + * pseudo-route such as `/apps/ghost/system/marketplace`), `AppContent` falls |
| 6 | + * back to the default launcher app. That fallback never lands on an app the |
| 7 | + * user's permission sets hide (`tabPermissions: hidden`): its list is the one |
| 8 | + * Home's tiles and the switcher offer, narrowed by the `GET /me/apps` answer. |
| 9 | + * |
| 10 | + * Under the real `LauncherAppsProvider` fed a `/me/apps` double; the routing |
| 11 | + * harness (stubs, `ConsoleLayout` probe echoing `activeAppName`) is |
| 12 | + * `AppContent.pseudoRouteSegments.test.tsx`'s. |
| 13 | + */ |
| 14 | + |
| 15 | +import '@testing-library/jest-dom/vitest'; |
| 16 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 17 | +import React from 'react'; |
| 18 | +import { cleanup, render, screen, waitFor } from '@testing-library/react'; |
| 19 | +import { |
| 20 | + MemoryRouter, |
| 21 | + Routes, |
| 22 | + Route, |
| 23 | + Navigate, |
| 24 | + useParams, |
| 25 | +} from 'react-router-dom'; |
| 26 | + |
| 27 | +// --------------------------------------------------------------------------- |
| 28 | +// Mocks — everything that takes part in the routing decision (AppContent's |
| 29 | +// guards, its nested <Routes>, react-router's matching) stays real. |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | + |
| 32 | +// AGENTS.md §测试纪律 — the lazy page modules are stubbed so no unbounded |
| 33 | +// `import()` races a bounded `findBy` window. |
| 34 | +vi.mock('../../views/metadata-admin', () => ({ |
| 35 | + MetadataDirectoryPage: () => <div data-testid="metadata-directory-page">directory</div>, |
| 36 | + MetadataResourceListPage: () => { |
| 37 | + const { type } = useParams<{ type?: string }>(); |
| 38 | + return <div data-testid="metadata-resource-list-page">{type}</div>; |
| 39 | + }, |
| 40 | + MetadataResourceEditPage: () => <div data-testid="metadata-resource-edit-page" />, |
| 41 | + MetadataResourceHistoryPage: () => <div data-testid="metadata-resource-history-page" />, |
| 42 | + MetadataDiagnosticsPage: () => <div data-testid="metadata-diagnostics-page" />, |
| 43 | +})); |
| 44 | + |
| 45 | +vi.mock('../marketplace/MarketplacePage', () => ({ |
| 46 | + MarketplacePage: () => <div data-testid="marketplace-page">marketplace</div>, |
| 47 | +})); |
| 48 | +vi.mock('../marketplace/MarketplacePackagePage', () => ({ |
| 49 | + MarketplacePackagePage: () => <div data-testid="marketplace-package-page" />, |
| 50 | +})); |
| 51 | + |
| 52 | +vi.mock('@object-ui/plugin-designer', async (importOriginal) => ({ |
| 53 | + ...(await importOriginal<typeof import('@object-ui/plugin-designer')>()), |
| 54 | + CreateAppPage: () => <div data-testid="create-app-page">create app</div>, |
| 55 | + EditAppPage: () => <div data-testid="edit-app-page" />, |
| 56 | + DashboardDesignPage: () => <div data-testid="dashboard-design-page" />, |
| 57 | +})); |
| 58 | + |
| 59 | +// Pay the real designer barrel's load in the IMPORT phase, where no timeout |
| 60 | +// applies. `AppContent` reaches `CreateAppPage` only through |
| 61 | +// `React.lazy(() => import('@object-ui/plugin-designer'))`, so the factory |
| 62 | +// above -- which now awaits the real module -- first runs when that lazy |
| 63 | +// boundary resolves, i.e. INSIDE the `findByTestId` budget of the two cases |
| 64 | +// below that assert on `create-app-page`. Under a saturated transform pipeline |
| 65 | +// the barrel's graph does not fit in that budget and the file goes red on LOAD |
| 66 | +// rather than on behaviour (AGENTS.md, the flaky-test discipline: an unbounded |
| 67 | +// module load counted against a bounded window). The specifier is byte- |
| 68 | +// identical to the component's own, so ESM hands the lazy factory this |
| 69 | +// already-resolved module. |
| 70 | +import '@object-ui/plugin-designer'; |
| 71 | + |
| 72 | +/** |
| 73 | + * The probe that makes the bug visible: it reports WHICH app's shell rendered. |
| 74 | + * The real `ConsoleLayout` drags in the whole console chrome (sidebar, header, |
| 75 | + * chat dock) — none of it part of this question. |
| 76 | + */ |
| 77 | +vi.mock('../../layout/ConsoleLayout', () => ({ |
| 78 | + ConsoleLayout: ({ activeAppName, children }: { activeAppName?: string; children?: React.ReactNode }) => ( |
| 79 | + <div data-testid="console-layout" data-active-app={activeAppName}> |
| 80 | + {children} |
| 81 | + </div> |
| 82 | + ), |
| 83 | +})); |
| 84 | +vi.mock('../../chrome/CommandPalette', () => ({ CommandPalette: () => null })); |
| 85 | +vi.mock('../../chrome/KeyboardShortcutsDialog', () => ({ KeyboardShortcutsDialog: () => null })); |
| 86 | +vi.mock('../../chrome/OnboardingWalkthrough', () => ({ OnboardingWalkthrough: () => null })); |
| 87 | + |
| 88 | +/** Echoes `:objectName` — the near-miss segment is taken as an object name. */ |
| 89 | +vi.mock('../../views/ObjectView', () => ({ |
| 90 | + ObjectView: () => { |
| 91 | + const { objectName } = useParams<{ objectName?: string }>(); |
| 92 | + return <div data-testid="object-view">{objectName}</div>; |
| 93 | + }, |
| 94 | +})); |
| 95 | + |
| 96 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 97 | + ...(await importOriginal<Record<string, unknown>>()), |
| 98 | + useObjectTranslation: () => ({ |
| 99 | + t: (key: string, options?: Record<string, unknown>) => String(options?.defaultValue ?? key), |
| 100 | + }), |
| 101 | + useObjectLabel: () => ({ |
| 102 | + objectLabel: ({ label }: { label?: string }) => label, |
| 103 | + }), |
| 104 | +})); |
| 105 | + |
| 106 | +// A workspace admin: the zero-app cases below land on the "no apps configured" |
| 107 | +// empty state, which since objectui#4473 renders for admins only — a viewer who |
| 108 | +// cannot act on its CTAs is bounced to `/home` with the shell chrome instead of |
| 109 | +// being stranded there (`AppContent.inaccessibleAppStrand.test.tsx`). Which |
| 110 | +// BRANCH a segment reaches — the question this file measures — does not depend |
| 111 | +// on the viewer. |
| 112 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 113 | + ...(await importOriginal<Record<string, unknown>>()), |
| 114 | + // A signed-in user: the `/me/apps` read is asked per user. |
| 115 | + useAuth: () => ({ |
| 116 | + user: { id: 'u1' }, |
| 117 | + getAuthConfig: async () => ({ features: {} }), |
| 118 | + activeOrganization: null, |
| 119 | + }), |
| 120 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 121 | +})); |
| 122 | + |
| 123 | +const dataSourceStub = { |
| 124 | + onConnectionStateChange: () => () => {}, |
| 125 | + getConnectionState: () => 'connected', |
| 126 | +}; |
| 127 | +vi.mock('../../providers/AdapterProvider', async (importOriginal) => ({ |
| 128 | + ...(await importOriginal<Record<string, unknown>>()), |
| 129 | + useAdapter: () => dataSourceStub, |
| 130 | +})); |
| 131 | + |
| 132 | +/** |
| 133 | + * Two published apps, one of them the default. The default is what a broken |
| 134 | + * `requestedAppMissing` falls back to, so `crm` is the app that must NEVER |
| 135 | + * appear for a `/apps/<mistyped>/…` URL. |
| 136 | + */ |
| 137 | +const APPS = [ |
| 138 | + { name: 'crm', label: 'CRM', isDefault: true, navigation: [] }, |
| 139 | + { name: 'sales', label: 'Sales', navigation: [] }, |
| 140 | +]; |
| 141 | + |
| 142 | +const refreshMetadata = vi.fn(async () => {}); |
| 143 | +let metadataApps: unknown[] = APPS; |
| 144 | +vi.mock('../../providers/MetadataProvider', async (importOriginal) => ({ |
| 145 | + ...(await importOriginal<Record<string, unknown>>()), |
| 146 | + useMetadata: () => ({ |
| 147 | + apps: metadataApps, |
| 148 | + objects: [], |
| 149 | + loading: false, |
| 150 | + // `undefined` — no bucket preloading to await, so the shell is ready on |
| 151 | + // first render (mirrors a host that ships metadata eagerly). |
| 152 | + ensureType: undefined, |
| 153 | + error: null, |
| 154 | + refresh: refreshMetadata, |
| 155 | + }), |
| 156 | +})); |
| 157 | + |
| 158 | +import { AppContent } from '../AppContent'; |
| 159 | +import { LauncherAppsProvider } from '../../hooks/useLauncherApps'; |
| 160 | + |
| 161 | +/** A `/me/apps` double: the listed names, or a bare status. */ |
| 162 | +function meApps(answer: string[] | number) { |
| 163 | + return vi.fn(async (_url: unknown) => |
| 164 | + typeof answer === 'number' |
| 165 | + ? new Response('{}', { status: answer }) |
| 166 | + : new Response(JSON.stringify({ apps: answer.map((name) => ({ name })) }), { status: 200 }), |
| 167 | + ); |
| 168 | +} |
| 169 | + |
| 170 | +function renderAt(url: string, fetcher: ReturnType<typeof meApps>) { |
| 171 | + render( |
| 172 | + <MemoryRouter initialEntries={[url]}> |
| 173 | + <LauncherAppsProvider endpoint="/me-apps-double" fetcher={fetcher as unknown as typeof fetch}> |
| 174 | + <Routes> |
| 175 | + <Route path="/apps/:appName/*" element={<AppContent />} /> |
| 176 | + <Route path="/home" element={<div data-testid="home-launcher">home</div>} /> |
| 177 | + <Route path="*" element={<Navigate to="/home" replace />} /> |
| 178 | + </Routes> |
| 179 | + </LauncherAppsProvider> |
| 180 | + </MemoryRouter>, |
| 181 | + ); |
| 182 | +} |
| 183 | + |
| 184 | +beforeEach(() => { |
| 185 | + vi.clearAllMocks(); |
| 186 | + metadataApps = APPS; |
| 187 | +}); |
| 188 | + |
| 189 | +afterEach(() => cleanup()); |
| 190 | + |
| 191 | +describe('the default-app fallback skips an app a permission set hides (objectstack#26838)', () => { |
| 192 | + it('the default app hidden by /me/apps: the fallback lands on the next launcher app', async () => { |
| 193 | + const fetcher = meApps(['sales']); |
| 194 | + renderAt('/apps/ghost/system/marketplace', fetcher); |
| 195 | + expect(await screen.findByTestId('marketplace-page')).toBeInTheDocument(); |
| 196 | + await waitFor(() => expect(fetcher).toHaveBeenCalledTimes(1)); |
| 197 | + await waitFor(() => |
| 198 | + expect(screen.getByTestId('console-layout')).toHaveAttribute('data-active-app', 'sales'), |
| 199 | + ); |
| 200 | + }); |
| 201 | + |
| 202 | + it('CONTROL — a 404 (no security plugin) keeps the declared default', async () => { |
| 203 | + const fetcher = meApps(404); |
| 204 | + renderAt('/apps/ghost/system/marketplace', fetcher); |
| 205 | + expect(await screen.findByTestId('marketplace-page')).toBeInTheDocument(); |
| 206 | + await waitFor(() => expect(fetcher).toHaveBeenCalledTimes(1)); |
| 207 | + expect(screen.getByTestId('console-layout')).toHaveAttribute('data-active-app', 'crm'); |
| 208 | + }); |
| 209 | + |
| 210 | + it('a URL that names the hidden app still opens it', async () => { |
| 211 | + renderAt('/apps/crm/system/marketplace', meApps(['sales'])); |
| 212 | + expect(await screen.findByTestId('marketplace-page')).toBeInTheDocument(); |
| 213 | + expect(screen.getByTestId('console-layout')).toHaveAttribute('data-active-app', 'crm'); |
| 214 | + }); |
| 215 | +}); |
0 commit comments