From 3d4e1bbfd977a45e26ae35e0cf8cd27581b40e6f Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Wed, 12 Jul 2023 10:48:57 +0200 Subject: [PATCH] Add httpd test fixture to vitest httpd-api tests This fixture is simil to the way Playwright handles fixtures, which are being setup and teardown for each test. Signed-off-by: Sebastian Martinez --- httpd-client/tests/support/fixtures.ts | 32 ++++++++++++++++++++++++++ httpd-client/tests/support/httpd.ts | 24 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 httpd-client/tests/support/fixtures.ts create mode 100644 httpd-client/tests/support/httpd.ts diff --git a/httpd-client/tests/support/fixtures.ts b/httpd-client/tests/support/fixtures.ts new file mode 100644 index 000000000..8d0dcee9a --- /dev/null +++ b/httpd-client/tests/support/fixtures.ts @@ -0,0 +1,32 @@ +import * as FsSync from "node:fs"; +import * as Path from "node:path"; +import { test } from "vitest"; + +import { HttpdClient } from "@httpd-client"; +import { RadiclePeer, createPeerManager } from "@tests/support/peerManager.js"; +import { gitOptions } from "@tests/support/fixtures.js"; +import { tmpDir } from "@tests/support/support.js"; + +interface TestFixtures { + httpd: { api: HttpdClient; peer: RadiclePeer }; +} + +export const testFixture = test.extend({ + // eslint-disable-next-line no-empty-pattern + httpd: async ({}, use) => { + const peerManager = await createPeerManager({ + dataDir: Path.resolve(Path.join(tmpDir, "peers")), + outputLog: FsSync.createWriteStream( + Path.join(tmpDir, "peerManager.log"), + ).setMaxListeners(16), + }); + const peer = await peerManager.startPeer({ + name: "palm", + gitOptions: gitOptions["alice"], + }); + await peer.startHttpd(); + const api = new HttpdClient(peer.httpdBaseUrl); + await use({ api, peer }); + await peer.stopHttpd(); + }, +}); diff --git a/httpd-client/tests/support/httpd.ts b/httpd-client/tests/support/httpd.ts new file mode 100644 index 000000000..cd21f294b --- /dev/null +++ b/httpd-client/tests/support/httpd.ts @@ -0,0 +1,24 @@ +import type { HttpdClient } from "@httpd-client"; +import type { RadiclePeer } from "@tests/support/peerManager.js"; + +import { sessionPayloadSchema } from "@httpd-client/lib/session.js"; + +export async function authenticate( + api: HttpdClient, + peer: RadiclePeer, +): Promise { + const { stdout } = await peer.rad(["web", "--backend", api.url, "--json"]); + const session = sessionPayloadSchema.safeParse(JSON.parse(stdout)); + + if (!session.success) { + throw new Error("Failed to parse session payload"); + } + + const { sessionId, signature, publicKey } = session.data; + await api.session.update(sessionId, { + sig: signature, + pk: publicKey, + }); + + return sessionId; +}