tool_service_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package plugin_daemon
  2. import (
  3. "testing"
  4. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/tool_entities"
  5. "github.com/langgenius/dify-plugin-daemon/internal/utils/stream"
  6. )
  7. func TestToolInvokeJSONSchemaValidator(t *testing.T) {
  8. response := stream.NewStream[tool_entities.ToolResponseChunk](128)
  9. bindValidator(response, map[string]any{
  10. "output_schema": map[string]any{
  11. "type": "object",
  12. "properties": map[string]any{
  13. "name": map[string]any{
  14. "type": "string",
  15. },
  16. },
  17. },
  18. })
  19. response.Write(tool_entities.ToolResponseChunk{
  20. Type: tool_entities.ToolResponseChunkTypeVariable,
  21. Message: map[string]any{
  22. "variable_name": "name",
  23. "variable_value": "1",
  24. "stream": true,
  25. },
  26. })
  27. response.Close()
  28. for response.Next() {
  29. data, err := response.Read()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. t.Log(data)
  34. }
  35. }
  36. func TestToolInvokeJSONSchemaValidatorWithInvalidSchema(t *testing.T) {
  37. response := stream.NewStream[tool_entities.ToolResponseChunk](128)
  38. bindValidator(response, map[string]any{
  39. "output_schema": map[string]any{
  40. "type": "object",
  41. "properties": map[string]any{
  42. "name": map[string]any{
  43. "type": "string",
  44. },
  45. },
  46. },
  47. })
  48. response.Write(tool_entities.ToolResponseChunk{
  49. Type: tool_entities.ToolResponseChunkTypeVariable,
  50. Message: map[string]any{
  51. "variable_name": "name",
  52. "variable_value": 1,
  53. "stream": false,
  54. },
  55. })
  56. response.Close()
  57. _, err := response.Read()
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. _, err = response.Read()
  62. if err == nil {
  63. t.Fatal("expected error, got nil")
  64. }
  65. }