Skip to content

[Test Improver] test: add unit tests for drift-detection helpers (0% -> ~100%)#727

Open
danielmeppiel wants to merge 3 commits intomainfrom
test-assist/drift-detection-coverage-24486614511-e6485df22d208295
Open

[Test Improver] test: add unit tests for drift-detection helpers (0% -> ~100%)#727
danielmeppiel wants to merge 3 commits intomainfrom
test-assist/drift-detection-coverage-24486614511-e6485df22d208295

Conversation

@danielmeppiel
Copy link
Copy Markdown
Collaborator

🤖 Test Improver — automated AI assistant

Goal and Rationale

src/apm_cli/drift.py contains four pure, stateless functions that drive every diff-aware apm install decision: which packages need re-downloading, which files should be cleaned up, and how to build reproducible download references.

The module's own docstring says these helpers are "easy to test in isolation" — yet they had no dedicated test file. A silent bug in any of these functions can cause:

  • packages to be silently skipped when they should be re-downloaded (missed updates)
  • orphaned files to accumulate on disk (drift not cleaned up)
  • proxy/Artifactory installs to use the wrong auth token

Approach

Tests use minimal in-file dataclass stubs (_LockedDep, _LockFile) to keep the test file self-contained and dependency-free. No mocking framework or I/O required.

Function Tests Key scenarios
detect_ref_change 11 update_refs bypass, new package, same/added/removed/changed ref, hash-based pins
detect_orphans 7 partial install guard, no lockfile, all kept, single and multi orphans, empty manifest, None treated as empty
detect_config_drift 7 empty inputs, unchanged, changed, new entries ignored, multi-entry partial drift, stored superset not flagged
build_download_ref 10 no lockfile, update_refs, ref_changed, locked commit SHA, 'cached' sentinel skipped, proxy host+prefix restored, proxy fallback ref, key missing

Coverage Impact

File Before After
src/apm_cli/drift.py ~0% ~100%

Test Status

All 3865 tests pass (3830 baseline + 35 new):

uv run pytest tests/unit tests/test_console.py -x -q
3865 passed in 16.13s

Reproducibility

uv run pytest tests/unit/test_drift_detection.py -v

Generated by Daily Test Improver ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/daily-test-improver.md@b87234850bf9664d198f28a02df0f937d0447295

Add 35 new tests for apm_cli.drift -- pure stateless helpers that
drive diff-aware apm install decisions:

- detect_ref_change: update_refs mode, new package (None locked), same ref,
  ref added/removed/changed, hash-based pin changes (11 tests)
- detect_orphans: partial install guard, no-lockfile first install, all
  packages present, single and multiple orphaned packages, empty manifest,
  None only_packages treated as empty (7 tests)
- detect_config_drift: empty inputs, unchanged config, changed config,
  brand-new entries ignored, multi-entry partial drift, stored superset
  not flagged (7 tests)
- build_download_ref: no lockfile, update_refs mode, ref_changed, locked
  commit SHA used, 'cached' sentinel not used, proxy host+prefix restored,
  proxy fallback ref, proxy with existing ref not overridden, non-proxy
  host diff, key missing from lockfile (10 tests)

These helpers are documented as 'easy to test in isolation' yet had no
dedicated test file.  They are on the critical path of every apm install
run -- a bug here can silently prevent re-downloads or incorrectly
trigger them.

Tests: 3830 -> 3865 (+35)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@danielmeppiel danielmeppiel marked this pull request as ready for review April 18, 2026 02:47
Copilot AI review requested due to automatic review settings April 18, 2026 02:47
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds dedicated unit coverage for the pure drift-detection helpers in apm_cli.drift, ensuring core apm install drift decisions (ref changes, orphan cleanup, config drift, and reproducible download refs) are validated in isolation.

Changes:

  • Introduces a new tests/unit/test_drift_detection.py suite covering detect_ref_change, detect_orphans, detect_config_drift, and build_download_ref.
  • Uses small in-file lockfile/locked-dependency stubs to keep the tests fast and dependency-free.
Show a summary per file
File Description
tests/unit/test_drift_detection.py Adds unit tests for drift-detection helper functions, including ref drift, orphan detection, config drift, and download-ref construction.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 2

import unittest
from dataclasses import dataclass, field
from typing import Dict, List, Optional
from unittest.mock import MagicMock
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

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

Unused import: MagicMock is imported but never used in this test module. Please remove it to keep the test file clean and avoid potential lint failures.

Suggested change
from unittest.mock import MagicMock

Copilot uses AI. Check for mistakes.
dep = _dep(reference="v2.0.0")
self.assertFalse(detect_ref_change(dep, locked, update_refs=True))

def test_update_refs_false_no_lockfile(self):
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

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

Test name/docstring mismatch: this test passes update_refs=True but is named test_update_refs_false_no_lockfile, which makes the intent harder to follow. Consider renaming the test (or updating its docstring) so it consistently reflects the exercised behavior.

Suggested change
def test_update_refs_false_no_lockfile(self):
def test_update_refs_true_no_lockfile_returns_false(self):

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants