diff --git a/codex/codex_test.go b/codex/codex_test.go index ae973c2..d0cb91f 100644 --- a/codex/codex_test.go +++ b/codex/codex_test.go @@ -3,8 +3,7 @@ package codex import "testing" func TestCodexVersion(t *testing.T) { - start := false - node := newCodexNode(t, start) + node := newCodexNode(t, withNoStart()) version, err := node.Version() if err != nil { @@ -18,8 +17,7 @@ func TestCodexVersion(t *testing.T) { } func TestCodexRevision(t *testing.T) { - start := false - node := newCodexNode(t, start) + node := newCodexNode(t, withNoStart()) revision, err := node.Revision() if err != nil { @@ -33,8 +31,7 @@ func TestCodexRevision(t *testing.T) { } func TestCodexRepo(t *testing.T) { - start := true - node := newCodexNode(t, start) + node := newCodexNode(t) repo, err := node.Repo() if err != nil { @@ -48,8 +45,7 @@ func TestCodexRepo(t *testing.T) { } func TestSpr(t *testing.T) { - start := true - node := newCodexNode(t, start) + node := newCodexNode(t) spr, err := node.Spr() if err != nil { @@ -63,8 +59,7 @@ func TestSpr(t *testing.T) { } func TestPeerId(t *testing.T) { - start := true - node := newCodexNode(t, start) + node := newCodexNode(t) peerId, err := node.PeerId() if err != nil { diff --git a/codex/debug_test.go b/codex/debug_test.go index af9d5af..9355347 100644 --- a/codex/debug_test.go +++ b/codex/debug_test.go @@ -8,8 +8,7 @@ import ( ) func TestDebug(t *testing.T) { - start := true - codex := newCodexNode(t, start) + codex := newCodexNode(t) info, err := codex.Debug() if err != nil { diff --git a/codex/download_test.go b/codex/download_test.go index 0827da3..19c2855 100644 --- a/codex/download_test.go +++ b/codex/download_test.go @@ -1,28 +1,13 @@ package codex import ( - "bytes" "os" "strings" "testing" ) -func uploadHelper(t *testing.T, codex *CodexNode) (string, int) { - t.Helper() - - buf := bytes.NewBuffer([]byte("Hello World!")) - len := buf.Len() - cid, err := codex.UploadReader(UploadOptions{filepath: "hello.txt"}, buf) - if err != nil { - t.Fatalf("Error happened during upload: %v\n", err) - } - - return cid, len -} - func TestDownloadStream(t *testing.T) { - start := true - codex := newCodexNode(t, start) + codex := newCodexNode(t) cid, len := uploadHelper(t, codex) f, err := os.Create("testdata/hello.downloaded.txt") @@ -59,7 +44,7 @@ func TestDownloadStream(t *testing.T) { t.Fatalf("UploadReader progress callback total bytes %d but expected %d", totalBytes, len) } - data, err := os.ReadFile("testdata/hello.writer.txt") + data, err := os.ReadFile("testdata/hello.downloaded.writer.txt") if err != nil { t.Fatal(err) } @@ -70,8 +55,7 @@ func TestDownloadStream(t *testing.T) { } func TestDownloadStreamWithAutosize(t *testing.T) { - start := true - codex := newCodexNode(t, start) + codex := newCodexNode(t) cid, len := uploadHelper(t, codex) totalBytes := 0 @@ -101,9 +85,17 @@ func TestDownloadStreamWithAutosize(t *testing.T) { } } +func TestDownloadStreamWithNotExisting(t *testing.T) { + codex := newCodexNode(t, withBlockRetries(1)) + + opt := DownloadStreamOptions{} + if err := codex.DownloadStream("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku", opt); err == nil { + t.Fatal("Error expected when downloading non-existing cid") + } +} + func TestDownloadManual(t *testing.T) { - start := true - codex := newCodexNode(t, start) + codex := newCodexNode(t) cid, _ := uploadHelper(t, codex) if err := codex.DownloadInit(cid, DownloadInitOptions{}); err != nil { @@ -126,3 +118,38 @@ func TestDownloadManual(t *testing.T) { t.Fatalf("Error when cancelling the download %s", err) } } + +func TestDownloadManifest(t *testing.T) { + codex := newCodexNode(t) + cid, _ := uploadHelper(t, codex) + + manifest, err := codex.DownloadManifest(cid) + if err != nil { + t.Fatal("Error when downloading manifest:", err) + } + + if manifest.Cid != cid { + t.Errorf("expected cid %q, got %q", cid, manifest.Cid) + } +} + +func TestDownloadManifestWithNotExistingCid(t *testing.T) { + codex := newCodexNode(t, withBlockRetries(1)) + + manifest, err := codex.DownloadManifest("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku") + if err == nil { + t.Fatal("Error when downloading manifest:", err) + } + + if manifest.Cid != "" { + t.Errorf("expected empty cid, got %q", manifest.Cid) + } +} + +func TestDownloadInitWithNotExistingCid(t *testing.T) { + codex := newCodexNode(t, withBlockRetries(1)) + + if err := codex.DownloadInit("bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku", DownloadInitOptions{}); err == nil { + t.Fatal("expected error when initializing download for non-existent cid") + } +} diff --git a/codex/testutil.go b/codex/testutil.go index 69d4b73..3155ace 100644 --- a/codex/testutil.go +++ b/codex/testutil.go @@ -1,20 +1,44 @@ package codex import ( + "bytes" "testing" ) -func newCodexNode(t *testing.T, start bool) *CodexNode { +type codexNodeTestOption func(*codexNodeTestOptions) + +type codexNodeTestOptions struct { + noStart bool + blockRetries int +} + +func withNoStart() codexNodeTestOption { + return func(o *codexNodeTestOptions) { o.noStart = true } +} + +func withBlockRetries(n int) codexNodeTestOption { + return func(o *codexNodeTestOptions) { o.blockRetries = n } +} + +func newCodexNode(t *testing.T, opts ...codexNodeTestOption) *CodexNode { + o := codexNodeTestOptions{ + blockRetries: 3000, + } + for _, opt := range opts { + opt(&o) + } + node, err := CodexNew(CodexConfig{ DataDir: t.TempDir(), LogFormat: LogFormatNoColors, MetricsEnabled: false, + BlockRetries: o.blockRetries, }) if err != nil { t.Fatalf("Failed to create Codex node: %v", err) } - if start { + if !o.noStart { err = node.Start() if err != nil { t.Fatalf("Failed to start Codex node: %v", err) @@ -22,7 +46,7 @@ func newCodexNode(t *testing.T, start bool) *CodexNode { } t.Cleanup(func() { - if start { + if !o.noStart { if err := node.Stop(); err != nil { t.Logf("cleanup codex: %v", err) } @@ -35,3 +59,16 @@ func newCodexNode(t *testing.T, start bool) *CodexNode { return node } + +func uploadHelper(t *testing.T, codex *CodexNode) (string, int) { + t.Helper() + + buf := bytes.NewBuffer([]byte("Hello World!")) + len := buf.Len() + cid, err := codex.UploadReader(UploadOptions{filepath: "hello.txt"}, buf) + if err != nil { + t.Fatalf("Error happened during upload: %v\n", err) + } + + return cid, len +} diff --git a/codex/upload_test.go b/codex/upload_test.go index 6e6657a..af04371 100644 --- a/codex/upload_test.go +++ b/codex/upload_test.go @@ -10,8 +10,7 @@ import ( const expectedCID = "zDvZRwzmAkhzDRPH5EW242gJBNZ2T7aoH2v1fVH66FxXL4kSbvyM" func TestUploadReader(t *testing.T) { - start := true - codex := newCodexNode(t, start) + codex := newCodexNode(t) totalBytes := 0 finalPercent := 0.0 @@ -44,8 +43,7 @@ func TestUploadReader(t *testing.T) { } func TestUploadFile(t *testing.T) { - start := true - codex := newCodexNode(t, start) + codex := newCodexNode(t) totalBytes := 0 finalPercent := 0.0 @@ -82,8 +80,7 @@ func TestUploadFile(t *testing.T) { } func TestUploadFileNoProgress(t *testing.T) { - start := true - codex := newCodexNode(t, start) + codex := newCodexNode(t) options := UploadOptions{filepath: "./testdata/doesnt_exist.txt"} @@ -98,8 +95,7 @@ func TestUploadFileNoProgress(t *testing.T) { } func TestManualUpload(t *testing.T) { - start := true - codex := newCodexNode(t, start) + codex := newCodexNode(t) sessionId, err := codex.UploadInit(&UploadOptions{filepath: "hello.txt"}) if err != nil {