|
| 1 | +/** |
| 2 | + * Profile View Command Tests |
| 3 | + * |
| 4 | + * Tests for positional argument parsing in src/commands/profile/view.ts |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, expect, test } from "bun:test"; |
| 8 | +import { parsePositionalArgs } from "../../../src/commands/profile/view.js"; |
| 9 | +import { ContextError } from "../../../src/lib/errors.js"; |
| 10 | + |
| 11 | +describe("parsePositionalArgs", () => { |
| 12 | + describe("single argument (transaction only)", () => { |
| 13 | + test("parses single arg as transaction name", () => { |
| 14 | + const result = parsePositionalArgs(["/api/users"]); |
| 15 | + expect(result.transactionRef).toBe("/api/users"); |
| 16 | + expect(result.targetArg).toBeUndefined(); |
| 17 | + }); |
| 18 | + |
| 19 | + test("parses transaction index", () => { |
| 20 | + const result = parsePositionalArgs(["1"]); |
| 21 | + expect(result.transactionRef).toBe("1"); |
| 22 | + expect(result.targetArg).toBeUndefined(); |
| 23 | + }); |
| 24 | + |
| 25 | + test("parses transaction alias", () => { |
| 26 | + const result = parsePositionalArgs(["a"]); |
| 27 | + expect(result.transactionRef).toBe("a"); |
| 28 | + expect(result.targetArg).toBeUndefined(); |
| 29 | + }); |
| 30 | + |
| 31 | + test("parses complex transaction name", () => { |
| 32 | + const result = parsePositionalArgs(["POST /api/v2/users/:id/settings"]); |
| 33 | + expect(result.transactionRef).toBe("POST /api/v2/users/:id/settings"); |
| 34 | + expect(result.targetArg).toBeUndefined(); |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + describe("two arguments (target + transaction)", () => { |
| 39 | + test("parses org/project target and transaction name", () => { |
| 40 | + const result = parsePositionalArgs(["my-org/backend", "/api/users"]); |
| 41 | + expect(result.targetArg).toBe("my-org/backend"); |
| 42 | + expect(result.transactionRef).toBe("/api/users"); |
| 43 | + }); |
| 44 | + |
| 45 | + test("parses project-only target and transaction", () => { |
| 46 | + const result = parsePositionalArgs(["backend", "/api/users"]); |
| 47 | + expect(result.targetArg).toBe("backend"); |
| 48 | + expect(result.transactionRef).toBe("/api/users"); |
| 49 | + }); |
| 50 | + |
| 51 | + test("parses org/ target (all projects) and transaction", () => { |
| 52 | + const result = parsePositionalArgs(["my-org/", "/api/users"]); |
| 53 | + expect(result.targetArg).toBe("my-org/"); |
| 54 | + expect(result.transactionRef).toBe("/api/users"); |
| 55 | + }); |
| 56 | + |
| 57 | + test("parses target and transaction index", () => { |
| 58 | + const result = parsePositionalArgs(["my-org/backend", "1"]); |
| 59 | + expect(result.targetArg).toBe("my-org/backend"); |
| 60 | + expect(result.transactionRef).toBe("1"); |
| 61 | + }); |
| 62 | + |
| 63 | + test("parses target and transaction alias", () => { |
| 64 | + const result = parsePositionalArgs(["my-org/backend", "a"]); |
| 65 | + expect(result.targetArg).toBe("my-org/backend"); |
| 66 | + expect(result.transactionRef).toBe("a"); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe("error cases", () => { |
| 71 | + test("throws ContextError for empty args", () => { |
| 72 | + expect(() => parsePositionalArgs([])).toThrow(ContextError); |
| 73 | + }); |
| 74 | + |
| 75 | + test("throws ContextError with usage hint", () => { |
| 76 | + try { |
| 77 | + parsePositionalArgs([]); |
| 78 | + expect.unreachable("Should have thrown"); |
| 79 | + } catch (error) { |
| 80 | + expect(error).toBeInstanceOf(ContextError); |
| 81 | + expect((error as ContextError).message).toContain("Transaction"); |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe("edge cases", () => { |
| 87 | + test("handles more than two args (ignores extras)", () => { |
| 88 | + const result = parsePositionalArgs([ |
| 89 | + "my-org/backend", |
| 90 | + "/api/users", |
| 91 | + "extra-arg", |
| 92 | + ]); |
| 93 | + expect(result.targetArg).toBe("my-org/backend"); |
| 94 | + expect(result.transactionRef).toBe("/api/users"); |
| 95 | + }); |
| 96 | + |
| 97 | + test("handles empty string transaction in two-arg case", () => { |
| 98 | + const result = parsePositionalArgs(["my-org/backend", ""]); |
| 99 | + expect(result.targetArg).toBe("my-org/backend"); |
| 100 | + expect(result.transactionRef).toBe(""); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments