Files
laralog/tests/Feature/AiModerationTest.php
T
gouki 2e052498a5
Build and Deploy / verify (push) Successful in 10s
Build and Deploy / deploy (push) Successful in 20s
feat: AI 任务同步执行、评论垃圾管理、会员导航分组、多 AI 配置
- 评论 AI 审核/文章润色改为同步执行(站点无 queue worker,任务曾永久卡队列,
  导致垃圾评论滞留待审核、润色毫无效果);审核在响应结束后经 terminating 触发
- 评论表:修复 approve 闭包里误嵌入 recordUrl 的坏链;新增「非垃圾」恢复发布、
  批量「标为垃圾」、工具栏「清空垃圾评论」;各操作补充通知反馈
- 会员套餐/订阅/订单移入新导航组「会员」
- LlmClient 支持多服务商 profile;AI 设置页可添加多个 AI 并为审核/润色/封面
  分别指定服务商(不指定用默认配置);封面生成同步支持 profile
- AI 设置页:润色改同步并即时展示结果,按钮加 loading;删除无 workerman 的
  WebSocket 进度区块
2026-10-15 00:06:55 +00:00

116 lines
3.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Blog\Services\LlmClient;
use App\Models\Comment;
use App\Models\Post;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Plugins\Neatstudio\AiModeration\Jobs\AiModerateCommentJob;
use Tests\TestCase;
class AiModerationTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->seed();
Setting::set('ai_moderation_enabled', '1');
Setting::set('comment_audit', '1'); // 新评论先进入待审核
Setting::set('llm_base_url', 'https://example.test/v1');
Setting::set('llm_api_key', 'test-key');
Setting::set('llm_model', 'test-model');
}
public function test_comment_created_runs_moderation_after_response(): void
{
$post = Post::create([
'title' => '评论审核',
'slug' => 'ai-comment',
'content' => '正文',
'content_format' => 'markdown',
'status' => 'published',
'published_at' => now(),
]);
$llm = $this->mock(LlmClient::class);
$llm->shouldReceive('isConfigured')->andReturn(true);
$llm->shouldReceive('forProfile')->andReturnSelf();
$llm->shouldReceive('chat')->andReturn('{"verdict":"approved","reason":"正常"}');
$this->post('/posts/'.$post->id.'/comments', [
'author_name' => '访客',
'content' => '这是一条需要审核的评论',
]);
// 审核在响应结束后同步执行(app()->terminating),测试请求周期内即完成
$comment = Comment::query()->latest('id')->first();
$this->assertSame('published', $comment->status);
$this->assertStringContainsString('approved', $comment->ai_review['raw'] ?? '');
}
public function test_moderation_job_approves_comment(): void
{
$post = Post::create([
'title' => '评论审核',
'slug' => 'ai-comment-2',
'content' => '正文',
'content_format' => 'markdown',
'status' => 'published',
'published_at' => now(),
]);
$comment = Comment::create([
'post_id' => $post->id,
'author_name' => '访客',
'content' => '正常评论内容',
'status' => 'pending',
'created_at' => now(),
]);
$llm = $this->mock(LlmClient::class);
$llm->shouldReceive('forProfile')->andReturnSelf();
$llm->shouldReceive('chat')->once()->andReturn('{"verdict":"approved","reason":"正常"}');
(new AiModerateCommentJob($comment->id))->handle($llm);
$comment->refresh();
$this->assertSame('published', $comment->status);
$this->assertStringContainsString('approved', $comment->ai_review['raw'] ?? '');
}
public function test_moderation_job_rejects_spam(): void
{
$post = Post::create([
'title' => '评论审核',
'slug' => 'ai-comment-3',
'content' => '正文',
'content_format' => 'markdown',
'status' => 'published',
'published_at' => now(),
]);
$comment = Comment::create([
'post_id' => $post->id,
'author_name' => '广告机',
'content' => '点击链接赚钱',
'status' => 'pending',
'created_at' => now(),
]);
$llm = $this->mock(LlmClient::class);
$llm->shouldReceive('forProfile')->andReturnSelf();
$llm->shouldReceive('chat')->once()->andReturn('{"verdict":"spam","reason":"广告"}');
(new AiModerateCommentJob($comment->id))->handle($llm);
$comment->refresh();
$this->assertSame('spam', $comment->status);
}
}