diff --git a/src/core/prompts/sections/__tests__/system-info.spec.ts b/src/core/prompts/sections/__tests__/system-info.spec.ts new file mode 100644 index 00000000000..749b53a0fd1 --- /dev/null +++ b/src/core/prompts/sections/__tests__/system-info.spec.ts @@ -0,0 +1,66 @@ +import os from "os" + +// Mock the modules - must be hoisted before imports +vi.mock("os-name", () => ({ + default: vi.fn(), +})) + +vi.mock("../../../../utils/shell", () => ({ + getShell: vi.fn(() => "/bin/bash"), +})) + +import { getSystemInfoSection } from "../system-info" +import osName from "os-name" + +const mockOsName = osName as unknown as ReturnType + +describe("getSystemInfoSection", () => { + const mockCwd = "/test/workspace" + const mockHomeDir = "/home/user" + + beforeEach(() => { + vi.spyOn(os, "homedir").mockReturnValue(mockHomeDir) + vi.spyOn(os, "platform").mockReturnValue("linux" as any) + vi.spyOn(os, "release").mockReturnValue("5.15.0") + }) + + afterEach(() => { + vi.clearAllMocks() + }) + + it("should return system info with os-name when available", () => { + mockOsName.mockReturnValue("Ubuntu 22.04") + + const result = getSystemInfoSection(mockCwd) + + expect(result).toContain("Operating System: Ubuntu 22.04") + expect(result).toContain("Default Shell: /bin/bash") + expect(result).toContain(`Home Directory: ${mockHomeDir}`) + expect(result).toContain(`Current Workspace Directory: ${mockCwd}`) + }) + + it("should fallback to platform and release when os-name throws error", () => { + mockOsName.mockImplementation(() => { + throw new Error("Command failed with ENOENT: powershell") + }) + + const result = getSystemInfoSection(mockCwd) + + expect(result).toContain("Operating System: linux 5.15.0") + expect(result).toContain("Default Shell: /bin/bash") + expect(result).toContain(`Home Directory: ${mockHomeDir}`) + expect(result).toContain(`Current Workspace Directory: ${mockCwd}`) + }) + + it("should handle Windows platform in fallback", () => { + mockOsName.mockImplementation(() => { + throw new Error("Command failed with ENOENT: powershell") + }) + vi.spyOn(os, "platform").mockReturnValue("win32" as any) + vi.spyOn(os, "release").mockReturnValue("10.0.19043") + + const result = getSystemInfoSection(mockCwd) + + expect(result).toContain("Operating System: win32 10.0.19043") + }) +}) diff --git a/src/core/prompts/sections/system-info.ts b/src/core/prompts/sections/system-info.ts index 8adc90a160e..486e46ee232 100644 --- a/src/core/prompts/sections/system-info.ts +++ b/src/core/prompts/sections/system-info.ts @@ -4,11 +4,22 @@ import osName from "os-name" import { getShell } from "../../../utils/shell" export function getSystemInfoSection(cwd: string): string { + // Try to get detailed OS name, fall back to basic info if it fails + let osInfo: string + try { + osInfo = osName() + } catch (error) { + // Fallback when os-name fails (e.g., PowerShell not available on Windows) + const platform = os.platform() + const release = os.release() + osInfo = `${platform} ${release}` + } + let details = `==== SYSTEM INFORMATION -Operating System: ${osName()} +Operating System: ${osInfo} Default Shell: ${getShell()} Home Directory: ${os.homedir().toPosix()} Current Workspace Directory: ${cwd.toPosix()}