/ tests / support / globalSetup.ts
globalSetup.ts
  1  import * as Fs from "node:fs";
  2  import * as Path from "node:path";
  3  import {
  4    assertBinariesInstalled,
  5    heartwoodRelease,
  6    radicleHttpdRelease,
  7    removeWorkspace,
  8    tmpDir,
  9  } from "@tests/support/support.js";
 10  import {
 11    defaultConfig,
 12    createCobsFixture,
 13    createMarkdownFixture,
 14    createSourceBrowsingFixture,
 15    defaultHttpdPort,
 16    gitOptions,
 17  } from "@tests/support/fixtures.js";
 18  import { createPeerManager } from "@tests/support/peerManager.js";
 19  
 20  const heartwoodBinaryPath = Path.join(
 21    tmpDir,
 22    "bin",
 23    "heartwood",
 24    heartwoodRelease,
 25  );
 26  const httpdBinaryPath = Path.join(tmpDir, "bin", "httpd", radicleHttpdRelease);
 27  
 28  process.env.PATH = [
 29    heartwoodBinaryPath,
 30    httpdBinaryPath,
 31    process.env.PATH,
 32  ].join(Path.delimiter);
 33  
 34  export default async function globalSetup(): Promise<() => void> {
 35    try {
 36      await assertBinariesInstalled("rad", heartwoodRelease, heartwoodBinaryPath);
 37      await assertBinariesInstalled(
 38        "radicle-httpd",
 39        radicleHttpdRelease,
 40        httpdBinaryPath,
 41      );
 42    } catch (error) {
 43      console.error(error);
 44      console.log("");
 45      console.log("To download the required test binaries, run:");
 46      console.log(" 👉 ./scripts/install-binaries");
 47      console.log("");
 48      process.exit(1);
 49    }
 50  
 51    if (!process.env.SKIP_FIXTURE_CREATION) {
 52      console.log(
 53        "Recreating static fixtures. Set SKIP_FIXTURE_CREATION to skip this",
 54      );
 55      await removeWorkspace();
 56    }
 57  
 58    const peerManager = await createPeerManager({
 59      dataDir: Path.resolve(tmpDir, "peers"),
 60      outputLog: Fs.createWriteStream(
 61        Path.resolve(tmpDir, "globalPeerManager.log"),
 62      )
 63        // Workaround for fixing MaxListenersExceededWarning.
 64        // Since every prefixOutput stream adds stream listeners that don't autoClose.
 65        // TODO: We still seem to have some descriptors left open when running vitest, which we should handle.
 66        .setMaxListeners(16),
 67    });
 68  
 69    const palm = await peerManager.createPeer({
 70      name: "palm",
 71      gitOptions: gitOptions["alice"],
 72    });
 73  
 74    if (!process.env.SKIP_FIXTURE_CREATION) {
 75      await palm.startNode({
 76        web: {
 77          pinned: {
 78            repositories: ["rad:z4BwwjPCFNVP27FwVbDFgwVwkjcir"],
 79          },
 80          description: `:seedling: Radicle is an open source, peer-to-peer code collaboration stack built on Git.
 81  
 82  :construction: [radicle.xyz](https://radicle.xyz)`,
 83        },
 84        node: {
 85          ...defaultConfig.node,
 86          seedingPolicy: { default: "allow", scope: "all" },
 87          alias: "palm",
 88        },
 89      });
 90      await palm.startHttpd(defaultHttpdPort);
 91  
 92      try {
 93        console.log("Creating source-browsing fixture");
 94        await createSourceBrowsingFixture(peerManager, palm);
 95        console.log("Creating markdown fixture");
 96        await createMarkdownFixture(palm);
 97        console.log("Creating cobs fixture");
 98        await createCobsFixture(peerManager, palm);
 99        console.log("All fixtures created");
100      } catch (error) {
101        console.log("");
102        console.log("Not able to create the required fixtures.");
103        console.log("Make sure you are not using binaries compiled for release.");
104        console.log("");
105        console.log(error);
106        console.log("");
107        process.exit(1);
108      }
109      await palm.stopNode();
110    } else {
111      await palm.startHttpd(defaultHttpdPort);
112    }
113  
114    return async () => {
115      await peerManager.shutdown();
116    };
117  }