vue-h5-template/tests/api/client.test.ts
fonghehe 7b1c087c00 feat: 重构整体架构并迁移 oxlint/oxfmt 工具链
架构与目录:
- 引入分层 API:transport/interceptors 收敛到 src/api/client.ts,
  域名端点拆到 src/api/modules,页面不再直接依赖 Axios
- 服务层(src/services)承载 AI 流式解析与框架无关的 provider
- TanStack Query 接管服务端状态,Pinia 仅保留会话/主题等客户端状态
- 路由表显式懒加载,组件/组合式函数按职责拆分

功能:
- AI 流式对话:fetchStreamProvider + useStreamingChat + 安全 Markdown 渲染
- 商店模块:商品列表/详情/购物车/后台管理,多语言(中/英/日)
- 示例页:请求错误矩阵、query 缓存、移动端适配、图标演示
- 离线页、PWA、图片优化、eruda 调试等能力

工程化:
- ESLint/Prettier 迁移到 oxlint/oxfmt,stylelint 保留
  - 新增 oxlint.config.ts(移植自 monorepo 的 @vh5/oxlint-config 全套规则)
  - 新增 oxfmt.config.ts(移植自 @vh5/oxfmt-config,printWidth 80)
  - lint-staged、format、lint 脚本同步切换
- pnpm 作为唯一包管理器,删除 package-lock.json 与 yarn.lock
- 引入 Vitest/Playwright/release-please,补充单元与组件测试
- OpenAPI schema 与生成类型入库
- 环境变量按 mode 拆分,重构 wrapperEnv 解析

清理:
- 删除 .commitlintrc.js(CJS 配置在 type:module 下会崩溃),改用 commitlint.config.mjs
- 删除 eslint.config.mjs、prettier.config.js、.prettierignore
- 停止跟踪 .eslintrc-auto-import.json(已切 oxlint,无人引用)
- 移除 iconfont、旧 demo/list 页与 useAxiosApi/useFetchApi 等遗留实现
- husky 钩子移除 DEPRECATED 的 shebang 写法
- .workbuddy/ 加入 .gitignore
2026-09-02 14:37:52 +08:00

84 lines
2.6 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { AxiosError, AxiosHeaders } from 'axios';
import type { AxiosAdapter } from 'axios';
import {
apiClient,
axiosInstance,
configureApiClient,
normalizeApiError,
} from '@/api/client';
import { ApiError } from '@/types/api/common';
const originalAdapter = axiosInstance.defaults.adapter;
afterEach(() => {
axiosInstance.defaults.adapter = originalAdapter;
configureApiClient({ getAccessToken: undefined, onUnauthorized: undefined });
vi.unstubAllEnvs();
});
describe('api client', () => {
it('injects auth and request id, then unwraps data', async () => {
vi.stubEnv('VITE_REQUEST_ID_ENABLED', 'true');
configureApiClient({ getAccessToken: () => 'token-123' });
const adapter = vi.fn<AxiosAdapter>(async (config) => {
expect(config.headers.get('Authorization')).toBe('Bearer token-123');
expect(config.headers.get('X-Request-ID')).toMatch(
/^req-|^[0-9a-f-]{36}$/i,
);
return {
data: { code: 200, data: { id: 1 }, msg: 'ok' },
status: 200,
statusText: 'OK',
headers: new AxiosHeaders(),
config,
};
});
axiosInstance.defaults.adapter = adapter;
await expect(apiClient.get<{ id: number }>('/test')).resolves.toEqual({
id: 1,
});
});
it('turns business failures into ApiError and invokes unauthorized', async () => {
const onUnauthorized = vi.fn();
configureApiClient({ onUnauthorized });
axiosInstance.defaults.adapter = async (config) => ({
data: { code: 401, data: null, msg: 'Expired' },
status: 200,
statusText: 'OK',
headers: new AxiosHeaders(),
config,
});
await expect(apiClient.get('/test')).rejects.toMatchObject({
name: 'ApiError',
kind: 'business',
code: 401,
});
expect(onUnauthorized).toHaveBeenCalledOnce();
});
it('accepts successful 2xx business codes', async () => {
axiosInstance.defaults.adapter = async (config) => ({
data: { code: 201, data: { id: 1 }, msg: 'created' },
status: 201,
statusText: 'Created',
headers: new AxiosHeaders(),
config,
});
await expect(apiClient.post<{ id: number }>('/projects')).resolves.toEqual({
id: 1,
});
});
it('normalizes timeout, network and unknown failures', () => {
expect(
normalizeApiError(new AxiosError('late', 'ECONNABORTED')),
).toMatchObject({ kind: 'timeout' });
expect(normalizeApiError(new AxiosError('offline'))).toMatchObject({
kind: 'network',
});
expect(normalizeApiError('bad')).toBeInstanceOf(ApiError);
});
});