// @vitest-environment jsdom import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; /** * FE-1(2026-10-19):`workflowApi` 的 401 必须与 `localApiClient` 共用同一登出处理。 * * 缺陷:workflow 请求层 401 只 throw 普通 Error,不清 session、不跳登录页、错误也没有 * status/code ⇒ 页面停在旧数据上,用户不知道要重新登录(与 localApiClient 行为不一致)。 * 修复:401 ⇒ `handleUnauthorized()`(清 session + 跳 `/login?reason=expired`;**无 session 不跳**), * 抛出的错误带 `status:401` 与后端 `error` 字段 code。 */ const SESSION_KEY = 'canteen.auth.session'; async function loadWorkflowApi() { vi.resetModules(); return import('../utils/workflowApi'); } function installLocationStub(): { href: string } { const loc = { href: '' }; try { Object.defineProperty(window, 'location', { configurable: true, value: loc }); } catch { // 极端 jsdom 版本不可重定义时,退化为直接赋值观察 (window as unknown as { location: { href: string } }).location = loc; } return loc; } function mockUnauthorized(): void { vi.stubGlobal( 'fetch', vi.fn(async () => ({ ok: false, status: 401, json: async () => ({ error: 'token_expired', error_description: '登录已过期,请重新登录' }), })) ); } beforeEach(() => { localStorage.clear(); vi.unstubAllGlobals(); }); afterEach(() => { vi.unstubAllGlobals(); localStorage.clear(); }); describe('B-10.1:raw fetch(文件上传)401 也走统一登出', () => { it('uploadPickingOutboundPhotoApi 401 ⇒ 清 session、跳一次登录、错误带 status/code', async () => { const { setStoredSession } = await import('../lib/localApiClient'); setStoredSession({ access_token: 't-upload', user: { id: 'u1' } } as never); const loc = installLocationStub(); vi.stubGlobal( 'fetch', vi.fn(async () => ({ ok: false, status: 401, json: async () => ({ error: 'token_expired' }) })) ); const { uploadPickingOutboundPhotoApi } = await loadWorkflowApi(); const file = new File(['x'], 'a.jpg', { type: 'image/jpeg' }); const error = await uploadPickingOutboundPhotoApi(file).catch((err) => err); expect(error).toMatchObject({ status: 401, code: 'token_expired' }); expect(localStorage.getItem(SESSION_KEY)).toBeNull(); expect(loc.href).toContain('/login'); expect(loc.href).toContain('reason=expired'); }); }); describe('workflowApi 401:与 localApiClient 共用登出处理', () => { it('有 session ⇒ 清 session、跳 /login?reason=expired、错误带 status=401 与 code', async () => { const { setStoredSession } = await import('../lib/localApiClient'); setStoredSession({ access_token: 't-1', user: { id: 'u1' } } as never); expect(localStorage.getItem(SESSION_KEY)).toBeTruthy(); const loc = installLocationStub(); mockUnauthorized(); const { submitInspectionComplianceApi } = await loadWorkflowApi(); const error = await submitInspectionComplianceApi({}).catch((err) => err); expect(error).toMatchObject({ status: 401, code: 'token_expired' }); expect(localStorage.getItem(SESSION_KEY), '401 后必须清掉本地 session').toBeNull(); expect(loc.href).toContain('/login'); expect(loc.href).toContain('reason=expired'); }); it('401 且 body 为 JSON null ⇒ 不崩、仍登出、错误带 status=401/code=unauthorized', async () => { const { setStoredSession } = await import('../lib/localApiClient'); setStoredSession({ access_token: 't-null', user: { id: 'u1' } } as never); const loc = installLocationStub(); vi.stubGlobal( 'fetch', vi.fn(async () => ({ ok: false, status: 401, body: null, json: async () => null })) ); const { submitInspectionComplianceApi } = await loadWorkflowApi(); const error = await submitInspectionComplianceApi({}).catch((err) => err); expect(error, 'body=null 时不得因读 payload.error 抛 TypeError').toMatchObject({ status: 401, code: 'unauthorized', }); expect(localStorage.getItem(SESSION_KEY)).toBeNull(); expect(loc.href).toContain('/login'); expect(loc.href).toContain('reason=expired'); }); it('无 session(登录页本身 401)⇒ 不跳转、不清数据', async () => { const loc = installLocationStub(); mockUnauthorized(); const { submitInspectionComplianceApi } = await loadWorkflowApi(); const error = await submitInspectionComplianceApi({}).catch((err) => err); expect(error).toMatchObject({ status: 401 }); expect(loc.href, '无 session 时不得把登录页再跳走').toBe(''); expect(localStorage.getItem(SESSION_KEY)).toBeNull(); }); });