-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmetaai_test.go
More file actions
115 lines (106 loc) Β· 3.07 KB
/
Copy pathmetaai_test.go
File metadata and controls
115 lines (106 loc) Β· 3.07 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
package metaai
// These tests lock down cookie loading, cookie-header formatting,
// offline-threading-id shape, challenge detection, and access-token parsing.
import (
"strings"
"testing"
)
func TestLoadCookiesFromEnv(t *testing.T) {
t.Setenv("META_AI_DATR", "test_datr")
t.Setenv("META_AI_ABRA_SESS", "test_abra_sess")
t.Setenv("META_AI_ECTO_1_SESS", "test_ecto")
t.Setenv("META_AI_DPR", "1.25")
cookies := loadCookiesFromEnv()
if cookies["datr"] != "test_datr" {
t.Errorf("datr = %q", cookies["datr"])
}
if cookies["abra_sess"] != "test_abra_sess" {
t.Errorf("abra_sess = %q", cookies["abra_sess"])
}
if cookies["ecto_1_sess"] != "test_ecto" {
t.Errorf("ecto_1_sess = %q", cookies["ecto_1_sess"])
}
if cookies["dpr"] != "1.25" {
t.Errorf("dpr = %q", cookies["dpr"])
}
}
func TestLoadCookiesFromEnvRequiresDatr(t *testing.T) {
// Clear datr β nil.
t.Setenv("META_AI_DATR", "")
t.Setenv("META_AI_ABRA_SESS", "x")
if got := loadCookiesFromEnv(); got != nil {
t.Errorf("expected nil when DATR absent, got %v", got)
}
}
func TestCookieHeader(t *testing.T) {
h := cookieHeader(map[string]string{
"datr": "test_datr",
"abra_sess": "test_abra_sess",
})
if !strings.Contains(h, "datr=test_datr") {
t.Errorf("missing datr pair: %q", h)
}
if !strings.Contains(h, "abra_sess=test_abra_sess") {
t.Errorf("missing abra_sess pair: %q", h)
}
if !strings.Contains(h, "; ") {
t.Errorf("pairs not '; '-separated: %q", h)
}
}
func TestGenerateOfflineThreadingID(t *testing.T) {
id := generateOfflineThreadingID()
if id == "" {
t.Fatal("empty id")
}
// Should be a numeric string.
for _, r := range id {
if r < '0' || r > '9' {
t.Errorf("non-digit in id: %q", id)
break
}
}
}
func TestDetectChallengePage(t *testing.T) {
cases := []struct {
html string
want string
}{
{"<html>normal</html>", ""},
{"<html>executeChallenge fetch('/__rd_verify/abc123')</html>", "/__rd_verify/abc123"},
{"<html>__rd_verify present but no fetch</html>", ""},
}
for _, tc := range cases {
if got := detectChallengePage(tc.html); got != tc.want {
t.Errorf("detectChallengePage(%q) = %q, want %q", tc.html, got, tc.want)
}
}
}
func TestExtractAccessToken(t *testing.T) {
html := `<script>window.__x={\"accessToken\":\"ecto1:ABCdef123_-456XYZ\"}</script>`
got := extractAccessToken(html)
if got != "ecto1:ABCdef123_-456XYZ" {
t.Errorf("extractAccessToken = %q", got)
}
if got := extractAccessToken("<html>no token</html>"); got != "" {
t.Errorf("expected empty, got %q", got)
}
}
func TestNewClientConfigPriority(t *testing.T) {
t.Setenv("META_AI_DATR", "")
t.Setenv("META_AI_DEFAULT_THINKING", "true")
t.Setenv("META_AI_DEFAULT_INSTANT", "true")
c, err := NewClient(WithCookies(map[string]string{"datr": "d"}))
if err != nil {
t.Fatal(err)
}
// Both thinking and instant from env β mutual exclusion forces thinking off.
if c.cfg.DefaultThinking {
t.Error("DefaultThinking should be false when both set")
}
if !c.cfg.DefaultInstant {
t.Error("DefaultInstant should be true")
}
if !c.IsAuthed() {
t.Error("cookies present β should be authed")
}
}