import { describe, expect, test } from 'bun:test'; import { DemoReader, EntityMode } from '../../src/index.js'; import { HttpBroadcastReader } from '../../src/broadcast/index.js'; import { resolveEntityMode, Storage } from '../../src/parser/entities/entityMode.js'; describe('EntityMode values', () => { test('are frozen plain objects that serialize', () => { for (const mode of [EntityMode.NONE, EntityMode.GAME_RULES, EntityMode.GAMEPLAY, EntityMode.ALL]) { expect(Object.isFrozen(mode)).toBe(true); expect(JSON.parse(JSON.stringify(mode))).toEqual({ ...mode }); } expect(Object.isFrozen(EntityMode)).toBe(true); const withSky = EntityMode.gameplayWith(['CEnvSky']); expect(withSky).toEqual({ kind: 'gameplay', include: ['CEnvSky'] }); expect(Object.isFrozen(withSky) && Object.isFrozen(withSky.include)).toBe(true); expect(structuredClone(withSky)).toEqual(withSky); }); test('gameplayWith copies its list and validates input', () => { const list = ['CEnvSky']; const mode = EntityMode.gameplayWith(list); list.push('CGradientFog'); expect(mode.include).toEqual(['CEnvSky']); const predicate = (name: string) => name.startsWith('CEnv'); expect(EntityMode.gameplayWith(predicate).include).toBe(predicate); expect(() => EntityMode.gameplayWith([1] as unknown as string[])).toThrow(TypeError); expect(() => EntityMode.gameplayWith('CEnvSky' as unknown as string[])).toThrow(TypeError); }); test('resolve to the internal storage and class filter', () => { expect(resolveEntityMode(undefined)).toEqual({ storage: Storage.NONE, classes: 'gameplay' }); expect(resolveEntityMode(EntityMode.NONE).storage).toBe(Storage.NONE); expect(resolveEntityMode(EntityMode.GAME_RULES).storage).toBe(Storage.GAME_RULES); expect(resolveEntityMode(EntityMode.GAMEPLAY)).toEqual({ storage: Storage.ENTITIES, classes: 'gameplay' }); expect(resolveEntityMode(EntityMode.ALL)).toEqual({ storage: Storage.ENTITIES, classes: 'all' }); expect(resolveEntityMode(EntityMode.gameplayWith(['CEnvSky'])).classes).toEqual(['CEnvSky']); // Hand-written or deserialized values work too. expect(resolveEntityMode({ kind: 'gameplay', include: ['CEnvSky'] }).classes).toEqual(['CEnvSky']); }); test('reject numeric v2 modes and malformed values', () => { expect(() => resolveEntityMode(1)).toThrow(/Numeric entity modes were removed in v3/); for (const bad of [null, 'ALL', {}, { kind: 'everything' }, { kind: 'gameplay', include: [1] }]) expect(() => resolveEntityMode(bad)).toThrow(TypeError); }); }); describe('reader options', () => { // Option shapes that throw a TypeError (and per-call options) are covered for both exports in // parse-result.test.ts. test('the constructor keeps the mode and validates synchronously', () => { expect(new DemoReader().entityMode).toBe(EntityMode.NONE); expect(new DemoReader({ entities: EntityMode.ALL }).entityMode).toBe(EntityMode.ALL); expect(new DemoReader({ smokeDensityThreshold: 9 }).smokeDensityThreshold).toBe(9); expect(() => new DemoReader({ smokeDensityThreshold: -1 })).toThrow(RangeError); expect(() => new DemoReader({ maxFrameBytes: 0 })).toThrow(RangeError); }); test('options are copied: later changes to the key do not affect the reader', () => { const key = new Uint8Array(16).fill(1); const reader = new DemoReader({ decryptionKey: key }); key.fill(2); expect(Array.from(reader._sessionOptions.decryptionKey!)).toEqual(Array(16).fill(1)); expect(Object.isFrozen(reader._sessionOptions)).toBe(true); }); test('broadcasts take only transport options', async () => { const parser = new DemoReader(); expect(() => new HttpBroadcastReader(parser, 'http://relay/', { entities: EntityMode.ALL } as never)).toThrow( /moved to the DemoReader constructor/ ); expect(() => new HttpBroadcastReader(parser, 'http://relay/', { svc_UserCmds: false } as never)).toThrow( TypeError ); await expect( parser.parseHttpBroadcast('http://relay/', { decryptionKey: new Uint8Array(16) } as never) ).rejects.toThrow(TypeError); expect(parser.hasEnded).toBe(false); }); });