48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package elderagent
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestProductBindingAndRetry(t *testing.T) {
|
||
|
|
bound := false
|
||
|
|
calls := 0
|
||
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
switch r.URL.Query().Get("Action") {
|
||
|
|
case "AibotQuery":
|
||
|
|
if bound {
|
||
|
|
fmt.Fprint(w, `{"Result":{"Bots":[{"Id":"bot-1","Bindings":[{"InstanceId":"instance","ProductKey":"product"}]}]}}`)
|
||
|
|
} else {
|
||
|
|
fmt.Fprint(w, `{"Result":{"Bots":[{"Id":"bot-1"}]}}`)
|
||
|
|
}
|
||
|
|
case "AibotBind":
|
||
|
|
var in map[string]any
|
||
|
|
json.NewDecoder(r.Body).Decode(&in)
|
||
|
|
b, ok := in["Binding"].(map[string]any)
|
||
|
|
if !ok || b["InstanceId"] != "instance" || b["ProductKey"] != "product" || in["Id"] != "bot-1" {
|
||
|
|
t.Error("incorrect AibotBind body")
|
||
|
|
}
|
||
|
|
calls++
|
||
|
|
bound = true
|
||
|
|
fmt.Fprint(w, `{"Result":{}}`)
|
||
|
|
default:
|
||
|
|
t.Error("unexpected action")
|
||
|
|
}
|
||
|
|
}))
|
||
|
|
defer server.Close()
|
||
|
|
client := NewClient("ak", "sk", "", server.URL, "").ForProduct("instance", "product")
|
||
|
|
for n := 0; n < 2; n++ {
|
||
|
|
if err := client.BindConfigured(context.Background(), "bot-1"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if calls != 1 {
|
||
|
|
t.Fatal("binding repeated", calls)
|
||
|
|
}
|
||
|
|
}
|