Repository navigation
Expand file tree
/
Copy pathApp.nativeShellMode-29039.test.tsx
More file actions
449 lines (414 loc) · 19.5 KB
/
Copy pathApp.nativeShellMode-29039.test.tsx
File metadata and controls
449 lines (414 loc) · 19.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Native shell mode, as the real `App` wires it to its router.
*
* The ObjectStack mobile shell injects `window.ObjectStackNative` before the
* Console's scripts run. With a fake bridge installed before `App` renders,
* the Console must post `ready()` once, after the first route has rendered,
* post `routeChanged` with the router path on every navigation, and move its
* own router (no document reload) when the shell dispatches
* `objectstack-native:navigate`. Without the global, every one of those is
* absent: that is the control half of each case.
*
* `App` is the real module under the real `BrowserRouter`; the pages and the
* shell chrome are stubs, as in `App.docsPortalLazy.test.tsx`, because this
* file is about the router wiring, not about what the pages draw.
*/
import { describe, expect, it, vi, afterEach, beforeEach } from 'vitest';
import { render, screen, cleanup, act } from '@testing-library/react';
import type { ReactNode } from 'react';
import {
NATIVE_CONNECTION_REMOVED_EVENT,
NATIVE_HOST_GLOBAL,
NATIVE_NAVIGATE_EVENT,
resetNativeHostEventsForTests,
type ObjectStackNative,
} from '../../../../packages/mobile/src/nativeHost';
import { resetNativeHostWatchForTests } from '../../../../packages/mobile/src/useNativeHost';
import {
NATIVE_PUSH_PROMPT_DELAY_MS,
NATIVE_PUSH_REGISTRATION_KEY,
resetNativePushRegistrationForTests,
} from '../nativePushRegistration';
const { passthrough, stub, shellState, authState } = vi.hoisted(() => {
const passthrough = ({ children }: { children?: ReactNode }) => <>{children}</>;
const stub = (testid: string) => () => <div data-testid={testid} />;
// objectstack#29750: what the shell's navigation context and the metadata
// provider answer. The defaults are what this file's other cases read
// without either provider: no current app, an empty app list, not loading.
const shellState = {
currentAppName: undefined as string | undefined,
apps: [] as Array<Record<string, unknown>>,
loading: false,
appStatus: undefined as 'idle' | 'loading' | 'ready' | 'error' | undefined,
};
// objectstack#29042: who the auth context names. The default is what this
// file's other cases read: a user id and no session answer.
const authState = { value: { user: { id: 'u1' } } as Record<string, unknown> };
return { passthrough, stub, shellState, authState };
});
vi.mock('@object-ui/app-shell', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
ConsoleShell: passthrough,
// The organization gate over `/home`, `/apps` and `/forms` sits beside the
// stubbed `ProtectedRoute` now, not inside it (objectstack#32633); auth and
// organizations are not the question here either.
RequireOrganization: ({ children }: { children?: ReactNode }) => <>{children}</>,
ConsoleToaster: () => null,
LoadingScreen: stub('loading-screen'),
// Records where it would go, so the catch-all's answer is readable.
RedirectWithSplash: ({ to }: { to: string }) => <div data-testid="redirect-with-splash" data-to={to} />,
LoadingFallback: stub('loading-fallback'),
useNavigationContext: () => ({
context: 'app',
setContext: () => {},
currentAppName: shellState.currentAppName,
setCurrentAppName: () => {},
recordTitle: undefined,
setRecordTitle: () => {},
}),
useMetadata: () => ({
apps: shellState.apps,
loading: shellState.loading,
getTypeStatus: (type: string) => (type === 'app' ? shellState.appStatus : undefined),
}),
RequireAiSurface: passthrough,
SystemRedirect: () => null,
DefaultHomeLayout: passthrough,
DefaultHomePage: stub('home-page'),
DefaultOrganizationsLayout: passthrough,
DefaultOrganizationsPage: stub('organizations-page'),
DefaultOrganizationLayout: stub('organization-layout'),
DefaultMembersPage: stub('members-page'),
DefaultInvitationsPage: stub('invitations-page'),
DefaultSettingsPage: stub('settings-page'),
DefaultAcceptInvitationPage: stub('accept-invitation-page'),
AiAddressRedirect: stub('ai-chat-page'),
StudioDesignSurface: stub('studio-design-surface'),
BuilderLanding: stub('builder-landing'),
getProductName: () => 'ObjectOS',
getFaviconUrl: () => '',
}));
vi.mock('@object-ui/auth', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
AuthProvider: passthrough,
useAuth: () => authState.value,
}));
vi.mock('../AppContent', () => ({
AppContent: stub('app-content'),
ConsolePermissionsScope: ({ children }: { children?: ReactNode }) => <>{children}</>,
// objectstack#29114: the early permissions request beside the route, nothing here.
ConsolePermissionsRequest: () => null,
}));
vi.mock('../components/ProtectedRoute', () => ({ ProtectedRoute: passthrough }));
// objectstack#29750: the catch-all reads `isAppListConclusive` from here.
vi.mock('../components/RootLandingRedirect', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
RootLandingRedirect: stub('root-landing'),
}));
vi.mock('../components/SetupRoute', () => ({ SetupRoute: stub('setup-route') }));
vi.mock('../components/FormPage', () => ({ FormPage: stub('form-page') }));
vi.mock('../components/InternalFormRoute', () => ({ InternalFormRoute: stub('internal-form-route') }));
vi.mock('../components/MetadataHmrReloader', () => ({ MetadataHmrReloader: () => null }));
vi.mock('../pages/auth/LoginPage', () => ({ LoginPage: stub('login-page') }));
vi.mock('../pages/auth/RegisterPage', () => ({ RegisterPage: stub('register-page') }));
vi.mock('../pages/auth/ForgotPasswordPage', () => ({ ForgotPasswordPage: stub('forgot-page') }));
vi.mock('../pages/auth/ResetPasswordPage', () => ({ ResetPasswordPage: stub('reset-page') }));
vi.mock('../pages/auth/SetPasswordPage', () => ({ SetPasswordPage: stub('set-password-page') }));
vi.mock('../pages/auth/VerifyEmailPage', () => ({ VerifyEmailPage: stub('verify-email-page') }));
vi.mock('../pages/auth/VerifyEmailPromptPage', () => ({ VerifyEmailPromptPage: stub('verify-prompt-page') }));
vi.mock('../pages/auth/OAuthConsentPage', () => ({ OAuthConsentPage: stub('oauth-consent-page') }));
vi.mock('../pages/auth/DeviceAuthPage', () => ({ DeviceAuthPage: stub('device-auth-page') }));
vi.mock('../dev/DevMasterDetail', () => ({ DevMasterDetail: stub('dev-master-detail') }));
vi.mock('../dev/DevLists', () => ({ DevLists: stub('dev-lists') }));
vi.mock('../dev/DevModal', () => ({ DevModal: stub('dev-modal') }));
vi.mock('../dev/DevLookup', () => ({ DevLookup: stub('dev-lookup') }));
vi.mock('../dev/DevRowActions', () => ({ DevRowActions: stub('dev-row-actions') }));
vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
import { App } from '../App';
/** The fake bridge: the contract's members, recording what the Console posts. */
function installBridge() {
const events: string[] = [];
const bridge: ObjectStackNative = {
version: 1,
platform: 'android',
appVersion: '2.0.0',
capabilities: ['getPushToken', 'haptic', 'openExternal', 'navigate', 'resetServer'],
safeArea: { top: 24, bottom: 16, left: 0, right: 0 },
has: () => true,
getPushToken: async () => null,
haptic: async () => null,
openExternal: async () => null,
navigate: async () => null,
resetServer: async () => null,
// What the page shows at the moment `ready` arrives: the shell lifts its
// loading cover on it, so the first route must already be on screen.
ready: () => events.push(`ready:${screen.queryByTestId('login-page') ? 'login-page' : 'nothing'}`),
routeChanged: (route: string) => events.push(`route:${route}`),
};
(window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL] = bridge;
return events;
}
function dispatchNavigate(route: unknown): CustomEvent {
const event = new CustomEvent(NATIVE_NAVIGATE_EVENT, { cancelable: true, detail: { route } });
act(() => {
window.dispatchEvent(event);
});
return event;
}
beforeEach(() => {
resetNativeHostEventsForTests();
resetNativeHostWatchForTests();
shellState.currentAppName = undefined;
shellState.apps = [];
shellState.loading = false;
shellState.appStatus = undefined;
authState.value = { user: { id: 'u1' } };
resetNativePushRegistrationForTests();
sessionStorage.clear();
window.history.pushState({}, '', '/login');
});
afterEach(() => {
cleanup();
delete (window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL];
window.history.pushState({}, '', '/');
});
describe('the Console in the mobile shell', () => {
it('posts ready once, after the first route rendered, and the route the router is on', async () => {
const events = installBridge();
render(<App />);
expect(await screen.findByTestId('login-page')).toBeInTheDocument();
expect(events).toEqual(['ready:login-page', 'route:/login']);
expect(document.documentElement.getAttribute('data-os-native-shell')).toBe('android');
});
it('the shell navigate event moves the router without a reload, and posts that route once', async () => {
const events = installBridge();
render(<App />);
await screen.findByTestId('login-page');
const before = window.history.length;
const event = dispatchNavigate('/register?from=push');
expect(event.defaultPrevented).toBe(true);
expect(await screen.findByTestId('register-page')).toBeInTheDocument();
// The router pushed a history entry: an SPA move, not a document load.
expect(window.location.pathname).toBe('/register');
expect(window.history.length).toBe(before + 1);
expect(events).toEqual(['ready:login-page', 'route:/login', 'route:/register?from=push']);
});
it('a route that is not an in-app path is left to the shell', async () => {
const events = installBridge();
render(<App />);
await screen.findByTestId('login-page');
const event = dispatchNavigate('//evil.example/register');
expect(event.defaultPrevented).toBe(false);
expect(window.location.pathname).toBe('/login');
expect(events).toEqual(['ready:login-page', 'route:/login']);
});
});
describe('control: the Console in a browser behaves as today', () => {
it('posts nothing, marks nothing, and leaves the navigate event alone', async () => {
render(<App />);
expect(await screen.findByTestId('login-page')).toBeInTheDocument();
expect(document.documentElement.hasAttribute('data-os-native-shell')).toBe(false);
const event = dispatchNavigate('/register');
expect(event.defaultPrevented).toBe(false);
expect(window.location.pathname).toBe('/login');
});
});
/**
* objectstack#29750 — the shell's route is a push route, the notification's
* `actionUrl`, and the push contract (`packages/spec/src/api/push-delivery.zod.ts`)
* says it is resolved "as the inbox does, never verbatim": app-relative paths
* are hosted under `/apps/APP/`, an `/apps/…` route passes through. Navigated
* verbatim, `/opportunity/123` matched no route and the catch-all sent the
* person to Home.
*/
describe('a push route from the shell is hosted the way the inbox hosts it', () => {
it('an app-relative route is hosted under the app in the URL', async () => {
installBridge();
window.history.pushState({}, '', '/apps/crm/opportunity');
render(<App />);
await screen.findByTestId('app-content');
const event = dispatchNavigate('/opportunity/123?tab=activity#notes');
expect(event.defaultPrevented).toBe(true);
expect(`${window.location.pathname}${window.location.search}${window.location.hash}`)
.toBe('/apps/crm/opportunity/123?tab=activity#notes');
});
it('away from an app, the catch-all hosts it under the app the person last had open', async () => {
// `currentAppName` is an app NAME; the catch-all turns it into the app's
// route segment with the app list, which the listener does not have.
installBridge();
shellState.currentAppName = 'sales';
shellState.apps = [{ name: 'crm', active: true }, { name: 'sales', active: true, _packageId: 'com.acme.sales' }];
shellState.appStatus = 'ready';
window.history.pushState({}, '', '/home');
render(<App />);
await screen.findByTestId('home-page');
const event = dispatchNavigate('/opportunity/123');
expect(event.defaultPrevented).toBe(true);
expect(window.location.pathname).toBe('/opportunity/123');
expect((await screen.findByTestId('redirect-with-splash')).getAttribute('data-to'))
.toBe('/apps/com.acme.sales/opportunity/123');
});
it('a Console route under /apps passes through unchanged', async () => {
installBridge();
window.history.pushState({}, '', '/apps/crm/opportunity');
render(<App />);
await screen.findByTestId('app-content');
const event = dispatchNavigate('/apps/x/y/1');
expect(event.defaultPrevented).toBe(true);
expect(window.location.pathname).toBe('/apps/x/y/1');
});
it('with no app open, the catch-all hosts the handed-over route under the default app', async () => {
installBridge();
shellState.apps = [{ name: 'crm', active: true }];
shellState.appStatus = 'ready';
render(<App />);
await screen.findByTestId('login-page');
const event = dispatchNavigate('/opportunity/123');
expect(event.defaultPrevented).toBe(true);
expect(window.location.pathname).toBe('/opportunity/123');
expect((await screen.findByTestId('redirect-with-splash')).getAttribute('data-to'))
.toBe('/apps/crm/opportunity/123');
});
});
/**
* objectstack#29750 — the same push route can arrive as the page's FIRST URL:
* the mobile shell starts a re-created WebView at a held route, and falls back
* to `location.assign` of it when no navigate listener answered. The
* catch-all hosts an app-relative path like the inbox; a stale Console link
* keeps the old redirect to `/`.
*/
describe('the catch-all hosts an app-relative first URL under an app', () => {
function catchAllTarget(): Promise<string | null> {
return screen.findByTestId('redirect-with-splash').then((el) => el.getAttribute('data-to'));
}
it('hosts it under the first active app, query and hash kept', async () => {
shellState.apps = [{ name: 'gone', active: false }, { name: 'crm', active: true }];
shellState.appStatus = 'ready';
window.history.pushState({}, '', '/opportunity/123?tab=activity#notes');
render(<App />);
expect(await catchAllTarget()).toBe('/apps/crm/opportunity/123?tab=activity#notes');
});
it('hosts it under the app the person last had open while that app is still active', async () => {
shellState.apps = [{ name: 'crm', active: true }, { name: 'sales', active: true }];
shellState.appStatus = 'ready';
shellState.currentAppName = 'sales';
window.history.pushState({}, '', '/opportunity/123');
render(<App />);
expect(await catchAllTarget()).toBe('/apps/sales/opportunity/123');
});
it('waits for the app list before it answers', async () => {
shellState.loading = true;
window.history.pushState({}, '', '/opportunity/123');
render(<App />);
expect(await screen.findByTestId('loading-fallback')).toBeInTheDocument();
expect(screen.queryByTestId('redirect-with-splash')).toBeNull();
});
it('waits while a session-cached app list is still being re-read', async () => {
// Measured live: the provider stops `loading` on the cached list while
// the app type's own status still reads `loading`.
shellState.apps = [{ name: 'crm', active: true }];
shellState.appStatus = 'loading';
window.history.pushState({}, '', '/opportunity/123');
render(<App />);
expect(await screen.findByTestId('loading-fallback')).toBeInTheDocument();
expect(screen.queryByTestId('redirect-with-splash')).toBeNull();
});
it('an app list that failed to load is no answer: the old redirect to / runs', async () => {
shellState.appStatus = 'error';
window.history.pushState({}, '', '/opportunity/123');
render(<App />);
expect(await catchAllTarget()).toBe('/');
});
it('control: a stale Console link still redirects to /', async () => {
shellState.apps = [{ name: 'crm', active: true }];
shellState.appStatus = 'ready';
for (const path of ['/dev/nope', '/home/nope', '/apps']) {
window.history.pushState({}, '', path);
render(<App />);
expect(await catchAllTarget()).toBe('/');
cleanup();
}
});
it('control: a real top-level Console route is never rewritten', async () => {
shellState.apps = [{ name: 'crm', active: true }];
shellState.appStatus = 'ready';
window.history.pushState({}, '', '/home');
render(<App />);
expect(await screen.findByTestId('home-page')).toBeInTheDocument();
expect(screen.queryByTestId('redirect-with-splash')).toBeNull();
expect(window.location.pathname).toBe('/home');
});
});
/**
* objectstack#29042 — the router wiring of push registration: the route the
* router is on decides when the token is asked for, and the shell's removal of
* this connection is answered with the unregister, under the Console's session.
*/
describe('push registration through the router wiring', () => {
const signedIn = { user: { id: 'u1' }, session: { token: 'session' }, isAuthenticated: true };
function deviceCalls(fetchMock: ReturnType<typeof vi.fn>): string[] {
return fetchMock.mock.calls
.map(([url, init]) => `${(init as RequestInit | undefined)?.method ?? 'GET'} ${String(url)}`)
.filter((call) => call.includes('/api/v1/notifications/devices'));
}
function stubFetch() {
const fetchMock = vi.fn(async (_url: string, init?: RequestInit) => ({
ok: true,
status: 200,
headers: new Headers(),
json: async () => ({
success: true,
data: init?.method === 'POST' ? { deviceId: 'install-7', success: true } : { success: true },
}),
}));
vi.stubGlobal('fetch', fetchMock);
return fetchMock;
}
afterEach(() => {
vi.unstubAllGlobals();
});
it('Home asks the shell for the token once the delay has passed, and registers it', async () => {
const fetchMock = stubFetch();
authState.value = signedIn;
installBridge();
const bridge = window[NATIVE_HOST_GLOBAL as keyof Window] as unknown as ObjectStackNative;
const getPushToken = vi.spyOn(bridge, 'getPushToken').mockResolvedValue({ token: 'tok-1', vendor: 'fcm' });
window.history.pushState({}, '', '/home');
render(<App />);
await screen.findByTestId('home-page');
expect(getPushToken).not.toHaveBeenCalled();
await vi.waitFor(
() => expect(deviceCalls(fetchMock)).toEqual([expect.stringMatching(/^POST .*\/api\/v1\/notifications\/devices$/)]),
{ timeout: NATIVE_PUSH_PROMPT_DELAY_MS + 2000 },
);
expect(getPushToken).toHaveBeenCalledTimes(1);
});
it('the shell removing this connection waits for the unregister of the registered device', async () => {
const fetchMock = stubFetch();
authState.value = signedIn;
sessionStorage.setItem(
NATIVE_PUSH_REGISTRATION_KEY,
JSON.stringify({ userId: 'u1', token: 'tok-1', vendor: 'fcm', deviceId: 'install-7' }),
);
installBridge();
render(<App />);
await screen.findByTestId('login-page');
const held: Promise<unknown>[] = [];
const event = new Event(NATIVE_CONNECTION_REMOVED_EVENT);
Object.assign(event, { waitUntil: (promise: Promise<unknown>) => held.push(promise) });
act(() => {
window.dispatchEvent(event);
});
expect(held).toHaveLength(1);
await held[0];
expect(deviceCalls(fetchMock)).toEqual([expect.stringMatching(/^DELETE .*\/api\/v1\/notifications\/devices\/install-7$/)]);
expect(sessionStorage.getItem(NATIVE_PUSH_REGISTRATION_KEY)).toBeNull();
});
});