Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions src/action/actions/build.action.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { defaultClientConfig, defaultServerConfig } from "@nanoforge-dev/config";
import { join, relative } from "node:path";
import { describe, expect, it } from "vitest";

import { type WorkspaceConfig } from "@lib/config";
import { type Input } from "@lib/input";

import { BuildAction } from "./build.action";

interface BuildTargetShape {
name: string;
directory: string;
entry: string;
static?: string;
output: string;
platform: "browser" | "node";
sourcemap: boolean;
}

type ResolveTargets = (
workspace: WorkspaceConfig,
baseDirectory: string,
options: Input,
isEditor: boolean,
) => BuildTargetShape[];

const resolveTargets = (
workspace: WorkspaceConfig,
baseDirectory: string,
options: Input = new Map(),
isEditor = false,
): BuildTargetShape[] => {
const action = new BuildAction();
const fn = (action as unknown as { resolveTargets: ResolveTargets }).resolveTargets;
return fn.call(action, workspace, baseDirectory, options, isEditor);
};

const clientProject = { type: "project", config: { type: "client" } } as const;
const serverProject = { type: "project", config: { type: "server" } } as const;

const p2pWorkspace = (): WorkspaceConfig =>
({
type: "workspace",
config: { network: "p2p" },
projects: { client: clientProject, server: serverProject },
}) as unknown as WorkspaceConfig;

describe("BuildAction.resolveTargets", () => {
it("should add a browser copy of the server target for each node target in p2p mode", () => {
const targets = resolveTargets(p2pWorkspace(), "/ws");

expect(targets).toHaveLength(3);

const client = targets.find((target) => target.name.startsWith("Client"));
const server = targets.find((target) => target.name === "Server (server)");
const browserServer = targets.find((target) => target.name === "Server (server) (browser)");

expect(client?.platform).toBe("browser");
expect(server?.platform).toBe("node");
expect(browserServer).toBeDefined();
expect(browserServer?.platform).toBe("browser");
// The browser copy of the server is emitted inside the client's output.
expect(browserServer?.output).toBe(
relative(
join("/ws", "server"),
join(join("/ws", "client"), defaultClientConfig.out.dir, "server"),
),
);
expect(browserServer?.directory).toBe(server?.directory);
expect(browserServer?.entry).toBe(server?.entry);
});

it("should not add browser copies outside p2p mode", () => {
const workspace = {
type: "workspace",
config: { network: "lan" },
projects: { client: clientProject, server: serverProject },
} as unknown as WorkspaceConfig;

const targets = resolveTargets(workspace, "/ws");

expect(targets).toHaveLength(2);
expect(targets.map((target) => target.platform)).toEqual(["browser", "node"]);
});

it("should not add browser copies in p2p mode when there is no client target", () => {
const workspace = {
type: "workspace",
config: { network: "p2p" },
projects: { server: serverProject },
} as unknown as WorkspaceConfig;

const targets = resolveTargets(workspace, "/ws");

expect(targets).toHaveLength(1);
expect(targets[0]?.platform).toBe("node");
});

it("should point the browser server output at the overridden client output folder", () => {
const options: Input = new Map([["clientOutDir", { value: "public" }]]);

const targets = resolveTargets(p2pWorkspace(), "/ws", options);
const browserServer = targets.find((target) => target.name === "Server (server) (browser)");

expect(browserServer?.output).toBe(
relative(join("/ws", "server"), join(join("/ws", "client"), "public", "server")),
);
});

it("should use the config defaults for a single project and skip name disambiguation", () => {
const workspace = {
type: "project",
config: { type: "client" },
} as unknown as WorkspaceConfig;

const targets = resolveTargets(workspace, "/ws");

expect(targets).toHaveLength(1);
expect(targets[0]).toMatchObject({
name: "Client",
directory: "/ws",
entry: defaultClientConfig.entryFile,
static: defaultClientConfig.dir.assets,
output: defaultClientConfig.out.dir,
platform: "browser",
sourcemap: false,
});
});

it("should use the editor entry point and enable sourcemaps in editor mode", () => {
const workspace = {
type: "project",
config: { type: "server" },
} as unknown as WorkspaceConfig;

const targets = resolveTargets(workspace, "/ws", new Map(), true);

expect(targets[0]).toMatchObject({
name: "Server",
entry: defaultServerConfig.editor.entryFile,
sourcemap: true,
});
});
});
171 changes: 171 additions & 0 deletions src/action/actions/start.action.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { type ClientConfig } from "@nanoforge-dev/config";
import fs from "fs";
import { tmpdir } from "os";
import { join } from "path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";

import { type Input } from "@lib/input";

import { CLIError } from "@utils/errors";

import { StartAction } from "./start.action";

interface FullEnvShape {
client: Record<string, string>;
server: Record<string, string>;
}

interface TLSOptionsShape {
cert: string;
key: string;
}

type ParseEnv = (dir: string) => FullEnvShape;
type ResolveTLS = (options: Input, config: ClientConfig) => TLSOptionsShape | undefined;

const internals = () => {
const action = new StartAction();
const cast = action as unknown as { parseEnv: ParseEnv; resolveTLS: ResolveTLS };
return {
parseEnv: cast.parseEnv.bind(action),
resolveTLS: cast.resolveTLS.bind(action),
};
};

const clientConfig = (tls?: ClientConfig["tls"]): ClientConfig =>
({ type: "client", tls }) as unknown as ClientConfig;

describe("StartAction.parseEnv", () => {
let dir: string;
let savedEnv: NodeJS.ProcessEnv;

beforeEach(() => {
dir = fs.mkdtempSync(join(tmpdir(), "nf-start-env-"));
savedEnv = { ...process.env };
for (const key of Object.keys(process.env)) {
if (key.startsWith("NANOFORGE_")) Reflect.deleteProperty(process.env, key);
}
});

afterEach(() => {
process.env = savedEnv;
fs.rmSync(dir, { recursive: true, force: true });
});

it("should only pass NANOFORGE_ variables through, to both sides", () => {
process.env.NANOFORGE_SHARED = "shared";
process.env.UNRELATED_VARIABLE = "nope";

const env = internals().parseEnv(dir);

expect(env.client).toEqual({ NANOFORGE_SHARED: "shared" });
expect(env.server).toEqual({ NANOFORGE_SHARED: "shared" });
});

it("should strip the CLIENT_ prefix for the client and hide the variable from the server", () => {
process.env.NANOFORGE_CLIENT_API_URL = "https://example.test";

const env = internals().parseEnv(dir);

expect(env.client).toEqual({ NANOFORGE_API_URL: "https://example.test" });
expect(env.server).toEqual({});
});

it("should strip the SERVER_ prefix for the server and hide the variable from the client", () => {
process.env.NANOFORGE_SERVER_SECRET = "s3cret";

const env = internals().parseEnv(dir);

expect(env.server).toEqual({ NANOFORGE_SECRET: "s3cret" });
expect(env.client).toEqual({});
});

it("should drop variables with empty values", () => {
process.env.NANOFORGE_EMPTY = "";
process.env.NANOFORGE_SET = "1";

const env = internals().parseEnv(dir);

expect(env.client).toEqual({ NANOFORGE_SET: "1" });
expect(env.server).toEqual({ NANOFORGE_SET: "1" });
});

it("should load variables from the project's .env file", () => {
fs.writeFileSync(join(dir, ".env"), "NANOFORGE_FROM_FILE=file-value\nOTHER=ignored\n");

const env = internals().parseEnv(dir);

expect(env.client.NANOFORGE_FROM_FILE).toBe("file-value");
expect(env.server.NANOFORGE_FROM_FILE).toBe("file-value");
expect(env.client).not.toHaveProperty("OTHER");
});

it("should let the process environment win over the .env file", () => {
process.env.NANOFORGE_MODE = "process";
fs.writeFileSync(join(dir, ".env"), "NANOFORGE_MODE=file\n");

const env = internals().parseEnv(dir);

expect(env.client.NANOFORGE_MODE).toBe("process");
expect(env.server.NANOFORGE_MODE).toBe("process");
});
});

describe("StartAction.resolveTLS", () => {
it("should return undefined when TLS is not requested anywhere", () => {
expect(internals().resolveTLS(new Map(), clientConfig())).toBeUndefined();
expect(
internals().resolveTLS(
new Map(),
clientConfig({ enable: false, cert: "cert.pem", key: "key.pem" }),
),
).toBeUndefined();
});

it("should return the CLI certificate and key when both are given", () => {
const options: Input = new Map([
["cert", { value: "cli-cert.pem" }],
["key", { value: "cli-key.pem" }],
]);

expect(internals().resolveTLS(options, clientConfig())).toEqual({
cert: "cli-cert.pem",
key: "cli-key.pem",
});
});

it("should fall back to the enabled config TLS pair", () => {
const config = clientConfig({ enable: true, cert: "cfg-cert.pem", key: "cfg-key.pem" });

expect(internals().resolveTLS(new Map(), config)).toEqual({
cert: "cfg-cert.pem",
key: "cfg-key.pem",
});
});

it("should combine a CLI certificate with the config key", () => {
const options: Input = new Map([["cert", { value: "cli-cert.pem" }]]);
const config = clientConfig({ enable: true, cert: "cfg-cert.pem", key: "cfg-key.pem" });

expect(internals().resolveTLS(options, config)).toEqual({
cert: "cli-cert.pem",
key: "cfg-key.pem",
});
});

it("should throw a CLIError when TLS is requested without a certificate", () => {
const options: Input = new Map([["key", { value: "cli-key.pem" }]]);

expect(() => internals().resolveTLS(options, clientConfig())).toThrow(CLIError);
expect(() => internals().resolveTLS(options, clientConfig())).toThrow(
"No certificate found for TLS.",
);
});

it("should throw a CLIError when TLS is requested without a key", () => {
const config = clientConfig({ enable: true, cert: "cfg-cert.pem" });

expect(() => internals().resolveTLS(new Map(), config)).toThrow(CLIError);
expect(() => internals().resolveTLS(new Map(), config)).toThrow("No key found for TLS.");
});
});
Loading
Loading