test: 补充 hatchEgg 和 species 补充字段测试(227→237)

- egg.test.ts: 新增 hatchEgg 5 个测试
  - 创建 creature 并移除 egg
  - 放入 party 空位
  - totalEggsObtained 计数
  - 新 species dex entry 创建
  - 已有 species caughtCount 递增
- species.test.ts: 新增 ensureSpeciesData、baseHappiness、captureRate、names、shinyChance 测试

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
claude-code-best
2026-04-22 05:04:16 +08:00
parent 363ba39cad
commit 080bd93efc
2 changed files with 73 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
import { describe, test, expect } from 'bun:test'
import { checkEggEligibility, generateEgg, advanceEggSteps, isEggReadyToHatch } from '../core/egg'
import { checkEggEligibility, generateEgg, advanceEggSteps, isEggReadyToHatch, hatchEgg } from '../core/egg'
import type { BuddyData } from '../types'
import { generateCreature } from '../core/creature'
@@ -113,3 +113,48 @@ describe('isEggReadyToHatch', () => {
expect(isEggReadyToHatch(egg)).toBe(false)
})
})
describe('hatchEgg', () => {
test('creates a creature and removes egg', async () => {
const data = makeBuddyData()
const egg = { id: 'egg-1', obtainedAt: Date.now(), stepsRemaining: 0, totalSteps: 2000, speciesId: 'charmander' as const }
const result = await hatchEgg(data, egg)
expect(result.creature.speciesId).toBe('charmander')
expect(result.buddyData.creatures.length).toBe(data.creatures.length + 1)
expect(result.buddyData.eggs.length).toBe(0)
})
test('adds creature to party when slot available', async () => {
const data = makeBuddyData()
const egg = { id: 'egg-1', obtainedAt: Date.now(), stepsRemaining: 0, totalSteps: 2000, speciesId: 'pikachu' as const }
const result = await hatchEgg(data, egg)
const newCreature = result.creature
const inParty = result.buddyData.party.includes(newCreature.id)
expect(inParty).toBe(true)
})
test('increments totalEggsObtained', async () => {
const data = makeBuddyData()
const egg = { id: 'egg-1', obtainedAt: Date.now(), stepsRemaining: 0, totalSteps: 2000, speciesId: 'squirtle' as const }
const result = await hatchEgg(data, egg)
expect(result.buddyData.stats.totalEggsObtained).toBe(1)
})
test('updates dex entry with new species', async () => {
const data = makeBuddyData()
const egg = { id: 'egg-1', obtainedAt: Date.now(), stepsRemaining: 0, totalSteps: 2000, speciesId: 'charmander' as const }
const result = await hatchEgg(data, egg)
const entry = result.buddyData.dex.find(d => d.speciesId === 'charmander')
expect(entry).toBeDefined()
expect(entry!.caughtCount).toBe(1)
})
test('increments caughtCount for existing dex entry', async () => {
const data = makeBuddyData()
const egg = { id: 'egg-1', obtainedAt: Date.now(), stepsRemaining: 0, totalSteps: 2000, speciesId: 'bulbasaur' as const }
const result = await hatchEgg(data, egg)
const entry = result.buddyData.dex.find(d => d.speciesId === 'bulbasaur')
expect(entry!.caughtCount).toBe(2)
})
})

View File

@@ -1,5 +1,5 @@
import { describe, test, expect } from 'bun:test'
import { getSpeciesData, getAllSpeciesData, DEX_TO_SPECIES } from '../data/species'
import { getSpeciesData, getAllSpeciesData, DEX_TO_SPECIES, ensureSpeciesData } from '../data/species'
import { ALL_SPECIES_IDS } from '../types'
import type { SpeciesId } from '../types'
@@ -67,3 +67,29 @@ describe('DEX_TO_SPECIES', () => {
expect(DEX_TO_SPECIES[25]).toBe('pikachu')
})
})
describe('ensureSpeciesData', () => {
test('resolves without error', async () => {
await expect(ensureSpeciesData()).resolves.toBeUndefined()
})
})
describe('getSpeciesData - supplementary fields', () => {
test('has baseHappiness', () => {
expect(getSpeciesData('bulbasaur').baseHappiness).toBe(70)
})
test('pikachu has higher captureRate', () => {
expect(getSpeciesData('pikachu').captureRate).toBeGreaterThan(getSpeciesData('charmander').captureRate)
})
test('has names with en key', () => {
const data = getSpeciesData('charmander')
expect(data.names).toBeDefined()
expect(data.names.en).toBe('Charmander')
})
test('shinyChance is 1/4096', () => {
expect(getSpeciesData('bulbasaur').shinyChance).toBe(1 / 4096)
})
})