|
| 1 | +import { expect, test } from "vite-plus/test"; |
| 2 | +import { ExitCode, GLOBAL_OPTIONS } from "bailian-cli-core"; |
| 3 | +import { parseFlags } from "../src/args.ts"; |
| 4 | +import { BOOL_FLAG_WATERMARK } from "../src/utils/flag-descriptions.ts"; |
| 5 | + |
| 6 | +const IMAGE_GENERATE_OPTIONS = [ |
| 7 | + { flag: "--prompt <text>", description: "Image description", required: true }, |
| 8 | + { flag: "--model <model>", description: "Model ID" }, |
| 9 | + { flag: "--watermark <bool>", description: BOOL_FLAG_WATERMARK }, |
| 10 | + { flag: "--no-wait", description: "Return task ID immediately without waiting" }, |
| 11 | +]; |
| 12 | + |
| 13 | +test("parseFlags rejects unknown long flags", () => { |
| 14 | + expect(() => |
| 15 | + parseFlags(["--prompt", "cat", "--xxxx", "a"], [...GLOBAL_OPTIONS, ...IMAGE_GENERATE_OPTIONS]), |
| 16 | + ).toThrowError( |
| 17 | + expect.objectContaining({ |
| 18 | + name: "BailianError", |
| 19 | + exitCode: ExitCode.USAGE, |
| 20 | + message: expect.stringContaining('Unknown flag "--xxxx"'), |
| 21 | + }), |
| 22 | + ); |
| 23 | +}); |
| 24 | + |
| 25 | +test("parseFlags rejects unknown flags with = syntax", () => { |
| 26 | + expect(() => |
| 27 | + parseFlags( |
| 28 | + ["--prompt=cat", "--unknown-flag=yes"], |
| 29 | + [...GLOBAL_OPTIONS, ...IMAGE_GENERATE_OPTIONS], |
| 30 | + ), |
| 31 | + ).toThrow(/Unknown flag "--unknown-flag"/); |
| 32 | +}); |
| 33 | + |
| 34 | +test("parseFlags accepts defined command and global flags", () => { |
| 35 | + const flags = parseFlags( |
| 36 | + ["--quiet", "--prompt", "cat", "--watermark", "false"], |
| 37 | + [...GLOBAL_OPTIONS, ...IMAGE_GENERATE_OPTIONS], |
| 38 | + ); |
| 39 | + expect(flags.quiet).toBe(true); |
| 40 | + expect(flags.prompt).toBe("cat"); |
| 41 | + expect(flags.watermark).toBe("false"); |
| 42 | +}); |
| 43 | + |
| 44 | +test("parseFlags rejects value flag when next token is another flag", () => { |
| 45 | + const opts = [...GLOBAL_OPTIONS, ...IMAGE_GENERATE_OPTIONS]; |
| 46 | + for (const argv of [ |
| 47 | + ["--watermark", "--prompt", "cat"], |
| 48 | + ["--watermark", "-h"], |
| 49 | + ["--prompt", "cat", "--watermark", "--model", "qwen-image-2.0"], |
| 50 | + ]) { |
| 51 | + expect(() => parseFlags(argv, opts)).toThrowError( |
| 52 | + expect.objectContaining({ |
| 53 | + name: "BailianError", |
| 54 | + exitCode: ExitCode.USAGE, |
| 55 | + message: expect.stringContaining("Flag --watermark requires a value"), |
| 56 | + }), |
| 57 | + ); |
| 58 | + } |
| 59 | +}); |
| 60 | + |
| 61 | +test("parseFlags rejects trailing value flag without value", () => { |
| 62 | + expect(() => |
| 63 | + parseFlags(["--prompt", "cat", "--watermark"], [...GLOBAL_OPTIONS, ...IMAGE_GENERATE_OPTIONS]), |
| 64 | + ).toThrowError( |
| 65 | + expect.objectContaining({ |
| 66 | + message: expect.stringContaining("Flag --watermark requires a value"), |
| 67 | + }), |
| 68 | + ); |
| 69 | +}); |
| 70 | + |
| 71 | +test("parseFlags allows boolean flags without values adjacent to other flags", () => { |
| 72 | + const opts = [...GLOBAL_OPTIONS, ...IMAGE_GENERATE_OPTIONS]; |
| 73 | + const flags = parseFlags( |
| 74 | + ["--quiet", "--dry-run", "--no-wait", "--prompt", "cat", "--watermark", "false"], |
| 75 | + opts, |
| 76 | + ); |
| 77 | + expect(flags.quiet).toBe(true); |
| 78 | + expect(flags.dryRun).toBe(true); |
| 79 | + expect(flags.noWait).toBe(true); |
| 80 | + expect(flags.prompt).toBe("cat"); |
| 81 | + expect(flags.watermark).toBe("false"); |
| 82 | +}); |
| 83 | + |
| 84 | +test("parseFlags does not treat the next flag as a boolean flag value", () => { |
| 85 | + const opts = [...GLOBAL_OPTIONS, ...IMAGE_GENERATE_OPTIONS]; |
| 86 | + expect(() => parseFlags(["--dry-run", "--prompt"], opts)).toThrowError( |
| 87 | + expect.objectContaining({ |
| 88 | + message: expect.stringContaining("Flag --prompt requires a value"), |
| 89 | + }), |
| 90 | + ); |
| 91 | + // --dry-run is boolean: no value check; parsing continues to --prompt. |
| 92 | + const flags = parseFlags(["--dry-run", "--prompt", "cat"], opts); |
| 93 | + expect(flags.dryRun).toBe(true); |
| 94 | + expect(flags.prompt).toBe("cat"); |
| 95 | +}); |
0 commit comments