| 1 | package runnersetup |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "testing/synctest" |
| 13 | "time" |
| 14 | |
| 15 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 16 | "amadan.net/oficina/equipo/internal/runnerproto" |
| 17 | ) |
| 18 | |
| 19 | type loginFixture struct { |
| 20 | home string |
| 21 | state string |
| 22 | cancelled bool |
| 23 | identity provider.Identity |
| 24 | } |
| 25 | |
| 26 | func (d *loginFixture) Begin(_ context.Context, name, home string) (provider.LoginChallenge, error) { |
| 27 | d.home = home |
| 28 | return provider.LoginChallenge{ID: "provider-secret", Mode: map[string]string{"claude": "browser", "codex": "device"}[name], VerificationURI: "https://auth.openai.com/codex/device", UserCode: "ABCD-EFGH", ExpiresAt: time.Now().Add(time.Minute)}, nil |
| 29 | } |
| 30 | func (d *loginFixture) Poll(context.Context, string) (provider.LoginResult, error) { |
| 31 | return provider.LoginResult{State: d.state, Identity: d.identity}, nil |
| 32 | } |
| 33 | func (d *loginFixture) Cancel(context.Context, string) error { d.cancelled = true; return nil } |
| 34 | |
| 35 | type loginInstance struct { |
| 36 | setup runnerproto.AccountSetup |
| 37 | result runnerproto.AccountSetupResult |
| 38 | err error |
| 39 | } |
| 40 | |
| 41 | func (i *loginInstance) AccountSetup(_ context.Context, id string) (runnerproto.AccountSetup, error) { |
| 42 | if id != i.setup.ID || i.err != nil { |
| 43 | return runnerproto.AccountSetup{}, provider.ErrIdentity |
| 44 | } |
| 45 | return i.setup, nil |
| 46 | } |
| 47 | func (i *loginInstance) ReportAccountSetup(_ context.Context, r runnerproto.AccountSetupResult) (runnerproto.AccountSetup, error) { |
| 48 | i.result = r |
| 49 | if i.err != nil { |
| 50 | return runnerproto.AccountSetup{}, i.err |
| 51 | } |
| 52 | i.setup.State = r.State |
| 53 | i.setup.Revision++ |
| 54 | return i.setup, nil |
| 55 | } |
| 56 | func loginHarness(t *testing.T, name string) (*AccountLogin, *loginFixture, *loginInstance) { |
| 57 | t.Helper() |
| 58 | root := filepath.Join(t.TempDir(), "private") |
| 59 | d := &loginFixture{state: "pending", identity: provider.Identity{AccountID: "account", HomeID: "home", ProviderVersion: "fixture"}} |
| 60 | i := &loginInstance{setup: runnerproto.AccountSetup{ID: "setup-one", RunnerID: "runner", Provider: name, State: "pending", Mode: map[string]string{"claude": "browser", "codex": "device"}[name], Revision: 1}} |
| 61 | l := &AccountLogin{Root: root, RunnerID: "runner", Instance: i, Driver: d, Probe: func(context.Context, provider.Binding) (provider.Identity, error) { return d.identity, nil }, Save: func(context.Context, runnerproto.AccountSetup, provider.Binding) error { return nil }} |
| 62 | return l, d, i |
| 63 | } |
| 64 | func TestOnboardingAccountDedicatedHome(t *testing.T) { |
| 65 | ctx := context.Background() |
| 66 | l, d, i := loginHarness(t, "codex") |
| 67 | c, e := l.Begin(ctx, "setup-one") |
| 68 | if e != nil { |
| 69 | t.Fatal(e) |
| 70 | } |
| 71 | first := d.home |
| 72 | st, e := os.Stat(first) |
| 73 | if e != nil || st.Mode().Perm() != 0700 { |
| 74 | t.Fatal("home not private", e) |
| 75 | } |
| 76 | if e = l.Cancel(ctx); e != nil { |
| 77 | t.Fatal(e) |
| 78 | } |
| 79 | i.setup.State = "pending" |
| 80 | i.setup.ID = "setup-two" |
| 81 | _, e = l.Begin(ctx, "setup-two") |
| 82 | if e != nil { |
| 83 | t.Fatal(e) |
| 84 | } |
| 85 | if first == d.home { |
| 86 | t.Fatal("same-provider homes shared") |
| 87 | } |
| 88 | if err := os.WriteFile(filepath.Join(d.home, "auth.json"), []byte(`{"token":"NATIVE-SECRET"}`), 0600); err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | d.state = "connected" |
| 92 | probes := 0 |
| 93 | l.Probe = func(context.Context, provider.Binding) (provider.Identity, error) { probes++; return d.identity, nil } |
| 94 | out, err := l.Poll(ctx) |
| 95 | if err != nil || out.State != "connected" || probes != 1 || i.result.AccountID != d.identity.AccountID { |
| 96 | t.Fatal("dedicated home missing identity readback", out, err) |
| 97 | } |
| 98 | entries, err := os.ReadDir(l.Root) |
| 99 | if err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | for _, entry := range entries { |
| 103 | if strings.Contains(entry.Name(), "journal") || strings.HasSuffix(entry.Name(), ".jsonl") { |
| 104 | t.Fatal("login emitted journal events") |
| 105 | } |
| 106 | } |
| 107 | raw, _ := json.Marshal(i.result) |
| 108 | if strings.Contains(string(raw), "NATIVE-SECRET") { |
| 109 | t.Fatal("credential left runner") |
| 110 | } |
| 111 | if strings.Contains(string(raw), "provider-secret") || strings.Contains(string(raw), l.Root) { |
| 112 | t.Fatal("secret left runner") |
| 113 | } |
| 114 | if c.ID != "provider-secret" { |
| 115 | t.Fatal("local challenge lost") |
| 116 | } |
| 117 | bad, _, bi := loginHarness(t, "codex") |
| 118 | bi.setup.ID = "fresh" |
| 119 | bad.Root = filepath.Join(l.Root, ".codex") |
| 120 | t.Setenv("CODEX_HOME", bad.Root) |
| 121 | if _, e = bad.Begin(ctx, "fresh"); e == nil { |
| 122 | t.Fatal("interactive home accepted") |
| 123 | } |
| 124 | } |
| 125 | func TestOnboardingDeviceFlow(t *testing.T) { |
| 126 | for _, state := range []string{"pending", "connected", "expired", "denied", "cancelled"} { |
| 127 | t.Run(state, func(t *testing.T) { |
| 128 | l, d, i := loginHarness(t, "codex") |
| 129 | ctx := context.Background() |
| 130 | _, e := l.Begin(ctx, i.setup.ID) |
| 131 | if e != nil { |
| 132 | t.Fatal(e) |
| 133 | } |
| 134 | d.state = state |
| 135 | r, e := l.Poll(ctx) |
| 136 | if state == "connected" { |
| 137 | if e == nil || r.State == "connected" { |
| 138 | t.Fatal("login succeeded without credential readback") |
| 139 | } |
| 140 | } else if e != nil || r.State != state { |
| 141 | t.Fatal(state, r, e) |
| 142 | } |
| 143 | }) |
| 144 | } |
| 145 | l, d, i := loginHarness(t, "codex") |
| 146 | _, e := l.Begin(context.Background(), i.setup.ID) |
| 147 | if e != nil { |
| 148 | t.Fatal(e) |
| 149 | } |
| 150 | if e = l.Cancel(context.Background()); e != nil || !d.cancelled || i.result.State != "cancelled" { |
| 151 | t.Fatal("cancel not contained", e) |
| 152 | } |
| 153 | restart := *l |
| 154 | restart.active = nil |
| 155 | if _, e = restart.Poll(context.Background()); e == nil { |
| 156 | t.Fatal("restart invented success") |
| 157 | } |
| 158 | } |
| 159 | func TestOnboardingNoDeviceCapability(t *testing.T) { |
| 160 | for _, name := range []string{"claude", "codex"} { |
| 161 | d := provider.NewLoginDriver() |
| 162 | _, e := d.Begin(context.Background(), name, filepath.Join(t.TempDir(), "never-created")) |
| 163 | if !errors.Is(e, provider.ErrLoginUnavailable) { |
| 164 | t.Fatal("unrecorded login contract enabled", name, e) |
| 165 | } |
| 166 | } |
| 167 | l, _, i := loginHarness(t, "claude") |
| 168 | c, e := l.Begin(context.Background(), i.setup.ID) |
| 169 | if e != nil || c.Mode != "browser" || i.result.Challenge != nil { |
| 170 | t.Fatal("browser fallback exposed challenge", e) |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestOnboardingAccountBindings(t *testing.T) { |
| 175 | root := filepath.Join(t.TempDir(), "state") |
| 176 | if e := os.Mkdir(root, 0700); e != nil { |
| 177 | t.Fatal(e) |
| 178 | } |
| 179 | path := filepath.Join(root, "accounts.json") |
| 180 | s := runnerproto.AccountSetup{ID: "one", Provider: "codex", Label: "Personal"} |
| 181 | b := provider.Binding{ID: "one", Provider: "codex", Home: filepath.Join(root, "one"), AccountID: "account-one", Expected: provider.Identity{AccountID: "account-one", HomeID: "home-one", ProviderVersion: "0.165.0"}} |
| 182 | if e := SaveAccountBinding(context.Background(), path, "person", "model", s, b); e != nil { |
| 183 | t.Fatal(e) |
| 184 | } |
| 185 | original, e := os.ReadFile(path) |
| 186 | if e != nil { |
| 187 | t.Fatal(e) |
| 188 | } |
| 189 | if e = SaveAccountBinding(context.Background(), path, "person", "model", s, b); e == nil { |
| 190 | t.Fatal("new setup replaced existing home") |
| 191 | } |
| 192 | after, _ := os.ReadFile(path) |
| 193 | if string(after) != string(original) { |
| 194 | t.Fatal("rejected save changed bindings") |
| 195 | } |
| 196 | s.ID = "two" |
| 197 | s.Label = "Work" |
| 198 | b.ID = "two" |
| 199 | b.Home = filepath.Join(root, "two") |
| 200 | b.AccountID = "account-two" |
| 201 | b.Expected.AccountID = b.AccountID |
| 202 | b.Expected.HomeID = "home-two" |
| 203 | if e = SaveAccountBinding(context.Background(), path, "person", "model", s, b); e != nil { |
| 204 | t.Fatal(e) |
| 205 | } |
| 206 | raw, _ := os.ReadFile(path) |
| 207 | var rows []AccountBinding |
| 208 | if e = json.Unmarshal(raw, &rows); e != nil || len(rows) != 2 || rows[0].Label != "Personal" || rows[1].Label != "Work" { |
| 209 | t.Fatal("labeled accounts lost", e) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | func TestOnboardingAccountReportRetry(t *testing.T) { |
| 214 | l, d, i := loginHarness(t, "codex") |
| 215 | ctx := context.Background() |
| 216 | if _, e := l.Begin(ctx, i.setup.ID); e != nil { |
| 217 | t.Fatal(e) |
| 218 | } |
| 219 | if e := os.WriteFile(filepath.Join(d.home, "auth.json"), []byte(`{"token":"fixture"}`), 0600); e != nil { |
| 220 | t.Fatal(e) |
| 221 | } |
| 222 | saves := 0 |
| 223 | l.Save = func(context.Context, runnerproto.AccountSetup, provider.Binding) error { |
| 224 | saves++ |
| 225 | i.err = errors.New("lost response") |
| 226 | return nil |
| 227 | } |
| 228 | d.state = "connected" |
| 229 | if _, e := l.Poll(ctx); e == nil { |
| 230 | t.Fatal("expected report failure") |
| 231 | } |
| 232 | i.err = nil |
| 233 | out, e := l.Poll(ctx) |
| 234 | if e != nil || out.State != "connected" || saves != 1 { |
| 235 | t.Fatal("saved account report not retryable", out, e, saves) |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | func TestOnboardingAccountClockExpiry(t *testing.T) { |
| 240 | l, d, i := loginHarness(t, "codex") |
| 241 | if _, e := l.Begin(context.Background(), i.setup.ID); e != nil { |
| 242 | t.Fatal(e) |
| 243 | } |
| 244 | l.active.challenge.ExpiresAt = time.Now().Add(-time.Second) |
| 245 | out, e := l.Poll(context.Background()) |
| 246 | if e != nil || out.State != "expired" || !d.cancelled { |
| 247 | t.Fatal("clock expiry ignored", out, e) |
| 248 | } |
| 249 | if _, e = os.Stat(d.home); !os.IsNotExist(e) { |
| 250 | t.Fatal("expired home retained") |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | type beginFailure struct { |
| 255 | loginFixture |
| 256 | invalid bool |
| 257 | } |
| 258 | |
| 259 | func (d *beginFailure) Begin(ctx context.Context, name, home string) (provider.LoginChallenge, error) { |
| 260 | c, e := d.loginFixture.Begin(ctx, name, home) |
| 261 | if d.invalid { |
| 262 | c.Mode = "wrong" |
| 263 | return c, e |
| 264 | } |
| 265 | return c, provider.ErrLoginUnavailable |
| 266 | } |
| 267 | func TestOnboardingAccountBeginCleanup(t *testing.T) { |
| 268 | for _, invalid := range []bool{false, true} { |
| 269 | t.Run(fmt.Sprint(invalid), func(t *testing.T) { |
| 270 | l, base, i := loginHarness(t, "claude") |
| 271 | d := &beginFailure{loginFixture: *base, invalid: invalid} |
| 272 | l.Driver = d |
| 273 | if _, e := l.Begin(context.Background(), i.setup.ID); e == nil { |
| 274 | t.Fatal("bad Begin accepted") |
| 275 | } |
| 276 | entries, e := os.ReadDir(l.Root) |
| 277 | if e != nil || len(entries) != 0 { |
| 278 | t.Fatal("failed Begin leaked home or marker", entries, e) |
| 279 | } |
| 280 | if invalid && !d.cancelled { |
| 281 | t.Fatal("invalid challenge left CLI running") |
| 282 | } |
| 283 | }) |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | func TestOnboardingAccountResumeSavedReport(t *testing.T) { |
| 288 | l, d, i := loginHarness(t, "codex") |
| 289 | ctx := context.Background() |
| 290 | if _, e := l.Begin(ctx, i.setup.ID); e != nil { |
| 291 | t.Fatal(e) |
| 292 | } |
| 293 | if e := os.WriteFile(filepath.Join(d.home, "auth.json"), []byte(`{"token":"fixture"}`), 0600); e != nil { |
| 294 | t.Fatal(e) |
| 295 | } |
| 296 | path := filepath.Join(l.Root, "bindings.json") |
| 297 | l.Save = func(ctx context.Context, s runnerproto.AccountSetup, b provider.Binding) error { |
| 298 | if e := SaveAccountBinding(ctx, path, "person", "model", s, b); e != nil { |
| 299 | return e |
| 300 | } |
| 301 | i.err = errors.New("offline after save") |
| 302 | return nil |
| 303 | } |
| 304 | d.state = "connected" |
| 305 | if _, e := l.Poll(ctx); e == nil { |
| 306 | t.Fatal("expected report loss") |
| 307 | } |
| 308 | i.err = nil |
| 309 | handled, e := ResumeSavedAccount(ctx, path, "person", i.setup, i, l.Probe) |
| 310 | if e != nil || !handled || i.setup.State != "connected" { |
| 311 | t.Fatal("restart could not report saved binding", handled, e) |
| 312 | } |
| 313 | if _, e = os.Stat(d.home); e != nil { |
| 314 | t.Fatal("saved home removed", e) |
| 315 | } |
| 316 | i.setup.State = "pending" |
| 317 | _, e = ResumeSavedAccount(ctx, path, "other-person", i.setup, i, l.Probe) |
| 318 | if e == nil { |
| 319 | t.Fatal("saved binding changed owner") |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | type cancelOnBegin struct { |
| 324 | loginFixture |
| 325 | stop context.CancelFunc |
| 326 | } |
| 327 | |
| 328 | func (d *cancelOnBegin) Begin(ctx context.Context, name, home string) (provider.LoginChallenge, error) { |
| 329 | c, e := d.loginFixture.Begin(ctx, name, home) |
| 330 | d.stop() |
| 331 | return c, e |
| 332 | } |
| 333 | func TestOnboardingAccountCommandCancel(t *testing.T) { |
| 334 | l, base, i := loginHarness(t, "claude") |
| 335 | ctx, stop := context.WithCancel(context.Background()) |
| 336 | defer stop() |
| 337 | d := &cancelOnBegin{loginFixture: *base, stop: stop} |
| 338 | l.Driver = d |
| 339 | if e := RunAccountLogin(ctx, l, i.setup.ID); !errors.Is(e, context.Canceled) { |
| 340 | t.Fatal("interrupt not returned", e) |
| 341 | } |
| 342 | if !d.cancelled || i.setup.State != "cancelled" { |
| 343 | t.Fatal("interrupt not reported", i.setup) |
| 344 | } |
| 345 | if _, e := os.Stat(d.home); !os.IsNotExist(e) { |
| 346 | t.Fatal("cancelled home retained") |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | type contextAccountInstance struct{ *loginInstance } |
| 351 | |
| 352 | func (i contextAccountInstance) ReportAccountSetup(ctx context.Context, q runnerproto.AccountSetupResult) (runnerproto.AccountSetup, error) { |
| 353 | if e := ctx.Err(); e != nil { |
| 354 | return runnerproto.AccountSetup{}, e |
| 355 | } |
| 356 | return i.loginInstance.ReportAccountSetup(ctx, q) |
| 357 | } |
| 358 | func TestOnboardingAccountInterruptDuringBegin(t *testing.T) { |
| 359 | l, base, i := loginHarness(t, "claude") |
| 360 | l.Instance = contextAccountInstance{i} |
| 361 | ctx, stop := context.WithCancel(context.Background()) |
| 362 | defer stop() |
| 363 | d := &cancelOnBegin{loginFixture: *base, stop: stop} |
| 364 | l.Driver = d |
| 365 | if e := RunAccountLogin(ctx, l, i.setup.ID); !errors.Is(e, context.Canceled) { |
| 366 | t.Fatal("interrupt not returned", e) |
| 367 | } |
| 368 | if !d.cancelled || i.setup.State != "cancelled" { |
| 369 | t.Fatal("interrupt during Begin not reported", i.setup) |
| 370 | } |
| 371 | if _, e := os.Stat(d.home); !os.IsNotExist(e) { |
| 372 | t.Fatal("cancelled home retained") |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | func TestOnboardingAccountInterruptDuringProbe(t *testing.T) { |
| 377 | l, d, i := loginHarness(t, "codex") |
| 378 | l.Instance = contextAccountInstance{i} |
| 379 | ctx, stop := context.WithCancel(context.Background()) |
| 380 | defer stop() |
| 381 | if _, e := l.Begin(ctx, i.setup.ID); e != nil { |
| 382 | t.Fatal(e) |
| 383 | } |
| 384 | if e := os.WriteFile(filepath.Join(d.home, "auth.json"), []byte(`{"token":"fixture"}`), 0600); e != nil { |
| 385 | t.Fatal(e) |
| 386 | } |
| 387 | d.state = "connected" |
| 388 | l.Probe = func(context.Context, provider.Binding) (provider.Identity, error) { |
| 389 | stop() |
| 390 | return provider.Identity{}, context.Canceled |
| 391 | } |
| 392 | _, e := l.Poll(ctx) |
| 393 | if !errors.Is(e, context.Canceled) || i.setup.State != "cancelled" || !d.cancelled { |
| 394 | t.Fatal("probe interrupt not cancelled", e, i.setup) |
| 395 | } |
| 396 | if _, e = os.Stat(d.home); !os.IsNotExist(e) { |
| 397 | t.Fatal("interrupted probe retained home") |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | type transientInstance struct { |
| 402 | *loginInstance |
| 403 | fail bool |
| 404 | } |
| 405 | |
| 406 | func (i *transientInstance) AccountSetup(ctx context.Context, id string) (runnerproto.AccountSetup, error) { |
| 407 | if i.fail { |
| 408 | return runnerproto.AccountSetup{}, runnerproto.ErrTransient |
| 409 | } |
| 410 | return i.loginInstance.AccountSetup(ctx, id) |
| 411 | } |
| 412 | func TestFinalFixAccountTransientPoll(t *testing.T) { |
| 413 | l, d, base := loginHarness(t, "codex") |
| 414 | i := &transientInstance{loginInstance: base} |
| 415 | l.Instance = i |
| 416 | if _, err := l.Begin(context.Background(), base.setup.ID); err != nil { |
| 417 | t.Fatal(err) |
| 418 | } |
| 419 | i.fail = true |
| 420 | if _, err := l.Poll(context.Background()); !errors.Is(err, runnerproto.ErrTransient) { |
| 421 | t.Fatal("transient error lost", err) |
| 422 | } |
| 423 | if d.cancelled || l.active == nil { |
| 424 | t.Fatal("network error cancelled in-progress login") |
| 425 | } |
| 426 | if _, err := os.Stat(d.home); err != nil { |
| 427 | t.Fatal("network error deleted login home", err) |
| 428 | } |
| 429 | i.fail = false |
| 430 | out, err := l.Poll(context.Background()) |
| 431 | if err != nil || out.State != "pending" { |
| 432 | t.Fatal("login could not continue", out, err) |
| 433 | } |
| 434 | if err = l.Cancel(context.Background()); err != nil { |
| 435 | t.Fatal(err) |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | type commandRetryInstance struct { |
| 440 | *loginInstance |
| 441 | calls []time.Time |
| 442 | stop context.CancelFunc |
| 443 | driver *loginFixture |
| 444 | } |
| 445 | |
| 446 | func (i *commandRetryInstance) AccountSetup(ctx context.Context, id string) (runnerproto.AccountSetup, error) { |
| 447 | i.calls = append(i.calls, time.Now()) |
| 448 | if len(i.calls) == 2 { |
| 449 | return runnerproto.AccountSetup{}, runnerproto.ErrTransient |
| 450 | } |
| 451 | if len(i.calls) == 3 { |
| 452 | i.stop() |
| 453 | } |
| 454 | return i.loginInstance.AccountSetup(ctx, id) |
| 455 | } |
| 456 | func TestFinalFixAccountPollBackoff(t *testing.T) { |
| 457 | l, d, base := loginHarness(t, "claude") |
| 458 | ctx, stop := context.WithTimeout(context.Background(), 6*time.Second) |
| 459 | defer stop() |
| 460 | i := &commandRetryInstance{loginInstance: base, stop: stop, driver: d} |
| 461 | l.Instance = i |
| 462 | _ = RunAccountLogin(ctx, l, base.setup.ID) |
| 463 | if len(i.calls) < 3 { |
| 464 | t.Fatal("transient poll was not retried", len(i.calls)) |
| 465 | } |
| 466 | if i.calls[1].Sub(i.calls[0]) < time.Second || i.calls[2].Sub(i.calls[1]) < 2*time.Second { |
| 467 | t.Fatal("poll interval or backoff too short", i.calls) |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | func TestFinalFixAccountTransientExpiry(t *testing.T) { |
| 472 | l, d, base := loginHarness(t, "codex") |
| 473 | i := &transientInstance{loginInstance: base} |
| 474 | l.Instance = i |
| 475 | if _, err := l.Begin(context.Background(), base.setup.ID); err != nil { |
| 476 | t.Fatal(err) |
| 477 | } |
| 478 | l.active.challenge.ExpiresAt = time.Now().Add(-time.Second) |
| 479 | i.fail = true |
| 480 | _, err := l.Poll(context.Background()) |
| 481 | if err == nil || !d.cancelled || l.active != nil { |
| 482 | t.Fatal("expired offline login left provider running", err, d.cancelled) |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | func TestFinalFixAccountBackoffBound(t *testing.T) { |
| 487 | synctest.Test(t, func(t *testing.T) { |
| 488 | l, d, base := loginHarness(t, "codex") |
| 489 | i := &commandRetryInstance{loginInstance: base, driver: d} |
| 490 | l.Instance = &offlineAfterBegin{instance: i} |
| 491 | err := RunAccountLogin(context.Background(), l, base.setup.ID) |
| 492 | if !errors.Is(err, runnerproto.ErrTransient) || !d.cancelled { |
| 493 | t.Fatal("expired offline login not stopped", err) |
| 494 | } |
| 495 | if len(i.calls) < 7 { |
| 496 | t.Fatal("retry schedule not exercised", len(i.calls)) |
| 497 | } |
| 498 | for n := 1; n < len(i.calls); n++ { |
| 499 | delta := i.calls[n].Sub(i.calls[n-1]) |
| 500 | if delta < time.Second || delta > 30*time.Second { |
| 501 | t.Fatal("unbounded backoff", delta) |
| 502 | } |
| 503 | } |
| 504 | }) |
| 505 | } |
| 506 | |
| 507 | type offlineAfterBegin struct{ instance *commandRetryInstance } |
| 508 | |
| 509 | func (i *offlineAfterBegin) AccountSetup(ctx context.Context, id string) (runnerproto.AccountSetup, error) { |
| 510 | i.instance.calls = append(i.instance.calls, time.Now()) |
| 511 | if len(i.instance.calls) > 1 { |
| 512 | return runnerproto.AccountSetup{}, runnerproto.ErrTransient |
| 513 | } |
| 514 | return i.instance.loginInstance.AccountSetup(ctx, id) |
| 515 | } |
| 516 | func (i *offlineAfterBegin) ReportAccountSetup(ctx context.Context, q runnerproto.AccountSetupResult) (runnerproto.AccountSetup, error) { |
| 517 | return i.instance.ReportAccountSetup(ctx, q) |
| 518 | } |
| 519 | |
| 520 | func TestFinalFixAccountAuthorityAfterProbe(t *testing.T) { |
| 521 | for _, mode := range []string{"revoked", "transient"} { |
| 522 | t.Run(mode, func(t *testing.T) { |
| 523 | l, d, base := loginHarness(t, "codex") |
| 524 | i := &transientInstance{loginInstance: base} |
| 525 | l.Instance = i |
| 526 | if _, err := l.Begin(context.Background(), base.setup.ID); err != nil { |
| 527 | t.Fatal(err) |
| 528 | } |
| 529 | if err := os.WriteFile(filepath.Join(d.home, "auth.json"), []byte(`{"token":"fixture"}`), 0600); err != nil { |
| 530 | t.Fatal(err) |
| 531 | } |
| 532 | d.state = "connected" |
| 533 | probes, saves := 0, 0 |
| 534 | l.Probe = func(context.Context, provider.Binding) (provider.Identity, error) { |
| 535 | probes++ |
| 536 | if mode == "revoked" { |
| 537 | base.setup.State = "cancelled" |
| 538 | } else { |
| 539 | i.fail = true |
| 540 | } |
| 541 | return d.identity, nil |
| 542 | } |
| 543 | l.Save = func(context.Context, runnerproto.AccountSetup, provider.Binding) error { saves++; return nil } |
| 544 | _, err := l.Poll(context.Background()) |
| 545 | if err == nil || saves != 0 { |
| 546 | t.Fatal("saved without current instance authority", err, saves) |
| 547 | } |
| 548 | if mode == "transient" { |
| 549 | i.fail = false |
| 550 | out, err := l.Poll(context.Background()) |
| 551 | if err != nil || out.State != "connected" || probes != 1 || saves != 1 { |
| 552 | t.Fatal("verified login could not resume", out, err, probes, saves) |
| 553 | } |
| 554 | } |
| 555 | }) |
| 556 | } |
| 557 | } |
| 558 | |