feat: 删除 知识库-终端 关联, 简化 prompt 配置

This commit is contained in:
2026-03-23 15:27:06 +08:00
parent 81a22a2b54
commit 89af7c17f1
23 changed files with 102 additions and 1633 deletions

View File

@@ -1,266 +0,0 @@
<?php
namespace Tests\Feature;
use App\Models\Terminal;
use App\Models\TerminalPrompt;
use App\Models\User;
use App\Services\PromptTemplateService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class PromptTemplateTest extends TestCase
{
use RefreshDatabase;
protected PromptTemplateService $service;
protected User $user;
protected Terminal $terminal;
protected function setUp(): void
{
parent::setUp();
$this->service = app(PromptTemplateService::class);
// 创建测试用户
$this->user = User::factory()->create([
'name' => '测试用户',
]);
// 创建测试终端
$this->terminal = Terminal::factory()->create([
'name' => '测试终端',
'code' => 'TEST-001',
'station_id' => 1001,
]);
$this->actingAs($this->user);
}
/** @test */
public function it_can_get_all_templates()
{
$templates = $this->service->getTemplates();
$this->assertIsArray($templates);
$this->assertNotEmpty($templates);
$this->assertArrayHasKey('id', $templates[0]);
$this->assertArrayHasKey('name', $templates[0]);
$this->assertArrayHasKey('content', $templates[0]);
}
/** @test */
public function it_can_get_template_by_id()
{
$template = $this->service->getTemplate('general_assistant');
$this->assertNotNull($template);
$this->assertEquals('general_assistant', $template['id']);
$this->assertArrayHasKey('content', $template);
}
/** @test */
public function it_returns_null_for_invalid_template_id()
{
$template = $this->service->getTemplate('non_existent_template');
$this->assertNull($template);
}
/** @test */
public function it_can_apply_template_to_terminal()
{
$content = $this->service->applyTemplate($this->terminal, 'general_assistant');
$this->assertIsString($content);
$this->assertNotEmpty($content);
$this->assertStringContainsString('{user}', $content);
$this->assertStringContainsString('{station}', $content);
}
/** @test */
public function it_throws_exception_for_invalid_template()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('模板不存在');
$this->service->applyTemplate($this->terminal, 'invalid_template');
}
/** @test */
public function it_can_replace_variables_in_template()
{
$template = '你好,{user}!当前时间是 {time},你在 {station}。';
$result = $this->service->replaceVariables($template, $this->terminal);
$this->assertStringContainsString('测试用户', $result);
$this->assertStringContainsString('工作站 1001', $result);
$this->assertStringNotContainsString('{user}', $result);
$this->assertStringNotContainsString('{station}', $result);
}
/** @test */
public function it_can_get_variable_values()
{
$variables = $this->service->getVariableValues($this->terminal);
$this->assertIsArray($variables);
$this->assertArrayHasKey('user', $variables);
$this->assertArrayHasKey('terminal_name', $variables);
$this->assertArrayHasKey('station', $variables);
$this->assertArrayHasKey('time', $variables);
$this->assertEquals('测试用户', $variables['user']);
$this->assertEquals('测试终端', $variables['terminal_name']);
$this->assertEquals('TEST-001', $variables['terminal_code']);
}
/** @test */
public function it_handles_array_variables_correctly()
{
// 创建知识库关联
$kb1 = \App\Models\KnowledgeBase::factory()->create(['name' => '知识库1']);
$kb2 = \App\Models\KnowledgeBase::factory()->create(['name' => '知识库2']);
$this->terminal->knowledgeBases()->attach([
$kb1->id => ['priority' => 1],
$kb2->id => ['priority' => 2],
]);
$template = '可用知识库:{knowledge_bases}';
$result = $this->service->replaceVariables($template, $this->terminal);
$this->assertStringContainsString('知识库1', $result);
$this->assertStringContainsString('知识库2', $result);
}
/** @test */
public function it_can_preview_template()
{
$template = '你好,{user}!终端:{terminal_name}';
$preview = $this->service->preview($template, $this->terminal);
$this->assertStringContainsString('测试用户', $preview);
$this->assertStringContainsString('测试终端', $preview);
$this->assertStringNotContainsString('{user}', $preview);
}
/** @test */
public function it_can_validate_variables()
{
$validTemplate = '你好,{user}!时间:{time}';
$invalidVariables = $this->service->validateVariables($validTemplate);
$this->assertEmpty($invalidVariables);
}
/** @test */
public function it_detects_invalid_variables()
{
$invalidTemplate = '你好,{user}!无效变量:{invalid_var},另一个:{another_invalid}';
$invalidVariables = $this->service->validateVariables($invalidTemplate);
$this->assertNotEmpty($invalidVariables);
$this->assertContains('invalid_var', $invalidVariables);
$this->assertContains('another_invalid', $invalidVariables);
}
/** @test */
public function it_determines_shift_correctly()
{
// 使用反射访问protected方法
$reflection = new \ReflectionClass($this->service);
$method = $reflection->getMethod('getCurrentShift');
$method->setAccessible(true);
// 测试早班 (8:00-16:00)
$morningTime = now()->setTime(10, 0);
$this->assertEquals('早班', $method->invoke($this->service, $morningTime));
// 测试中班 (16:00-24:00)
$afternoonTime = now()->setTime(18, 0);
$this->assertEquals('中班', $method->invoke($this->service, $afternoonTime));
// 测试夜班 (0:00-8:00)
$nightTime = now()->setTime(2, 0);
$this->assertEquals('夜班', $method->invoke($this->service, $nightTime));
}
/** @test */
public function terminal_can_save_prompt_template()
{
$promptData = [
'prompt_template' => '这是一个测试提示词模板,包含变量 {user} 和 {station}',
'variables' => ['user', 'station'],
];
$prompt = $this->terminal->prompt()->create($promptData);
$this->assertInstanceOf(TerminalPrompt::class, $prompt);
$this->assertEquals($promptData['prompt_template'], $prompt->prompt_template);
$this->assertEquals($promptData['variables'], $prompt->variables);
}
/** @test */
public function terminal_prompt_relationship_works()
{
TerminalPrompt::factory()->create([
'terminal_id' => $this->terminal->id,
'prompt_template' => '测试模板',
]);
$this->terminal->refresh();
$this->assertNotNull($this->terminal->prompt);
$this->assertEquals('测试模板', $this->terminal->prompt->prompt_template);
}
/** @test */
public function config_files_are_properly_structured()
{
// 测试变量配置
$variables = config('prompt_variables.variables');
$this->assertIsArray($variables);
$this->assertNotEmpty($variables);
foreach ($variables as $variable) {
$this->assertArrayHasKey('name', $variable);
$this->assertArrayHasKey('label', $variable);
$this->assertArrayHasKey('description', $variable);
$this->assertArrayHasKey('example', $variable);
$this->assertArrayHasKey('type', $variable);
$this->assertArrayHasKey('category', $variable);
}
// 测试模板配置
$templates = config('prompt_templates.templates');
$this->assertIsArray($templates);
$this->assertNotEmpty($templates);
foreach ($templates as $template) {
$this->assertArrayHasKey('id', $template);
$this->assertArrayHasKey('name', $template);
$this->assertArrayHasKey('description', $template);
$this->assertArrayHasKey('category', $template);
$this->assertArrayHasKey('content', $template);
}
}
/** @test */
public function all_template_variables_are_valid()
{
$templates = config('prompt_templates.templates');
foreach ($templates as $template) {
$invalidVars = $this->service->validateVariables($template['content']);
$this->assertEmpty(
$invalidVars,
"模板 '{$template['name']}' 包含无效变量: " . implode(', ', $invalidVars)
);
}
}
}

View File

@@ -1,158 +0,0 @@
<?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);
}
}

View File

@@ -1,181 +0,0 @@
<?php
namespace Tests\Feature;
use App\Filament\Resources\TerminalResource;
use App\Models\KnowledgeBase;
use App\Models\Terminal;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class TerminalKnowledgeBaseFormTest extends TestCase
{
use RefreshDatabase;
protected User $user;
protected function setUp(): void
{
parent::setUp();
// 创建管理员用户
$this->user = User::factory()->create();
$this->actingAs($this->user);
}
/** @test */
public function terminal_form_has_knowledge_base_association_field()
{
// 创建知识库
KnowledgeBase::create([
'name' => '测试知识库',
'description' => '测试描述',
'status' => 'active',
]);
// 测试创建表单包含知识库关联字段
$component = Livewire::test(TerminalResource\Pages\CreateTerminal::class);
// 验证表单可以渲染
$component->assertSuccessful();
// 验证表单组件存在(通过检查表单实例)
$this->assertNotNull($component->instance()->form);
}
/** @test */
public function terminal_can_be_created_with_knowledge_base_associations()
{
// 创建知识库
$kb1 = KnowledgeBase::create([
'name' => '生产流程知识库',
'status' => 'active',
]);
$kb2 = KnowledgeBase::create([
'name' => '设备维护知识库',
'status' => 'active',
]);
// 创建终端
$terminal = Terminal::factory()->create([
'name' => '测试终端',
'code' => 'TEST-001',
]);
// 手动关联知识库(模拟表单提交后的效果)
$terminal->knowledgeBases()->attach([
$kb1->id => ['priority' => 1],
$kb2->id => ['priority' => 2],
]);
// 验证关联关系
$this->assertCount(2, $terminal->knowledgeBases);
$this->assertTrue($terminal->knowledgeBases->contains($kb1));
$this->assertTrue($terminal->knowledgeBases->contains($kb2));
}
/** @test */
public function edit_form_can_load_terminal_with_knowledge_bases()
{
// 创建终端和知识库
$terminal = Terminal::factory()->create();
$kb1 = KnowledgeBase::create(['name' => '知识库1', 'status' => 'active']);
$kb2 = KnowledgeBase::create(['name' => '知识库2', 'status' => 'active']);
// 关联知识库
$terminal->knowledgeBases()->attach([
$kb1->id => ['priority' => 5],
$kb2->id => ['priority' => 10],
]);
// 测试编辑表单可以加载
$component = Livewire::test(TerminalResource\Pages\EditTerminal::class, [
'record' => $terminal->getRouteKey(),
]);
// 验证表单可以渲染
$component->assertSuccessful();
// 验证基本信息加载正确
$component->assertFormSet([
'name' => $terminal->name,
'code' => $terminal->code,
]);
}
/** @test */
public function terminal_knowledge_base_associations_can_be_updated()
{
// 创建终端和知识库
$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' => 1]);
// 更新关联(移除旧的,添加新的)
$terminal->knowledgeBases()->sync([
$kb2->id => ['priority' => 5],
$kb3->id => ['priority' => 10],
]);
// 验证更新后的关联
$terminal->refresh();
$this->assertCount(2, $terminal->knowledgeBases);
$this->assertFalse($terminal->knowledgeBases->contains($kb1));
$this->assertTrue($terminal->knowledgeBases->contains($kb2));
$this->assertTrue($terminal->knowledgeBases->contains($kb3));
}
/** @test */
public function terminal_can_remove_all_knowledge_base_associations()
{
// 创建终端和知识库
$terminal = Terminal::factory()->create();
$kb1 = KnowledgeBase::create(['name' => '知识库1', 'status' => 'active']);
// 初始关联
$terminal->knowledgeBases()->attach($kb1->id, ['priority' => 1]);
$this->assertCount(1, $terminal->knowledgeBases);
// 移除所有关联
$terminal->knowledgeBases()->detach();
// 验证所有关联已移除
$terminal->refresh();
$this->assertCount(0, $terminal->knowledgeBases);
}
/** @test */
public function knowledge_bases_are_ordered_by_priority_in_form()
{
// 创建终端和知识库
$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],
$kb2->id => ['priority' => 5],
$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
}
}