diff --git a/httpd-client/tests/support/globalSetup.ts b/httpd-client/tests/support/globalSetup.ts new file mode 100644 index 000000000..aea9f2283 --- /dev/null +++ b/httpd-client/tests/support/globalSetup.ts @@ -0,0 +1,59 @@ +import * as FsSync from "node:fs"; +import * as Path from "node:path"; +import { + assertRadInstalled, + promptWorkspaceRemoval, + removeWorkspace, + tmpDir, +} from "@tests/support/support.js"; +import { createPeerManager } from "@tests/support/peerManager"; +import { + createSourceBrowsingFixture, + gitOptions, + startPalmHttpd, +} from "@tests/support/fixtures"; +import { killAllProcesses } from "@tests/support/process"; + +export default async function globalSetup(): Promise<() => void> { + try { + await assertRadInstalled(); + } catch (error) { + console.error(error); + console.log(""); + console.log("To download the required test binaries, run:"); + console.log(" 👉 ./scripts/install-binaries"); + console.log(""); + process.exit(1); + } + + if (!process.env.SKIP_FIXTURE_CREATION) { + console.log("Setting up global test environment"); + if (!process.env.CI) { + await promptWorkspaceRemoval(); + } + await removeWorkspace(); + + const peerManager = await createPeerManager({ + dataDir: Path.resolve(Path.join(tmpDir, "peers")), + outputLog: FsSync.createWriteStream( + Path.join(tmpDir, "peerManager.log"), + ).setMaxListeners(16), + }); + + const palm = await peerManager.startPeer({ + name: "palm", + gitOptions: gitOptions["alice"], + }); + await palm.startHttpd(8080); + await palm.startNode({ trackingPolicy: "track", trackingScope: "all" }); + + console.log("Creating source-browsing fixture"); + await createSourceBrowsingFixture(peerManager, palm); + console.log("Running tests"); + await palm.stopNode(); + } else { + await startPalmHttpd(); + } + + return () => killAllProcesses(); +} diff --git a/httpd-client/vite.config.ts b/httpd-client/vite.config.ts index e0ab4a109..11011d175 100644 --- a/httpd-client/vite.config.ts +++ b/httpd-client/vite.config.ts @@ -6,7 +6,7 @@ export default defineConfig({ environment: "node", include: ["httpd-client/tests/*.test.ts"], reporters: "verbose", - globalSetup: "./tests/support/globalSetup", + globalSetup: "./httpd-client/tests/support/globalSetup", }, resolve: { alias: { diff --git a/tests/support/globalSetup.ts b/tests/support/globalSetup.ts index 52b701bfd..053d02148 100644 --- a/tests/support/globalSetup.ts +++ b/tests/support/globalSetup.ts @@ -1,11 +1,11 @@ -import type { FullConfig } from "@playwright/test"; - -import * as Fs from "node:fs/promises"; import * as FsSync from "node:fs"; import * as Path from "node:path"; -import * as readline from "node:readline/promises"; -import { execa } from "execa"; - +import { + assertRadInstalled, + promptWorkspaceRemoval, + removeWorkspace, + tmpDir, +} from "@tests/support/support.js"; import { createCobsFixture, createMarkdownFixture, @@ -14,14 +14,9 @@ import { startPalmHttpd, } from "@tests/support/fixtures.js"; import { createPeerManager } from "@tests/support/peerManager.js"; -import { heartwoodShortSha, tmpDir } from "@tests/support/support.js"; import { killAllProcesses } from "@tests/support/process.js"; -const workspacePaths = [Path.join(tmpDir, "peers"), Path.join(tmpDir, "repos")]; - -export default async function globalSetup( - _config: FullConfig, -): Promise<() => void> { +export default async function globalSetup(): Promise<() => void> { try { await assertRadInstalled(); } catch (error) { @@ -71,48 +66,3 @@ export default async function globalSetup( return () => killAllProcesses(); } - -// Assert that the `rad` CLI is installed and has the correct version. -async function assertRadInstalled(): Promise { - const { stdout: version } = await execa("rad", ["--version"]); - if (!version.includes(heartwoodShortSha)) { - throw new Error( - `rad version ${version} does not satisfy ${heartwoodShortSha}`, - ); - } -} - -async function promptWorkspaceRemoval(): Promise { - console.log(""); - console.log("This will irrevocably destroy the following directories:"); - console.log(""); - workspacePaths.forEach(path => console.log(path)); - console.log(""); - - const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout, - }); - - const result = await rl.question( - "Are you sure you want to continue? [yes/no]: ", - ); - rl.close(); - - if (result.toLowerCase() === "yes") { - console.log("Done"); - return; - } - - console.log("Ok, I won't touch your data."); - process.exit(1); -} - -async function removeWorkspace(): Promise { - for (const path of workspacePaths) { - await Fs.rm(path, { - recursive: true, - force: true, - }); - } -} diff --git a/tests/support/support.ts b/tests/support/support.ts index 85dd28bf2..7e2ef610f 100644 --- a/tests/support/support.ts +++ b/tests/support/support.ts @@ -1,8 +1,11 @@ import type { Options } from "execa"; + +import { execa } from "execa"; import * as Crypto from "node:crypto"; import { fileURLToPath } from "node:url"; import * as Path from "node:path"; import * as Fs from "node:fs/promises"; +import * as readline from "node:readline/promises"; // Generate string of 12 random characters with 8 bits of entropy. export function randomTag(): string { @@ -21,6 +24,7 @@ const filename = fileURLToPath(import.meta.url); export const supportDir = Path.dirname(filename); export const tmpDir = Path.resolve(supportDir, "..", "./tmp"); export const fixturesDir = Path.resolve(supportDir, "..", "./fixtures"); +const workspacePaths = [Path.join(tmpDir, "peers"), Path.join(tmpDir, "repos")]; export const heartwoodShortSha = ( await Fs.readFile(`${supportDir}/heartwood-version`, "utf8") @@ -30,3 +34,48 @@ process.env.PATH = [ Path.join(tmpDir, "bin", heartwoodShortSha), process.env.PATH, ].join(Path.delimiter); + +// Assert that the `rad` CLI is installed and has the correct version. +export async function assertRadInstalled(): Promise { + const { stdout: version } = await execa("rad", ["--version"]); + if (!version.includes(heartwoodShortSha)) { + throw new Error( + `rad version ${version} does not satisfy ${heartwoodShortSha}`, + ); + } +} + +export async function promptWorkspaceRemoval(): Promise { + console.log(""); + console.log("This will irrevocably destroy the following directories:"); + console.log(""); + workspacePaths.forEach(path => console.log(path)); + console.log(""); + + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + }); + + const result = await rl.question( + "Are you sure you want to continue? [yes/no]: ", + ); + rl.close(); + + if (result.toLowerCase() === "yes") { + console.log("Done"); + return; + } + + console.log("Ok, I won't touch your data."); + process.exit(1); +} + +export async function removeWorkspace(): Promise { + for (const path of workspacePaths) { + await Fs.rm(path, { + recursive: true, + force: true, + }); + } +}