51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
//go:build windows
|
|
|
|
package control
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Microsoft/go-winio"
|
|
)
|
|
|
|
func TestNamedPipeForwardsDeclaredPluginMessage(t *testing.T) {
|
|
pipeName := fmt.Sprintf(`\\.\pipe\reims-go-control-test-%d`, time.Now().UnixNano())
|
|
server, err := ListenAt(pipeName, func(_ context.Context, pluginID, channel string, message json.RawMessage) (json.RawMessage, error) {
|
|
if pluginID != "test.agent" || channel != "mcp" {
|
|
t.Fatalf("unexpected channel target %s/%s", pluginID, channel)
|
|
}
|
|
return message, nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = server.Close() })
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
conn, err := winio.DialPipeContext(ctx, pipeName)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
if _, err := fmt.Fprintln(conn, `{"plugin_id":"test.agent","channel":"mcp","message":{"jsonrpc":"2.0","id":1,"method":"ping"}}`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
response, err := bufio.NewReader(conn).ReadBytes('\n')
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var result Response
|
|
if err := json.Unmarshal(response, &result); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(result.Message) != `{"jsonrpc":"2.0","id":1,"method":"ping"}` || result.Error != "" {
|
|
t.Fatalf("unexpected host response: %+v", result)
|
|
}
|
|
}
|