| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "slices" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | "amadan.net/oficina/equipo/internal/specialist" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | // Task 17 (dynamic specialists slice 1): the coordinator's surface for |
| 16 | // routing to Design, as places added its tools: the schemas and their |
| 17 | // descriptions, the Claude allowlist, provider.ConverseTools, |
| 18 | // wire.MutationKinds, the opening's sentence and a new house rule; and |
| 19 | // the bound specialist_added continuation as the runner runs it. |
| 20 | |
| 21 | func converseTool(t *testing.T, name string) map[string]any { |
| 22 | t.Helper() |
| 23 | for _, tool := range converseTools { |
| 24 | if m := tool.(map[string]any); m["name"] == name { |
| 25 | return m |
| 26 | } |
| 27 | } |
| 28 | t.Fatalf("no %s", name) |
| 29 | return nil |
| 30 | } |
| 31 | |
| 32 | // TestCoordinatorDesignSurface: start_task takes an optional specialist |
| 33 | // id, propose_specialist offers design-v1 (scoped to one project) beside |
| 34 | // research-v1, status says it lists the specialists, every tool is in |
| 35 | // the Claude allowlist and provider.ConverseTools with its mutation kind, |
| 36 | // and the opening routes visual design to Design under a new house rule. |
| 37 | func TestCoordinatorDesignSurface(t *testing.T) { |
| 38 | start := converseTool(t, wire.ToolStartTask) |
| 39 | schema := start["inputSchema"].(map[string]any) |
| 40 | if p, ok := schema["properties"].(map[string]any)["specialist"]; !ok || p.(map[string]string)["type"] != "string" || slices.Contains(schema["required"].([]string), "specialist") { |
| 41 | t.Fatalf("start_task's specialist: %v", schema) |
| 42 | } |
| 43 | if d := start["description"].(string); !strings.Contains(d, "specialist") || !strings.Contains(d, "status") { |
| 44 | t.Fatalf("start_task's description: %s", d) |
| 45 | } |
| 46 | propose := converseTool(t, wire.ToolProposeSpecialist) |
| 47 | enum := propose["inputSchema"].(map[string]any)["properties"].(map[string]any)["profile"].(map[string]any)["enum"].([]string) |
| 48 | if !slices.Equal(enum, []string{specialist.ProfileResearch, specialist.ProfileDesign}) { |
| 49 | t.Fatalf("profiles %v", enum) |
| 50 | } |
| 51 | d := propose["description"].(string) |
| 52 | for _, want := range []string{"design-v1", `["branch.edit", "page.render"]`, `["render_page", "submit_candidate", "ask_person"]`, "project"} { |
| 53 | if !strings.Contains(d, want) { |
| 54 | t.Fatalf("propose_specialist's description lacks %q: %s", want, d) |
| 55 | } |
| 56 | } |
| 57 | // The menu's own capabilities and tools, as the description names them. |
| 58 | p, _ := specialist.Lookup(specialist.ProfileDesign) |
| 59 | caps, _ := json.Marshal(p.Capabilities) |
| 60 | tools, _ := json.Marshal(p.Tools) |
| 61 | if !strings.Contains(strings.ReplaceAll(d, ", ", ","), string(caps)) || !strings.Contains(strings.ReplaceAll(d, ", ", ","), string(tools)) { |
| 62 | t.Fatalf("the menu's design-v1 is %s %s", caps, tools) |
| 63 | } |
| 64 | if d := converseTool(t, wire.ToolStatus)["description"].(string); !strings.Contains(d, "specialists") { |
| 65 | t.Fatalf("status: %s", d) |
| 66 | } |
| 67 | for _, name := range []string{wire.ToolStatus, wire.ToolStartTask, wire.ToolProposeSpecialist} { |
| 68 | if !slices.Contains(provider.ConverseTools(), name) || !slices.Contains(wire.ConverseTools, name) { |
| 69 | t.Fatalf("%s is not a converse tool", name) |
| 70 | } |
| 71 | } |
| 72 | for _, name := range []string{wire.ToolStartTask, wire.ToolProposeSpecialist} { |
| 73 | if wire.MutationKinds[name] == "" || !slices.Contains(wire.WriteTools, name) { |
| 74 | t.Fatalf("%s is not a designated mutation", name) |
| 75 | } |
| 76 | } |
| 77 | o := opening("p@example.com", nil) |
| 78 | for _, want := range []string{"Visual design, layout, themes and mockups go to Design", "status lists", "\"specialist\"", "propose_specialist with profile design-v1", "say in one line why"} { |
| 79 | if !strings.Contains(o, want) { |
| 80 | t.Fatalf("the opening lacks %q", want) |
| 81 | } |
| 82 | } |
| 83 | if houseRule == "2026-11-26.1" { |
| 84 | t.Fatal("the opening's rule changed without a new house rule") |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // TestSpecialistAddedTurnRuns: the bound continuation is framed with the |
| 89 | // instance's own labelled opening (never as the person's newest |
| 90 | // message), it is not held to the 24-call budget of a turn that only |
| 91 | // looks around, and it never marks the desk moved in. |
| 92 | func TestSpecialistAddedTurnRuns(t *testing.T) { |
| 93 | desk, err := OpenDesk(t.TempDir(), "p@example.com") |
| 94 | if err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | opening := "[Equipo's record, not the person's words: Paul added Design for a project.]\nThe person's own words, quoted: \"Calmer\"" |
| 98 | c := &coordBase{person: "p@example.com", desk: desk, profile: profileFor(wire.LaneCoordinator)} |
| 99 | for _, fresh := range []bool{true, false} { |
| 100 | frame, _ := c.prompt(fresh, TurnInput{Person: "p@example.com", TurnID: "t1", Kind: wire.TurnSpecialistAdded, Text: opening}, nil) |
| 101 | if !strings.HasSuffix(frame, opening) || strings.Contains(frame, "Newest message") { |
| 102 | t.Fatalf("fresh %v frame:\n%s", fresh, frame) |
| 103 | } |
| 104 | } |
| 105 | // A person turn's frame is as before. |
| 106 | if frame, _ := c.prompt(true, TurnInput{Person: "p@example.com", TurnID: "t2", Kind: wire.TurnPerson, Text: "Hi"}, nil); !strings.Contains(frame, "Newest message:\nHi") { |
| 107 | t.Fatalf("a person turn:\n%s", frame) |
| 108 | } |
| 109 | if lookingAround(wire.TurnSpecialistAdded) || lookingAround(wire.TurnPerson) || !lookingAround(wire.TurnMoveIn) || !lookingAround(wire.TurnExplore) || !lookingAround(wire.TurnTeamUpdate) { |
| 110 | t.Fatal("the looking-around turns") |
| 111 | } |
| 112 | calls := 0 |
| 113 | c.o.Tools = func(context.Context, string, string, int64, string, string, json.RawMessage) toolReply { |
| 114 | calls++ |
| 115 | return toolReply{Text: "ok"} |
| 116 | } |
| 117 | c.turn, c.attempt, c.turnCtx, c.kind, c.calls = "t1", 1, context.Background(), wire.TurnSpecialistAdded, moveInCalls+5 |
| 118 | if out := c.toolReply(context.Background(), wire.ToolReadFile, "u1", json.RawMessage(`{}`)); out.Text != "ok" || calls != 1 { |
| 119 | t.Fatalf("the continuation was held to the budget: %+v", out) |
| 120 | } |
| 121 | c.kind = wire.TurnMoveIn |
| 122 | if out := c.toolReply(context.Background(), wire.ToolReadFile, "u2", json.RawMessage(`{}`)); out.Text != toolNoBudget || calls != 1 { |
| 123 | t.Fatalf("a move-in past its budget: %+v", out) |
| 124 | } |
| 125 | if settles(wire.TurnSpecialistAdded) || settles(wire.TurnPerson) || !settles(wire.TurnMoveIn) || !settles(wire.TurnExplore) { |
| 126 | t.Fatal("settled after a continuation") |
| 127 | } |
| 128 | } |
| 129 | |