5bd1fb1a8f
Expo 移动壳默认 H5 地址改为线上主站 本机 Vite 地址仅通过环境变量显式启用 补充默认入口检查测试和架构文档
92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
import { describe, expect, test } from 'vitest';
|
|
|
|
import {
|
|
DEFAULT_MOBILE_SHELL_WEB_URL,
|
|
buildMobileShellUrl,
|
|
resolveMobileShellBaseWebUrl,
|
|
} from './mobileShellUrl';
|
|
|
|
describe('buildMobileShellUrl', () => {
|
|
test('默认 H5 地址指向真实主站', () => {
|
|
expect(DEFAULT_MOBILE_SHELL_WEB_URL).toBe('https://app.genarrative.world/');
|
|
});
|
|
|
|
test('为 H5 附加原生移动壳上下文', () => {
|
|
const url = new URL(
|
|
buildMobileShellUrl('https://app.test/works/detail?work=PZ-1', {
|
|
platform: 'ios',
|
|
hostVersion: '0.1.0',
|
|
capabilities: ['host.getRuntime', 'share.open'],
|
|
}),
|
|
);
|
|
|
|
expect(url.searchParams.get('clientRuntime')).toBe('native_app');
|
|
expect(url.searchParams.get('clientType')).toBe('native_app');
|
|
expect(url.searchParams.get('hostShell')).toBe('expo_mobile');
|
|
expect(url.searchParams.get('hostPlatform')).toBe('ios');
|
|
expect(url.searchParams.get('hostVersion')).toBe('0.1.0');
|
|
expect(url.searchParams.get('bridgeVersion')).toBe('1');
|
|
expect(url.searchParams.get('hostCapabilities')).toBe(
|
|
'host.getRuntime,share.open',
|
|
);
|
|
expect(url.searchParams.get('work')).toBe('PZ-1');
|
|
});
|
|
|
|
test('支持按平台注入不同能力清单', () => {
|
|
const iosUrl = new URL(
|
|
buildMobileShellUrl('https://app.test/', {
|
|
platform: 'ios',
|
|
hostVersion: '0.1.0',
|
|
capabilities: ['host.getRuntime', 'app.setBadgeCount'],
|
|
}),
|
|
);
|
|
const androidUrl = new URL(
|
|
buildMobileShellUrl('https://app.test/', {
|
|
platform: 'android',
|
|
hostVersion: '0.1.0',
|
|
capabilities: ['host.getRuntime'],
|
|
}),
|
|
);
|
|
|
|
expect(iosUrl.searchParams.get('hostCapabilities')).toBe(
|
|
'host.getRuntime,app.setBadgeCount',
|
|
);
|
|
expect(androidUrl.searchParams.get('hostCapabilities')).toBe(
|
|
'host.getRuntime',
|
|
);
|
|
});
|
|
|
|
test('移动壳基准 URL 只接受 http 和 https', () => {
|
|
expect(resolveMobileShellBaseWebUrl('https://app.test/path')).toBe(
|
|
'https://app.test/path',
|
|
);
|
|
expect(resolveMobileShellBaseWebUrl(' http://127.0.0.1:3000/ ')).toBe(
|
|
'http://127.0.0.1:3000/',
|
|
);
|
|
expect(resolveMobileShellBaseWebUrl('javascript:alert(1)')).toBe(
|
|
DEFAULT_MOBILE_SHELL_WEB_URL,
|
|
);
|
|
expect(resolveMobileShellBaseWebUrl('/relative/path')).toBe(
|
|
DEFAULT_MOBILE_SHELL_WEB_URL,
|
|
);
|
|
expect(resolveMobileShellBaseWebUrl('')).toBe(DEFAULT_MOBILE_SHELL_WEB_URL);
|
|
expect(resolveMobileShellBaseWebUrl(null)).toBe(
|
|
DEFAULT_MOBILE_SHELL_WEB_URL,
|
|
);
|
|
});
|
|
|
|
test('非法启动 URL 回退到默认 H5 地址并继续附加宿主上下文', () => {
|
|
const url = new URL(
|
|
buildMobileShellUrl('javascript:alert(1)', {
|
|
platform: 'android',
|
|
hostVersion: '0.1.0',
|
|
capabilities: ['host.getRuntime'],
|
|
}),
|
|
);
|
|
|
|
expect(url.toString().startsWith(DEFAULT_MOBILE_SHELL_WEB_URL)).toBe(true);
|
|
expect(url.searchParams.get('clientRuntime')).toBe('native_app');
|
|
expect(url.searchParams.get('hostShell')).toBe('expo_mobile');
|
|
});
|
|
});
|