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
29 changes: 24 additions & 5 deletions pm-stack/aggregator/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ import { join } from "node:path";

const s = (v: string | undefined, d: string): string => (v && v.trim() !== "" ? v.trim() : d);

function parseAggregatorPort(value: string | undefined): number {
const normalized = s(value, "4870");
const port = Number(normalized);
if (!/^\d+$/.test(normalized) || !Number.isSafeInteger(port) || port < 1 || port > 65535) {
throw new Error(
`Invalid PM_AGG_PORT=${JSON.stringify(value)}; expected a decimal integer in range 1-65535`,
);
}
return port;
}

function parseAggregatorHosts(value: string | undefined): string[] {
const hosts = s(value, "127.0.0.1")
.split(",")
.map((host) => host.trim())
.filter(Boolean);
if (hosts.length === 0) {
throw new Error(`Invalid PM_AGG_HOSTS=${JSON.stringify(value)}; expected at least one non-empty host`);
}
return hosts;
}

export interface Config {
/** "fixtures" | "live" — the default Source. */
source: "fixtures" | "live";
Expand Down Expand Up @@ -78,11 +100,8 @@ export interface Config {
export function loadConfig(env: Record<string, string | undefined> = process.env): Config {
return {
source: env.PM_AGG_SOURCE === "live" ? "live" : "fixtures",
port: Number(s(env.PM_AGG_PORT, "4870")),
hosts: s(env.PM_AGG_HOSTS, "127.0.0.1")
.split(",")
.map((h) => h.trim())
.filter(Boolean),
port: parseAggregatorPort(env.PM_AGG_PORT),
hosts: parseAggregatorHosts(env.PM_AGG_HOSTS),
feedFile: s(env.PM_AGG_FEED_FILE, join(process.cwd(), "fleet-feed.json")),
commentsFile: s(env.PM_AGG_COMMENTS_FILE, join(process.cwd(), "comments.json")),
roadmapAnnotationsFile: s(env.PM_AGG_ROADMAP_ANNOTATIONS_FILE, join(process.cwd(), "roadmap-annotations.json")),
Expand Down
62 changes: 62 additions & 0 deletions pm-stack/aggregator/test/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { describe, expect, test } from "bun:test";
import { loadConfig } from "../src/config.ts";

describe("loadConfig PM_AGG_PORT", () => {
test("keeps the existing default for unset and empty values", () => {
expect(loadConfig({}).port).toBe(4870);
expect(loadConfig({ PM_AGG_PORT: "" }).port).toBe(4870);
expect(loadConfig({ PM_AGG_PORT: " " }).port).toBe(4870);
});

test("accepts decimal integer ports from 1 through 65535", () => {
expect(loadConfig({ PM_AGG_PORT: "1" }).port).toBe(1);
expect(loadConfig({ PM_AGG_PORT: "4871" }).port).toBe(4871);
expect(loadConfig({ PM_AGG_PORT: "65535" }).port).toBe(65535);
});

test("rejects invalid values with the variable, value, and expected range", () => {
for (const value of ["0", "abc", "12.5", "-1", "99999", "0x10", "1e3", "4870oops"]) {
let thrown: unknown;
try {
loadConfig({ PM_AGG_PORT: value });
} catch (error) {
thrown = error;
}

expect(thrown).toBeInstanceOf(Error);
expect((thrown as Error).message).toContain("PM_AGG_PORT");
expect((thrown as Error).message).toContain(JSON.stringify(value));
expect((thrown as Error).message).toContain("1-65535");
}
});
});

describe("loadConfig PM_AGG_HOSTS", () => {
test("keeps the existing default for unset and empty values", () => {
expect(loadConfig({}).hosts).toEqual(["127.0.0.1"]);
expect(loadConfig({ PM_AGG_HOSTS: "" }).hosts).toEqual(["127.0.0.1"]);
expect(loadConfig({ PM_AGG_HOSTS: " " }).hosts).toEqual(["127.0.0.1"]);
});

test("trims and preserves configured hosts", () => {
expect(loadConfig({ PM_AGG_HOSTS: "localhost, 127.0.0.1" }).hosts).toEqual([
"localhost",
"127.0.0.1",
]);
});

test("rejects lists that contain no non-empty host", () => {
for (const value of [",", " , , "]) {
let thrown: unknown;
try {
loadConfig({ PM_AGG_HOSTS: value });
} catch (error) {
thrown = error;
}

expect(thrown).toBeInstanceOf(Error);
expect((thrown as Error).message).toContain("PM_AGG_HOSTS");
expect((thrown as Error).message).toContain(JSON.stringify(value));
}
});
});
Loading