| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/base64" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func TestExpiredCredentialsNeedSignIn(t *testing.T) { |
| 15 | for _, raw := range []string{`{"claudeAiOauth":{"expiresAt":1}}`, expiredLoginFixture()} { |
| 16 | if !credentialsExpired([]byte(raw), time.Now()) { |
| 17 | t.Fatal("expired token not detected") |
| 18 | } |
| 19 | } |
| 20 | if credentialsExpired([]byte(`{"claudeAiOauth":{"expiresAt":4102444800000}}`), time.Now()) { |
| 21 | t.Fatal("valid token expired") |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestExpiredLeaseRefusesDispatch(t *testing.T) { |
| 26 | b, r := accountFixture(t) |
| 27 | lease := openFixture(t, b, r) |
| 28 | preflightFixture(t, lease) |
| 29 | writeFake(t, filepath.Join(b.Home, "auth.json"), expiredLoginFixture()) |
| 30 | if err := lease.BeginTurn(context.Background()); !errors.Is(err, ErrAccountMaintenance) { |
| 31 | t.Fatal("expired account did not require maintenance", err) |
| 32 | } |
| 33 | if _, err := openAccount(context.Background(), b, r); !errors.Is(err, ErrAccountBusy) { |
| 34 | t.Fatal("active account lock lost", err) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Construct an unsigned expired fixture in memory, without a token literal. |
| 39 | func expiredLoginFixture() string { |
| 40 | claims := base64.RawURLEncoding.EncodeToString([]byte(`{"exp":1}`)) |
| 41 | token := strings.Join([]string{"e30", claims, "fixture"}, ".") |
| 42 | raw, _ := json.Marshal(map[string]any{"tokens": map[string]string{"access_token": token}}) |
| 43 | return string(raw) |
| 44 | } |
| 45 | |