Skip to content

Commit 519cfe3

Browse files
committed
wip(mobile): tests and changeset for the open platform set
Claude-Session: https://claude.ai/code/session_01QsR2jSpg4SQPs6XFLsrBhv Co-authored-by: Claude <noreply@anthropic.com>
1 parent 31642f9 commit 519cfe3

3 files changed

Lines changed: 191 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@object-ui/mobile': patch
3+
---
4+
5+
fix(mobile): the native bridge's `platform` is an open set, and a bridge speaking a newer protocol reads as no bridge
6+
7+
Clause-②: yes (widening)
8+
Metadata-protocol: none
9+
10+
The ObjectStack desktop shell hosts the Console through the same `window.ObjectStackNative` bridge as the mobile app, and reports a platform other than `ios` or `android`.
11+
12+
- **`ObjectStackNative.platform` widens** from `'ios' | 'android'` to `'ios' | 'android' | (string & {})`. The two known values still autocomplete. Code that switches exhaustively over the old two values now has to handle another string; branch on a capability (`has()`) or the viewport instead of the platform. The Console's root attribute `data-os-native-shell` carries the value verbatim.
13+
- **Bridge protocol version gate.** The bridge's `version` bumps only on a breaking change, and this package speaks version `1`. `getNativeHost()` and `useNativeHost()` now answer `null` for a bridge whose `version` is greater than `1` or not a number, so a page keeps its browser behaviour (no shell mode, no `ready`, the PWA manifest kept) instead of calling a protocol it cannot read.
14+
- **What `ready()` means, written down.** `ready` is posted when the Console has painted its own UI, which may be a loading fallback such as the auth guard's spinner while the session is read on a cold start. The shell lifts its loading cover on it, so the cover hands over to the Console's spinner rather than staying up through the session round-trip. Nothing about when it is posted changes.
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* The PWA manifest at boot, as the real `main.tsx` decides it.
11+
*
12+
* Inside the native shell the Console drops its install manifest: the app is
13+
* installed natively there. A bridge speaking a newer protocol than this
14+
* Console (`version` 2) reads as no bridge, so the boot keeps the browser path
15+
* and injects its generated manifest. Control: a version 1 bridge drops it,
16+
* and no bridge at all keeps it.
17+
*
18+
* `main.tsx` runs for real; what it calls before the manifest step (runtime
19+
* config, auth preflight, the language seed and catalogue) and what it renders
20+
* after it are stubs, because this file is about that one decision.
21+
*/
22+
23+
import { describe, expect, it, vi, afterEach, beforeEach } from 'vitest';
24+
import { screen, waitFor, act } from '@testing-library/react';
25+
import type { ReactNode } from 'react';
26+
import type { Root } from 'react-dom/client';
27+
import { NATIVE_HOST_GLOBAL } from '../../../../packages/mobile/src/nativeHost';
28+
29+
const { passthrough, roots } = vi.hoisted(() => ({
30+
passthrough: ({ children }: { children?: ReactNode }) => <>{children}</>,
31+
roots: [] as Root[],
32+
}));
33+
34+
vi.mock('react-dom/client', async (importOriginal) => {
35+
const actual = await importOriginal<typeof import('react-dom/client')>();
36+
const createRoot: typeof actual.createRoot = (container, options) => {
37+
const root = actual.createRoot(container, options);
38+
roots.push(root);
39+
return root;
40+
};
41+
return { ...actual, createRoot, default: { ...actual, createRoot } };
42+
});
43+
vi.mock('../index.css', () => ({}));
44+
vi.mock('../App', () => ({ App: () => <div data-testid="app" /> }));
45+
vi.mock('@object-ui/i18n', async (importOriginal) => ({
46+
...(await importOriginal<Record<string, unknown>>()),
47+
I18nProvider: passthrough,
48+
preloadBootstrapLocale: async () => undefined,
49+
}));
50+
vi.mock('@object-ui/mobile', async (importOriginal) => ({
51+
...(await importOriginal<Record<string, unknown>>()),
52+
MobileProvider: passthrough,
53+
}));
54+
vi.mock('@object-ui/components', () => ({ registerPlaceholders: () => undefined }));
55+
vi.mock('@object-ui/app-shell', () => ({
56+
initSentry: async () => undefined,
57+
initRuntimeConfig: async () => undefined,
58+
getProductName: () => 'ObjectOS',
59+
getProductShortName: () => 'ObjectOS',
60+
getFaviconUrl: () => '',
61+
getPwaDescription: () => '',
62+
getPwaThemeColor: () => '#000000',
63+
}));
64+
vi.mock('../loadLanguage', () => ({ loadLanguage: async () => ({}) }));
65+
vi.mock('../loadLocales', () => ({ loadLocales: async () => [] }));
66+
vi.mock('../languageSeed', () => ({ seedTenantLanguage: async () => undefined }));
67+
vi.mock('../lib/auth-preflight', () => ({ preflightAuth: async () => undefined }));
68+
vi.mock('../register-plugins', () => ({}));
69+
vi.mock('../registerDeveloperComponents', () => ({}));
70+
vi.mock('../registerStudioComponents', () => ({}));
71+
vi.mock('../registerAccountComponents', () => ({}));
72+
vi.mock('../registerApprovalsComponents', () => ({}));
73+
vi.mock('../registerSystemComponents', () => ({}));
74+
vi.mock('../registerChatbotComponents', () => ({}));
75+
76+
const BLOB_MANIFEST = 'blob:generated-manifest';
77+
78+
function installBridge(version: number): void {
79+
(window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL] = {
80+
version,
81+
platform: 'ios',
82+
appVersion: '2.0.0',
83+
capabilities: [],
84+
safeArea: { top: 0, bottom: 0, left: 0, right: 0 },
85+
has: () => false,
86+
ready: () => undefined,
87+
routeChanged: () => undefined,
88+
};
89+
}
90+
91+
/** Run `main.tsx` once, as a page load does, and wait for it to render. */
92+
async function boot(): Promise<string[]> {
93+
vi.resetModules();
94+
await import('../main');
95+
await waitFor(() => expect(screen.getByTestId('app')).toBeInTheDocument());
96+
return Array.from(document.querySelectorAll<HTMLLinkElement>('link[rel="manifest"]')).map((link) =>
97+
link.getAttribute('href') ?? '',
98+
);
99+
}
100+
101+
beforeEach(() => {
102+
document.head.innerHTML = '<link rel="manifest" href="./manifest.json" />';
103+
document.body.innerHTML = '<div id="root"></div>';
104+
vi.spyOn(URL, 'createObjectURL').mockReturnValue(BLOB_MANIFEST);
105+
});
106+
107+
afterEach(() => {
108+
act(() => {
109+
for (const root of roots.splice(0)) root.unmount();
110+
});
111+
delete (window as unknown as Record<string, unknown>)[NATIVE_HOST_GLOBAL];
112+
document.head.innerHTML = '';
113+
document.body.innerHTML = '';
114+
vi.restoreAllMocks();
115+
});
116+
117+
describe('the boot manifest and the bridge protocol version', () => {
118+
it('a version 2 bridge is no shell: the generated manifest replaces the static one', async () => {
119+
installBridge(2);
120+
expect(await boot()).toEqual([BLOB_MANIFEST]);
121+
});
122+
123+
it('control: a version 1 bridge is the shell, and the manifest is dropped', async () => {
124+
installBridge(1);
125+
expect(await boot()).toEqual([]);
126+
});
127+
128+
it('control: without a bridge the generated manifest replaces the static one', async () => {
129+
expect(await boot()).toEqual([BLOB_MANIFEST]);
130+
});
131+
});

‎ui/packages/mobile/src/__tests__/nativeHost.test.tsx‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import React from 'react';
2020
import { describe, it, expect, vi, beforeEach, afterEach, type Mock } from 'vitest';
2121
import { render, cleanup, act } from '@testing-library/react';
2222
import {
23+
NATIVE_BRIDGE_VERSION,
2324
NATIVE_HOST_GLOBAL,
2425
NATIVE_NAVIGATE_EVENT,
2526
getNativeHost,
@@ -120,6 +121,40 @@ describe('detection', () => {
120121
});
121122
});
122123

124+
describe('an open platform set and the bridge protocol version', () => {
125+
it('a platform other than ios or android is a bridge like any other', () => {
126+
const bridge = { ...fakeBridge(['haptic']), platform: 'desktop' };
127+
install(bridge);
128+
expect(getNativeHost()).toBe(bridge);
129+
expect(getNativeHost()?.platform).toBe('desktop');
130+
});
131+
132+
it('this package speaks protocol version 1', () => {
133+
expect(NATIVE_BRIDGE_VERSION).toBe(1);
134+
});
135+
136+
it.each([
137+
['a newer version', NATIVE_BRIDGE_VERSION + 1],
138+
['a string version', '1'],
139+
['NaN', Number.NaN],
140+
['no version', undefined],
141+
])('%s reads as no bridge: getNativeHost is null and every call is unavailable', async (_label, version) => {
142+
const bridge = { ...fakeBridge(), version };
143+
install(bridge);
144+
expect(getNativeHost()).toBeNull();
145+
await expect(nativeHaptic('light')).resolves.toEqual({ status: 'unavailable' });
146+
expect(bridge.haptic).not.toHaveBeenCalled();
147+
});
148+
149+
it('control: the version this package speaks, and an older one, are the bridge', () => {
150+
for (const version of [NATIVE_BRIDGE_VERSION, 0]) {
151+
const bridge = { ...fakeBridge(), version };
152+
install(bridge);
153+
expect(getNativeHost()).toBe(bridge);
154+
}
155+
});
156+
});
157+
123158
describe('late injection', () => {
124159
function Probe({ onHost }: { onHost: (host: ObjectStackNative | null) => void }) {
125160
onHost(useNativeHost());
@@ -337,6 +372,17 @@ describe('useNativeHostRouting', () => {
337372
expect(navigate).not.toHaveBeenCalled();
338373
});
339374

375+
it('a bridge speaking a newer protocol gets no ready, no route and no navigate listener', () => {
376+
const bridge = { ...fakeBridge(), version: NATIVE_BRIDGE_VERSION + 1 };
377+
install(bridge);
378+
const add = vi.spyOn(window, 'addEventListener');
379+
const navigate = vi.fn();
380+
render(<Router route="/home" navigate={navigate} />);
381+
expect(bridge.ready).not.toHaveBeenCalled();
382+
expect(bridge.routeChanged).not.toHaveBeenCalled();
383+
expect(add.mock.calls.some(([type]) => type === NATIVE_NAVIGATE_EVENT)).toBe(false);
384+
});
385+
340386
it('control: outside shell mode no navigate listener is installed and nothing is posted', () => {
341387
const add = vi.spyOn(window, 'addEventListener');
342388
const navigate = vi.fn();

0 commit comments

Comments
 (0)