Repository navigation
Expand file tree
/
Copy pathApp.nativeShellOpenSet-29330.test.tsx
More file actions
240 lines (222 loc) · 10.9 KB
/
Copy pathApp.nativeShellOpenSet-29330.test.tsx
File metadata and controls
240 lines (222 loc) · 10.9 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
/**
* 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 beyond the mobile app, as the real `App` wires it.
*
* `ObjectStackNative.platform` is an open set: the desktop shell reports a
* value other than `ios` or `android`, and announces only the capabilities it
* has. The Console must treat that host exactly like the mobile one: shell
* mode on, `ready` posted once, the root attribute carrying the platform
* verbatim, and no call to a method the shell did not announce.
*
* Two more pins sit beside it:
*
* - `ready` means "the Console has painted its own UI". On a cold start to a
* protected route that UI is the auth guard's loading fallback, and `ready`
* is posted while it is still on screen, so the shell's loading cover hands
* over to the Console's spinner instead of waiting out the session read.
* - A bridge speaking a newer protocol than this Console (`version` 2) reads
* as no bridge: no shell mode and no `ready`.
*
* `App` is the real module under the real `BrowserRouter`, and so are
* `ProtectedRoute` and `AuthGuard`; the pages and the shell chrome are stubs,
* as in `App.nativeShellMode-29039.test.tsx`.
*/
import { describe, expect, it, vi, afterEach, beforeEach, type Mock } from 'vitest';
import { render, screen, cleanup } from '@testing-library/react';
import type { ReactNode } from 'react';
import {
NATIVE_HOST_GLOBAL,
nativeGetPushToken,
nativeHaptic,
nativeNavigate,
nativeOpenExternal,
nativeResetServer,
resetNativeHostEventsForTests,
type ObjectStackNative,
} from '../../../../packages/mobile/src/nativeHost';
import { resetNativeHostWatchForTests } from '../../../../packages/mobile/src/useNativeHost';
const { passthrough, stub, authState } = vi.hoisted(() => {
const passthrough = ({ children }: { children?: ReactNode }) => <>{children}</>;
const stub = (testid: string) => () => <div data-testid={testid} />;
/** What the session read has answered so far; `isLoading` is a cold start. */
const authState = { isLoading: false };
return { passthrough, stub, authState };
});
vi.mock('@object-ui/app-shell', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
ConsoleShell: passthrough,
ConsoleToaster: () => null,
ConnectedShell: passthrough,
RequireOrganization: passthrough,
LoadingFallback: stub('auth-loading-fallback'),
LoadingScreen: stub('loading-screen'),
RedirectWithSplash: stub('redirect-with-splash'),
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: () => '',
}));
// The real `AuthGuard` reads the real auth context. The provider is replaced
// by one that publishes `authState`, so a test can hold the session read open.
vi.mock('@object-ui/auth', async (importOriginal) => {
const actual = await importOriginal<typeof import('@object-ui/auth')>();
const { AuthCtx } = await import('../../../../packages/auth/src/AuthContext');
function AuthStateProvider({ children }: { children?: ReactNode }) {
// Outside any provider `useAuth` answers its signed-out defaults.
const signedOut = actual.useAuth();
return <AuthCtx.Provider value={{ ...signedOut, isLoading: authState.isLoading }}>{children}</AuthCtx.Provider>;
}
return { ...actual, AuthProvider: AuthStateProvider };
});
vi.mock('../AppContent', () => ({
AppContent: stub('app-content'),
ConsolePermissionsScope: ({ children }: { children?: ReactNode }) => <>{children}</>,
ConsolePermissionsRequest: () => null,
}));
vi.mock('../components/RootLandingRedirect', () => ({ 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';
type FakeBridge = ObjectStackNative & {
getPushToken: Mock<ObjectStackNative['getPushToken']>;
haptic: Mock<ObjectStackNative['haptic']>;
openExternal: Mock<ObjectStackNative['openExternal']>;
navigate: Mock<ObjectStackNative['navigate']>;
resetServer: Mock<ObjectStackNative['resetServer']>;
};
/**
* A fake bridge recording what the Console posts. `ready` notes which of the
* two first screens is up when it arrives: the shell lifts its cover on it.
*/
function installBridge({
platform,
version = 1,
capabilities,
}: {
platform: string;
version?: number;
capabilities: readonly string[];
}): { bridge: FakeBridge; events: string[] } {
const events: string[] = [];
const onScreen = () =>
screen.queryByTestId('auth-loading-fallback')
? 'auth-loading-fallback'
: screen.queryByTestId('login-page')
? 'login-page'
: 'nothing';
const bridge: FakeBridge = {
version,
platform,
appVersion: '1.0.0',
capabilities,
safeArea: { top: 0, bottom: 0, left: 0, right: 0 },
has: (capability: string) => capabilities.includes(capability),
getPushToken: vi.fn(async () => null),
haptic: vi.fn(async () => null),
openExternal: vi.fn(async () => null),
navigate: vi.fn(async () => null),
resetServer: vi.fn(async () => null),
ready: () => events.push(`ready:${onScreen()}`),
routeChanged: (route: string) => events.push(`route:${route}`),
};
(window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL] = bridge;
return { bridge, events };
}
beforeEach(() => {
resetNativeHostEventsForTests();
resetNativeHostWatchForTests();
authState.isLoading = false;
window.history.pushState({}, '', '/login');
});
afterEach(() => {
cleanup();
delete (window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL];
window.history.pushState({}, '', '/');
});
describe('a shell whose platform is neither ios nor android', () => {
it('desktop with a capability subset: shell mode on, ready once, the platform verbatim, nothing unannounced called', async () => {
const { bridge, events } = installBridge({ platform: 'desktop', capabilities: ['haptic', 'openExternal'] });
const { rerender } = render(<App />);
expect(await screen.findByTestId('login-page')).toBeInTheDocument();
expect(document.documentElement.getAttribute('data-os-native-shell')).toBe('desktop');
expect(events).toEqual(['ready:login-page', 'route:/login']);
// Every typed call, from inside the running Console: the announced ones
// reach the shell, the rest resolve `unavailable` without a call.
await expect(nativeHaptic('success')).resolves.toEqual({ status: 'ok', value: null });
await expect(nativeOpenExternal('https://example.com')).resolves.toEqual({ status: 'ok', value: null });
await expect(nativeGetPushToken()).resolves.toEqual({ status: 'unavailable' });
await expect(nativeNavigate('/home')).resolves.toEqual({ status: 'unavailable' });
await expect(nativeResetServer()).resolves.toEqual({ status: 'unavailable' });
rerender(<App />);
expect(events.filter((e) => e.startsWith('ready:'))).toHaveLength(1);
expect(bridge.haptic).toHaveBeenCalledTimes(1);
expect(bridge.openExternal).toHaveBeenCalledTimes(1);
expect(bridge.getPushToken).not.toHaveBeenCalled();
expect(bridge.navigate).not.toHaveBeenCalled();
expect(bridge.resetServer).not.toHaveBeenCalled();
});
});
describe('what ready means: the Console has painted its own UI', () => {
it('a cold start to a protected route posts ready while the auth guard still shows its loading fallback', async () => {
authState.isLoading = true;
window.history.pushState({}, '', '/');
const { events } = installBridge({ platform: 'ios', capabilities: [] });
render(<App />);
expect(await screen.findByTestId('auth-loading-fallback')).toBeInTheDocument();
// The session is still being read: the guard has not let the route through.
expect(screen.queryByTestId('root-landing')).not.toBeInTheDocument();
expect(events).toEqual(['ready:auth-loading-fallback', 'route:/']);
});
});
describe('a bridge speaking a newer protocol is no bridge', () => {
it('version 2: no shell mode, no ready, no route posts', async () => {
const { events } = installBridge({ platform: 'ios', version: 2, capabilities: ['haptic'] });
render(<App />);
expect(await screen.findByTestId('login-page')).toBeInTheDocument();
expect(document.documentElement.hasAttribute('data-os-native-shell')).toBe(false);
expect(events).toEqual([]);
await expect(nativeHaptic('success')).resolves.toEqual({ status: 'unavailable' });
});
it('control: version 1, the same bridge otherwise, is the shell', async () => {
const { events } = installBridge({ platform: 'ios', version: 1, capabilities: ['haptic'] });
render(<App />);
expect(await screen.findByTestId('login-page')).toBeInTheDocument();
expect(document.documentElement.getAttribute('data-os-native-shell')).toBe('ios');
expect(events).toEqual(['ready:login-page', 'route:/login']);
});
});