From eca9995c5218f28ee9f58114c3e605db477ca612 Mon Sep 17 00:00:00 2001 From: Christ Date: Sat, 15 Aug 2026 11:21:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E9=BD=90=E8=AF=B7=E6=B1=82=E5=B1=82?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E5=BC=82=E5=B8=B8=E4=B8=8E=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E8=BD=BD=E8=8D=B7=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/architecture/refactoring-master-plan.md | 1 + frontend/request.test.ts | 35 +++++++++++++++++++ frontend/services/amapLocation.loader.test.ts | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index 251a24f..b2c328f 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -660,3 +660,4 @@ npm --prefix frontend run build | 2026-08-15 | 完成业务测试覆盖切片“AMap 地点查询适配边界” | 新增高德地点查询的无效坐标、无数据、失败响应、POI 字段校验和结果映射测试;仅使用浏览器全局与 PlaceSearch 可控替身,不触发真实地图网络请求;前端测试、类型和注释门禁通过,纯 UI 组件不纳入覆盖率目标 | 下一步:补齐请求层与业务 Hook 的未覆盖错误分支 | | 2026-08-15 | 完成业务测试覆盖切片“AMap 脚本加载错误边界” | 新增高德脚本首次注入、已有脚本节点、加载完成但对象缺失、脚本错误和超时测试;通过 jsdom 与动态模块隔离验证加载器生命周期,不访问真实地图网络;前端测试、类型和注释门禁通过 | 下一步:补齐业务 Hook 的未覆盖错误分支 | | 2026-08-15 | 完成业务测试覆盖切片“请求层方法与取消/错误边界” | 新增 PUT/DELETE 请求、纯文本错误、损坏 JSON 错误体、外部 AbortSignal 及上传取消测试;验证统一错误消息、认证失败通知和超时/取消区分;前端请求测试、类型和注释门禁通过,纯 UI 组件不纳入覆盖率目标 | 下一步:补齐业务 Hook 的未覆盖错误分支 | +| 2026-08-15 | 完成业务测试覆盖切片“请求层网络异常与上传载荷” | 补充普通请求/上传请求网络异常透传、上传失败响应和原始错误载荷保留测试;覆盖请求层非业务网络分支,不依赖真实服务;前端请求测试、类型和注释门禁通过 | 下一步:补齐业务 Hook 的未覆盖错误分支 | diff --git a/frontend/request.test.ts b/frontend/request.test.ts index a0b70e2..9285e33 100644 --- a/frontend/request.test.ts +++ b/frontend/request.test.ts @@ -77,6 +77,14 @@ describe('request helpers', () => { await expect(get('/broken')).rejects.toThrow('请求失败: 500'); } /* 回调函数负责当前业务流程。 */); + test('网络层异常原样透传', async () => { + // fetchMock 是抛出非取消网络异常的替身。 + const fetchMock = vi.fn().mockRejectedValue(new Error('网络断开')); + vi.stubGlobal('fetch', fetchMock); + + await expect(get('/network-error')).rejects.toThrow('网络断开'); + } /* 回调函数负责当前业务流程。 */); + test('notifies the app when an authenticated request returns 401', async () => { const events = new EventTarget(); /* events 表示events。 */ const listener = vi.fn(); /* listener 表示listener。 */ @@ -164,4 +172,31 @@ describe('request helpers', () => { controller.abort(); await expect(pending).rejects.toThrow('请求已取消'); } /* 回调函数负责当前业务流程。 */); + + test('上传网络层异常原样透传', async () => { + // fetchMock 是抛出非取消上传异常的替身。 + const fetchMock = vi.fn().mockRejectedValue(new Error('上传网络断开')); + vi.stubGlobal('fetch', fetchMock); + + await expect(postForm('/upload', new FormData())).rejects.toThrow('上传网络断开'); + } /* 回调函数负责当前业务流程。 */); + + test('上传失败保留原始错误载荷', async () => { + // payload 是服务端返回的上传错误详情。 + const payload = { code: 'invalid_file', message: '文件格式不支持' }; + vi.stubGlobal('fetch', vi.fn().mockResolvedValue(new Response(JSON.stringify(payload), { + status: 422, + headers: { 'content-type': 'application/json' }, + }))); + + try { + await postForm('/upload', new FormData()); + throw new Error('应当抛出上传错误'); + } catch (error /* error 表示上传异常。 */) { + // uploadError 是包含后端载荷的上传异常。 + const uploadError = error as Error & { /* payload 表示原始错误载荷。 */ payload?: unknown }; + expect(uploadError.message).toBe('文件格式不支持'); + expect(uploadError.payload).toEqual(payload); + } + } /* 回调函数负责当前业务流程。 */); } /* 回调函数负责当前业务流程。 */); diff --git a/frontend/services/amapLocation.loader.test.ts b/frontend/services/amapLocation.loader.test.ts index 7d0eabd..29b48b3 100644 --- a/frontend/services/amapLocation.loader.test.ts +++ b/frontend/services/amapLocation.loader.test.ts @@ -48,7 +48,7 @@ describe('AMap 脚本加载边界', /* describeCallback 组织高德脚本加载 const script = document.getElementById(SCRIPT_ID) as HTMLScriptElement; expect(script.src).toContain('webapi.amap.com/maps'); // amapStub 是脚本加载完成后暴露的高德对象。 - window.AMap = createAmapStub(); + window.AMap = createAmapStub() as NonNullable; window.__ydisksAmapLoaded?.(); await expect(pending).resolves.toMatchObject([{ poi_id: 'poi-1' }]); });