Skip to content

Commit a11ecdc

Browse files
committed
decoupling CodexClient from the rest of the system
1 parent 2e85ae3 commit a11ecdc

9 files changed

+62
-48
lines changed

codexclient/codex_client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ func (c *CodexClient) FetchManifestWithContext(ctx context.Context, cid string)
102102
Cid: manifest.Cid,
103103
TreeCid: manifest.TreeCid,
104104
DatasetSize: manifest.DatasetSize,
105+
BlockSize: manifest.BlockSize,
105106
Filename: manifest.Filename,
107+
Mimetype: manifest.Mimetype,
106108
}, nil
107109
}
108110

@@ -115,7 +117,9 @@ func (c *CodexClient) TriggerDownloadWithContext(ctx context.Context, cid string
115117
Cid: manifest.Cid,
116118
TreeCid: manifest.TreeCid,
117119
DatasetSize: manifest.DatasetSize,
120+
BlockSize: manifest.BlockSize,
118121
Filename: manifest.Filename,
122+
Mimetype: manifest.Mimetype,
119123
}, nil
120124
}
121125

communities/codex_client_integration_test.go renamed to codexclient/codex_client_integration_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//go:build codex_integration
22
// +build codex_integration
33

4-
package communities_test
4+
package codexclient_test
55

66
import (
77
"bytes"
@@ -16,19 +16,21 @@ import (
1616
"github.com/stretchr/testify/require"
1717
"github.com/stretchr/testify/suite"
1818

19+
"go-codex-client/codexclient"
20+
"go-codex-client/codextestutils"
1921
"go-codex-client/communities"
2022
)
2123

2224
// CodexClientIntegrationTestSuite demonstrates testify's suite functionality for CodexClient integration tests
2325
type CodexClientIntegrationTestSuite struct {
2426
suite.Suite
25-
client *communities.CodexClient
27+
client communities.CodexClientInterface
2628
}
2729

2830
// SetupSuite runs once before all tests in the suite
2931
func (suite *CodexClientIntegrationTestSuite) SetupSuite() {
3032
var err error
31-
suite.client, err = communities.NewCodexClient(codex.Config{
33+
suite.client, err = codexclient.NewCodexClient(codex.Config{
3234
DataDir: suite.T().TempDir(),
3335
LogFormat: codex.LogFormatNoColors,
3436
MetricsEnabled: false,
@@ -107,7 +109,7 @@ func (suite *CodexClientIntegrationTestSuite) TestIntegration_CheckNonExistingCI
107109
}
108110

109111
func (suite *CodexClientIntegrationTestSuite) TestIntegration_TriggerDownload() {
110-
client := NewCodexClientTest(suite.T())
112+
client := codextestutils.NewCodexClientTest(suite.T())
111113

112114
// Generate random payload to ensure proper round-trip verification
113115
payload := make([]byte, 1024)

codexclient/codex_client_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//go:build codex_integration
2+
// +build codex_integration
23

34
package codexclient_test
45

communities/codex_archive_downloader_integration_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/stretchr/testify/suite"
1818
"go.uber.org/zap"
1919

20+
"go-codex-client/codexclient"
2021
"go-codex-client/communities"
2122
"go-codex-client/protobuf"
2223
)
@@ -25,14 +26,14 @@ import (
2526
// against a real Codex instance
2627
type CodexArchiveDownloaderIntegrationSuite struct {
2728
suite.Suite
28-
client *communities.CodexClient
29+
client communities.CodexClientInterface
2930
uploadedCIDs []string // Track uploaded CIDs for cleanup
3031
}
3132

3233
// SetupSuite runs once before all tests in the suite
3334
func (suite *CodexArchiveDownloaderIntegrationSuite) SetupSuite() {
3435
var err error
35-
suite.client, err = communities.NewCodexClient(codex.Config{
36+
suite.client, err = codexclient.NewCodexClient(codex.Config{
3637
LogFormat: codex.LogFormatNoColors,
3738
MetricsEnabled: false,
3839
BlockRetries: 5,

communities/codex_archive_downloader_test.go

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import (
1111
"github.com/stretchr/testify/require"
1212
"github.com/stretchr/testify/suite"
1313

14-
"go-codex-client/communities"
1514
mock_communities "go-codex-client/communities/mock"
1615
"go-codex-client/protobuf"
1716

1817
"go.uber.org/mock/gomock"
1918
"go.uber.org/zap"
19+
20+
"go-codex-client/codexmanifest"
21+
"go-codex-client/communities"
2022
)
2123

2224
// CodexArchiveDownloaderSuite demonstrates testify's suite functionality
@@ -63,7 +65,7 @@ func (suite *CodexArchiveDownloaderSuite) TestBasicSingleArchive() {
6365
// Set up mock expectations - same as before
6466
suite.mockClient.EXPECT().
6567
TriggerDownloadWithContext(gomock.Any(), "test-cid-1").
66-
Return(communities.CodexManifest{Cid: "test-cid-1"}, nil).
68+
Return(codexmanifest.CodexManifest{Cid: "test-cid-1"}, nil).
6769
Times(1)
6870

6971
// First HasCid call returns false, second returns true (simulating polling)
@@ -149,7 +151,7 @@ func (suite *CodexArchiveDownloaderSuite) TestMultipleArchives() {
149151
for _, cid := range expectedCids {
150152
suite.mockClient.EXPECT().
151153
TriggerDownloadWithContext(gomock.Any(), cid).
152-
Return(communities.CodexManifest{Cid: cid}, nil).
154+
Return(codexmanifest.CodexManifest{Cid: cid}, nil).
153155
Times(1)
154156

155157
// Each archive becomes available after one poll
@@ -236,7 +238,7 @@ func (suite *CodexArchiveDownloaderSuite) TestErrorDuringTriggerDownload() {
236238
// Mock TriggerDownloadWithContext to simulate an error
237239
suite.mockClient.EXPECT().
238240
TriggerDownloadWithContext(gomock.Any(), "test-cid-1").
239-
Return(communities.CodexManifest{}, assert.AnError). // Return a generic error to simulate failure
241+
Return(codexmanifest.CodexManifest{}, assert.AnError). // Return a generic error to simulate failure
240242
Times(1)
241243

242244
// No HasCid calls should be made since TriggerDownload fails
@@ -288,13 +290,13 @@ func (suite *CodexArchiveDownloaderSuite) TestActualCancellationDuringTriggerDow
288290
// Use DoAndReturn to create a realistic TriggerDownload that waits for cancellation
289291
suite.mockClient.EXPECT().
290292
TriggerDownloadWithContext(gomock.Any(), "test-cid-1").
291-
DoAndReturn(func(ctx context.Context, cid string) (communities.CodexManifest, error) {
293+
DoAndReturn(func(ctx context.Context, cid string) (codexmanifest.CodexManifest, error) {
292294
// Simulate work by waiting for context cancellation
293295
select {
294296
case <-time.After(5 * time.Second): // This should never happen in our test
295-
return communities.CodexManifest{Cid: cid}, nil
297+
return codexmanifest.CodexManifest{Cid: cid}, nil
296298
case <-ctx.Done(): // Wait for actual context cancellation
297-
return communities.CodexManifest{}, ctx.Err() // Return the actual cancellation error
299+
return codexmanifest.CodexManifest{}, ctx.Err() // Return the actual cancellation error
298300
}
299301
}).
300302
Times(1)
@@ -352,7 +354,7 @@ func (suite *CodexArchiveDownloaderSuite) TestCancellationDuringPolling() {
352354
// Mock successful TriggerDownload
353355
suite.mockClient.EXPECT().
354356
TriggerDownloadWithContext(gomock.Any(), "test-cid-1").
355-
Return(communities.CodexManifest{Cid: "test-cid-1"}, nil).
357+
Return(codexmanifest.CodexManifest{Cid: "test-cid-1"}, nil).
356358
Times(1)
357359

358360
// Mock polling - allow multiple calls, but we'll cancel before completion
@@ -420,7 +422,7 @@ func (suite *CodexArchiveDownloaderSuite) TestPollingTimeout() {
420422
// Mock successful TriggerDownload
421423
suite.mockClient.EXPECT().
422424
TriggerDownloadWithContext(gomock.Any(), "test-cid-1").
423-
Return(communities.CodexManifest{Cid: "test-cid-1"}, nil).
425+
Return(codexmanifest.CodexManifest{Cid: "test-cid-1"}, nil).
424426
Times(1)
425427

426428
// Mock polling to always return false (simulating timeout)
@@ -496,7 +498,7 @@ func (suite *CodexArchiveDownloaderSuite) TestWithExistingArchives() {
496498
// Only archive-2 should be downloaded (not in existingArchiveIDs)
497499
suite.mockClient.EXPECT().
498500
TriggerDownloadWithContext(gomock.Any(), "cid-2").
499-
Return(communities.CodexManifest{Cid: "cid-2"}, nil).
501+
Return(codexmanifest.CodexManifest{Cid: "cid-2"}, nil).
500502
Times(1) // Only one call expected
501503

502504
// Only archive-2 should be polled
@@ -577,15 +579,15 @@ func (suite *CodexArchiveDownloaderSuite) TestPartialSuccess_OneSuccessOneError(
577579
// Archive-2 succeeds
578580
suite.mockClient.EXPECT().
579581
TriggerDownloadWithContext(gomock.Any(), "cid-2").
580-
Return(communities.CodexManifest{Cid: "cid-2"}, nil)
582+
Return(codexmanifest.CodexManifest{Cid: "cid-2"}, nil)
581583
suite.mockClient.EXPECT().
582584
HasCid("cid-2").
583585
Return(true, nil)
584586

585587
// Archive-1 fails
586588
suite.mockClient.EXPECT().
587589
TriggerDownloadWithContext(gomock.Any(), "cid-1").
588-
Return(communities.CodexManifest{}, fmt.Errorf("trigger failed"))
590+
Return(codexmanifest.CodexManifest{}, fmt.Errorf("trigger failed"))
589591

590592
logger := zap.NewNop()
591593
downloader := communities.NewCodexArchiveDownloader(suite.mockClient, index, communityID, []string{}, cancelChan, logger)
@@ -633,22 +635,22 @@ func (suite *CodexArchiveDownloaderSuite) TestPartialSuccess_SuccessErrorCancell
633635
// Archive-3 (newest) succeeds
634636
suite.mockClient.EXPECT().
635637
TriggerDownloadWithContext(gomock.Any(), "cid-3").
636-
Return(communities.CodexManifest{Cid: "cid-3"}, nil)
638+
Return(codexmanifest.CodexManifest{Cid: "cid-3"}, nil)
637639
suite.mockClient.EXPECT().
638640
HasCid("cid-3").
639641
Return(true, nil)
640642

641643
// Archive-2 fails
642644
suite.mockClient.EXPECT().
643645
TriggerDownloadWithContext(gomock.Any(), "cid-2").
644-
Return(communities.CodexManifest{}, fmt.Errorf("trigger failed"))
646+
Return(codexmanifest.CodexManifest{}, fmt.Errorf("trigger failed"))
645647

646648
// Archive-1 will be cancelled (no expectations needed)
647649
suite.mockClient.EXPECT().
648650
TriggerDownloadWithContext(gomock.Any(), "cid-1").
649-
DoAndReturn(func(ctx context.Context, cid string) (communities.CodexManifest, error) {
651+
DoAndReturn(func(ctx context.Context, cid string) (codexmanifest.CodexManifest, error) {
650652
<-ctx.Done() // Wait for cancellation
651-
return communities.CodexManifest{}, ctx.Err()
653+
return codexmanifest.CodexManifest{}, ctx.Err()
652654
}).
653655
AnyTimes()
654656

@@ -700,17 +702,17 @@ func (suite *CodexArchiveDownloaderSuite) TestPartialSuccess_SuccessThenCancella
700702
// Archive-2 (newer) succeeds
701703
suite.mockClient.EXPECT().
702704
TriggerDownloadWithContext(gomock.Any(), "cid-2").
703-
Return(communities.CodexManifest{Cid: "cid-2"}, nil)
705+
Return(codexmanifest.CodexManifest{Cid: "cid-2"}, nil)
704706
suite.mockClient.EXPECT().
705707
HasCid("cid-2").
706708
Return(true, nil)
707709

708710
// Archive-1 will be cancelled
709711
suite.mockClient.EXPECT().
710712
TriggerDownloadWithContext(gomock.Any(), "cid-1").
711-
DoAndReturn(func(ctx context.Context, cid string) (communities.CodexManifest, error) {
713+
DoAndReturn(func(ctx context.Context, cid string) (codexmanifest.CodexManifest, error) {
712714
<-ctx.Done() // Wait for cancellation
713-
return communities.CodexManifest{}, ctx.Err()
715+
return codexmanifest.CodexManifest{}, ctx.Err()
714716
}).
715717
AnyTimes()
716718

@@ -762,9 +764,9 @@ func (suite *CodexArchiveDownloaderSuite) TestNoSuccess_OnlyCancellation() {
762764
// Both archives will be cancelled
763765
suite.mockClient.EXPECT().
764766
TriggerDownloadWithContext(gomock.Any(), gomock.Any()).
765-
DoAndReturn(func(ctx context.Context, cid string) (communities.CodexManifest, error) {
767+
DoAndReturn(func(ctx context.Context, cid string) (codexmanifest.CodexManifest, error) {
766768
<-ctx.Done() // Wait for cancellation
767-
return communities.CodexManifest{}, ctx.Err()
769+
return codexmanifest.CodexManifest{}, ctx.Err()
768770
}).
769771
AnyTimes()
770772

@@ -815,10 +817,10 @@ func (suite *CodexArchiveDownloaderSuite) TestNoSuccess_OnlyErrors() {
815817
// Both archives fail
816818
suite.mockClient.EXPECT().
817819
TriggerDownloadWithContext(gomock.Any(), "cid-1").
818-
Return(communities.CodexManifest{}, fmt.Errorf("trigger failed for cid-1"))
820+
Return(codexmanifest.CodexManifest{}, fmt.Errorf("trigger failed for cid-1"))
819821
suite.mockClient.EXPECT().
820822
TriggerDownloadWithContext(gomock.Any(), "cid-2").
821-
Return(communities.CodexManifest{}, fmt.Errorf("trigger failed for cid-2"))
823+
Return(codexmanifest.CodexManifest{}, fmt.Errorf("trigger failed for cid-2"))
822824

823825
logger := zap.NewNop()
824826
downloader := communities.NewCodexArchiveDownloader(suite.mockClient, index, communityID, []string{}, cancelChan, logger)

communities/codex_client_interface.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package communities
33
import (
44
"context"
55
"io"
6+
7+
"go-codex-client/codexmanifest"
68
)
79

810
// Mock generation instruction above will create a mock in package `mock_communities`
@@ -24,11 +26,11 @@ type CodexClientInterface interface {
2426
LocalDownloadWithContext(ctx context.Context, cid string, output io.Writer) error
2527

2628
// Async download methods
27-
TriggerDownload(cid string) (CodexManifest, error)
28-
TriggerDownloadWithContext(ctx context.Context, cid string) (CodexManifest, error)
29+
TriggerDownload(cid string) (codexmanifest.CodexManifest, error)
30+
TriggerDownloadWithContext(ctx context.Context, cid string) (codexmanifest.CodexManifest, error)
2931

3032
// Manifest methods
31-
FetchManifestWithContext(ctx context.Context, cid string) (CodexManifest, error)
33+
FetchManifestWithContext(ctx context.Context, cid string) (codexmanifest.CodexManifest, error)
3234

3335
// CID management methods
3436
HasCid(cid string) (bool, error)

communities/codex_index_downloader_integration_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/stretchr/testify/suite"
1818
"go.uber.org/zap"
1919

20+
"go-codex-client/codextestutils"
2021
"go-codex-client/communities"
2122
)
2223

@@ -28,14 +29,14 @@ import (
2829
// - CODEX_TIMEOUT_MS (optional; default: 60000)
2930
type CodexIndexDownloaderIntegrationTestSuite struct {
3031
suite.Suite
31-
client *communities.CodexClient
32+
client communities.CodexClientInterface
3233
testDir string
3334
logger *zap.Logger
3435
}
3536

3637
// SetupSuite runs once before all tests in the suite
3738
func (suite *CodexIndexDownloaderIntegrationTestSuite) SetupSuite() {
38-
suite.client = NewCodexClientTest(suite.T())
39+
suite.client = codextestutils.NewCodexClientTest(suite.T())
3940

4041
// Create logger
4142
suite.logger, _ = zap.NewDevelopment()

communities/codex_index_downloader_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"go.uber.org/mock/gomock"
1616
"go.uber.org/zap"
1717

18+
"go-codex-client/codexmanifest"
1819
"go-codex-client/communities"
1920
mock_communities "go-codex-client/communities/mock"
2021
)
@@ -79,7 +80,7 @@ func (suite *CodexIndexDownloaderTestSuite) TestGotManifest_SuccessClosesChannel
7980
filePath := filepath.Join(suite.testDir, "index.bin")
8081

8182
// Setup mock to return a successful manifest
82-
expectedManifest := communities.CodexManifest{
83+
expectedManifest := codexmanifest.CodexManifest{
8384
Cid: testCid,
8485
}
8586
expectedManifest.DatasetSize = 1024
@@ -119,7 +120,7 @@ func (suite *CodexIndexDownloaderTestSuite) TestGotManifest_ErrorDoesNotCloseCha
119120
// Setup mock to return an error
120121
suite.mockClient.EXPECT().
121122
FetchManifestWithContext(gomock.Any(), testCid).
122-
Return(communities.CodexManifest{}, errors.New("fetch error"))
123+
Return(codexmanifest.CodexManifest{}, errors.New("fetch error"))
123124

124125
// Create downloader
125126
downloader := communities.NewCodexIndexDownloader(suite.mockClient, testCid, filePath, suite.cancelChan, suite.logger)
@@ -154,7 +155,7 @@ func (suite *CodexIndexDownloaderTestSuite) TestGotManifest_CidMismatchDoesNotCl
154155
filePath := filepath.Join(suite.testDir, "index.bin")
155156

156157
// Setup mock to return a manifest with different CID
157-
mismatchedManifest := communities.CodexManifest{
158+
mismatchedManifest := codexmanifest.CodexManifest{
158159
Cid: differentCid, // Different CID!
159160
}
160161
mismatchedManifest.DatasetSize = 1024
@@ -198,12 +199,12 @@ func (suite *CodexIndexDownloaderTestSuite) TestGotManifest_Cancellation() {
198199
fetchCalled := make(chan struct{})
199200
suite.mockClient.EXPECT().
200201
FetchManifestWithContext(gomock.Any(), testCid).
201-
DoAndReturn(func(ctx context.Context, cid string) (communities.CodexManifest, error) {
202+
DoAndReturn(func(ctx context.Context, cid string) (codexmanifest.CodexManifest, error) {
202203
close(fetchCalled) // Signal that fetch was called
203204

204205
// Wait for context cancellation
205206
<-ctx.Done()
206-
return communities.CodexManifest{}, ctx.Err()
207+
return codexmanifest.CodexManifest{}, ctx.Err()
207208
})
208209

209210
// Create downloader
@@ -250,7 +251,7 @@ func (suite *CodexIndexDownloaderTestSuite) TestGotManifest_RecordsDatasetSize()
250251
expectedSize := int64(2048)
251252

252253
// Setup mock to return a manifest with specific dataset size
253-
expectedManifest := communities.CodexManifest{
254+
expectedManifest := codexmanifest.CodexManifest{
254255
Cid: testCid,
255256
}
256257
expectedManifest.DatasetSize = int(expectedSize)
@@ -503,7 +504,7 @@ func (suite *CodexIndexDownloaderTestSuite) TestLength_ReturnsDatasetSize() {
503504
expectedSize := 4096
504505

505506
// Setup mock to return a manifest
506-
expectedManifest := communities.CodexManifest{
507+
expectedManifest := codexmanifest.CodexManifest{
507508
Cid: testCid,
508509
}
509510
expectedManifest.DatasetSize = expectedSize

0 commit comments

Comments
 (0)