|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import type { ArgDefinition } from "../../src/command/args"; |
| 3 | +import type { PositionalDefinition } from "../../src/command/positionals"; |
| 4 | +import { buildMriOptions, convertNumbers, mapPositionals, validatePositionals } from "../../src/util/mri-utils"; |
| 5 | + |
| 6 | +describe("buildMriOptions", () => { |
| 7 | + test("boolean args go to boolean array", () => { |
| 8 | + const defs: Record<string, ArgDefinition> = { debug: { type: "boolean" }, verbose: { type: "boolean" } }; |
| 9 | + |
| 10 | + expect(buildMriOptions(defs)).toMatchObject({ boolean: ["debug", "verbose"], string: [] }); |
| 11 | + }); |
| 12 | + |
| 13 | + test("string and number args go to string array", () => { |
| 14 | + const defs: Record<string, ArgDefinition> = { count: { type: "number" }, name: { type: "string" } }; |
| 15 | + |
| 16 | + expect(buildMriOptions(defs)).toMatchObject({ boolean: [], string: ["count", "name"] }); |
| 17 | + }); |
| 18 | + |
| 19 | + test("aliases registered correctly", () => { |
| 20 | + const defs: Record<string, ArgDefinition> = { |
| 21 | + output: { alias: "o", type: "string" }, |
| 22 | + verbose: { alias: "v", type: "boolean" }, |
| 23 | + }; |
| 24 | + |
| 25 | + expect(buildMriOptions(defs).alias).toMatchObject({ o: "output", v: "verbose" }); |
| 26 | + }); |
| 27 | + |
| 28 | + test("camelCase auto-generates kebab-case alias", () => { |
| 29 | + const defs: Record<string, ArgDefinition> = { dryRun: { type: "boolean" }, outputDir: { type: "string" } }; |
| 30 | + |
| 31 | + expect(buildMriOptions(defs).alias).toMatchObject({ "dry-run": "dryRun", "output-dir": "outputDir" }); |
| 32 | + }); |
| 33 | + |
| 34 | + test("defaults populated", () => { |
| 35 | + const defs: Record<string, ArgDefinition> = { |
| 36 | + count: { default: 10, type: "number" }, |
| 37 | + name: { type: "string" }, |
| 38 | + verbose: { default: false, type: "boolean" }, |
| 39 | + }; |
| 40 | + const opts = buildMriOptions(defs); |
| 41 | + expect(opts.default).toEqual({ count: 10, verbose: false }); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe("convertNumbers", () => { |
| 46 | + test("converts string '42' to number 42", () => { |
| 47 | + const defs: Record<string, ArgDefinition> = { count: { type: "number" } }; |
| 48 | + const result = convertNumbers({ count: "42" }, defs); |
| 49 | + expect(result.count).toBe(42); |
| 50 | + }); |
| 51 | + |
| 52 | + test("throws on invalid number (NaN)", () => { |
| 53 | + const defs: Record<string, ArgDefinition> = { count: { type: "number" } }; |
| 54 | + expect(() => convertNumbers({ count: "abc" }, defs)).toThrow('Invalid number for --count: "abc"'); |
| 55 | + }); |
| 56 | + |
| 57 | + test("skips boolean and string types", () => { |
| 58 | + const defs: Record<string, ArgDefinition> = { name: { type: "string" }, verbose: { type: "boolean" } }; |
| 59 | + |
| 60 | + expect(convertNumbers({ name: "hello", verbose: true }, defs)).toMatchObject({ name: "hello", verbose: true }); |
| 61 | + }); |
| 62 | + |
| 63 | + test("skips undefined values", () => { |
| 64 | + const defs: Record<string, ArgDefinition> = { count: { type: "number" } }; |
| 65 | + const result = convertNumbers({} as Record<string, string | boolean>, defs); |
| 66 | + expect(result.count).toBeUndefined(); |
| 67 | + }); |
| 68 | +}); |
| 69 | + |
| 70 | +describe("mapPositionals", () => { |
| 71 | + test("maps single positional correctly", () => { |
| 72 | + const defs: Record<string, PositionalDefinition> = { name: { required: true } }; |
| 73 | + const result = mapPositionals(["foo"], defs); |
| 74 | + expect(result.name).toBe("foo"); |
| 75 | + }); |
| 76 | + |
| 77 | + test("maps multiple positionals by index", () => { |
| 78 | + const defs: Record<string, PositionalDefinition> = { dest: { required: true }, source: { required: true } }; |
| 79 | + |
| 80 | + expect(mapPositionals(["a.txt", "b.txt"], defs)).toMatchObject({ dest: "a.txt", source: "b.txt" }); |
| 81 | + }); |
| 82 | + |
| 83 | + test("variadic positional captures rest as array", () => { |
| 84 | + const defs: Record<string, PositionalDefinition> = { first: { required: true }, rest: { variadic: true } }; |
| 85 | + |
| 86 | + expect(mapPositionals(["a", "b", "c", "d"], defs)).toMatchObject({ first: "a", rest: ["b", "c", "d"] }); |
| 87 | + }); |
| 88 | + |
| 89 | + test("missing positional returns undefined", () => { |
| 90 | + const defs: Record<string, PositionalDefinition> = { extra: {}, name: { required: true } }; |
| 91 | + |
| 92 | + expect(mapPositionals(["foo"], defs)).toMatchObject({ extra: "foo", name: undefined }); |
| 93 | + }); |
| 94 | + |
| 95 | + test("extra positionals silently dropped", () => { |
| 96 | + const defs: Record<string, PositionalDefinition> = { name: { required: true } }; |
| 97 | + const result = mapPositionals(["foo", "bar", "baz"], defs); |
| 98 | + |
| 99 | + expect(result).toEqual({ name: "foo" }); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe("validatePositionals", () => { |
| 104 | + test("returns null when all required present", () => { |
| 105 | + const defs: Record<string, PositionalDefinition> = { name: { required: true } }; |
| 106 | + const result = validatePositionals({ name: "foo" }, defs); |
| 107 | + expect(result).toBeNull(); |
| 108 | + }); |
| 109 | + |
| 110 | + test("returns error for missing required", () => { |
| 111 | + const defs: Record<string, PositionalDefinition> = { name: { required: true } }; |
| 112 | + const result = validatePositionals({ name: undefined }, defs); |
| 113 | + expect(result).toBe("Missing required argument: <name>"); |
| 114 | + }); |
| 115 | + |
| 116 | + test("returns error for empty variadic required", () => { |
| 117 | + const defs: Record<string, PositionalDefinition> = { files: { required: true, variadic: true } }; |
| 118 | + const result = validatePositionals({ files: [] }, defs); |
| 119 | + expect(result).toBe("Missing required argument: <files...>"); |
| 120 | + }); |
| 121 | + |
| 122 | + test("skips non-required", () => { |
| 123 | + const defs: Record<string, PositionalDefinition> = { optional: { required: false } }; |
| 124 | + const result = validatePositionals({ optional: undefined }, defs); |
| 125 | + expect(result).toBeNull(); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +describe("toKebabCase (via buildMriOptions)", () => { |
| 130 | + test("camelCase generates kebab-case alias", () => { |
| 131 | + const defs: Record<string, ArgDefinition> = { myLongOption: { type: "string" } }; |
| 132 | + const opts = buildMriOptions(defs); |
| 133 | + expect(opts.alias["my-long-option"]).toBe("myLongOption"); |
| 134 | + }); |
| 135 | + |
| 136 | + test("already kebab-case does not produce duplicate alias", () => { |
| 137 | + const defs: Record<string, ArgDefinition> = { "already-kebab": { type: "string" } }; |
| 138 | + const opts = buildMriOptions(defs); |
| 139 | + expect(opts.alias["already-kebab"]).toBeUndefined(); |
| 140 | + }); |
| 141 | +}); |
0 commit comments