Skip to content

Commit 873bc83

Browse files
author
Idan Attias
committed
fix(test): mock project metadata to prevent CI failure due to missing CRM credentials
1 parent 79504cf commit 873bc83

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

internal/testutil/testutil.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"archive/tar"
66
"archive/zip"
77
"bytes"
8+
"context"
89
"fmt"
910
"os"
1011
"path/filepath"
@@ -40,6 +41,28 @@ func SetupGCSMock(t *testing.T, initialObjects []fakestorage.Object, port uint16
4041
return server, gcsClient
4142
}
4243

44+
// MockProjectGCSClient wraps a real GCS client but mocks the project metadata calls.
45+
type MockProjectGCSClient struct {
46+
tui.GCSClient
47+
Projects map[string]*gcs.ProjectMetadata
48+
ProjectError error
49+
}
50+
51+
// GetProjectMetadata returns mocked project metadata or a default if not found.
52+
func (m *MockProjectGCSClient) GetProjectMetadata(_ context.Context, projectID string) (*gcs.ProjectMetadata, error) {
53+
if m.ProjectError != nil {
54+
return nil, m.ProjectError
55+
}
56+
if p, ok := m.Projects[projectID]; ok {
57+
return p, nil
58+
}
59+
// Return a default mock to avoid CRM service initialization errors in tests.
60+
return &gcs.ProjectMetadata{
61+
ProjectID: projectID,
62+
Name: "Mock Project " + projectID,
63+
}, nil
64+
}
65+
4366
// CreateConfigFile creates a temporary config file for tests.
4467
func CreateConfigFile(t *testing.T, projects []string, downloadDir string) string {
4568
t.Helper()
@@ -77,7 +100,8 @@ func SetupTestApp(t *testing.T, initialObjects []fakestorage.Object, port uint16
77100
assert.NilError(t, err)
78101

79102
gcsClient := gcs.NewClient(server.Client())
80-
m := tui.NewModel(cfg.Projects, gcsClient, cfg.DownloadDir, cfg.FuzzySearch, cfg.NerdIcons)
103+
mockClient := &MockProjectGCSClient{GCSClient: gcsClient}
104+
m := tui.NewModel(cfg.Projects, mockClient, cfg.DownloadDir, cfg.FuzzySearch, cfg.NerdIcons)
81105
m.SetDeterministicSpinner(true)
82106

83107
tm := teatest.NewTestModel(t, &m)

tests/testdata/TestSnapshot_HelpMenu.golden

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
 gs:// 
1+
_Ga=d,d=A\ gs:// 
22

33
╭───────────────────────╮╭────────────────────────────╮╭───────────────────────────────────────────╮
44
│ Buckets ││  ││ Project Information │
55
│ ││ ││  │
66
│ ▼ prod-project  ┃ ││ ││ Project: prod-project │
77
│ ││ ││  │
8-
│ ││ ││  │
9-
╰───────────────────────╯╰────────────────────────────╯│ * Loading project info... │
8+
│ ││ ││ Project Name: Mock Project prod-project │
9+
╰───────────────────────╯╰────────────────────────────╯│  │
10+
│ Total Buckets: 1 │
1011
│  │
1112
╰───────────────────────────────────────────╯
1213
╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮

tests/testdata/TestSnapshot_MessagesView.golden

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
 gs:// 
1+
_Ga=d,d=A\ gs:// 
22

33
╭───────────────────────╮╭────────────────────────────╮╭───────────────────────────────────────────╮
44
│ Buckets ││  ││ Project Information │
55
│ ││ ││  │
66
│ ▼ prod-project  ┃ ││ ││ Project: prod-project │
77
│ ││ ││  │
8-
│ ││ ││  │
9-
╰───────────────────────╯╰────────────────────────────╯│ * Loading project info... │
8+
│ ││ ││ Project Name: Mock Project prod-project │
9+
╰───────────────────────╯╰────────────────────────────╯│  │
10+
│ Total Buckets: 1 │
1011
│  │
1112
╰───────────────────────────────────────────╯
1213
╭──────────────────────────────────────────────────────────────────────────────────────────╮

tests/testdata/TestSnapshot_SearchView.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
 gs:// 
1+
_Ga=d,d=A\ gs:// 
22

33
╭───────────────────────╮╭────────────────────────────╮╭───────────────────────────────────────────╮
44
│ Buckets ││  ││ Project Information │
55
│ ││ ││  │
66
│ ▼ prod-project  ││ ││ Project: prod-project │
77
│  📦 assets ││ ││  │
8+
│ ││ ││ Project Name: Mock Project prod-project │
89
│ ││ ││  │
9-
│ ││ ││ * Loading project info... │
10+
│ ││ ││ Total Buckets: 1 │
1011
│ ││ ││  │
1112
│ ││ ││ │
1213
│ ││ ││ │
@@ -24,6 +25,5 @@
2425
│ ││ ││ │
2526
│ ││ ││ │
2627
│ ││ ││ │
27-
│ ││ ││ │
2828
╰───────────────────────╯╰────────────────────────────╯╰───────────────────────────────────────────╯
2929
  SEARCH: ass_  / filter • space select • d download • y copy uri • i metadata • v versions • ? toggle help • q quit

tests/tui_test.go

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"archive/zip"
5-
"context"
65
"fmt"
76
"os"
87
"os/exec"
@@ -211,18 +210,6 @@ func TestDownloadObject_MultiSelect(t *testing.T) {
211210
assert.Assert(t, foundFile2, "file2.txt should be in the zip")
212211
}
213212

214-
type mockProjectGCSClient struct {
215-
*gcs.Client
216-
projects map[string]*gcs.ProjectMetadata
217-
}
218-
219-
func (m *mockProjectGCSClient) GetProjectMetadata(_ context.Context, projectID string) (*gcs.ProjectMetadata, error) {
220-
if p, ok := m.projects[projectID]; ok {
221-
return p, nil
222-
}
223-
return nil, fmt.Errorf("project not found")
224-
}
225-
226213
func TestProjectInformationPreview(t *testing.T) {
227214
objects := []fakestorage.Object{
228215
{ObjectAttrs: fakestorage.ObjectAttrs{BucketName: "test-bucket-1", Name: "init"}, Content: []byte("hi")},
@@ -244,9 +231,9 @@ func TestProjectInformationPreview(t *testing.T) {
244231
cfg, _ := config.Load(cfgPath)
245232

246233
realClient := gcs.NewClient(server.Client())
247-
mockClient := &mockProjectGCSClient{
248-
Client: realClient,
249-
projects: map[string]*gcs.ProjectMetadata{
234+
mockClient := &testutil.MockProjectGCSClient{
235+
GCSClient: realClient,
236+
Projects: map[string]*gcs.ProjectMetadata{
250237
"test-project-1": {
251238
ProjectID: "test-project-1",
252239
Name: "Test Project",
@@ -319,9 +306,9 @@ func TestProjectInformationPreview_Error(t *testing.T) {
319306
cfg, _ := config.Load(cfgPath)
320307

321308
realClient := gcs.NewClient(server.Client())
322-
mockClient := &mockProjectGCSClient{
323-
Client: realClient,
324-
projects: map[string]*gcs.ProjectMetadata{}, // Empty to trigger error
309+
mockClient := &testutil.MockProjectGCSClient{
310+
GCSClient: realClient,
311+
ProjectError: fmt.Errorf("project not found"),
325312
}
326313

327314
m := tui.NewModel(cfg.Projects, mockClient, cfg.DownloadDir, cfg.FuzzySearch, cfg.NerdIcons)
@@ -798,9 +785,9 @@ func TestProjectLabelsSorted(t *testing.T) {
798785
cfg, _ := config.Load(cfgPath)
799786

800787
realClient := gcs.NewClient(server.Client())
801-
mockClient := &mockProjectGCSClient{
802-
Client: realClient,
803-
projects: map[string]*gcs.ProjectMetadata{
788+
mockClient := &testutil.MockProjectGCSClient{
789+
GCSClient: realClient,
790+
Projects: map[string]*gcs.ProjectMetadata{
804791
"test-project-1": {
805792
ProjectID: "test-project-1",
806793
Name: "Test Project",

0 commit comments

Comments
 (0)