Files
Genarrative/apps/mobile-shell/src/host-bridge/runtime.test.ts
T
kdletters 29bce53669 补齐移动壳运行态单测
移动壳 runtime helper 增加独立单测覆盖

原生壳结构门禁登记移动运行态测试

宿主壳文档和共享决策同步运行态测试边界
2026-06-20 08:16:48 +08:00

95 lines
2.5 KiB
TypeScript

import { Platform } from 'react-native';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import {
HOST_BRIDGE_PROTOCOL,
HOST_BRIDGE_VERSION,
type HostBridgeRequest,
} from '../../../../packages/shared/src/contracts/hostBridge';
import { MOBILE_SHELL_HOST_VERSION } from '../shell/runtime';
import {
getMobileHostBridgeRuntime,
getMobileHostBridgeRuntimeResponse,
getMobileRuntimePlatform,
} from './runtime';
vi.mock('react-native', () => ({
Platform: {
OS: 'ios',
},
}));
function request(): HostBridgeRequest {
return {
bridge: HOST_BRIDGE_PROTOCOL,
version: HOST_BRIDGE_VERSION,
id: 'runtime-request',
method: 'host.getRuntime',
};
}
function setPlatformOS(os: 'ios' | 'android') {
(Platform as { OS: 'ios' | 'android' }).OS = os;
}
beforeEach(() => {
setPlatformOS('ios');
});
describe('mobile HostBridge runtime helper', () => {
test('reports iOS runtime with host version, bridge version and iOS capabilities', () => {
expect(getMobileRuntimePlatform()).toBe('ios');
const runtime = getMobileHostBridgeRuntime();
expect(runtime).toEqual(
expect.objectContaining({
shell: 'expo_mobile',
platform: 'ios',
hostVersion: MOBILE_SHELL_HOST_VERSION,
bridgeVersion: HOST_BRIDGE_VERSION,
}),
);
expect(runtime.capabilities).toEqual(
expect.arrayContaining([
'host.getRuntime',
'appearance.getColorScheme',
'network.status',
'app.setBadgeCount',
'file.captureImage',
]),
);
});
test('reports Android runtime without iOS-only app badge capability', () => {
setPlatformOS('android');
const runtime = getMobileHostBridgeRuntime();
expect(getMobileRuntimePlatform()).toBe('android');
expect(runtime).toEqual(
expect.objectContaining({
shell: 'expo_mobile',
platform: 'android',
hostVersion: MOBILE_SHELL_HOST_VERSION,
bridgeVersion: HOST_BRIDGE_VERSION,
}),
);
expect(runtime.capabilities).toContain('host.getRuntime');
expect(runtime.capabilities).toContain('file.captureImage');
expect(runtime.capabilities).not.toContain('app.setBadgeCount');
});
test('wraps runtime metadata in the HostBridge response shape', () => {
const response = getMobileHostBridgeRuntimeResponse(request());
expect(response).toEqual({
bridge: HOST_BRIDGE_PROTOCOL,
version: HOST_BRIDGE_VERSION,
id: 'runtime-request',
ok: true,
result: getMobileHostBridgeRuntime(),
});
});
});