mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-10-25 14:50:30 +08:00
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:
@@ -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();
|
||||
},
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user