summaryrefslogtreecommitdiff
path: root/internal/cli/repo_client_factory_injection_test.go
blob: 902eec15c2778adf535ebc895b2f617810fe898d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package cli

import (
	"errors"
	"testing"

	"codeberg.org/snonux/gitsyncer/internal/codeberg"
	"codeberg.org/snonux/gitsyncer/internal/config"
	"codeberg.org/snonux/gitsyncer/internal/forge"
	"codeberg.org/snonux/gitsyncer/internal/github"
)

type createCall struct {
	repoName    string
	description string
	privateRepo bool
}

type updateCall struct {
	repoName    string
	description string
}

type stubDescriptionRepoClient struct {
	hasToken           bool
	descriptionByRepo  map[string]string
	updatedDescription []updateCall
	createCalls        []createCall
}

func (s *stubDescriptionRepoClient) HasToken() bool { return s.hasToken }

func (s *stubDescriptionRepoClient) RepoExists(repoName string) (bool, error) {
	_, exists := s.descriptionByRepo[repoName]
	return exists, nil
}

func (s *stubDescriptionRepoClient) CreateRepo(repoName, description string, private bool) error {
	s.createCalls = append(s.createCalls, createCall{
		repoName:    repoName,
		description: description,
		privateRepo: private,
	})
	return nil
}

func (s *stubDescriptionRepoClient) DeleteRepo(_ string) error {
	return nil
}

func (s *stubDescriptionRepoClient) GetRepoDescription(repoName string) (string, bool, error) {
	description, exists := s.descriptionByRepo[repoName]
	return description, exists, nil
}

func (s *stubDescriptionRepoClient) UpdateRepoDescription(repoName, description string) error {
	s.updatedDescription = append(s.updatedDescription, updateCall{
		repoName:    repoName,
		description: description,
	})
	if s.descriptionByRepo == nil {
		s.descriptionByRepo = map[string]string{}
	}
	s.descriptionByRepo[repoName] = description
	return nil
}

type stubGitHubPublicRepoClient struct {
	hasToken bool
	repos    []github.Repository
}

func (s *stubGitHubPublicRepoClient) HasToken() bool {
	return s.hasToken
}

func (s *stubGitHubPublicRepoClient) ListPublicRepos() ([]github.Repository, error) {
	return s.repos, nil
}

type stubCodebergPublicRepoClient struct {
	orgRepos  []codeberg.Repository
	userRepos []codeberg.Repository
	orgErr    error
	userErr   error
}

func (s *stubCodebergPublicRepoClient) ListPublicRepos() ([]codeberg.Repository, error) {
	return s.orgRepos, s.orgErr
}

func (s *stubCodebergPublicRepoClient) ListUserPublicRepos() ([]codeberg.Repository, error) {
	return s.userRepos, s.userErr
}

type stubRepoClientFactory struct {
	githubRepoClient        forge.RepoClient
	codebergRepoClient      forge.RepoClient
	githubDescriptionClient forge.RepoDescriptionClient
	codebergDescClient      forge.RepoDescriptionClient
	githubPublicClient      githubPublicRepoClient
	codebergPublicClient    codebergPublicRepoClient

	githubRepoCalls       int
	codebergRepoCalls     int
	githubDescCalls       int
	codebergDescCalls     int
	githubPublicRepoCalls int
	codebergPublicCalls   int
	githubPublicToken     string
	githubPublicOrg       string
	codebergPublicToken   string
	codebergPublicOrg     string
	githubRepoToken       string
	githubRepoOrg         string
	codebergRepoToken     string
	codebergRepoOrg       string
}

func (s *stubRepoClientFactory) NewGitHubRepoClient(token, org string) forge.RepoClient {
	s.githubRepoCalls++
	s.githubRepoToken = token
	s.githubRepoOrg = org
	return s.githubRepoClient
}

func (s *stubRepoClientFactory) NewCodebergRepoClient(token, org string) forge.RepoClient {
	s.codebergRepoCalls++
	s.codebergRepoToken = token
	s.codebergRepoOrg = org
	return s.codebergRepoClient
}

func (s *stubRepoClientFactory) NewGitHubDescriptionClient(_, _ string) forge.RepoDescriptionClient {
	s.githubDescCalls++
	return s.githubDescriptionClient
}

func (s *stubRepoClientFactory) NewCodebergDescriptionClient(_, _ string) forge.RepoDescriptionClient {
	s.codebergDescCalls++
	return s.codebergDescClient
}

func (s *stubRepoClientFactory) NewGitHubPublicRepoClient(token, org string) githubPublicRepoClient {
	s.githubPublicRepoCalls++
	s.githubPublicToken = token
	s.githubPublicOrg = org
	return s.githubPublicClient
}

func (s *stubRepoClientFactory) NewCodebergPublicRepoClient(token, org string) codebergPublicRepoClient {
	s.codebergPublicCalls++
	s.codebergPublicToken = token
	s.codebergPublicOrg = org
	return s.codebergPublicClient
}

func TestSyncRepoDescriptionsWithFactory_UsesInjectedClients(t *testing.T) {
	t.Parallel()

	githubClient := &stubDescriptionRepoClient{
		hasToken:          true,
		descriptionByRepo: map[string]string{"demo": "from github"},
	}
	codebergClient := &stubDescriptionRepoClient{
		hasToken:          true,
		descriptionByRepo: map[string]string{"demo": "from codeberg"},
	}
	factory := &stubRepoClientFactory{
		githubDescriptionClient: githubClient,
		codebergDescClient:      codebergClient,
	}

	cfg := &config.Config{
		Organizations: []config.Organization{
			{
				Host:          "git@github.com",
				Name:          "acme",
				GitHubToken:   "gh-token",
				CodebergToken: "",
			},
			{
				Host:          "git@codeberg.org",
				Name:          "acme",
				CodebergToken: "cb-token",
			},
		},
	}
	cache := map[string]string{}

	syncRepoDescriptionsWithFactory(cfg, false, "demo", "", "", cache, factory)

	if factory.githubDescCalls != 1 || factory.codebergDescCalls != 1 {
		t.Fatalf("expected one injected description client creation per forge, got github=%d codeberg=%d", factory.githubDescCalls, factory.codebergDescCalls)
	}

	if len(codebergClient.updatedDescription) != 0 {
		t.Fatalf("expected no Codeberg update when it already has canonical description, got %d updates", len(codebergClient.updatedDescription))
	}

	if len(githubClient.updatedDescription) != 1 {
		t.Fatalf("expected GitHub description to be updated once, got %d updates", len(githubClient.updatedDescription))
	}
	if githubClient.updatedDescription[0].description != "from codeberg" {
		t.Fatalf("github description update = %q, want %q", githubClient.updatedDescription[0].description, "from codeberg")
	}

	if cache["demo"] != "from codeberg" {
		t.Fatalf("cache description = %q, want %q", cache["demo"], "from codeberg")
	}
}

func TestCreateRepoHelpersWithFactory_UseInjectedCreateClients(t *testing.T) {
	t.Parallel()

	githubClient := &stubDescriptionRepoClient{hasToken: true}
	codebergClient := &stubDescriptionRepoClient{hasToken: true}
	factory := &stubRepoClientFactory{
		githubRepoClient:   githubClient,
		codebergRepoClient: codebergClient,
	}

	cfg := &config.Config{
		Organizations: []config.Organization{
			{
				Host:        "git@github.com",
				Name:        "acme",
				GitHubToken: "gh-token",
			},
			{
				Host:          "git@codeberg.org",
				Name:          "acme",
				CodebergToken: "cb-token",
			},
		},
	}

	if err := createGitHubRepoIfNeededWithFactory(cfg, "demo", factory); err != nil {
		t.Fatalf("createGitHubRepoIfNeededWithFactory() error = %v", err)
	}
	if err := createCodebergRepoIfNeededWithFactory(cfg, "demo", factory); err != nil {
		t.Fatalf("createCodebergRepoIfNeededWithFactory() error = %v", err)
	}

	if factory.githubRepoCalls != 1 || factory.codebergRepoCalls != 1 {
		t.Fatalf("expected one injected create client per forge, got github=%d codeberg=%d", factory.githubRepoCalls, factory.codebergRepoCalls)
	}
	if factory.githubRepoToken != "gh-token" || factory.githubRepoOrg != "acme" {
		t.Fatalf("github repo client init args = (%q, %q), want (%q, %q)", factory.githubRepoToken, factory.githubRepoOrg, "gh-token", "acme")
	}
	if factory.codebergRepoToken != "cb-token" || factory.codebergRepoOrg != "acme" {
		t.Fatalf("codeberg repo client init args = (%q, %q), want (%q, %q)", factory.codebergRepoToken, factory.codebergRepoOrg, "cb-token", "acme")
	}

	if len(githubClient.createCalls) != 1 {
		t.Fatalf("expected one GitHub create call, got %d", len(githubClient.createCalls))
	}
	if len(codebergClient.createCalls) != 1 {
		t.Fatalf("expected one Codeberg create call, got %d", len(codebergClient.createCalls))
	}

	if githubClient.createCalls[0].description != "Mirror of demo" {
		t.Fatalf("github create description = %q, want %q", githubClient.createCalls[0].description, "Mirror of demo")
	}
	if codebergClient.createCalls[0].description != "Mirror of demo" {
		t.Fatalf("codeberg create description = %q, want %q", codebergClient.createCalls[0].description, "Mirror of demo")
	}
}

func TestHandleSyncGitHubPublic_UsesInjectedFactoryClient(t *testing.T) {
	t.Parallel()

	oldFactory := cliRepoClientFactory
	t.Cleanup(func() { cliRepoClientFactory = oldFactory })

	factory := &stubRepoClientFactory{
		githubPublicClient: &stubGitHubPublicRepoClient{
			hasToken: true,
			repos: []github.Repository{
				{Name: "demo"},
			},
		},
	}
	cliRepoClientFactory = factory

	cfg := &config.Config{
		Organizations: []config.Organization{
			{Host: "git@github.com", Name: "acme", GitHubToken: "gh-token"},
		},
	}
	flags := &Flags{
		DryRun:  true,
		WorkDir: t.TempDir(),
	}

	if got := HandleSyncGitHubPublic(cfg, flags); got != 0 {
		t.Fatalf("HandleSyncGitHubPublic() = %d, want 0", got)
	}
	if factory.githubPublicRepoCalls != 1 {
		t.Fatalf("expected exactly one injected GitHub public client creation, got %d", factory.githubPublicRepoCalls)
	}
	if factory.githubPublicToken != "gh-token" || factory.githubPublicOrg != "acme" {
		t.Fatalf("github public client init args = (%q, %q), want (%q, %q)", factory.githubPublicToken, factory.githubPublicOrg, "gh-token", "acme")
	}
}

func TestHandleSyncCodebergPublic_UsesInjectedFactoryClient(t *testing.T) {
	t.Parallel()

	oldFactory := cliRepoClientFactory
	t.Cleanup(func() { cliRepoClientFactory = oldFactory })

	factory := &stubRepoClientFactory{
		codebergPublicClient: &stubCodebergPublicRepoClient{
			orgErr: errors.New("org lookup failed"),
			userRepos: []codeberg.Repository{
				{Name: "demo"},
			},
		},
	}
	cliRepoClientFactory = factory

	cfg := &config.Config{
		Organizations: []config.Organization{
			{Host: "git@codeberg.org", Name: "acme", CodebergToken: "cb-token"},
		},
	}
	flags := &Flags{
		DryRun:           true,
		SyncGitHubPublic: false,
		WorkDir:          t.TempDir(),
	}

	if got := HandleSyncCodebergPublic(cfg, flags); got != 0 {
		t.Fatalf("HandleSyncCodebergPublic() = %d, want 0", got)
	}
	if factory.codebergPublicCalls != 1 {
		t.Fatalf("expected exactly one injected Codeberg public client creation, got %d", factory.codebergPublicCalls)
	}
	if factory.codebergPublicToken != "cb-token" || factory.codebergPublicOrg != "acme" {
		t.Fatalf("codeberg public client init args = (%q, %q), want (%q, %q)", factory.codebergPublicToken, factory.codebergPublicOrg, "cb-token", "acme")
	}
}

func TestInitGitHubClientWithFactory_Branches(t *testing.T) {
	t.Parallel()

	cfg := &config.Config{
		Organizations: []config.Organization{
			{Host: "git@github.com", Name: "acme", GitHubToken: "gh-token"},
		},
	}

	t.Run("returns client when token exists", func(t *testing.T) {
		t.Parallel()
		client := &stubDescriptionRepoClient{hasToken: true}
		factory := &stubRepoClientFactory{githubRepoClient: client}

		got := initGitHubClientWithFactory(cfg, factory)
		if got == nil {
			t.Fatal("expected GitHub client, got nil")
		}
		if factory.githubRepoCalls != 1 {
			t.Fatalf("github repo factory calls = %d, want 1", factory.githubRepoCalls)
		}
	})

	t.Run("returns nil without token", func(t *testing.T) {
		t.Parallel()
		factory := &stubRepoClientFactory{githubRepoClient: &stubDescriptionRepoClient{hasToken: false}}

		if got := initGitHubClientWithFactory(cfg, factory); got != nil {
			t.Fatal("expected nil GitHub client when token is missing")
		}
	})

	t.Run("returns nil when factory returns nil client", func(t *testing.T) {
		t.Parallel()
		factory := &stubRepoClientFactory{githubRepoClient: nil}

		if got := initGitHubClientWithFactory(cfg, factory); got != nil {
			t.Fatal("expected nil GitHub client when factory returns nil")
		}
	})
}

func TestInitCodebergClientWithFactory_Branches(t *testing.T) {
	t.Parallel()

	cfg := &config.Config{
		Organizations: []config.Organization{
			{Host: "git@codeberg.org", Name: "acme", CodebergToken: "cb-token"},
		},
	}

	t.Run("returns client when token exists", func(t *testing.T) {
		t.Parallel()
		client := &stubDescriptionRepoClient{hasToken: true}
		factory := &stubRepoClientFactory{codebergRepoClient: client}

		got := initCodebergClientWithFactory(cfg, factory)
		if got == nil {
			t.Fatal("expected Codeberg client, got nil")
		}
		if factory.codebergRepoCalls != 1 {
			t.Fatalf("codeberg repo factory calls = %d, want 1", factory.codebergRepoCalls)
		}
	})

	t.Run("returns nil without token", func(t *testing.T) {
		t.Parallel()
		factory := &stubRepoClientFactory{codebergRepoClient: &stubDescriptionRepoClient{hasToken: false}}

		if got := initCodebergClientWithFactory(cfg, factory); got != nil {
			t.Fatal("expected nil Codeberg client when token is missing")
		}
	})

	t.Run("returns nil when factory returns nil client", func(t *testing.T) {
		t.Parallel()
		factory := &stubRepoClientFactory{codebergRepoClient: nil}

		if got := initCodebergClientWithFactory(cfg, factory); got != nil {
			t.Fatal("expected nil Codeberg client when factory returns nil")
		}
	})
}