package store import ( "context" "errors" "testing" "github.com/GLINCKER/levelrail/internal/spec" ) func TestSaveAndGetGitSource(t *testing.T) { db := openTestDB(t) ctx := context.Background() want := GitSource{ ServiceName: "web", RepoURL: "https://github.com/org/web.git", Branch: "main", BuildType: "dockerfile", BuildPath: "./Dockerfile", } if err := db.SaveGitSource(ctx, want); err != nil { t.Fatalf("SaveGitSource() error = %v", err) } got, err := db.GetGitSource(ctx, "web") if err != nil { t.Fatalf("GetGitSource() error = %v", err) } if got.ServiceName != want.ServiceName || got.RepoURL != want.RepoURL || got.Branch != want.Branch || got.BuildType != want.BuildType || got.BuildPath != want.BuildPath { t.Errorf("GetGitSource() = %+v, want fields to match %+v", got, want) } if got.CreatedAt.IsZero() || got.UpdatedAt.IsZero() { t.Errorf("GetGitSource() timestamps not populated: %+v", got) } } // TestSaveAndGetGitSource_Databases covers Databases' own JSON-column // round trip (migrations/0091_git_source_databases_spec.sql), the same // shape TestSaveAndGetGitSource already proves for Services. func TestSaveAndGetGitSource_Databases(t *testing.T) { db := openTestDB(t) ctx := context.Background() want := GitSource{ ServiceName: "web", RepoURL: "https://github.com/org/web.git", Branch: "main", BuildType: "dockerfile", Databases: map[string]spec.Database{ "main": {Engine: spec.EnginePostgres, Version: "16", EphemeralInPreviews: true}, }, } if err := db.SaveGitSource(ctx, want); err != nil { t.Fatalf("SaveGitSource() error = %v", err) } got, err := db.GetGitSource(ctx, "web") if err != nil { t.Fatalf("GetGitSource() error = %v", err) } main, ok := got.Databases["main"] if !ok { t.Fatal("expected a \"main\" database") } if main.Engine != spec.EnginePostgres || main.Version != "16" || !main.EphemeralInPreviews { t.Errorf("Databases[main] = %+v, want Engine=postgres Version=16 EphemeralInPreviews=true", main) } } func TestListGitSources(t *testing.T) { db := openTestDB(t) ctx := context.Background() got, err := db.ListGitSources(ctx) if err != nil { t.Fatalf("ListGitSources() on empty table error = %v", err) } if len(got) != 0 { t.Fatalf("ListGitSources() on empty table = %+v, want empty", got) } sources := []GitSource{ {ServiceName: "web", RepoURL: "https://github.com/org/web.git", Branch: "main", BuildType: "dockerfile"}, {ServiceName: "worker", RepoURL: "https://gitlab.com/org/worker.git", Branch: ""}, } for _, s := range sources { if err := db.SaveGitSource(ctx, s); err != nil { t.Fatalf("SaveGitSource(%q) error = %v", s.ServiceName, err) } } got, err = db.ListGitSources(ctx) if err != nil { t.Fatalf("ListGitSources() error = %v", err) } if len(got) != len(sources) { t.Fatalf("ListGitSources() = %+v, want %d rows", got, len(sources)) } byName := map[string]GitSourceSummary{} for _, s := range got { byName[s.ServiceName] = s } if byName["web"].RepoURL != "https://github.com/org/web.git" || byName["web"].Branch != "main" { t.Errorf("ListGitSources()[web] = %+v, want matching repo_url/branch", byName["web"]) } if byName["worker"].RepoURL != "https://gitlab.com/org/worker.git" || byName["worker"].Branch != "" { t.Errorf("ListGitSources()[worker] = %+v, want empty branch", byName["worker"]) } } // TestGetGitSource_Databases_EmptyDefaultsToNil covers a git source // saved with no Databases at all (every one saved before this field // existed): reading it back must not error, and must not confuse an // absent map with one holding some default entry. func TestGetGitSource_Databases_EmptyDefaultsToNil(t *testing.T) { db := openTestDB(t) ctx := context.Background() if err := db.SaveGitSource(ctx, GitSource{ServiceName: "web", RepoURL: "https://github.com/org/web.git", Branch: "main", BuildType: "dockerfile"}); err != nil { t.Fatalf("SaveGitSource() error = %v", err) } got, err := db.GetGitSource(ctx, "web") if err != nil { t.Fatalf("GetGitSource() error = %v", err) } if len(got.Databases) != 0 { t.Errorf("Databases = %+v, want empty", got.Databases) } } func TestGetGitSource_NotFound(t *testing.T) { db := openTestDB(t) ctx := context.Background() _, err := db.GetGitSource(ctx, "ghost") if !errors.Is(err, ErrGitSourceNotFound) { t.Fatalf("GetGitSource() error = %v, want ErrGitSourceNotFound", err) } } func TestSaveGitSource_UpsertsAndPreservesCreatedAt(t *testing.T) { db := openTestDB(t) ctx := context.Background() first := GitSource{ServiceName: "web", RepoURL: "https://github.com/org/web.git", Branch: "main", BuildType: "dockerfile"} if err := db.SaveGitSource(ctx, first); err != nil { t.Fatalf("SaveGitSource(first) error = %v", err) } created, err := db.GetGitSource(ctx, "web") if err != nil { t.Fatalf("GetGitSource() error = %v", err) } second := GitSource{ServiceName: "web", RepoURL: "https://github.com/org/web.git", Branch: "release", BuildType: "railpack"} if err := db.SaveGitSource(ctx, second); err != nil { t.Fatalf("SaveGitSource(second) error = %v", err) } got, err := db.GetGitSource(ctx, "web") if err != nil { t.Fatalf("GetGitSource() error = %v", err) } if got.Branch != "release" || got.BuildType != "railpack" { t.Errorf("GetGitSource() after upsert = %+v, want branch=release build_type=railpack", got) } if !got.CreatedAt.Equal(created.CreatedAt) { t.Errorf("CreatedAt = %v, want unchanged across upsert (%v)", got.CreatedAt, created.CreatedAt) } } func TestDeleteGitSource(t *testing.T) { db := openTestDB(t) ctx := context.Background() want := GitSource{ServiceName: "web", RepoURL: "https://github.com/org/web.git", Branch: "main", BuildType: "dockerfile"} if err := db.SaveGitSource(ctx, want); err != nil { t.Fatalf("SaveGitSource() error = %v", err) } if err := db.DeleteGitSource(ctx, "web"); err != nil { t.Fatalf("DeleteGitSource() error = %v", err) } _, err := db.GetGitSource(ctx, "web") if !errors.Is(err, ErrGitSourceNotFound) { t.Fatalf("GetGitSource() after delete error = %v, want ErrGitSourceNotFound", err) } } func TestDeleteGitSource_NotFound(t *testing.T) { db := openTestDB(t) ctx := context.Background() err := db.DeleteGitSource(ctx, "ghost") if !errors.Is(err, ErrGitSourceNotFound) { t.Fatalf("DeleteGitSource() error = %v, want ErrGitSourceNotFound", err) } } func TestGitSourceSecretsKey_DistinctFromServiceName(t *testing.T) { got := GitSourceSecretsKey("web") if got == "web" { t.Errorf("GitSourceSecretsKey(%q) = %q, must not equal the service name itself: a git source's credentials must not share a DEK with the app's own runtime env secrets", "web", got) } } func TestSaveAndGetGitSource_AdditionalServices(t *testing.T) { db := openTestDB(t) ctx := context.Background() want := GitSource{ ServiceName: "web", RepoURL: "https://github.com/org/web.git", Branch: "main", BuildType: "dockerfile", AdditionalServices: map[string]GitSourceBuild{ "worker": {BuildType: "dockerfile", BuildPath: "./worker/Dockerfile"}, }, } if err := db.SaveGitSource(ctx, want); err != nil { t.Fatalf("SaveGitSource() error = %v", err) } got, err := db.GetGitSource(ctx, "web") if err != nil { t.Fatalf("GetGitSource() error = %v", err) } if len(got.AdditionalServices) != 1 || got.AdditionalServices["worker"] != want.AdditionalServices["worker"] { t.Errorf("GetGitSource().AdditionalServices = %+v, want %+v", got.AdditionalServices, want.AdditionalServices) } } func TestGetGitSource_NoAdditionalServices_ReturnsEmptyNotNilError(t *testing.T) { db := openTestDB(t) ctx := context.Background() if err := db.SaveGitSource(ctx, GitSource{ServiceName: "web", RepoURL: "https://github.com/org/web.git", Branch: "main", BuildType: "dockerfile"}); err != nil { t.Fatalf("SaveGitSource() error = %v", err) } got, err := db.GetGitSource(ctx, "web") if err != nil { t.Fatalf("GetGitSource() error = %v", err) } if len(got.AdditionalServices) != 0 { t.Errorf("GetGitSource().AdditionalServices = %+v, want empty", got.AdditionalServices) } } func TestSaveGitSourceWithAppAuthWritesBothTogether(t *testing.T) { db := openTestDB(t) ctx := context.Background() src := GitSource{ServiceName: "web", RepoURL: "https://example.test/a.git", Branch: "main", BuildType: "dockerfile", BuildPath: "Dockerfile"} if err := db.SaveGitSourceWithAppAuth(ctx, src, false); err != nil { t.Fatalf("save new: %v", err) } got, err := db.GetGitSource(ctx, "web") if err != nil || got.AppAuth { t.Fatalf("new source app_auth = %v, err = %v, want false", got != nil && got.AppAuth, err) } // Plain SaveGitSource keeps the stored flag on update. src.Branch = "dev" if err := db.SaveGitSource(ctx, src); err != nil { t.Fatalf("plain save: %v", err) } if got, _ := db.GetGitSource(ctx, "web"); got == nil || got.AppAuth || got.Branch != "dev" { t.Fatalf("after plain save = %+v", got) } if err := db.SaveGitSourceWithAppAuth(ctx, src, true); err != nil { t.Fatalf("save update: %v", err) } if got, _ := db.GetGitSource(ctx, "web"); got == nil || !got.AppAuth { t.Fatalf("after update app_auth not true: %+v", got) } other := GitSource{ServiceName: "api", RepoURL: "https://example.test/b.git", Branch: "main", BuildType: "dockerfile", BuildPath: "Dockerfile"} if err := db.SaveGitSource(ctx, other); err != nil { t.Fatal(err) } if got, _ := db.GetGitSource(ctx, "api"); got == nil || !got.AppAuth { t.Fatalf("plain new source app_auth default = %+v, want true", got) } }