|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { SerializableConfig } from '../src/systems/node-configs/serializableConfig' |
| 3 | + |
| 4 | +@SerializableConfig.decorate |
| 5 | +export class TestConfig extends SerializableConfig<TestConfig> { |
| 6 | + foo? = 'string' |
| 7 | + bar? = 42 |
| 8 | + baz? = false |
| 9 | +} |
| 10 | + |
| 11 | +describe('SerializableConfig', () => { |
| 12 | + it('assumes default values if local values are undefined', () => { |
| 13 | + const config = new TestConfig() |
| 14 | + expect(config.foo).toBe('string') |
| 15 | + expect(config.bar).toBe(42) |
| 16 | + expect(config.baz).toBe(false) |
| 17 | + }) |
| 18 | + |
| 19 | + it('allows setting local values', () => { |
| 20 | + const config = new TestConfig() |
| 21 | + config.foo = 'baz' |
| 22 | + config.bar = 123 |
| 23 | + config.baz = true |
| 24 | + expect(config.foo).toBe('baz') |
| 25 | + expect(config.bar).toBe(123) |
| 26 | + expect(config.baz).toBe(true) |
| 27 | + }) |
| 28 | + |
| 29 | + it('does not include default values when serialized unless explicitly set', () => { |
| 30 | + const config = new TestConfig() |
| 31 | + expect(config.toJSON()).toEqual({}) |
| 32 | + config.makeDefault('foo') |
| 33 | + config.makeDefault('bar') |
| 34 | + config.makeDefault('baz') |
| 35 | + expect(config.toJSON()).toEqual({ foo: 'string', bar: 42, baz: false }) |
| 36 | + }) |
| 37 | + |
| 38 | + it('includes explicitly set values when serialized', () => { |
| 39 | + const config = new TestConfig() |
| 40 | + config.foo = 'baz' |
| 41 | + config.makeDefault('bar') |
| 42 | + expect(config.toJSON()).toEqual({ foo: 'baz', bar: 42 }) |
| 43 | + }) |
| 44 | + |
| 45 | + it('correctly inherits values from parent configs', () => { |
| 46 | + const parent = new TestConfig() |
| 47 | + parent.foo = 'parent' |
| 48 | + |
| 49 | + const child = new TestConfig() |
| 50 | + child.setParent(parent) |
| 51 | + child.setKeyInheritance('foo') |
| 52 | + |
| 53 | + expect(child.foo).toBe('parent') |
| 54 | + expect(child.toJSON()).toEqual({ __inheritedKeys__: ['foo'], foo: 'parent' }) |
| 55 | + |
| 56 | + child.foo = 'child' |
| 57 | + expect(child.foo).toBe('child') |
| 58 | + expect(child.toJSON()).toEqual({ __inheritedKeys__: ['foo'], foo: 'child' }) |
| 59 | + |
| 60 | + child.foo = undefined |
| 61 | + child.setKeyInheritance('foo', false) |
| 62 | + expect(child.foo).toBe('string') |
| 63 | + expect(child.toJSON()).toEqual({}) |
| 64 | + }) |
| 65 | + |
| 66 | + it('inherits values from parent configs recursively', () => { |
| 67 | + const grandparent = new TestConfig() |
| 68 | + grandparent.foo = 'grandparent' |
| 69 | + grandparent.bar = 69 // This should not be inherited |
| 70 | + |
| 71 | + const parent = new TestConfig() |
| 72 | + parent.setParent(grandparent) |
| 73 | + parent.setKeyInheritance('foo') |
| 74 | + |
| 75 | + const child = new TestConfig() |
| 76 | + child.setParent(parent) |
| 77 | + child.setKeyInheritance('foo') |
| 78 | + child.setKeyInheritance('bar') |
| 79 | + |
| 80 | + expect(child.foo).toBe('grandparent') |
| 81 | + expect(child.toJSON()).toEqual({ __inheritedKeys__: ['foo', 'bar'], foo: 'grandparent' }) |
| 82 | + parent.foo = 'parent' |
| 83 | + parent.bar = 420 |
| 84 | + expect(child.foo).toBe('parent') |
| 85 | + expect(child.toJSON()).toEqual({ |
| 86 | + __inheritedKeys__: ['foo', 'bar'], |
| 87 | + foo: 'parent', |
| 88 | + bar: 420, |
| 89 | + }) |
| 90 | + child.foo = 'child' |
| 91 | + expect(child.foo).toBe('child') |
| 92 | + expect(child.toJSON()).toEqual({ |
| 93 | + __inheritedKeys__: ['foo', 'bar'], |
| 94 | + foo: 'child', |
| 95 | + bar: 420, |
| 96 | + }) |
| 97 | + parent.foo = undefined |
| 98 | + parent.bar = undefined |
| 99 | + expect(child.foo).toBe('child') |
| 100 | + expect(child.toJSON()).toEqual({ __inheritedKeys__: ['foo', 'bar'], foo: 'child' }) |
| 101 | + }) |
| 102 | + |
| 103 | + it('parses JSON', () => { |
| 104 | + // Set values |
| 105 | + const config = new TestConfig().fromJSON({ foo: 'baz', bar: 123, baz: true }) |
| 106 | + expect(config.foo).toBe('baz') |
| 107 | + expect(config.bar).toBe(123) |
| 108 | + expect(config.baz).toBe(true) |
| 109 | + // Clear values if not in JSON |
| 110 | + config.fromJSON({ foo: 'baz' }) |
| 111 | + expect(config.foo).toBe('baz') |
| 112 | + expect(config.bar).toBe(42) |
| 113 | + expect(config.baz).toBe(false) |
| 114 | + // Partial JSON |
| 115 | + config.fromJSON({ foo: 'baz', bar: 123, baz: true }).fromJSON({}, true) |
| 116 | + expect(config.foo).toBe('baz') |
| 117 | + expect(config.bar).toBe(123) |
| 118 | + expect(config.baz).toBe(true) |
| 119 | + }) |
| 120 | + |
| 121 | + it('parses JSON correctly with inheritance', () => { |
| 122 | + const parent = new TestConfig() |
| 123 | + parent.foo = 'parent' |
| 124 | + parent.bar = 69 // This should not be inherited |
| 125 | + |
| 126 | + const child = new TestConfig() |
| 127 | + .setParent(parent) |
| 128 | + .fromJSON({ __inheritedKeys__: ['foo'], foo: 'child' }) |
| 129 | + expect(child.foo).toBe('child') |
| 130 | + expect(child.bar).toBe(42) |
| 131 | + expect(child.toJSON()).toEqual({ __inheritedKeys__: ['foo'], foo: 'child' }) |
| 132 | + // Exclude metadata |
| 133 | + expect(child.toJSON(false)).toEqual({ foo: 'child' }) |
| 134 | + }) |
| 135 | +}) |
0 commit comments