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 <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez
2023-07-12 10:58:45 +02:00
parent 92f19d0ca4
commit 3d4e1bbfd9
2 changed files with 56 additions and 0 deletions
+32
View File
@@ -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<TestFixtures>({
// 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();
},
});
+24
View File
@@ -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<string> {
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;
}