收紧移动壳分享目标缓存

移动壳分享目标缓存改为保存规范化后的分享 payload

移动壳分享测试覆盖缓存裁剪外层目标和私有字段

移动壳配置检查锁定分享目标缓存结构
This commit is contained in:
2026-06-20 15:00:03 +08:00
parent 2332461570
commit 3788d0e02c
3 changed files with 34 additions and 2 deletions
@@ -1643,6 +1643,9 @@ for (const snippet of [
'type HostBridgeRequest',
'const explicitPayload = normalizeHostBridgeShareOpenPayload(request.payload);',
'normalizeHostBridgeShareOpenPayload(currentShareTarget)',
'type ShareOpenPayload',
'let currentShareTarget: ShareOpenPayload | null = null;',
'currentShareTarget = normalizedTarget.payload;',
'setMobileHostBridgeShareTarget',
'request: HostBridgeRequest',
"throw invalidRequest('target is required')",
@@ -1671,6 +1674,7 @@ if (
for (const snippet of [
'resetMobileHostBridgeShareTargetForTest()',
'uses cached work target when share.open has no explicit payload',
'stores only the normalized cached share payload',
'keeps the previous cached target when a new target is missing or invalid',
'maps native share sheet failures to stable host errors',
'does not fall back to cached target when explicit payload is unsafe',
@@ -95,6 +95,33 @@ describe('mobile share helpers', () => {
});
});
test('stores only the normalized cached share payload', async () => {
setMobileHostBridgeShareTarget(
request('share.setTarget', {
target: {
type: 'genarrative:share-target',
payload: {
title: ' 暖灯猫街 ',
message: ' 来玩这个作品 ',
work: 'PZ-00000001',
privateDraftId: 'draft-1',
},
},
}),
);
await expect(openShare(request('share.open'))).resolves.toMatchObject({
ok: true,
result: true,
});
expect(Share.share).toHaveBeenCalledWith({
title: '暖灯猫街',
message:
'来玩这个作品\nhttps://app.genarrative.world/works/detail?work=PZ-00000001',
url: 'https://app.genarrative.world/works/detail?work=PZ-00000001',
});
});
test('keeps the previous cached target when a new target is missing or invalid', async () => {
setMobileHostBridgeShareTarget(
request('share.setTarget', {
+3 -2
View File
@@ -4,10 +4,11 @@ import {
type HostBridgeError,
type HostBridgeRequest,
normalizeHostBridgeShareOpenPayload,
type ShareOpenPayload,
} from '../../../../packages/shared/src/contracts/hostBridge';
import { invalidRequest, ok } from './protocol';
let currentShareTarget: unknown = null;
let currentShareTarget: ShareOpenPayload | null = null;
export function setMobileHostBridgeShareTarget(request: HostBridgeRequest) {
const payload = request.payload;
@@ -27,7 +28,7 @@ export function setMobileHostBridgeShareTarget(request: HostBridgeRequest) {
);
}
currentShareTarget = target;
currentShareTarget = normalizedTarget.payload;
return ok(request, true);
}