Files
wrong-notebook/vitest.config.mts
margrop af2521ffa7 refactor: AI 额度与凭据改为可替换的扩展接口
新增 AI 账户接口 AiAccountProvider(src/lib/ai/account.ts),集中四件事:调用前的额度判断、按用户的 AI 配置、每次补全的用量记录、用户删除后的清理。默认实现即原有行为:每日次数上限(管理员不受限)、全站共用 AI 配置、用量写入 AiCompletion。

- consumeAiQuota 在 AI 数据授权检查之后交给 authorize 判断,实现出错时放行并记日志;授权检查仍由上游负责,扩展不能跳过
- getAIService 先取 resolveConfig:返回 OpenAI 兼容配置时按该地址、密钥和模型请求,并发名额与 API Key 无关
- 自助注销、管理员删除用户、系统初始化删除用户成功后通知 onUserDeleted,出错不影响删除
- 构建时用 AI_ACCOUNT_EXTENSION 指定实现文件,next.config.ts 经 Turbopack resolveAlias 把 @wrong-notebook/ai-account 指向它;Vitest 固定使用默认实现
- 私有实现约定放在 src/extensions/local/,加入 .gitignore 与上传前检查的私有路径

文档补充 FORK_EXTENSION_POINTS.md 的 AI 账户一节、.env.example 与 AGENTS.md 约定;测试覆盖扩展拒绝与出错、单独配置、删除通知(含真实 SQLite)和上传前检查。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 14:18:09 +08:00

34 lines
1.3 KiB
TypeScript

import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
import { fileURLToPath } from 'node:url'
export default defineConfig({
plugins: [react()],
test: {
environment: 'jsdom',
globals: true,
setupFiles: ['./src/__tests__/setup.ts'],
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'server-only': fileURLToPath(new URL('./src/__tests__/stubs/server-only.ts', import.meta.url)),
// 测试始终使用默认的 AI 账户实现,不受 AI_ACCOUNT_EXTENSION 影响
'@wrong-notebook/ai-account': fileURLToPath(new URL('./src/lib/ai/account-default.ts', import.meta.url)),
},
include: ['src/__tests__/**/*.test.ts', 'src/__tests__/**/*.test.tsx'],
exclude: ['node_modules', '.next', 'dist'],
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'json-summary', 'html'],
include: ['src/lib/**/*.ts', 'src/app/api/**/*.ts'],
exclude: ['src/__tests__/**', 'node_modules/**'],
// 略低于当前覆盖率,防止覆盖率下降;补充测试后可逐步提高
thresholds: {
statements: 70,
branches: 65,
functions: 75,
lines: 70,
},
},
},
})