From 722d67e2365c6cc4758f165d2b9dab1e37be0918 Mon Sep 17 00:00:00 2001 From: Arnaud Date: Thu, 30 Oct 2025 06:16:33 +0100 Subject: [PATCH] Use CodexManifest and CodexConfig types instead of exposing codex. --- communities/codex_archive_downloader_test.go | 45 ++++++++++---------- communities/codex_client.go | 9 ++-- communities/codex_client_interface.go | 8 ++-- communities/codex_index_downloader_test.go | 15 +++---- communities/mock/codex_client_interface.go | 14 +++--- 5 files changed, 45 insertions(+), 46 deletions(-) diff --git a/communities/codex_archive_downloader_test.go b/communities/codex_archive_downloader_test.go index a5de2c5..5dd82d1 100644 --- a/communities/codex_archive_downloader_test.go +++ b/communities/codex_archive_downloader_test.go @@ -7,7 +7,6 @@ import ( "testing" "time" - "github.com/codex-storage/codex-go-bindings/codex" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -64,7 +63,7 @@ func (suite *CodexArchiveDownloaderSuite) TestBasicSingleArchive() { // Set up mock expectations - same as before suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "test-cid-1"). - Return(codex.Manifest{Cid: "test-cid-1"}, nil). + Return(communities.CodexManifest{Cid: "test-cid-1"}, nil). Times(1) // First HasCid call returns false, second returns true (simulating polling) @@ -150,7 +149,7 @@ func (suite *CodexArchiveDownloaderSuite) TestMultipleArchives() { for _, cid := range expectedCids { suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), cid). - Return(codex.Manifest{Cid: cid}, nil). + Return(communities.CodexManifest{Cid: cid}, nil). Times(1) // Each archive becomes available after one poll @@ -237,7 +236,7 @@ func (suite *CodexArchiveDownloaderSuite) TestErrorDuringTriggerDownload() { // Mock TriggerDownloadWithContext to simulate an error suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "test-cid-1"). - Return(codex.Manifest{}, assert.AnError). // Return a generic error to simulate failure + Return(communities.CodexManifest{}, assert.AnError). // Return a generic error to simulate failure Times(1) // No HasCid calls should be made since TriggerDownload fails @@ -289,13 +288,13 @@ func (suite *CodexArchiveDownloaderSuite) TestActualCancellationDuringTriggerDow // Use DoAndReturn to create a realistic TriggerDownload that waits for cancellation suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "test-cid-1"). - DoAndReturn(func(ctx context.Context, cid string) (codex.Manifest, error) { + DoAndReturn(func(ctx context.Context, cid string) (communities.CodexManifest, error) { // Simulate work by waiting for context cancellation select { case <-time.After(5 * time.Second): // This should never happen in our test - return codex.Manifest{Cid: cid}, nil + return communities.CodexManifest{Cid: cid}, nil case <-ctx.Done(): // Wait for actual context cancellation - return codex.Manifest{}, ctx.Err() // Return the actual cancellation error + return communities.CodexManifest{}, ctx.Err() // Return the actual cancellation error } }). Times(1) @@ -353,7 +352,7 @@ func (suite *CodexArchiveDownloaderSuite) TestCancellationDuringPolling() { // Mock successful TriggerDownload suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "test-cid-1"). - Return(codex.Manifest{Cid: "test-cid-1"}, nil). + Return(communities.CodexManifest{Cid: "test-cid-1"}, nil). Times(1) // Mock polling - allow multiple calls, but we'll cancel before completion @@ -421,7 +420,7 @@ func (suite *CodexArchiveDownloaderSuite) TestPollingTimeout() { // Mock successful TriggerDownload suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "test-cid-1"). - Return(codex.Manifest{Cid: "test-cid-1"}, nil). + Return(communities.CodexManifest{Cid: "test-cid-1"}, nil). Times(1) // Mock polling to always return false (simulating timeout) @@ -497,7 +496,7 @@ func (suite *CodexArchiveDownloaderSuite) TestWithExistingArchives() { // Only archive-2 should be downloaded (not in existingArchiveIDs) suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "cid-2"). - Return(codex.Manifest{Cid: "cid-2"}, nil). + Return(communities.CodexManifest{Cid: "cid-2"}, nil). Times(1) // Only one call expected // Only archive-2 should be polled @@ -578,7 +577,7 @@ func (suite *CodexArchiveDownloaderSuite) TestPartialSuccess_OneSuccessOneError( // Archive-2 succeeds suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "cid-2"). - Return(codex.Manifest{Cid: "cid-2"}, nil) + Return(communities.CodexManifest{Cid: "cid-2"}, nil) suite.mockClient.EXPECT(). HasCid("cid-2"). Return(true, nil) @@ -586,7 +585,7 @@ func (suite *CodexArchiveDownloaderSuite) TestPartialSuccess_OneSuccessOneError( // Archive-1 fails suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "cid-1"). - Return(codex.Manifest{}, fmt.Errorf("trigger failed")) + Return(communities.CodexManifest{}, fmt.Errorf("trigger failed")) logger := zap.NewNop() downloader := communities.NewCodexArchiveDownloader(suite.mockClient, index, communityID, []string{}, cancelChan, logger) @@ -634,7 +633,7 @@ func (suite *CodexArchiveDownloaderSuite) TestPartialSuccess_SuccessErrorCancell // Archive-3 (newest) succeeds suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "cid-3"). - Return(codex.Manifest{Cid: "cid-3"}, nil) + Return(communities.CodexManifest{Cid: "cid-3"}, nil) suite.mockClient.EXPECT(). HasCid("cid-3"). Return(true, nil) @@ -642,14 +641,14 @@ func (suite *CodexArchiveDownloaderSuite) TestPartialSuccess_SuccessErrorCancell // Archive-2 fails suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "cid-2"). - Return(codex.Manifest{}, fmt.Errorf("trigger failed")) + Return(communities.CodexManifest{}, fmt.Errorf("trigger failed")) // Archive-1 will be cancelled (no expectations needed) suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "cid-1"). - DoAndReturn(func(ctx context.Context, cid string) (codex.Manifest, error) { + DoAndReturn(func(ctx context.Context, cid string) (communities.CodexManifest, error) { <-ctx.Done() // Wait for cancellation - return codex.Manifest{}, ctx.Err() + return communities.CodexManifest{}, ctx.Err() }). AnyTimes() @@ -701,7 +700,7 @@ func (suite *CodexArchiveDownloaderSuite) TestPartialSuccess_SuccessThenCancella // Archive-2 (newer) succeeds suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "cid-2"). - Return(codex.Manifest{Cid: "cid-2"}, nil) + Return(communities.CodexManifest{Cid: "cid-2"}, nil) suite.mockClient.EXPECT(). HasCid("cid-2"). Return(true, nil) @@ -709,9 +708,9 @@ func (suite *CodexArchiveDownloaderSuite) TestPartialSuccess_SuccessThenCancella // Archive-1 will be cancelled suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "cid-1"). - DoAndReturn(func(ctx context.Context, cid string) (codex.Manifest, error) { + DoAndReturn(func(ctx context.Context, cid string) (communities.CodexManifest, error) { <-ctx.Done() // Wait for cancellation - return codex.Manifest{}, ctx.Err() + return communities.CodexManifest{}, ctx.Err() }). AnyTimes() @@ -763,9 +762,9 @@ func (suite *CodexArchiveDownloaderSuite) TestNoSuccess_OnlyCancellation() { // Both archives will be cancelled suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), gomock.Any()). - DoAndReturn(func(ctx context.Context, cid string) (codex.Manifest, error) { + DoAndReturn(func(ctx context.Context, cid string) (communities.CodexManifest, error) { <-ctx.Done() // Wait for cancellation - return codex.Manifest{}, ctx.Err() + return communities.CodexManifest{}, ctx.Err() }). AnyTimes() @@ -816,10 +815,10 @@ func (suite *CodexArchiveDownloaderSuite) TestNoSuccess_OnlyErrors() { // Both archives fail suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "cid-1"). - Return(codex.Manifest{}, fmt.Errorf("trigger failed for cid-1")) + Return(communities.CodexManifest{}, fmt.Errorf("trigger failed for cid-1")) suite.mockClient.EXPECT(). TriggerDownloadWithContext(gomock.Any(), "cid-2"). - Return(codex.Manifest{}, fmt.Errorf("trigger failed for cid-2")) + Return(communities.CodexManifest{}, fmt.Errorf("trigger failed for cid-2")) logger := zap.NewNop() downloader := communities.NewCodexArchiveDownloader(suite.mockClient, index, communityID, []string{}, cancelChan, logger) diff --git a/communities/codex_client.go b/communities/codex_client.go index 357c25a..aa3f70e 100644 --- a/communities/codex_client.go +++ b/communities/codex_client.go @@ -21,6 +21,9 @@ type CodexClient struct { config *codex.Config } +type CodexManifest = codex.Manifest +type CodexConf = codex.Config + // NewCodexClient creates a new Codex client func NewCodexClient(config codex.Config) (*CodexClient, error) { node, err := codex.New(config) @@ -58,7 +61,7 @@ func (c *CodexClient) Download(cid string, output io.Writer) error { return c.DownloadWithContext(context.Background(), cid, output) } -func (c *CodexClient) TriggerDownload(cid string) (codex.Manifest, error) { +func (c *CodexClient) TriggerDownload(cid string) (CodexManifest, error) { return c.TriggerDownloadWithContext(context.Background(), cid) } @@ -89,11 +92,11 @@ func (c *CodexClient) LocalDownloadWithContext(ctx context.Context, cid string, return c.LocalDownload(cid, output) } -func (c *CodexClient) FetchManifestWithContext(ctx context.Context, cid string) (codex.Manifest, error) { +func (c *CodexClient) FetchManifestWithContext(ctx context.Context, cid string) (CodexManifest, error) { return c.node.DownloadManifest(cid) } -func (c *CodexClient) TriggerDownloadWithContext(ctx context.Context, cid string) (codex.Manifest, error) { +func (c *CodexClient) TriggerDownloadWithContext(ctx context.Context, cid string) (CodexManifest, error) { return c.node.Fetch(cid) } diff --git a/communities/codex_client_interface.go b/communities/codex_client_interface.go index ec05ea8..110eba3 100644 --- a/communities/codex_client_interface.go +++ b/communities/codex_client_interface.go @@ -3,8 +3,6 @@ package communities import ( "context" "io" - - "github.com/codex-storage/codex-go-bindings/codex" ) // Mock generation instruction above will create a mock in package `mock_communities` @@ -26,11 +24,11 @@ type CodexClientInterface interface { LocalDownloadWithContext(ctx context.Context, cid string, output io.Writer) error // Async download methods - TriggerDownload(cid string) (codex.Manifest, error) - TriggerDownloadWithContext(ctx context.Context, cid string) (codex.Manifest, error) + TriggerDownload(cid string) (CodexManifest, error) + TriggerDownloadWithContext(ctx context.Context, cid string) (CodexManifest, error) // Manifest methods - FetchManifestWithContext(ctx context.Context, cid string) (codex.Manifest, error) + FetchManifestWithContext(ctx context.Context, cid string) (CodexManifest, error) // CID management methods HasCid(cid string) (bool, error) diff --git a/communities/codex_index_downloader_test.go b/communities/codex_index_downloader_test.go index 3a3e00b..e7dcdf6 100644 --- a/communities/codex_index_downloader_test.go +++ b/communities/codex_index_downloader_test.go @@ -9,7 +9,6 @@ import ( "testing" "time" - "github.com/codex-storage/codex-go-bindings/codex" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -80,7 +79,7 @@ func (suite *CodexIndexDownloaderTestSuite) TestGotManifest_SuccessClosesChannel filePath := filepath.Join(suite.testDir, "index.bin") // Setup mock to return a successful manifest - expectedManifest := codex.Manifest{ + expectedManifest := communities.CodexManifest{ Cid: testCid, } expectedManifest.DatasetSize = 1024 @@ -120,7 +119,7 @@ func (suite *CodexIndexDownloaderTestSuite) TestGotManifest_ErrorDoesNotCloseCha // Setup mock to return an error suite.mockClient.EXPECT(). FetchManifestWithContext(gomock.Any(), testCid). - Return(codex.Manifest{}, errors.New("fetch error")) + Return(communities.CodexManifest{}, errors.New("fetch error")) // Create downloader downloader := communities.NewCodexIndexDownloader(suite.mockClient, testCid, filePath, suite.cancelChan, suite.logger) @@ -155,7 +154,7 @@ func (suite *CodexIndexDownloaderTestSuite) TestGotManifest_CidMismatchDoesNotCl filePath := filepath.Join(suite.testDir, "index.bin") // Setup mock to return a manifest with different CID - mismatchedManifest := codex.Manifest{ + mismatchedManifest := communities.CodexManifest{ Cid: differentCid, // Different CID! } mismatchedManifest.DatasetSize = 1024 @@ -199,12 +198,12 @@ func (suite *CodexIndexDownloaderTestSuite) TestGotManifest_Cancellation() { fetchCalled := make(chan struct{}) suite.mockClient.EXPECT(). FetchManifestWithContext(gomock.Any(), testCid). - DoAndReturn(func(ctx context.Context, cid string) (codex.Manifest, error) { + DoAndReturn(func(ctx context.Context, cid string) (communities.CodexManifest, error) { close(fetchCalled) // Signal that fetch was called // Wait for context cancellation <-ctx.Done() - return codex.Manifest{}, ctx.Err() + return communities.CodexManifest{}, ctx.Err() }) // Create downloader @@ -251,7 +250,7 @@ func (suite *CodexIndexDownloaderTestSuite) TestGotManifest_RecordsDatasetSize() expectedSize := int64(2048) // Setup mock to return a manifest with specific dataset size - expectedManifest := codex.Manifest{ + expectedManifest := communities.CodexManifest{ Cid: testCid, } expectedManifest.DatasetSize = int(expectedSize) @@ -504,7 +503,7 @@ func (suite *CodexIndexDownloaderTestSuite) TestLength_ReturnsDatasetSize() { expectedSize := 4096 // Setup mock to return a manifest - expectedManifest := codex.Manifest{ + expectedManifest := communities.CodexManifest{ Cid: testCid, } expectedManifest.DatasetSize = expectedSize diff --git a/communities/mock/codex_client_interface.go b/communities/mock/codex_client_interface.go index c7db270..2a3ae82 100644 --- a/communities/mock/codex_client_interface.go +++ b/communities/mock/codex_client_interface.go @@ -11,11 +11,11 @@ package mock_communities import ( context "context" + "go-codex-client/communities" io "io" reflect "reflect" time "time" - "github.com/codex-storage/codex-go-bindings/codex" gomock "go.uber.org/mock/gomock" ) @@ -72,10 +72,10 @@ func (mr *MockCodexClientInterfaceMockRecorder) DownloadWithContext(ctx, cid, ou } // FetchManifestWithContext mocks base method. -func (m *MockCodexClientInterface) FetchManifestWithContext(ctx context.Context, cid string) (codex.Manifest, error) { +func (m *MockCodexClientInterface) FetchManifestWithContext(ctx context.Context, cid string) (communities.CodexManifest, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FetchManifestWithContext", ctx, cid) - ret0, _ := ret[0].(codex.Manifest) + ret0, _ := ret[0].(communities.CodexManifest) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -156,10 +156,10 @@ func (mr *MockCodexClientInterfaceMockRecorder) SetRequestTimeout(timeout any) * } // TriggerDownload mocks base method. -func (m *MockCodexClientInterface) TriggerDownload(cid string) (codex.Manifest, error) { +func (m *MockCodexClientInterface) TriggerDownload(cid string) (communities.CodexManifest, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "TriggerDownload", cid) - ret0, _ := ret[0].(codex.Manifest) + ret0, _ := ret[0].(communities.CodexManifest) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -171,10 +171,10 @@ func (mr *MockCodexClientInterfaceMockRecorder) TriggerDownload(cid any) *gomock } // TriggerDownloadWithContext mocks base method. -func (m *MockCodexClientInterface) TriggerDownloadWithContext(ctx context.Context, cid string) (codex.Manifest, error) { +func (m *MockCodexClientInterface) TriggerDownloadWithContext(ctx context.Context, cid string) (communities.CodexManifest, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "TriggerDownloadWithContext", ctx, cid) - ret0, _ := ret[0].(codex.Manifest) + ret0, _ := ret[0].(communities.CodexManifest) ret1, _ := ret[1].(error) return ret0, ret1 }