php-agentic/tests/Feature/ContentServiceTest.php

84 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2026-01-27 00:28:29 +00:00
<?php
use Core\Mod\Agentic\Services\AgenticManager;
use Core\Mod\Agentic\Services\AgenticProviderInterface;
use Core\Mod\Agentic\Services\ContentService;
2026-01-27 00:28:29 +00:00
use Illuminate\Support\Facades\File;
beforeEach(function () {
$this->manager = Mockery::mock(AgenticManager::class);
$this->service = new ContentService($this->manager);
});
it('lists available batches', function () {
$batches = $this->service->listBatches();
expect($batches)->toBeArray();
expect(count($batches))->toBeGreaterThan(0);
// Check the first batch found
$firstBatch = collect($batches)->firstWhere('id', 'batch-001-link-getting-started');
expect($firstBatch)->not->toBeNull();
expect($firstBatch)->toHaveKeys(['id', 'service', 'category', 'article_count']);
expect($firstBatch['service'])->toBe('Host Link');
});
it('loads a specific batch', function () {
$batch = $this->service->loadBatch('batch-001-link-getting-started');
expect($batch)->toBeArray();
expect($batch['service'])->toBe('Host Link');
expect($batch['articles'])->toBeArray();
expect(count($batch['articles']))->toBeGreaterThan(0);
});
it('generates content for a batch (dry run)', function () {
$results = $this->service->generateBatch('batch-001-link-getting-started', 'gemini', true);
expect($results['batch_id'])->toBe('batch-001-link-getting-started');
expect($results['articles'])->not->toBeEmpty();
foreach ($results['articles'] as $slug => $status) {
expect($status['status'])->toBe('would_generate');
}
});
it('handles generation errors gracefully', function () {
$provider = Mockery::mock(AgenticProviderInterface::class);
$provider->shouldReceive('generate')->andThrow(new \Exception('API Error'));
$this->manager->shouldReceive('provider')->with('gemini')->andReturn($provider);
// Create a temporary test batch file
$testBatchPath = base_path('app/Mod/Agentic/Resources/tasks/batch-test-error.md');
// Ensure the prompts directory exists for the test if it's looking for a template
$promptPath = base_path('app/Mod/Agentic/Resources/prompts/content/help-article.md');
// We need to ensure the help-article prompt exists, otherwise it fails before hitting the API
if (! File::exists($promptPath)) {
$this->markTestSkipped('Help article prompt not found');
}
File::put($testBatchPath, "# Test Batch\n**Service:** Test\n### Article 1:\n```yaml\nSLUG: test-slug-error\nTITLE: Test\n```");
// Clean up potential leftover draft
$draftPath = base_path('app/Mod/Agentic/Resources/drafts/help/general/test-slug-error.md');
if (File::exists($draftPath)) {
File::delete($draftPath);
}
try {
$results = $this->service->generateBatch('batch-test-error', 'gemini', false);
expect($results['failed'])->toBe(1);
expect($results['articles']['test-slug-error']['status'])->toBe('failed');
expect($results['articles']['test-slug-error']['error'])->toBe('API Error');
} finally {
if (File::exists($testBatchPath)) {
File::delete($testBatchPath);
}
if (File::exists($draftPath)) {
File::delete($draftPath);
}
}
});