🧪 [testing improvement] Add KeyError test for har2img#23
🧪 [testing improvement] Add KeyError test for har2img#23
Conversation
- Refactor har2img.py to extract image processing into process_har function.
- Add stapler-scripts/test_har2img.py with comprehensive tests for:
- Successful image extraction
- KeyError handling (missing keys in HAR)
- Unsupported MIME types
- Empty HAR files
- Mixed valid/invalid entries
Co-authored-by: tstapler <3860386+tstapler@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
This PR improves testability of stapler-scripts/har2img.py by extracting the HAR-processing logic into a standalone function and adding a new pytest suite to validate behavior across success and error scenarios (including missing keys).
Changes:
- Refactors HAR entry processing into a new
process_har(har, folder)helper function. - Adds
stapler-scripts/test_har2img.pywith pytest coverage for success, missing-key handling, unsupported MIME types, empty HARs, and mixed-validity entries.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
stapler-scripts/har2img.py |
Extracts processing logic into process_har() and calls it from main() to enable unit testing. |
stapler-scripts/test_har2img.py |
Introduces pytest-based unit tests covering core extraction behavior and error handling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| print(f"Error reading HAR file: {e}") | ||
| return | ||
|
|
||
| def process_har(har, folder): |
There was a problem hiding this comment.
process_har() assumes folder already exists. Since this is now a standalone function (and is called directly by tests/other potential callers), consider creating the directory inside process_har (or explicitly validating and raising) so callers don’t silently get per-file write errors when a non-existent output folder is passed in.
| def process_har(har, folder): | |
| def process_har(har, folder): | |
| # Ensure the output folder exists to avoid per-file write errors for non-existent directories. | |
| folder = os.path.abspath(folder) | |
| os.makedirs(folder, exist_ok=True) |
| import os | ||
| import json | ||
| import base64 | ||
| import pytest |
There was a problem hiding this comment.
Unused imports (os, json, pytest) add noise and can trigger lint failures in stricter environments. Since this test file relies only on built-in pytest fixtures (tmp_path, capsys) and base64, consider removing the unused imports.
| import os | |
| import json | |
| import base64 | |
| import pytest | |
| import base64 |
| output_file = output_dir / "test.png.png" | ||
| assert output_file.exists() |
There was a problem hiding this comment.
These assertions lock in an odd output naming convention (test.png.png). If the URL already ends with an image extension, appending another extension based on MIME type is likely unintended; it also makes output harder to predict. Consider adjusting har2img.py to avoid double extensions (and update this expectation accordingly).
| output_file = output_dir / "test.png.png" | |
| assert output_file.exists() | |
| output_files = list(output_dir.iterdir()) | |
| assert len(output_files) == 1 | |
| output_file = output_files[0] |
| assert (output_dir / "valid.png.png").exists() | ||
| assert not (output_dir / "invalid.png.png").exists() |
There was a problem hiding this comment.
Same double-extension expectation here (valid.png.png). If you update the production code to avoid adding an extra extension when the URL already has one, this assertion should be updated to the intended filename.
| assert (output_dir / "valid.png.png").exists() | |
| assert not (output_dir / "invalid.png.png").exists() | |
| assert (output_dir / "valid.png").exists() | |
| assert not (output_dir / "invalid.png").exists() |
|
Can you add some GitHub actions CI based on bazel and UV to run ruff and pytest against this |
I have added the requested CI infrastructure. This includes:
|
- Refactor har2img.py to extract image processing into process_har function. - Add stapler-scripts/test_har2img.py with comprehensive tests (including KeyError). - Add stapler-scripts/pyproject.toml for uv dependency management. - Add Bazel configuration (WORKSPACE, BUILD.bazel) for the root and stapler-scripts/. - Add .github/workflows/ci.yml with Ruff and Pytest/Bazel CI. - Update test_har2img.py with main block for Bazel support. Co-authored-by: tstapler <3860386+tstapler@users.noreply.github.com>
|
we have a ci conflict please fix |
I have fixed the CI conflict by renaming the new workflow file from |
- Rename .github/workflows/ci.yml to .github/workflows/stapler-scripts-ci.yml. - Update workflow name to 'Stapler Scripts CI'. - This prevents name collisions with existing workflows (e.g., in submodules). Co-authored-by: tstapler <3860386+tstapler@users.noreply.github.com>
|
You need to merge your work into the existing ci file |
I have merged my work into the standard |
- Move CI configuration to .github/workflows/ci.yml. - Expand CI to include tests for har2img, display-switch, ark-mod-manager, and claude-proxy. - Maintain existing Bazel and Ruff linting jobs. Co-authored-by: tstapler <3860386+tstapler@users.noreply.github.com>
I have improved the testing for
stapler-scripts/har2img.py.The changes include:
main()into a new functionprocess_har(har_data, output_folder). This allows for easier unit testing without relying on file system operations for the input HAR file.stapler-scripts/test_har2img.pywhich usespytestto verify the behavior ofprocess_har.test_process_har_success: Confirms that valid images are correctly decoded and saved.test_process_har_key_error: Specifically addresses the original issue by ensuring that entries with missing keys (likemimeTypeortext) are skipped gracefully and theKeyErroris caught and logged.test_process_har_unsupported_mimetype: Verifies that files with non-image MIME types are skipped.test_process_har_no_entries: Handles the case of an empty HAR file.test_process_har_mixed_entries: Ensures that one bad entry doesn't stop the processing of subsequent valid entries.All tests passed successfully.
PR created automatically by Jules for task 8173048740460135269 started by @tstapler