package test import ( "go/parser" "go/token" "strings" "testing" ) func TestImageVariantCacheLayering(t *testing.T) { t.Run("The cache use cases and the tracker import no persistence, mailer, models, gorm, net/http, chi or api package", func(t *testing.T) { files := []string{ "internal/infrastructure/useCases/GetVariantCacheStats.go", "internal/infrastructure/useCases/PurgeVariantCache.go", "internal/infrastructure/usage/variantUsageTracker.go", } for _, file := range files { for _, path := range importsOf(t, file) { for _, fragment := range []string{"persistence", "mailer", "models", "gorm", "net/http", "go-chi", "RenewCMS/api"} { if strings.Contains(path, fragment) { t.Errorf("%s imports %s", file, path) } } } } }) t.Run("The cache handler imports no persistence, mailer, models or gorm package", func(t *testing.T) { for _, path := range importsOf(t, "api/handlers/imagevariant/cacheHandler.go") { for _, fragment := range []string{"persistence", "mailer", "models", "gorm"} { if strings.Contains(path, fragment) { t.Errorf("imports %s", path) } } } }) t.Run("The tracker, usage and cache ports are declared in internal/domain/image", func(t *testing.T) { declared := readRepositoryFile(t, "internal/domain/image/variantRepositoryInterface.go") for _, port := range []string{"type VariantUsageTracker interface", "type VariantUsageRepository interface", "type VariantCacheRepository interface"} { if !strings.Contains(declared, port) { t.Errorf("%s is not declared", port) } } }) t.Run("The new models are registered in migrate.go", func(t *testing.T) { migrate := readRepositoryFile(t, "internal/infrastructure/persistence/database/migrate.go") if !strings.Contains(migrate, "&models.ImageVariantRejectionDay{}") { t.Error("the rejection day model is missing from AutoMigrate") } }) t.Run("Only the composition root constructs the tracker", func(t *testing.T) { for _, file := range []string{"api/router.go", "cmd/server/main.go", "api/handlers/staticimages/staticImagesHandler.go", "api/handlers/imagevariant/cacheHandler.go"} { if strings.Contains(readRepositoryFile(t, file), "NewTracker") { t.Errorf("%s constructs the tracker", file) } } }) t.Run("The new files carry no comment", func(t *testing.T) { for _, relative := range []string{ "internal/domain/image/variantCache.go", "internal/infrastructure/usage/variantUsageTracker.go", "internal/infrastructure/useCases/GetVariantCacheStats.go", "internal/infrastructure/useCases/PurgeVariantCache.go", "internal/infrastructure/persistence/repositories/imageVariantUsageRepository.go", "api/handlers/imagevariant/cacheHandler.go", } { parsed, err := parser.ParseFile(token.NewFileSet(), "../"+relative, nil, parser.ParseComments) if err != nil { t.Fatal(err) } for _, group := range parsed.Comments { for _, comment := range group.List { if !strings.HasPrefix(comment.Text, "//go:") { t.Errorf("%s has comment %q", relative, comment.Text) } } } } }) }