Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Add a `NodeConfigurator` class, to modify a `config.json`
Sebastian Martinez committed 2 years ago
commit cccee12477ca0e1c373aaec98315f7ce28e1547d
parent 3dd590d8e688e89f22c38ba883abd6cdf21c0844
4 files changed +31 -42
modified httpd-client/tests/support/globalSetup.ts
@@ -47,7 +47,7 @@ export default async function globalSetup(): Promise<() => void> {
      gitOptions: gitOptions["alice"],
    });
    await palm.startHttpd(defaultHttpdPort);
-
    await palm.startNode({ trackingPolicy: "track", trackingScope: "all" });
+
    await palm.startNode({ policy: "track", scope: "all" });

    console.log("Creating source-browsing fixture");
    await createSourceBrowsingFixture(peerManager, palm);
modified tests/support/fixtures.ts
@@ -310,10 +310,12 @@ export async function createSourceBrowsingFixture(
    gitOptions: gitOptions["bob"],
  });
  const bobProjectPath = Path.join(bob.checkoutPath, "source-browsing");
-
  await alice.startNode();
-
  await bob.startNode();
-
  await alice.connect(palm);
-
  await bob.connect(palm);
+
  await alice.startNode({ connect: [palm.address] });
+
  await bob.startNode({ connect: [palm.address] });
+
  await alice.waitForEvent({ type: "peerConnected", nid: palm.nodeId }, 1000);
+
  await palm.waitForEvent({ type: "peerConnected", nid: alice.nodeId }, 1000);
+
  await bob.waitForEvent({ type: "peerConnected", nid: palm.nodeId }, 1000);
+
  await palm.waitForEvent({ type: "peerConnected", nid: bob.nodeId }, 1000);

  await alice.git(["clone", sourceBrowsingDir], { cwd: alice.checkoutPath });
  await alice.git(["checkout", "feature/branch"], { cwd: aliceProjectPath });
modified tests/support/globalSetup.ts
@@ -51,7 +51,7 @@ export default async function globalSetup(): Promise<() => void> {
      gitOptions: gitOptions["alice"],
    });
    await palm.startHttpd(defaultHttpdPort);
-
    await palm.startNode({ trackingPolicy: "track", trackingScope: "all" });
+
    await palm.startNode({ policy: "track", scope: "all" });

    console.log("Creating source-browsing fixture");
    await createSourceBrowsingFixture(peerManager, palm);
modified tests/support/peerManager.ts
@@ -203,17 +203,6 @@ export class RadiclePeer {
    };

    await execa("rad", ["auth", "--alias", name], { env });
-
    const config = await Fs.readFile(
-
      Path.join(radHome, "config.json"),
-
      "utf-8",
-
    );
-
    const parsedConfig = NodeConfigSchema.parse(JSON.parse(config));
-
    parsedConfig.node.network = "test";
-
    await Fs.writeFile(
-
      Path.join(radHome, "config.json"),
-
      JSON.stringify(parsedConfig),
-
      "utf-8",
-
    );
    const { stdout: nodeId } = await execa("rad", ["self", "--nid"], { env });

    return new RadiclePeer({
@@ -265,27 +254,22 @@ export class RadiclePeer {
    this.#httpdBaseUrl = undefined;
  }

-
  public async startNode(params?: {
-
    trackingScope?: "trusted" | "all";
-
    trackingPolicy?: "track" | "block";
-
  }) {
+
  public async startNode(nodeParams: Partial<NodeConfig["node"]> = {}) {
    const gitPort = await getPort();
    const gitSocketAddr = `0.0.0.0:${gitPort}`;
    const listenPort = await getPort();
    this.#listenSocketAddr = `0.0.0.0:${listenPort}`;

+
    // We need to set the `config.json` file after creating the node identity and before starting it.
+
    // Since we aren't able to pass these configuration options through the CLI.
+
    await updateNodeConfig(this.#radHome, nodeParams);
+

    const args = [
      "--git-daemon",
      gitSocketAddr,
      "--listen",
      this.#listenSocketAddr,
    ];
-
    if (params?.trackingScope) {
-
      args.push("--tracking-scope", params.trackingScope);
-
    }
-
    if (params?.trackingPolicy) {
-
      args.push("--tracking-policy", params.trackingPolicy);
-
    }

    this.#nodeProcess = this.spawn("radicle-node", args);

@@ -353,23 +337,11 @@ export class RadiclePeer {
    }
  }

-
  public async connect(remote: RadiclePeer) {
-
    if (!remote.#listenSocketAddr) {
+
  public get address(): string {
+
    if (!this.#listenSocketAddr) {
      throw new Error("Remote node has no listen addr yet");
    }
-
    await this.rad(
-
      ["node", "connect", `${remote.nodeId}@${remote.#listenSocketAddr}`],
-
      { cwd: this.#radHome },
-
    );
-

-
    await this.waitForEvent(
-
      { type: "peerConnected", nid: remote.nodeId },
-
      1000,
-
    );
-
    await remote.waitForEvent(
-
      { type: "peerConnected", nid: this.nodeId },
-
      1000,
-
    );
+
    return `${this.nodeId}@${this.#listenSocketAddr}`;
  }

  public uiUrl(): string {
@@ -426,3 +398,18 @@ export class RadiclePeer {
    return childProcess;
  }
}
+

+
async function updateNodeConfig(
+
  radHome: string,
+
  nodeParams: Partial<NodeConfig["node"]>,
+
) {
+
  const configPath = Path.join(radHome, "config.json");
+
  const config = await Fs.readFile(configPath, "utf-8");
+
  const nodeConfig = NodeConfigSchema.parse(JSON.parse(config));
+
  nodeConfig.node = {
+
    ...nodeConfig.node,
+
    network: "test",
+
    ...nodeParams,
+
  };
+
  await Fs.writeFile(configPath, JSON.stringify(nodeConfig), "utf-8");
+
}