package server import ( "context" "encoding/json" "git.a-wan.net/alan/pipelines/internal/client" "git.a-wan.net/alan/pipelines/internal/protocol" "net/http/httptest" "strings" "testing" "time" ) func TestStatusRPCReportsServerWithoutNtfy(t *testing.T) { srv := httptest.NewServer(Handler(context.Background())) defer srv.Close() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() c, err := client.Open(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect") if err != nil { t.Fatal(err) } defer c.Close() response, err := c.Call(ctx, protocol.Message{Type: protocol.GetStatus}) if err != nil { t.Fatal(err) } s := response.Status if s == nil || s.Server.Health != "healthy" || len(s.Addresses) == 0 || s.Hostname == "" || s.Server.Port == "" || s.StartedAt == "" { t.Fatal("incomplete status", s) } encoded, _ := json.Marshal(s) for _, gone := range []string{`"ntfy"`, `"topic"`, `"ntfy_user"`} { if strings.Contains(string(encoded), gone) { t.Fatalf("status still reports %s: %s", gone, encoded) } } if !strings.Contains(s.Server.URL, s.Server.Port) || !strings.Contains(s.Note, "not reachability checks") { t.Fatal("ports or safety disclaimer missing", s) } } // TestStatusReportsOpenCodeAttachment pins that get_status carries how chat is // attached to OpenCode, so an endpoint that could not be attached shows as // "fallback" instead of silently disappearing, and that the report never // contains a credential. func TestStatusReportsOpenCodeAttachment(t *testing.T) { options := Options{OpenCodeStatus: protocol.ServiceStatus{Health: "fallback", URL: "http://127.0.0.1:49374"}} srv := httptest.NewServer(HandlerWithOptions(context.Background(), options)) defer srv.Close() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() c, err := client.Open(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect") if err != nil { t.Fatal(err) } defer c.Close() response, err := c.Call(ctx, protocol.Message{Type: protocol.GetStatus}) if err != nil { t.Fatal(err) } if response.Status == nil || response.Status.OpenCode.Health != "fallback" || response.Status.OpenCode.URL != "http://127.0.0.1:49374" { t.Fatalf("opencode status not reported: %+v", response.Status) } encoded, _ := json.Marshal(response.Status) if strings.Contains(string(encoded), "password") || strings.Contains(string(encoded), "token") { t.Fatalf("status leaked a credential-shaped field: %s", encoded) } } func TestStatusIPv6Candidates(t *testing.T) { addresses := []protocol.NetworkAddress{{Interface: "test", IP: "::1"}, {Interface: "test", IP: "192.0.2.1"}} urls := candidateURLs(addresses, "ws", "8080", "/connect") if urls[0] != "ws://[::1]:8080/connect" || urls[1] != "ws://192.0.2.1:8080/connect" { t.Fatal(urls) } }