import { describe, expect, it } from 'vitest' import { arith } from '../src/renderer/game/data/arith' describe('0.1.36 arith 纯解析器(CSP safe——零 Function/eval)', () => { it('基础四则与变量', () => { expect(arith('0.5 + 0.1*L', 3)).toBeCloseTo(0.8) expect(arith('10 * L', 2)).toBe(20) expect(arith('4 * L ^ 2', 3)).toBe(36) expect(arith('80 * 1.6^(L-1)', 3)).toBeCloseTo(204.8, 3) expect(arith('(1 + 2) * 3')).toBe(9) }) it('非法输入→NaN(不抛异常)', () => { expect(Number.isNaN(arith(''))).toBe(true) expect(Number.isNaN(arith('L + foo'))).toBe(true) expect(Number.isNaN(arith('1 + '))).toBe(true) expect(Number.isNaN(arith('alert(1)'))).toBe(true) expect(Number.isNaN(arith('0.5 + 0.1*L^', 3))).toBe(true) expect(Number.isNaN(arith('L/0 +1', 3))).toBe(true) }) it('负号/右结合幂/除零', () => { expect(arith('-3 + 5')).toBe(2) expect(arith('2 ^ 3 ^ 2')).toBe(512) expect(Number.isNaN(arith('1/0'))).toBe(true) }) it('与旧 bonusOf 语义一致(0.5+0.1*L @3→0.8)', () => { expect(arith('0.5 + 0.1*L', 3)).toBeCloseTo(0.8) expect(arith('0.35*L', 2)).toBeCloseTo(0.7) }) })