Direct probe on 2026-04-21 confirmed the Cloudflare AI Gateway route to Workers AI (`gateway.ai.cloudflare.com/.../workers-ai/@cf/...`) is no longer 403ing — the upstream regression noted in v0.8.0's ARCHITECTURE.md #16 and in thread image-pipeline seq 6 has been resolved. Routing via the gateway gives us the same benefits we already get for OpenRouter LLM calls: response caching, cost/latency dashboard, rate limiting. Changes: - `Cloudflare_Image_Support::build_endpoint_url()` now prefers the gateway URL derived from the existing `prautoblogger_ai_gateway_base_url` option. Direct API remains the fallback. - New private helper `build_gateway_workers_ai_url()` strips the OpenRouter path suffix from the configured gateway URL and appends `/workers-ai/{model}`. Returns empty string (fallback to direct) when: no gateway URL configured, the new toggle is off, or the URL isn't a valid `https://gateway.ai.cloudflare.com/...` shape. - New admin toggle `prautoblogger_cf_image_via_gateway` (default on) in the Images section. Operators can disable to force direct API without clearing the LLM-path gateway URL. - ARCHITECTURE.md #16 rewritten to reflect the v0.8.2+ gateway routing. - CHANGELOG.md v0.8.2 entry under Changed. Tests (new `CloudflareImageSupportTest`): - Direct URL when gateway not configured. - Gateway URL derived correctly when configured + toggle on. - Toggle off forces direct URL even with gateway configured. - Malformed gateway URL falls back to direct (defensive). - `is_nsfw_error()` only matches error code 3030 (pinned separately so a tolerance regression fails early). Cost impact: near-zero. Gateway adds a few ms of latency and enables response caching — could *reduce* cost on identical prompts within the configurable cache TTL. Uninstall: new option covered by existing prautoblogger_% LIKE sweep. Implements Commit 1b (narrow AI-Gateway-migration scope) per thread image-pipeline seq 7. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
116 lines
4.2 KiB
PHP
116 lines
4.2 KiB
PHP
<?php
|
|
/**
|
|
* Tests for PRAutoBlogger_Cloudflare_Image_Support — URL building in
|
|
* particular. The v0.8.2 gateway migration added a derivation path from
|
|
* the existing `prautoblogger_ai_gateway_base_url` OpenRouter gateway
|
|
* URL to the corresponding Workers AI URL at the same gateway root.
|
|
*
|
|
* @package PRAutoBlogger\Tests\Providers
|
|
*/
|
|
|
|
namespace PRAutoBlogger\Tests\Providers;
|
|
|
|
use PRAutoBlogger\Tests\BaseTestCase;
|
|
use Brain\Monkey\Functions;
|
|
|
|
class CloudflareImageSupportTest extends BaseTestCase {
|
|
|
|
private const ACCOUNT_ID = 'a16f4baa82a1074c47f696e7cb6d5995';
|
|
private const MODEL = '@cf/black-forest-labs/flux-1-schnell';
|
|
|
|
/**
|
|
* With no gateway URL configured, the support class returns the direct
|
|
* Workers AI API URL — the historical behaviour prior to v0.8.2.
|
|
*/
|
|
public function test_returns_direct_url_when_gateway_not_configured(): void {
|
|
$this->stub_get_option( [
|
|
'prautoblogger_ai_gateway_base_url' => '',
|
|
'prautoblogger_cf_image_via_gateway' => '1',
|
|
] );
|
|
|
|
$support = new \PRAutoBlogger_Cloudflare_Image_Support();
|
|
$url = $support->build_endpoint_url( self::ACCOUNT_ID, self::MODEL );
|
|
|
|
$this->assertStringStartsWith( 'https://api.cloudflare.com/client/v4/accounts/', $url );
|
|
$this->assertStringContainsString( self::ACCOUNT_ID, $url );
|
|
$this->assertStringEndsWith( self::MODEL, $url );
|
|
}
|
|
|
|
/**
|
|
* With the AI Gateway URL configured and the toggle on, image calls
|
|
* route through `gateway.ai.cloudflare.com/.../workers-ai/{model}` at
|
|
* the same gateway root as the OpenRouter LLM path.
|
|
*/
|
|
public function test_returns_gateway_url_when_configured_and_toggle_on(): void {
|
|
$this->stub_get_option( [
|
|
'prautoblogger_ai_gateway_base_url' => 'https://gateway.ai.cloudflare.com/v1/' . self::ACCOUNT_ID . '/peptiderepo-prod/openrouter',
|
|
'prautoblogger_cf_image_via_gateway' => '1',
|
|
] );
|
|
|
|
$support = new \PRAutoBlogger_Cloudflare_Image_Support();
|
|
$url = $support->build_endpoint_url( self::ACCOUNT_ID, self::MODEL );
|
|
|
|
$this->assertSame(
|
|
'https://gateway.ai.cloudflare.com/v1/' . self::ACCOUNT_ID . '/peptiderepo-prod/workers-ai/' . self::MODEL,
|
|
$url
|
|
);
|
|
}
|
|
|
|
/**
|
|
* The toggle overrides the gateway URL: operators can fall back to the
|
|
* direct API if the gateway regresses, without clearing the OpenRouter
|
|
* gateway setting that the LLM provider depends on.
|
|
*/
|
|
public function test_toggle_off_forces_direct_url_even_with_gateway_configured(): void {
|
|
$this->stub_get_option( [
|
|
'prautoblogger_ai_gateway_base_url' => 'https://gateway.ai.cloudflare.com/v1/' . self::ACCOUNT_ID . '/peptiderepo-prod/openrouter',
|
|
'prautoblogger_cf_image_via_gateway' => '0',
|
|
] );
|
|
|
|
$support = new \PRAutoBlogger_Cloudflare_Image_Support();
|
|
$url = $support->build_endpoint_url( self::ACCOUNT_ID, self::MODEL );
|
|
|
|
$this->assertStringStartsWith( 'https://api.cloudflare.com/client/v4/accounts/', $url );
|
|
}
|
|
|
|
/**
|
|
* A malformed gateway URL (e.g. http, or a totally different host) is
|
|
* rejected and the direct-API path is used instead. Defensive — a bad
|
|
* option value shouldn't break image generation.
|
|
*/
|
|
public function test_falls_back_to_direct_when_gateway_url_is_malformed(): void {
|
|
$this->stub_get_option( [
|
|
'prautoblogger_ai_gateway_base_url' => 'http://evil.example.com/openrouter',
|
|
'prautoblogger_cf_image_via_gateway' => '1',
|
|
] );
|
|
|
|
$support = new \PRAutoBlogger_Cloudflare_Image_Support();
|
|
$url = $support->build_endpoint_url( self::ACCOUNT_ID, self::MODEL );
|
|
|
|
$this->assertStringStartsWith( 'https://api.cloudflare.com/client/v4/accounts/', $url );
|
|
}
|
|
|
|
/**
|
|
* The NSFW detector must match on the documented error code without
|
|
* tripping on other 4xx bodies. (Covered at the provider level too,
|
|
* but worth pinning at the support class so a tolerance regression
|
|
* fails early.)
|
|
*/
|
|
public function test_is_nsfw_error_matches_code_3030_only(): void {
|
|
$support = new \PRAutoBlogger_Cloudflare_Image_Support();
|
|
|
|
$this->assertTrue(
|
|
$support->is_nsfw_error( '{"errors":[{"code":3030,"message":"NSFW"}]}' )
|
|
);
|
|
$this->assertFalse(
|
|
$support->is_nsfw_error( '{"errors":[{"code":7003,"message":"bad token"}]}' )
|
|
);
|
|
$this->assertFalse(
|
|
$support->is_nsfw_error( 'not json at all' )
|
|
);
|
|
$this->assertFalse(
|
|
$support->is_nsfw_error( '{}' )
|
|
);
|
|
}
|
|
}
|