Skip to content

Latest commit

 

History

History
1182 lines (960 loc) · 30.5 KB

TESTING.md

File metadata and controls

1182 lines (960 loc) · 30.5 KB

测试指南

快速开始

1. 启动测试数据库

使用 Docker Compose 启动临时 PostgreSQL 测试数据库:

docker compose -f docker-compose.test.yml up -d

测试数据库使用 tmpfs(内存文件系统),数据不会持久化,重启容器后自动清空。

2. 运行测试

# 运行所有测试
pnpm test

# 运行完整浏览器 E2E 测试
pnpm test:e2e

# 监听模式(开发时推荐)
pnpm test:watch

# 查看测试 UI
pnpm test:ui

# 生成覆盖率报告
pnpm test:coverage

2.1 运行 E2E 测试

E2E 测试使用 Playwright,会自动启动本地 Next.js 开发服务器:

pnpm test:e2e

首次在新机器运行时,先安装 Chromium:

pnpm exec playwright install chromium

如果要测试已经启动的站点,可以指定地址:

E2E_BASE_URL=http://127.0.0.1:3000 pnpm test:e2e

E2E 覆盖的功能名:

  • 公开页面:首页改词、讨论、修改提议、词库管理、GitHub 同步、赞助、安装、练习、关于、登录、注册
  • 响应式导航:移动端菜单和核心入口
  • 认证功能:注册、退出、重新登录
  • 账号功能:资料页、数据统计、修改密码入口
  • 个人词库:新增、编辑、删除词条
  • 开发者功能:创建、删除 API Key
  • 批次功能:创建批次、添加修改提议
  • 公开 API:词库、批次、提议、讨论、同步、赞助接口
  • 权限边界:未登录、普通用户、ROOT 管理员访问控制
  • 开发者 API:X-API-Key 调用 v1 词库查询

3. 停止测试数据库

docker compose -f docker-compose.test.yml down

测试架构

目录结构

lib/test/
├── setup.ts       # 全局测试设置,数据库初始化和清理
├── helpers.ts     # 测试辅助函数
└── fixtures.ts    # 测试数据fixtures

tests/e2e/
├── global-setup.ts                 # E2E 迁移、角色初始化、测试数据清理
├── helpers/e2e-data.ts             # E2E 用户、登录态、种子数据辅助函数
├── public-pages.spec.ts            # 公开页面与移动导航 smoke 流程
├── auth-and-account.spec.ts        # 认证、账号、个人词库、开发者页面流程
└── api-and-permissions.spec.ts     # API、权限、批次、讨论流程

app/api/
├── pull-requests/
│   └── check-conflicts-batch/
│       └── route.test.ts    # 批次冲突检测测试
└── batches/
    └── submit-and-approve.test.ts  # 批次提交和审批测试

测试辅助函数

lib/test/helpers.ts

核心函数:

// 创建测试用户(自动生成唯一用户名)
createTestUser(role?: 'User' | 'Admin'): Promise<User>

// 批量添加测试词条
seedPhrases(userId: number, phrases: Array<{
  word: string
  code: string
  type: PhraseType
  weight?: number
}>): Promise<Phrase[]>

// 调用批次冲突检测服务
checkBatchConflicts(items: ConflictCheckItem[]): Promise<{
  results: Array<{
    id: string
    conflict: ConflictInfo
    calculatedWeight?: number
  }>
}>

// 创建测试批次和 PR
createTestBatch(userId: number, prs: Array<{
  action: 'Create' | 'Change' | 'Delete'
  word?: string
  oldWord?: string
  code?: string
  type?: PhraseType
  weight?: number
}>): Promise<{ batch: Batch, prs: PullRequest[] }>

使用示例:

describe('My Test', () => {
  let userId: number

  beforeEach(async () => {
    const user = await createTestUser()
    userId = user.id
    
    // 准备测试数据
    await seedPhrases(userId, [
      { word: '测试', code: 'test', type: 'Phrase', weight: 100 }
    ])
  })

  it('should check conflicts', async () => {
    const { results } = await checkBatchConflicts([
      { id: '1', action: 'Create', word: '新词', code: 'test', type: 'Phrase' }
    ])
    
    expect(results[0].conflict.hasConflict).toBe(false)
    expect(results[0].calculatedWeight).toBe(101)
  })
})

lib/test/fixtures.ts

测试数据集:

// 基础词条数据
export const basePhrases = [
  { word: '如果', code: 'rjgl', type: 'Phrase' as PhraseType },
  { word: '然后', code: 'rhoh', type: 'Phrase' as PhraseType },
  { word: '因为', code: 'ywoi', type: 'Phrase' as PhraseType },
]

// 测试用户凭据
export const testUserCredentials = {
  name: 'testuser',
  password: 'test123'
}

lib/test/setup.ts

全局 hooks:

// 测试开始前: 运行数据库迁移
beforeAll(async () => {
  await execAsync('pnpm prisma migrate dev --skip-generate')
})

// 每个测试后: 清理所有数据
afterEach(async () => {
  // 使用 TRUNCATE CASCADE 清理所有表
  await prisma.$executeRaw`TRUNCATE TABLE ...`
})

// 测试结束后: 断开数据库连接
afterAll(async () => {
  await prisma.$disconnect()
})

测试覆盖场景

测试套件 1: 批次冲突检测 (19 个场景,27 个测试)

app/api/pull-requests/check-conflicts-batch/route.test.ts

测试重点:验证批次操作的冲突检测、动态权重计算、冲突解决逻辑

基础场景 (Scenario 1-9)

Scenario 1: 添加重码词

数据库: { word: '如果', code: 'rjgl', weight: 100 }
操作: Create '茹果' at 'rjgl'
预期: 
  - hasConflict = false (允许创建)
  - currentPhrase = '如果' (显示现有词)
  - 提供建议: Move/Adjust/Cancel
  - 生成次选编码建议

Scenario 2: 批次内冲突解决 - 先添加后删除

数据库: { word: '如果', code: 'rjgl' }
操作: [
  Create '茹果' at 'rjgl',  // 与现有词冲突
  Delete '如果' at 'rjgl'   // 删除冲突词
]
预期:
  - Create: 检测到冲突但被后续 Delete 解决
  - Delete: 成功
  - impact 包含 "冲突已由批次内修改解决"

Scenario 3: 删除后添加 - 位置释放

数据库: { word: '如果', code: 'rjgl' }
操作: [
  Delete '如果' at 'rjgl',  // 释放位置
  Create '茹果' at 'rjgl'   // 使用释放的位置
]
预期:
  - Delete: 成功
  - Create: 无冲突 (位置已被释放)

Scenario 4: 修改后添加

数据库: { word: '如果', code: 'rjgl' }
操作: [
  Change '如果' → '茹果' at 'rjgl',
  Create '新词' at 'rjgl'
]
预期:
  - Change: 成功
  - Create: 检测到现有词但允许创建 (重码)

Scenario 5: 批次内重复检测

操作: [
  Create '测试' at 'test',
  Create '测试' at 'test'  // 完全相同
]
预期:
  - 第一个: 成功
  - 第二个: hasConflict = true, impact 包含 "批次内重复"

Scenario 6: Change 缺少 oldWord - 词不存在

数据库: 空
操作: Change '新词' (oldWord: '旧词') at 'test'
预期:
  - hasConflict = true
  - impact: "词条不存在,无法修改"

Scenario 7: Delete 不存在的词

数据库: 空
操作: Delete '不存在' at 'test'
预期:
  - hasConflict = true
  - impact: "不存在,无法删除"

Scenario 8: Change 未提供 oldWord

操作: Change (word: '新词', 无 oldWord) at 'test'
预期:
  - hasConflict = true
  - impact: "Change 操作必须提供 oldWord"

Scenario 9: 次选编码生成

操作: Create '茹果' at 'rjgl'
预期:
  - 生成次选编码: rjgla, rjgli, rjgld, rjglu 等
  - 验证建议数组非空
高级场景 (Scenario 10-18) - 动态权重与复杂操作

Scenario 10: Delete → Create 冲突解决

数据库: { word: '如果', code: 'rjgl', weight: 100 }
操作: [
  Delete '如果' at 'rjgl',
  Create '茹果' at 'rjgl'
]
预期:
  - Delete: 成功,找到现有词
  - Create: 检测到数据库中的 '如果'
  - 冲突解决: 先前的 Delete 解决了这个冲突
  - impact: "冲突已由批次内修改 #1 解决(删除了重码词)"
  - calculatedWeight: 100 (base, 无其他词)

Scenario 11: Create → Delete 循环错误

数据库: 空
操作: [
  Create '新词' at 'xinc',
  Delete '新词' at 'xinc'  // 试图删除刚创建的词
]
预期:
  - Create: 成功(数据库中不存在)
  - Delete: hasConflict = true(数据库中还不存在,批次未执行)
  - impact: "不存在,无法删除"

Scenario 12: 多个 Create - 权重递进

数据库: { word: '测试1', code: 'test', weight: 100 }
操作: [
  Create '测试2' at 'test',
  Create '测试3' at 'test',
  Create '测试4' at 'test'
]
预期权重计算:
  - PR1: base(100) + existing(1) + previous_creates(0) = 101
  - PR2: base(100) + existing(1) + previous_creates(1) = 102
  - PR3: base(100) + existing(1) + previous_creates(2) = 103
验证: impact 消息中包含 "101", "102", "103"

Scenario 13: 删除全部 → Create (权重重置)

数据库: [
  { word: '测试A', code: 'ceshi', weight: 100 },
  { word: '测试B', code: 'ceshi', weight: 101 }
]
操作: [
  Delete '测试A' at 'ceshi',  // -1
  Delete '测试B' at 'ceshi',  // -1
  Create '测试C' at 'ceshi'
]
预期权重计算:
  - existing = 2
  - deletes = -2
  - adjusted = 2 - 2 = 0
  - calculatedWeight = 100 + 0 = 100 (重置为 base)
验证: impact 包含 "100"

Scenario 14: Change A→B, 然后 Create A (名称复用)

数据库: { word: '原词', code: 'code1', weight: 100 }
操作: [
  Change '原词' → '新词' at 'code1',
  Create '原词' at 'code1'  // 复用被修改掉的名称
]
预期:
  - Change: 成功
  - Create: 数据库中看到 '原词',但 Change 会释放这个名称
  - 冲突解决: "冲突已由批次内修改 #1 解决(将 '原词' 修改为 '新词')"

Scenario 15: 复杂链 - Delete, Change, Create

数据库: [
  { word: '词一', code: 'chain', weight: 100 },
  { word: '词二', code: 'chain', weight: 101 },
  { word: '词三', code: 'chain', weight: 102 }
]
操作: [
  Delete '词一' at 'chain',        // existing: 3 → 2
  Change '词二' → '词二改' at 'chain',  // 不改变数量
  Create '词四' at 'chain'
]
预期:
  - All operations: hasConflict = false
  - Weight: 3 existing - 1 delete = 2
  - calculatedWeight = 100 + 2 = 102
验证: impact 包含 "102"

Scenario 16: 批次内精确重复检测

操作: [
  Create '重复词' at 'cfuc',
  Create '重复词' at 'cfuc'  // 完全相同:word + code
]
预期:
  - 第一个: hasConflict = false
  - 第二个: hasConflict = true, impact 包含 "批次内重复"

Scenario 17: Delete 减少后续 Create 权重

数据库: [
  { word: '词A', code: 'worda', weight: 100 },
  { word: '词B', code: 'worda', weight: 101 },
  { word: '词C', code: 'worda', weight: 102 }
]
操作: [
  Delete '词A' at 'worda',  // -1
  Create '词D' at 'worda'
]
预期:
  - existing = 3
  - delete effect = -1
  - adjusted = 3 - 1 = 2
  - calculatedWeight = 100 + 2 = 102
验证: impact 包含 "102"

Scenario 18: 多个 Delete 累积效果

数据库: [
  { word: '词1', code: 'multi', weight: 100 },
  { word: '词2', code: 'multi', weight: 101 },
  { word: '词3', code: 'multi', weight: 102 },
  { word: '词4', code: 'multi', weight: 103 }
]
操作: [
  Delete '词1' at 'multi',  // -1
  Delete '词2' at 'multi',  // -2
  Delete '词3' at 'multi',  // -3
  Create '词5' at 'multi'
]
预期:
  - existing = 4
  - cumulative deletes = -3
  - adjusted = 4 - 3 = 1
  - calculatedWeight = 100 + 1 = 101
验证: impact 包含 "101"

Scenario 19: 相同词+编码组合重复检测(重要业务规则)

数据库: { word: '这里', code: 'felk', weight: 100 }

测试 1: 尝试添加完全相同的词+编码组合
操作: Create '这里' at 'felk'
预期:
  - hasConflict = true
  - currentPhrase.word = '这里'
  - impact 包含 "组合已存在"
  - suggestions[0].action = 'Cancel'
  - 原因: ❌ 不允许相同词+相同编码重复

测试 2: 添加相同编码但不同词(重码)
操作: Create '那里' at 'felk'
预期:
  - hasConflict = false(允许重码)
  - currentPhrase.word = '这里'
  - calculatedWeight = 101 (base 100 + 1 existing)
  - suggestions.length > 0(提供次选编码建议)
  - 原因: ✅ 允许不同词+相同编码(重码词)

业务规则总结:
  - ❌ 禁止: 相同词 + 相同编码
  - ✅ 允许: 不同词 + 相同编码(重码)
  - ✅ 允许: 相同词 + 不同编码

测试套件 2: 批次提交和执行 (7 个场景)

app/api/batches/submit-and-approve.test.ts

测试重点:验证批次提交流程、管理员审批、数据库事务执行

Scenario 1: 提交无效批次(无 PR)

批次: 空 PR 列表
操作: 提交批次
预期: 
  - Status 400
  - Error: "No pull requests in batch"

Scenario 2: 提交有冲突的批次(需管理员)

数据库: { word: '测试', code: 'test' }
PR: Create '测试2' at 'test' (会产生重码)
操作: 普通用户提交
预期:
  - Status 200
  - 批次状态更新为 'PendingReview'
  - 检测到冲突,需要管理员审批

Scenario 3: 管理员审批并执行批次

PR: Delete existing phrase
操作: 管理员调用 approve 接口
预期:
  - Status 200
  - 批次状态 'Approved' → 'Merged'
  - PR 状态 'Open' → 'Merged'
  - 数据库中词条被删除

Scenario 4: 执行 Delete 操作

数据库: { word: '待删除', code: 'del', weight: 100 }
批次: [Delete '待删除' at 'del']
操作: Execute batch
预期:
  - 词条从数据库中删除
  - Phrase count 减少

Scenario 5: 执行 Create 操作

批次: [Create '新词' at 'new' with type='Phrase']
操作: Execute batch
预期:
  - 词条添加到数据库
  - weight = base weight (100)
  - type = 'Phrase'

Scenario 6: 执行 Change 操作

数据库: { word: '旧词', code: 'code', weight: 100 }
批次: [Change '旧词' → '新词' at 'code']
操作: Execute batch
预期:
  - 词条 word 字段更新
  - code 和 weight 保持不变

Scenario 7: 事务回滚(错误处理)

批次: [
  Delete '存在的词',
  Delete '不存在的词'  // 会导致错误
]
操作: Execute batch
预期:
  - 整个事务回滚
  - 第一个 Delete 也不会生效
  - 数据库状态保持一致

测试套件 3: 性能与可扩展性 (7 个场景)

app/api/pull-requests/check-conflicts-batch/performance.test.ts

测试重点:验证大批次处理能力、性能基准、算法复杂度

Scenario 1: 100+ Create 操作效率

操作: 100 个 Create 操作在同一编码
预期:
  - 全部成功处理
  - 执行时间 < 5 秒
  - 权重正确递进 (101 → 150 → 200)
实测: ~250ms ✅

Scenario 2: 100+ 混合操作 (Delete/Change/Create)

数据库: 50 个已存在词条
操作: 30 Delete + 30 Change + 40 Create
预期:
  - 全部成功处理
  - 执行时间 < 10 秒
实测: ~99ms ✅

Scenario 3: 200+ 操作带冲突解决

数据库: 100 个已存在词条
操作: 100 Delete + 100 Create (删除所有再创建新的)
预期:
  - 200 个操作全部成功
  - Create 看到冲突但被 Delete 解决
  - 执行时间 < 15 秒
  - impact 包含 "冲突已由批次内修改"
实测: ~169ms ✅

Scenario 4: O(n) 性能验证

批次大小: 10, 50, 100, 200
测试: 测量每个批次大小的执行时间
预期:
  - ops/ms 保持相对稳定
  - 证明算法复杂度为 O(n)
实测结果:
  Size  10: 12.72ms (0.79 ops/ms)
  Size  50: 65.63ms (0.76 ops/ms)
  Size 100: 127.68ms (0.78 ops/ms)
  Size 200: 248.72ms (0.80 ops/ms)
结论: O(n) 线性增长 ✅

Scenario 5: 重复检测效率

操作: 100 个 Create (50 对重复)
预期:
  - 快速检测重复
  - 执行时间 < 3 秒
  - 每对中第二个标记为重复
实测: ~38ms ✅

Scenario 6: 极限批次压力测试 (500 operations)

操作: 500 个 Create 操作
预期:
  - 全部成功处理
  - 执行时间 < 30 秒
  - 权重正确计算 (101 → 600)
实测: ~741ms (远低于预期) ✅

Scenario 7: 并发编码模拟

操作: 5 个不同编码,每个 20 个操作,交错执行
预期:
  - 100 个操作全部成功
  - 不同编码操作互不干扰
  - 每个编码权重独立计算
  - 执行时间 < 5 秒
实测: ~153ms ✅

性能优化说明:

  • 使用 Map 数据结构替代嵌套循环
  • 冲突解决检查从 O(n²) 优化到 O(n)
  • 大批次 (200+ ops) 性能提升 37-70%

测试配置

测试统计

  • 测试文件: 3 个
  • 测试场景: 34 个 (功能测试 27 + 性能测试 7)
  • 测试套件: 3 个
  • 覆盖率: 核心冲突检测、批次执行、权重计算逻辑、性能与可扩展性

核心测试技术

1. 动态权重计算算法

// 公式
calculatedWeight = baseWeight + adjustedExistingCount

// 其中:
baseWeight = getDefaultWeight(phraseType)  // Phrase: 100
adjustedExistingCount = existingCount + net_change

// net_change 通过模拟批次操作计算:
net_change = Σ (operation_effect)
  - Delete: -1 (减少一个词条)
  - Change: 0  (不改变编码上的词条数量)
  - Create: +1 (仅计算当前操作之前的 Create)

计算示例:

场景 1: 数据库有 1 个词,批次中连续 Create 3 个
  
  PR1 (Create):
    existing = 1
    previous_creates = 0
    adjusted = 1 + 0 = 1
    weight = 100 + 1 = 101 ✅

  PR2 (Create):
    existing = 1
    previous_creates = 1 (PR1)
    adjusted = 1 + 1 = 2
    weight = 100 + 2 = 102 ✅

  PR3 (Create):
    existing = 1
    previous_creates = 2 (PR1, PR2)
    adjusted = 1 + 2 = 3
    weight = 100 + 3 = 103 ✅

场景 2: 数据库有 4 个词,批次中先 Delete 3 个,再 Create 1 个

  Delete #1: net_change = -1
  Delete #2: net_change = -2
  Delete #3: net_change = -3
  
  Create:
    existing = 4
    net_change = -3
    adjusted = 4 - 3 = 1
    weight = 100 + 1 = 101 ✅

2. 冲突解决检测机制

// 检测逻辑: 向前查找解决操作
for (let i = 0; i < results.length; i++) {
  if (result[i].conflict.currentPhrase) {
    // 检查之前的操作 (j = 0 to i-1)
    for (let j = 0; j < i; j++) {
      const resolution = checkConflictResolution(items[j], result[i])
      if (resolution.resolved) {
        // 更新冲突状态
        result[i].conflict.hasConflict = false
        result[i].conflict.impact = "冲突已由批次内修改解决"
        break
      }
    }
  }
}

解决规则:

冲突类型 解决操作 匹配条件 示例
Create 遇到已存在词 Delete word + code 匹配 Delete '如果' 解决 Create '茹果' at 'rjgl' 的冲突
Create 遇到已存在词 Change oldWord + code 匹配 Change '原词'→'新词' 解决 Create '原词' 的冲突

重要: 检查方向是向前 (earlier operations),因为批次按顺序执行,先执行的操作会影响后续操作。

3. 批次内重复检测

// 使用 Map 追踪已处理的 Create 操作
const processedCreates = new Map<string, number>()
key = `${code}:${word}`

// 对于每个 Create 操作:
if (item.action === 'Create' && processedCreates.has(key)) {
  // 检测到重复
  conflict.hasConflict = true
  conflict.impact = "批次内重复添加相同的词条"
}

4. 数据库事务处理

所有批次执行操作都在事务中进行:

await prisma.$transaction(async (tx) => {
  for (const pr of pullRequests) {
    if (pr.action === 'Delete') {
      await tx.phrase.delete({ where: { id: pr.phraseId } })
    }
    // ... 其他操作
  }
})

如果任何操作失败,整个批次回滚,保证数据一致性。

环境变量 (.env.test)

DATABASE_URL="postgresql://postgres:postgres@localhost:5433/keytao_test?schema=public"
JWT_SECRET="test-jwt-secret-key"

Vitest 配置 (vitest.config.ts)

  • 环境: happy-dom(轻量级 DOM 实现)
  • 设置文件: lib/test/setup.ts
  • 全局变量: 启用(describe、it、expect 等)
  • 覆盖率: v8 provider
  • 文件并行: 禁用(fileParallelism: false)避免数据库竞争

测试覆盖总结

覆盖的功能模块

功能模块 测试数量 覆盖率 关键验证点
冲突检测 20 100% ✅ 数据库冲突
✅ 批次内冲突
✅ 冲突解决
✅ 次选编码生成
✅ 词+编码组合唯一性
权重计算 9 100% ✅ 动态权重
✅ 增删影响
✅ 权重递进
✅ 权重重置
批次执行 7 100% ✅ Create 执行
✅ Delete 执行
✅ Change 执行
✅ 事务回滚
数据验证 7 100% ✅ oldWord 验证
✅ 存在性检查
✅ 重复检测
✅ 词+编码组合验证
性能扩展 7 100% ✅ 大批次处理 (100-500 ops)
✅ O(n) 算法验证
✅ 并发模拟
✅ 压力测试

验证的边界条件

  • ✅ 空数据库: 验证初始创建行为
  • ✅ 单词条: 验证基础操作
  • ✅ 多词条: 验证权重计算
  • ✅ 批次空操作: 验证错误处理
  • ✅ 批次内重复: 验证去重逻辑
  • ✅ 操作顺序: 验证先后执行影响
  • ✅ 冲突解决: 验证 Delete/Change 解决逻辑
  • ✅ 权重边界: 验证权重重置和递进
  • ✅ 词+编码组合唯一性: 验证相同词+编码不能重复,重码词允许

核心代码覆盖

服务层

  • ✅ lib/services/batchConflictService.ts (100%)

    • calculateDynamicWeight() - 9 个场景测试
    • checkConflictResolution() - 3 个场景测试
    • checkBatchDuplicates() - 1 个场景测试
    • checkBatchConflictsWithWeight() - 所有场景使用
  • ✅ lib/services/conflictDetector.ts (核心路径 100%)

    • checkConflict() - 19 个场景覆盖
    • generateSuggestions() - 验证次选编码
    • generateAlternativeCodes() - 验证编码生成

API 层

  • ✅ app/api/pull-requests/check-conflicts-batch/route.ts (100%)

    • POST endpoint 全覆盖
    • 错误处理验证
  • ✅ app/api/batches/submit-and-approve.test.ts (核心流程 100%)

    • 提交流程
    • 审批流程
    • 执行流程

测试质量指标

✅ 测试文件: 3 个
✅ 测试场景: 34 个 (功能 27 + 性能 7)
✅ 测试套件: 3 个  
✅ 断言数量: 160+ 个
✅ 通过率: 100%
✅ 平均执行时间: ~6.5s
✅ 数据库隔离: 完全隔离
✅ 可重复性: 100%
✅ 性能验证: O(n) 算法复杂度
✅ 极限容量: 500+ operations

常见问题

Q: 测试失败提示数据库连接错误?

A: 确保测试数据库已启动:

docker compose -f docker-compose.test.yml up -d
docker compose -f docker-compose.test.yml ps  # 检查状态

Q: 测试之间有数据污染?

A: 每个测试后会自动清理数据(afterEach),如果遇到问题,手动重启数据库:

docker compose -f docker-compose.test.yml restart

Q: 如何调试单个测试?

A: 使用 .only 或命令行过滤:

it.only('should test something', () => {
  // 只运行这个测试
})
pnpm test route.test.ts  # 只运行指定文件

Q: 为什么使用 PostgreSQL 而不是 SQLite?

A:

  • Prisma schema 配置为 PostgreSQL,切换到 SQLite 需要修改 schema
  • 使用 tmpfs 的 PostgreSQL 容器同样快速且内存隔离
  • 测试环境与生产环境保持一致,避免 SQL 方言差异

性能优化

并行测试

Vitest 默认并行运行测试文件,但同一文件内的测试串行执行。如需完全隔离:

// 在测试文件顶部添加
import { describe } from 'vitest'

describe.concurrent('My Suite', () => {
  // 测试将并发运行
})

跳过数据库清理

如果测试不修改数据库,可以跳过清理:

import { describe, it, expect } from 'vitest'

describe('Read-only tests', () => {
  // 这些测试不会触发 afterEach 清理
})

扩展测试

添加新的测试场景

步骤 1: 定义测试数据 (可选)

在 lib/test/fixtures.ts 中添加新的测试数据:

export const complexScenarioPhrases = [
  { word: '复杂', code: 'fzjd', type: 'Phrase' as PhraseType, weight: 100 },
  { word: '场景', code: 'qjpw', type: 'Phrase' as PhraseType, weight: 100 },
]

步骤 2: 添加辅助函数 (可选)

在 lib/test/helpers.ts 中添加特定场景的辅助函数:

export async function setupComplexScenario(userId: number) {
  await seedPhrases(userId, complexScenarioPhrases)
  return await createTestBatch(userId, [
    { action: 'Delete', word: '复杂', code: 'fzjd' },
    { action: 'Create', word: '复杂', code: 'fzjd', type: 'Phrase' },
  ])
}

步骤 3: 编写测试用例

创建新的测试文件或在现有文件中添加:

import { describe, it, expect, beforeEach } from 'vitest'
import { createTestUser, checkBatchConflicts, seedPhrases } from '@/lib/test/helpers'

describe('Advanced Scenario: Triple operation chain', () => {
  let userId: number

  beforeEach(async () => {
    const user = await createTestUser()
    userId = user.id
  })

  it('should handle Delete → Change → Create chain', async () => {
    // 准备数据
    await seedPhrases(userId, [
      { word: 'A', code: 'test', type: 'Phrase', weight: 100 },
      { word: 'B', code: 'test', type: 'Phrase', weight: 101 },
    ])

    // 定义操作
    const items = [
      { id: '1', action: 'Delete' as const, word: 'A', code: 'test' },
      { id: '2', action: 'Change' as const, word: 'B2', oldWord: 'B', code: 'test', type: 'Phrase' as const },
      { id: '3', action: 'Create' as const, word: 'C', code: 'test', type: 'Phrase' as const },
    ]

    // 执行检测
    const { results } = await checkBatchConflicts(items)

    // 验证结果
    expect(results).toHaveLength(3)
    expect(results[0].conflict.hasConflict).toBe(false) // Delete 成功
    expect(results[1].conflict.hasConflict).toBe(false) // Change 成功
    expect(results[2].conflict.hasConflict).toBe(false) // Create 允许
    
    // 验证权重计算: 2 existing - 1 deleted = 1
    expect(results[2].calculatedWeight).toBe(101)
  })
})

典型测试模式

模式 1: 基础操作验证

it('should perform basic operation', async () => {
  // Arrange: 准备数据
  await seedPhrases(userId, [{ word: 'test', code: 'test', type: 'Phrase' }])
  
  // Act: 执行操作
  const { results } = await checkBatchConflicts([
    { id: '1', action: 'Delete', word: 'test', code: 'test' }
  ])
  
  // Assert: 验证结果
  expect(results[0].conflict.hasConflict).toBe(false)
})

模式 2: 冲突检测验证

it('should detect conflict', async () => {
  await seedPhrases(userId, [{ word: 'existing', code: 'code', type: 'Phrase' }])
  
  const { results } = await checkBatchConflicts([
    { id: '1', action: 'Create', word: 'new', code: 'code', type: 'Phrase' }
  ])
  
  // 验证检测到现有词
  expect(results[0].conflict.currentPhrase?.word).toBe('existing')
  // 验证提供建议
  expect(results[0].conflict.suggestions.length).toBeGreaterThan(0)
})

模式 3: 批次内交互验证

it('should resolve conflict within batch', async () => {
  await seedPhrases(userId, [{ word: 'old', code: 'code', type: 'Phrase' }])
  
  const { results } = await checkBatchConflicts([
    { id: '1', action: 'Delete', word: 'old', code: 'code' },
    { id: '2', action: 'Create', word: 'new', code: 'code', type: 'Phrase' }
  ])
  
  // 验证第二个操作的冲突被第一个操作解决
  expect(results[1].conflict.hasConflict).toBe(false)
  expect(results[1].conflict.impact).toContain('冲突已由批次内修改')
})

模式 4: 权重计算验证

it('should calculate weight correctly', async () => {
  await seedPhrases(userId, [
    { word: 'w1', code: 'code', type: 'Phrase', weight: 100 },
    { word: 'w2', code: 'code', type: 'Phrase', weight: 101 },
  ])
  
  const { results } = await checkBatchConflicts([
    { id: '1', action: 'Create', word: 'w3', code: 'code', type: 'Phrase' }
  ])
  
  // 验证权重 = base(100) + existing(2) = 102
  expect(results[0].calculatedWeight).toBe(102)
  expect(results[0].conflict.impact).toContain('102')
})

调试技巧

1. 查看完整结果

it('debug test', async () => {
  const { results } = await checkBatchConflicts(items)
  
  // 打印完整结果用于调试
  console.log('Results:', JSON.stringify(results, null, 2))
  
  // 针对性断言
  expect(results[0]).toMatchObject({
    conflict: {
      hasConflict: false,
      // ...
    }
  })
})

2. 使用 .only 运行单个测试

// 只运行这个测试
it.only('should test specific scenario', async () => {
  // ...
})

3. 跳过测试

// 暂时跳过
it.skip('should test later', async () => {
  // ...
})

示例:添加新测试

需求: 测试 Change → Change 连续修改同一词条

describe('Scenario 19: Multiple Changes on same phrase', () => {
  it('should handle consecutive changes', async () => {
    // 准备
    await seedPhrases(userId, [
      { word: 'v1', code: 'test', type: 'Phrase', weight: 100 }
    ])

    // 操作
    const items = [
      { id: '1', action: 'Change' as const, word: 'v2', oldWord: 'v1', code: 'test', type: 'Phrase' as const },
      { id: '2', action: 'Change' as const, word: 'v3', oldWord: 'v2', code: 'test', type: 'Phrase' as const },
    ]

    const { results } = await checkBatchConflicts(items)

    // 验证
    expect(results).toHaveLength(2)
    
    // 第一个 Change 应该成功
    expect(results[0].conflict.hasConflict).toBe(false)
    expect(results[0].conflict.currentPhrase?.word).toBe('v1')
    
    // 第二个 Change:
    // - 数据库中仍是 'v1'(批次未执行)
    // - 查找 oldWord='v2' 会失败
    expect(results[1].conflict.hasConflict).toBe(true)
    expect(results[1].conflict.impact).toContain('不存在')
  })
})

CI/CD 集成

GitHub Actions 示例

name: Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:15-alpine
        env:
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: keytao_test
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5433:5432

    steps:
      - uses: actions/checkout@v3
      - uses: pnpm/action-setup@v2
      - uses: actions/setup-node@v3
        with:
          node-version: '20'
          cache: 'pnpm'
      
      - run: pnpm install
      - run: pnpm test

参考资料