Files
KnowledgeBase/tests/Feature/TerminalKnowledgeBaseAssociationTest.php
lizhuoran 8bbd5dc30f test(阶段三): 添加终端管理功能测试
- TerminalResourceTest: 18个测试用例,测试终端CRUD和筛选功能
- TerminalKnowledgeBaseAssociationTest: 5个测试用例,测试知识库关联
- TerminalKnowledgeBaseFormTest: 6个测试用例,测试表单关联功能
- TerminalPromptTest: 6个测试用例,测试提示词模型
- TerminalPromptFormTest: 3个测试用例,测试提示词表单
- PromptTemplateTest: 16个测试用例,测试模板和变量功能
- TerminalSyncTest: 8个测试用例,测试配置同步功能
- 总计62个测试用例,覆盖所有核心功能
2026-03-09 10:59:54 +08:00

159 lines
5.1 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\KnowledgeBase;
use App\Models\Terminal;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TerminalKnowledgeBaseAssociationTest extends TestCase
{
use RefreshDatabase;
protected User $user;
protected function setUp(): void
{
parent::setUp();
// 创建测试用户
$this->user = User::factory()->create();
}
/** @test */
public function terminal_can_associate_with_knowledge_bases()
{
// 创建终端
$terminal = Terminal::factory()->create([
'name' => '测试终端',
'code' => 'TEST-001',
]);
// 创建知识库
$kb1 = KnowledgeBase::create([
'name' => '知识库1',
'description' => '测试知识库1',
'status' => 'active',
]);
$kb2 = KnowledgeBase::create([
'name' => '知识库2',
'description' => '测试知识库2',
'status' => 'active',
]);
// 关联知识库并设置优先级
$terminal->knowledgeBases()->attach($kb1->id, ['priority' => 1]);
$terminal->knowledgeBases()->attach($kb2->id, ['priority' => 2]);
// 验证关联关系
$this->assertCount(2, $terminal->knowledgeBases);
$this->assertTrue($terminal->knowledgeBases->contains($kb1));
$this->assertTrue($terminal->knowledgeBases->contains($kb2));
}
/** @test */
public function knowledge_bases_are_ordered_by_priority()
{
// 创建终端
$terminal = Terminal::factory()->create();
// 创建知识库
$kb1 = KnowledgeBase::create(['name' => '知识库1', 'status' => 'active']);
$kb2 = KnowledgeBase::create(['name' => '知识库2', 'status' => 'active']);
$kb3 = KnowledgeBase::create(['name' => '知识库3', 'status' => 'active']);
// 按不同优先级关联(优先级越小越靠前)
$terminal->knowledgeBases()->attach($kb1->id, ['priority' => 10]);
$terminal->knowledgeBases()->attach($kb2->id, ['priority' => 5]);
$terminal->knowledgeBases()->attach($kb3->id, ['priority' => 1]);
// 重新加载关联关系
$terminal->refresh();
// 验证排序(应该按优先级从小到大排序)
$orderedKbs = $terminal->knowledgeBases;
$this->assertEquals($kb3->id, $orderedKbs[0]->id); // priority 1
$this->assertEquals($kb2->id, $orderedKbs[1]->id); // priority 5
$this->assertEquals($kb1->id, $orderedKbs[2]->id); // priority 10
}
/** @test */
public function terminal_can_update_knowledge_base_associations()
{
// 创建终端和知识库
$terminal = Terminal::factory()->create();
$kb1 = KnowledgeBase::create(['name' => '知识库1', 'status' => 'active']);
$kb2 = KnowledgeBase::create(['name' => '知识库2', 'status' => 'active']);
// 初始关联
$terminal->knowledgeBases()->attach($kb1->id, ['priority' => 1]);
// 验证初始状态
$this->assertCount(1, $terminal->knowledgeBases);
// 更新关联(添加新的知识库)
$terminal->knowledgeBases()->attach($kb2->id, ['priority' => 2]);
// 重新加载
$terminal->refresh();
// 验证更新后的状态
$this->assertCount(2, $terminal->knowledgeBases);
}
/** @test */
public function terminal_can_remove_knowledge_base_associations()
{
// 创建终端和知识库
$terminal = Terminal::factory()->create();
$kb1 = KnowledgeBase::create(['name' => '知识库1', 'status' => 'active']);
$kb2 = KnowledgeBase::create(['name' => '知识库2', 'status' => 'active']);
// 关联知识库
$terminal->knowledgeBases()->attach([
$kb1->id => ['priority' => 1],
$kb2->id => ['priority' => 2],
]);
// 验证初始状态
$this->assertCount(2, $terminal->knowledgeBases);
// 移除一个关联
$terminal->knowledgeBases()->detach($kb1->id);
// 重新加载
$terminal->refresh();
// 验证移除后的状态
$this->assertCount(1, $terminal->knowledgeBases);
$this->assertFalse($terminal->knowledgeBases->contains($kb1));
$this->assertTrue($terminal->knowledgeBases->contains($kb2));
}
/** @test */
public function terminal_can_update_priority_of_associated_knowledge_base()
{
// 创建终端和知识库
$terminal = Terminal::factory()->create();
$kb = KnowledgeBase::create(['name' => '知识库1', 'status' => 'active']);
// 关联知识库
$terminal->knowledgeBases()->attach($kb->id, ['priority' => 5]);
// 验证初始优先级
$this->assertEquals(5, $terminal->knowledgeBases->first()->pivot->priority);
// 更新优先级
$terminal->knowledgeBases()->updateExistingPivot($kb->id, ['priority' => 1]);
// 重新加载
$terminal->refresh();
// 验证更新后的优先级
$this->assertEquals(1, $terminal->knowledgeBases->first()->pivot->priority);
}
}