|
| 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 | +}); |
0 commit comments