回读宿主运行态能力
HostBridge 启动时通过真实 host.getRuntime 回读并缓存宿主能力 主 App 订阅宿主能力变化并在回读后刷新能力入口 补充宿主 runtime 回读测试和 App 能力刷新测试 更新 Expo/Tauri 壳方案、HostBridge 协议文档和共享决策记录
This commit is contained in:
@@ -16,14 +16,17 @@ import {
|
||||
openHostShareGrid,
|
||||
openWechatMiniProgramShareGridPage,
|
||||
postWechatMiniProgramMessage,
|
||||
refreshNativeAppHostRuntime,
|
||||
requestHostHapticsImpact,
|
||||
requestHostLogin,
|
||||
requestHostPayment,
|
||||
requestWechatMiniProgramPayment,
|
||||
requestWechatMiniProgramPhoneLogin,
|
||||
resetHostRuntimeCacheForTest,
|
||||
resolveHostRuntime,
|
||||
setHostAppTitle,
|
||||
setHostShareTarget,
|
||||
subscribeHostRuntimeChange,
|
||||
writeHostClipboardText,
|
||||
} from './hostBridge';
|
||||
import { resetNativeAppHostBridgeForTest } from './nativeAppHostBridge';
|
||||
@@ -57,6 +60,7 @@ afterEach(() => {
|
||||
delete window.ReactNativeWebView;
|
||||
delete window.__TAURI__;
|
||||
resetNativeAppHostBridgeForTest();
|
||||
resetHostRuntimeCacheForTest();
|
||||
});
|
||||
|
||||
describe('hostBridge', () => {
|
||||
@@ -133,6 +137,103 @@ describe('hostBridge', () => {
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
test('从真实宿主 runtime 回读能力并通知订阅者', async () => {
|
||||
const listener = vi.fn();
|
||||
const unsubscribe = subscribeHostRuntimeChange(listener);
|
||||
const invoke = vi.fn(
|
||||
async (_command: string, args?: Record<string, unknown>) => {
|
||||
const request = (args as { request: { id: string; method: string } })
|
||||
.request;
|
||||
return {
|
||||
bridge: 'GenarrativeHostBridge',
|
||||
version: 1,
|
||||
id: request.id,
|
||||
ok: true,
|
||||
result: {
|
||||
shell: 'tauri_desktop',
|
||||
platform: 'linux',
|
||||
hostVersion: '0.1.0',
|
||||
bridgeVersion: 1,
|
||||
capabilities: [
|
||||
'host.getRuntime',
|
||||
'share.open',
|
||||
'clipboard.writeText',
|
||||
'unknown.capability',
|
||||
],
|
||||
},
|
||||
};
|
||||
},
|
||||
);
|
||||
window.history.replaceState(
|
||||
null,
|
||||
'',
|
||||
'/?clientRuntime=native_app&hostShell=tauri_desktop',
|
||||
);
|
||||
window.__TAURI__ = {
|
||||
core: {
|
||||
invoke: asTauriInvoke(invoke),
|
||||
},
|
||||
};
|
||||
|
||||
expect(canUseNativeHostCapability('share.open')).toBe(false);
|
||||
await expect(refreshNativeAppHostRuntime()).resolves.toMatchObject({
|
||||
shell: 'tauri_desktop',
|
||||
capabilities: ['host.getRuntime', 'share.open', 'clipboard.writeText'],
|
||||
});
|
||||
|
||||
expect(canUseNativeHostCapability('share.open')).toBe(true);
|
||||
expect(canUseNativeHostCapability('clipboard.writeText')).toBe(true);
|
||||
expect(canUseNativeHostCapability('file.exportText')).toBe(false);
|
||||
expect(getHostRuntime().hostCapabilities).toEqual([
|
||||
'host.getRuntime',
|
||||
'share.open',
|
||||
'clipboard.writeText',
|
||||
]);
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
expect(invoke).toHaveBeenCalledTimes(1);
|
||||
|
||||
unsubscribe();
|
||||
});
|
||||
|
||||
test('普通浏览器不混入缓存的原生宿主能力', async () => {
|
||||
const invoke = vi.fn(
|
||||
async (_command: string, args?: Record<string, unknown>) => {
|
||||
const request = (args as { request: { id: string } }).request;
|
||||
return {
|
||||
bridge: 'GenarrativeHostBridge',
|
||||
version: 1,
|
||||
id: request.id,
|
||||
ok: true,
|
||||
result: {
|
||||
shell: 'tauri_desktop',
|
||||
platform: 'linux',
|
||||
hostVersion: '0.1.0',
|
||||
bridgeVersion: 1,
|
||||
capabilities: ['share.open'],
|
||||
},
|
||||
};
|
||||
},
|
||||
);
|
||||
window.history.replaceState(
|
||||
null,
|
||||
'',
|
||||
'/?clientRuntime=native_app&hostShell=tauri_desktop',
|
||||
);
|
||||
window.__TAURI__ = {
|
||||
core: {
|
||||
invoke: asTauriInvoke(invoke),
|
||||
},
|
||||
};
|
||||
|
||||
await refreshNativeAppHostRuntime();
|
||||
delete window.__TAURI__;
|
||||
window.history.replaceState(null, '', '/?clientRuntime=browser');
|
||||
|
||||
expect(getHostRuntime().kind).toBe('browser');
|
||||
expect(getHostRuntime().hostCapabilities).toEqual([]);
|
||||
expect(canUseNativeHostCapability('share.open')).toBe(false);
|
||||
});
|
||||
|
||||
test('通过微信小程序原生页请求登录', async () => {
|
||||
const navigateTo = vi.fn((options) => {
|
||||
options.success?.();
|
||||
|
||||
Reference in New Issue
Block a user