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);