|
| 1 | +/** |
| 2 | + * Release URL 组装单元测试 |
| 3 | + * 验证点: |
| 4 | + * 1. APK 更新跳转目标必须是 GitHub releases 页面 |
| 5 | + * 2. 仓库名必须严格大小写匹配远端 (CarGuo/GSYGithubAPP) |
| 6 | + * 3. 协议必须是 https |
| 7 | + */ |
| 8 | + |
| 9 | +describe('release URL 组装', () => { |
| 10 | + const hostWeb = 'https://github.com/'; |
| 11 | + const RELEASE_URL = hostWeb + 'CarGuo/GSYGithubAPP/releases'; |
| 12 | + |
| 13 | + test('hostWeb 必须是 GitHub 主域 https 协议', () => { |
| 14 | + expect(hostWeb).toBe('https://github.com/'); |
| 15 | + }); |
| 16 | + |
| 17 | + test('RELEASE_URL 指向 GitHub releases 页面', () => { |
| 18 | + expect(RELEASE_URL.endsWith('/releases')).toBe(true); |
| 19 | + expect(RELEASE_URL.startsWith('https://github.com/')).toBe(true); |
| 20 | + }); |
| 21 | + |
| 22 | + test('仓库名严格大小写匹配 origin 远端 (CarGuo/GSYGithubAPP)', () => { |
| 23 | + expect(RELEASE_URL).toContain('CarGuo/GSYGithubAPP'); |
| 24 | + expect(RELEASE_URL).not.toMatch(/CarGuo\/GSYGithubApp(?!P)/); |
| 25 | + }); |
| 26 | + |
| 27 | + test('完整 URL 等于 https://github.com/CarGuo/GSYGithubAPP/releases', () => { |
| 28 | + expect(RELEASE_URL).toBe('https://github.com/CarGuo/GSYGithubAPP/releases'); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe('openReleasePage 浏览器跳转兜底逻辑(行为契约)', () => { |
| 33 | + const url = 'https://github.com/CarGuo/GSYGithubAPP/releases'; |
| 34 | + |
| 35 | + const buildOpener = (Linking, Toast) => async () => { |
| 36 | + try { |
| 37 | + const supported = await Linking.canOpenURL(url); |
| 38 | + if (!supported) { |
| 39 | + Toast('openLinkFailed'); |
| 40 | + return false; |
| 41 | + } |
| 42 | + await Linking.openURL(url); |
| 43 | + return true; |
| 44 | + } catch (_) { |
| 45 | + Toast('openLinkFailed'); |
| 46 | + return false; |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + test('canOpenURL 返回 true 时调用 openURL', async () => { |
| 51 | + const Linking = { |
| 52 | + canOpenURL: jest.fn(() => Promise.resolve(true)), |
| 53 | + openURL: jest.fn(() => Promise.resolve()), |
| 54 | + }; |
| 55 | + const Toast = jest.fn(); |
| 56 | + const ok = await buildOpener(Linking, Toast)(); |
| 57 | + expect(ok).toBe(true); |
| 58 | + expect(Linking.canOpenURL).toHaveBeenCalledWith(url); |
| 59 | + expect(Linking.openURL).toHaveBeenCalledWith(url); |
| 60 | + expect(Toast).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + test('canOpenURL 返回 false 时 Toast 提示且不调用 openURL', async () => { |
| 64 | + const Linking = { |
| 65 | + canOpenURL: jest.fn(() => Promise.resolve(false)), |
| 66 | + openURL: jest.fn(() => Promise.resolve()), |
| 67 | + }; |
| 68 | + const Toast = jest.fn(); |
| 69 | + const ok = await buildOpener(Linking, Toast)(); |
| 70 | + expect(ok).toBe(false); |
| 71 | + expect(Linking.openURL).not.toHaveBeenCalled(); |
| 72 | + expect(Toast).toHaveBeenCalledWith('openLinkFailed'); |
| 73 | + }); |
| 74 | + |
| 75 | + test('canOpenURL 抛异常时被 catch 兜底,Toast 提示', async () => { |
| 76 | + const Linking = { |
| 77 | + canOpenURL: jest.fn(() => Promise.reject(new Error('boom'))), |
| 78 | + openURL: jest.fn(() => Promise.resolve()), |
| 79 | + }; |
| 80 | + const Toast = jest.fn(); |
| 81 | + const ok = await buildOpener(Linking, Toast)(); |
| 82 | + expect(ok).toBe(false); |
| 83 | + expect(Linking.openURL).not.toHaveBeenCalled(); |
| 84 | + expect(Toast).toHaveBeenCalledWith('openLinkFailed'); |
| 85 | + }); |
| 86 | + |
| 87 | + test('openURL 抛异常时也被 catch 兜底', async () => { |
| 88 | + const Linking = { |
| 89 | + canOpenURL: jest.fn(() => Promise.resolve(true)), |
| 90 | + openURL: jest.fn(() => Promise.reject(new Error('blocked'))), |
| 91 | + }; |
| 92 | + const Toast = jest.fn(); |
| 93 | + const ok = await buildOpener(Linking, Toast)(); |
| 94 | + expect(ok).toBe(false); |
| 95 | + expect(Toast).toHaveBeenCalledWith('openLinkFailed'); |
| 96 | + }); |
| 97 | +}); |
0 commit comments