-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathai_test.go
More file actions
81 lines (74 loc) · 2.32 KB
/
Copy pathai_test.go
File metadata and controls
81 lines (74 loc) · 2.32 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
package subtitles
import (
"encoding/json"
"strings"
"testing"
)
func TestAIPayload(t *testing.T) {
for _, tc := range []struct {
name, body, want string
}{
{"bare object", `{"request_id":"req_1"}`, `{"request_id":"req_1"}`},
{"wrapped in data", `{"status":true,"data":{"request_id":"req_1"}}`, `{"request_id":"req_1"}`},
{"wrapped in result", `{"result":{"request_id":"req_1"}}`, `{"request_id":"req_1"}`},
{"top-level array", `[{"id":"a"}]`, `[{"id":"a"}]`},
{"leading space, array", " \n[{\"id\":\"a\"}]", `[{"id":"a"}]`},
{"null data", `{"request_id":"req_1","data":null}`, `{"request_id":"req_1","data":null}`},
} {
got, err := aiPayload([]byte(tc.body))
if err != nil {
t.Errorf("%s: %v", tc.name, err)
continue
}
if string(got) != tc.want {
t.Errorf("%s: got %s, want %s", tc.name, got, tc.want)
}
}
// a refusal with a 200 still has to be an error, and keep the server's
// message (only clue that a pro endpoint got hit with a free key)
for _, body := range []string{
`{"status":false,"error":"subscription required"}`,
`{"success":false,"message":"subscription required"}`,
} {
_, err := aiPayload([]byte(body))
if err == nil {
t.Fatalf("%s: no error", body)
}
if !strings.Contains(err.Error(), "subscription required") {
t.Errorf("%s: lost the server message: %v", body, err)
}
}
if _, err := aiPayload([]byte("<html>gateway timeout</html>")); err == nil {
t.Error("non-JSON body accepted")
}
}
func TestJobRef(t *testing.T) {
if got := (Job{RequestID: "req_1"}).Ref(); got != "req_1" {
t.Errorf("request_id: %q", got)
}
if got := (Job{ID: "id_1"}).Ref(); got != "id_1" {
t.Errorf("id: %q", got)
}
}
func TestFilenameMatchQuery(t *testing.T) {
var m FilenameMatch
json.Unmarshal([]byte(`{"type":"tv","tmdb_id":1396,"title":"Breaking Bad","season":1,"episode":2}`), &m)
q := m.Query("ar")
if q.TMDBID != 1396 || q.Lang != "ar" || !q.isEpisode() {
t.Errorf("got %+v", q)
}
}
// real values from api.subdl.com
func TestPageToken(t *testing.T) {
for page, want := range map[string]string{
"/s/info/JFF78dtmp2": "JFF78dtmp2",
"/s/info/3PuqEgKNz8a": "3PuqEgKNz8a",
"/s/info/abc/": "abc",
"bare": "bare",
"": "",
} {
if got := pageToken(page); got != want {
t.Errorf("pageToken(%q) = %q, want %q", page, got, want)
}
}
}