package stdio import ( "bufio" "encoding/json" "io" "strings" "testing" "time" "git.novox.be/novox/mesh-sdk/go/buscalls" ) // callWith serves one tool that answers the caller CallerOf reads from its arguments, sends it one tools/call // with the params given, and returns the caller it answered. func callWith(t *testing.T, params map[string]any) string { caller, _, _ := callAnswering(t, params) return caller } // callAnswering is callWith answering the call's id CallIDOf read too. func callAnswering(t *testing.T, params map[string]any) (string, string, string) { servesAlone(t) t.Helper() inR, inW := io.Pipe() outR, outW := io.Pipe() go func() { _ = ServeOn("tracker", []Tool{{Name: "who", Run: func(args map[string]any) (any, error) { return map[string]any{"caller": CallerOf(args), "call": CallIDOf(args), "ask": AskIDOf(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')) lines := bufio.NewScanner(outR) done := make(chan [3]string, 1) go func() { if lines.Scan() { var m struct { Result struct { Content []struct { Text string `json:"text"` } `json:"content"` } `json:"result"` } _ = json.Unmarshal(lines.Bytes(), &m) var answer struct { Caller string `json:"caller"` Call string `json:"call"` Ask string `json:"ask"` } if len(m.Result.Content) > 0 { _ = json.Unmarshal([]byte(m.Result.Content[0].Text), &answer) } done <- [3]string{answer.Caller, answer.Call, answer.Ask} } }() select { case got := <-done: return got[0], got[1], got[2] case <-time.After(3 * time.Second): t.Fatal("the bundle did not answer the call") return "", "", "" } } // **A tool reads its caller from the runtime, never from the call's arguments** (novox/hq issue 365): the // runtime names the caller it took from the subject the call arrived on in the call's `_meta`, and CallerOf // hands it to the tool by the arguments the tool was given. func TestAToolReadsTheCallerTheRuntimeNamed(t *testing.T) { got := callWith(t, map[string]any{"name": "who", "arguments": map[string]any{"title": "x"}, "_meta": map[string]any{MetaCaller: "shanks.node-tools"}}) if got != "shanks.node-tools" { t.Fatalf("the tool read caller %q, want the one the runtime named", got) } // The call's id is the runtime's too (ADR 0597 §7): read from `_meta`, never from the arguments, and none when it // is not one token. for _, c := range []struct { args, meta map[string]any want string }{ {map[string]any{}, map[string]any{MetaCaller: "shanks.node-tools", MetaCall: "c-7Hq2"}, "c-7Hq2"}, {map[string]any{}, map[string]any{MetaCaller: "shanks.node-tools"}, ""}, {map[string]any{MetaCall: "c-forged"}, map[string]any{}, ""}, {map[string]any{}, map[string]any{MetaCall: "a b"}, ""}, {map[string]any{}, map[string]any{MetaCall: 7}, ""}, } { if _, call, _ := callAnswering(t, map[string]any{"name": "who", "arguments": c.args, "_meta": c.meta}); call != c.want { t.Errorf("args %v, meta %v: the tool read call %q, want %q", c.args, c.meta, call, c.want) } } // Design 47 H2 §775, H-A-351: the ask id a kept call carried is the runtime's to name under mesh/ask, never the // arguments'. for _, c := range []struct { args, meta map[string]any want string }{ {map[string]any{}, map[string]any{MetaAsk: "build-7Hq2"}, "build-7Hq2"}, {map[string]any{MetaAsk: "forged"}, map[string]any{}, ""}, {map[string]any{}, map[string]any{MetaAsk: "a.b"}, ""}, } { if _, _, ask := callAnswering(t, map[string]any{"name": "who", "arguments": c.args, "_meta": c.meta}); ask != c.want { t.Errorf("args %v, meta %v: the tool read ask %q, want %q", c.args, c.meta, ask, c.want) } } if CallIDOf(map[string]any{"x": 1}) != "" || CallIDOf(nil) != "" { t.Error("arguments no call was handed have a call id") } // Design 47 H2 §781, H-A-370: a process and a bundle read a setting by its key from the word the node-engine composes. t.Setenv("MESH_CONFIG_ASK_UNANSWERED_BOUND", "10m") t.Setenv("MESH_CONFIG_KEPT_CALL_PACE_FIRST", "soon") if v, ok := Setting("ask-unanswered-bound"); !ok || v != "10m" { t.Errorf("the setting read %q, %v", v, ok) } if d, err := SettingDuration("ask-unanswered-bound", time.Minute); err != nil || d != 10*time.Minute { t.Errorf("the duration read %s, %v", d, err) } if d, err := SettingDuration("compose-ask-bound", 10*time.Second); err != nil || d != 10*time.Second { t.Errorf("an absent setting read %s, %v, not its default", d, err) } if _, err := SettingDuration("kept-call-pace-first", time.Second); err == nil || !strings.Contains(err.Error(), "kept-call-pace-first") { t.Errorf("a value that is no duration read as %v", err) } // §775: a bundle reads its kept-call pace from the same settings at its start, and refuses one that is no duration. if err := ServeOn("b", nil, strings.NewReader(""), io.Discard); err == nil || !strings.Contains(err.Error(), "kept-call-pace-first") { t.Errorf("a bundle served with a pace that is no duration: %v", err) } if MetaCall != buscalls.MetaCall { t.Errorf("the call id's place is %q here and %q in buscalls", MetaCall, buscalls.MetaCall) } } // A caller named in the arguments is not the caller: an argument is the caller's to write. func TestACallerInTheArgumentsIsNeverTheCaller(t *testing.T) { got := callWith(t, map[string]any{"name": "who", "arguments": map[string]any{"caller": "novox.node-tools", "_meta": map[string]any{MetaCaller: "novox.node-tools"}, MetaCaller: "novox.node-tools"}}) if got != Unproven { t.Fatalf("a caller the arguments claimed was read as %q, want %q", got, Unproven) } got = callWith(t, map[string]any{"name": "who", "arguments": map[string]any{"caller": "novox.node-tools"}, "_meta": map[string]any{MetaCaller: "shanks.node-tools"}}) if got != "shanks.node-tools" { t.Fatalf("with a caller in the arguments the tool read %q, want the runtime's", got) } if ModuleOf("ace.mesh-build-go") != "mesh-build-go" { t.Error("ModuleOf does not read the module of a proved caller") } } // A runtime that names no caller — one from before issue 365, or a call on the subject that names none — is // said as Unproven, explicitly; a caller that is not a bus user's name is Unproven too. func TestACallTheRuntimeNamedNoCallerForIsUnproven(t *testing.T) { for _, params := range []map[string]any{ {"name": "who", "arguments": map[string]any{}}, {"name": "who"}, {"name": "who", "arguments": map[string]any{}, "_meta": map[string]any{}}, {"name": "who", "arguments": map[string]any{}, "_meta": map[string]any{MetaCaller: ""}}, {"name": "who", "arguments": map[string]any{}, "_meta": map[string]any{MetaCaller: "a b"}}, {"name": "who", "arguments": map[string]any{}, "_meta": map[string]any{MetaCaller: 7}}, } { if got := callWith(t, params); got != Unproven { t.Errorf("%v: the tool read caller %q, want %q", params, got, Unproven) } } } // Arguments the runtime never handed a tool have no caller. func TestArgumentsOfNoCallHaveNoCaller(t *testing.T) { if got := CallerOf(map[string]any{"x": 1}); got != Unproven { t.Fatalf("CallerOf of a map no call was given answered %q", got) } if got := CallerOf(nil); got != Unproven { t.Fatalf("CallerOf(nil) answered %q", got) } }