Files
Genarrative/apps/mobile-shell/src/mobileShellNavigation.test.ts
T
kdletters 080ebaedfd 接入移动壳受控路由导航
移动 HostBridge 声明 navigation.openNativePage 并限制为同源 H5 route

Expo WebView 将受控导航请求切换为新的 WebView URL

补充移动壳 HostBridge 与导航解析测试和配置守卫

更新宿主壳方案、统一协议和团队共享决策记录
2026-06-17 22:15:31 +08:00

60 lines
1.8 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import {
resolveMobileShellWebViewUrl,
shouldOpenInMobileShellWebView,
} from './mobileShellNavigation';
describe('shouldOpenInMobileShellWebView', () => {
test('只允许主站同源页面留在移动壳 WebView 内', () => {
const allowedOrigin = 'https://app.genarrative.world';
expect(
shouldOpenInMobileShellWebView(
'https://app.genarrative.world/works/detail?work=PZ-1',
allowedOrigin,
),
).toBe(true);
expect(
shouldOpenInMobileShellWebView('/creation/puzzle', allowedOrigin),
).toBe(true);
expect(
shouldOpenInMobileShellWebView('about:blank', allowedOrigin),
).toBe(true);
});
test('外链和非网页协议必须离开带 HostBridge 的 WebView', () => {
const allowedOrigin = 'https://app.genarrative.world';
expect(
shouldOpenInMobileShellWebView('https://example.com/', allowedOrigin),
).toBe(false);
expect(
shouldOpenInMobileShellWebView('mailto:hi@example.com', allowedOrigin),
).toBe(false);
expect(shouldOpenInMobileShellWebView('not a url', allowedOrigin)).toBe(
false,
);
});
test('HostBridge 主动导航只解析同源网页目标', () => {
const allowedOrigin = 'https://app.genarrative.world';
expect(
resolveMobileShellWebViewUrl('/works/detail?work=PZ-1', allowedOrigin),
).toBe('https://app.genarrative.world/works/detail?work=PZ-1');
expect(
resolveMobileShellWebViewUrl(
'https://app.genarrative.world/creation/puzzle#draft',
allowedOrigin,
),
).toBe('https://app.genarrative.world/creation/puzzle#draft');
expect(
resolveMobileShellWebViewUrl('https://example.com/', allowedOrigin),
).toBeNull();
expect(
resolveMobileShellWebViewUrl('about:blank', allowedOrigin),
).toBeNull();
});
});