Add core classes and tests for Groq AI compatibility, logging, and model services

- Implement Groq_AI_Compatibility_Service to manage WooCommerce dependency and admin notices.
- Create Groq_AI_Log_Scheduler for scheduled log cleanup based on settings.
- Develop Groq_AI_Model_Service for model selection and caching.
- Add language translations in POT file for Dutch.
- Set up PHPUnit configuration and bootstrap for testing.
- Implement unit tests for model exclusions, provider request building, settings management, and term saving functionality.
This commit is contained in:
2026-01-31 17:48:46 +00:00
parent 26aabdb2d8
commit 6cff0b6f58
25 changed files with 3131 additions and 368 deletions

View File

@@ -0,0 +1,17 @@
<?php
use PHPUnit\Framework\TestCase;
class ModelExclusionsTest extends TestCase {
public function test_ensure_allowed_blocks_excluded_model() {
$blocked = Groq_AI_Model_Exclusions::ensure_allowed( 'groq', 'whisper-large-v3' );
$this->assertSame( '', $blocked );
}
public function test_filter_models_removes_excluded_entries() {
$models = [ 'llama3-70b-8192', 'whisper-large-v3', 'mixtral-8x7b-32768' ];
$filtered = Groq_AI_Model_Exclusions::filter_models( 'groq', $models );
$this->assertSame( [ 'llama3-70b-8192', 'mixtral-8x7b-32768' ], $filtered );
}
}

View File

@@ -0,0 +1,102 @@
<?php
use PHPUnit\Framework\TestCase;
class ProviderRequestBuilderTest extends TestCase {
public function test_openai_request_payload_respects_settings() {
$provider = new Groq_AI_Provider_OpenAI();
$result = $provider->generate_content(
[
'prompt' => 'Hallo',
'system_prompt' => 'System',
'model' => 'gpt-4o-mini',
'settings' => [
'openai_api_key' => 'test-key',
'max_output_tokens' => 512,
],
'temperature' => 0.5,
'response_format' => [
'type' => 'json_object',
],
]
);
$this->assertIsArray( $result );
$payload = $result['request_payload']['body'];
$this->assertSame( 'gpt-4o-mini', $payload['model'] );
$this->assertSame( 0.5, $payload['temperature'] );
$this->assertSame( 512, $payload['max_tokens'] );
$this->assertSame( 'json_object', $payload['response_format']['type'] );
$this->assertSame( 'System', $payload['messages'][0]['content'] );
$this->assertSame( 'Hallo', $payload['messages'][1]['content'] );
}
public function test_groq_request_payload_uses_default_model_when_missing() {
$provider = new Groq_AI_Provider_Groq();
$result = $provider->generate_content(
[
'prompt' => 'Hallo',
'system_prompt' => 'System',
'settings' => [
'groq_api_key' => 'test-key',
],
]
);
$this->assertIsArray( $result );
$payload = $result['request_payload']['body'];
$this->assertSame( $provider->get_default_model(), $payload['model'] );
$this->assertSame( 'System', $payload['messages'][0]['content'] );
$this->assertSame( 'Hallo', $payload['messages'][1]['content'] );
}
public function test_google_request_payload_builds_schema_and_images() {
$provider = new Groq_AI_Provider_Google();
$result = $provider->generate_content(
[
'prompt' => 'Hallo',
'system_prompt' => 'System',
'model' => 'gemini-1.5-flash',
'settings' => [
'google_api_key' => 'test-key',
'max_output_tokens' => 256,
'google_safety_settings' => [
'HARM_CATEGORY_HARASSMENT' => 'BLOCK_LOW_AND_ABOVE',
],
],
'temperature' => 0.2,
'response_format' => [
'type' => 'json_schema',
'json_schema' => [
'schema' => [
'type' => 'object',
'properties' => [
'name' => [
'type' => 'string',
],
],
],
],
],
'image_context' => [
[
'label' => 'Image 1',
'mime_type' => 'image/png',
'data' => 'BASE64DATA',
],
],
]
);
$this->assertIsArray( $result );
$payload = $result['request_payload']['body'];
$this->assertSame( 0.2, $payload['generationConfig']['temperature'] );
$this->assertSame( 256, $payload['generationConfig']['maxOutputTokens'] );
$this->assertSame( 'application/json', $payload['generationConfig']['responseMimeType'] );
$this->assertArrayHasKey( 'responseJsonSchema', $payload['generationConfig'] );
$this->assertSame( 'System', $payload['contents'][0]['parts'][0]['text'] );
$this->assertSame( 'Hallo', $payload['contents'][0]['parts'][1]['text'] );
$this->assertSame( 'image/png', $payload['contents'][0]['parts'][3]['inline_data']['mime_type'] );
$this->assertSame( 'BASE64DATA', $payload['contents'][0]['parts'][3]['inline_data']['data'] );
}
}

View File

@@ -0,0 +1,112 @@
<?php
use PHPUnit\Framework\TestCase;
class SettingsManagerTest extends TestCase {
private function make_manager() {
$provider_manager = new Groq_AI_Provider_Manager();
return new Groq_AI_Settings_Manager( 'groq_ai_test_settings', $provider_manager );
}
public function test_logs_retention_days_sanitized_and_capped() {
$manager = $this->make_manager();
$result = $manager->sanitize( [
'logs_retention_days' => 5000,
] );
$this->assertSame( 3650, $result['logs_retention_days'] );
}
public function test_logs_retention_days_allows_zero() {
$manager = $this->make_manager();
$result = $manager->sanitize( [
'logs_retention_days' => 0,
] );
$this->assertSame( 0, $result['logs_retention_days'] );
}
public function test_logs_retention_days_negative_becomes_zero() {
$manager = $this->make_manager();
$result = $manager->sanitize( [
'logs_retention_days' => -5,
] );
$this->assertSame( 0, $result['logs_retention_days'] );
}
public function test_sanitize_accepts_all_settings_keys() {
$manager = $this->make_manager();
$context_fields = $manager->get_default_context_fields();
$modules = $manager->get_default_modules_settings();
$google_categories = Groq_AI_Settings_Manager::get_google_safety_categories_list();
$first_category = array_key_first( $google_categories );
$input = [
'provider' => 'openai',
'model' => 'gpt-4o-mini',
'store_context' => 'Test winkelcontext',
'default_prompt' => 'Schrijf een korte tekst',
'max_output_tokens' => 2048,
'logs_retention_days' => 30,
'product_attribute_includes' => [ '__all__', '__custom__', 'pa_color', 'invalid key' ],
'term_bottom_description_meta_key' => 'custom_bottom_key',
'groq_api_key' => 'groq-key',
'openai_api_key' => 'openai-key',
'google_api_key' => 'google-key',
'google_oauth_client_id' => 'client-id',
'google_oauth_client_secret' => 'client-secret',
'google_oauth_refresh_token' => 'refresh-token',
'google_oauth_connected_email' => 'user@example.com',
'google_oauth_connected_at' => 123456,
'google_enable_gsc' => true,
'google_enable_ga' => false,
'google_gsc_site_url' => 'https://example.com/',
'google_ga4_property_id' => '123456',
'google_safety_settings' => $first_category ? [ $first_category => 'BLOCK_LOW_AND_ABOVE' ] : [],
'context_fields' => $context_fields,
'modules' => $modules,
'image_context_mode' => 'base64',
'image_context_limit' => 5,
'response_format_compat' => true,
'term_top_description_char_limit' => 700,
'term_bottom_description_char_limit' => 1400,
];
$result = $manager->sanitize( $input );
$this->assertSame( 'openai', $result['provider'] );
$this->assertSame( 'gpt-4o-mini', $result['model'] );
$this->assertSame( 'Test winkelcontext', $result['store_context'] );
$this->assertSame( 'Schrijf een korte tekst', $result['default_prompt'] );
$this->assertSame( 2048, $result['max_output_tokens'] );
$this->assertSame( 30, $result['logs_retention_days'] );
$this->assertContains( '__all__', $result['product_attribute_includes'] );
$this->assertContains( '__custom__', $result['product_attribute_includes'] );
$this->assertContains( 'pa_color', $result['product_attribute_includes'] );
$this->assertSame( 'custom_bottom_key', $result['term_bottom_description_meta_key'] );
$this->assertSame( 'groq-key', $result['groq_api_key'] );
$this->assertSame( 'openai-key', $result['openai_api_key'] );
$this->assertSame( 'google-key', $result['google_api_key'] );
$this->assertSame( 'client-id', $result['google_oauth_client_id'] );
$this->assertSame( 'client-secret', $result['google_oauth_client_secret'] );
$this->assertSame( 'refresh-token', $result['google_oauth_refresh_token'] );
$this->assertSame( 'user@example.com', $result['google_oauth_connected_email'] );
$this->assertSame( 123456, $result['google_oauth_connected_at'] );
$this->assertTrue( $result['google_enable_gsc'] );
$this->assertFalse( $result['google_enable_ga'] );
$this->assertSame( 'https://example.com/', $result['google_gsc_site_url'] );
$this->assertSame( '123456', $result['google_ga4_property_id'] );
$this->assertIsArray( $result['google_safety_settings'] );
$this->assertIsArray( $result['context_fields'] );
$this->assertIsArray( $result['modules'] );
$this->assertSame( 'base64', $result['image_context_mode'] );
$this->assertSame( 5, $result['image_context_limit'] );
$this->assertTrue( $result['response_format_compat'] );
$this->assertSame( 700, $result['term_top_description_char_limit'] );
$this->assertSame( 1400, $result['term_bottom_description_char_limit'] );
}
}

67
tests/TermSaveTest.php Normal file
View File

@@ -0,0 +1,67 @@
<?php
use PHPUnit\Framework\TestCase;
class TermSaveTest extends TestCase {
protected function setUp(): void {
$GLOBALS['wp_term_updates'] = [];
$GLOBALS['wp_term_meta_updates'] = [];
$GLOBALS['wp_filters'] = [];
}
public function test_save_term_generation_result_saves_descriptions_and_filtered_meta_key() {
$plugin = new class {
public function get_settings() {
return [ 'term_bottom_description_meta_key' => '' ];
}
public function is_module_enabled( $module, $settings = null ) {
return false;
}
};
$controller_ref = new ReflectionClass( Groq_AI_Ajax_Controller::class );
$controller = $controller_ref->newInstanceWithoutConstructor();
$plugin_prop = $controller_ref->getProperty( 'plugin' );
$plugin_prop->setAccessible( true );
$plugin_prop->setValue( $controller, $plugin );
add_filter(
'groq_ai_term_bottom_description_meta_key',
function ( $default_key ) {
return 'Custom Key';
},
10,
3
);
$term = (object) [
'term_id' => 12,
'taxonomy' => 'product_cat',
'name' => 'Test',
'description' => '',
];
$result = [
'top_description' => '<p>Dit is een test.</p>',
'bottom_description' => '<p>Onderste tekst.</p>',
];
$settings = $plugin->get_settings();
$method = $controller_ref->getMethod( 'save_term_generation_result' );
$method->setAccessible( true );
$saved = $method->invoke( $controller, $term, $result, $settings );
$this->assertIsArray( $saved );
$this->assertSame( 4, $saved['words'] );
$this->assertCount( 1, $GLOBALS['wp_term_updates'] );
$this->assertSame( 12, $GLOBALS['wp_term_updates'][0]['term_id'] );
$this->assertSame( 'product_cat', $GLOBALS['wp_term_updates'][0]['taxonomy'] );
$this->assertSame( '<p>Dit is een test.</p>', $GLOBALS['wp_term_updates'][0]['args']['description'] );
$this->assertArrayHasKey( 12, $GLOBALS['wp_term_meta_updates'] );
$this->assertArrayHasKey( 'customkey', $GLOBALS['wp_term_meta_updates'][12] );
$this->assertSame( '<p>Onderste tekst.</p>', $GLOBALS['wp_term_meta_updates'][12]['customkey'] );
}
}

258
tests/bootstrap.php Normal file
View File

@@ -0,0 +1,258 @@
<?php
if ( ! defined( 'GROQ_AI_PRODUCT_TEXT_DOMAIN' ) ) {
define( 'GROQ_AI_PRODUCT_TEXT_DOMAIN', 'siti-ai-product-content-generator' );
}
if ( ! defined( 'HOUR_IN_SECONDS' ) ) {
define( 'HOUR_IN_SECONDS', 3600 );
}
if ( ! defined( 'DAY_IN_SECONDS' ) ) {
define( 'DAY_IN_SECONDS', 86400 );
}
if ( ! class_exists( 'WP_Error' ) ) {
class WP_Error {
private $message;
public function __construct( $code = '', $message = '' ) {
$this->message = (string) $message;
}
public function get_error_message() {
return $this->message;
}
}
}
if ( ! function_exists( 'is_wp_error' ) ) {
function is_wp_error( $thing ) {
return $thing instanceof WP_Error;
}
}
if ( ! function_exists( '__' ) ) {
function __( $text ) {
return $text;
}
}
if ( ! function_exists( 'wp_parse_args' ) ) {
function wp_parse_args( $args, $defaults = [] ) {
if ( is_object( $args ) ) {
$args = get_object_vars( $args );
}
if ( ! is_array( $args ) ) {
$args = [];
}
return array_merge( $defaults, $args );
}
}
if ( ! function_exists( 'sanitize_text_field' ) ) {
function sanitize_text_field( $text ) {
return trim( (string) $text );
}
}
if ( ! function_exists( 'sanitize_textarea_field' ) ) {
function sanitize_textarea_field( $text ) {
return trim( (string) $text );
}
}
if ( ! function_exists( 'sanitize_key' ) ) {
function sanitize_key( $key ) {
$key = strtolower( (string) $key );
return preg_replace( '/[^a-z0-9_\-]/', '', $key );
}
}
if ( ! function_exists( 'absint' ) ) {
function absint( $number ) {
return abs( (int) $number );
}
}
if ( ! function_exists( 'esc_url_raw' ) ) {
function esc_url_raw( $url ) {
return (string) $url;
}
}
if ( ! function_exists( 'add_filter' ) ) {
function add_filter( $tag, $callback, $priority = 10, $accepted_args = 1 ) {
$GLOBALS['wp_filters'][ $tag ][ $priority ][] = [
'callback' => $callback,
'accepted_args' => (int) $accepted_args,
];
}
}
if ( ! function_exists( 'apply_filters' ) ) {
function apply_filters( $tag, $value ) {
$args = func_get_args();
if ( empty( $GLOBALS['wp_filters'][ $tag ] ) ) {
return $value;
}
ksort( $GLOBALS['wp_filters'][ $tag ] );
foreach ( $GLOBALS['wp_filters'][ $tag ] as $callbacks ) {
foreach ( $callbacks as $filter ) {
$accepted = isset( $filter['accepted_args'] ) ? (int) $filter['accepted_args'] : 1;
$call_args = array_slice( $args, 0, max( 1, $accepted ) );
$call_args[0] = $value;
$value = call_user_func_array( $filter['callback'], $call_args );
$args[0] = $value;
}
}
return $value;
}
}
if ( ! function_exists( 'wp_kses_post' ) ) {
function wp_kses_post( $content ) {
return (string) $content;
}
}
if ( ! function_exists( 'wp_strip_all_tags' ) ) {
function wp_strip_all_tags( $text ) {
return strip_tags( (string) $text );
}
}
if ( ! function_exists( 'wp_update_term' ) ) {
function wp_update_term( $term_id, $taxonomy, $args = [] ) {
$GLOBALS['wp_term_updates'][] = [
'term_id' => (int) $term_id,
'taxonomy' => (string) $taxonomy,
'args' => $args,
];
return [ 'term_id' => (int) $term_id ];
}
}
if ( ! function_exists( 'update_term_meta' ) ) {
function update_term_meta( $term_id, $meta_key, $meta_value ) {
$term_id = (int) $term_id;
if ( ! isset( $GLOBALS['wp_term_meta_updates'][ $term_id ] ) ) {
$GLOBALS['wp_term_meta_updates'][ $term_id ] = [];
}
$GLOBALS['wp_term_meta_updates'][ $term_id ][ (string) $meta_key ] = $meta_value;
return true;
}
}
if ( ! function_exists( 'wp_json_encode' ) ) {
function wp_json_encode( $data, $options = 0, $depth = 512 ) {
return json_encode( $data, $options, $depth );
}
}
if ( ! function_exists( 'add_query_arg' ) ) {
function add_query_arg( $args, $url = '' ) {
if ( is_string( $args ) ) {
return $url;
}
$query = http_build_query( (array) $args );
$separator = strpos( $url, '?' ) === false ? '?' : '&';
return $url . $separator . $query;
}
}
if ( ! function_exists( 'wp_remote_post' ) ) {
function wp_remote_post( $url, $args = [] ) {
$GLOBALS['wp_last_http_request'] = [
'url' => $url,
'args' => $args,
];
$body = json_encode(
[
'choices' => [
[
'message' => [
'content' => 'ok',
],
'finish_reason' => 'stop',
],
],
'usage' => [
'prompt_tokens' => 10,
'completion_tokens' => 20,
'total_tokens' => 30,
],
'candidates' => [
[
'content' => [
'parts' => [
[ 'text' => 'ok' ],
],
],
'finishReason' => 'STOP',
],
],
'usageMetadata' => [
'promptTokenCount' => 10,
'candidatesTokenCount' => 20,
'totalTokenCount' => 30,
],
]
);
return [
'body' => $body,
'response' => [ 'code' => 200 ],
];
}
}
if ( ! function_exists( 'wp_remote_get' ) ) {
function wp_remote_get( $url, $args = [] ) {
$GLOBALS['wp_last_http_request'] = [
'url' => $url,
'args' => $args,
];
return [
'body' => json_encode( [ 'data' => [], 'models' => [] ] ),
'response' => [ 'code' => 200 ],
];
}
}
if ( ! function_exists( 'wp_remote_retrieve_body' ) ) {
function wp_remote_retrieve_body( $response ) {
return isset( $response['body'] ) ? $response['body'] : '';
}
}
if ( ! function_exists( 'wp_remote_retrieve_response_code' ) ) {
function wp_remote_retrieve_response_code( $response ) {
return isset( $response['response']['code'] ) ? (int) $response['response']['code'] : 0;
}
}
if ( ! function_exists( 'get_option' ) ) {
function get_option( $key, $default = false ) {
return isset( $GLOBALS['wp_options'][ $key ] ) ? $GLOBALS['wp_options'][ $key ] : $default;
}
}
if ( ! function_exists( 'update_option' ) ) {
function update_option( $key, $value ) {
$GLOBALS['wp_options'][ $key ] = $value;
return true;
}
}
require_once __DIR__ . '/../includes/Core/class-groq-ai-model-exclusions.php';
require_once __DIR__ . '/../includes/Contracts/interface-groq-ai-provider.php';
require_once __DIR__ . '/../includes/Providers/class-groq-ai-abstract-openai-provider.php';
require_once __DIR__ . '/../includes/Providers/class-groq-ai-provider-groq.php';
require_once __DIR__ . '/../includes/Providers/class-groq-ai-provider-openai.php';
require_once __DIR__ . '/../includes/Providers/class-groq-ai-provider-google.php';
require_once __DIR__ . '/../includes/Providers/class-groq-ai-provider-manager.php';
require_once __DIR__ . '/../includes/Services/Settings/class-groq-ai-settings-manager.php';
require_once __DIR__ . '/../includes/Core/class-groq-ai-ajax-controller.php';