Files
mesh-catalog/modules/mesh-knowledge/cmd/mesh-knowledge/caller_test.go
T
jschoubben b3fcf408f2 Make tests that wait for concurrent work wait on the event or a condition with a long deadline (issue 1814)
A test that slept a fixed time and then asserted what a goroutine, process or
watcher did failed under the checker's load. Each now waits for its positive
evidence (a started baseline, a call counted, a state reached) within 30 s, and
the concurrency-limit tests meet at a barrier instead of overlapping by sleep.

Claude-Session: https://claude.ai/code/session_01NcbgfJ9Gum47zkFKWRaovL
2026-11-01 17:24:19 +01:00

66 lines
2.0 KiB
Go

package main
import (
"bufio"
"encoding/json"
"io"
"testing"
"time"
stdio "git.novox.be/novox/mesh-sdk/go"
)
// whoCalled serves one tool that answers callerOf of its arguments, over the SDK's stdio loop as the runtime
// launches a bundle, and sends it one call with params.
func whoCalled(t *testing.T, params map[string]any) string {
t.Helper()
inR, inW := io.Pipe()
outR, outW := io.Pipe()
go func() {
_ = stdio.ServeOn("mesh-knowledge", []stdio.Tool{{Name: "who", Run: func(args map[string]any) (any, error) {
return callerOf(args), nil
}}}, inR, outW)
}()
defer inW.Close()
b, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": params})
_, _ = inW.Write(append(b, '\n'))
got := make(chan string, 1)
go func() {
lines := bufio.NewScanner(outR)
if lines.Scan() {
var m struct {
Result struct {
Content []struct {
Text string `json:"text"`
} `json:"content"`
} `json:"result"`
}
_ = json.Unmarshal(lines.Bytes(), &m)
var who string
if len(m.Result.Content) > 0 {
_ = json.Unmarshal([]byte(m.Result.Content[0].Text), &who)
}
got <- who
}
}()
select {
case who := <-got:
return who
case <-time.After(30 * time.Second):
t.Fatal("the call was not answered")
return ""
}
}
// **Who called is the caller the runtime proved** (hq issue 365): read from the call's runtime-written `_meta`,
// never from its arguments; Unproven where the runtime named none.
func TestTheCallerIsTheOneTheRuntimeProved(t *testing.T) {
if got := whoCalled(t, map[string]any{"name": "who", "arguments": map[string]any{"caller": "novox.node-tools"},
"_meta": map[string]any{stdio.MetaCaller: "shanks.node-tools"}}); got != "shanks.node-tools" {
t.Errorf("callerOf answered %q, want the runtime's caller", got)
}
if got := whoCalled(t, map[string]any{"name": "who", "arguments": map[string]any{"caller": "novox.node-tools"}}); got != Unproven {
t.Errorf("a call the runtime named no caller for answered %q, want %s", got, Unproven)
}
}