fix: version check logic and validation#41
Conversation
…Type - Parse registry response as unknown and validate 'version' field. - Resolve duplicate getUpdateType declarations. - Export semver utilities for testing. - Add unit tests for version comparison and update logic.
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughExports semantic-version helpers ( ChangesVersion Check Utilities with Registry Validation
Sequence Diagram(s)sequenceDiagram
participant checkForUpdates
participant fetch
participant RegistryAPI
participant logger
participant console
checkForUpdates->>fetch: request latest package metadata
fetch->>RegistryAPI: GET /package
RegistryAPI-->>fetch: response JSON (unknown)
fetch-->>checkForUpdates: parsed JSON
checkForUpdates->>checkForUpdates: validate { version: string }
alt invalid structure
checkForUpdates->>logger: error("Invalid registry response: version not found")
checkForUpdates-->>checkForUpdates: return
else network error
fetch-->>checkForUpdates: rejection
checkForUpdates-->>checkForUpdates: return (silent)
else valid version
checkForUpdates->>checkForUpdates: compareSemver(installed, latest)
alt newer available
checkForUpdates->>console: log("update available!", latest)
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/utils/version-check.ts (1)
33-40:⚠️ Potential issue | 🟡 Minor | ⚡ Quick winHandle downgrade inputs in exported
getUpdateType.When
installedis newer thanlatest,compareSemverreturns'gt', but this logic falls through and returns'patch'. For an exported helper, downgrades should not be classified as updates.Proposed fix
export function getUpdateType(installed: string, latest: string): string { const cmp = compareSemver(installed, latest); - if (cmp === 'eq') return ''; + if (cmp !== 'lt') return ''; const [i1 = 0, i2 = 0] = installed.replace(/^v/, '').split('.').map(Number); const [l1 = 0, l2 = 0] = latest.replace(/^v/, '').split('.').map(Number); if (l1 > i1) return 'major'; if (l2 > i2) return 'minor'; return 'patch'; }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/utils/version-check.ts` around lines 33 - 40, The exported getUpdateType currently treats a newer installed version as a 'patch' because it ignores compareSemver's 'gt' result; update the function (getUpdateType) to explicitly handle the 'gt' case (in addition to 'eq') and return '' for downgrades so they are not classified as updates, keeping the existing major/minor/patch logic for actual newer latest versions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/__tests__/version-check.test.ts`:
- Around line 57-60: The teardown currently calls vi.unstubAllGlobals() and
vi.clearAllMocks() but does not restore spy implementations (the console.log spy
created near line 54), leaving spies active across tests; update the afterEach
to call vi.restoreAllMocks() (either in addition to or instead of
vi.clearAllMocks()) so spies and mocked implementations created during tests are
restored to their originals, ensuring test isolation for the afterEach block and
functions like the console.log spy.
---
Outside diff comments:
In `@src/utils/version-check.ts`:
- Around line 33-40: The exported getUpdateType currently treats a newer
installed version as a 'patch' because it ignores compareSemver's 'gt' result;
update the function (getUpdateType) to explicitly handle the 'gt' case (in
addition to 'eq') and return '' for downgrades so they are not classified as
updates, keeping the existing major/minor/patch logic for actual newer latest
versions.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro Plus
Run ID: 2700e711-1928-4cc7-a322-9cf497398304
📒 Files selected for processing (2)
src/__tests__/version-check.test.tssrc/utils/version-check.ts
- Update afterEach to use vi.restoreAllMocks(). - Update getUpdateType to return empty string for downgrades. - Add test case for downgrades in getUpdateType.
This PR improves the version check utility by adding runtime validation for the npm registry response and cleaning up duplicate function declarations.
Key changes:
getUpdateTypedeclarations.src/__tests__/version-check.test.ts.compareSemverandgetUpdateTypefor better testability.Summary by CodeRabbit
Tests
Bug Fixes