BxDolAiAgentToolsTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Locked tools are attached in code. The picker and a saved CSV cannot grant them.
  4. */
  5. class BxDolAiAgentToolsTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /** @var callable|null */
  8. protected $handler;
  9. protected function setUp(): void
  10. {
  11. $this->handler = set_exception_handler(static function () {
  12. });
  13. restore_exception_handler();
  14. }
  15. protected function tearDown(): void
  16. {
  17. for ($i = 0; $i < 5; $i++) {
  18. $current = set_exception_handler(static function () {
  19. });
  20. restore_exception_handler();
  21. if ($current === $this->handler)
  22. return;
  23. restore_exception_handler();
  24. }
  25. }
  26. public function testTypesForNormalAgentAreChatOnly()
  27. {
  28. $this->assertSame(
  29. ['chat_buttons', 'chat_title'],
  30. BxDolAiAgentTools::typesFor(['name' => 'site_helper'])
  31. );
  32. }
  33. public function testTypesForOperatorIncludesReadToolsThatStayVisible()
  34. {
  35. $this->assertSame([
  36. 'chat_buttons',
  37. 'chat_title',
  38. 'agent_create',
  39. 'mockup_upsert',
  40. 'mysql_write_safe',
  41. 'mysql_undo',
  42. 'mysql_schema',
  43. 'mysql_select',
  44. ], BxDolAiAgentTools::typesFor(['name' => 'una_operator']));
  45. }
  46. public function testPickerOmitsLockedTypes()
  47. {
  48. $oDb = BxDolDb::getInstance();
  49. $aLocked = $oDb->getAll("SELECT `id`, `type` FROM `sys_agents_tools` WHERE `type` IN (" . $oDb->implode_escape(BxDolAiAgentTools::hiddenTypes()) . ")");
  50. $this->assertNotEmpty($aLocked);
  51. $aHidden = [];
  52. foreach ($aLocked as $aRow)
  53. $aHidden[(int)$aRow['id']] = $aRow['type'];
  54. $aTools = (new BxDolAiQuery())->getTools();
  55. foreach (array_keys($aTools) as $iId)
  56. $this->assertArrayNotHasKey((int)$iId, $aHidden);
  57. $iContent = (int)$oDb->getOne("SELECT `id` FROM `sys_agents_tools` WHERE `type` = 'content_get' AND `active` = 1 ORDER BY `id` LIMIT 1");
  58. $this->assertArrayHasKey($iContent, $aTools);
  59. foreach (['mysql_schema', 'mysql_select'] as $sType) {
  60. $iId = $this->toolId($sType);
  61. $this->assertGreaterThan(0, $iId);
  62. $this->assertArrayHasKey($iId, $aTools);
  63. }
  64. }
  65. public function testToolsGridSourceOmitsHiddenTypes()
  66. {
  67. $sSource = BxDolAiAgentTools::excludeHiddenFromSource('SELECT * FROM `sys_agents_tools` WHERE 1 ');
  68. $this->assertStringStartsWith('SELECT * FROM `sys_agents_tools` WHERE 1 ', $sSource);
  69. foreach (BxDolAiAgentTools::hiddenTypes() as $sType)
  70. $this->assertStringContainsString("'" . $sType . "'", $sSource);
  71. $this->assertStringNotContainsString("'mysql_schema'", $sSource);
  72. $this->assertStringNotContainsString("'mysql_select'", $sSource);
  73. $this->assertStringNotContainsString("'mysql_write'", $sSource);
  74. }
  75. public function testStripCsvDropsLockedIdAndKeepsContentGet()
  76. {
  77. $oDb = BxDolDb::getInstance();
  78. $iChat = (int)$oDb->getOne("SELECT `id` FROM `sys_agents_tools` WHERE `type` = 'chat_buttons' ORDER BY `id` LIMIT 1");
  79. $iContent = (int)$oDb->getOne("SELECT `id` FROM `sys_agents_tools` WHERE `type` = 'content_get' ORDER BY `id` LIMIT 1");
  80. $this->assertGreaterThan(0, $iChat);
  81. $this->assertGreaterThan(0, $iContent);
  82. $this->assertSame((string)$iContent, BxDolAiAgentTools::stripCsv($iChat . ',' . $iContent));
  83. }
  84. public function testResolveIdsIgnoresStoredLockedTools()
  85. {
  86. $oDb = BxDolDb::getInstance();
  87. $iMysql = (int)$oDb->getOne("SELECT `id` FROM `sys_agents_tools` WHERE `type` = 'mysql_select' AND `active` = 1 ORDER BY `id` LIMIT 1");
  88. $iContent = (int)$oDb->getOne("SELECT `id` FROM `sys_agents_tools` WHERE `type` = 'content_get' AND `active` = 1 ORDER BY `id` LIMIT 1");
  89. $iWrite = (int)$oDb->getOne("SELECT `id` FROM `sys_agents_tools` WHERE `type` = 'mysql_write' ORDER BY `id` LIMIT 1");
  90. $this->assertGreaterThan(0, $iMysql);
  91. $this->assertGreaterThan(0, $iContent);
  92. $iWriteSafe = $this->toolId('mysql_write_safe');
  93. $iUndo = $this->toolId('mysql_undo');
  94. $aNormal = BxDolAiAgentTools::resolveIds([
  95. 'name' => 'site_helper',
  96. 'tools' => $iMysql . ',' . $iContent . ',' . $iWriteSafe . ',' . $iUndo,
  97. ]);
  98. $this->assertContains($iContent, $aNormal);
  99. $this->assertContains($iMysql, $aNormal);
  100. $this->assertNotContains($iWriteSafe, $aNormal);
  101. $this->assertNotContains($iUndo, $aNormal);
  102. $this->assertContains($this->toolId('chat_buttons'), $aNormal);
  103. $this->assertContains($this->toolId('chat_title'), $aNormal);
  104. $this->assertNotContains($this->toolId('mysql_schema'), $aNormal);
  105. $aOperator = BxDolAiAgentTools::resolveIds(['name' => 'una_operator', 'tools' => '']);
  106. foreach (['chat_buttons', 'chat_title', 'agent_create', 'mockup_upsert', 'mysql_write_safe', 'mysql_undo', 'mysql_schema', 'mysql_select'] as $sType)
  107. $this->assertContains($this->toolId($sType), $aOperator);
  108. $this->assertContains($iMysql, $aOperator);
  109. if ($iWrite > 0)
  110. $this->assertNotContains($iWrite, $aOperator);
  111. }
  112. protected function toolId(string $sType): int
  113. {
  114. return (int)BxDolDb::getInstance()->getOne(
  115. "SELECT `id` FROM `sys_agents_tools` WHERE `type` = :type AND `active` = 1 ORDER BY `id` LIMIT 1",
  116. ['type' => $sType]
  117. );
  118. }
  119. }