补齐移动壳分享单测
移动壳分享 helper 增加独立单测覆盖 原生壳结构门禁登记移动分享测试 宿主壳文档和共享决策同步分享测试边界
This commit is contained in:
@@ -59,6 +59,8 @@ const hostBridgeRuntimePath = new URL('../src/host-bridge/runtime.ts', import.me
|
||||
const hostBridgeRuntimeSource = fs.readFileSync(hostBridgeRuntimePath, 'utf8');
|
||||
const sharePath = new URL('../src/host-bridge/share.ts', import.meta.url);
|
||||
const shareSource = fs.readFileSync(sharePath, 'utf8');
|
||||
const shareTestPath = new URL('../src/host-bridge/share.test.ts', import.meta.url);
|
||||
const shareTestSource = fs.readFileSync(shareTestPath, 'utf8');
|
||||
const bridgeDirPath = new URL('../src/host-bridge/', import.meta.url);
|
||||
const bridgeSourceFiles = fs
|
||||
.readdirSync(bridgeDirPath, { withFileTypes: true })
|
||||
@@ -1467,6 +1469,21 @@ if (
|
||||
throw new Error('mobile shell share HostBridge methods must delegate to share module');
|
||||
}
|
||||
|
||||
for (const snippet of [
|
||||
'resetMobileHostBridgeShareTargetForTest()',
|
||||
'uses cached work target when share.open has no explicit payload',
|
||||
'keeps the previous cached target when a new target is missing or invalid',
|
||||
'does not fall back to cached target when explicit payload is unsafe',
|
||||
'rejects empty share requests before opening native share sheet',
|
||||
'https://app.genarrative.world/works/detail?work=PZ-00000001',
|
||||
'javascript:alert(1)',
|
||||
'expect(Share.share).not.toHaveBeenCalled()',
|
||||
]) {
|
||||
if (!shareTestSource.includes(snippet)) {
|
||||
throw new Error(`mobile shell share tests missing ${snippet}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (shareSource.includes("const WEB_APP_ORIGIN = 'https://app.genarrative.world'")) {
|
||||
throw new Error('mobile shell share URL policy must reuse the shared web origin');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
import { Share } 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 {
|
||||
openShare,
|
||||
resetMobileHostBridgeShareTargetForTest,
|
||||
setMobileHostBridgeShareTarget,
|
||||
} from './share';
|
||||
|
||||
vi.mock('react-native', () => ({
|
||||
Share: {
|
||||
share: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
function request(
|
||||
method: 'share.open' | 'share.setTarget',
|
||||
payload?: unknown,
|
||||
): HostBridgeRequest {
|
||||
return {
|
||||
bridge: HOST_BRIDGE_PROTOCOL,
|
||||
version: HOST_BRIDGE_VERSION,
|
||||
id: `${method}-request`,
|
||||
method,
|
||||
payload,
|
||||
};
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
vi.mocked(Share.share).mockReset();
|
||||
resetMobileHostBridgeShareTargetForTest();
|
||||
});
|
||||
|
||||
describe('mobile share helpers', () => {
|
||||
test('opens the native share sheet with normalized explicit payload', async () => {
|
||||
const response = await openShare(
|
||||
request('share.open', {
|
||||
title: '测试作品',
|
||||
message: '来玩这个作品',
|
||||
url: 'https://app.genarrative.world/works/detail?work=PZ-1',
|
||||
}),
|
||||
);
|
||||
|
||||
expect(response).toEqual({
|
||||
bridge: HOST_BRIDGE_PROTOCOL,
|
||||
version: HOST_BRIDGE_VERSION,
|
||||
id: 'share.open-request',
|
||||
ok: true,
|
||||
result: true,
|
||||
});
|
||||
expect(Share.share).toHaveBeenCalledWith({
|
||||
title: '测试作品',
|
||||
message:
|
||||
'来玩这个作品\nhttps://app.genarrative.world/works/detail?work=PZ-1',
|
||||
url: 'https://app.genarrative.world/works/detail?work=PZ-1',
|
||||
});
|
||||
});
|
||||
|
||||
test('uses cached work target when share.open has no explicit payload', async () => {
|
||||
const targetResponse = setMobileHostBridgeShareTarget(
|
||||
request('share.setTarget', {
|
||||
target: {
|
||||
type: 'genarrative:share-target',
|
||||
payload: {
|
||||
title: '暖灯猫街',
|
||||
message: '来玩这个作品',
|
||||
work: 'PZ-00000001',
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
expect(targetResponse).toEqual({
|
||||
bridge: HOST_BRIDGE_PROTOCOL,
|
||||
version: HOST_BRIDGE_VERSION,
|
||||
id: 'share.setTarget-request',
|
||||
ok: true,
|
||||
result: true,
|
||||
});
|
||||
|
||||
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', {
|
||||
target: {
|
||||
type: 'genarrative:share-target',
|
||||
payload: {
|
||||
title: '暖灯猫街',
|
||||
work: 'PZ-00000001',
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
expect(() => setMobileHostBridgeShareTarget(request('share.setTarget', {})))
|
||||
.toThrowError('target is required');
|
||||
expect(() =>
|
||||
setMobileHostBridgeShareTarget(
|
||||
request('share.setTarget', {
|
||||
target: {},
|
||||
}),
|
||||
),
|
||||
).toThrowError('share target is required');
|
||||
expect(() =>
|
||||
setMobileHostBridgeShareTarget(
|
||||
request('share.setTarget', {
|
||||
target: {
|
||||
title: '危险作品',
|
||||
url: 'https://example.com/works/detail?work=PZ-1',
|
||||
},
|
||||
}),
|
||||
),
|
||||
).toThrowError('share target is invalid');
|
||||
|
||||
await expect(openShare(request('share.open'))).resolves.toMatchObject({
|
||||
ok: true,
|
||||
result: true,
|
||||
});
|
||||
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('normalizes same-origin paths into public share URLs', async () => {
|
||||
await expect(
|
||||
openShare(
|
||||
request('share.open', {
|
||||
title: '测试作品',
|
||||
path: '/works/detail?work=PZ-2#play',
|
||||
}),
|
||||
),
|
||||
).resolves.toMatchObject({
|
||||
ok: true,
|
||||
result: true,
|
||||
});
|
||||
|
||||
expect(Share.share).toHaveBeenCalledWith({
|
||||
title: '测试作品',
|
||||
message: 'https://app.genarrative.world/works/detail?work=PZ-2#play',
|
||||
url: 'https://app.genarrative.world/works/detail?work=PZ-2#play',
|
||||
});
|
||||
});
|
||||
|
||||
test.each([
|
||||
'https://example.com/works/detail?work=PZ-1',
|
||||
'//example.com/works/detail?work=PZ-1',
|
||||
'//app.genarrative.world/works/detail?work=PZ-1',
|
||||
'javascript:alert(1)',
|
||||
])('rejects unsafe explicit share URLs: %s', async (url) => {
|
||||
await expect(
|
||||
openShare(
|
||||
request('share.open', {
|
||||
title: '测试作品',
|
||||
url,
|
||||
}),
|
||||
),
|
||||
).rejects.toMatchObject({
|
||||
code: 'invalid_request',
|
||||
message: 'share target is invalid',
|
||||
});
|
||||
expect(Share.share).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('does not fall back to cached target when explicit payload is unsafe', async () => {
|
||||
setMobileHostBridgeShareTarget(
|
||||
request('share.setTarget', {
|
||||
target: {
|
||||
type: 'genarrative:share-target',
|
||||
payload: {
|
||||
title: '暖灯猫街',
|
||||
work: 'PZ-00000001',
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
await expect(
|
||||
openShare(
|
||||
request('share.open', {
|
||||
title: '测试作品',
|
||||
url: 'https://example.com/works/detail?work=PZ-1',
|
||||
}),
|
||||
),
|
||||
).rejects.toMatchObject({
|
||||
code: 'invalid_request',
|
||||
message: 'share target is invalid',
|
||||
});
|
||||
expect(Share.share).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('rejects empty share requests before opening native share sheet', async () => {
|
||||
await expect(openShare(request('share.open', {}))).rejects.toMatchObject({
|
||||
code: 'invalid_request',
|
||||
message: 'share target is required',
|
||||
});
|
||||
expect(Share.share).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -2920,3 +2920,10 @@
|
||||
- 决策:`apps/mobile-shell/src/shell/ShellApp.test.tsx` 必须覆盖 AppState 到 `app.lifecycle`、Expo Network listener 到 `network.statusChanged`、WebView native / H5 history 合成到 `navigation.canGoBack` 的真实注入脚本;页面 load 后网络状态重放失败必须记录日志,不允许静默 `.catch(() => undefined)`。移动壳配置检查反查这些测试片段和失败日志 helper。
|
||||
- 影响范围:`apps/mobile-shell/src/shell/ShellApp.tsx`、`apps/mobile-shell/src/shell/ShellApp.test.tsx`、`apps/mobile-shell/scripts/check-config.mjs`、宿主壳能力统一协议文档。
|
||||
- 验证方式:`npm run mobile-shell:test -- src/shell/ShellApp.test.tsx`、`npm run mobile-shell:typecheck`、`npm run check:native-shells`、`npm run check:encoding`、`git diff --check`。
|
||||
|
||||
## 2026-06-20 移动分享单测边界
|
||||
|
||||
- 背景:Expo 移动壳 `share.open` / `share.setTarget` 已经由 `share.ts` 承接共享 HostBridge 分享 URL 归一和缓存目标,但关键边界主要压在巨型 `bridge.test.ts` 中,后续拆分桥接 helper 时容易遗漏非法显式 payload 不回退缓存、空分享拒绝和缓存目标保留语义。
|
||||
- 决策:新增 `apps/mobile-shell/src/host-bridge/share.test.ts`,直接覆盖显式分享 payload、缓存作品目标、同源路径归一、非法 URL / 协议相对 URL 拒绝、非法显式 payload 不回退缓存、空分享拒绝和无效 `share.setTarget` 不清空已有目标;移动壳单端配置检查和根级原生壳门禁登记该测试文件并反查关键断言片段。
|
||||
- 影响范围:`apps/mobile-shell/src/host-bridge/share.test.ts`、`apps/mobile-shell/scripts/check-config.mjs`、`scripts/check-native-shells.mjs`、宿主壳方案文档、宿主壳能力统一协议文档。
|
||||
- 验证方式:`npm run mobile-shell:test -- src/host-bridge/share.test.ts`、`npm run mobile-shell:typecheck`、`npm run check:native-shells`、`npm run check:encoding`、`git diff --check`。
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -269,6 +269,7 @@ const expectedMobileHostBridgeFiles = [
|
||||
'protocol.ts',
|
||||
'runtime.ts',
|
||||
'scanner.ts',
|
||||
'share.test.ts',
|
||||
'share.ts',
|
||||
];
|
||||
const expectedMobileSrcRootEntries = [
|
||||
|
||||
Reference in New Issue
Block a user