TestPiPackage added testdata/pi, and TestGatewayHookMixedPiPackage a copy in a TempDir. Neither folder has a node_modules above it, so as the host started, Bun took each import of pi from there as one to install itself (its auto-install): first pi.js's resolve of @earendil-works/pi-coding-agent from the package's folder, then the extension's own imports, which jiti 2.7 imports natively under Bun (tryNative) from the extension's folder. Each start asked npm for 115 manifests, and init waited on them under its 2 minute bound. This would explain the red yetone sees under a full run, and with npm slow it is red alone. The host then ran npm's latest pi (1.4.0 today), not the 1.2.0 installPinnedPi installs from testdata/pi-lock, so #1879's pin never reached the host. The test's Version(piAgent) check reads the plugins folder's copy, so it passed. Now both tests put their package in the plugins folder (Dir()/fake-pi-package, Dir()/mixed-pi-package), beside the node_modules installPinnedPi fills. pi.js and the extension both resolve the pinned pi, and nothing is asked of npm as the host starts. fakepi.ts tells the VERSION its import of pi reached, and TestPiPackage checks it is the pinned one. With the old layout and npm reachable, that check is red. Not changed: the app. A pi package added from a folder with no node_modules above it should go the same way (the tests add theirs through the same Add, as a path): pi.js and its extensions get npm's latest from Bun as the host starts, and the pi ensurePi put in the plugins folder for it isn't the one that runs. Making pi.js resolve from the plugins folder alone isn't enough. Tried here with the old layout, the host then waited on @earendil-works/pi-ai's manifest for the extension's import instead, and both tests were still red. Each way out changes how plugins load: JITI_TRY_NATIVE=0 in pi.js (every pi extension then goes through jiti's transform and pi's aliases), `bun run --no-install` for the host (auto-install also stops for OpenCode plugins on disk), or a Bun.plugin onResolve for pi's scopes. That is the maintainer's call, so it isn't in this commit. Also not changed: installPinnedPi's `bun install --frozen-lockfile` still fetches pi's 118 tarballs on every run, as the sandbox's install cache starts empty, and the test skips when that fails (noted in #2865). What was run (tree 8aee6487, on origin/mainacdc85ac, go1.26.8, linux/amd64, Linux 5.15, umask 077). This tree only rewords TestPiPackage's version check message from tree 859ae995, where the rest was run. - At this tree (Bun 1.4.2, npm direct): TestPiPackage and TestGatewayHookMixedPiPackage pass. Broken by hand (TestPiPackage's package back in testdata/pi): red, `the extension's import reached pi 1.4.0, not the 1.2.0 the lock installed`. At 859ae995: - A local npm relay as BUN_CONFIG_REGISTRY (Bun 1.4.2) that passes tarballs through at once and holds each manifest 150s, with `go test -tags nogui -count=1 -v -run 'TestPiPackage$|TestGatewayHookMixedPiPackage$' ./internal/plugin/`, load 10 to 16: - origin/mainacdc85ac: both red. `mixed package lost Pi providers: [] starting plugins: context deadline exceeded` (157.92s), and `Plugins = [], starting plugins: context deadline exceeded` (157.69s). Each test's host asked the relay for @earendil-works/pi-coding-agent's manifest as it started. - this tree: both pass (29.35s, and 181.28s, most of it the frozen install's tarballs through the relay), and the relay was asked for no manifest. With a print-only probe around the first Providers or Plugins call, the host started in 0.5s in each test. - main's layout with pi.js resolving pi only where a node_modules above the folder has it (tried, not kept): both still red (host start 122.0s and 104.8s), the relay asked for @earendil-works/pi-ai's manifest by the extension's import. - Broken by hand: both packages back where main puts them (testdata/pi and a TempDir), the version check kept, the relay passing manifests through at once. TestPiPackage red on the version check (pi 1.4.0, not 1.2.0). The mixed test passed with a 60.7s host start (TestPiPackage's took 27.8s). 230 manifest requests: 115 packages, once per host start. - go test -tags nogui -count=1 -timeout 45m ./internal/plugin/ (Bun 1.4.2, npm direct): ok, 340 PASS lines counting subtests, 2 skip (TestFlowBigReplyProd wants MAGPIE_FLOW_BIG, TestRenewOtherHost is run by TestRenewOnceAcrossHosts). TestPiPackage 10.49s, TestGatewayHookMixedPiPackage 22.77s. - The same with -race: ok, the same counts, no race reported. - gofmt -l clean. go vet -tags nogui for linux, windows and darwin, go vet for windows, go build -tags nogui for linux and darwin and go build for windows: exit 0 each. go vet and go build without the tag on linux: exit 1 (no libsoup-3.0 or gio-unix-2.0 here). - go test -tags nogui -count=1 -timeout 45m ./... (Bun 1.4.2; 9m49s, load 16 at its start): 79 packages ok, internal/plugin among them, and 8 tests red in 2 packages, each filed: agent TestInstalledVersion, TestCursorFolderFromTheEditorIsNotTheCLI, TestCopilotFolderFromAnIDEPluginIsNotTheCLI, TestQoderSettingsFromAnIDEPluginIsNotTheCLI, TestOmpDetectedWithPiDir and TestZedAppPath (#3141); gui TestGatewayHeldByAContainer and TestGatewayFixQuitsOnlyAMagpie (#3649). Run by name at origin/mainacdc85ac, all are red there too but the Qoder test, which passed alone. Not tried: the real app, and macOS and Windows (the tests ran on Linux only). Reviewed and run again by the maintainer at the stack's tree 45994757c (this commit and the six beside it, on origin/main90518de26), go1.26.8: - mote (macOS, arm64): gofmt -l clean; go vet and go build for darwin, GOOS=linux -tags nogui and GOOS=windows (library, plugin, gui): exit 0. go test -tags nogui -count=1 -timeout 45m ./...: every package ok but internal/gui TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and internal/proc TestNoCommandBypassesProc (#4491), both red on main fromb09349d17andc59c547c5, in files this stack doesn't touch. go test -race -run 'TestWSL|TestTagged|TestPinnedPiCache' for library and plugin: ok. - magpie-desktop (Linux, wsl-run; WebKit hangs on mote), Chromium and WebKit: every library-*, sync-*, s3-sync, github-sync and i18n-keys file, 4 at a time: 1003 of 1020. The 17 reds are in 6 files no commit here touches: i18n-keys ("Unmarked", #4524), library-chip-fan, -items, -mcp-health, -mcp-hub (#4483) and -mcp-tools. Those 6 alone, one at a time, at load ~60: 74 of 92 here, 77 of 92 at90518de26itself. Red in both: i18n-keys, -chip-fan and -items in WebKit, -mcp-tools in Chromium ja 420; -mcp-health green in both; -mcp-hub's WebKit case red here only, the case #4483 has red on main. -chip-fan and -items are #3072's and #4501's kind, WebKit under load. - Broken by hand on mote: TestPiPackage's package back at testdata/pi is red, "the extension's import reached pi 1.4.0, not the 1.2.0 the lock installed".
541 lines
19 KiB
Go
541 lines
19 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/filememo"
|
|
)
|
|
|
|
func gatewayFixture(t *testing.T, source string) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"magpie":{"hooks":"./request.js"}}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "request.js"), []byte(source), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Add(t.Context(), dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return dir
|
|
}
|
|
|
|
// TestHasGatewayLooksOnceASecond: every request asks HasGateway, so it
|
|
// reads the plugins at most once a second (#3725), and at once after magpie
|
|
// wrote a file of its own.
|
|
func TestHasGatewayLooksOnceASecond(t *testing.T) {
|
|
sandbox(t)
|
|
dir := gatewayFixture(t, `export function onRequest(body){return body}`)
|
|
if !HasGateway() {
|
|
t.Fatal("a plugin with hooks has no gateway")
|
|
}
|
|
pkg := filepath.Join(dir, "package.json")
|
|
if err := os.WriteFile(pkg, []byte(`{"name":"x"}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !HasGateway() {
|
|
t.Fatal("the plugins were read again within the second")
|
|
}
|
|
filememo.Forget()
|
|
if HasGateway() {
|
|
t.Fatal("magpie's own write wasn't looked at")
|
|
}
|
|
if err := os.WriteFile(pkg, []byte(`{"magpie":{"hooks":"./request.js"}}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// a second later: the look is due
|
|
c := *hasGateway.Load()
|
|
c.until = time.Now().UnixNano()
|
|
hasGateway.Store(&c)
|
|
if !HasGateway() {
|
|
t.Fatal("another program's change wasn't seen a second later")
|
|
}
|
|
}
|
|
|
|
func TestGatewayHookWaitsForStartingHost(t *testing.T) {
|
|
sandbox(t)
|
|
gatewayFixture(t, `export function onRequest(body){return {...body,note:"recalled"}}`)
|
|
// Provider discovery holds this lock while starting the shared host.
|
|
hostMu.Lock()
|
|
locked := true
|
|
defer func() {
|
|
if locked {
|
|
hostMu.Unlock()
|
|
}
|
|
}()
|
|
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
|
defer cancel()
|
|
done := make(chan *GatewayRun, 1)
|
|
go func() { done <- GatewayBegin(ctx, []byte(`{"note":"original"}`), GatewayContext{}) }()
|
|
select {
|
|
case run := <-done:
|
|
if run != nil {
|
|
run.Close()
|
|
}
|
|
t.Fatal("first request skipped the starting host")
|
|
case <-time.After(50 * time.Millisecond):
|
|
}
|
|
hostMu.Unlock()
|
|
locked = false
|
|
select {
|
|
case run := <-done:
|
|
if run == nil {
|
|
t.Fatal("first request did not run its recall hook")
|
|
}
|
|
defer run.Close()
|
|
if !strings.Contains(string(run.Body), `"recalled"`) {
|
|
t.Fatalf("first request was not recalled: %s", run.Body)
|
|
}
|
|
case <-ctx.Done():
|
|
t.Fatal("first request did not resume after host startup")
|
|
}
|
|
}
|
|
|
|
func TestGatewayHookHostWaitCancellation(t *testing.T) {
|
|
sandbox(t)
|
|
gatewayFixture(t, `export function onRequest(body){return {...body,note:"recalled"}}`)
|
|
hostMu.Lock()
|
|
defer hostMu.Unlock()
|
|
ctx, cancel := context.WithTimeout(t.Context(), 50*time.Millisecond)
|
|
defer cancel()
|
|
body := []byte(`{"note":"original"}`)
|
|
if got := gatewayRequest(ctx, body, GatewayContext{}); string(got) != string(body) {
|
|
t.Fatalf("cancelled startup changed the original request: %s", got)
|
|
}
|
|
if ctx.Err() == nil {
|
|
t.Fatal("request skipped host startup instead of waiting for its deadline")
|
|
}
|
|
}
|
|
|
|
func TestGatewayRunStateOwnershipAndAsyncResponse(t *testing.T) {
|
|
sandbox(t)
|
|
started, release := make(chan struct{}, 2), make(chan struct{}, 2)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
started <- struct{}{}
|
|
select {
|
|
case <-release:
|
|
case <-r.Context().Done():
|
|
}
|
|
}))
|
|
defer func() { close(release); server.Close() }()
|
|
output := filepath.Join(t.TempDir(), "responses.jsonl")
|
|
fixture := gatewayFixture(t, `import fs from "node:fs";
|
|
export function onRequest(body,ctx) {
|
|
ctx.state.original=body;ctx.state.value=()=>ctx.session;
|
|
return {...body,note:"recalled"};
|
|
}
|
|
export async function onResponse(reply,ctx) {
|
|
await fetch(ctx.options.url,{signal:ctx.signal});
|
|
fs.appendFileSync(ctx.options.output,JSON.stringify({reply,request:ctx.request,success:ctx.success,session:ctx.state.value(),original:ctx.state.original})+"\n");
|
|
}`)
|
|
if err := SetOptions(fixture, map[string]any{"output": output, "url": server.URL, "gatewayTimeoutMs": 5000}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
first := GatewayBegin(ctx, []byte(`{"note":"original"}`), GatewayContext{Session: "first"})
|
|
second := GatewayBegin(t.Context(), []byte(`{"note":"original"}`), GatewayContext{Session: "second"})
|
|
if first == nil || second == nil || !strings.Contains(string(first.Body), `"recalled"`) {
|
|
t.Fatal("request lifecycle did not begin")
|
|
}
|
|
t.Cleanup(first.Close)
|
|
t.Cleanup(second.Close)
|
|
first.Finish(ctx, json.RawMessage(`{"answer":"first"}`), true, first.Body)
|
|
cancel() // Completed notifications outlive the HTTP request context.
|
|
first.Finish(t.Context(), json.RawMessage(`{"duplicate":true}`), true)
|
|
second.Finish(t.Context(), json.RawMessage(`{"answer":"second"}`), true, second.Body)
|
|
for range 2 {
|
|
select {
|
|
case <-started:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("Finish blocked or notification was cancelled with client request")
|
|
}
|
|
}
|
|
release <- struct{}{}
|
|
release <- struct{}{}
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for {
|
|
b, _ := os.ReadFile(output)
|
|
if strings.Count(string(b), "\n") == 2 {
|
|
if strings.Contains(string(b), "duplicate") || !strings.Contains(string(b), `"session":"first"`) || !strings.Contains(string(b), `"session":"second"`) || !strings.Contains(string(b), `"note":"original"`) || !strings.Contains(string(b), `"success":true`) {
|
|
t.Fatalf("completion state crossed requests: %s", b)
|
|
}
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("notifications did not finish: %s", b)
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func TestGatewayHookCannotRegisterTools(t *testing.T) {
|
|
for _, source := range []string{
|
|
`export function onRequest(body,ctx){ctx.addTools([]);return {...body,note:"corrupt"}}`,
|
|
`export function onRequest(body){return {...body,tools:[{type:"function",function:{name:"recall"}}]}}`,
|
|
} {
|
|
t.Run(source, func(t *testing.T) {
|
|
sandbox(t)
|
|
gatewayFixture(t, source)
|
|
body := []byte(`{"note":"original","tools":[]}`)
|
|
if out := gatewayRequest(t.Context(), body, GatewayContext{}); !strings.Contains(string(out), `"original"`) || strings.Contains(string(out), "recall") {
|
|
t.Fatalf("context hook changed client tools: %s", out)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The gateway checked the caller's key against the model and answers in
|
|
// the streaming the client asked for: a hook that changes either is rolled
|
|
// back, while one that adds context alone is kept.
|
|
func TestGatewayHookCannotChangeModelOrStream(t *testing.T) {
|
|
body := []byte(`{"model":"original","stream":true,"messages":[]}`)
|
|
for _, tc := range []struct{ source, want string }{
|
|
{`export function onRequest(body){return {...body,model:"other"}}`, `{"model":"original","stream":true,"messages":[]}`},
|
|
{`export function onRequest(body){return {...body,stream:false}}`, `{"model":"original","stream":true,"messages":[]}`},
|
|
{`export function onRequest(body){const {stream, ...rest} = body; return rest}`, `{"model":"original","stream":true,"messages":[]}`},
|
|
{`export function onRequest(body){return {...body,messages:[{role:"system",content:"recalled"}]}}`, `{"model":"original","stream":true,"messages":[{"role":"system","content":"recalled"}]}`},
|
|
} {
|
|
t.Run(tc.source, func(t *testing.T) {
|
|
sandbox(t)
|
|
gatewayFixture(t, tc.source)
|
|
var got, want any
|
|
json.Unmarshal(gatewayRequest(t.Context(), body, GatewayContext{}), &got)
|
|
json.Unmarshal([]byte(tc.want), &want)
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got %v, want %s", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGatewayRunCapacityAndRelease(t *testing.T) {
|
|
sandbox(t)
|
|
gatewayFixture(t, `export function onRequest(body){return body}`)
|
|
runs := make([]*GatewayRun, 0, 128)
|
|
t.Cleanup(func() {
|
|
for _, run := range runs {
|
|
run.Close()
|
|
}
|
|
})
|
|
for range 128 {
|
|
run := GatewayBegin(t.Context(), []byte(`{}`), GatewayContext{})
|
|
if run == nil {
|
|
t.Fatalf("run capacity exhausted at %d", len(runs))
|
|
}
|
|
runs = append(runs, run)
|
|
}
|
|
if run := GatewayBegin(t.Context(), []byte(`{}`), GatewayContext{}); run != nil {
|
|
run.Close()
|
|
t.Fatal("host retained more than its bounded request capacity")
|
|
}
|
|
runs[0].Close()
|
|
run := GatewayBegin(t.Context(), []byte(`{}`), GatewayContext{})
|
|
if run == nil {
|
|
t.Fatal("closed run did not release capacity")
|
|
}
|
|
defer run.Close()
|
|
}
|
|
|
|
func TestGatewayHookChainRollbackAndOptions(t *testing.T) {
|
|
sandbox(t)
|
|
first := gatewayFixture(t, `export async function onRequest(body, ctx) { await Promise.resolve(); body.note = ctx.options.note; return body }`)
|
|
if err := SetOptions(first, map[string]any{"note": "first"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gatewayFixture(t, `export async function onRequest(body) { body.note = "corrupt"; throw new Error("private token must not be logged") }`)
|
|
gatewayFixture(t, `export async function onRequest(body) { body.note += "-last"; return body }`)
|
|
loaded, err := Plugins(t.Context())
|
|
if err != nil || len(loaded) != 3 {
|
|
t.Fatalf("loaded: %+v %v", loaded, err)
|
|
}
|
|
for _, p := range loaded {
|
|
if p.Error != "" {
|
|
t.Fatalf("load: %+v", p)
|
|
}
|
|
}
|
|
out := gatewayRequest(t.Context(), []byte(`{"note":"original","messages":[]}`), GatewayContext{Protocol: "chat"})
|
|
var got struct{ Note string }
|
|
if err := json.Unmarshal(out, &got); err != nil || got.Note != "first-last" {
|
|
t.Fatalf("chain did not preserve successful hooks and undo failed mutation: %s (%v)", out, err)
|
|
}
|
|
if err := SetOff(first, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out = gatewayRequest(t.Context(), []byte(`{"note":"original"}`), GatewayContext{})
|
|
if !strings.Contains(string(out), "original-last") {
|
|
t.Fatalf("disabled hook ran: %s", out)
|
|
}
|
|
}
|
|
|
|
func TestGatewayHookMixedProviderPackage(t *testing.T) {
|
|
sandbox(t)
|
|
dir := gatewayFixture(t, `export function onRequest(body) { body.note="recalled";return body }`)
|
|
if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"type":"module","main":"./provider.js","magpie":{"hooks":"./request.js"}}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "provider.js"), []byte(`export default async function(){return {auth:{provider:"fixture",methods:[]},config(config){config.provider.fixture={name:"Gateway fixture",models:{m1:{name:"m1"}}}}}}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
providers, err := Providers(t.Context())
|
|
if err != nil || len(providers) != 1 || providers[0].ID != "fixture" {
|
|
t.Fatalf("mixed package lost its provider: %+v %v", providers, err)
|
|
}
|
|
out := gatewayRequest(t.Context(), []byte(`{"note":"original"}`), GatewayContext{})
|
|
if !strings.Contains(string(out), "recalled") {
|
|
t.Fatalf("mixed package lost its gateway hook: %s", out)
|
|
}
|
|
}
|
|
|
|
func TestGatewayHookMixedPiPackage(t *testing.T) {
|
|
sandbox(t)
|
|
ctx, cancel := context.WithTimeout(t.Context(), 4*time.Minute)
|
|
defer cancel()
|
|
installPinnedPi(t, ctx)
|
|
dir := filepath.Join(Dir(), "mixed-pi-package") // where pi resolves to the pinned one (installPinnedPi)
|
|
if err := os.MkdirAll(filepath.Join(dir, "extensions"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, file := range []string{"package.json", "extensions/fakepi.ts"} {
|
|
b, err := os.ReadFile(filepath.Join("testdata", "pi", file))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if file == "package.json" {
|
|
var pkg map[string]any
|
|
if err := json.Unmarshal(b, &pkg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pkg["magpie"] = map[string]string{"hooks": "./request.js"}
|
|
b = mustJSON(t, pkg)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, filepath.FromSlash(file)), b, 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "request.js"), []byte(`export function onRequest(body){body.note="recalled";return body}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Add(ctx, dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
providers, err := Providers(ctx)
|
|
if err != nil || len(providers) != 2 {
|
|
t.Fatalf("mixed package lost Pi providers: %+v %v", providers, err)
|
|
}
|
|
out := gatewayRequest(t.Context(), []byte(`{"note":"original"}`), GatewayContext{})
|
|
if !strings.Contains(string(out), "recalled") {
|
|
t.Fatalf("mixed Pi package lost gateway hook: %s", out)
|
|
}
|
|
}
|
|
|
|
func TestGatewayHookTimeoutCancelsHTTPAndKeepsBody(t *testing.T) {
|
|
sandbox(t)
|
|
started, stopped := make(chan struct{}), make(chan struct{})
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
close(started)
|
|
<-r.Context().Done()
|
|
close(stopped)
|
|
}))
|
|
defer srv.Close()
|
|
source := `export async function onRequest(body, ctx) { body.note="corrupt"; await fetch(` + string(mustJSON(t, srv.URL)) + `,{signal:ctx.signal}); return body }`
|
|
gatewayFixture(t, source)
|
|
if _, err := Plugins(t.Context()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
original := []byte(`{"note":"original"}`)
|
|
out := gatewayRequest(t.Context(), original, GatewayContext{})
|
|
if string(out) != string(original) {
|
|
t.Fatalf("timeout changed request: %s", out)
|
|
}
|
|
select {
|
|
case <-started:
|
|
default:
|
|
t.Fatal("hook never reached HTTP")
|
|
}
|
|
select {
|
|
case <-stopped:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("timed-out hook did not cancel HTTP")
|
|
}
|
|
}
|
|
|
|
func TestGatewayHookCallerCancellation(t *testing.T) {
|
|
sandbox(t)
|
|
started, stopped := make(chan struct{}), make(chan struct{})
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
close(started)
|
|
<-r.Context().Done()
|
|
close(stopped)
|
|
}))
|
|
defer srv.Close()
|
|
gatewayFixture(t, `export async function onRequest(body,ctx) { await fetch(`+string(mustJSON(t, srv.URL))+`,{signal:ctx.signal}); return body }`)
|
|
if _, err := Plugins(t.Context()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
defer cancel()
|
|
done := make(chan []byte, 1)
|
|
go func() { done <- gatewayRequest(ctx, []byte(`{"note":"original"}`), GatewayContext{}) }()
|
|
select {
|
|
case <-started:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("hook never started")
|
|
}
|
|
cancel()
|
|
select {
|
|
case out := <-done:
|
|
if string(out) != `{"note":"original"}` {
|
|
t.Fatalf("cancel changed body: %s", out)
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("caller cancellation did not return")
|
|
}
|
|
select {
|
|
case <-stopped:
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("caller cancellation did not stop host HTTP")
|
|
}
|
|
// The canceled RPC releases its capacity; another RPC can still finish.
|
|
var out any
|
|
if err := Call(t.Context(), "providers", map[string]any{}, &out); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func mustJSON(t *testing.T, v any) []byte {
|
|
t.Helper()
|
|
b, err := json.Marshal(v)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
func TestGatewayDiagnosticsAndExtendedBudget(t *testing.T) {
|
|
sandbox(t)
|
|
spec := gatewayFixture(t, `export async function onRequest(body,ctx) {
|
|
await new Promise(resolve=>setTimeout(resolve,1100));
|
|
ctx.report("private secret query", 99);
|
|
ctx.report("ready", 12);
|
|
body.note="recalled"; return body
|
|
}`)
|
|
if err := SetOptions(spec, map[string]any{"gatewayTimeoutMs": 2000}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out := gatewayRequest(t.Context(), []byte(`{"note":"original"}`), GatewayContext{Query: "private secret query"})
|
|
if !strings.Contains(string(out), "recalled") {
|
|
t.Fatalf("configured recall budget ignored: %s", out)
|
|
}
|
|
state := GatewayStates(t.Context())[spec]
|
|
if state.Calls != 1 || state.Failures != 0 || state.LastOutcome != "ready" || state.LastBytes != 12 || state.AvgMillis < 1000 {
|
|
t.Fatalf("diagnostics: %+v", state)
|
|
}
|
|
if strings.Contains(string(mustJSON(t, state)), "private") {
|
|
t.Fatal("diagnostics retained private data")
|
|
}
|
|
}
|
|
|
|
// A malformed result and an undefined result both keep original bytes.
|
|
func TestGatewayHookInvalidOrUndefinedResult(t *testing.T) {
|
|
for _, result := range []string{"null", "[]", "undefined", "{toJSON(){return null}}"} {
|
|
t.Run(result, func(t *testing.T) {
|
|
sandbox(t)
|
|
gatewayFixture(t, `export function onRequest(body){body.note="corrupt";return `+result+`}`)
|
|
out := gatewayRequest(t.Context(), []byte(`{"note":"original"}`), GatewayContext{})
|
|
if !strings.Contains(string(out), `"original"`) {
|
|
t.Fatalf("invalid return changed request: %s", out)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Bun loads TypeScript itself: a direct *.hooks.ts file and a package
|
|
// whose magpie.hooks names a .ts entry both run without a build step.
|
|
func TestGatewayHookTypeScript(t *testing.T) {
|
|
sandbox(t)
|
|
direct := filepath.Join(t.TempDir(), "recall.hooks.ts")
|
|
if err := os.WriteFile(direct, []byte(`type Body = { note: string }
|
|
export async function onRequest(body: Body): Promise<Body> { body.note += "-direct"; return body }`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if Gateway(direct) == "" {
|
|
t.Fatal("direct .hooks.ts file not recognised")
|
|
}
|
|
if _, err := Add(t.Context(), direct); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"magpie":{"hooks":"./src/request.ts"}}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.MkdirAll(filepath.Join(dir, "src"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "src", "request.ts"), []byte(`interface Body { note: string }
|
|
export function onRequest(body: Body): Body { body.note += "-package"; return body }`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Add(t.Context(), dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out := gatewayRequest(t.Context(), []byte(`{"note":"m"}`), GatewayContext{Protocol: "chat"})
|
|
if !strings.Contains(string(out), `"m-direct-package"`) {
|
|
t.Fatalf("TypeScript hooks did not run in order: %s", out)
|
|
}
|
|
}
|
|
|
|
// gatewayRequest is a hook run on body alone: the body it leaves.
|
|
func gatewayRequest(ctx context.Context, body []byte, info GatewayContext) []byte {
|
|
run := GatewayBegin(ctx, body, info)
|
|
if run == nil {
|
|
return body
|
|
}
|
|
defer run.Close()
|
|
return run.Body
|
|
}
|
|
|
|
// A JS gateway middleware (*.gateway.js, magpie.gateway) and a request hook
|
|
// (*.hooks.js, magpie.hooks) never claim the same entry: a hook importing
|
|
// node:fs loaded as middleware too would show "didn't load" for a plugin
|
|
// that works.
|
|
func TestGatewayHookNamesApartFromMiddleware(t *testing.T) {
|
|
files := map[string]bool{"a.hooks.js": true, "a.hooks.mjs": true, "a.hooks.ts": true, "a.hooks.mts": true, "a.gateway.js": false, "a.gateway.mjs": false, "a.middleware.js": false}
|
|
for name, hook := range files {
|
|
p := filepath.Join(t.TempDir(), name)
|
|
if err := os.WriteFile(p, []byte(`export async function onRequest(b) { return b }`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := Gateway(p) != ""; got != hook {
|
|
t.Errorf("%s: hook %v, want %v", name, got, hook)
|
|
}
|
|
if f, _ := Middleware(p); hook && f != "" {
|
|
t.Errorf("%s is loaded as middleware too", name)
|
|
}
|
|
}
|
|
for key, hook := range map[string]bool{"hooks": true, "gateway": false, "middleware": false} {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "package.json"), []byte(`{"magpie":{"`+key+`":"./entry.js"}}`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "entry.js"), []byte(`export async function onRequest(b) { return b }`), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := Gateway(dir) != ""; got != hook {
|
|
t.Errorf("magpie.%s: hook %v, want %v", key, got, hook)
|
|
}
|
|
if f, _ := Middleware(dir); hook && f != "" {
|
|
t.Errorf("magpie.%s is loaded as middleware too", key)
|
|
}
|
|
}
|
|
}
|