|
| 1 | +package syncer |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + |
| 10 | + "github.com/chaitin/MonkeyCode/backend/biz/mcphub/repo" |
| 11 | +) |
| 12 | + |
| 13 | +func TestSyncMarksUpstreamHealthy(t *testing.T) { |
| 14 | + upstreamID := uuid.New() |
| 15 | + upstreams := &upstreamRepoStub{ |
| 16 | + upstream: &repo.UpstreamConfig{ID: upstreamID, Slug: "images"}, |
| 17 | + } |
| 18 | + service := NewService(upstreams, &toolRepoStub{}, ®istryStub{}, &upstreamClientStub{ |
| 19 | + tools: []repo.UpstreamTool{{Name: "search"}}, |
| 20 | + }) |
| 21 | + |
| 22 | + if err := service.Sync(context.Background(), upstreamID); err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + if upstreams.healthy == nil || !*upstreams.healthy { |
| 26 | + t.Fatalf("health status = %v, want healthy", upstreams.healthy) |
| 27 | + } |
| 28 | + if !upstreams.syncSuccess { |
| 29 | + t.Fatal("sync success was not recorded") |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestSyncMarksUpstreamUnhealthyWhenListToolsFails(t *testing.T) { |
| 34 | + upstreamID := uuid.New() |
| 35 | + upstreams := &upstreamRepoStub{ |
| 36 | + upstream: &repo.UpstreamConfig{ID: upstreamID, Slug: "images"}, |
| 37 | + } |
| 38 | + service := NewService(upstreams, &toolRepoStub{}, ®istryStub{}, &upstreamClientStub{ |
| 39 | + err: errors.New("upstream unavailable"), |
| 40 | + }) |
| 41 | + |
| 42 | + if err := service.Sync(context.Background(), upstreamID); err == nil { |
| 43 | + t.Fatal("Sync() error = nil, want upstream error") |
| 44 | + } |
| 45 | + if upstreams.healthy == nil || *upstreams.healthy { |
| 46 | + t.Fatalf("health status = %v, want unhealthy", upstreams.healthy) |
| 47 | + } |
| 48 | + if !upstreams.syncFailed { |
| 49 | + t.Fatal("sync failure was not recorded") |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +type upstreamRepoStub struct { |
| 54 | + upstream *repo.UpstreamConfig |
| 55 | + healthy *bool |
| 56 | + syncSuccess bool |
| 57 | + syncFailed bool |
| 58 | +} |
| 59 | + |
| 60 | +func (s *upstreamRepoStub) Get(context.Context, uuid.UUID) (*repo.UpstreamConfig, error) { |
| 61 | + return s.upstream, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (s *upstreamRepoStub) MarkSyncSuccess(context.Context, uuid.UUID) error { |
| 65 | + s.syncSuccess = true |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +func (s *upstreamRepoStub) MarkSyncFailed(context.Context, uuid.UUID) error { |
| 70 | + s.syncFailed = true |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func (s *upstreamRepoStub) MarkHealthStatus(_ context.Context, _ uuid.UUID, healthy bool) error { |
| 75 | + s.healthy = &healthy |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +type toolRepoStub struct{} |
| 80 | + |
| 81 | +func (s *toolRepoStub) ReplaceByUpstream(context.Context, uuid.UUID, []repo.UpsertToolInput) error { |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +type registryStub struct{} |
| 86 | + |
| 87 | +func (s *registryStub) Publish(context.Context) error { |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +type upstreamClientStub struct { |
| 92 | + tools []repo.UpstreamTool |
| 93 | + err error |
| 94 | +} |
| 95 | + |
| 96 | +func (s *upstreamClientStub) ListTools(context.Context, *repo.UpstreamConfig) ([]repo.UpstreamTool, error) { |
| 97 | + return s.tools, s.err |
| 98 | +} |
0 commit comments