watcher_test.go 3.0 KB

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