From 12f7be21a0dd551f93998cf49dfcbbceb3891575 Mon Sep 17 00:00:00 2001 From: cat-shark <1716967236@qq.com> Date: Sat, 10 Oct 2026 10:52:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BC=95=E5=AF=BC=E6=81=A2=E5=A4=8D?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E8=BF=9B=E5=85=A5=E9=9B=B6=E8=BD=AE=E6=AC=A1?= =?UTF-8?q?=E7=9A=84=E6=AD=BB=E4=BC=9A=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../integration/guidance-flow.spec.ts | 46 +++++++++++++++++++ src/components/composables/useGuidance.ts | 20 +++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/__tests__/integration/guidance-flow.spec.ts b/src/__tests__/integration/guidance-flow.spec.ts index 905114e..635fa12 100644 --- a/src/__tests__/integration/guidance-flow.spec.ts +++ b/src/__tests__/integration/guidance-flow.spec.ts @@ -185,6 +185,52 @@ describe('学习引导(集成)', () => { expect(wrapper.text()).not.toContain('AI 没有回应') }) + it('恢复时不接受没有轮次的死会话:作废旧会话并重新开启引导', async () => { + localStorage.setItem(STORAGE_KEY, 'G9') + vi.mocked(abandonGuidance).mockResolvedValue(ok(null)) + // 首轮 AI 失败留下的会话:没结束,但一条轮次都没有 + vi.mocked(getGuidanceSession).mockResolvedValue( + ok({ + guidanceNum: 'G9', + taskNum: 'T001', + concept: '组合式 API', + turnsPayload: '[]', + reviewDurationSeconds: 0, + finished: false, + }), + ) + + const wrapper = await mountDialog() + + expect(getGuidanceSession).toHaveBeenCalledWith('G9') + expect(abandonGuidance).toHaveBeenCalledWith('G9') + expect(startGuidance).toHaveBeenCalledWith('T001', '组合式 API') + expect(localStorage.getItem(STORAGE_KEY)).toBe('G1') + expect(wrapper.text()).toContain('第一个问题') + }) + + it('作废旧会话失败不影响重新开启引导,界面照常拿到 AI 回复', async () => { + localStorage.setItem(STORAGE_KEY, 'G9') + vi.mocked(abandonGuidance).mockRejectedValue(new Error('作废失败')) + vi.mocked(getGuidanceSession).mockResolvedValue( + ok({ + guidanceNum: 'G9', + taskNum: 'T001', + concept: '组合式 API', + turnsPayload: '[]', + reviewDurationSeconds: 0, + finished: false, + }), + ) + + const wrapper = await mountDialog() + + expect(startGuidance).toHaveBeenCalledWith('T001', '组合式 API') + expect(localStorage.getItem(STORAGE_KEY)).toBe('G1') + expect(wrapper.text()).toContain('第一个问题') + expect(wrapper.text()).not.toContain('作废失败') + }) + it('离开页面再回来用 GET 恢复同一段对话,不重新开启', async () => { localStorage.setItem(STORAGE_KEY, 'G9') vi.mocked(getGuidanceSession).mockResolvedValue( diff --git a/src/components/composables/useGuidance.ts b/src/components/composables/useGuidance.ts index 2e0edcd..b638a96 100644 --- a/src/components/composables/useGuidance.ts +++ b/src/components/composables/useGuidance.ts @@ -68,6 +68,17 @@ export function useGuidanceSession() { localStorage.removeItem(storageKey(taskNum, concept)); }; + /** 作废一段什么都没聊成的会话:fire-and-forget,失败也不弹提示、不拦住建新引导 */ + const discardEmptySession = (guidanceNum: string) => { + try { + abandonGuidance(guidanceNum).catch(() => { + // 作废失败不影响接下来的新建 + }); + } catch { + // 同上:作废失败只是脏数据留档,不打断用户 + } + }; + const reset = () => { // 关弹窗/重开时取消在途轮询,避免无人等待的轮询结果影响下一次引导 stopPolling(); @@ -92,12 +103,17 @@ export function useGuidanceSession() { try { const res = await getGuidanceSession(stored); const restored = res?.data; - if (restored && !restored.finished) { + const restoredTurns = restored ? parseTurns(restored.turnsPayload) : []; + // 只有“还没结束且真的有轮次”的会话才值得续聊;空会话是 AI 失败留下的死会话 + if (restored && !restored.finished && restoredTurns.length > 0) { session.value = restored; - turns.value = parseTurns(restored.turnsPayload); + turns.value = restoredTurns; startDuration(); return; } + if (restored && !restored.finished) { + discardEmptySession(stored); + } clearStorage(taskNum, concept); } catch { clearStorage(taskNum, concept);