-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_test.go
More file actions
209 lines (187 loc) · 6.33 KB
/
app_test.go
File metadata and controls
209 lines (187 loc) · 6.33 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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
wailsapp "github.com/linhay/gettokens/internal/wailsapp"
"github.com/wailsapp/wails/v2/pkg/menu"
)
func TestGitHubRepoUsesPublishedReleaseRepository(t *testing.T) {
if GitHubRepo != "AxApp/GetTokens" {
t.Fatalf("GitHubRepo = %q, want %q", GitHubRepo, "AxApp/GetTokens")
}
}
func TestFetchVendorStatusRSSReturnsBody(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if got := r.Header.Get("Accept"); !strings.Contains(got, "application/rss+xml") {
t.Fatalf("Accept header = %q, want rss accept header", got)
}
if got := r.Header.Get("User-Agent"); got != "GetTokens Vendor Status/1.0" {
t.Fatalf("User-Agent = %q, want %q", got, "GetTokens Vendor Status/1.0")
}
w.Header().Set("Content-Type", "application/rss+xml")
_, _ = w.Write([]byte("<rss><channel><title>OpenAI status</title></channel></rss>"))
}))
defer server.Close()
body, err := (&App{}).FetchVendorStatusRSS(server.URL)
if err != nil {
t.Fatalf("FetchVendorStatusRSS returned error: %v", err)
}
if body != "<rss><channel><title>OpenAI status</title></channel></rss>" {
t.Fatalf("FetchVendorStatusRSS body = %q", body)
}
}
func TestFetchVendorStatusRSSErrorOnNon2xx(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "upstream unavailable", http.StatusBadGateway)
}))
defer server.Close()
_, err := (&App{}).FetchVendorStatusRSS(server.URL)
if err == nil {
t.Fatal("FetchVendorStatusRSS error = nil, want non-nil")
}
if !strings.Contains(err.Error(), "vendor status rss returned 502") {
t.Fatalf("FetchVendorStatusRSS error = %q, want status code message", err.Error())
}
}
func TestBuildApplicationMenuIncludesUpdateEntry(t *testing.T) {
appMenu := buildApplicationMenuWithUpdateAction(func() {})
updateItem := findMenuItemByLabel(appMenu, macOSCheckForUpdatesMenuLabel)
if updateItem == nil {
t.Fatalf("application menu does not include %q", macOSCheckForUpdatesMenuLabel)
}
if updateItem.Disabled {
t.Fatalf("%q menu item is disabled", macOSCheckForUpdatesMenuLabel)
}
if updateItem.Click == nil {
t.Fatalf("%q menu item has no click action", macOSCheckForUpdatesMenuLabel)
}
}
func TestApplicationMenuUpdateEntryUsesSharedUpdateAction(t *testing.T) {
called := false
appMenu := buildApplicationMenuWithUpdateAction(func() {
called = true
})
updateItem := findMenuItemByLabel(appMenu, macOSCheckForUpdatesMenuLabel)
if updateItem == nil {
t.Fatalf("application menu does not include %q", macOSCheckForUpdatesMenuLabel)
}
updateItem.Click(&menu.CallbackData{MenuItem: updateItem})
if !called {
t.Fatalf("%q click did not call update action", macOSCheckForUpdatesMenuLabel)
}
}
func TestConsumeLoginItemArgRemovesCustomArgBeforeWailsParsesFlags(t *testing.T) {
args, found := consumeLoginItemArg([]string{
"/Applications/GetTokens.app/Contents/MacOS/GetTokens",
"-loglevel",
"Info",
wailsapp.GetTokensLoginItemArg,
"-assetdir",
"frontend/dist",
})
if !found {
t.Fatal("consumeLoginItemArg found = false, want true")
}
if strings.Contains(strings.Join(args, " "), wailsapp.GetTokensLoginItemArg) {
t.Fatalf("consumeLoginItemArg did not remove %q from %v", wailsapp.GetTokensLoginItemArg, args)
}
want := []string{
"/Applications/GetTokens.app/Contents/MacOS/GetTokens",
"-loglevel",
"Info",
"-assetdir",
"frontend/dist",
}
if strings.Join(args, "\x00") != strings.Join(want, "\x00") {
t.Fatalf("consumeLoginItemArg args = %v, want %v", args, want)
}
}
func TestConsumeLoginItemArgReportsMissingArg(t *testing.T) {
args, found := consumeLoginItemArg([]string{
"/Applications/GetTokens.app/Contents/MacOS/GetTokens",
"-loglevel",
"Info",
})
if found {
t.Fatal("consumeLoginItemArg found = true, want false")
}
if strings.Join(args, "\x00") != "/Applications/GetTokens.app/Contents/MacOS/GetTokens\x00-loglevel\x00Info" {
t.Fatalf("consumeLoginItemArg changed args unexpectedly: %v", args)
}
}
func findMenuItemByLabel(appMenu *menu.Menu, label string) *menu.MenuItem {
for _, item := range appMenu.Items {
if found := findMenuItemByLabelInItem(item, label); found != nil {
return found
}
}
return nil
}
func findMenuItemByLabelInItem(item *menu.MenuItem, label string) *menu.MenuItem {
if item.Label == label {
return item
}
if item.SubMenu == nil {
return nil
}
for _, child := range item.SubMenu.Items {
if found := findMenuItemByLabelInItem(child, label); found != nil {
return found
}
}
return nil
}
func TestMapCodexQuotaResponsePreservesBilling(t *testing.T) {
usedTokens := 125.0
limitTokens := 1000.0
remainingTokens := 875.0
result := mapCodexQuotaResponse(&wailsapp.CodexQuotaResponse{
PlanType: "metered",
Windows: []wailsapp.CodexQuotaWindow{
{
ID: "weekly",
Label: "7D",
UsedTokens: &usedTokens,
LimitTokens: &limitTokens,
RemainingTokens: &remainingTokens,
ResetLabel: "tomorrow",
},
},
Billing: &wailsapp.CodexQuotaBillingInfo{
IsAvailable: true,
BalanceInfos: []wailsapp.CodexQuotaBillingBalanceInfo{
{
Currency: "USD",
TotalBalance: "12.34",
GrantedBalance: "5.67",
},
},
},
})
if result == nil {
t.Fatal("mapCodexQuotaResponse returned nil")
}
if result.Billing == nil {
t.Fatal("mapCodexQuotaResponse billing = nil, want preserved billing")
}
if !result.Billing.IsAvailable {
t.Fatal("mapCodexQuotaResponse billing availability = false, want true")
}
if got := len(result.Billing.BalanceInfos); got != 1 {
t.Fatalf("mapCodexQuotaResponse billing entries = %d, want 1", got)
}
if got := result.Billing.BalanceInfos[0].Currency; got != "USD" {
t.Fatalf("mapCodexQuotaResponse currency = %q, want %q", got, "USD")
}
if result.Windows[0].UsedTokens == nil || *result.Windows[0].UsedTokens != 125 {
t.Fatalf("mapCodexQuotaResponse used tokens = %#v, want 125", result.Windows[0].UsedTokens)
}
if result.Windows[0].LimitTokens == nil || *result.Windows[0].LimitTokens != 1000 {
t.Fatalf("mapCodexQuotaResponse limit tokens = %#v, want 1000", result.Windows[0].LimitTokens)
}
if result.Windows[0].RemainingTokens == nil || *result.Windows[0].RemainingTokens != 875 {
t.Fatalf("mapCodexQuotaResponse remaining tokens = %#v, want 875", result.Windows[0].RemainingTokens)
}
}