/** * Node smoke tests for sim layer (no Cocos runtime). * Run: node tests/sim-world.test.mjs */ import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { fileURLToPath } from 'node:url' import { dirname, join } from 'node:path' const root = join(dirname(fileURLToPath(import.meta.url)), '..') const city = JSON.parse(readFileSync(join(root, 'assets/data/city-prototype.json'), 'utf8')) const economy = JSON.parse(readFileSync(join(root, 'assets/data/economy.json'), 'utf8')) function formatClock(minutes) { const m = ((minutes % 1440) + 1440) % 1440 const h = Math.floor(m / 60) const mm = m % 60 return `${String(h).padStart(2, '0')}:${String(mm).padStart(2, '0')}` } function advanceMinutes(current, day, delta) { let minutes = current + delta let d = day while (minutes >= 1440) { minutes -= 1440 d += 1 } return { minutes, day: d } } function applyAction(stats, action) { if (!action) return { ok: false, stats, message: '未知互动' } if (action.cashDelta < 0 && stats.cash + action.cashDelta < 0) { return { ok: false, stats, message: '现金不足' } } if (action.energyDelta < 0 && stats.energy + action.energyDelta < 0) { return { ok: false, stats, message: '精力不足' } } let next = { ...stats, cash: Math.max(0, Math.round(stats.cash + action.cashDelta)), energy: Math.min( stats.maxEnergy, Math.max(0, Math.round(stats.energy + action.energyDelta)), ), } const t = advanceMinutes(next.minutes, next.day, action.minutesDelta) next = { ...next, minutes: t.minutes, day: t.day } return { ok: true, stats: next, message: action.message } } function nearest(x, z) { let best = null let bestD = Infinity for (const it of city.interactables) { const d = Math.hypot(it.x - x, it.z - z) if (d <= it.radius && d < bestD) { best = it bestD = d } } return best } // --- assertions --- assert.equal(city.structures.length >= 6, true, 'city has prototype buildings') assert.ok(city.interactables.find((i) => i.action === 'work_remote')) assert.ok(economy.actions.sleep) let stats = { cash: economy.startCash, energy: economy.startEnergy, maxEnergy: economy.maxEnergy, minutes: 7 * 60 + 10, day: 1, } const nearBed = nearest(-9.2, -8.2) assert.equal(nearBed?.action, 'sleep') const slept = applyAction(stats, economy.actions.sleep) assert.equal(slept.ok, true) assert.ok(slept.stats.energy > stats.energy) stats = slept.stats const broke = applyAction( { ...stats, cash: 10 }, economy.actions.shop, ) assert.equal(broke.ok, false) const work = applyAction(stats, economy.actions.work_remote) assert.equal(work.ok, true) assert.ok(work.stats.cash > stats.cash) assert.equal(formatClock(0), '00:00') assert.equal(formatClock(7 * 60 + 10), '07:10') const dayRoll = advanceMinutes(23 * 60, 1, 120) assert.equal(dayRoll.day, 2) console.log('sim-world.test.mjs OK') console.log( JSON.stringify( { structures: city.structures.map((s) => s.id), cashAfterWork: work.stats.cash, clock: formatClock(work.stats.minutes), }, null, 2, ), )