package api

import (
	"testing"

	. "github.com/smartystreets/goconvey/convey"
	"gitlink.org.cn/cloudream/common/pkgs/types"
)

func Test_Presigned(t *testing.T) {
	cli := NewClient(&Config{
		URL:       "http://localhost:7890",
		AccessKey: "123456",
		SecretKey: "123456",
	})

	Convey("下载文件", t, func() {
		pre := cli.Presigned()
		url, err := pre.ObjectDownloadByPath(PresignedObjectDownloadByPath{
			PackageID: 3,
			Path:      "example.java",
			Offset:    1,
			Length:    types.Ref(int64(100)),
		}, 100)
		So(err, ShouldEqual, nil)
		t.Logf("url: %s", url)
	})

	Convey("上传文件", t, func() {
		pre := cli.Presigned()
		url, err := pre.ObjectUpload(PresignedObjectUpload{
			PackageID: 3,
			Path:      "example.java",
		}, 100)
		So(err, ShouldEqual, nil)
		t.Logf("url: %s", url)
	})
}

func Test_PresignedObjectListByPath(t *testing.T) {
	cli := NewClient(&Config{
		URL:       "http://localhost:7890",
		AccessKey: "123456",
		SecretKey: "123456",
	})

	Convey("下载文件", t, func() {
		pre := cli.Presigned()
		url, err := pre.ObjectListByPath(PresignedObjectListByPath{
			ObjectListByPath: ObjectListByPath{
				PackageID:         12,
				Path:              "a/",
				IsPrefix:          true,
				NoRecursive:       true,
				MaxKeys:           10,
				ContinuationToken: "123456",
			},
		}, 100)
		So(err, ShouldEqual, nil)
		t.Logf("url: %s", url)
	})
}

func Test_PresignedObjectDownloadByPath(t *testing.T) {
	cli := NewClient(&Config{
		URL:       "http://localhost:7890",
		AccessKey: "123456",
		SecretKey: "123456",
	})

	Convey("下载文件", t, func() {
		pre := cli.Presigned()
		url, err := pre.ObjectDownloadByPath(PresignedObjectDownloadByPath{
			PackageID: 3,
			Path:      "example.java",
			// Offset:    1,
			// Length:    types.Ref(int64(100)),
		}, 100)
		So(err, ShouldEqual, nil)
		t.Logf("url: %s", url)
	})
}

func Test_PresignedObjectDownload(t *testing.T) {
	cli := NewClient(&Config{
		URL:       "http://localhost:7890",
		AccessKey: "123456",
		SecretKey: "123456",
	})

	Convey("下载文件", t, func() {
		pre := cli.Presigned()
		url, err := pre.ObjectDownload(PresignedObjectDownload{
			ObjectID: 1039,
			// Offset:    1,
			// Length:    types.Ref(int64(100)),
		}, 100)
		So(err, ShouldEqual, nil)
		t.Logf("url: %s", url)
	})
}

func Test_PresignedObjectUpload(t *testing.T) {
	cli := NewClient(&Config{
		URL: "http://localhost:7890",
	})

	Convey("上传文件", t, func() {
		pre := cli.Presigned()
		url, err := pre.ObjectUpload(PresignedObjectUpload{
			PackageID: 3,
			Path:      "example.java",
		}, 100)
		So(err, ShouldEqual, nil)
		t.Logf("url: %s", url)
	})
}

func Test_PresignedNewMultipartUpload(t *testing.T) {
	cli := NewClient(&Config{
		URL: "http://localhost:7890",
	})

	Convey("启动分片上传", t, func() {
		pre := cli.Presigned()
		url, err := pre.ObjectNewMultipartUpload(PresignedObjectNewMultipartUpload{
			PackageID: 3,
			Path:      "example.java",
		}, 600)
		So(err, ShouldEqual, nil)
		t.Logf("url: %s", url)
	})
}

func Test_PresignedObjectUploadPart(t *testing.T) {
	cli := NewClient(&Config{
		URL:       "http://localhost:7890",
		AccessKey: "123456",
		SecretKey: "123456",
	})

	Convey("上传分片", t, func() {
		pre := cli.Presigned()
		url, err := pre.ObjectUploadPart(PresignedObjectUploadPart{
			ObjectID: 7,
			Index:    3,
		}, 600)
		So(err, ShouldEqual, nil)
		t.Logf("url: %s", url)
	})
}

func Test_PresignedCompleteMultipartUpload(t *testing.T) {
	cli := NewClient(&Config{
		URL:       "http://localhost:7890",
		AccessKey: "123456",
		SecretKey: "123456",
	})

	Convey("合并分片", t, func() {
		pre := cli.Presigned()
		url, err := pre.ObjectCompleteMultipartUpload(PresignedObjectCompleteMultipartUpload{
			ObjectID: 7,
			Indexes:  []int{1, 2, 3},
		}, 600)
		So(err, ShouldEqual, nil)
		t.Logf("url: %s", url)
	})
}
