test: AuthManager disconnect resets state

This commit is contained in:
Jay
2026-06-03 16:28:45 -04:00
parent f9d9dfac56
commit e16aa908bf
+38 -5
View File
@@ -3,6 +3,7 @@ package prism
import ( import (
"bytes" "bytes"
"errors" "errors"
"sync"
"testing" "testing"
"git.wisehodl.dev/jay/go-roots-ws" "git.wisehodl.dev/jay/go-roots-ws"
@@ -94,11 +95,43 @@ func TestAuthManager(t *testing.T) {
}) })
t.Run("disconnect resets state", func(t *testing.T) { t.Run("disconnect resets state", func(t *testing.T) {
// p.connect(); feed AUTH "abc"; callback fires obs := &mockObserver{}
// p.disconnect() p, envoy := newMockEnvoy(t, WithEmbassyObserver(obs))
// assert m.challenge == "" (inspect via exported accessor or package-level test access)
// p.connect(); feed AUTH "def" var mu sync.Mutex
// assert callback fires again with "def" var calls []string
callback := func(challenge string) ([]byte, error) {
mu.Lock()
calls = append(calls, challenge)
mu.Unlock()
return []byte(`{"kind":22242}`), nil
}
m := NewAuthManager(envoy, callback)
t.Cleanup(m.Close)
p.connect()
p.receive([]byte(envelope.EncloseAuthChallenge("abc")))
Eventually(t, func() bool {
mu.Lock()
defer mu.Unlock()
return len(calls) == 1 && calls[0] == "abc"
}, "first callback not fired")
p.disconnect()
// drain the sent channel so p.sent doesn't block the second send
select {
case <-p.sent:
default:
}
p.connect()
p.receive([]byte(envelope.EncloseAuthChallenge("def")))
Eventually(t, func() bool {
mu.Lock()
defer mu.Unlock()
return len(calls) == 2 && calls[1] == "def"
}, "second callback not fired with new challenge")
}) })
t.Run("replacement challenge", func(t *testing.T) { t.Run("replacement challenge", func(t *testing.T) {