Repository navigation
Expand file tree
/
Copy pathplugin_cliproxy_test.go
More file actions
132 lines (128 loc) · 4.22 KB
/
Copy pathplugin_cliproxy_test.go
File metadata and controls
132 lines (128 loc) · 4.22 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
package plugin
import (
"context"
"net/url"
"os"
"path/filepath"
"testing"
"time"
)
// 𝕏 on Discord: the Grok plugin's sign-in said "grok login gave no link
// to open". The plugin runs `grok login`, and the host has magpie's proxy
// only as MAGPIE_*_PROXY, so the CLI went out with none where x.ai is
// reached only through one. A program a plugin starts has it, as the
// built-in's `grok login` had it.
func TestPluginCLIHasTheProxy(t *testing.T) {
sandbox(t)
const proxy = "http://127.0.0.1:7890"
t.Setenv("HTTPS_PROXY", proxy)
t.Setenv("HTTP_PROXY", proxy)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/cliproxy/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
a, err := Authorize(ctx, "cliproxy", 0, nil, NewAccount)
if err != nil {
t.Fatal(err)
}
u, err := url.Parse(a.URL)
if err != nil {
t.Fatal(err)
}
for _, k := range []string{"inherited", "copied"} {
if got := u.Query().Get(k); got != proxy {
t.Errorf("a CLI started with the %s environment has HTTPS_PROXY %q, want %q", k, got, proxy)
}
}
}
// larchsis on 𝕏: a plugin's sign-in on Windows 11 said its download
// failed, with a proxy set in magpie and none in the system. The plugin
// downloads in a Node program it starts, and Node's fetch goes past
// HTTPS_PROXY unless NODE_USE_ENV_PROXY=1 says to take it, so the proxy
// the host handed on was never used. It is said with the proxy, and a
// value the plugin set itself is kept.
func TestPluginNodeTakesTheProxy(t *testing.T) {
sandbox(t)
const proxy = "http://127.0.0.1:7892"
t.Setenv("HTTPS_PROXY", proxy)
t.Setenv("HTTP_PROXY", proxy)
t.Setenv("NODE_USE_ENV_PROXY", "") // restored after the test
os.Unsetenv("NODE_USE_ENV_PROXY")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/cliproxy/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
a, err := Authorize(ctx, "cliproxy", 0, nil, NewAccount)
if err != nil {
t.Fatal(err)
}
u, err := url.Parse(a.URL)
if err != nil {
t.Fatal(err)
}
q := u.Query()
for _, k := range []string{"inheritedNode", "copiedNode"} {
if got := q.Get(k); got != "1" {
t.Errorf("%s: a program the plugin starts with the proxy has NODE_USE_ENV_PROXY %q, want 1", k, got)
}
}
if got := q.Get("ownNode"); got != "0" {
t.Errorf("the plugin's own NODE_USE_ENV_PROXY=0 became %q", got)
}
}
// #1363 (iamyhzhao): Grok, moved onto its plugin, read as signed out
// after nearly every restart of the computer, until moved back to the
// built-in and on again. magpie, started at login, started the plugin
// host before the proxy app had set the system's proxy, and the host
// keeps the proxy it was started with: `grok models`, which renews the
// sign-in, went out with none from then on. A host whose proxy is no
// longer magpie's is replaced at the next call.
func TestPluginHostTakesANewProxy(t *testing.T) {
sandbox(t)
prev := proxyLookEvery
proxyLookEvery = 0
t.Cleanup(func() { proxyLookEvery = prev })
// at login: no proxy yet in the environment
for _, k := range []string{"HTTPS_PROXY", "HTTP_PROXY", "ALL_PROXY", "https_proxy", "http_proxy", "all_proxy"} {
t.Setenv(k, "")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/cliproxy/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
ask := func() string {
a, err := Authorize(ctx, "cliproxy", 0, nil, NewAccount)
if err != nil {
t.Fatal(err)
}
u, err := url.Parse(a.URL)
if err != nil {
t.Fatal(err)
}
return u.Query().Get("inherited")
}
before := ask()
// then the proxy app sets one
const proxy = "http://127.0.0.1:7891"
if before == proxy {
t.Fatalf("the host had %s before it was set", proxy)
}
t.Setenv("HTTPS_PROXY", proxy)
t.Setenv("HTTP_PROXY", proxy)
if got := ask(); got != proxy {
t.Errorf("after the proxy was set, a CLI the plugin starts has HTTPS_PROXY %q, want %q", got, proxy)
}
// and a proxy unchanged keeps the host
now := func() *host { hostMu.Lock(); defer hostMu.Unlock(); return current }
h := now()
ask()
if now() != h {
t.Error("the host was replaced with the proxy unchanged")
}
}