|
| 1 | +import os from "os" |
| 2 | + |
| 3 | +// Mock the modules - must be hoisted before imports |
| 4 | +vi.mock("os-name", () => ({ |
| 5 | + default: vi.fn(), |
| 6 | +})) |
| 7 | + |
| 8 | +vi.mock("../../../../utils/shell", () => ({ |
| 9 | + getShell: vi.fn(() => "/bin/bash"), |
| 10 | +})) |
| 11 | + |
| 12 | +import { getSystemInfoSection } from "../system-info" |
| 13 | +import osName from "os-name" |
| 14 | + |
| 15 | +const mockOsName = osName as unknown as ReturnType<typeof vi.fn> |
| 16 | + |
| 17 | +describe("getSystemInfoSection", () => { |
| 18 | + const mockCwd = "/test/workspace" |
| 19 | + const mockHomeDir = "/home/user" |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + vi.spyOn(os, "homedir").mockReturnValue(mockHomeDir) |
| 23 | + vi.spyOn(os, "platform").mockReturnValue("linux" as any) |
| 24 | + vi.spyOn(os, "release").mockReturnValue("5.15.0") |
| 25 | + }) |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + vi.clearAllMocks() |
| 29 | + }) |
| 30 | + |
| 31 | + it("should return system info with os-name when available", () => { |
| 32 | + mockOsName.mockReturnValue("Ubuntu 22.04") |
| 33 | + |
| 34 | + const result = getSystemInfoSection(mockCwd) |
| 35 | + |
| 36 | + expect(result).toContain("Operating System: Ubuntu 22.04") |
| 37 | + expect(result).toContain("Default Shell: /bin/bash") |
| 38 | + expect(result).toContain(`Home Directory: ${mockHomeDir}`) |
| 39 | + expect(result).toContain(`Current Workspace Directory: ${mockCwd}`) |
| 40 | + }) |
| 41 | + |
| 42 | + it("should fallback to platform and release when os-name throws error", () => { |
| 43 | + mockOsName.mockImplementation(() => { |
| 44 | + throw new Error("Command failed with ENOENT: powershell") |
| 45 | + }) |
| 46 | + |
| 47 | + const result = getSystemInfoSection(mockCwd) |
| 48 | + |
| 49 | + expect(result).toContain("Operating System: linux 5.15.0") |
| 50 | + expect(result).toContain("Default Shell: /bin/bash") |
| 51 | + expect(result).toContain(`Home Directory: ${mockHomeDir}`) |
| 52 | + expect(result).toContain(`Current Workspace Directory: ${mockCwd}`) |
| 53 | + }) |
| 54 | + |
| 55 | + it("should handle Windows platform in fallback", () => { |
| 56 | + mockOsName.mockImplementation(() => { |
| 57 | + throw new Error("Command failed with ENOENT: powershell") |
| 58 | + }) |
| 59 | + vi.spyOn(os, "platform").mockReturnValue("win32" as any) |
| 60 | + vi.spyOn(os, "release").mockReturnValue("10.0.19043") |
| 61 | + |
| 62 | + const result = getSystemInfoSection(mockCwd) |
| 63 | + |
| 64 | + expect(result).toContain("Operating System: win32 10.0.19043") |
| 65 | + }) |
| 66 | +}) |
0 commit comments