Skip to content

Tests: Add unit test coverage for GoogleTextToSpeechConversionModel - #49

Merged
superdav42 merged 8 commits into
WordPress:trunkfrom
noruzzamans:test/google-tts-model-unit-tests
Oct 14, 2026
Merged

superdav42 merged 8 commits into
WordPress:trunkfrom
noruzzamans:test/google-tts-model-unit-tests

Conversation

@noruzzamans

@noruzzamans noruzzamans commented Sep 28, 2026 •

Copy link
Copy Markdown
Member

Summary

This pull request introduces dedicated unit test coverage for the GoogleTextToSpeechConversionModel class (src/Models/GoogleTextToSpeechConversionModel.php).

While the text generation model possesses dedicated unit test coverage, the text-to-speech conversion model previously lacked isolated unit test verification. This suite verifies the full lifecycle of request parameter assembly, prompt validation, audio packaging, response parsing, and authentication wrapping.

Test Coverage Highlights

  1. Parameter Assembly (prepareConvertParams):

    • Asserts default speech generation configuration enforces AUDIO response modality and resolves the default Kore voice.
    • Asserts configured prebuilt voices (e.g. Puck) properly override default voice selection.
    • Asserts custom options merging supports nested generationConfig.* parameters alongside top-level request parameters.
    • Asserts conflicting custom options for generationConfig (e.g. responseModalities) throw an InvalidArgumentException.
    • Asserts conflicting top-level custom options (e.g. contents) throw an InvalidArgumentException.
  2. Prompt Processing (preparePromptText):

    • Asserts plain text is extracted and concatenated across multiple prompt messages and message parts with newline delimiters.
    • Asserts prompts lacking text throw an InvalidArgumentException.
  3. Response Parsing & Audio Container Handling (parseResponseToGenerativeAiResult):

    • Asserts standard Gemini API audio responses parse into a GenerativeAiResult with expected ID prefix (google-tts-) and stop finish reason.
    • Asserts audio parts are converted into inline audio/wav File instances containing valid RIFF/WAVE container headers wrapping raw PCM data.
    • Asserts custom sample rate specification (e.g. rate=16000) is parsed from MIME types and encoded into the WAV header.
    • Asserts MIME types omitting the rate parameter fall back to standard 24000 Hz.
    • Asserts missing candidate parts, missing inline data, and invalid base64 payloads throw descriptive ResponseException errors.
  4. WAV Container Construction (wrapPcmInWav):

    • Validates the standard 44-byte RIFF/WAVE header assembly (chunk IDs, subchunk sizes, PCM audio format tag 1, channels, sample rate, byte rate, block align, and bits per sample).
  5. Authentication Handling (getRequestAuthentication):

    • Asserts ApiKeyRequestAuthentication instances are wrapped into GoogleApiKeyRequestAuthentication with matching credentials.
    • Asserts non-API-key authentication instances are preserved intact.

Verification

1. Isolated PHPUnit Suite

vendor/bin/phpunit tests/Models/GoogleTextToSpeechConversionModelTest.php
# OK (16 tests, 63 assertions)

2. Full PHPUnit Suite

composer run-script test
# OK (20 tests, 74 assertions)

3. PHP CodeSniffer (PER / PSR-12 / WordPress Standards)

composer run-script phpcs
# 14 / 14 (100%) - 0 errors, 0 warnings

4. PHPStan Static Analysis (Level 8 / Max)

composer run-script phpstan
# [OK] No errors

@noruzzamans
noruzzamans marked this pull request as ready for review September 28, 2026 06:28
@noruzzamans
noruzzamans requested a review from a team September 28, 2026 06:28
@github-actions

github-actions Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: noruzzamans <noruzzaman@git.wordpress.org>
Co-authored-by: superdav42 <superdav42@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@superdav42 superdav42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for backfilling coverage for the TTS model from #31. The file location, namespace and the anonymous-subclass pattern for exposing protected methods match tests/Models/GoogleTextGenerationModelTest.php, phpcs is clean, and CI passes.

I ran the suite locally and also mutation-probed GoogleTextToSpeechConversionModel to see what the new tests actually catch. A few assertions are weaker than their names suggest, so I'm requesting a small set of changes (inline suggestions below):

  1. @since tags should be n.e.x.t rather than 1.2.0. 1.2.0 has already shipped (#46), and the release process replaces n.e.x.t placeholders, including in tests/.
  2. testParseResponseWithDefaultSampleRateFallback omits mimeType entirely, so it exercises the default MIME type in extractInlineAudioData(), not the parseSampleRate() fallback described in its docblock. Changing that fallback to return 0; still passes the suite.
  3. assertStringContainsString('data', $wavBytes) always passes, because the fixture PCM string 'dummy-raw-pcm-audio-stream-data' itself contains data.
  4. preparePromptText() non-text handling isn't exercised. The empty-prompt test uses a message with no parts, so if the getText() !== null guard is removed, everything still passes.

With all suggestions applied locally: OK (16 tests, 63 assertions), phpcs clean, and each of the three mutations above is now caught.

Non-blocking, for a possible follow-up: the four anonymous subclasses could collapse into a single createModel(?ModelConfig $config = null) helper that exposes all four protected methods.

noruzzamans and others added 7 commits October 14, 2026 07:34
Co-authored-by: David Stone <david@nnucomputerwhiz.com>
Co-authored-by: David Stone <david@nnucomputerwhiz.com>
Co-authored-by: David Stone <david@nnucomputerwhiz.com>
Co-authored-by: David Stone <david@nnucomputerwhiz.com>
Co-authored-by: David Stone <david@nnucomputerwhiz.com>
Co-authored-by: David Stone <david@nnucomputerwhiz.com>
@noruzzamans

Copy link
Copy Markdown
Member Author

Thanks @superdav42! Accepted all suggestions and updated in fd6f383:

  • Replaced all 21 @since 1.2.0 tags across the file with @since n.e.x.t.
  • Corrected the duplicate 'First line of speech.' text part back to 'Second line of speech.' in testPreparePromptTextConcatenatesMultipleMessageParts (with the non-text File part in between).

All 13 CI matrix checks (PHP 7.4–8.4) and local PHPCS/PHPStan suites are passing cleanly.

@superdav42 superdav42 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

looks great! Thanks

@superdav42
superdav42 merged commit 0aa0a7a into WordPress:trunk Oct 14, 2026
14 checks passed
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