Skip to content

🧪 Add test coverage for MediaFile factory class#264

Merged
segin merged 2 commits into
masterfrom
fix/add-mediafile-test-coverage-891522087837761371
Jun 13, 2026
Merged

🧪 Add test coverage for MediaFile factory class#264
segin merged 2 commits into
masterfrom
fix/add-mediafile-test-coverage-891522087837761371

Conversation

@segin

@segin segin commented Apr 8, 2026

Copy link
Copy Markdown
Owner

🎯 What: The testing gap for the MediaFile factory class (specifically around parsing failures/exceptions on opening real files) has been addressed by writing a new comprehensive unit test suite in tests/test_mediafile.cpp.
📊 Coverage: Tests the open, openByMimeType, detectMimeType, supportsExtension, supportsMimeType, split, mimeTypeToExtension, and extensionToMimeType functions. Covers edge cases for invalid inputs and explicit tests for file parsing resilience by generating malformed/truncated actual files (such as .ogg) on disk before invoking open.
Result: Test coverage for mediafile.cpp is significantly improved. A proper regression safety net ensures invalid media triggers appropriate error handling (exceptions) down the line without crashing the stream constructors.


PR created automatically by Jules for task 891522087837761371 started by @segin

Added comprehensive C++ test coverage in `tests/test_mediafile.cpp` targeting `MediaFile::open` and related helper functions. The test suite correctly creates malformed and truncated `.ogg` files along with random unparseable binary files directly to disk to assert that proper `InvalidMediaException` standard IO/parse exceptions are triggered per issue requirements. Registered the new test suite into the Autotools `check_PROGRAMS` in `tests/Makefile.am`.

Co-authored-by: segin <480709+segin@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings April 8, 2026 03:49
@gemini-code-assist

Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a new unit test executable to cover MediaFile factory behavior, focusing on invalid inputs and resilience to malformed on-disk media.

Changes:

  • Introduces tests/test_mediafile.cpp with coverage for MediaFile open/detection/mapping helpers and failure modes.
  • Registers test_mediafile in tests/Makefile.am so it builds/runs with make check.

Reviewed changes

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

File Description
tests/test_mediafile.cpp New test suite exercising MediaFile APIs, including malformed/truncated file handling.
tests/Makefile.am Adds test_mediafile to check_PROGRAMS and wires its build/link settings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/test_mediafile.cpp
addTest("test_open_malformed_ogg", []() {
std::string tempFile = "malformed_test.ogg";
// Create a file with a valid OGG extension but invalid/truncated content
createTempFile(tempFile, "OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); // Just a partial header

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

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

The malformed OGG payload is not being written as intended: the string literal contains \x00, and the implicit conversion to std::string uses the std::string(const char*) constructor, which stops at the first NUL byte. As a result, only "OggS" is written, not the partial header. Construct the binary test payload with an explicit length (e.g., std::string(<literal>, <size>)) or pass a byte container (std::vector<std::uint8_t>) so embedded NULs are preserved.

Suggested change
createTempFile(tempFile, "OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); // Just a partial header
const char malformedOggHeader[] = "OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
createTempFile(tempFile, std::string(malformedOggHeader, sizeof(malformedOggHeader) - 1)); // Just a partial header

Copilot uses AI. Check for mistakes.
Comment thread tests/test_mediafile.cpp
#include "test_framework.h"
#include "psymp3.h"
#include <fstream>
#include <cstdio>

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

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

This test file uses std::string and std::vector but does not include <string> or <vector>, which makes the build dependent on transitive includes from other headers. Add the direct standard headers to make the compilation unit self-contained and resilient to header changes.

Suggested change
#include <cstdio>
#include <cstdio>
#include <string>
#include <vector>

Copilot uses AI. Check for mistakes.
Comment thread tests/test_mediafile.cpp
Comment on lines +93 to +96
addTest("test_open_malformed_ogg", []() {
std::string tempFile = "malformed_test.ogg";
// Create a file with a valid OGG extension but invalid/truncated content
createTempFile(tempFile, "OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"); // Just a partial header

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

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

The test writes to a fixed filename in the current working directory. This can be flaky under parallel make check -j (collisions) or when the working directory is not writable. Prefer creating a unique temp path (e.g., via mkstemp, std::filesystem::temp_directory_path() + unique suffix) and use an RAII cleanup mechanism to ensure deletion on all paths.

Copilot uses AI. Check for mistakes.
Comment thread tests/test_mediafile.cpp
Comment on lines +111 to +114
addTest("test_open_random_bytes_file", []() {
std::string tempFile = "random_bytes.bin";
// Create a file with random/garbage content, completely unidentifiable
createTempFile(tempFile, "RANDOM_GARBAGE_CONTENT_NOT_A_MEDIA_FILE");

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

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

Same fixed-filename issue as the malformed OGG test. Use a unique temp filename and RAII cleanup to avoid collisions and leftover files if the test aborts mid-run.

Copilot uses AI. Check for mistakes.
Comment thread tests/test_mediafile.cpp
Comment on lines +86 to +92
addTest("test_exists", []() {
// currently always returns true
if (!MediaFile::exists("any_file.mp3")) {
throw AssertionFailure("exists() should return true based on current implementation");
}
});

Copilot AI Apr 8, 2026

Copy link

Choose a reason for hiding this comment

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

This test codifies a known-incorrect/placeholder behavior (“currently always returns true”), which will make it harder to fix MediaFile::exists() later without breaking tests. Either remove this test, or assert the intended contract (e.g., non-existent paths return false) and update the implementation accordingly; if the API is intentionally stubbed, consider marking the test as expected-fail/disabled until the behavior is implemented.

Suggested change
addTest("test_exists", []() {
// currently always returns true
if (!MediaFile::exists("any_file.mp3")) {
throw AssertionFailure("exists() should return true based on current implementation");
}
});

Copilot uses AI. Check for mistakes.
…est-coverage-891522087837761371

# Conflicts:
#	tests/Makefile.am
@segin segin merged commit a02a6d4 into master Jun 13, 2026
1 check passed
@segin segin deleted the fix/add-mediafile-test-coverage-891522087837761371 branch June 13, 2026 04:01
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