Skip to content

Commit

Permalink
Add basic server smoke test (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrudd2 committed Oct 26, 2023
1 parent 3bee8c9 commit 391f6ab
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
"devDependencies": {
"@craftamap/esbuild-plugin-html": "^0.6.1",
"@rehooks/component-size": "^1.0.2",
"@serialport/bindings-cpp": "^12.0.0",
"@types/cors": "^2.8.4",
"@types/express": "^4.16.1",
"@types/jest": "^29.0.0",
"@types/node": "^14.0.0",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@types/supertest": "^2.0.12",
"@types/w3c-web-serial": "^1.0.2",
"@types/ws": "8.0.0 - 8.5",
"@types/yargs": "^17.0.0",
Expand All @@ -60,6 +62,7 @@
"react-dom": "^18.0.0",
"rimraf": "^5.0.0",
"semver": "^7.5.2",
"supertest": "^6.3.3",
"ts-jest": "^29.0.0",
"typescript": "~5.0 || ~5.2.0"
},
Expand Down
31 changes: 31 additions & 0 deletions src/__tests__/server-smoke.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { startServer, waitForEbb } from '../server';
import type { Server } from 'http';
import request from 'supertest';

jest.mock("../serialport-serialport")
jest.mock("../ebb")
jest.mock("../server", () => {
const original = jest.requireActual("../server");
return {
...original,
waitForEbb: jest.fn(()=> 'fake-ebb-path'),
};
});

describe('Server Smoke Test', () => {
let server: Server;

beforeAll(async () => {
server = await startServer(0);
});

afterAll( () => {
server.close();
});

test('POST /cancel should return 200 OK', async () => {
const response = await request(server).post('/cancel');
expect(response.status).toBe(200);
});

});
11 changes: 6 additions & 5 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import { EBB } from "./ebb";
import { Device, PenMotion, Motion, Plan } from "./planning";
import { formatDuration } from "./util";
import { autoDetect } from '@serialport/bindings-cpp';
import * as self from './server' // for mocking

export function startServer(port: number, device: string | null = null, enableCors = false, maxPayloadSize = "200mb") {
export function startServer(port: number, device: string | null = null, enableCors = false, maxPayloadSize = "200mb"): Promise<http.Server> {
const app = express();

app.use("/", express.static(path.join(__dirname, "..", "ui")));
Expand Down Expand Up @@ -222,7 +223,7 @@ export function startServer(port: number, device: string | null = null, enableCo
await plotter.postPlot();
}

return new Promise((resolve) => {
return new Promise<http.Server>((resolve) => {
server.listen(port, () => {
async function connect() {
for await (const d of ebbs(device)) {
Expand Down Expand Up @@ -259,8 +260,8 @@ async function listEBBs() {
return ports.filter(isEBB).map((p: { path: any; }) => p.path);
}

async function waitForEbb() {
// eslint-disable-next-line no-constant-condition
export async function waitForEbb() {
// eslint-disable-next-line no-constant-condition
while (true) {
const ebbs = await listEBBs();
if (ebbs.length) {
Expand All @@ -273,7 +274,7 @@ async function waitForEbb() {
async function* ebbs(path?: string) {
while (true) {
try {
const com = path || (await waitForEbb());
const com = path || (await self.waitForEbb()); // use self-import for mocking
console.log(`Found EBB at ${com}`);
const port = await tryOpen(com);
const closed = new Promise((resolve) => {
Expand Down

0 comments on commit 391f6ab

Please sign in to comment.