| 1 | import { expect, test } from "vitest"; |
| 2 | import { getNumberArg } from "../parseArgs"; |
| 3 | |
| 4 | test("readFileRange should validate positive line number arguments", () => { |
| 5 | // Test positive startLine validation |
| 6 | expect(() => getNumberArg({ startLine: 1 }, "startLine")).not.toThrow(); |
| 7 | expect(() => getNumberArg({ startLine: "1" }, "startLine")).not.toThrow(); |
| 8 | expect(() => getNumberArg({ startLine: 100 }, "startLine")).not.toThrow(); |
| 9 | |
| 10 | // Test positive endLine validation |
| 11 | expect(() => getNumberArg({ endLine: 5 }, "endLine")).not.toThrow(); |
| 12 | expect(() => getNumberArg({ endLine: "10" }, "endLine")).not.toThrow(); |
| 13 | expect(() => getNumberArg({ endLine: 1000 }, "endLine")).not.toThrow(); |
| 14 | |
| 15 | // Test missing arguments |
| 16 | expect(() => getNumberArg({}, "startLine")).toThrow( |
| 17 | "Argument `startLine` is required (type number)", |
| 18 | ); |
| 19 | expect(() => getNumberArg({}, "endLine")).toThrow( |
| 20 | "Argument `endLine` is required (type number)", |
| 21 | ); |
| 22 | |
| 23 | // Test invalid arguments |
| 24 | expect(() => |
| 25 | getNumberArg({ startLine: "not-a-number" }, "startLine"), |
| 26 | ).toThrow("Argument `startLine` must be a valid number"); |
| 27 | expect(() => getNumberArg({ endLine: true }, "endLine")).toThrow( |
| 28 | "Argument `endLine` must be a valid number", |
| 29 | ); |
| 30 | }); |
| 31 | |
| 32 | test("readFileRange should handle line number conversion", () => { |
| 33 | // Test that our parsing function converts correctly |
| 34 | expect(getNumberArg({ line: 1 }, "line")).toBe(1); |
| 35 | expect(getNumberArg({ line: "5" }, "line")).toBe(5); |
| 36 | expect(getNumberArg({ line: 10.7 }, "line")).toBe(10); // Should floor |
| 37 | expect(getNumberArg({ line: "15.9" }, "line")).toBe(15); // Should floor |
| 38 | }); |
| 39 | |
| 40 | test("readFileRange parses negative numbers (validation happens in implementation)", () => { |
| 41 | // The parseArgs function should parse negative numbers correctly |
| 42 | // but the implementation will reject them with helpful error messages |
| 43 | expect(getNumberArg({ line: -1 }, "line")).toBe(-1); |
| 44 | expect(getNumberArg({ line: -10 }, "line")).toBe(-10); |
| 45 | expect(getNumberArg({ line: "-5" }, "line")).toBe(-5); |
| 46 | }); |
| 47 | |
| 48 | test("readFileRange handles out-of-bounds ranges gracefully", () => { |
| 49 | // IDE implementations handle out-of-bounds gracefully: |
| 50 | // - VS Code: Uses array.slice() which handles out-of-bounds gracefully |
| 51 | // - IntelliJ: Uses lines.getOrNull() which returns null for out-of-bounds |
| 52 | // Both return empty content or available content without throwing errors |
| 53 | |
| 54 | expect(() => { |
| 55 | const startLine = getNumberArg({ startLine: 1000 }, "startLine"); |
| 56 | const endLine = getNumberArg({ endLine: 2000 }, "endLine"); |
| 57 | expect(startLine).toBe(1000); |
| 58 | expect(endLine).toBe(2000); |
| 59 | }).not.toThrow(); |
| 60 | }); |