import { describe, expect, test } from 'vitest'; import { normalizeMobileShellLoadFailure } from './loadFailure'; const allowedOrigin = 'https://www.genarrative.world'; describe('loadFailure', () => { test('归一化同源 HTTP 加载失败', () => { expect( normalizeMobileShellLoadFailure( { type: 'http', url: 'https://www.genarrative.world/creation/puzzle?sessionId=private#recover', statusCode: 503, description: 'Service Unavailable', }, allowedOrigin, ), ).toEqual({ type: 'http', url: 'https://www.genarrative.world/creation/puzzle', title: '加载失败 503', detail: '服务器暂时没有返回可用页面', retryLabel: '重试', }); }); test('归一化同源原生加载失败并隐藏系统错误描述', () => { expect( normalizeMobileShellLoadFailure( { type: 'native', url: '/works/detail?work=WF-1&token=private#runtime', code: -1009, description: ' The Internet connection appears to be offline. ', }, allowedOrigin, ), ).toEqual({ type: 'native', url: 'https://www.genarrative.world/works/detail', title: '网络不可用', detail: '当前页面没有加载成功', retryLabel: '重试', }); }); test('忽略外域和非页面加载失败', () => { expect( normalizeMobileShellLoadFailure( { type: 'http', url: 'https://example.com/', statusCode: 500, }, allowedOrigin, ), ).toBeNull(); expect( normalizeMobileShellLoadFailure( { type: 'native', url: 'about:blank', code: -1, }, allowedOrigin, ), ).toBeNull(); expect( normalizeMobileShellLoadFailure( { type: 'native', url: 'javascript:alert(1)', code: -1, }, allowedOrigin, ), ).toBeNull(); expect( normalizeMobileShellLoadFailure( { type: 'http', url: '/favicon.ico', statusCode: 404, }, allowedOrigin, ), ).toBeNull(); }); test('只展示当前主页面失败', () => { expect( normalizeMobileShellLoadFailure( { type: 'http', url: 'https://www.genarrative.world/assets/main.js', statusCode: 404, }, allowedOrigin, 'https://www.genarrative.world/creation/puzzle', ), ).toBeNull(); expect( normalizeMobileShellLoadFailure( { type: 'http', url: 'https://www.genarrative.world/creation/puzzle', statusCode: 503, }, allowedOrigin, 'https://www.genarrative.world/creation/puzzle', )?.title, ).toBe('加载失败 503'); }); test('连续 WebView 进程恢复失败时展示同源页面兜底', () => { expect( normalizeMobileShellLoadFailure( { type: 'process', url: 'https://www.genarrative.world/works/detail?work=WF-1&token=private#runtime', description: 'WebView renderer terminated repeatedly', }, allowedOrigin, 'https://www.genarrative.world/works/detail?work=WF-1&token=private#runtime', ), ).toEqual({ type: 'process', url: 'https://www.genarrative.world/works/detail', title: '页面已停止', detail: '当前页面连续恢复失败', retryLabel: '重试', }); }); });