Initial commit: Text Adventure Game
Features: - Combat system with AP/EP hit calculation and three-layer defense - Auto-combat/farming mode - Item system with stacking support - Skill system with levels, milestones, and parent skill sync - Shop system with dynamic pricing - Inventory management with bulk selling - Event system - Game loop with offline earnings - Save/Load system Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
74
tests/helpers/random.js
Normal file
74
tests/helpers/random.js
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* 随机数测试辅助函数
|
||||
*/
|
||||
|
||||
import { vi } from 'vitest'
|
||||
|
||||
let mockRandomValue = 0.5
|
||||
|
||||
/**
|
||||
* 设置 Math.random() 返回值
|
||||
* @param {number} value - 返回值(0-1)
|
||||
*/
|
||||
export function setMockRandom(value) {
|
||||
mockRandomValue = value
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 Math.random() 的 mock
|
||||
* @returns {Object} spy
|
||||
*/
|
||||
export function mockMathRandom() {
|
||||
return vi.spyOn(Math, 'random').mockImplementation(() => mockRandomValue)
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置 mock 随机值
|
||||
*/
|
||||
export function resetMockRandom() {
|
||||
mockRandomValue = 0.5
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置随机数序列
|
||||
* @param {Array<number>} values - 随机数序列
|
||||
* @returns {Object} spy
|
||||
*/
|
||||
export function mockRandomSequence(values) {
|
||||
let index = 0
|
||||
return vi.spyOn(Math, 'random').mockImplementation(() => {
|
||||
const value = values[index % values.length]
|
||||
index++
|
||||
return value
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建确定性的随机数生成器
|
||||
* @param {number} seed - 种子值
|
||||
* @returns {Function} 随机函数
|
||||
*/
|
||||
export function createSeededRandom(seed) {
|
||||
return () => {
|
||||
seed = (seed * 9301 + 49297) % 233280
|
||||
return seed / 233280
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock 随机数生成器
|
||||
* @param {number} seed - 种子值
|
||||
* @returns {Object} spy
|
||||
*/
|
||||
export function mockSeededRandom(seed) {
|
||||
const randomFunc = createSeededRandom(seed)
|
||||
return vi.spyOn(Math, 'random').mockImplementation(randomFunc)
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理所有 mock
|
||||
*/
|
||||
export function cleanupMocks() {
|
||||
vi.restoreAllMocks()
|
||||
resetMockRandom()
|
||||
}
|
||||
Reference in New Issue
Block a user