From 50c73c0af3fc8de27c4a5e0287bef40649578e25 Mon Sep 17 00:00:00 2001 From: nprimo Date: Wed, 2 Nov 2022 18:04:11 +0000 Subject: [PATCH] feat(friend-support): add draft test - currently working only with `guests` provided directory in public/js/tests dir -> test must be able to get `guests` dir from student repo - need to check if there are more cases to test --- js/tests/friend-support_test.mjs | 88 ++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 js/tests/friend-support_test.mjs diff --git a/js/tests/friend-support_test.mjs b/js/tests/friend-support_test.mjs new file mode 100644 index 000000000..a5492ae92 --- /dev/null +++ b/js/tests/friend-support_test.mjs @@ -0,0 +1,88 @@ +import { once } from 'node:events' +import * as cp from 'node:child_process' +import fs from 'node:fs/promises' + +export const tests = [] +const fetch = _fetch // to redefine the real fetch + +const port = 5000 + +// tell-me-who_test setup -- see how to have same files in testing directories +// TO BE IMPLEMENTED + +const sendRequest = async (path, options) => { + const response = await fetch(`http://localhost:${port}${path}`, options) + const { status, statusText, ok } = response + const headers = Object.fromEntries(response.headers) + let body = '' + try { + body = await response.json() + } catch (err) { + body = err + } + return { status, body, headers } +} + +tests.push(async ({ path, ctx }) => { + ctx.server = cp.spawn('node', [`${path}`]) + const message = await Promise.race([ + once(ctx.server.stdout, 'data'), + Promise.race([ + once(ctx.server.stderr, 'data').then(String).then(Error), + once(ctx.server, 'error'), + ]).then(x => Promise.reject(x)), + ]) + return message[0].toString().includes(port) +}) + +tests.push(async ({ eq }) => { + const { status, body, headers } = await sendRequest('/Zoe_Sierra', { + method: 'GET', + }) + let cont = await fs.readFile('./guests/Zoe_Sierra.json', { + encoding: 'utf-8', + }) + return eq( + { + status: status, + body: body, + contentType: headers['content-type'], + }, + { + status: 200, + body: JSON.parse(cont), + contentType: 'application/json', + }, + ) +}) + +tests.push(async ({ eq }) => { + const { status, body, headers } = await sendRequest('/Zoe_Sierra1', { + method: 'GET', + }) + console.log(body.error) + return eq( + { + status: status, + body: body.error.trim(), + contentType: headers['content-type'], + }, + { + status: 404, + body: "guest not found", + contentType: 'application/json', + }, + ) + }) + +// this test must always run to shutdown server +/* currently breaking if one test fails - it might not be a problem +since the tests are run in a separate Docker image... +*/ +tests.push(({ ctx }) => { + const { server } = ctx + server.kill('SIGQUIT') + return true +}) + +Object.freeze(tests)