/ tests / support / support.ts
support.ts
 1  import type { Options } from "execa";
 2  
 3  import { execa } from "execa";
 4  import * as Crypto from "node:crypto";
 5  import { fileURLToPath } from "node:url";
 6  import * as Path from "node:path";
 7  import * as Fs from "node:fs/promises";
 8  
 9  // Generate string of 12 random characters with 8 bits of entropy.
10  export function randomTag(): string {
11    return Crypto.randomBytes(8).toString("hex");
12  }
13  
14  export function createOptions(repoFolder: string, days: number): Options {
15    return {
16      cwd: repoFolder,
17      // eslint-disable-next-line @typescript-eslint/naming-convention
18      env: { RAD_LOCAL_TIME: (1671211684 + days * 86400).toString() },
19    };
20  }
21  
22  const filename = fileURLToPath(import.meta.url);
23  export const supportDir = Path.dirname(filename);
24  export const tmpDir = Path.resolve(supportDir, "..", "./tmp");
25  export const fixturesDir = Path.resolve(supportDir, "..", "./fixtures");
26  const workspacePaths = [Path.join(tmpDir, "peers"), Path.join(tmpDir, "repos")];
27  
28  export const heartwoodRelease = await Fs.readFile(
29    `${supportDir}/heartwood-release`,
30    "utf8",
31  );
32  
33  export const radicleHttpdRelease = await Fs.readFile(
34    `${supportDir}/radicle-httpd-release`,
35    "utf8",
36  );
37  
38  // Assert that binaries are installed and are the correct version.
39  export async function assertBinariesInstalled(
40    binary: string,
41    expectedVersion: string,
42    expectedPath: string,
43  ): Promise<void> {
44    const { stdout: which } = await execa("which", [binary]);
45    if (Path.dirname(which) !== expectedPath) {
46      throw new Error(
47        `${binary} path doesn't match used ${binary} binary: ${expectedPath} !== ${which}`,
48      );
49    }
50    const { stdout: version } = await execa(binary, ["--version"]);
51    if (!version.includes(expectedVersion)) {
52      throw new Error(
53        `${binary} version ${version} does not satisfy ${expectedVersion}`,
54      );
55    }
56  }
57  
58  export async function removeWorkspace(): Promise<void> {
59    for (const path of workspacePaths) {
60      await Fs.rm(path, {
61        recursive: true,
62        force: true,
63      });
64    }
65  }