Skip to content

Conversation

@weiyuanyue
Copy link
Contributor

@weiyuanyue weiyuanyue commented Jan 13, 2026

This PR introduces comprehensive testing infrastructure to validate model download URLs and refactors URL handling code for better maintainability, robustness, and testability.

Key Highlights

New Integration Test Suite: ModelUrlConnectivityTests.cs

The centerpiece of this PR is a robust test suite that automatically validates all model URLs defined in .model.json and .modelgroup.json files across the codebase. This test:

  • Prevents broken links: Automatically detects when model download URLs become inaccessible
  • Validates real download flows: Tests the actual URLs used during downloads (raw URLs for GitHub files, resolve URLs for HuggingFace files, API endpoints for directories)
  • Provides actionable feedback: Clear error messages showing which models have broken URLs, status codes, and download endpoints
  • Supports both individual and batch testing: Run tests per file or get a comprehensive report across all model definitions

Business Value: This test suite acts as an early warning system, helping developers quickly identify and fix broken model links before they impact users.

Changes

1. New Test Files

  • ModelUrlConnectivityTests.cs (352 lines): Integration tests that make real HTTP requests to validate model URL accessibility
  • ModelUrlTests.cs (589 lines): Comprehensive unit tests for URL parsing, construction, and edge cases without network calls
  • ApiIntegrationTests.cs (101 lines): Integration tests for GitHub and HuggingFace API functionality
  • ApiTests.cs (126 lines): Unit tests for API helper classes

2. Enhanced ModelUrl.cs Utility Class

Before: URLs were constructed inline with string concatenation throughout the codebase
After: Centralized URL building methods with proper validation

New static builder methods added:

  • HuggingFaceUrl:

    • BuildRepoUrl(org, repo) - Base repository URLs
    • BuildTreeUrl(org, repo, ref, path) - Directory browsing URLs
    • BuildResolveUrl(org, repo, ref, filePath) - File download URLs
    • BuildBlobUrl(org, repo, ref, filePath) - File viewing URLs
    • BuildApiUrl(org, repo, ref, path) - API endpoints
  • GitHubUrl:

    • BuildRepoUrl(org, repo) - Base repository URLs
    • BuildApiUrl(org, repo, ref, path) - API endpoints
    • BuildRawUrl(org, repo, ref, filePath) - Raw file download URLs
    • BuildBlobUrl(org, repo, ref, filePath) - File viewing URLs

Benefits:

  • ✅ Single source of truth for URL construction
  • ✅ Consistent URL formatting across the codebase
  • ✅ Input validation with descriptive error messages
  • ✅ Easier to test and maintain
  • ✅ Supports both display URLs and actual download URLs

3. Refactored ModelInformationHelper.cs

  • Replaced inline URL string concatenation with new builder methods
  • Improved code readability and maintainability
  • Better null handling and validation
  • Cleaner separation between display URLs and API URLs

4. Updated AddHFModelView.xaml.cs

  • Replaced hardcoded URL construction with builder methods
  • Added proper validation for HuggingFace model IDs (must be in org/repo format)
  • Enhanced null safety and error handling
  • More robust license URL handling

5. Improved API Helpers (GithubApi.cs, HuggingFaceApi.cs)

  • Use centralized URL builders instead of local constants
  • Added proper error handling with HTTP status code checks
  • Better exception messages for debugging
  • Input validation for file paths

6. Enhanced ModelDownload.cs

  • Minor improvements aligned with URL handling changes

Testing Strategy

The test suite validates URLs at two levels:

  1. Unit Tests (ModelUrlTests.cs, ApiTests.cs):

    • Fast, no network calls
    • Test URL parsing, construction, edge cases
    • Validate error handling for malformed URLs
    • Test local path mapping logic
  2. Integration Tests (ModelUrlConnectivityTests.cs, ApiIntegrationTests.cs):

    • Real HTTP requests to validate accessibility
    • Test both individual files and batch processing
    • Verify actual download flow mirrors business logic
    • Rate-limited to be respectful to servers

Test Coverage

The connectivity tests cover:

  • All .model.json files
  • All .modelgroup.json files
  • GitHub file URLs (tests raw.githubusercontent.com)
  • GitHub directory URLs (tests API endpoints)
  • HuggingFace file URLs (tests resolve URLs)
  • HuggingFace directory URLs (tests API endpoints)

Future Benefits

  1. Proactive Monitoring: Run these tests in CI/CD to catch broken links before deployment
  2. Easier Refactoring: Centralized URL logic makes future changes safer
  3. Better Debugging: Clear error messages help identify issues quickly
  4. Quality Assurance: Ensures model definitions remain valid over time

Note

Integration tests make real HTTP requests and should be run separately from unit tests in CI/CD pipelines to avoid:

  • Long test execution times
  • Rate limiting issues
  • Network dependency in core test suites

Copilot AI review requested due to automatic review settings January 13, 2026 12:49
@weiyuanyue weiyuanyue marked this pull request as draft January 13, 2026 12:50
Copy link
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

This PR refactors URL handling infrastructure for HuggingFace and GitHub model repositories by introducing static factory methods to replace string concatenation. The changes centralize URL construction logic and improve type safety.

Changes:

  • Added static factory methods (BuildRepoUrl, BuildTreeUrl, BuildResolveUrl, BuildBlobUrl, BuildApiUrl, BuildRawUrl) to HuggingFaceUrl and GitHubUrl classes
  • Updated all internal usages to use new factory methods in ModelInformationHelper, HuggingFaceApi, GithubApi, AddHFModelView, and ModelDownload
  • Enhanced error handling with HTTP status code validation and file path validation
  • Added comprehensive test coverage with 589 lines of tests across unit, API, and integration tests

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
AIDevGallery.Utils/ModelUrl.cs Added static factory methods for URL construction to HuggingFaceUrl and GitHubUrl classes; updated existing methods to use new factories
AIDevGallery.Utils/ModelInformationHelper.cs Replaced string concatenation with static factory method calls for API and resolve URLs
AIDevGallery/Utils/HuggingFaceApi.cs Updated to use BuildResolveUrl factory method; added validation for modelId format and file path; enhanced error handling with HTTP status codes
AIDevGallery/Utils/GithubApi.cs Updated to use BuildRawUrl factory method; added validation for file path and HTTP status code checking
AIDevGallery/Controls/ModelPicker/AddHFModelView.xaml.cs Updated UI code to use factory methods for building tree, blob, and repo URLs
AIDevGallery/Utils/ModelDownload.cs Added validation to check for empty file list after filtering
AIDevGallery.Tests/UnitTests/Utils/ModelUrlTests.cs New comprehensive unit tests for URL parsing and construction (326 lines)
AIDevGallery.Tests/UnitTests/Utils/ApiTests.cs New unit tests for API utility functions (126 lines)
AIDevGallery.Tests/IntegrationTests/ApiIntegrationTests.cs New integration tests for API connectivity (101 lines)
AIDevGallery.Tests/IntegrationTests/ModelUrlConnectivityTests.cs New integration tests to validate model URLs in definition files (356 lines)

This commit refactors the URL handling infrastructure for HuggingFace and GitHub model repositories, replacing string concatenation with type-safe static factory methods.

## Key Changes

### URL Construction Refactoring
- Added static factory methods to HuggingFaceUrl and GitHubUrl classes:
  - BuildRepoUrl(), BuildTreeUrl(), BuildResolveUrl(), BuildBlobUrl(), BuildApiUrl()
  - Centralized URL format management for better maintainability
  - Eliminated error-prone string concatenation across codebase

### Enhanced URL Parsing
- Extended HuggingFaceUrl to support 'resolve' URL format (previously only 'blob')
- Improved URL validation with explicit path requirement checks
- Added defensive null checks for file operations

### Improved Error Handling
- Added HTTP status code validation in API calls
- Enhanced error messages with detailed context (URL, status code)
- Implemented empty file list validation after filtering

### Code Quality Improvements
- Replaced hardcoded URL strings with named constants
- Unified URL construction patterns across all API utilities
- Improved code readability and type safety

### Comprehensive Test Coverage
- Added 589 lines of unit tests covering all URL parsing scenarios
- Created 63 test cases for both HuggingFace and GitHub URL handling
- Added integration tests for API connectivity
- Validated edge cases: whitespace handling, invalid formats, empty strings

## Backward Compatibility

**All changes are fully backward compatible:**
- Existing public APIs remain unchanged (constructors, properties, methods)
- New static methods are pure additions, no breaking changes
- All current code continues to work without modification
- Internal refactoring does not affect external consumers

## Testing

- All new static methods comprehensively tested
- Verified URL parsing for various formats (tree, blob, resolve)
- Tested local path mapping functionality
- Validated error handling for malformed URLs

This refactoring improves code maintainability, reduces bugs from string concatenation errors, and provides a more robust foundation for future URL-related features.
@weiyuanyue weiyuanyue changed the title Refactor: Modernize URL construction with static factory methods [Test]Add Model URL Connectivity Tests and Improve URL Handling Jan 13, 2026
Copy link
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

Copilot reviewed 10 out of 10 changed files in this pull request and generated 9 comments.

@weiyuanyue weiyuanyue marked this pull request as ready for review January 14, 2026 03:40
Copilot AI review requested due to automatic review settings January 14, 2026 03:40
…orization

- Add test categories (Integration, Network, Slow) for better test filtering
- Fix HttpClient resource leak with ClassCleanup disposal
- Use ClassCleanupBehavior.EndOfClass to fix MSTEST0034 warning
- Add warning comment about batch test execution time
Copy link
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

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

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.

2 participants