// #78 token panel seam: the vision controls' local rules and the exact `flags`/`vision_radius` // fields the panel sends. Pure, no DOM, no socket. `flags` is free-form (DESIGN 8), so an edit must // copy the stored object and change only `vision`; a new token keeps the `{}` default. import { describe, expect, it } from 'vitest' import { createVisionFields, parseVisionRadius, updateVisionFields, withVisionFlag, } from '../src/tokenPanel.ts' describe('vision radius', () => { it('treats blank as the scene default and accepts any finite positive number', () => { expect(parseVisionRadius('')).toEqual({ ok: true, value: null }) expect(parseVisionRadius(' ')).toEqual({ ok: true, value: null }) expect(parseVisionRadius('300')).toEqual({ ok: true, value: 300 }) expect(parseVisionRadius(' 12.5 ')).toEqual({ ok: true, value: 12.5 }) }) it('refuses zero, negative, non-finite and non-numeric values like the radius field', () => { for (const raw of ['0', '-1', 'abc', 'Infinity', 'NaN']) { const result = parseVisionRadius(raw) expect(result.ok).toBe(false) if (!result.ok) expect(result.error).toMatch(/positive number or blank/) } }) }) describe('create fields', () => { it('sends only the vision key when vision is on, plus the radius', () => { expect(createVisionFields(true, '300')).toEqual({ ok: true, fields: { flags: { vision: true }, vision_radius: 300 }, }) expect(createVisionFields(true, '')).toEqual({ ok: true, fields: { flags: { vision: true }, vision_radius: null }, }) }) it('sends no flags when vision is off, keeping the DESIGN 8 `{}` default', () => { expect(createVisionFields(false, '')).toEqual({ ok: true, fields: { vision_radius: null }, }) }) it('refuses a bad radius before the intent leaves the client', () => { const result = createVisionFields(true, '0') expect(result.ok).toBe(false) }) }) describe('update fields', () => { it('copies the stored flags and writes only vision, so other GM flags survive', () => { expect(updateVisionFields('abc', { blocks_move: true, note: 'x' }, false, '250')).toEqual({ ok: true, fields: { id: 'abc', flags: { blocks_move: true, note: 'x', vision: false }, vision_radius: 250, }, }) expect(withVisionFlag({ vision: true, other: 1 }, true)).toEqual({ vision: true, other: 1 }) }) it('clears the radius back to the scene default with null', () => { expect(updateVisionFields('abc', { vision: true }, true, '')).toEqual({ ok: true, fields: { id: 'abc', flags: { vision: true }, vision_radius: null }, }) }) it('refuses a non-positive radius on edit too', () => { const result = updateVisionFields('abc', {}, true, '-5') expect(result.ok).toBe(false) }) })