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()
|
||||
}
|
||||
94
tests/helpers/store.js
Normal file
94
tests/helpers/store.js
Normal file
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* Store 测试辅助函数
|
||||
*/
|
||||
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { createMockPlayer } from '../fixtures/player.js'
|
||||
import { createMockGame } from '../fixtures/game.js'
|
||||
|
||||
/**
|
||||
* 设置测试用 Pinia Store
|
||||
* @returns {Object} { playerStore, gameStore, pinia }
|
||||
*/
|
||||
export function setupTestStore() {
|
||||
const pinia = createPinia()
|
||||
setActivePinia(pinia)
|
||||
|
||||
// 动态导入 stores
|
||||
const { usePlayerStore } = require('@/store/player.js')
|
||||
const { useGameStore } = require('@/store/game.js')
|
||||
|
||||
const playerStore = usePlayerStore()
|
||||
const gameStore = useGameStore()
|
||||
|
||||
// 初始化 mock 数据
|
||||
Object.assign(playerStore, createMockPlayer())
|
||||
Object.assign(gameStore, createMockGame())
|
||||
|
||||
return { playerStore, gameStore, pinia }
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建带有特定技能的 Store
|
||||
* @param {string} skillId - 技能ID
|
||||
* @param {number} level - 技能等级
|
||||
* @returns {Object} { playerStore, gameStore }
|
||||
*/
|
||||
export function setupStoreWithSkill(skillId, level = 1) {
|
||||
const { playerStore, gameStore, pinia } = setupTestStore()
|
||||
|
||||
playerStore.skills = {
|
||||
[skillId]: {
|
||||
level,
|
||||
exp: 0,
|
||||
unlocked: true,
|
||||
maxExp: (level + 1) * 100
|
||||
}
|
||||
}
|
||||
|
||||
return { playerStore, gameStore, pinia }
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建战斗中的 Store
|
||||
* @param {Object} enemy - 敌人对象
|
||||
* @returns {Object} { playerStore, gameStore }
|
||||
*/
|
||||
export function setupStoreInCombat(enemy) {
|
||||
const { playerStore, gameStore, pinia } = setupTestStore()
|
||||
|
||||
gameStore.inCombat = true
|
||||
gameStore.combatState = {
|
||||
enemyId: enemy.id,
|
||||
enemy,
|
||||
stance: 'balance',
|
||||
environment: 'normal',
|
||||
startTime: Date.now(),
|
||||
ticks: 0
|
||||
}
|
||||
|
||||
return { playerStore, gameStore, pinia }
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建带有物品的 Store
|
||||
* @param {Array} items - 物品数组
|
||||
* @returns {Object} { playerStore, gameStore }
|
||||
*/
|
||||
export function setupStoreWithItems(items) {
|
||||
const { playerStore, gameStore, pinia } = setupTestStore()
|
||||
|
||||
playerStore.inventory = [...items]
|
||||
|
||||
return { playerStore, gameStore, pinia }
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置 Store 状态
|
||||
* @param {Object} playerStore - 玩家Store
|
||||
* @param {Object} gameStore - 游戏Store
|
||||
*/
|
||||
export function resetStore(playerStore, gameStore) {
|
||||
Object.assign(playerStore, createMockPlayer())
|
||||
Object.assign(gameStore, createMockGame())
|
||||
}
|
||||
64
tests/helpers/timer.js
Normal file
64
tests/helpers/timer.js
Normal file
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* 时间相关测试辅助函数
|
||||
*/
|
||||
|
||||
import { vi } from 'vitest'
|
||||
|
||||
/**
|
||||
* Mock Date.now()
|
||||
* @param {number} timestamp - 时间戳
|
||||
* @returns {Object} spy
|
||||
*/
|
||||
export function mockDateNow(timestamp = Date.now()) {
|
||||
return vi.spyOn(Date, 'now').mockReturnValue(timestamp)
|
||||
}
|
||||
|
||||
/**
|
||||
* Mock setInterval/setTimeout
|
||||
* @returns {Object} { useFakeTimers, useRealTimers }
|
||||
*/
|
||||
export function mockTimers() {
|
||||
return {
|
||||
useFakeTimers: () => vi.useFakeTimers(),
|
||||
useRealTimers: () => vi.useRealTimers(),
|
||||
runAllTimers: () => vi.runAllTimers(),
|
||||
runOnlyPendingTimers: () => vi.runOnlyPendingTimers(),
|
||||
advanceTimersByTime: (ms) => vi.advanceTimersByTime(ms)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 等待指定时间
|
||||
* @param {number} ms - 毫秒
|
||||
* @returns {Promise}
|
||||
*/
|
||||
export function wait(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速时间(用于测试)
|
||||
* @param {number} seconds - 秒数
|
||||
* @returns {number} 毫秒
|
||||
*/
|
||||
export function seconds(seconds) {
|
||||
return seconds * 1000
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速时间(分钟)
|
||||
* @param {number} minutes - 分钟数
|
||||
* @returns {number} 毫秒
|
||||
*/
|
||||
export function minutes(minutes) {
|
||||
return minutes * 60 * 1000
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速时间(小时)
|
||||
* @param {number} hours - 小时数
|
||||
* @returns {number} 毫秒
|
||||
*/
|
||||
export function hours(hours) {
|
||||
return hours * 60 * 60 * 1000
|
||||
}
|
||||
Reference in New Issue
Block a user