endpoint_declaration_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package plugin_entities
  2. import (
  3. "testing"
  4. "github.com/langgenius/dify-plugin-daemon/internal/utils/parser"
  5. )
  6. func TestUnmarshalEndpointDeclarationFromYaml(t *testing.T) {
  7. const data = `settings:
  8. - name: api_key
  9. type: secret-input
  10. required: true
  11. label:
  12. en_US: API key
  13. zh_Hans: API key
  14. pt_BR: API key
  15. placeholder:
  16. en_US: Please input your API key
  17. zh_Hans: 请输入你的 API key
  18. pt_BR: Please input your API key
  19. endpoints:
  20. - endpoints/duck.yaml
  21. - endpoints/neko.yaml
  22. `
  23. dec, err := parser.UnmarshalYamlBytes[EndpointProviderDeclaration]([]byte(data))
  24. if err != nil {
  25. t.Fatalf("Failed to unmarshal EndpointProviderDeclaration: %v", err)
  26. }
  27. if len(dec.EndpointFiles) != 2 {
  28. t.Fatalf("Failed to unmarshal EndpointProviderDeclaration: %v", err)
  29. }
  30. }
  31. func TestUnmarshalEndpointDeclarationFromYaml2(t *testing.T) {
  32. const data = `settings:
  33. - name: api_key
  34. type: secret-input
  35. required: true
  36. label:
  37. en_US: API key
  38. zh_Hans: API key
  39. pt_BR: API key
  40. placeholder:
  41. en_US: Please input your API key
  42. zh_Hans: 请输入你的 API key
  43. pt_BR: Please input your API key
  44. endpoints:
  45. - path: "/duck/<app_id>"
  46. method: "GET"`
  47. dec, err := parser.UnmarshalYamlBytes[EndpointProviderDeclaration]([]byte(data))
  48. if err != nil {
  49. t.Fatalf("Failed to unmarshal EndpointProviderDeclaration: %v", err)
  50. }
  51. if len(dec.Endpoints) != 1 {
  52. t.Fatalf("Failed to unmarshal EndpointProviderDeclaration: %v", err)
  53. }
  54. }
  55. func TestUnmarshalEndpointDeclarationFromJSON(t *testing.T) {
  56. const data = `{
  57. "settings": [
  58. {
  59. "name": "api_key",
  60. "type": "secret-input",
  61. "required": true,
  62. "label": {
  63. "en_US": "API key",
  64. "zh_Hans": "API key",
  65. "pt_BR": "API key"
  66. },
  67. "placeholder": {
  68. "en_US": "Please input your API key",
  69. "zh_Hans": "请输入你的 API key",
  70. "pt_BR": "Please input your API key"
  71. }
  72. }
  73. ],
  74. "endpoints": [
  75. "endpoints/duck.yaml",
  76. "endpoints/neko.yaml"
  77. ]
  78. }`
  79. dec, err := parser.UnmarshalJsonBytes[EndpointProviderDeclaration]([]byte(data))
  80. if err != nil {
  81. t.Fatalf("Failed to unmarshal EndpointProviderDeclaration from JSON: %v", err)
  82. }
  83. if len(dec.EndpointFiles) != 2 {
  84. t.Fatalf("Failed to unmarshal EndpointProviderDeclaration from JSON: expected 1 endpoint, got %d", len(dec.Endpoints))
  85. }
  86. if len(dec.Settings) != 1 {
  87. t.Fatalf("Failed to unmarshal EndpointProviderDeclaration from JSON: expected 1 setting, got %d", len(dec.Settings))
  88. }
  89. }
  90. func TestUnmarshalEndpointDeclarationFromJSON2(t *testing.T) {
  91. const data = `{
  92. "settings": [
  93. {
  94. "name": "api_key",
  95. "type": "secret-input",
  96. "required": true,
  97. "label": {
  98. "en_US": "API key",
  99. "zh_Hans": "API key",
  100. "pt_BR": "API key"
  101. },
  102. "placeholder": {
  103. "en_US": "Please input your API key",
  104. "zh_Hans": "请输入你的 API key",
  105. "pt_BR": "Please input your API key"
  106. }
  107. }
  108. ],
  109. "endpoints": [
  110. {
  111. "path": "/duck/<app_id>",
  112. "method": "GET"
  113. }
  114. ]
  115. }`
  116. dec, err := parser.UnmarshalJsonBytes[EndpointProviderDeclaration]([]byte(data))
  117. if err != nil {
  118. t.Fatalf("Failed to unmarshal EndpointProviderDeclaration from JSON: %v", err)
  119. }
  120. if len(dec.Endpoints) != 1 {
  121. t.Fatalf("Failed to unmarshal EndpointProviderDeclaration from JSON: expected 1 endpoint, got %d", len(dec.Endpoints))
  122. }
  123. if len(dec.Settings) != 1 {
  124. t.Fatalf("Failed to unmarshal EndpointProviderDeclaration from JSON: expected 1 setting, got %d", len(dec.Settings))
  125. }
  126. }