|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#36150 — the record ROUTE's object scope follows the page the |
| 5 | + * record view draws. An authored full record page is drawn on the desktop |
| 6 | + * full page only; on a phone the view draws the standard page, whose child |
| 7 | + * tabs are derived from every object that points at the record's object. So |
| 8 | + * on a phone the route must not read only the objects the authored page binds |
| 9 | + * (L2 M1 on #36174): it takes the synthesized page's arm, as the view does. |
| 10 | + * |
| 11 | + * Through the real `AppContent` and `MetadataProvider` (the harness of |
| 12 | + * `AppContent.emptyPackageScope-13046.test.tsx`), the view a double that |
| 13 | + * prints the object list it is handed: |
| 14 | + * |
| 15 | + * - desktop: the authored page's objects, as before (CONTROL); a child the |
| 16 | + * page does not bind is not in the list. |
| 17 | + * - phone: the whole registry, the unbound child included. |
| 18 | + * - the width crossing 768 on an open record moves the scope with it. |
| 19 | + * - a media-query change that leaves the window wide (what printing fires) |
| 20 | + * keeps the authored page's scope (N4). |
| 21 | + * - the host's exceptions on a phone: a page named in the URL, and the |
| 22 | + * platform user page, still read their page's objects. |
| 23 | + */ |
| 24 | + |
| 25 | +import '@testing-library/jest-dom/vitest'; |
| 26 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 27 | +import React from 'react'; |
| 28 | +import { act, cleanup, render, screen, waitFor } from '@testing-library/react'; |
| 29 | +import { MemoryRouter, Routes, Route } from 'react-router-dom'; |
| 30 | +import { AuthProvider, ActiveOrganizationStorage } from '@object-ui/auth'; |
| 31 | +import type { AuthClient, AuthOrganization } from '@object-ui/auth'; |
| 32 | + |
| 33 | +vi.mock('@object-ui/plugin-designer', async (importOriginal) => ({ |
| 34 | + ...(await importOriginal<typeof import('@object-ui/plugin-designer')>()), |
| 35 | + CreateAppPage: () => null, |
| 36 | + EditAppPage: () => null, |
| 37 | + DashboardDesignPage: () => null, |
| 38 | +})); |
| 39 | +// The chrome: what the sidebar's capability gate asks, as `ConsoleLayout` does. |
| 40 | +vi.mock('../../layout/ConsoleLayout', async () => { |
| 41 | + const { useNavMemberGates } = await import('../../hooks/useNavMemberGates'); |
| 42 | + return { |
| 43 | + ConsoleLayout: ({ children }: { children?: React.ReactNode }) => { |
| 44 | + const gates = useNavMemberGates(); |
| 45 | + return ( |
| 46 | + <div data-testid="console-layout"> |
| 47 | + <span data-testid="members-entry">{String(gates.checkCapability('object', 'sys_member'))}</span> |
| 48 | + {children} |
| 49 | + </div> |
| 50 | + ); |
| 51 | + }, |
| 52 | + }; |
| 53 | +}); |
| 54 | +vi.mock('../../chrome/CommandPalette', () => ({ CommandPalette: () => null })); |
| 55 | +vi.mock('../../chrome/KeyboardShortcutsDialog', () => ({ KeyboardShortcutsDialog: () => null })); |
| 56 | +vi.mock('../../chrome/OnboardingWalkthrough', () => ({ OnboardingWalkthrough: () => null })); |
| 57 | +vi.mock('../../views/ObjectView', () => ({ |
| 58 | + ObjectView: ({ objects }: { objects: Array<{ name: string }> }) => ( |
| 59 | + <div data-testid="object-view">{objects.map((o) => o.name).join(',')}</div> |
| 60 | + ), |
| 61 | +})); |
| 62 | +vi.mock('../../views/RecordDetailView', () => ({ |
| 63 | + RecordDetailView: ({ objects }: { objects: Array<{ name: string }> }) => ( |
| 64 | + <div data-testid="record-view">{objects.map((o) => o.name).join(',')}</div> |
| 65 | + ), |
| 66 | +})); |
| 67 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 68 | + ...(await importOriginal<Record<string, unknown>>()), |
| 69 | + useObjectTranslation: () => ({ |
| 70 | + t: (key: string, options?: Record<string, unknown>) => String(options?.defaultValue ?? key), |
| 71 | + }), |
| 72 | + useObjectLabel: () => ({ objectLabel: ({ label }: { label?: string }) => label, objectPluralLabel: () => '' }), |
| 73 | +})); |
| 74 | + |
| 75 | +const dataSourceStub = { |
| 76 | + onConnectionStateChange: () => () => {}, |
| 77 | + getConnectionState: () => 'connected', |
| 78 | +}; |
| 79 | +vi.mock('../../providers/AdapterProvider', async (importOriginal) => ({ |
| 80 | + ...(await importOriginal<Record<string, unknown>>()), |
| 81 | + useAdapter: () => dataSourceStub, |
| 82 | +})); |
| 83 | + |
| 84 | +const actionRunnerStub = { registerHandler: vi.fn(), getContext: () => ({}) }; |
| 85 | +vi.mock('@object-ui/react', async (importOriginal) => ({ |
| 86 | + ...(await importOriginal<Record<string, unknown>>()), |
| 87 | + useActionRunner: () => ({ execute: vi.fn(), runner: actionRunnerStub }), |
| 88 | + useGlobalUndo: () => {}, |
| 89 | + useMutationInvalidationBridge: () => {}, |
| 90 | +})); |
| 91 | + |
| 92 | +// `AppContent` reaches a dynamic `import('@objectstack/lint')` (through |
| 93 | +// `ConsoleLayout` → `DraftPreviewBar`, statically): pay its load at collection, |
| 94 | +// never inside a case's clocked window (`check:test-source-alias`). |
| 95 | +import '@objectstack/lint'; |
| 96 | +import { AppContent } from '../AppContent'; |
| 97 | +import { MetadataProvider } from '../../providers/MetadataProvider'; |
| 98 | + |
| 99 | +const CLOUD = 'com.example.cloud'; |
| 100 | +const CRM = 'app.crm'; |
| 101 | +const OBJECTS = [ |
| 102 | + { name: 'sys_member', _packageId: 'plugin.auth', fields: { user: { type: 'lookup', reference: 'sys_user' } } }, |
| 103 | + { name: 'sys_user', _packageId: 'plugin.auth', fields: {} }, |
| 104 | + { name: 'crm_account', _packageId: CRM, fields: {} }, |
| 105 | + { name: 'crm_opportunity', _packageId: CRM, fields: { account: { type: 'lookup', reference: 'crm_account' } } }, |
| 106 | + { name: 'crm_quote', _packageId: 'app.sales', fields: { opportunity: { type: 'master_detail', reference: 'crm_opportunity' } } }, |
| 107 | + // A child of the opportunity the authored page does not bind. |
| 108 | + { name: 'crm_task', _packageId: 'app.service', fields: { opportunity: { type: 'lookup', reference: 'crm_opportunity' } } }, |
| 109 | +]; |
| 110 | +const CLOUD_NAVIGATION = [{ id: 'n1', type: 'object', label: 'Members', objectName: 'sys_member' }]; |
| 111 | +const CRM_NAVIGATION = [{ id: 'c1', type: 'object', label: 'Accounts', objectName: 'crm_account' }]; |
| 112 | +const OPPORTUNITY_PAGE = { |
| 113 | + name: 'opportunity_detail', |
| 114 | + type: 'record', |
| 115 | + object: 'crm_opportunity', |
| 116 | + kind: 'full', |
| 117 | + isDefault: true, |
| 118 | + regions: [{ name: 'main', components: [{ type: 'record:related_list', properties: { objectName: 'crm_quote' } }] }], |
| 119 | +}; |
| 120 | +const USER_PAGE = { |
| 121 | + name: 'user_detail', |
| 122 | + type: 'record', |
| 123 | + object: 'sys_user', |
| 124 | + kind: 'full', |
| 125 | + isDefault: true, |
| 126 | + regions: [{ name: 'main', components: [{ type: 'record:related_list', properties: { objectName: 'sys_member' } }] }], |
| 127 | +}; |
| 128 | + |
| 129 | +const server = { calls: [] as string[], apps: [] as unknown[], pages: [] as unknown[] }; |
| 130 | + |
| 131 | +function makeAdapter() { |
| 132 | + return { |
| 133 | + clearCache: vi.fn(), |
| 134 | + invalidateCache: vi.fn(), |
| 135 | + // objectui#13044 — the provider's by-name OBJECT read goes through the |
| 136 | + // adapter's `getObjectSchema`; this double answers it from the same door. |
| 137 | + getObjectSchema(this: { getClient(): any }, name: string) { |
| 138 | + return this.getClient() |
| 139 | + .meta.getItem('object', name) |
| 140 | + .then((res: any) => (res && typeof res === 'object' && 'item' in res ? res.item : res)); |
| 141 | + }, |
| 142 | + getClient: () => ({ |
| 143 | + meta: { |
| 144 | + getItems: (type: string, options?: { packageId?: string; names?: readonly string[] }) => { |
| 145 | + // objectstack#34244 — the `?names=` presence read answers the asked names it holds and leaves an absent one out, as the list door does. |
| 146 | + if (options?.names) { |
| 147 | + server.calls.push(`${type}?names=${options.names.join(',')}`); |
| 148 | + const named: any[] = type === 'object' ? (OBJECTS as any[]).filter((o: any) => options.names!.includes(o.name)) : []; |
| 149 | + return Promise.resolve({ type, items: structuredClone(named) }); |
| 150 | + } |
| 151 | + server.calls.push(options?.packageId ? `${type}?package=${options.packageId}` : type); |
| 152 | + const all = type === 'object' ? OBJECTS : type === 'app' ? server.apps : type === 'page' ? server.pages : []; |
| 153 | + const items = options?.packageId ? all.filter((o: any) => o._packageId === options.packageId) : all; |
| 154 | + return Promise.resolve({ type, items: structuredClone(items) }); |
| 155 | + }, |
| 156 | + getItem: (type: string, name: string) => { |
| 157 | + server.calls.push(`${type}/${name}`); |
| 158 | + const item = (type === 'object' ? OBJECTS : []).find((o) => o.name === name); |
| 159 | + return item |
| 160 | + ? Promise.resolve({ type, name, item: structuredClone(item) }) |
| 161 | + : Promise.reject(Object.assign(new Error('not found'), { httpStatus: 404, code: 'RESOURCE_NOT_FOUND' })); |
| 162 | + }, |
| 163 | + }, |
| 164 | + }), |
| 165 | + } as unknown as Parameters<typeof MetadataProvider>[0]['adapter']; |
| 166 | +} |
| 167 | + |
| 168 | +const USER = 'u_36150'; |
| 169 | +const ORG: AuthOrganization = { id: 'org_a', name: 'Org A', slug: 'a' }; |
| 170 | + |
| 171 | +function authClient(): AuthClient { |
| 172 | + return { |
| 173 | + getSession: vi.fn(async () => ({ |
| 174 | + user: { id: USER, name: 'Ada', email: 'ada@test.com', role: 'admin', positions: ['admin'] }, |
| 175 | + session: { token: 'tok-36150', activeOrganizationId: ORG.id }, |
| 176 | + })), |
| 177 | + signOut: vi.fn(async () => undefined), |
| 178 | + getConfig: vi.fn(async () => ({ features: {} })), |
| 179 | + listOrganizations: vi.fn(async () => [ORG]), |
| 180 | + getActiveOrganization: vi.fn(async () => ORG), |
| 181 | + setActiveOrganization: vi.fn(async () => ORG), |
| 182 | + getActiveMember: vi.fn(async () => ({ id: 'm1', organizationId: ORG.id, userId: USER, role: 'owner' })), |
| 183 | + } as unknown as AuthClient; |
| 184 | +} |
| 185 | + |
| 186 | +function boot(url: string) { |
| 187 | + render( |
| 188 | + <AuthProvider authUrl="/api/auth" client={authClient()}> |
| 189 | + <MetadataProvider adapter={makeAdapter()}> |
| 190 | + <MemoryRouter initialEntries={[url]}> |
| 191 | + <Routes> |
| 192 | + <Route path="/apps/:appName/*" element={<AppContent />} /> |
| 193 | + </Routes> |
| 194 | + </MemoryRouter> |
| 195 | + </MetadataProvider> |
| 196 | + </AuthProvider>, |
| 197 | + ); |
| 198 | +} |
| 199 | + |
| 200 | +const objectCalls = () => server.calls.filter((c) => c === 'object' || c.startsWith('object?') || c.startsWith('object/')); |
| 201 | +const settle = () => new Promise((resolve) => setTimeout(resolve, 50)); |
| 202 | +const listed = () => (screen.getByTestId('record-view').textContent ?? '').split(','); |
| 203 | + |
| 204 | +// The viewport: `usePhoneViewport` reads `innerWidth` and listens to the |
| 205 | +// media query; this double lets a case move the width and fire the query. |
| 206 | +const mediaListeners = new Set<() => void>(); |
| 207 | +function setWidth(width: number) { |
| 208 | + Object.defineProperty(window, 'innerWidth', { value: width, configurable: true, writable: true }); |
| 209 | +} |
| 210 | +function fireMediaChange() { |
| 211 | + act(() => { |
| 212 | + for (const listener of [...mediaListeners]) listener(); |
| 213 | + }); |
| 214 | +} |
| 215 | +const originalWidth = window.innerWidth; |
| 216 | +const originalMatchMedia = window.matchMedia; |
| 217 | + |
| 218 | +beforeEach(() => { |
| 219 | + server.calls = []; |
| 220 | + server.pages = [OPPORTUNITY_PAGE, USER_PAGE]; |
| 221 | + server.apps = [ |
| 222 | + { name: 'cloud', label: 'Cloud', _packageId: CLOUD, navigation: CLOUD_NAVIGATION }, |
| 223 | + { name: 'crm', label: 'CRM', _packageId: CRM, navigation: CRM_NAVIGATION }, |
| 224 | + ]; |
| 225 | + localStorage.clear(); |
| 226 | + sessionStorage.clear(); |
| 227 | + ActiveOrganizationStorage.clear(); |
| 228 | + mediaListeners.clear(); |
| 229 | + setWidth(1280); |
| 230 | + window.matchMedia = ((query: string) => { |
| 231 | + const max = Number(/max-width:\s*(\d+)px/.exec(query)?.[1] ?? Infinity); |
| 232 | + return { |
| 233 | + media: query, |
| 234 | + get matches() { |
| 235 | + return window.innerWidth <= max; |
| 236 | + }, |
| 237 | + onchange: null, |
| 238 | + addEventListener: (_type: string, listener: () => void) => mediaListeners.add(listener), |
| 239 | + removeEventListener: (_type: string, listener: () => void) => mediaListeners.delete(listener), |
| 240 | + addListener: (listener: () => void) => mediaListeners.add(listener), |
| 241 | + removeListener: (listener: () => void) => mediaListeners.delete(listener), |
| 242 | + dispatchEvent: () => false, |
| 243 | + }; |
| 244 | + }) as unknown as typeof window.matchMedia; |
| 245 | +}); |
| 246 | + |
| 247 | +afterEach(() => { |
| 248 | + cleanup(); |
| 249 | + ActiveOrganizationStorage.clear(); |
| 250 | + setWidth(originalWidth); |
| 251 | + window.matchMedia = originalMatchMedia; |
| 252 | +}); |
| 253 | + |
| 254 | +describe('the record route reads the objects of the page the view draws (objectstack#36150)', () => { |
| 255 | + it('CONTROL — desktop: the authored page\'s objects, by name; the child it does not bind is not read', async () => { |
| 256 | + boot('/apps/crm/crm_opportunity/record/r1'); |
| 257 | + await waitFor(() => expect(screen.getByTestId('record-view')).toHaveTextContent('crm_quote')); |
| 258 | + await settle(); |
| 259 | + expect(objectCalls()).not.toContain('object'); |
| 260 | + expect(listed()).not.toContain('crm_task'); |
| 261 | + }); |
| 262 | + |
| 263 | + it('phone: the standard page\'s scope, every object pointing at the record\'s, the unbound child included', async () => { |
| 264 | + setWidth(375); |
| 265 | + boot('/apps/crm/crm_opportunity/record/r1'); |
| 266 | + await waitFor(() => expect(listed()).toContain('crm_task')); |
| 267 | + expect(listed()).toContain('crm_quote'); |
| 268 | + expect(objectCalls()).toContain('object'); |
| 269 | + }); |
| 270 | + |
| 271 | + it('the width crossing 768 on an open record moves the scope with it', async () => { |
| 272 | + boot('/apps/crm/crm_opportunity/record/r1'); |
| 273 | + await waitFor(() => expect(screen.getByTestId('record-view')).toHaveTextContent('crm_quote')); |
| 274 | + await settle(); |
| 275 | + expect(listed()).not.toContain('crm_task'); |
| 276 | + setWidth(600); |
| 277 | + fireMediaChange(); |
| 278 | + await waitFor(() => expect(listed()).toContain('crm_task')); |
| 279 | + }); |
| 280 | + |
| 281 | + it('N4 — a media-query change that leaves the window wide (printing fires one) keeps the authored page\'s scope', async () => { |
| 282 | + boot('/apps/crm/crm_opportunity/record/r1'); |
| 283 | + await waitFor(() => expect(screen.getByTestId('record-view')).toHaveTextContent('crm_quote')); |
| 284 | + await settle(); |
| 285 | + act(() => { |
| 286 | + window.dispatchEvent(new Event('beforeprint')); |
| 287 | + }); |
| 288 | + fireMediaChange(); |
| 289 | + await settle(); |
| 290 | + expect(objectCalls()).not.toContain('object'); |
| 291 | + expect(listed()).not.toContain('crm_task'); |
| 292 | + }); |
| 293 | + |
| 294 | + it('phone: a page named in the URL still reads its own objects, never the whole registry', async () => { |
| 295 | + setWidth(375); |
| 296 | + boot('/apps/crm/crm_opportunity/record/r1/page/opportunity_detail'); |
| 297 | + await waitFor(() => expect(screen.getByTestId('record-view')).toHaveTextContent('crm_quote')); |
| 298 | + await settle(); |
| 299 | + expect(objectCalls()).not.toContain('object'); |
| 300 | + expect(listed()).not.toContain('crm_task'); |
| 301 | + }); |
| 302 | + |
| 303 | + it('phone: the platform user page keeps its own page, so its route reads that page\'s objects', async () => { |
| 304 | + setWidth(375); |
| 305 | + boot('/apps/cloud/sys_user/record/u1'); |
| 306 | + await waitFor(() => expect(screen.getByTestId('record-view')).toHaveTextContent('sys_member')); |
| 307 | + await settle(); |
| 308 | + expect(objectCalls()).not.toContain('object'); |
| 309 | + }); |
| 310 | +}); |
0 commit comments