diff --git a/apps/mobile-shell/scripts/check-config.mjs b/apps/mobile-shell/scripts/check-config.mjs index 56faf7445..06b6d1432 100644 --- a/apps/mobile-shell/scripts/check-config.mjs +++ b/apps/mobile-shell/scripts/check-config.mjs @@ -1318,6 +1318,7 @@ for (const snippet of [ 'normalizeHostBridgeShareOpenPayload(currentShareTarget)', 'setMobileHostBridgeShareTarget', 'request: HostBridgeRequest', + "throw invalidRequest('target is required')", 'ok(request, true)', 'resetMobileHostBridgeShareTargetForTest', ]) { diff --git a/apps/mobile-shell/src/host-bridge/bridge.test.ts b/apps/mobile-shell/src/host-bridge/bridge.test.ts index e16da2256..090dade71 100644 --- a/apps/mobile-shell/src/host-bridge/bridge.test.ts +++ b/apps/mobile-shell/src/host-bridge/bridge.test.ts @@ -922,6 +922,33 @@ describe('handleMobileHostBridgeMessage', () => { }); }); + test('share.setTarget 拒绝缺少目标且不清空已有目标', async () => { + expectOk( + await send( + request('share.setTarget', { + target: { + type: 'genarrative:share-target', + payload: { + title: '暖灯猫街', + work: 'PZ-00000001', + }, + }, + }), + ), + ); + + const invalid = await send(request('share.setTarget', {})); + + expect(expectFailed(invalid).error.message).toBe('target is required'); + + expectOk(await send(request('share.open'))); + expect(Share.share).toHaveBeenCalledWith({ + title: '暖灯猫街', + message: 'https://app.genarrative.world/works/detail?work=PZ-00000001', + url: 'https://app.genarrative.world/works/detail?work=PZ-00000001', + }); + }); + test('share.open 只把同源路径归一为公开主站分享 URL', async () => { const response = await send( request('share.open', { diff --git a/apps/mobile-shell/src/host-bridge/share.ts b/apps/mobile-shell/src/host-bridge/share.ts index ea7a9806b..40a917207 100644 --- a/apps/mobile-shell/src/host-bridge/share.ts +++ b/apps/mobile-shell/src/host-bridge/share.ts @@ -10,10 +10,15 @@ let currentShareTarget: unknown = null; export function setMobileHostBridgeShareTarget(request: HostBridgeRequest) { const payload = request.payload; - currentShareTarget = + const target = payload && typeof payload === 'object' ? (payload as { target?: unknown }).target - : null; + : undefined; + if (target === undefined) { + throw invalidRequest('target is required'); + } + + currentShareTarget = target; return ok(request, true); }