test(images): green ImagePromptBuilderTest (follow-up to #97)

* test(images): update ImagePromptBuilderTest for array-returning builder

`build_article_prompt()` and `build_source_prompt()` have returned
`array{prompt: string, caption: string}` since v0.4.x; five existing
tests still asserted against the envelope as if it were a string. On
PHP 8+ `assertStringContainsString` and `strlen()` raise a TypeError
when handed an array, so every one of those tests failed on the CI
matrix even though the production code is fine.

Fix is mechanical per CTO spec in convo seq 6: destructure the return
on receipt and assert against `$scene` / `$caption`. Cases updated:

- test_build_article_prompt_from_content
- test_build_article_prompt_with_missing_content
- test_build_source_prompt_from_reddit_data
- test_build_source_prompt_without_comments
- test_prompts_are_reasonably_sized (500-char cap now bound to the
  scene+style-suffix, which is what the cap was always meant to measure)

No production code changes. Unrelated pre-existing failures
(`Call to undefined function post_type_exists()` in Publisher fixtures
and the repo-wide PHPCS backlog) remain out of scope per CTO.

Follow-up to PR #97 (v0.8.0 image bundle), which shipped before this
test fix landed. Context in convo thread image-mime-bug seq 07.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* test(images): drop unreachable content-based assertion in article-prompt test

Per CTO review on PR #99 (convo image-mime-bug seq 8): the earlier diff
destructured the return value correctly, but left an assertion for
'Dragons are fascinating' — a content substring. With no OpenRouter key
in the unit-test env, `rewrite_via_llm()` catches and falls back to
`synthesize_visual_concepts_fallback()`, which uses title only. The
content substring is structurally unreachable on that path, so the
assertion can never pass without mocking the LLM.

Picking CTO option 1 ("honest and minimal"): drop the content
assertion. The title + non-empty + length + caption-is-string checks
remain — those describe what the method actually guarantees regardless
of LLM reachability. Content-path testing belongs in a mocked-LLM test
(see `test_rewrite_uses_setting_when_present`, which already covers it).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: peptiderepo <peptiderepo@users.noreply.github.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
peptiderepo
2026-04-21 02:34:05 +07:00
committed by GitHub
parent 5c7736bf4e
commit 188e6603dd

View File

@@ -45,7 +45,17 @@ class ImagePromptBuilderTest extends BaseTestCase {
}
/**
* Test build_article_prompt() generates a prompt from article title and content.
* Test build_article_prompt() generates a prompt from article title.
*
* `build_article_prompt()` has returned `array{prompt: string, caption: string}`
* since v0.4.x. Destructure on receipt so the assertions run against the
* scene string, not the envelope.
*
* Note: this test does NOT assert on the article content substring. With no
* OpenRouter API key in the unit-test env, `rewrite_via_llm()` catches and
* falls back to `synthesize_visual_concepts_fallback()`, which reliably
* includes only the title — not the content. Content-based assertions
* belong in a test that mocks the LLM (see `test_rewrite_uses_setting_when_present`).
*/
public function test_build_article_prompt_from_content(): void {
$builder = new \PRAutoBlogger_Image_Prompt_Builder();
@@ -55,13 +65,13 @@ class ImagePromptBuilderTest extends BaseTestCase {
'post_content' => '<p>Dragons are fascinating creatures. Learn the secrets.</p>',
];
$prompt = $builder->build_article_prompt( $article_data );
[ 'prompt' => $scene, 'caption' => $caption ] = $builder->build_article_prompt( $article_data );
$this->assertStringContainsString( 'How to Train Your Dragon', $prompt );
$this->assertStringContainsString( 'Dragons are fascinating', $prompt );
$this->assertStringContainsString( 'How to Train Your Dragon', $scene );
// Style suffix should be appended.
$this->assertNotEmpty( $prompt );
$this->assertGreaterThan( 50, strlen( $prompt ) );
$this->assertNotEmpty( $scene );
$this->assertGreaterThan( 50, strlen( $scene ) );
$this->assertIsString( $caption );
}
/**
@@ -74,10 +84,11 @@ class ImagePromptBuilderTest extends BaseTestCase {
'post_title' => 'Minimal Article',
];
$prompt = $builder->build_article_prompt( $article_data );
[ 'prompt' => $scene, 'caption' => $caption ] = $builder->build_article_prompt( $article_data );
$this->assertStringContainsString( 'Minimal Article', $prompt );
$this->assertNotEmpty( $prompt );
$this->assertStringContainsString( 'Minimal Article', $scene );
$this->assertNotEmpty( $scene );
$this->assertIsString( $caption );
}
/**
@@ -94,11 +105,12 @@ class ImagePromptBuilderTest extends BaseTestCase {
],
];
$prompt = $builder->build_source_prompt( $source_data );
[ 'prompt' => $scene, 'caption' => $caption ] = $builder->build_source_prompt( $source_data );
$this->assertStringContainsString( 'Robot', $prompt );
$this->assertStringContainsString( 'Dancing', $prompt );
$this->assertNotEmpty( $prompt );
$this->assertStringContainsString( 'Robot', $scene );
$this->assertStringContainsString( 'Dancing', $scene );
$this->assertNotEmpty( $scene );
$this->assertIsString( $caption );
}
/**
@@ -111,14 +123,18 @@ class ImagePromptBuilderTest extends BaseTestCase {
'title' => 'Standalone Title',
];
$prompt = $builder->build_source_prompt( $source_data );
[ 'prompt' => $scene, 'caption' => $caption ] = $builder->build_source_prompt( $source_data );
$this->assertStringContainsString( 'Standalone Title', $prompt );
$this->assertNotEmpty( $prompt );
$this->assertStringContainsString( 'Standalone Title', $scene );
$this->assertNotEmpty( $scene );
$this->assertIsString( $caption );
}
/**
* Test that prompts don't exceed reasonable length.
*
* The 500-char cap applies to the scene + style suffix (the `prompt`
* field), not the full array envelope.
*/
public function test_prompts_are_reasonably_sized(): void {
$builder = new \PRAutoBlogger_Image_Prompt_Builder();
@@ -129,10 +145,10 @@ class ImagePromptBuilderTest extends BaseTestCase {
'post_content' => $long_content,
];
$prompt = $builder->build_article_prompt( $article_data );
[ 'prompt' => $scene ] = $builder->build_article_prompt( $article_data );
// Prompt should be under 500 chars (concept + style suffix).
$this->assertLessThan( 500, strlen( $prompt ) );
// Scene + style suffix should be under 500 chars.
$this->assertLessThan( 500, strlen( $scene ) );
}
/**