|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + hush "github.com/jack-work/hush/client" |
| 10 | +) |
| 11 | + |
| 12 | +// goneErr is the error a real hush client returns when no agent serves its |
| 13 | +// socket: the exact shape Gluck's turns failed with, produced by the real |
| 14 | +// client rather than spelled by hand. |
| 15 | +func goneErr(t *testing.T) error { |
| 16 | + t.Helper() |
| 17 | + c := hush.NewWithSocket(filepath.Join(t.TempDir(), "agent.sock")) |
| 18 | + _, _, err := c.OAuthGetFull("copilot") |
| 19 | + if err == nil { |
| 20 | + t.Fatal("a client with no agent behind it answered") |
| 21 | + } |
| 22 | + if !strings.Contains(err.Error(), "no such file or directory") { |
| 23 | + t.Fatalf("unexpected shape for a missing agent: %v", err) |
| 24 | + } |
| 25 | + return err |
| 26 | +} |
| 27 | + |
| 28 | +func TestAgentGoneRecognisesTheRealClientError(t *testing.T) { |
| 29 | + if !AgentGone(goneErr(t)) { |
| 30 | + t.Fatal("the error a missing agent produces is not recognised as a missing agent") |
| 31 | + } |
| 32 | + for _, other := range []error{ |
| 33 | + hush.ErrOAuthNotFound, |
| 34 | + errors.New("oauth refresh rejected"), |
| 35 | + nil, |
| 36 | + } { |
| 37 | + if AgentGone(other) { |
| 38 | + t.Fatalf("%v was taken for a missing agent", other) |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// A call that finds the agent gone revives it ONCE and asks again; the turn |
| 44 | +// sees only the second answer. |
| 45 | +func TestWithAgentRevivesAndRetriesOnce(t *testing.T) { |
| 46 | + gone := goneErr(t) |
| 47 | + calls, revives := 0, 0 |
| 48 | + got, err := WithAgent(func() error { revives++; return nil }, func() (string, error) { |
| 49 | + calls++ |
| 50 | + if calls == 1 { |
| 51 | + return "", gone |
| 52 | + } |
| 53 | + return "token", nil |
| 54 | + }) |
| 55 | + if err != nil || got != "token" { |
| 56 | + t.Fatalf("got (%q, %v), want the token from the retry", got, err) |
| 57 | + } |
| 58 | + if calls != 2 || revives != 1 { |
| 59 | + t.Fatalf("%d calls and %d revives, want 2 and 1", calls, revives) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// Anything that is not a missing agent is the agent's answer, and is not |
| 64 | +// second-guessed; nor is a missing agent retried with nothing to revive it. |
| 65 | +func TestWithAgentLeavesOtherFailuresAlone(t *testing.T) { |
| 66 | + revives := 0 |
| 67 | + revive := func() error { revives++; return nil } |
| 68 | + _, err := WithAgent(revive, func() (string, error) { return "", hush.ErrOAuthNotFound }) |
| 69 | + if !errors.Is(err, hush.ErrOAuthNotFound) || revives != 0 { |
| 70 | + t.Fatalf("a real answer was retried (revives=%d, err=%v)", revives, err) |
| 71 | + } |
| 72 | + gone := goneErr(t) |
| 73 | + calls := 0 |
| 74 | + _, err = WithAgent[string](nil, func() (string, error) { calls++; return "", gone }) |
| 75 | + if calls != 1 || !AgentGone(err) { |
| 76 | + t.Fatalf("with no reviver: %d calls, err %v; want one call and the original error", calls, err) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// A revival that fails keeps the original error AND says the agent would not |
| 81 | +// start, so the message for the user can name the vault, not the provider. |
| 82 | +func TestWithAgentReportsAFailedRevival(t *testing.T) { |
| 83 | + gone := goneErr(t) |
| 84 | + _, err := WithAgent(func() error { return errors.New("unlock figaro: keyring locked") }, func() (string, error) { |
| 85 | + return "", gone |
| 86 | + }) |
| 87 | + var rev *ReviveError |
| 88 | + if !errors.As(err, &rev) || !AgentGone(err) { |
| 89 | + t.Fatalf("want both the dial error and a ReviveError, got %v", err) |
| 90 | + } |
| 91 | + if !strings.Contains(err.Error(), "hush agent would not start: unlock figaro: keyring locked") { |
| 92 | + t.Fatalf("message does not name the revival: %v", err) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// The strategy itself, end to end through the real client: the first ask |
| 97 | +// finds no agent, Revive is called, and the error that reaches the caller |
| 98 | +// after a failed revival is the one the hint recognises. |
| 99 | +func TestOAuthRevivesThroughTheRealClient(t *testing.T) { |
| 100 | + revives := 0 |
| 101 | + o := &OAuth{ |
| 102 | + Hush: hush.NewWithSocket(filepath.Join(t.TempDir(), "agent.sock")), |
| 103 | + Name: "copilot", |
| 104 | + Revive: func() error { revives++; return errors.New("no identity") }, |
| 105 | + } |
| 106 | + _, ok, err := o.TryResolve() |
| 107 | + if ok || err == nil { |
| 108 | + t.Fatalf("resolved with no agent: ok=%v err=%v", ok, err) |
| 109 | + } |
| 110 | + if revives != 1 { |
| 111 | + t.Fatalf("the strategy tried to revive %d times, want 1", revives) |
| 112 | + } |
| 113 | + if !strings.Contains(err.Error(), "hush agent would not start") { |
| 114 | + t.Fatalf("the failed revival is not in the error: %v", err) |
| 115 | + } |
| 116 | +} |
0 commit comments