diff --git a/docs/architecture/refactoring-master-plan.md b/docs/architecture/refactoring-master-plan.md index 57ad9b0..1fbf681 100644 --- a/docs/architecture/refactoring-master-plan.md +++ b/docs/architecture/refactoring-master-plan.md @@ -689,3 +689,4 @@ npm --prefix frontend run build | 2026-08-15 | 完成业务测试覆盖切片“Items Hook 批量任务守卫与轮询错误” | 补充无最近任务、无预检、无有效行、用户拒绝取消、不可重试任务和轮询失败测试;验证批量发布状态机前置条件与错误收口,不触发真实商品文件或外部发布平台;前端 Items Hook 测试、类型和注释门禁通过 | 下一步:继续补齐 Settings/Accounts 等 Hook 的可控生命周期分支 | | 2026-08-15 | 完成阶段性业务覆盖收口切片“请求、账户、卡密、订单与设置边界” | 补充上传超时、批量发布空输入、卡密批量操作空守卫、订单无文件导入、设置外部点击与重载调度、账户绑定移除/AI 默认值/密码终态,以及仪表盘和聊天领域排序时间边界;前端全量 50 个文件、301 个用例通过,语句覆盖率提升至 96.58%、函数 97.25%、行 99.63%;纯 UI 和真实账号/平台流程仍按边界排除 | 下一步:继续处理剩余可控的过期响应、取消清理和领域分支,形成最终业务覆盖率验收清单 | | 2026-08-15 | 完成业务测试覆盖切片“账号任务、账号轮询与聊天实时边界” | 补充账号任务保存并发门禁、账号运行状态下一轮定时轮询、通知绑定移除/暂停状态更新、密码登录处理中轮询与关闭清理、聊天空发送/滚动守卫/未知会话排序,以及规则分页兼容入口测试;前端注释门禁、定向测试和类型检查通过,阶段覆盖率提升至语句 97.49%、函数 99.08%、行 99.86%;过期响应保护仍按真实取消时序分类保留 | 下一步:继续评估通知、卡密、设置与请求层过期响应是否能在不引入不稳定时序的前提下补测 | +| 2026-08-15 | 完成业务测试覆盖切片“通知与批量任务过期响应并发” | 补充通知渠道刷新丢弃旧响应、批量轮询防并发和关闭后旧响应失效,以及普通请求纯文本错误读取失败测试;验证请求代次和轮询保护,不触发真实推送、商品或平台服务;前端注释门禁、定向测试和类型检查通过 | 下一步:执行全量回归、覆盖率终审并记录仍需真实取消时序或不可达代码分支 | diff --git a/frontend/app/features/accounts/submoduleHooks.test.tsx b/frontend/app/features/accounts/submoduleHooks.test.tsx index d8e5140..2a5ca63 100644 --- a/frontend/app/features/accounts/submoduleHooks.test.tsx +++ b/frontend/app/features/accounts/submoduleHooks.test.tsx @@ -195,6 +195,15 @@ describe('useAccountSubmodules', /* 当前回调处理账号编辑、AI、通知 expect(passwordLoginMock).toHaveBeenCalledWith(expect.objectContaining({ account_id: 'account-1', account: 'user@example.com' }), expect.anything()); expect(hook.result.current.passwordLoginView.status).toBe('success'); expect(loadAccounts).toHaveBeenCalled(); + // passwordUpdater 是密码登录成功后清空密码字段的函数式更新器。 + let passwordUpdater: ((current: AccountEditForm) => AccountEditForm) | undefined; + for (const call /* call 是密码表单状态替身记录的一次调用参数。 */ of setEditForm.mock.calls) { + if (typeof call[0] === 'function') { + passwordUpdater = call[0] as (current: AccountEditForm) => AccountEditForm; + break; + } + } + expect(passwordUpdater?.(editFormFixture)).toMatchObject({ login_password: '', showLoginPassword: false }); await act( // cancelAction 取消当前密码登录会话并回到空闲状态。 @@ -339,6 +348,7 @@ describe('useAccountSubmodules', /* 当前回调处理账号编辑、AI、通知 // secondProcessingAction 再次进入处理中状态以验证关闭时取消会话。 async () => hook.result.current.handlePasswordLogin(), ); + cancelPasswordMock.mockRejectedValueOnce(new Error('取消失败')); await act( // closeAction 关闭编辑弹窗并取消处理中会话。 async () => hook.result.current.closeEditModal(), diff --git a/frontend/app/features/items/hooks.test.tsx b/frontend/app/features/items/hooks.test.tsx index 06c27f0..c7ec50c 100644 --- a/frontend/app/features/items/hooks.test.tsx +++ b/frontend/app/features/items/hooks.test.tsx @@ -401,4 +401,44 @@ describe('useItemPublishBatch', /* 当前回调处理批量发布的表单、任 vi.useRealTimers(); } }); + + test('批量轮询避免并发请求并丢弃关闭弹窗后的旧响应', /* 当前回调验证批量轮询并发与代次隔离。 */ async () => { + vi.useFakeTimers(); + // resolvePoll 是延迟轮询请求的完成控制器。 + let resolvePoll: (value: ItemPublishBatchResponse) => void = () => undefined; + // pendingPoll 是保持轮询进行中的请求 Promise。 + const pendingPoll = new Promise(/* pollExecutor 保存轮询完成函数。 */ resolve => { resolvePoll = resolve; }); + getBatchMock.mockReset(); + getBatchMock.mockReturnValueOnce(pendingPoll); + // hook 是批量轮询并发场景的 Hook 渲染结果。 + const hook = renderHook(/* stalePollHookFactory 创建批量轮询并发场景的 Hook。 */ () => useItemPublishBatch({ selectedAccount: 'account-1', loadItems: vi.fn(), loadShippingRules: vi.fn() })); + await act( + // stateAction 打开弹窗并注入运行中的任务详情。 + () => { + hook.result.current.setShowBatchModal(true); + hook.result.current.setBatchDetail(runningBatchFixture); + }, + ); + await act( + // firstTimerAction 推进首次轮询并保持请求未完成。 + async () => { await vi.advanceTimersByTimeAsync(3_000); }, + ); + await act( + // secondTimerAction 推进下一次轮询但不允许并发请求。 + async () => { await vi.advanceTimersByTimeAsync(3_000); }, + ); + expect(getBatchMock).toHaveBeenCalledOnce(); + await act( + // closeAction 关闭弹窗并使未完成轮询响应过期。 + async () => hook.result.current.closeBatchModal(), + ); + resolvePoll(runningBatchFixture); + await act( + // staleResolveAction 完成已过期的轮询响应。 + async () => { await pendingPoll; }, + ); + expect(hook.result.current.showBatchModal).toBe(false); + hook.unmount(); + vi.useRealTimers(); + }); }); diff --git a/frontend/app/features/notifications/hooks.test.tsx b/frontend/app/features/notifications/hooks.test.tsx index c4a7f54..ca82f17 100644 --- a/frontend/app/features/notifications/hooks.test.tsx +++ b/frontend/app/features/notifications/hooks.test.tsx @@ -262,4 +262,37 @@ describe('useNotifications', /* 当前回调处理通知渠道、SMTP 和动作 vi.useRealTimers(); } }); + + test('渠道刷新时丢弃先发出的过期响应', /* 当前回调验证通知渠道请求代次隔离。 */ async () => { + // ChannelResponse 是旧渠道请求使用的最小成功响应。 + type ChannelResponse = { + // success 表示请求成功。 + success: true; + // data 保存渠道列表。 + data: NotificationChannel[]; + }; + // resolveFirst 是旧渠道请求的完成控制器。 + let resolveFirst: (value: ChannelResponse) => void = () => undefined; + // firstRequest 是保持未完成的旧渠道请求 Promise。 + const firstRequest = new Promise(/* firstExecutor 保存旧请求完成函数。 */ resolve => { resolveFirst = resolve; }); + getChannelsMock.mockReset(); + getChannelsMock.mockReturnValueOnce(firstRequest); + getChannelsMock.mockResolvedValueOnce({ success: true, data: [channelFixture] }); + // hook 是通知渠道刷新竞态场景的 Hook 渲染结果。 + const hook = renderHook( + // staleHookFactory 创建通知渠道旧响应场景的 Hook。 + () => useNotifications(false), + ); + await act( + // refreshAction 发起第二次渠道刷新并使首次请求过期。 + async () => hook.result.current.loadChannels(), + ); + resolveFirst({ success: true, data: [] }); + await act( + // staleResolveAction 完成已过期的首次响应。 + async () => { await firstRequest; }, + ); + expect(hook.result.current.channels).toEqual([channelFixture]); + hook.unmount(); + }); }); diff --git a/frontend/request.test.ts b/frontend/request.test.ts index f0f93c3..a3f49e4 100644 --- a/frontend/request.test.ts +++ b/frontend/request.test.ts @@ -88,6 +88,13 @@ describe('request helpers', () => { await expect(get('/broken')).rejects.toThrow('请求失败: 500'); } /* 回调函数负责当前业务流程。 */); + test('普通请求纯文本错误体读取失败时使用状态码兜底', /* 当前回调验证普通请求文本错误读取失败分支。 */ async () => { + // response 是文本读取失败的普通请求响应替身。 + const response = { ok: false, status: 502, headers: { get: /* contentTypeGetter 返回纯文本类型。 */ () => 'text/plain' }, text: vi.fn().mockRejectedValue(new Error('读取失败')) } as unknown as Response; + vi.stubGlobal('fetch', vi.fn().mockResolvedValue(response)); + await expect(get('/broken-text')).rejects.toThrow('请求失败: 502'); + } /* 回调函数负责当前业务流程。 */); + test('网络层异常原样透传', async () => { // fetchMock 是抛出非取消网络异常的替身。 const fetchMock = vi.fn().mockRejectedValue(new Error('网络断开'));