fixes tests for pathSelector

This commit is contained in:
ThatBen 2025-04-02 14:45:23 +02:00
parent 2b4fd7f456
commit 3dc5a38f57
No known key found for this signature in database
GPG Key ID: E020A7DDCD52E1AB
2 changed files with 101 additions and 85 deletions

View File

@ -12,11 +12,6 @@ export class PathSelector {
this.startingPath = startingPath;
this.pathMustExist = pathMustExist;
this.roots = this.fs.getAvailableRoots();
console.log("Roots: " + this.roots.length);
this.roots.forEach(function (root) {
console.log("Root: " + root);
});
this.currentPath = this.splitPath(startingPath);
if (!this.hasValidRoot(this.currentPath)) {
this.currentPath = [this.roots[0]];
@ -58,7 +53,7 @@ export class PathSelector {
};
splitPath = (str) => {
var result = str.replaceAll("\\", "/").split("/");
var result = this.dropEmptyParts(str.replaceAll("\\", "/").split("/"));
if (str.startsWith("/") && this.roots.includes("/")) {
result = ["/", ...result];
}
@ -66,19 +61,16 @@ export class PathSelector {
};
dropEmptyParts = (parts) => {
var result = [];
parts.forEach(function (part) {
if (part.length > 0) {
result.push(part);
}
});
return result;
return parts.filter(part => part.length > 0);
};
combine = (parts) => {
const toJoin = this.dropEmptyParts(parts);
if (toJoin.length == 1) return toJoin[0];
const result = this.fs.pathJoin(toJoin);
var result = this.fs.pathJoin(toJoin);
if (result.startsWith("//")) {
result = result.substring(1);
}
return result;
};

View File

@ -28,7 +28,7 @@ describe("PathSelector", () => {
it("initializes path selection with given path", async () => {
await pathSelector.show(mockStartPath, true);
expect(mockFsService.getAvailableRoots).toHaveBeenCalled();
expect(pathSelector.currentPath).toEqual(["home", "user"]);
expect(pathSelector.currentPath).toEqual(["/", "home", "user"]);
});
it("uses first root if starting path is invalid", async () => {
@ -48,90 +48,114 @@ describe("PathSelector", () => {
});
});
// describe("path operations", () => {
// beforeEach(async () => {
// await pathSelector.show(mockStartPath, true);
// });
describe("path operations", () => {
beforeEach(async () => {
await pathSelector.show(mockStartPath, true);
});
// it("splits paths correctly", () => {
// const result = pathSelector.splitPath("C:\\path\\to\\dir");
// expect(result).toEqual(["C:", "path", "to", "dir"]);
// });
it("splits paths correctly", () => {
const result = pathSelector.splitPath("C:\\path\\to\\dir");
expect(result).toEqual(["C:", "path", "to", "dir"]);
});
// it("drops empty path parts", () => {
// const result = pathSelector.dropEmptyParts(["", "path", "", "dir", ""]);
// expect(result).toEqual(["path", "dir"]);
// });
it("drops empty path parts", () => {
const result = pathSelector.dropEmptyParts(["", "path", "", "dir", ""]);
expect(result).toEqual(["path", "dir"]);
});
// it("combines path parts correctly", () => {
// const result = pathSelector.combine(["home", "user", "docs"]);
// expect(result).toBe("home/user/docs");
// });
it("combines path parts correctly", () => {
const result = pathSelector.combine(["C:", "user", "docs"]);
expect(result).toBe("C:/user/docs");
});
// it("handles single part paths in combine", () => {
// const result = pathSelector.combine(["root"]);
// expect(result).toBe("root");
// });
// });
it("combines path including root correctly", () => {
const result = pathSelector.combine(["/", "home", "user", "docs"]);
expect(result).toBe("/home/user/docs");
});
// describe("navigation", () => {
// beforeEach(async () => {
// await pathSelector.show(mockStartPath, true);
// });
it("handles single part paths in combine", () => {
const result = pathSelector.combine(["root"]);
expect(result).toBe("root");
});
});
// it("moves up one directory", () => {
// pathSelector.upOne();
// expect(pathSelector.currentPath).toEqual(["home"]);
// });
describe("navigation", () => {
beforeEach(async () => {
await pathSelector.show(mockStartPath, true);
});
// it("handles down directory navigation", async () => {
// mockFsService.readDir.mockReturnValue(["subdir1", "subdir2"]);
// mockFsService.isDir.mockReturnValue(true);
it("moves up one directory", () => {
pathSelector.upOne();
expect(pathSelector.currentPath).toEqual(["/", "home"]);
});
it("shows down directory navigation", async () => {
mockFsService.readDir.mockReturnValue(["subdir1", "subdir2"]);
mockFsService.isDir.mockReturnValue(true);
// await pathSelector.downOne();
await pathSelector.downOne();
// expect(mockUiService.askMultipleChoice).toHaveBeenCalled();
// expect(mockFsService.readDir).toHaveBeenCalled();
// });
expect(mockUiService.askMultipleChoice).toHaveBeenCalled();
expect(mockFsService.readDir).toHaveBeenCalledWith(mockStartPath);
});
// it("creates new subdirectory", async () => {
// mockUiService.askPrompt.mockResolvedValue("newdir");
// await pathSelector.createSubDir();
it("can navigate to a subdirectory", async () => {
const subdir = "subdir1";
mockFsService.readDir.mockReturnValue([subdir]);
mockUiService.askMultipleChoice.mockImplementation((_, options) => {
options[0].action(); // Select the first option
});
await pathSelector.downOne();
// expect(mockFsService.makeDir).toHaveBeenCalled();
// expect(pathSelector.currentPath).toEqual(["home", "user", "newdir"]);
// });
// });
expect(pathSelector.currentPath).toEqual(["/", "home", "user", subdir]);
});
// describe("path validation", () => {
// it("validates root paths", () => {
// expect(pathSelector.hasValidRoot(["/home"])).toBe(true);
// expect(pathSelector.hasValidRoot([])).toBe(false);
// expect(pathSelector.hasValidRoot(["invalid"])).toBe(false);
// });
it("creates new subdirectory", async () => {
const newDir = "newdir";
mockUiService.askPrompt.mockResolvedValue(newDir);
await pathSelector.createSubDir();
expect(mockUiService.askPrompt).toHaveBeenCalledWith("Enter name:");
expect(mockFsService.makeDir).toHaveBeenCalled(mockStartPath + "/" + newDir);
expect(pathSelector.currentPath).toEqual(["/", "home", "user", newDir]);
});
});
// it("validates full paths", () => {
// mockFsService.isDir.mockReturnValue(false);
// pathSelector.updateCurrentIfValidFull("/invalid/path");
// expect(mockUiService.showErrorMessage).toHaveBeenCalled();
// });
// });
describe("path validation", () => {
beforeEach(async () => {
await pathSelector.show(mockStartPath, true);
});
// describe("selection and cancellation", () => {
// beforeEach(async () => {
// await pathSelector.show(mockStartPath, true);
// });
it("validates root paths", () => {
expect(pathSelector.hasValidRoot(["/home"])).toBe(true);
expect(pathSelector.hasValidRoot([])).toBe(false);
expect(pathSelector.hasValidRoot(["invalid"])).toBe(false);
});
// it("selects current path", async () => {
// await pathSelector.selectThisPath();
// expect(pathSelector.resultingPath).toBe("home/user");
// expect(mockMenuLoop.stopLoop).toHaveBeenCalled();
// });
it("validates full paths", () => {
mockFsService.isDir.mockReturnValue(false);
pathSelector.updateCurrentIfValidFull("/invalid/path");
expect(mockUiService.showErrorMessage).toHaveBeenCalledWith("The path does not exist.");
});
});
// it("cancels and returns to starting path", async () => {
// await pathSelector.cancel();
// expect(pathSelector.resultingPath).toBe(mockStartPath);
// expect(mockMenuLoop.stopLoop).toHaveBeenCalled();
// });
// });
describe("selection and cancellation", () => {
beforeEach(async () => {
await pathSelector.show(mockStartPath, true);
});
it("selects current path", async () => {
pathSelector.upOne();
await pathSelector.selectThisPath();
expect(pathSelector.resultingPath).toBe("/home");
expect(mockMenuLoop.stopLoop).toHaveBeenCalled();
});
it("cancels and returns to starting path", async () => {
pathSelector.upOne();
await pathSelector.cancel();
expect(pathSelector.resultingPath).toBe(mockStartPath);
expect(mockMenuLoop.stopLoop).toHaveBeenCalled();
});
});
});