watcher_test.go 2.9 KB

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