| 1 | // eslint-disable-next-line import/order |
| 2 | import { vi } from "vitest"; |
| 3 | |
| 4 | vi.mock("node:os", () => ({ |
| 5 | default: { |
| 6 | homedir: vi.fn(), |
| 7 | }, |
| 8 | })); |
| 9 | |
| 10 | vi.mock("node:path", () => ({ |
| 11 | default: { |
| 12 | resolve: vi.fn(), |
| 13 | }, |
| 14 | })); |
| 15 | |
| 16 | import os from "node:os"; |
| 17 | import path from "node:path"; |
| 18 | |
| 19 | import { isInHomeDirectory } from "./isInHomeDirectory.js"; |
| 20 | |
| 21 | describe("isInHomeDirectory", () => { |
| 22 | const originalCwd = process.cwd; |
| 23 | const originalPlatform = process.platform; |
| 24 | |
| 25 | beforeEach(() => { |
| 26 | vi.clearAllMocks(); |
| 27 | process.cwd = vi.fn(); |
| 28 | }); |
| 29 | |
| 30 | afterEach(() => { |
| 31 | process.cwd = originalCwd; |
| 32 | Object.defineProperty(process, "platform", { |
| 33 | value: originalPlatform, |
| 34 | writable: true, |
| 35 | }); |
| 36 | }); |
| 37 | |
| 38 | it("should return true when current directory is home directory on Unix", () => { |
| 39 | Object.defineProperty(process, "platform", { |
| 40 | value: "linux", |
| 41 | writable: true, |
| 42 | }); |
| 43 | |
| 44 | const mockCwd = "/home/user"; |
| 45 | const mockHome = "/home/user"; |
| 46 | |
| 47 | vi.mocked(process.cwd).mockReturnValue(mockCwd); |
| 48 | vi.mocked(os.homedir).mockReturnValue(mockHome); |
| 49 | vi.mocked(path.resolve).mockImplementation((p: string) => p); |
| 50 | |
| 51 | expect(isInHomeDirectory()).toBe(true); |
| 52 | }); |
| 53 | |
| 54 | it("should return false when current directory is not home directory on Unix", () => { |
| 55 | Object.defineProperty(process, "platform", { |
| 56 | value: "linux", |
| 57 | writable: true, |
| 58 | }); |
| 59 | |
| 60 | const mockCwd = "/home/user/projects"; |
| 61 | const mockHome = "/home/user"; |
| 62 | |
| 63 | vi.mocked(process.cwd).mockReturnValue(mockCwd); |
| 64 | vi.mocked(os.homedir).mockReturnValue(mockHome); |
| 65 | vi.mocked(path.resolve).mockImplementation((p: string) => p); |
| 66 | |
| 67 | expect(isInHomeDirectory()).toBe(false); |
| 68 | }); |
| 69 | |
| 70 | it("should return true when current directory is home directory on Windows (case insensitive)", () => { |
| 71 | Object.defineProperty(process, "platform", { |
| 72 | value: "win32", |
| 73 | writable: true, |
| 74 | }); |
| 75 | |
| 76 | const mockCwd = "C:\\Users\\User"; |
| 77 | const mockHome = "c:\\users\\user"; |
| 78 | |
| 79 | vi.mocked(process.cwd).mockReturnValue(mockCwd); |
| 80 | vi.mocked(os.homedir).mockReturnValue(mockHome); |
| 81 | vi.mocked(path.resolve).mockImplementation((p: string) => p); |
| 82 | |
| 83 | expect(isInHomeDirectory()).toBe(true); |
| 84 | }); |
| 85 | }); |