package production import ( "context" "testing" "github.com/google/uuid" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/cursor" "gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/model/dto" ) type cacheFixture struct { profile dto.UserDTO hit bool writes int } // GetProfile returns the arranged optional-cache lookup outcome. func (f *cacheFixture) GetProfile(context.Context, uuid.UUID) (dto.UserDTO, bool) { return f.profile, f.hit } // PutProfile records successful cache population from authoritative data. func (f *cacheFixture) PutProfile(_ context.Context, user dto.UserDTO) { f.profile = user f.writes++ } // TestProfileCacheHitAvoidsProfileQuery verifies that the cache has a concrete role. func TestProfileCacheHitAvoidsProfileQuery(t *testing.T) { codec, err := cursor.New() if err != nil { t.Fatal(err) } cache := &cacheFixture{profile: dto.UserDTO{ID: uuid.New()}, hit: true} svc, err := NewCached(&storeStub{t: t}, codec, cache) if err != nil { t.Fatal(err) } got, err := svc.UsersGetMe(t.Context(), cache.profile.ID.String()) if err != nil || got.ID != cache.profile.ID { t.Fatalf("cache hit ignored: %v", err) } } // TestProfileCacheMissPopulatesAfterDatabaseRead checks the authoritative fallback. func TestProfileCacheMissPopulatesAfterDatabaseRead(t *testing.T) { codec, err := cursor.New() if err != nil { t.Fatal(err) } id := uuid.New() repo := &storeStub{t: t, user: func(context.Context, uuid.UUID) (dto.UserDTO, error) { return dto.UserDTO{ID: id}, nil }} cache := &cacheFixture{} svc, err := NewCached(repo, codec, cache) if err != nil { t.Fatal(err) } got, err := svc.UsersGetMe(t.Context(), id.String()) if err != nil || got.ID != id || cache.writes != 1 { t.Fatalf("fallback failed: %v", err) } } // TestCachedServiceRejectsMissingDependencies checks explicit service boundaries. func TestCachedServiceRejectsMissingDependencies(t *testing.T) { t.Run("store", func(t *testing.T) { if _, err := NewCached(nil, nil, &cacheFixture{}); err == nil { t.Fatal("missing store accepted") } }) t.Run("cache", func(t *testing.T) { codec, err := cursor.New() if err != nil { t.Fatal(err) } if _, err := NewCached(&storeStub{t: t}, codec, nil); err == nil { t.Fatal("missing cache accepted") } }) }