package plugin import ( "context" "encoding/json" "errors" "net" "testing" "time" "github.com/Nwflower/reims-go/internal/plugin/sidecarproc" "github.com/Nwflower/reims-go/pkg/pluginproto" ) // TestSupervisorHostMethodsAreCatalogued keeps the methods the supervisor // serves itself inside the wire contract. A method that is not catalogued is // not part of the contract, so a plugin must never be able to call it. func TestSupervisorHostMethodsAreCatalogued(t *testing.T) { for _, method := range ReverseHostAPIMethods() { spec, ok := pluginproto.Lookup(method) if !ok { t.Errorf("%s is served but not in the contract catalog", method) continue } if spec.Status != pluginproto.StatusStable { t.Errorf("%s is served but marked %q", method, spec.Status) } } } // TestHandlerErrorKeepsItsCode checks that a handler that names a JSON-RPC // code keeps it, so a rejected request stays distinguishable from a failed // one on the plugin side. func TestHandlerErrorKeepsItsCode(t *testing.T) { hostRead, pluginWrite := net.Pipe() pluginRead, hostWrite := net.Pipe() host := sidecarproc.NewRPCConn(hostRead, hostWrite) plugin := sidecarproc.NewRPCConn(pluginRead, pluginWrite) t.Cleanup(func() { host.Close() plugin.Close() }) host.Handle("host/console.key", func(context.Context, json.RawMessage) (any, error) { return nil, &RPCError{Code: RPCInvalidParams, Message: "unknown field"} }) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() err := plugin.Call(ctx, "host/console.key", map[string]any{"key": "a"}, nil) var rpcErr *RPCError if !errors.As(err, &rpcErr) { t.Fatalf("err = %v, want *RPCError", err) } if rpcErr.Code != RPCInvalidParams { t.Fatalf("code = %d, want %d", rpcErr.Code, RPCInvalidParams) } } // TestHostFeaturesAdvertiseEveryStableHostMethod checks that a plugin can // feature-detect each stable host method from the handshake. func TestHostFeaturesAdvertiseEveryStableHostMethod(t *testing.T) { advertised := map[string]bool{} for _, feature := range HostFeatures() { advertised[feature] = true } for _, method := range pluginproto.Methods() { spec, _ := pluginproto.Lookup(method) if spec.Status != pluginproto.StatusStable { continue } if !advertised[pluginproto.Feature(method)] { t.Errorf("%s is stable but its feature %q is not advertised", method, pluginproto.Feature(method)) } } }