v0.50.0 c4ec31f

codex_usage_test.go RawHistoryFolder

1package agent
2
3import (
4	"context"
5	"net/http"
6	"net/http/httptest"
7	"sync/atomic"
8	"testing"
9	"time"
10
11	"github.com/bnema/zut/packages/provider/auth"
12)
13
14func TestCodexWeeklyUsageFetcherEligibility(t *testing.T) {
15	for _, tc := range []struct {
16		name     string
17		resolved Resolved
18		want     bool
19	}{
20		{"subscription", Resolved{Provider: "openai-codex", AuthMethod: "oauth", AccountID: "synthetic"}, true},
21		{"canonical endpoint", Resolved{Provider: "openai-codex", AuthMethod: "oauth", AccountID: "synthetic", BaseURL: "https://chatgpt.com/backend-api/codex/responses/"}, true},
22		{"api key", Resolved{Provider: "openai-codex", AuthMethod: "apikey", AccountID: "synthetic"}, false},
23		{"no account", Resolved{Provider: "openai-codex", AuthMethod: "oauth"}, false},
24		{"other provider", Resolved{Provider: "openai", AuthMethod: "oauth", AccountID: "synthetic"}, false},
25		{"custom endpoint", Resolved{Provider: "openai-codex", AuthMethod: "oauth", AccountID: "synthetic", BaseURL: "https://custom.example/responses"}, false},
26	} {
27		t.Run(tc.name, func(t *testing.T) {
28			if got := tc.resolved.codexWeeklyUsageFetcher() != nil; got != tc.want {
29				t.Fatalf("fetcher available = %t, want %t", got, tc.want)
30			}
31		})
32	}
33}
34
35func TestCodexWeeklyUsageFetcherRejectsMissingChangedAndExpiredCredentials(t *testing.T) {
36	t.Setenv("ZUT_HOME", t.TempDir())
37	fetch := (Resolved{Provider: "openai-codex", AuthMethod: "oauth", AccountID: "synthetic-account"}).codexWeeklyUsageFetcher()
38	if _, err := fetch(context.Background()); err == nil {
39		t.Fatal("missing credential accepted")
40	}
41	store := AuthStoreFor()
42	if err := store.SetOAuth("openai", auth.OAuthToken{AccessToken: "synthetic-token", AccountID: "different-account", Expiry: time.Now().Add(time.Hour)}); err != nil {
43		t.Fatal(err)
44	}
45	if _, err := fetch(context.Background()); err == nil {
46		t.Fatal("different account accepted")
47	}
48	var refreshCalls atomic.Int32
49	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
50		refreshCalls.Add(1)
51		w.WriteHeader(http.StatusUnauthorized)
52	}))
53	defer srv.Close()
54	original := auth.OpenAIOAuth.TokenURL
55	auth.OpenAIOAuth.TokenURL = srv.URL
56	t.Cleanup(func() { auth.OpenAIOAuth.TokenURL = original })
57	if err := store.SetOAuth("openai", auth.OAuthToken{AccessToken: "synthetic-token", RefreshToken: "synthetic-refresh", AccountID: "synthetic-account", Expiry: time.Now().Add(-time.Hour)}); err != nil {
58		t.Fatal(err)
59	}
60	if _, err := fetch(context.Background()); err == nil {
61		t.Fatal("expired credential accepted")
62	}
63	if refreshCalls.Load() != 0 {
64		t.Fatal("passive usage meter attempted to rotate credentials")
65	}
66}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 476

↑↓ moveenter openesc close