Skip to content

test: added direct unit tests for app/cli/local_llm/hardware.py#1187

Merged
VaibhavUpreti merged 2 commits intoTracer-Cloud:mainfrom
umeraamir09:issue/905-test-hardware-detection-coverage
May 3, 2026
Merged

test: added direct unit tests for app/cli/local_llm/hardware.py#1187
VaibhavUpreti merged 2 commits intoTracer-Cloud:mainfrom
umeraamir09:issue/905-test-hardware-detection-coverage

Conversation

@umeraamir09
Copy link
Copy Markdown
Contributor

Fixes #905

Describe the changes you have made in this PR -

Add direct unit tests for _get_total_ram_gb(), _get_available_ram_gb(), and detect_hardware() in app/cli/local_llm/hardware.py.

  • 24 tests across Linux /proc/meminfo parsing, macOS sysctl parsing, and unknown platform fallbacks
  • Covers success paths, command/file-read failures, parse errors, missing keys, and non-integer output
  • detect_hardware() tests verify profile assembly: Apple Silicon, macOS Intel, Linux with/without NVIDIA, and fallback RAM propagation
  • NVIDIA detection via shutil.which("nvidia-smi") explicitly verified
  • All tests are fully mocked with sys.platform patches — zero host OS dependency

Demo/Screenshot for feature changes and bug fixes -

image

Code Understanding and AI Usage

Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?

  • No, I wrote all the code myself
  • Yes, I used AI assistance (continue below)
    If you used AI assistance:
  • I have reviewed every single line of the AI-generated code
  • I can explain the purpose and logic of each function/component I added
  • I have tested edge cases and understand how the code handles them
  • I have modified the AI output to follow this project's coding standards and conventions
    Explain your implementation approach:
    This test covers the three hardware detection functions in isolation by patching sys.platform, subprocess.check_output, builtins.open, platform.machine, and shutil.which. Each test class focuses on one function on one platform (Linux, macOS, or unknown), with helpers like _call() and _detect() reducing boilerplate. The detect_hardware() tests mock the lower-level RAM functions to verify profile assembly logic independently. Fallback behavior (8 GB total, total * 0.5 available) is tested on every failure mode — file I/O errors, parse failures, missing keys, subprocess failures, and unrecognized platforms.

Checklist before requesting a review

  • I have added proper PR title and linked to the issue
  • I have performed a self-review of my code
  • I can explain the purpose of every function, class, and logic block I added
  • I understand why my changes work and have tested them thoroughly
  • I have considered potential edge cases and how my code handles them
  • If it is a core feature, I have added thorough tests
  • My code follows the project's style guidelines and conventions

Note: Please check Allow edits from maintainers if you would like us to assist in the PR.

@umeraamir09 umeraamir09 changed the title Added direct unit tests for app/cli/local_llm/hardware.py test: added direct unit tests for app/cli/local_llm/hardware.py May 1, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 1, 2026

Greptile Summary

This PR adds 24 unit tests for _get_total_ram_gb(), _get_available_ram_gb(), and detect_hardware() in app/cli/local_llm/hardware.py. Tests cover Linux /proc/meminfo parsing, macOS sysctl calls, unknown-platform fallbacks, parse errors, missing keys, and full profile assembly — all with zero host-OS dependency via sys.platform patching.

Confidence Score: 5/5

Safe to merge — only a minor style nit on a test helper, no logic issues.

All findings are P2. The tests are logically correct, mock targets are accurate, fallback values and arithmetic match the implementation, and the iteration pattern used with mock_open is fully supported in Python 3.11+.

No files require special attention.

Important Files Changed

Filename Overview
tests/cli/test_local_llm_hardware.py Adds 24 unit tests across 8 test classes for _get_total_ram_gb, _get_available_ram_gb, and detect_hardware; all paths correctly mocked; minor missing return type on _detect helper

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[detect_hardware] --> B[_get_total_ram_gb]
    A --> C["_get_available_ram_gb(total)"]
    A --> D[platform.machine]
    A --> E["shutil.which('nvidia-smi')"]

    B --> B1{sys.platform?}
    B1 -->|darwin| B2["sysctl -n hw.memsize"]
    B1 -->|linux| B3["read /proc/meminfo MemTotal"]
    B1 -->|other or error| B4["fallback = 8.0 GB"]

    C --> C1{sys.platform?}
    C1 -->|darwin| C2["sysctl -n hw.usermem"]
    C1 -->|linux| C3["read /proc/meminfo MemAvailable"]
    C1 -->|other or error| C4["total * 0.5"]

    A --> F[HardwareProfile]
    F --> F1[total_ram_gb]
    F --> F2[available_ram_gb]
    F --> F3[arch]
    F --> F4[is_apple_silicon]
    F --> F5[has_nvidia_gpu]
Loading

Reviews (1): Last reviewed commit: "Test: Add direct tests for hardware dete..." | Re-trigger Greptile

Comment on lines +253 to +261


# ---------------------------------------------------------------------------
# _get_available_ram_gb — unknown / Windows platform
# ---------------------------------------------------------------------------


class TestGetAvailableRamGbUnknownPlatform:
def test_returns_half_total_on_unrecognised_platform(self) -> None:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Missing return type annotation on _detect helper

The code style guide requires type hints on all function parameters and return types, but _detect has no return annotation. The return type is HardwareProfile.

Suggested change
# ---------------------------------------------------------------------------
# _get_available_ram_gb — unknown / Windows platform
# ---------------------------------------------------------------------------
class TestGetAvailableRamGbUnknownPlatform:
def test_returns_half_total_on_unrecognised_platform(self) -> None:
def _detect(
self,
*,
platform_str: str,
machine: str,
total_gb: float,
available_gb: float,
nvidia: bool,
) -> "HardwareProfile":

Context Used: Code style and conventions (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!


from __future__ import annotations

import sys
@muddlebee
Copy link
Copy Markdown
Collaborator

@umeraamir09 CI is failing

@Devesh36
Copy link
Copy Markdown
Collaborator

Devesh36 commented May 3, 2026

Hey @umeraamir09 could you please resolve the test which are failing . Thankss !!

Copy link
Copy Markdown
Member

@VaibhavUpreti VaibhavUpreti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome @umeraamir09 ! Welcome to the OpenSRE community

@VaibhavUpreti VaibhavUpreti merged commit bd7dd1e into Tracer-Cloud:main May 3, 2026
10 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 3, 2026

🔥 Another one. @umeraamir09 said "here's a PR" and maintainers said "ship it". That's how it's done.


👋 Join us on Discord - OpenSRE : hang out, contribute, or hunt for features and issues. Everyone's welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add direct unit tests for app/cli/local_llm/hardware.py

6 participants