-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
174 lines (166 loc) · 5.43 KB
/
main_test.go
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
package main
import (
"bytes"
"context"
"encoding/json"
"github.com/Crocmagnon/paperless-alfred-go/internal/alfred"
"github.com/Crocmagnon/paperless-alfred-go/internal/paperless"
"github.com/jarcoal/httpmock"
"gotest.tools/v3/assert"
"net/http"
"testing"
)
func Test_run(t *testing.T) {
const fakePaperlessHost = "http://localhost:1234"
t.Parallel()
type args struct {
args []string
httpClient func() *http.Client
}
tests := []struct {
name string
args args
wantStdout alfred.Result
wantErr bool
}{
{
"simple query",
args{
[]string{fakePaperlessHost, "1234", "simple"},
func() *http.Client {
transport := httpmock.NewMockTransport()
transport.RegisterResponder(
http.MethodGet, "/api/documents/?query=simple",
httpmock.NewJsonResponderOrPanic(http.StatusOK, paperless.PageResponse[paperless.DocumentSearch]{
Results: []paperless.DocumentSearch{
{
Id: 12,
Correspondent: nil,
DocumentType: nil,
Title: "Simple doc",
CreatedDate: "2024-10-12",
ArchiveSerialNumber: nil,
},
},
}))
transport.RegisterResponder(http.MethodGet, "/api/correspondents/", httpmock.NewStringResponder(http.StatusOK, "{}"))
transport.RegisterResponder(http.MethodGet, "/api/document_types/", httpmock.NewStringResponder(http.StatusOK, "{}"))
return &http.Client{Transport: transport}
},
},
alfred.Result{
Items: []alfred.Item{
{
UID: "http://localhost:1234/documents/12/details",
Title: "Simple doc",
Subtitle: "2024-10-12",
Arg: "http://localhost:1234/documents/12/details",
Icon: &alfred.Icon{Type: "filetype", Path: "com.adobe.pdf"},
Mods: map[string]alfred.Mod{
"cmd": {Arg: "http://localhost:1234/documents?query=simple", Subtitle: `Open search for "simple" in Paperless`},
},
},
},
},
false,
},
{
"two docs, one with correspondent, doc type and ASN",
args{
[]string{fakePaperlessHost, "1234", "complex querÿ"},
func() *http.Client {
corID := 1
docTypeID := 2
asn := 123
transport := httpmock.NewMockTransport()
transport.RegisterResponder(
http.MethodGet, "/api/documents/?query=complex+quer%C3%BF",
httpmock.NewJsonResponderOrPanic(http.StatusOK, paperless.PageResponse[paperless.DocumentSearch]{
Results: []paperless.DocumentSearch{
{
Id: 11,
Correspondent: &corID,
DocumentType: &docTypeID,
Title: "Complete doc",
CreatedDate: "2024-10-11",
ArchiveSerialNumber: &asn,
},
{
Id: 12,
Correspondent: nil,
DocumentType: nil,
Title: "Simple doc",
CreatedDate: "2024-10-12",
ArchiveSerialNumber: nil,
},
},
}))
nextPage := fakePaperlessHost + "/api/correspondents/?page=2"
transport.RegisterResponder(http.MethodGet, "/api/correspondents/",
httpmock.NewJsonResponderOrPanic(http.StatusOK, paperless.PageResponse[paperless.Correspondent]{
PaginationEnvelope: paperless.PaginationEnvelope{
Next: &nextPage,
},
Results: nil,
}),
)
transport.RegisterResponder(http.MethodGet, "/api/correspondents/?page=2",
httpmock.NewJsonResponderOrPanic(http.StatusOK, paperless.PageResponse[paperless.Correspondent]{
Results: []paperless.Correspondent{
{Id: 1, Name: "Fake corresp."},
},
}),
)
transport.RegisterResponder(http.MethodGet, "/api/document_types/",
httpmock.NewJsonResponderOrPanic(http.StatusOK, paperless.PageResponse[paperless.DocumentType]{
Results: []paperless.DocumentType{
{Id: 2, Name: "Doc type"},
},
}))
return &http.Client{Transport: transport}
},
},
alfred.Result{
Items: []alfred.Item{
{
UID: "http://localhost:1234/documents/11/details",
Title: "Complete doc",
Subtitle: "2024-10-11 - Doc type - Fake corresp. - ASN 123",
Arg: "http://localhost:1234/documents/11/details",
Icon: &alfred.Icon{Type: "filetype", Path: "com.adobe.pdf"},
Mods: map[string]alfred.Mod{
"cmd": {Arg: "http://localhost:1234/documents?query=complex+quer%C3%BF", Subtitle: `Open search for "complex querÿ" in Paperless`},
},
},
{
UID: "http://localhost:1234/documents/12/details",
Title: "Simple doc",
Subtitle: "2024-10-12",
Arg: "http://localhost:1234/documents/12/details",
Icon: &alfred.Icon{Type: "filetype", Path: "com.adobe.pdf"},
Mods: map[string]alfred.Mod{
"cmd": {Arg: "http://localhost:1234/documents?query=complex+quer%C3%BF", Subtitle: `Open search for "complex querÿ" in Paperless`},
},
},
},
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
stdout := &bytes.Buffer{}
ctx := context.Background()
err := run(ctx, tt.args.args, stdout, tt.args.httpClient())
if (err != nil) != tt.wantErr {
t.Errorf("run() error = %v, wantErr %v", err, tt.wantErr)
return
}
gotStdout := alfred.Result{}
err = json.Unmarshal(stdout.Bytes(), &gotStdout)
assert.NilError(t, err)
assert.DeepEqual(t, tt.wantStdout, gotStdout)
})
}
}