Skip to content

Commit 4c421c2

Browse files
committed
chore: add tests for the moved validation
1 parent dbd633e commit 4c421c2

File tree

2 files changed

+202
-3
lines changed

2 files changed

+202
-3
lines changed

src/common/config.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,12 @@ function commaSeparatedToArray<T extends string[]>(str: string | string[] | unde
263263

264264
if (str.length === 1) {
265265
if (str[0]?.indexOf(",") === -1) {
266-
return str as T;
266+
return str.filter((e) => e.trim().length > 0) as T;
267267
} else {
268-
return str[0]?.split(",").map((e) => e.trim()) as T;
268+
return str[0]
269+
?.split(",")
270+
.map((e) => e.trim())
271+
.filter((e) => e.length > 0) as T;
269272
}
270273
}
271274

@@ -305,7 +308,8 @@ export function setupUserConfig({
305308
throw new Error(`Invalid telemetry: ${telemetry}`);
306309
}
307310

308-
if (userConfig.httpPort < 1 || userConfig.httpPort > 65535) {
311+
const httpPort = +userConfig.httpPort;
312+
if (httpPort < 1 || httpPort > 65535 || isNaN(httpPort)) {
309313
throw new Error(`Invalid httpPort: ${userConfig.httpPort}`);
310314
}
311315

tests/unit/common/config.test.ts

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, it, expect } from "vitest";
22
import { setupUserConfig, UserConfig, defaultUserConfig } from "../../../src/common/config.js";
3+
import assert from "assert";
34

45
describe("config", () => {
56
describe("env var parsing", () => {
@@ -409,4 +410,198 @@ describe("config", () => {
409410
expect(actual.connectionSpecifier).toBe("mongodb://localhost");
410411
});
411412
});
413+
414+
describe("validation", () => {
415+
describe("transport", () => {
416+
it("should support http", () => {
417+
const actual = setupUserConfig({
418+
cli: ["myself", "--", "--transport", "http"],
419+
env: {},
420+
defaults: defaultUserConfig,
421+
});
422+
423+
expect(actual.transport).toEqual("http");
424+
});
425+
426+
it("should support stdio", () => {
427+
const actual = setupUserConfig({
428+
cli: ["myself", "--", "--transport", "stdio"],
429+
env: {},
430+
defaults: defaultUserConfig,
431+
});
432+
433+
expect(actual.transport).toEqual("stdio");
434+
});
435+
436+
it("should not support sse", () => {
437+
expect(() =>
438+
setupUserConfig({
439+
cli: ["myself", "--", "--transport", "sse"],
440+
env: {},
441+
defaults: defaultUserConfig,
442+
})
443+
).toThrowError("Invalid transport: sse");
444+
});
445+
446+
it("should not support arbitrary values", () => {
447+
const value = Math.random() + "transport";
448+
449+
expect(() =>
450+
setupUserConfig({
451+
cli: ["myself", "--", "--transport", value],
452+
env: {},
453+
defaults: defaultUserConfig,
454+
})
455+
).toThrowError(`Invalid transport: ${value}`);
456+
});
457+
});
458+
459+
describe("telemetry", () => {
460+
it("can be enabled", () => {
461+
const actual = setupUserConfig({
462+
cli: ["myself", "--", "--telemetry", "enabled"],
463+
env: {},
464+
defaults: defaultUserConfig,
465+
});
466+
467+
expect(actual.telemetry).toEqual("enabled");
468+
});
469+
470+
it("can be disabled", () => {
471+
const actual = setupUserConfig({
472+
cli: ["myself", "--", "--telemetry", "disabled"],
473+
env: {},
474+
defaults: defaultUserConfig,
475+
});
476+
477+
expect(actual.telemetry).toEqual("disabled");
478+
});
479+
480+
it("should not support the boolean true value", () => {
481+
expect(() =>
482+
setupUserConfig({
483+
cli: ["myself", "--", "--telemetry", "true"],
484+
env: {},
485+
defaults: defaultUserConfig,
486+
})
487+
).toThrowError("Invalid telemetry: true");
488+
});
489+
490+
it("should not support the boolean false value", () => {
491+
expect(() =>
492+
setupUserConfig({
493+
cli: ["myself", "--", "--telemetry", "false"],
494+
env: {},
495+
defaults: defaultUserConfig,
496+
})
497+
).toThrowError("Invalid telemetry: false");
498+
});
499+
500+
it("should not support arbitrary values", () => {
501+
const value = Math.random() + "telemetry";
502+
503+
expect(() =>
504+
setupUserConfig({
505+
cli: ["myself", "--", "--telemetry", value],
506+
env: {},
507+
defaults: defaultUserConfig,
508+
})
509+
).toThrowError(`Invalid telemetry: ${value}`);
510+
});
511+
});
512+
513+
describe("httpPort", () => {
514+
it("must be above 1", () => {
515+
expect(() =>
516+
setupUserConfig({
517+
cli: ["myself", "--", "--httpPort", "0"],
518+
env: {},
519+
defaults: defaultUserConfig,
520+
})
521+
).toThrowError("Invalid httpPort: 0");
522+
});
523+
524+
it("must be below 65535 (OS limit)", () => {
525+
expect(() =>
526+
setupUserConfig({
527+
cli: ["myself", "--", "--httpPort", "89527345"],
528+
env: {},
529+
defaults: defaultUserConfig,
530+
})
531+
).toThrowError("Invalid httpPort: 89527345");
532+
});
533+
534+
it("should not support non numeric values", () => {
535+
expect(() =>
536+
setupUserConfig({
537+
cli: ["myself", "--", "--httpPort", "portAventura"],
538+
env: {},
539+
defaults: defaultUserConfig,
540+
})
541+
).toThrowError("Invalid httpPort: portAventura");
542+
});
543+
544+
it("should support numeric values", () => {
545+
const actual = setupUserConfig({
546+
cli: ["myself", "--", "--httpPort", "8888"],
547+
env: {},
548+
defaults: defaultUserConfig,
549+
});
550+
551+
expect(actual.httpPort).toEqual("8888");
552+
});
553+
});
554+
555+
describe("loggers", () => {
556+
it("must not be empty", () => {
557+
expect(() =>
558+
setupUserConfig({
559+
cli: ["myself", "--", "--loggers", ""],
560+
env: {},
561+
defaults: defaultUserConfig,
562+
})
563+
).toThrowError("No loggers found in config");
564+
});
565+
566+
it("must not allow duplicates", () => {
567+
expect(() =>
568+
setupUserConfig({
569+
cli: ["myself", "--", "--loggers", "disk,disk,disk"],
570+
env: {},
571+
defaults: defaultUserConfig,
572+
})
573+
).toThrowError("Duplicate loggers found in config");
574+
});
575+
576+
it("allows mcp logger", () => {
577+
const actual = setupUserConfig({
578+
cli: ["myself", "--", "--loggers", "mcp"],
579+
env: {},
580+
defaults: defaultUserConfig,
581+
});
582+
583+
expect(actual.loggers).toEqual(["mcp"]);
584+
});
585+
586+
it("allows disk logger", () => {
587+
const actual = setupUserConfig({
588+
cli: ["myself", "--", "--loggers", "disk"],
589+
env: {},
590+
defaults: defaultUserConfig,
591+
});
592+
593+
expect(actual.loggers).toEqual(["disk"]);
594+
});
595+
596+
it("allows stderr logger", () => {
597+
const actual = setupUserConfig({
598+
cli: ["myself", "--", "--loggers", "stderr"],
599+
env: {},
600+
defaults: defaultUserConfig,
601+
});
602+
603+
expect(actual.loggers).toEqual(["stderr"]);
604+
});
605+
});
606+
});
412607
});

0 commit comments

Comments
 (0)