From 59918b3207a3ced4e7ddecce81f1d3e16a10d67f Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Mon, 16 Dec 2024 19:22:32 -0800 Subject: [PATCH] uri util tests --- core/util/uri.test.ts | 230 +++++++++++++++++++++++++++++++----------- core/util/uri.ts | 50 +++++---- 2 files changed, 203 insertions(+), 77 deletions(-) diff --git a/core/util/uri.test.ts b/core/util/uri.test.ts index 016fd0668..9e2471054 100644 --- a/core/util/uri.test.ts +++ b/core/util/uri.test.ts @@ -1,67 +1,181 @@ -import { TEST_DIR } from "../test/testDir"; -import { getLastNPathParts, getUriPathBasename } from "./uri"; +// Generated by continue +import * as URI from "uri-js"; +import { + pathToUriPathSegment, + findUriInDirs, + relativePathOrUriToUri, + getUriPathBasename, + getUriFileExtension, + getFileExtensionFromBasename, + getLastNUriRelativePathParts, + joinPathsToUri, + getShortestUniqueRelativeUriPaths, + getLastNPathParts, +} from "./uri"; -describe("getUriPathBasename", () => { - it("should return the base name of a Unix-style path", () => { - const filepath = "/home/user/documents/file.txt"; - const output = getUriPathBasename(filepath); - expect(output).toBe("file.txt"); +describe("uri utils", () => { + describe("pathToUriPathSegment", () => { + it("should convert Windows paths to URI segments", () => { + expect(pathToUriPathSegment("\\path\\to\\folder\\")).toBe( + "path/to/folder", + ); + expect(pathToUriPathSegment("\\this\\is\\afile.ts")).toBe( + "this/is/afile.ts", + ); + }); + + it("should clean Unix paths", () => { + expect(pathToUriPathSegment("/path/to/folder/")).toBe("path/to/folder"); + expect(pathToUriPathSegment("is/already/clean")).toBe("is/already/clean"); + }); + + it("should encode URI special characters", () => { + expect(pathToUriPathSegment("path/with spaces/file")).toBe( + "path/with%20spaces/file", + ); + expect(pathToUriPathSegment("special#chars?in&path")).toBe( + "special%23chars%3Fin%26path", + ); + }); }); - it("should return the base name of a Windows-style path", () => { - const filepath = "C:\\Users\\User\\Documents\\file.txt"; - const output = getUriPathBasename(filepath); - expect(output).toBe("file.txt"); + describe("findUriInDirs", () => { + const dirUris = [ + "file:///workspace/project1", + "file:///workspace/project2", + ]; + + it("should find URI in directory", () => { + const result = findUriInDirs( + "file:///workspace/project1/src/file.ts", + dirUris, + ); + expect(result).toEqual({ + uri: "file:///workspace/project1/src/file.ts", + relativePathOrBasename: "src/file.ts", + foundInDir: "file:///workspace/project1", + }); + }); + + it("should return basename when URI not in directories", () => { + const result = findUriInDirs("file:///other/location/file.ts", dirUris); + expect(result).toEqual({ + uri: "file:///other/location/file.ts", + relativePathOrBasename: "file.ts", + foundInDir: null, + }); + }); + + it("should throw error for invalid URIs", () => { + expect(() => findUriInDirs("invalid-uri", dirUris)).toThrow( + "Invalid uri: invalid-uri", + ); + }); }); - it("should handle paths with mixed separators", () => { - const filepath = "C:/Users\\User/Documents/file.txt"; - const output = getUriPathBasename(filepath); - expect(output).toBe("file.txt"); + describe("URI path operations", () => { + it("should get URI path basename", () => { + expect(getUriPathBasename("file:///path/to/file.txt")).toBe("file.txt"); + expect(getUriPathBasename("file:///path/to/folder/")).toBe("folder"); + }); + + it("should get URI file extension", () => { + expect(getUriFileExtension("file:///path/to/file.TXT")).toBe("txt"); + expect(getUriFileExtension("file:///path/to/file")).toBe(""); + }); + + it("should get file extension from basename", () => { + expect(getFileExtensionFromBasename("file.txt")).toBe("txt"); + expect(getFileExtensionFromBasename("file")).toBe(""); + }); }); - it("should return an empty string for empty input", () => { - const filepath = ""; - const output = getUriPathBasename(filepath); - expect(output).toBe(""); - }); -}); - -describe("getLastNPathParts", () => { - test("returns the last N parts of a filepath with forward slashes", () => { - const filepath = "home/user/documents/project/file.txt"; - expect(getLastNPathParts(filepath, 2)).toBe("project/file.txt"); - }); - - test("returns the last N parts of a filepath with backward slashes", () => { - const filepath = "C:\\home\\user\\documents\\project\\file.txt"; - expect(getLastNPathParts(filepath, 3)).toBe("documents/project/file.txt"); - }); - - test("returns the last part if N is 1", () => { - const filepath = "/home/user/documents/project/file.txt"; - expect(getLastNPathParts(filepath, 1)).toBe("file.txt"); - }); - - test("returns the entire path if N is greater than the number of parts", () => { - const filepath = "home/user/documents/project/file.txt"; - expect(getLastNPathParts(filepath, 10)).toBe( - "home/user/documents/project/file.txt", - ); - }); - - test("returns an empty string if N is 0", () => { - const filepath = "home/user/documents/project/file.txt"; - expect(getLastNPathParts(filepath, 0)).toBe(""); - }); - - test("handles paths with mixed forward and backward slashes", () => { - const filepath = "home\\user/documents\\project/file.txt"; - expect(getLastNPathParts(filepath, 3)).toBe("documents/project/file.txt"); - }); - - test("handles edge case with empty filepath", () => { - const filepath = ""; - expect(getLastNPathParts(filepath, 2)).toBe(""); + describe("joinPathsToUri", () => { + it("should join paths to base URI", () => { + expect(joinPathsToUri("file:///base", "path", "to", "file.txt")).toBe( + "file:///base/path/to/file.txt", + ); + }); + + it("should handle paths with special characters", () => { + expect( + joinPathsToUri("file:///base", "path with spaces", "file.txt"), + ).toBe("file:///base/path%20with%20spaces/file.txt"); + }); + }); + + describe("getShortestUniqueRelativeUriPaths", () => { + it("should find shortest unique paths", () => { + const uris = [ + "file:///workspace/project1/src/components/Button.tsx", + "file:///workspace/project1/src/utils/Button.tsx", + ]; + const dirUris = ["file:///workspace/project1"]; + + const result = getShortestUniqueRelativeUriPaths(uris, dirUris); + expect(result).toEqual([ + { + uri: "file:///workspace/project1/src/components/Button.tsx", + uniquePath: "components/Button.tsx", + }, + { + uri: "file:///workspace/project1/src/utils/Button.tsx", + uniquePath: "utils/Button.tsx", + }, + ]); + }); + }); + + describe("getLastNPathParts", () => { + it("should get last N parts of path", () => { + expect(getLastNPathParts("path/to/some/file.txt", 2)).toBe( + "some/file.txt", + ); + expect(getLastNPathParts("path/to/some/file.txt", 0)).toBe(""); + }); + + it("should handle Windows paths", () => { + expect(getLastNPathParts("path\\to\\some\\file.txt", 2)).toBe( + "some/file.txt", + ); + }); + }); + + describe("relativePathOrUriToUri", () => { + const consoleSpy = jest.spyOn(console, "trace").mockImplementation(); + + afterEach(() => { + consoleSpy.mockClear(); + }); + + it("should return original URI if scheme exists", () => { + const uri = "file:///path/to/file.txt"; + expect(relativePathOrUriToUri(uri, "file:///base")).toBe(uri); + }); + + it("should convert relative path to URI", () => { + expect(relativePathOrUriToUri("path/to/file.txt", "file:///base")).toBe( + "file:///base/path/to/file.txt", + ); + expect(consoleSpy).toHaveBeenCalledWith("Received path with no scheme"); + }); + }); + + describe("getLastNUriRelativePathParts", () => { + it("should get last N parts of URI relative path", () => { + const dirUris = ["file:///workspace/project1"]; + const uri = "file:///workspace/project1/src/components/Button.tsx"; + + expect(getLastNUriRelativePathParts(dirUris, uri, 2)).toBe( + "components/Button.tsx", + ); + }); + + it("should handle URI not in directories", () => { + const dirUris = ["file:///workspace/project1"]; + const uri = "file:///other/path/file.txt"; + + expect(getLastNUriRelativePathParts(dirUris, uri, 2)).toBe("file.txt"); + }); }); }); diff --git a/core/util/uri.ts b/core/util/uri.ts index f4f62e5c4..49a9e8389 100644 --- a/core/util/uri.ts +++ b/core/util/uri.ts @@ -5,6 +5,7 @@ import * as URI from "uri-js"; \this\is\afile.ts -> this/is/afile.ts is/already/clean -> is/already/clean **/ + export function pathToUriPathSegment(path: string) { let clean = path.replace(/[\\]/g, "/"); // backslashes -> forward slashes clean = clean.replace(/^\//, ""); // remove start slash @@ -15,6 +16,14 @@ export function pathToUriPathSegment(path: string) { .join("/"); } +export function getCleanUriPath(uri: string) { + const path = URI.parse(uri).path; + if (!path) { + return ""; + } + return pathToUriPathSegment(path); +} + export function findUriInDirs( uri: string, dirUriCandidates: string[], @@ -97,7 +106,16 @@ export function relativePathOrUriToUri( Returns just the file or folder name of a URI */ export function getUriPathBasename(uri: string): string { - return URI.parse(uri).path?.split("/")?.pop() || ""; + const cleanPath = getCleanUriPath(uri); + return cleanPath.split("/")?.pop() || ""; +} + +export function getFileExtensionFromBasename(basename: string) { + const parts = basename.split("."); + if (parts.length < 2) { + return ""; + } + return (parts.slice(-1)[0] ?? "").toLowerCase(); } /* @@ -105,11 +123,7 @@ export function getUriPathBasename(uri: string): string { */ export function getUriFileExtension(uri: string) { const baseName = getUriPathBasename(uri); - return (baseName.split(".").slice(-1)[0] ?? "").toLowerCase(); -} - -export function getFileExtensionFromBasename(filename: string) { - return filename.split(".").pop() ?? ""; + return getFileExtensionFromBasename(baseName); } export function getLastNUriRelativePathParts( @@ -139,14 +153,14 @@ export function getShortestUniqueRelativeUriPaths( const segmentCombinationsMap = new Map(); const segmentsInfo = uris.map((uri) => { const { relativePathOrBasename } = findUriInDirs(uri, dirUriCandidates); - const segments = relativePathOrBasename.split("/"); + const cleanPath = pathToUriPathSegment(relativePathOrBasename); + const segments = cleanPath.split("/"); const suffixes: string[] = []; - // Generate all possible suffix combinations + // Generate all possible suffix combinations, starting from the shortest (basename) for (let i = segments.length - 1; i >= 0; i--) { const suffix = segments.slice(i).join("/"); - suffixes.unshift(suffix); - + suffixes.push(suffix); // Now pushing in order from shortest to longest // Count occurrences of each suffix segmentCombinationsMap.set( suffix, @@ -154,20 +168,18 @@ export function getShortestUniqueRelativeUriPaths( ); } - return { uri, segments, suffixes, relativePathOrBasename }; + return { uri, segments, suffixes, cleanPath }; }); - // Find shortest unique path for each URI - return segmentsInfo.map(({ uri, suffixes, relativePathOrBasename }) => { - // Find the first (shortest) unique suffix - const uniquePath = + return segmentsInfo.map(({ uri, suffixes, cleanPath }) => { + // Since suffixes are now ordered from shortest to longest, + // the first unique one we find will be the shortest + const uniqueCleanPath = suffixes.find((suffix) => segmentCombinationsMap.get(suffix) === 1) ?? - relativePathOrBasename; // case where not unique - perhaps basename - - return { uri, uniquePath }; + cleanPath; // fallback to full path if no unique suffix found + return { uri, uniquePath: decodeURIComponent(uniqueCleanPath) }; }); } - // Only used when working with system paths and relative paths // Since doesn't account for URI segements before workspace export function getLastNPathParts(filepath: string, n: number): string {