uptime/tst/args.spec.ts

105 lines
2.6 KiB
TypeScript

import { test, expect } from "bun:test";
import * as E from "fp-ts/lib/Either";
import * as O from "fp-ts/lib/Option";
import { parseArgs, type Args } from "../src/args";
import { TestType } from "../src/canary";
test("should return an error if argv has less than 2 arguments", () => {
const result = parseArgs([]);
expect(E.isLeft(result)).toBe(true);
expect(E.left<string, Args>("bad argv")).toEqual(result);
});
test("should return an error if --tests-file is not specified", () => {
const result = parseArgs(["node", "script.js", "--test-name", "test"]);
expect(E.isLeft(result)).toBe(true);
expect(E.left<string, Args>("please specify a test file")).toEqual(result);
});
test("should return an error if test type is invalid", () => {
const result = parseArgs([
"bun",
"script.js",
"--tests-file",
"testFile",
"--test-type",
"invalidType",
]);
expect(E.isLeft(result)).toBe(true);
expect(E.left<string, Args>("bad test type invalidType")).toEqual(result);
});
test("should parse arguments correctly with all options specified", () => {
const result = parseArgs([
"node",
"script.js",
"--tests-file",
"testFile",
"--test-name",
"testName",
"--test-type",
"dns",
]);
const expected: Args = {
testsFile: "testFile",
testName: O.some("testName"),
testType: O.some(TestType.DNS),
};
expect(E.isRight(result)).toBe(true);
expect(E.right<string, Args>(expected)).toEqual(result);
});
test("should parse arguments correctly with only testsFile specified", () => {
const result = parseArgs(["node", "script.js", "--tests-file", "testFile"]);
const expected: Args = {
testsFile: "testFile",
testName: O.none,
testType: O.none,
};
expect(E.isRight(result)).toBe(true);
expect(E.right<string, Args>(expected)).toEqual(result);
});
test("should handle missing values for flags gracefully", () => {
const result = parseArgs([
"node",
"script.js",
"--tests-file",
"testFile",
"--test-name",
]);
const expected: Args = {
testsFile: "testFile",
testName: O.none,
testType: O.none,
};
expect(E.isRight(result)).toBe(true);
expect(E.right<string, Args>(expected)).toEqual(result);
});
test("should not parse args as values", () => {
const result = parseArgs([
"node",
"script.js",
"--tests-file",
"testFile",
"--test-name",
"--tests-file",
]);
const expected: Args = {
testsFile: "testFile",
testName: O.none,
testType: O.none,
};
expect(E.isRight(result)).toBe(true);
expect(E.right<string, Args>(expected)).toEqual(result);
});