Skip to content

Commit

Permalink
Merge pull request #2 from thetayloredman/test/state
Browse files Browse the repository at this point in the history
test: remove shared state usage
  • Loading branch information
thetayloredman authored Feb 18, 2023
2 parents 85a69da + 9f331df commit 2a8d7be
Showing 1 changed file with 49 additions and 18 deletions.
67 changes: 49 additions & 18 deletions src/tests/stream/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { kb, mb } from "../../lib/sizeHelpers";
import Stream, { StreamEvents } from "../../stream/";
import { ControlCharacters } from "../../stream/controlCharacters";

describe("Stream", () => {
/** Makes a new {@link Stream} and attaches listeners. */
function makeNewStream(): { stream: Stream; onRead: jest.Mock; onPacket: jest.Mock; onReset: jest.Mock } {
const stream = new Stream();

const onRead = jest.fn(() => undefined);
Expand All @@ -31,47 +32,44 @@ describe("Stream", () => {
stream.on(StreamEvents.Packet, onPacket);
stream.on(StreamEvents.ReadReset, onReset);

beforeEach(() => {
// Some tests will screw up the buffer by adding invalid data, and this is
// the only way for us to recover.

// @ts-expect-error: required reset to a private property
stream._buffer = Buffer.alloc(0);
// @ts-expect-error: required reset to a private property
stream._bufferSize = 0;
// @ts-expect-error: required reset to a private property
stream._packet = Buffer.alloc(0);
// @ts-expect-error: required reset to a private property
stream._packetSize = 0;

onRead.mockClear();
onPacket.mockClear();
onReset.mockClear();
});
return { stream, onRead, onPacket, onReset };
}

describe("Stream", () => {
it("errors when provided jumbled data", () => {
const stream = new Stream();

expect(() => stream.feed(Buffer.from([0xff]))).toThrow(
"I don't know what I am looking at! Encountered unknown control character 0xff in stream."
);
});

it("does nothing for KeepAlive", () => {
const { stream, onRead, onPacket, onReset } = makeNewStream();

stream.feed(Buffer.from([ControlCharacters.KeepAlive]));

expect(onRead).not.toHaveBeenCalled();
expect(onPacket).not.toHaveBeenCalled();
expect(onReset).not.toHaveBeenCalled();
});

it("single reset", () => {
const { stream, onRead, onPacket, onReset } = makeNewStream();

stream.feed(Buffer.from([ControlCharacters.ReadReset]));

expect(onRead).not.toHaveBeenCalled();
expect(onPacket).not.toHaveBeenCalled();
expect(onReset).toHaveBeenCalledTimes(1);
expect(onReset).toHaveBeenCalledWith();
});

it("multiple resets", () => {
const { stream, onRead, onPacket, onReset } = makeNewStream();

stream.feed(Buffer.from([ControlCharacters.ReadReset, ControlCharacters.ReadReset]));

expect(onRead).not.toHaveBeenCalled();
expect(onPacket).not.toHaveBeenCalled();
expect(onReset).toHaveBeenCalledTimes(2);
Expand All @@ -80,15 +78,21 @@ describe("Stream", () => {
});

it("empty packet", () => {
const { stream, onRead, onPacket, onReset } = makeNewStream();

stream.feed(Buffer.from([ControlCharacters.EndOfPacket]));

expect(onRead).not.toHaveBeenCalled();
expect(onPacket).toHaveBeenCalledTimes(1);
expect(onPacket).toHaveBeenCalledWith(0, Buffer.alloc(0));
expect(onReset).not.toHaveBeenCalled();
});

it("single byte packet", () => {
const { stream, onRead, onPacket, onReset } = makeNewStream();

stream.feed(Buffer.from([ControlCharacters.ReadByte, 0x01, ControlCharacters.EndOfPacket]));

expect(onRead).toHaveBeenCalledTimes(1);
expect(onRead).toHaveBeenCalledWith(ControlCharacters.ReadByte, 1, 1);
expect(onPacket).toHaveBeenCalledTimes(1);
Expand All @@ -97,18 +101,25 @@ describe("Stream", () => {
});

it("waits when read is incomplete", () => {
const { stream, onRead, onPacket, onReset } = makeNewStream();

stream.feed(Buffer.from([ControlCharacters.ReadByte]));

expect(onRead).not.toHaveBeenCalled();
expect(onPacket).not.toHaveBeenCalled();
expect(onReset).not.toHaveBeenCalled();

stream.feed(Buffer.from([0x01]));

expect(onRead).toHaveBeenCalledTimes(1);
expect(onRead).toHaveBeenCalledWith(ControlCharacters.ReadByte, 1, 1);
expect(onPacket).not.toHaveBeenCalled();
expect(onReset).not.toHaveBeenCalled();
});

it("longer read", () => {
const { stream, onRead, onPacket, onReset } = makeNewStream();

stream.feed(
Buffer.from([
ControlCharacters.ReadBytes,
Expand All @@ -128,6 +139,7 @@ describe("Stream", () => {
ControlCharacters.EndOfPacket
])
);

expect(onRead).toHaveBeenCalledTimes(1);
expect(onRead).toHaveBeenCalledWith(
ControlCharacters.ReadBytes,
Expand All @@ -140,11 +152,16 @@ describe("Stream", () => {
});

it("long read with missing size", () => {
const { stream, onRead, onPacket, onReset } = makeNewStream();

stream.feed(Buffer.from([ControlCharacters.ReadBytes]));

expect(onRead).not.toHaveBeenCalled();
expect(onPacket).not.toHaveBeenCalled();
expect(onReset).not.toHaveBeenCalled();

stream.feed(Buffer.from([0x2, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, ControlCharacters.EndOfPacket]));

expect(onRead).toHaveBeenCalledTimes(1);
expect(onRead).toHaveBeenCalledWith(
ControlCharacters.ReadBytes,
Expand All @@ -157,11 +174,16 @@ describe("Stream", () => {
});

it("long read interrupted in the middle", () => {
const { stream, onRead, onPacket, onReset } = makeNewStream();

stream.feed(Buffer.from([ControlCharacters.ReadBytes, 0x02, 0x00, 0x01, 0x02, 0x03, 0x04]));

expect(onRead).not.toHaveBeenCalled();
expect(onPacket).not.toHaveBeenCalled();
expect(onReset).not.toHaveBeenCalled();

stream.feed(Buffer.from([0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, ControlCharacters.EndOfPacket]));

expect(onRead).toHaveBeenCalledTimes(1);
expect(onRead).toHaveBeenCalledWith(
ControlCharacters.ReadBytes,
Expand All @@ -174,7 +196,10 @@ describe("Stream", () => {
});

it("Read Reset after a read will actually reset", () => {
const { stream, onRead, onPacket, onReset } = makeNewStream();

stream.feed(Buffer.from([ControlCharacters.ReadByte, 0x00, ControlCharacters.ReadReset, ControlCharacters.EndOfPacket]));

expect(onRead).toHaveBeenCalledTimes(1);
expect(onRead).toHaveBeenCalledWith(ControlCharacters.ReadByte, 1, 0);
expect(onPacket).toHaveBeenCalledTimes(1);
Expand All @@ -185,12 +210,16 @@ describe("Stream", () => {

describe("_bestControlCharacter", () => {
it("nothing => returns false", () => {
const stream = new Stream();

// @ts-expect-error: testing a private method
expect(stream._bestControlCharacter(0)).toBe(false);
});

function factory(name: string, size: number, result: ReturnType<Stream["_bestControlCharacter"]>): void {
it(name, () => {
const stream = new Stream();

// @ts-expect-error: testing a private method
expect(stream._bestControlCharacter(size)).toEqual(result);
});
Expand Down Expand Up @@ -229,6 +258,8 @@ describe("Stream", () => {
describe("encode", () => {
function factory(name: string, input: Buffer, output: Buffer): void {
it(name, () => {
const stream = new Stream();

expect(stream.encode(input)).toEqual(output);
});
}
Expand Down

0 comments on commit 2a8d7be

Please sign in to comment.