mirror of
https://github.com/demostf/demo.js
synced 2026-06-04 00:54:14 +02:00
add tests for all entities in demo
This commit is contained in:
parent
450300c1b0
commit
ce6dec06ac
8 changed files with 218 additions and 2 deletions
BIN
src/tests/data/pov2_entities.json.gz
Normal file
BIN
src/tests/data/pov2_entities.json.gz
Normal file
Binary file not shown.
0
src/tests/data/snakewater_entities.json
Normal file
0
src/tests/data/snakewater_entities.json
Normal file
BIN
src/tests/data/snakewater_entities.json.gz
Normal file
BIN
src/tests/data/snakewater_entities.json.gz
Normal file
Binary file not shown.
114
src/tests/snapshot/entities.ts
Normal file
114
src/tests/snapshot/entities.ts
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
import * as assert from 'assert';
|
||||
import {readFileSync, createWriteStream, createReadStream} from 'fs';
|
||||
import {Demo} from '../../Demo';
|
||||
import {Packet} from '../../Data/Packet';
|
||||
import {BitStream} from 'bit-buffer';
|
||||
import * as split2 from 'split2';
|
||||
import {createUnzip, createGunzip} from 'zlib';
|
||||
import {PassThrough} from 'stream';
|
||||
|
||||
function writeEntities(name: string) {
|
||||
const targetFile = `${__dirname}/../data/${name}_entities.json`;
|
||||
const source = readFileSync(`${__dirname}/../data/${name}.dem`);
|
||||
const demo = Demo.fromNodeBuffer(source);
|
||||
const parser = demo.getParser(false);
|
||||
parser.readHeader();
|
||||
const match = parser.match;
|
||||
|
||||
const writeStream = createWriteStream(targetFile, 'utf8');
|
||||
|
||||
let index = 0;
|
||||
|
||||
const raw = new PassThrough();
|
||||
raw.pipe(createGunzip()).pipe(writeStream);
|
||||
|
||||
parser.on('packet', (packet: Packet) => {
|
||||
if (packet.packetType === 'packetEntities') {
|
||||
for (const entity of packet.entities) {
|
||||
const entityProps = {};
|
||||
for (const prop of entity.props) {
|
||||
entityProps[`${prop.definition.name}`] = prop.value;
|
||||
}
|
||||
raw.write(JSON.stringify({
|
||||
tick: match.tick,
|
||||
serverClass: entity.serverClass.name,
|
||||
id: entity.entityIndex,
|
||||
props: entityProps,
|
||||
pvs: entity.pvs
|
||||
}) + '\n');
|
||||
index++;
|
||||
}
|
||||
}
|
||||
});
|
||||
parser.parseBody();
|
||||
|
||||
raw.end();
|
||||
}
|
||||
|
||||
function testEntities(name: string, entityCount: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const targetFile = `${__dirname}/../data/${name}_entities.json.gz`;
|
||||
const source = readFileSync(`${__dirname}/../data/${name}.dem`);
|
||||
const demo = Demo.fromNodeBuffer(source);
|
||||
const parser = demo.getParser(false);
|
||||
parser.readHeader();
|
||||
const match = parser.match;
|
||||
|
||||
const resultData: any[] = [];
|
||||
parser.on('packet', (packet: Packet) => {
|
||||
if (packet.packetType === 'packetEntities') {
|
||||
for (const entity of packet.entities) {
|
||||
const entityProps = {};
|
||||
for (const prop of entity.props) {
|
||||
entityProps[`${prop.definition.name}`] = prop.value;
|
||||
}
|
||||
resultData.push({
|
||||
tick: match.tick,
|
||||
serverClass: entity.serverClass.name,
|
||||
id: entity.entityIndex,
|
||||
props: entityProps,
|
||||
pvs: entity.pvs
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function parseEntities() {
|
||||
const message = parser.tick();
|
||||
if (message && resultData.length === 0) {
|
||||
parseEntities();
|
||||
}
|
||||
}
|
||||
|
||||
const readStream = createReadStream(targetFile);
|
||||
|
||||
let parsed = 0;
|
||||
|
||||
readStream
|
||||
.pipe(createUnzip())
|
||||
.pipe(split2(JSON.parse)).on('data', (data) => {
|
||||
if (resultData.length < 1) {
|
||||
parseEntities();
|
||||
}
|
||||
const result = resultData.shift();
|
||||
assert.deepEqual(data, result);
|
||||
parsed++;
|
||||
}).on('end', () => {
|
||||
assert.equal(resultData.length, 0, 'Entities left to be checked');
|
||||
assert.equal(parsed, entityCount, 'unexpected number of entities');
|
||||
|
||||
resolve();
|
||||
}).on('error', err => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
suite('Parse all demo entities', () => {
|
||||
test('Parse snakewater.dem', () => {
|
||||
return testEntities('snakewater', 578869);
|
||||
});
|
||||
test('Parse post MyM POV demo', () => {
|
||||
return testEntities('pov2', 1066826);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,11 +1,13 @@
|
|||
import * as assert from 'assert';
|
||||
import {readFileSync} from 'fs';
|
||||
import {Demo} from '../../Demo';
|
||||
import {Packet} from '../../Data/Packet';
|
||||
import {BitStream} from 'bit-buffer';
|
||||
|
||||
function testDemo(name: string, fastMode: boolean = false) {
|
||||
const target = JSON.parse(readFileSync(`${__dirname}/../data/${name}.json`, 'utf8'));
|
||||
const source = readFileSync(`${__dirname}/../data/${name}.dem`);
|
||||
const demo = Demo.fromNodeBuffer(source);
|
||||
const demo = Demo.fromNodeBuffer(source);
|
||||
const parser = demo.getParser(fastMode);
|
||||
parser.readHeader();
|
||||
parser.parseBody();
|
||||
|
|
@ -13,6 +15,32 @@ function testDemo(name: string, fastMode: boolean = false) {
|
|||
assert.deepEqual(JSON.parse(JSON.stringify(parsed)), target);
|
||||
}
|
||||
|
||||
function testPackets() {
|
||||
const target = JSON.parse(readFileSync(`${__dirname}/../data/${name}_packets.json`, 'utf8'));
|
||||
const source = readFileSync(`${__dirname}/../data/${name}.dem`);
|
||||
const demo = Demo.fromNodeBuffer(source);
|
||||
const parser = demo.getParser(false);
|
||||
parser.readHeader();
|
||||
const match = parser.match;
|
||||
|
||||
const packets: {[tick: string]: Partial<Packet>[]} = {};
|
||||
|
||||
parser.on('packet', (packet: Packet) => {
|
||||
if (!packets[match.tick]) {
|
||||
packets[match.tick] = [];
|
||||
}
|
||||
const packetData = {};
|
||||
for (const key in packet) {
|
||||
if (packet.hasOwnProperty(key) && !(packet[key] instanceof BitStream)) {
|
||||
packetData[key] = packet[key];
|
||||
}
|
||||
}
|
||||
packets[match.tick].push(packetData);
|
||||
});
|
||||
|
||||
assert.deepEqual(JSON.parse(JSON.stringify(packets)), target);
|
||||
}
|
||||
|
||||
suite('Parse basic demo info', () => {
|
||||
test('Fast mode', () => {
|
||||
testDemo('snakewater', true);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue