| 1 | // Generated by continue |
| 2 | import { |
| 3 | pathToUriPathSegment, |
| 4 | findUriInDirs, |
| 5 | getUriPathBasename, |
| 6 | getUriFileExtension, |
| 7 | getFileExtensionFromBasename, |
| 8 | getLastNUriRelativePathParts, |
| 9 | joinPathsToUri, |
| 10 | getShortestUniqueRelativeUriPaths, |
| 11 | getLastNPathParts, |
| 12 | } from "./uri"; |
| 13 | |
| 14 | describe("uri utils", () => { |
| 15 | describe("pathToUriPathSegment", () => { |
| 16 | it("should convert Windows paths to URI segments", () => { |
| 17 | expect(pathToUriPathSegment("\\path\\to\\folder\\")).toBe( |
| 18 | "path/to/folder", |
| 19 | ); |
| 20 | expect(pathToUriPathSegment("\\this\\is\\afile.ts")).toBe( |
| 21 | "this/is/afile.ts", |
| 22 | ); |
| 23 | }); |
| 24 | |
| 25 | it("should clean Unix paths", () => { |
| 26 | expect(pathToUriPathSegment("/path/to/folder/")).toBe("path/to/folder"); |
| 27 | expect(pathToUriPathSegment("is/already/clean")).toBe("is/already/clean"); |
| 28 | }); |
| 29 | |
| 30 | it("should encode URI special characters", () => { |
| 31 | expect(pathToUriPathSegment("path/with spaces/file")).toBe( |
| 32 | "path/with%20spaces/file", |
| 33 | ); |
| 34 | expect(pathToUriPathSegment("special#chars?in&path")).toBe( |
| 35 | "special%23chars%3Fin%26path", |
| 36 | ); |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | describe("findUriInDirs", () => { |
| 41 | const dirUris = [ |
| 42 | "file:///workspace/project1", |
| 43 | "file:///workspace/project2", |
| 44 | ]; |
| 45 | |
| 46 | it("should find URI in directory", () => { |
| 47 | const result = findUriInDirs( |
| 48 | "file:///workspace/project1/src/file.ts", |
| 49 | dirUris, |
| 50 | ); |
| 51 | expect(result).toEqual({ |
| 52 | uri: "file:///workspace/project1/src/file.ts", |
| 53 | relativePathOrBasename: "src/file.ts", |
| 54 | foundInDir: "file:///workspace/project1", |
| 55 | }); |
| 56 | }); |
| 57 | |
| 58 | it("should return basename when URI not in directories", () => { |
| 59 | const result = findUriInDirs("file:///other/location/file.ts", dirUris); |
| 60 | expect(result).toEqual({ |
| 61 | uri: "file:///other/location/file.ts", |
| 62 | relativePathOrBasename: "file.ts", |
| 63 | foundInDir: null, |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | it("should throw error for invalid URIs", () => { |
| 68 | expect(() => findUriInDirs("invalid-uri", dirUris)).toThrow( |
| 69 | "Invalid uri: invalid-uri", |
| 70 | ); |
| 71 | }); |
| 72 | }); |
| 73 | |
| 74 | describe("URI path operations", () => { |
| 75 | it("should get URI path basename", () => { |
| 76 | expect(getUriPathBasename("file:///path/to/file.txt")).toBe("file.txt"); |
| 77 | expect(getUriPathBasename("file:///path/to/folder/")).toBe("folder"); |
| 78 | }); |
| 79 | |
| 80 | it("should get URI file extension", () => { |
| 81 | expect(getUriFileExtension("file:///path/to/file.TXT")).toBe("txt"); |
| 82 | expect(getUriFileExtension("file:///path/to/file")).toBe(""); |
| 83 | }); |
| 84 | |
| 85 | it("should get file extension from basename", () => { |
| 86 | expect(getFileExtensionFromBasename("file.txt")).toBe("txt"); |
| 87 | expect(getFileExtensionFromBasename("file")).toBe(""); |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | describe("joinPathsToUri", () => { |
| 92 | it("should join paths to base URI", () => { |
| 93 | expect(joinPathsToUri("file:///base", "path", "to", "file.txt")).toBe( |
| 94 | "file:///base/path/to/file.txt", |
| 95 | ); |
| 96 | }); |
| 97 | |
| 98 | it("should handle paths with special characters", () => { |
| 99 | expect( |
| 100 | joinPathsToUri("file:///base", "path with spaces", "file.txt"), |
| 101 | ).toBe("file:///base/path%20with%20spaces/file.txt"); |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | describe("getShortestUniqueRelativeUriPaths", () => { |
| 106 | it("should find shortest unique paths", () => { |
| 107 | const uris = [ |
| 108 | "file:///workspace/project1/src/components/Button.tsx", |
| 109 | "file:///workspace/project1/src/utils/Button.tsx", |
| 110 | ]; |
| 111 | const dirUris = ["file:///workspace/project1"]; |
| 112 | |
| 113 | const result = getShortestUniqueRelativeUriPaths(uris, dirUris); |
| 114 | expect(result).toEqual([ |
| 115 | { |
| 116 | uri: "file:///workspace/project1/src/components/Button.tsx", |
| 117 | uniquePath: "components/Button.tsx", |
| 118 | }, |
| 119 | { |
| 120 | uri: "file:///workspace/project1/src/utils/Button.tsx", |
| 121 | uniquePath: "utils/Button.tsx", |
| 122 | }, |
| 123 | ]); |
| 124 | }); |
| 125 | }); |
| 126 | |
| 127 | describe("getLastNPathParts", () => { |
| 128 | it("should get last N parts of path", () => { |
| 129 | expect(getLastNPathParts("path/to/some/file.txt", 2)).toBe( |
| 130 | "some/file.txt", |
| 131 | ); |
| 132 | expect(getLastNPathParts("path/to/some/file.txt", 0)).toBe(""); |
| 133 | }); |
| 134 | |
| 135 | it("should handle Windows paths", () => { |
| 136 | expect(getLastNPathParts("path\\to\\some\\file.txt", 2)).toBe( |
| 137 | "some/file.txt", |
| 138 | ); |
| 139 | }); |
| 140 | }); |
| 141 | |
| 142 | describe("getLastNUriRelativePathParts", () => { |
| 143 | it("should get last N parts of URI relative path", () => { |
| 144 | const dirUris = ["file:///workspace/project1"]; |
| 145 | const uri = "file:///workspace/project1/src/components/Button.tsx"; |
| 146 | |
| 147 | expect(getLastNUriRelativePathParts(dirUris, uri, 2)).toBe( |
| 148 | "components/Button.tsx", |
| 149 | ); |
| 150 | }); |
| 151 | |
| 152 | it("should handle URI not in directories", () => { |
| 153 | const dirUris = ["file:///workspace/project1"]; |
| 154 | const uri = "file:///other/path/file.txt"; |
| 155 | |
| 156 | expect(getLastNUriRelativePathParts(dirUris, uri, 2)).toBe("file.txt"); |
| 157 | }); |
| 158 | }); |
| 159 | }); |