package main import ( "context" "testing" "git.yeij.top/AskaEth/Cyrene_v2/internal/capability/registry" "git.yeij.top/AskaEth/Cyrene_v2/internal/capability/restrict" "git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/orchestration" "git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/tool" ) // TestWireSubagentToolRegistersOwnerOnly locks the composition-root wiring: the // dispatch tool must actually land in the capability registry (otherwise // orchestration stays library-only), and it must carry the default reachability // markers so a group/stranger session can neither see nor call it. func TestWireSubagentToolRegistersOwnerOnly(t *testing.T) { reg := registry.New() d, err := tool.New(reg) if err != nil { t.Fatalf("tool.New: %v", err) } a := &app{log: testLog(), dispatcher: d, scheduler: orchestration.New(orchestration.Options{})} a.wireSubagentTool() if _, gerr := reg.Get(orchestration.SpawnToolName); gerr != nil { t.Fatalf("capability %q is not registered: %v", orchestration.SpawnToolName, gerr) } names := make([]string, 0) for _, s := range d.ToolSchemas() { names = append(names, s.Name) } found := false for _, n := range names { if n == orchestration.SpawnToolName { found = true } } if !found { t.Fatalf("tool schemas = %v, want the spawn tool advertised", names) } if !restrict.SetContext(reg, restrict.ActorOwner).Allow(orchestration.SpawnToolName) { t.Fatal("owner was denied the spawn tool") } if restrict.SetContext(reg, restrict.ActorGroupMember).Allow(orchestration.SpawnToolName) { t.Fatal("group member may invoke the spawn tool; want owner-only") } if restrict.SetContext(reg, restrict.ActorStranger).Allow(orchestration.SpawnToolName) { t.Fatal("stranger may invoke the spawn tool; want owner-only") } // The registered capability must be the dispatch tool itself, not a placeholder. cap, _ := reg.Get(orchestration.SpawnToolName) sp, ok := cap.Value.(*orchestration.SpawnTool) if !ok { t.Fatalf("capability value = %T, want *orchestration.SpawnTool", cap.Value) } if _, err := sp.Execute(context.Background(), map[string]any{}); err == nil { t.Fatal("the registered tool accepted a goal-less call") } } // TestWireSubagentToolNilScheduler asserts a degraded boot (no scheduler) logs and // skips the registration instead of panicking. func TestWireSubagentToolNilScheduler(t *testing.T) { reg := registry.New() d, err := tool.New(reg) if err != nil { t.Fatalf("tool.New: %v", err) } a := &app{log: testLog(), dispatcher: d} a.wireSubagentTool() if _, gerr := reg.Get(orchestration.SpawnToolName); gerr == nil { t.Fatal("the tool was registered without a scheduler") } }