Files
Reims-Go/internal/plugin/host_contract_test.go
T
千川白浪 d55241cba2 feat(plugin): catalog the Host API, open secret typing and extend the SDK
Give the Sidecar Host API one authoritative catalog in pkg/pluginproto: every
host/* method's status, required permissions, handshake feature and request and
result shapes. The host, pkg/pluginsdk and plugins share those types, so a field
can no longer exist on one side only, and permissions come from the catalog
instead of from each handler. Tests gate that every served method is
catalogued, every stable catalogued method is served, and every catalogued
permission is one the host recognizes.

Open the interface up instead of adding consent gates. Typing a secret setting
is no longer reserved for reims.agent: any plugin that holds qmp:write and that
the user authorized for the VM can have the host type the plugin's own setting,
which the plugin never reads back. Restoring a snapshot works on any authorized
VM, a plugin names its own clones, a held key may carry modifiers, and a
screenshot width outside the supported range is brought into range rather than
refused. Deleting a VM stays limited to the temporary VMs a plugin created.

Record the keys and pointer buttons each plugin holds per VM and lift them when
the plugin's process exits, its scope is disposed, the VM stops or it stops
calling, so a guest never stays stuck with a modifier down. Requests from
plugins that declare pluginApi 1.3 or newer are decoded strictly and answer an
undefined field with JSON-RPC -32602, while older plugins keep tolerant
decoding.

Add typed VM and console methods to pkg/pluginsdk so a plugin no longer needs
its own JSON-RPC client. A method the contract does not catalog, or one the
host did not advertise in the handshake, is refused before it is sent, and the
host's JSON-RPC error code reaches the caller.
2026-12-05 01:43:28 -07:00

75 lines
2.3 KiB
Go

package plugin
import (
"context"
"encoding/json"
"errors"
"net"
"testing"
"time"
"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 := NewRPCConn(hostRead, hostWrite)
plugin := 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))
}
}
}