mirror of
https://github.com/sunniejs/vue-h5-template.git
synced 2026-11-26 17:53:34 +08:00
架构与目录: - 引入分层 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
74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
import {
|
|
configureStreamingClient,
|
|
FetchStreamChatProvider,
|
|
} from '@/services/ai/fetchStreamProvider';
|
|
|
|
afterEach(() => {
|
|
configureStreamingClient({
|
|
getAccessToken: undefined,
|
|
onUnauthorized: undefined,
|
|
});
|
|
vi.unstubAllGlobals();
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
describe('fetchStreamChatProvider', () => {
|
|
it('forwards the Gin access token and request id to FastAPI', async () => {
|
|
vi.stubEnv('VITE_REQUEST_ID_ENABLED', 'true');
|
|
configureStreamingClient({ getAccessToken: () => 'gin-jwt' });
|
|
const encoder = new TextEncoder();
|
|
const fetchMock = vi.fn(
|
|
async (_url: RequestInfo | URL, init?: RequestInit) => {
|
|
const headers = new Headers(init?.headers);
|
|
expect(headers.get('Authorization')).toBe('Bearer gin-jwt');
|
|
expect(headers.get('X-Request-ID')).toBeTruthy();
|
|
return new Response(
|
|
new ReadableStream({
|
|
start(controller) {
|
|
controller.enqueue(encoder.encode('data: {"type":"done"}\n\n'));
|
|
controller.close();
|
|
},
|
|
}),
|
|
{ status: 200 },
|
|
);
|
|
},
|
|
);
|
|
vi.stubGlobal('fetch', fetchMock);
|
|
|
|
const chunks = [];
|
|
const provider = new FetchStreamChatProvider('/api/ai/chat');
|
|
for await (const chunk of provider.chat([])) chunks.push(chunk);
|
|
expect(chunks).toEqual([{ type: 'done' }]);
|
|
});
|
|
|
|
it('normalizes a 401 response and clears the session', async () => {
|
|
const onUnauthorized = vi.fn();
|
|
configureStreamingClient({ onUnauthorized });
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async () =>
|
|
Response.json(
|
|
{
|
|
code: 401,
|
|
msg: 'Invalid token',
|
|
data: null,
|
|
requestId: 'request-1',
|
|
},
|
|
{
|
|
status: 401,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
},
|
|
),
|
|
),
|
|
);
|
|
|
|
const provider = new FetchStreamChatProvider('/api/ai/chat');
|
|
await expect(provider.chat([]).next()).rejects.toMatchObject({
|
|
status: 401,
|
|
requestId: 'request-1',
|
|
});
|
|
expect(onUnauthorized).toHaveBeenCalledOnce();
|
|
});
|
|
});
|