Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/core/prompts/sections/__tests__/system-info.spec.ts
Original file line number Diff line number Diff line change
@@ -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<typeof vi.fn>

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")
})
})
13 changes: 12 additions & 1 deletion src/core/prompts/sections/system-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}
Expand Down
Loading