Files
WeKnora/pluginsdk/pluginapi/pluginapi_test.go
T
lyingbug 4328ab26a9 feat(plugin): code plugins — extension protocol, Go SDK, plugin host, web search and data source adapters (#3720)
* feat(pluginsdk): extension protocol v1, Go SDK and conformance suite
* feat(plugin): run code plugins in an embedded plugin host
* feat(plugin): remote adapters for web search and data sources
* fix(plugin): show plugin errors by their message
* feat(frontend): plugin connectors and web search providers in the UI
* docs(pluginsdk): how to write, package, test and install a plugin
* fix(plugin): gofmt two files and read openapi.yaml line-ending agnostic
* fix(plugin): mark a host plugin ready before Activate returns
2026-09-26 14:45:15 +08:00

49 lines
1.4 KiB
Go

package pluginapi
import (
"strconv"
"testing"
"time"
)
func TestHandshake(t *testing.T) {
h := Handshake{Protocol: ProtocolVersion, Network: "unix", Address: "/tmp/p.sock"}
got, ok, err := ParseHandshake(h.String() + "\n")
if !ok || err != nil || got != h {
t.Fatalf("round trip = %+v %v %v", got, ok, err)
}
if _, ok, _ := ParseHandshake("starting up..."); ok {
t.Fatal("other output is not a handshake")
}
bad := []string{
"WEKNORA_PLUGIN|2|unix|/x", "WEKNORA_PLUGIN|1|pipe|/x", "WEKNORA_PLUGIN|1|unix|", "WEKNORA_PLUGIN|1",
}
for _, bad := range bad {
if _, ok, err := ParseHandshake(bad); !ok || err == nil {
t.Errorf("%q must be a rejected handshake", bad)
}
}
}
func TestSignature(t *testing.T) {
secret, body, now := []byte("k"), []byte(`{"a":1}`), time.Now()
ts := strconv.FormatInt(now.Unix(), 10)
sig := Sign(secret, now.Unix(), body)
if err := VerifySignature(secret, ts, sig, body, now); err != nil {
t.Fatal(err)
}
if VerifySignature(secret, ts, sig, []byte(`{"a":2}`), now) == nil {
t.Fatal("a changed body must fail")
}
if VerifySignature(secret, ts, sig, body, now.Add(10*time.Minute)) == nil {
t.Fatal("a replayed old request must fail")
}
}
func TestErrorStatus(t *testing.T) {
if Errorf(CodeRateLimited, "x").Code.HTTPStatus() != 429 || !Errorf(CodeUnavailable, "x").Retryable ||
Errorf(CodeInternal, "x").Retryable {
t.Fatal("error codes map wrong")
}
}