9e711957c0
Outsiders reading this module should not see private sibling names (tradey), unfinished rewire notes, or /fast/projects lab paths. Keep the library self-contained in README, design, plan, and test fixtures/identity strings.
95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
package rh_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"s1d3sw1ped/robinhood-agentic-mcp"
|
|
"s1d3sw1ped/robinhood-agentic-mcp/auth"
|
|
"s1d3sw1ped/robinhood-agentic-mcp/equity"
|
|
"s1d3sw1ped/robinhood-agentic-mcp/internal/rhntest"
|
|
)
|
|
|
|
func TestRegisteredTools_matchesFixture(t *testing.T) {
|
|
t.Parallel()
|
|
raw, err := os.ReadFile("testdata/tools.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var want []string
|
|
if err := json.Unmarshal(raw, &want); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := rh.RegisteredTools()
|
|
sort.Strings(want)
|
|
sort.Strings(got)
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
t.Fatal(diff)
|
|
}
|
|
}
|
|
|
|
func TestConnect_rpcFallback(t *testing.T) {
|
|
s := rhntest.New(t)
|
|
s.Token = "tok"
|
|
s.Set("get_equity_quotes", json.RawMessage(`{"quotes":[]}`))
|
|
path := filepath.Join(t.TempDir(), "tokens.json")
|
|
if err := auth.WriteTokens(path, "tok", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
api, err := rh.Connect(t.Context(), rh.Config{URL: s.URL, TokenFile: path, Name: "example-app"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if api.Client.HTTP == nil {
|
|
t.Fatal("nil HTTP client")
|
|
}
|
|
if api.Client.HTTP.Timeout != 60*time.Second {
|
|
t.Fatalf("HTTP.Timeout = %v", api.Client.HTTP.Timeout)
|
|
}
|
|
_, err = api.Equity.Quotes(t.Context(), equity.QuotesRequest{Symbols: []string{"MU"}})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestConnect_canceledContext(t *testing.T) {
|
|
t.Parallel()
|
|
path := filepath.Join(t.TempDir(), "tokens.json")
|
|
if err := auth.WriteTokens(path, "tok", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
cancel()
|
|
api, err := rh.Connect(ctx, rh.Config{URL: "http://127.0.0.1:1", TokenFile: path, Name: "example-app"})
|
|
if api != nil {
|
|
t.Fatal("expected nil API")
|
|
}
|
|
if !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("%v", err)
|
|
}
|
|
}
|
|
|
|
func TestConnect_deadlineExceeded(t *testing.T) {
|
|
t.Parallel()
|
|
path := filepath.Join(t.TempDir(), "tokens.json")
|
|
if err := auth.WriteTokens(path, "tok", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithDeadline(t.Context(), time.Now().Add(-time.Second))
|
|
defer cancel()
|
|
api, err := rh.Connect(ctx, rh.Config{URL: "http://127.0.0.1:1", TokenFile: path, Name: "example-app"})
|
|
if api != nil {
|
|
t.Fatal("expected nil API")
|
|
}
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("%v", err)
|
|
}
|
|
}
|