watcher_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package plugin_manager
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/google/uuid"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/positive_manager"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/app"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
  11. )
  12. type fakePlugin struct {
  13. plugin_entities.PluginRuntime
  14. positive_manager.PositivePluginRuntime
  15. }
  16. func (r *fakePlugin) InitEnvironment() error {
  17. return nil
  18. }
  19. func (r *fakePlugin) Checksum() (string, error) {
  20. return "", nil
  21. }
  22. func (r *fakePlugin) Identity() (plugin_entities.PluginUniqueIdentifier, error) {
  23. return plugin_entities.PluginUniqueIdentifier(""), nil
  24. }
  25. func (r *fakePlugin) StartPlugin() error {
  26. return nil
  27. }
  28. func (r *fakePlugin) Type() plugin_entities.PluginRuntimeType {
  29. return plugin_entities.PLUGIN_RUNTIME_TYPE_LOCAL
  30. }
  31. func (r *fakePlugin) Wait() (<-chan bool, error) {
  32. return nil, nil
  33. }
  34. func (r *fakePlugin) Listen(string) *entities.Broadcast[plugin_entities.SessionMessage] {
  35. return nil
  36. }
  37. func (r *fakePlugin) Write(string, []byte) {
  38. }
  39. func (r *fakePlugin) WaitStarted() <-chan bool {
  40. c := make(chan bool)
  41. close(c)
  42. return c
  43. }
  44. func (r *fakePlugin) WaitStopped() <-chan bool {
  45. c := make(chan bool)
  46. return c
  47. }
  48. func getRandomPluginRuntime() *fakePlugin {
  49. return &fakePlugin{
  50. PluginRuntime: plugin_entities.PluginRuntime{
  51. Config: plugin_entities.PluginDeclaration{
  52. PluginDeclarationWithoutAdvancedFields: plugin_entities.PluginDeclarationWithoutAdvancedFields{
  53. Name: uuid.New().String(),
  54. Label: plugin_entities.I18nObject{
  55. EnUS: "label",
  56. },
  57. Version: "0.0.1",
  58. Type: plugin_entities.PluginType,
  59. Author: "Yeuoly",
  60. CreatedAt: time.Now(),
  61. Plugins: plugin_entities.PluginExtensions{
  62. Tools: []string{"test"},
  63. },
  64. },
  65. },
  66. },
  67. }
  68. }
  69. type fakeRemotePluginServer struct {
  70. }
  71. func (f *fakeRemotePluginServer) Launch() error {
  72. return nil
  73. }
  74. func (f *fakeRemotePluginServer) Next() bool {
  75. return false
  76. }
  77. func (f *fakeRemotePluginServer) Read() (plugin_entities.PluginFullDuplexLifetime, error) {
  78. return nil, nil
  79. }
  80. func (f *fakeRemotePluginServer) Stop() error {
  81. return nil
  82. }
  83. func (f *fakeRemotePluginServer) Wrap(fn func(plugin_entities.PluginFullDuplexLifetime)) {
  84. fn(getRandomPluginRuntime())
  85. }
  86. func TestRemotePluginWatcherPluginStoredToManager(t *testing.T) {
  87. config := &app.Config{}
  88. config.SetDefault()
  89. routine.InitPool(1024)
  90. pm := InitGlobalManager(&app.Config{})
  91. pm.remotePluginServer = &fakeRemotePluginServer{}
  92. pm.startRemoteWatcher(config)
  93. time.Sleep(1 * time.Second)
  94. if pm.m.Len() != 1 {
  95. t.Fatalf("Expected 1 plugin, got %d", pm.m.Len())
  96. }
  97. }