|
| 1 | +import { BinaryData } from '../BinaryData.js'; |
| 2 | +import { BinaryReader } from '../BinaryReader.js'; |
| 3 | +import { BinaryWriter } from '../BinaryWriter.js'; |
| 4 | +import { Prefab } from '../Prefab.js'; |
| 5 | +import { ComponentHash } from '../types/ComponentHash.js'; |
| 6 | + |
| 7 | +import { LogicVector3SenderComponent } from './LogicVector3SenderComponent.js'; |
| 8 | + |
| 9 | +const componentHash = ComponentHash.LogicVector3Sender; |
| 10 | +const componentName = 'LogicVector3Sender'; |
| 11 | +const componentVersion = 1; |
| 12 | +const componentProps = { |
| 13 | + updatedValue: 1337, |
| 14 | + changedExternally: 420, |
| 15 | + identifier: 69, |
| 16 | + value: { x: 69, y: 420, z: 1337 } |
| 17 | +}; |
| 18 | + |
| 19 | +describe('new LogicVector3SenderComponent()', () => { |
| 20 | + describe('when given the required props', () => { |
| 21 | + it('returns an instance of the LogicVector3SenderComponent class', () => { |
| 22 | + const component = new LogicVector3SenderComponent({ version: componentVersion }); |
| 23 | + |
| 24 | + expect(component).toBeInstanceOf(LogicVector3SenderComponent); |
| 25 | + expect(component.hash).toStrictEqual(componentHash); |
| 26 | + expect(component.name).toStrictEqual(componentName); |
| 27 | + expect(component.version).toStrictEqual(componentVersion); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + describe('when given additional props', () => { |
| 32 | + it('returns an instance of the LogicVector3SenderComponent class', () => { |
| 33 | + const component = new LogicVector3SenderComponent({ version: componentVersion, ...componentProps }); |
| 34 | + |
| 35 | + expect(component).toBeInstanceOf(LogicVector3SenderComponent); |
| 36 | + expect(component.hash).toStrictEqual(componentHash); |
| 37 | + expect(component.name).toStrictEqual(componentName); |
| 38 | + expect(component.version).toStrictEqual(componentVersion); |
| 39 | + expect(component.updatedValue).toStrictEqual(componentProps.updatedValue); |
| 40 | + expect(component.changedExternally).toStrictEqual(componentProps.changedExternally); |
| 41 | + expect(component.identifier).toStrictEqual(componentProps.identifier); |
| 42 | + expect(component.value).toStrictEqual(componentProps.value); |
| 43 | + }); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe('LogicVector3SenderComponent.fromBinary()', () => { |
| 48 | + let fastForwardedReader: BinaryReader; |
| 49 | + |
| 50 | + const prefab = new Prefab('MRK_Small_Lever', { |
| 51 | + components: { |
| 52 | + LogicVector3Sender: new LogicVector3SenderComponent({ version: componentVersion, ...componentProps }) |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + beforeEach(() => { |
| 57 | + const saveString = prefab.toSaveString(); |
| 58 | + const [dataString] = saveString.split('|'); |
| 59 | + |
| 60 | + if (typeof dataString === 'undefined') { |
| 61 | + throw new Error('Invalid test data.'); |
| 62 | + } |
| 63 | + |
| 64 | + const unsignedIntegers = dataString.split(',').map(Number); |
| 65 | + |
| 66 | + const data = BinaryData.fromUnsignedIntegerArray(unsignedIntegers); |
| 67 | + const reader = new BinaryReader(data.toBinaryString()); |
| 68 | + |
| 69 | + reader.readUnsignedInteger(); // Root prefab hash. |
| 70 | + reader.readUnsignedInteger(); // SaveString bytes. |
| 71 | + reader.readUnsignedInteger(); // Parent prefab hash. |
| 72 | + reader.readFloat(); // Parent position X. |
| 73 | + reader.readFloat(); // Parent position Y. |
| 74 | + reader.readFloat(); // Parent position Z. |
| 75 | + reader.readFloat(); // Parent rotation X. |
| 76 | + reader.readFloat(); // Parent rotation Y. |
| 77 | + reader.readFloat(); // Parent rotation Z. |
| 78 | + reader.readFloat(); // Parent rotation W. |
| 79 | + reader.readFloat(); // Parent scale. |
| 80 | + reader.readUnsignedInteger(); // LogicVector3SenderComponent hash. |
| 81 | + reader.readUnsignedInteger(); // LogicVector3SenderComponent data length. |
| 82 | + |
| 83 | + fastForwardedReader = reader; |
| 84 | + }); |
| 85 | + |
| 86 | + it('returns an instance of the LogicVector3SenderComponent class', () => { |
| 87 | + const component = LogicVector3SenderComponent.fromBinary(fastForwardedReader, componentVersion); |
| 88 | + |
| 89 | + expect(component).toBeInstanceOf(LogicVector3SenderComponent); |
| 90 | + expect(component.hash).toStrictEqual(componentHash); |
| 91 | + expect(component.name).toStrictEqual(componentName); |
| 92 | + expect(component.version).toStrictEqual(componentVersion); |
| 93 | + expect(component.updatedValue).toStrictEqual(componentProps.updatedValue); |
| 94 | + expect(component.changedExternally).toStrictEqual(componentProps.changedExternally); |
| 95 | + expect(component.identifier).toStrictEqual(componentProps.identifier); |
| 96 | + expect(component.value).toStrictEqual(componentProps.value); |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe('LogicVector3SenderComponent.toBinary()', () => { |
| 101 | + it('returns a BinaryString representation of the component', () => { |
| 102 | + const component = new LogicVector3SenderComponent({ version: componentVersion, ...componentProps }); |
| 103 | + |
| 104 | + const data = component.toBinary(); |
| 105 | + |
| 106 | + const expectedData = |
| 107 | + '000000000000000000000101001110010000000000000000000000011010010000000000000000000000000001000101010000101000101000000000000000000100001111010010000000000000000001000100101001110010000000000000'; |
| 108 | + |
| 109 | + expect(data).toStrictEqual(expectedData); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe('LogicVector3SenderComponent.write()', () => { |
| 114 | + it('writes a BinaryString representation of the component to the given BinaryWriter, including component hash and data length', () => { |
| 115 | + const component = new LogicVector3SenderComponent({ version: componentVersion, ...componentProps }); |
| 116 | + |
| 117 | + const writer = new BinaryWriter(); |
| 118 | + component.write(writer); |
| 119 | + |
| 120 | + const data = writer.flush(); |
| 121 | + |
| 122 | + const expectedData = |
| 123 | + '0111100011001101101001111011001100000000000000000000000011000000000000000000000000000101001110010000000000000000000000011010010000000000000000000000000001000101010000101000101000000000000000000100001111010010000000000000000001000100101001110010000000000000'; |
| 124 | + |
| 125 | + expect(data).toStrictEqual(expectedData); |
| 126 | + |
| 127 | + const reader = new BinaryReader(data); |
| 128 | + |
| 129 | + const hash = reader.readUnsignedInteger(); |
| 130 | + reader.readUnsignedInteger(); // LogicVector3SenderComponent data length. |
| 131 | + const updatedValue = reader.readUnsignedInteger(); |
| 132 | + const changedExternally = reader.readUnsignedInteger(); |
| 133 | + const identifier = reader.readUnsignedInteger(); |
| 134 | + const value = { |
| 135 | + x: reader.readFloat(), |
| 136 | + y: reader.readFloat(), |
| 137 | + z: reader.readFloat() |
| 138 | + }; |
| 139 | + |
| 140 | + expect(hash).toStrictEqual(componentHash); |
| 141 | + expect(updatedValue).toStrictEqual(componentProps.updatedValue); |
| 142 | + expect(changedExternally).toStrictEqual(componentProps.changedExternally); |
| 143 | + expect(identifier).toStrictEqual(componentProps.identifier); |
| 144 | + expect(value).toStrictEqual(componentProps.value); |
| 145 | + }); |
| 146 | +}); |
0 commit comments