Repository navigation
Expand file tree
/
Copy pathmain_web_test.go
More file actions
106 lines (101 loc) · 3.48 KB
/
Copy pathmain_web_test.go
File metadata and controls
106 lines (101 loc) · 3.48 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
package main
import (
"bytes"
"context"
"errors"
"io"
"os"
"strings"
"testing"
"time"
"github.com/merefield/codexometer/internal/codex"
"github.com/merefield/codexometer/internal/ui"
"github.com/merefield/codexometer/internal/web"
)
func TestLaunchModesAreIsolated(t *testing.T) {
for _, webMode := range []bool{false, true} {
t.Run(map[bool]string{false: "terminal unchanged", true: "web read only"}[webMode], func(t *testing.T) {
t.Setenv("DIGBENCH_API_TOKEN", "private-digbench")
t.Setenv("OPENAI_API_KEY", "private-key")
terminal, browser, discovery := 0, 0, 0
deps := dependencies{
startUI: func(_ ui.Fetcher, refresh time.Duration, inline bool, threshold, hours int) error {
terminal++
if refresh != time.Minute || inline || threshold != 80 || hours != 72 {
t.Fatal("terminal defaults changed")
}
return nil
},
startWeb: func(source web.Source, refresh time.Duration, port int, output io.Writer, control bool) error {
browser++
if refresh != time.Minute || port != 54321 || control {
t.Fatal("web options missing")
}
client, ok := source.(codex.Client)
if !ok || client.BenchmarkAPIKey != "" || client.DigBenchToken != "" {
t.Fatal("web received action credentials")
}
if os.Getenv("OPENAI_API_KEY") != "" || os.Getenv("DIGBENCH_API_TOKEN") != "" {
t.Fatal("child credentials not scrubbed")
}
return nil
},
listDigBenchGames: func(context.Context, string) ([]string, error) { discovery++; return nil, nil },
}
args := []string{}
if webMode {
args = []string{"--web", "--web-port", "54321"}
}
var out, err bytes.Buffer
if code := run(args, &out, &err, deps); code != 0 {
t.Fatalf("%d %s", code, &err)
}
if webMode {
if browser != 1 || terminal != 0 || discovery != 0 {
t.Fatal("web invoked terminal/action discovery")
}
} else if terminal != 1 || browser != 0 || discovery != 1 {
t.Fatal("normal launch changed")
}
})
}
}
func TestWebFlagsAndErrors(t *testing.T) {
var flagOut, flagErr bytes.Buffer
if code := run([]string{"--web-control"}, &flagOut, &flagErr, dependencies{}); code != 2 {
t.Fatal("control allowed without web")
}
for _, args := range [][]string{{"--web-port", "1"}, {"--web", "--web-port", "-1"}, {"--web", "--web-port", "65536"}, {"--web", "--inline"}, {"--web", "--check-auth"}, {"--web", "--digbench-game", "x"}} {
var out, err bytes.Buffer
if code := run(args, &out, &err, dependencies{}); code != 2 {
t.Fatalf("%v: %d %s", args, code, &err)
}
}
var out, err bytes.Buffer
deps := dependencies{startWeb: func(source web.Source, _ time.Duration, _ int, _ io.Writer, _ bool) error {
if _, ok := source.(*demoFetcher); !ok {
t.Fatal("web demo not isolated")
}
return errors.New("listen failed")
}}
if code := run([]string{"--web", "--demo"}, &out, &err, deps); code != 1 || !strings.Contains(err.String(), "listen failed") {
t.Fatalf("%d %s", code, &err)
}
out.Reset()
err.Reset()
if code := run([]string{"--web", "--version"}, &out, &err, dependencies{}); code != 0 {
t.Fatal("version started a server")
}
}
func TestWebControlExplicitOptIn(t *testing.T) {
var out, err bytes.Buffer
deps := dependencies{startWeb: func(_ web.Source, _ time.Duration, _ int, _ io.Writer, control bool) error {
if !control {
t.Fatal("explicit control flag lost")
}
return nil
}}
if code := run([]string{"--web", "--web-control", "--demo"}, &out, &err, deps); code != 0 {
t.Fatalf("%d %s", code, &err)
}
}