Skip to content

Commit

Permalink
test: add first Shell test
Browse files Browse the repository at this point in the history
  • Loading branch information
thetayloredman committed Feb 20, 2023
1 parent d0eb2e1 commit b9010f3
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/tests/shell/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* node-subdata-2 - SubData 2 wrapper for Node.js
* Copyright (C) 2022 LogN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import Shell, { ShellEvents } from "../../shell";
import RawShellAlgorithm from "../../shell/algorithms/Raw";
import Stream from "../../stream";
import { ControlCharacters } from "../../stream/controlCharacters";
import DirectStream from "../../stream/DirectStream";
import ManualIOProvider from "../../stream/providers/ManualIOProvider";

function makeNewShell(): {
algorithm: RawShellAlgorithm;
provider: ManualIOProvider;
manual: ManualIOProvider["manual"];
stream: Stream;
shell: Shell;
remoteRx: jest.Mock;
localPacket: jest.Mock;
} {
const algorithm = new RawShellAlgorithm();
const provider = new ManualIOProvider();
const { manual } = provider;
const stream = new Stream(provider);
const shell = new Shell(algorithm, stream);

const remoteRx = jest.fn();
const localPacket = jest.fn();

manual.on("data", remoteRx);
shell.on(ShellEvents.Packet, localPacket);

return { algorithm, provider, manual, stream, shell, remoteRx, localPacket };
}

describe("Shell", () => {
it("simple transmit", () => {
const { shell, remoteRx, localPacket } = makeNewShell();
const direct = new DirectStream();
const data = Buffer.from("Hello, world!");
shell.send(data);
expect(remoteRx).toBeCalledTimes(1);
expect(remoteRx).toBeCalledWith(Buffer.concat([direct.encode(data), Buffer.from([ControlCharacters.EndOfPacket])]));
expect(localPacket).toBeCalledTimes(0);
});
});

0 comments on commit b9010f3

Please sign in to comment.