silphius/app/net/packet.test.js
2024-06-10 22:45:09 -05:00

31 lines
807 B
JavaScript

import {expect, test} from 'vitest';
import Packet from './packet.js';
class PackedPacket extends Packet {
static map = {
1: 'one',
2: 'two',
one: 1,
two: 2,
};
static pack(payload) {
return Object.fromEntries(Object.entries(payload).map(([k, v]) => [k, this.map[v]]));
}
static unpack(payload) {
return Object.fromEntries(Object.entries(payload).map(([k, v]) => [k, this.map[v]]));
}
}
test('packs and unpacks', async () => {
const payload = {foo: 'one', bar: 'two'};
const encoded = PackedPacket.encode(payload);
expect(Packet.decode(encoded))
.to.deep.equal(payload);
const packed = PackedPacket.pack(payload)
expect(packed)
.to.deep.equal({foo: 1, bar: 2});
expect(PackedPacket.unpack(packed))
.to.deep.equal({foo: 'one', bar: 'two'});
});