watcher_test.go 2.9 KB

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