-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathextensions_test.go
More file actions
192 lines (171 loc) · 8.29 KB
/
Copy pathextensions_test.go
File metadata and controls
192 lines (171 loc) · 8.29 KB
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
package cmd
import (
"archive/zip"
"bytes"
"context"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/kernel/kernel-go-sdk"
"github.com/kernel/kernel-go-sdk/option"
"github.com/kernel/kernel-go-sdk/packages/pagination"
"github.com/stretchr/testify/assert"
)
// FakeExtensionsService implements ExtensionsService
type FakeExtensionsService struct {
ListFunc func(ctx context.Context, query kernel.ExtensionListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ExtensionListResponse], error)
DeleteFunc func(ctx context.Context, idOrName string, opts ...option.RequestOption) error
DownloadFunc func(ctx context.Context, idOrName string, opts ...option.RequestOption) (*http.Response, error)
DownloadFromChromeStoreFn func(ctx context.Context, query kernel.ExtensionDownloadFromChromeStoreParams, opts ...option.RequestOption) (*http.Response, error)
UploadFunc func(ctx context.Context, body kernel.ExtensionUploadParams, opts ...option.RequestOption) (*kernel.ExtensionUploadResponse, error)
}
func (f *FakeExtensionsService) List(ctx context.Context, query kernel.ExtensionListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ExtensionListResponse], error) {
if f.ListFunc != nil {
return f.ListFunc(ctx, query, opts...)
}
return &pagination.OffsetPagination[kernel.ExtensionListResponse]{}, nil
}
func (f *FakeExtensionsService) Delete(ctx context.Context, idOrName string, opts ...option.RequestOption) error {
if f.DeleteFunc != nil {
return f.DeleteFunc(ctx, idOrName, opts...)
}
return nil
}
func (f *FakeExtensionsService) Download(ctx context.Context, idOrName string, opts ...option.RequestOption) (*http.Response, error) {
if f.DownloadFunc != nil {
return f.DownloadFunc(ctx, idOrName, opts...)
}
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader("")), Header: http.Header{}}, nil
}
func (f *FakeExtensionsService) DownloadFromChromeStore(ctx context.Context, query kernel.ExtensionDownloadFromChromeStoreParams, opts ...option.RequestOption) (*http.Response, error) {
if f.DownloadFromChromeStoreFn != nil {
return f.DownloadFromChromeStoreFn(ctx, query, opts...)
}
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader("")), Header: http.Header{}}, nil
}
func (f *FakeExtensionsService) Upload(ctx context.Context, body kernel.ExtensionUploadParams, opts ...option.RequestOption) (*kernel.ExtensionUploadResponse, error) {
if f.UploadFunc != nil {
return f.UploadFunc(ctx, body, opts...)
}
return &kernel.ExtensionUploadResponse{ID: "e-new", Name: body.Name.Value, CreatedAt: time.Unix(0, 0), SizeBytes: 1}, nil
}
func TestExtensionsList_Empty(t *testing.T) {
buf := capturePtermOutput(t)
fake := &FakeExtensionsService{}
e := ExtensionsCmd{extensions: fake}
_ = e.List(context.Background(), ExtensionsListInput{})
assert.Contains(t, buf.String(), "No extensions found")
}
func TestExtensionsList_WithRows(t *testing.T) {
buf := capturePtermOutput(t)
created := time.Unix(0, 0)
rows := []kernel.ExtensionListResponse{{ID: "e1", Name: "alpha", CreatedAt: created, SizeBytes: 10}, {ID: "e2", Name: "", CreatedAt: created, SizeBytes: 20}}
fake := &FakeExtensionsService{ListFunc: func(ctx context.Context, query kernel.ExtensionListParams, opts ...option.RequestOption) (*pagination.OffsetPagination[kernel.ExtensionListResponse], error) {
return &pagination.OffsetPagination[kernel.ExtensionListResponse]{Items: rows}, nil
}}
e := ExtensionsCmd{extensions: fake}
_ = e.List(context.Background(), ExtensionsListInput{})
out := buf.String()
assert.Contains(t, out, "e1")
assert.Contains(t, out, "alpha")
assert.Contains(t, out, "e2")
}
func TestExtensionsDelete_SkipConfirm(t *testing.T) {
buf := capturePtermOutput(t)
fake := &FakeExtensionsService{}
e := ExtensionsCmd{extensions: fake}
_ = e.Delete(context.Background(), ExtensionsDeleteInput{Identifier: "e1", SkipConfirm: true})
assert.Contains(t, buf.String(), "Deleted extension: e1")
}
func TestExtensionsDelete_NotFound(t *testing.T) {
buf := capturePtermOutput(t)
fake := &FakeExtensionsService{DeleteFunc: func(ctx context.Context, idOrName string, opts ...option.RequestOption) error {
return &kernel.Error{StatusCode: http.StatusNotFound}
}}
e := ExtensionsCmd{extensions: fake}
_ = e.Delete(context.Background(), ExtensionsDeleteInput{Identifier: "missing", SkipConfirm: true})
assert.Contains(t, buf.String(), "not found")
}
func TestExtensionsDownload_MissingOutput(t *testing.T) {
buf := capturePtermOutput(t)
fake := &FakeExtensionsService{DownloadFunc: func(ctx context.Context, idOrName string, opts ...option.RequestOption) (*http.Response, error) {
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader("content")), Header: http.Header{}}, nil
}}
e := ExtensionsCmd{extensions: fake}
_ = e.Download(context.Background(), ExtensionsDownloadInput{Identifier: "e1", Output: ""})
assert.Contains(t, buf.String(), "Missing --to output directory")
}
func TestExtensionsDownload_ExtractsToDir(t *testing.T) {
buf := capturePtermOutput(t)
// Create a small in-memory zip
var zbuf bytes.Buffer
zw := zip.NewWriter(&zbuf)
w, _ := zw.Create("manifest.json")
_, _ = w.Write([]byte("{}"))
_ = zw.Close()
fake := &FakeExtensionsService{DownloadFunc: func(ctx context.Context, idOrName string, opts ...option.RequestOption) (*http.Response, error) {
return &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(zbuf.Bytes())), Header: http.Header{}}, nil
}}
e := ExtensionsCmd{extensions: fake}
outDir := filepath.Join(os.TempDir(), "extdl-test")
_ = os.RemoveAll(outDir)
_ = e.Download(context.Background(), ExtensionsDownloadInput{Identifier: "e1", Output: outDir})
// Ensure extracted
_, statErr := os.Stat(filepath.Join(outDir, "manifest.json"))
assert.NoError(t, statErr)
assert.Contains(t, buf.String(), "Extracted extension to "+outDir)
_ = os.RemoveAll(outDir)
}
func TestExtensionsDownloadWebStore_ExtractsToDir(t *testing.T) {
buf := capturePtermOutput(t)
var zbuf bytes.Buffer
zw := zip.NewWriter(&zbuf)
w, _ := zw.Create("manifest.json")
_, _ = w.Write([]byte("{}"))
_ = zw.Close()
fake := &FakeExtensionsService{DownloadFromChromeStoreFn: func(ctx context.Context, query kernel.ExtensionDownloadFromChromeStoreParams, opts ...option.RequestOption) (*http.Response, error) {
return &http.Response{StatusCode: 200, Body: io.NopCloser(bytes.NewReader(zbuf.Bytes())), Header: http.Header{}}, nil
}}
e := ExtensionsCmd{extensions: fake}
outDir := filepath.Join(os.TempDir(), "webstoredl-test")
_ = os.RemoveAll(outDir)
_ = e.DownloadWebStore(context.Background(), ExtensionsDownloadWebStoreInput{URL: "https://store/link", Output: outDir, OS: "linux"})
_, statErr := os.Stat(filepath.Join(outDir, "manifest.json"))
assert.NoError(t, statErr)
assert.Contains(t, buf.String(), "Extracted extension to "+outDir)
_ = os.RemoveAll(outDir)
}
func TestExtensionsDownloadWebStore_InvalidOS(t *testing.T) {
buf := capturePtermOutput(t)
fake := &FakeExtensionsService{}
e := ExtensionsCmd{extensions: fake}
_ = e.DownloadWebStore(context.Background(), ExtensionsDownloadWebStoreInput{URL: "https://store/link", Output: "x", OS: "freebsd"})
assert.Contains(t, buf.String(), "--os must be one of mac, win, linux")
}
func TestExtensionsUpload_Success(t *testing.T) {
buf := capturePtermOutput(t)
dir := t.TempDir()
// create a sample file inside dir
err := os.WriteFile(filepath.Join(dir, "manifest.json"), []byte("{}"), 0644)
assert.NoError(t, err)
fake := &FakeExtensionsService{UploadFunc: func(ctx context.Context, body kernel.ExtensionUploadParams, opts ...option.RequestOption) (*kernel.ExtensionUploadResponse, error) {
return &kernel.ExtensionUploadResponse{ID: "e1", Name: "myext", CreatedAt: time.Unix(0, 0), SizeBytes: 10}, nil
}}
e := ExtensionsCmd{extensions: fake}
_ = e.Upload(context.Background(), ExtensionsUploadInput{Dir: dir, Name: "myext"})
out := buf.String()
assert.Contains(t, out, "ID")
assert.Contains(t, out, "e1")
assert.Contains(t, out, "Name")
assert.Contains(t, out, "myext")
}
func TestExtensionsUpload_InvalidDir(t *testing.T) {
fake := &FakeExtensionsService{}
e := ExtensionsCmd{extensions: fake}
err := e.Upload(context.Background(), ExtensionsUploadInput{Dir: "/does/not/exist"})
assert.Error(t, err)
}