]> git.giacomolli.uk - onechat/commitdiff
Read the time directly in the model client
authorAnderson A. Giacomolli <redacted>
Fri, 6 Nov 2026 17:38:59 +0000 (14:38 -0300)
committerAnderson A. Giacomolli <redacted>
Fri, 6 Nov 2026 17:38:59 +0000 (14:38 -0300)
The model client and the ChatGPT authorizer read the time with time.Now
instead of a clock field. No caller set the field, so it was an
injection with no user. A test that needs fake time can run in a
testing/synctest bubble, where the time package uses a fake clock.

Issue: b14d537

auth.go
responses.go

diff --git a/auth.go b/auth.go
index 1908b89507bc62f210a38679713cb825434462d5..736624e37e0850df3f21f116c60862815282f571 100644 (file)
--- a/auth.go
+++ b/auth.go
@@ -63,7 +63,6 @@ type chatgptAuth struct {
        path   string
        issuer string
        http   *http.Client
-       clock  func() time.Time
 }
 
 // tokenFile is the content of the token file.
@@ -115,7 +114,6 @@ func newChatGPTAuth(path, issuer string) *chatgptAuth {
                path:   path,
                issuer: issuer,
                http:   &http.Client{},
-               clock:  time.Now,
        }
 }
 
@@ -308,7 +306,7 @@ func (a *chatgptAuth) Authorize(ctx context.Context, header http.Header) error {
        }
 
        expiry, found := tokenExpiry(tokens.AccessToken)
-       if !found || !a.clock().Before(expiry.Add(-refreshMargin)) {
+       if !found || !time.Now().Before(expiry.Add(-refreshMargin)) {
                tokens, err = a.refresh(ctx, tokens)
                if err != nil {
                        return err
index 9d6b75f6c05ce2e55335e77b44877b69ea56df5f..ebffaa36aaa3e477cc9a722f2fbbfea16a8f5a52 100644 (file)
@@ -57,7 +57,6 @@ type Client struct {
        cacheKey string
        usage    *usageLog
        http     *http.Client
-       clock    func() time.Time
 }
 
 // Request is one model call. Input holds the input items in order, such as
@@ -179,8 +178,7 @@ func newClient(url string, auth authorizer, cacheKey, usagePath string) *Client
                usage: &usageLog{
                        path: usagePath,
                },
-               http:  &http.Client{},
-               clock: time.Now,
+               http: &http.Client{},
        }
 }
 
@@ -216,7 +214,7 @@ func (c *Client) Create(ctx context.Context, request Request) (Response, error)
        }
 
        record := usageRecord{
-               Date:  c.clock().Format(time.RFC3339Nano),
+               Date:  time.Now().Format(time.RFC3339Nano),
                Model: request.Model,
        }