Files
SkinbaseNova/tests/Feature/AiBiographyAdminTest.php
2026-04-18 17:02:56 +02:00

207 lines
8.1 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\CreatorAiBiography;
use App\Models\Artwork;
use App\Models\User;
use App\Services\AiBiography\AiBiographyGenerator;
use App\Services\AiBiography\AiBiographyInputBuilder;
use App\Services\AiBiography\AiBiographyService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia;
use Klevze\ControlPanel\Core\Structs\MenuRootItem;
use Klevze\ControlPanel\Framework\Core\Menu as ControlPanelMenu;
use Klevze\ControlPanel\Models\Admin\AdminVerification;
uses(RefreshDatabase::class);
function aiBiographyAdminUser(array $attributes = []): User
{
$admin = User::factory()->create(array_merge(['role' => 'admin'], $attributes));
$admin->forceFill([
'isAdmin' => true,
'activated' => true,
])->save();
AdminVerification::createForUser($admin->fresh());
return $admin->fresh();
}
function biographyRecord(User $user, array $attributes = []): CreatorAiBiography
{
return CreatorAiBiography::query()->create(array_merge([
'user_id' => $user->id,
'text' => 'This creator has built a consistent public body of work on Skinbase, with a long-running profile, visible uploads, and a biography long enough to support admin review without tripping validation rules.',
'source_hash' => 'hash-' . fake()->unique()->numerify('####'),
'model' => 'vision-gateway',
'prompt_version' => 'v1.1',
'input_quality_tier' => CreatorAiBiography::TIER_MEDIUM,
'generation_reason' => CreatorAiBiography::REASON_ADMIN_BATCH,
'status' => CreatorAiBiography::STATUS_GENERATED,
'is_active' => true,
'is_hidden' => false,
'is_user_edited' => false,
'needs_review' => false,
'generated_at' => now()->subHour(),
'approved_at' => now()->subHour(),
'last_attempted_at' => now()->subHour(),
'last_error_code' => null,
'last_error_reason' => null,
], $attributes));
}
it('blocks non staff users from the ai biography admin area', function (): void {
$user = User::factory()->create(['role' => 'user']);
$this->actingAs($user)
->get(route('cp.ai-biography.index'))
->assertRedirect(route('cp.login'));
});
it('renders the ai biography admin index with records and stats', function (): void {
$admin = aiBiographyAdminUser();
$creator = User::factory()->create(['username' => 'bioadmin']);
biographyRecord($creator, [
'needs_review' => true,
'status' => CreatorAiBiography::STATUS_NEEDS_REVIEW,
]);
biographyRecord($creator, [
'is_active' => false,
'status' => CreatorAiBiography::STATUS_FAILED,
'last_error_code' => 'generation_failed',
'last_error_reason' => 'Gateway timeout',
]);
$this->actingAs($admin)->actingAs($admin, 'controlpanel')
->get(route('cp.ai-biography.index'))
->assertOk()
->assertInertia(fn (AssertableInertia $page) => $page
->component('Moderation/AiBiographyAdmin')
->where('stats.total_records', 2)
->where('stats.needs_review', 1)
->where('stats.failed', 1)
->where('records.data.0.user.username', 'bioadmin')
->where('records.data.0.status', CreatorAiBiography::STATUS_FAILED)
->where('records.data.1.status', CreatorAiBiography::STATUS_NEEDS_REVIEW)
->where('endpoints.rebuildPattern', route('cp.ai-biography.rebuild', ['user' => '__USER__'])));
});
it('allows controlpanel-only admins to open the ai biography admin page', function (): void {
$admin = aiBiographyAdminUser();
$this->actingAs($admin, 'controlpanel')
->get(route('cp.ai-biography.index'))
->assertOk()
->assertInertia(fn (AssertableInertia $page) => $page
->component('Moderation/AiBiographyAdmin')
->where('title', 'AI Biography Review'));
});
it('registers the ai biography entry in the cpad artworks menu', function (): void {
$admin = aiBiographyAdminUser();
$creator = User::factory()->create(['username' => 'menubio']);
biographyRecord($creator, [
'needs_review' => true,
'status' => CreatorAiBiography::STATUS_NEEDS_REVIEW,
]);
$this->actingAs($admin)->actingAs($admin, 'controlpanel')
->get(route('cp.ai-biography.index'))
->assertOk();
$sidebarMenu = collect(app(ControlPanelMenu::class)->getSidebarMenu());
$artworksRoot = $sidebarMenu
->first(fn ($item): bool => $item instanceof MenuRootItem && $item->getName() === 'Artworks');
expect($artworksRoot)->toBeInstanceOf(MenuRootItem::class);
$aiBiographyItem = collect($artworksRoot->getItems())
->first(fn ($item): bool => str_starts_with((string) ($item->name ?? ''), 'AI Biographies'));
expect($aiBiographyItem)->not->toBeNull()
->and($aiBiographyItem->mainRoute)->toBe('cp.ai-biography.index')
->and($aiBiographyItem->icon)->toBe('fa-solid fa-feather-pointed');
});
it('rebuilds an existing active biography through the admin surface', function (): void {
$admin = aiBiographyAdminUser();
$creator = User::factory()->create(['username' => 'rebuildme']);
biographyRecord($creator);
for ($i = 0; $i < 5; $i++) {
Artwork::factory()->for($creator)->create([
'is_public' => true,
'is_approved' => true,
'published_at' => now()->subDays($i + 1),
'deleted_at' => null,
]);
}
$generator = Mockery::mock(AiBiographyGenerator::class);
$generator->shouldReceive('generate')
->once()
->andReturn([
'success' => true,
'text' => 'This refreshed biography gives the admin panel enough verified text to create a fresh active record while keeping the assertions here stable and predictable for the test suite.',
'action' => 'generated',
'errors' => [],
'model' => 'test-model',
'prompt_version' => 'v1.1',
'was_retried' => false,
]);
app()->instance(AiBiographyService::class, new AiBiographyService(new AiBiographyInputBuilder(), $generator));
$this->actingAs($admin)->actingAs($admin, 'controlpanel')
->postJson(route('cp.ai-biography.rebuild', ['user' => $creator->id]))
->assertOk()
->assertJsonPath('success', true)
->assertJsonPath('message', 'Biography rebuild completed.');
expect(CreatorAiBiography::query()->where('user_id', $creator->id)->where('is_active', true)->count())->toBe(1)
->and(CreatorAiBiography::query()->where('user_id', $creator->id)->count())->toBe(2);
});
it('allows admins to approve flag and toggle visibility on biography records', function (): void {
$admin = aiBiographyAdminUser();
$creator = User::factory()->create(['username' => 'reviewstate']);
$record = biographyRecord($creator, [
'needs_review' => true,
'status' => CreatorAiBiography::STATUS_NEEDS_REVIEW,
]);
$this->actingAs($admin)->actingAs($admin, 'controlpanel')
->postJson(route('cp.ai-biography.approve', ['biography' => $record->id]))
->assertOk()
->assertJsonPath('success', true);
expect($record->fresh()->needs_review)->toBeFalse()
->and($record->fresh()->status)->toBe(CreatorAiBiography::STATUS_APPROVED);
$this->actingAs($admin)->actingAs($admin, 'controlpanel')
->postJson(route('cp.ai-biography.hide', ['biography' => $record->id]))
->assertOk()
->assertJsonPath('success', true);
expect($record->fresh()->is_hidden)->toBeTrue();
$this->actingAs($admin)->actingAs($admin, 'controlpanel')
->postJson(route('cp.ai-biography.show', ['biography' => $record->id]))
->assertOk()
->assertJsonPath('success', true);
expect($record->fresh()->is_hidden)->toBeFalse();
$this->actingAs($admin)->actingAs($admin, 'controlpanel')
->postJson(route('cp.ai-biography.flag', ['biography' => $record->id]))
->assertOk()
->assertJsonPath('success', true);
expect($record->fresh()->needs_review)->toBeTrue()
->and($record->fresh()->status)->toBe(CreatorAiBiography::STATUS_NEEDS_REVIEW);
});