Promotes Automations to a top-level sidebar group (below Bookmarks) with user-defined AgentGroup folders (migration 025, FK SET NULL on delete). The /automations page becomes master-detail: overview (composer + grouped list + named global runs) and per-task detail (original prompt, group assignment, run-now/pause/delete, per-task run timeline with true/false/ error pills). Editing recompiles through /api/agent/compile and confirms the proposal before a versioned PATCH applies spec+prompt — the model never writes. Tasks store prompt+groupId; projections add runCount/ errorCount; runs carry taskName. The engine is untouched: groups/prompt are metadata, the validated spec remains the only thing it executes. Chaining, refinements and dry-run stay phase 2. Verified: full DB integration suite under -race, db-converge + migration atomicity, web lint/SSR/build, and a rewritten browser regression covering the sidebar folder, detail prompt, per-task runs, recompile-to-edit and named global runs.
93 lines
4.0 KiB
Go
93 lines
4.0 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"gitea.federated.computer/Federated_Computer/superbrain/internal/db"
|
|
)
|
|
|
|
// Automations workspace phase 1: groups, stored prompt, run stats and named
|
|
// runs. The engine is untouched — these are workspace metadata paths.
|
|
func TestAgentGroupsAndPrompt(t *testing.T) {
|
|
srv, d := testServerDB(t)
|
|
alice, aliceID := accountClient(t, srv.URL, "ag-alice@superbrain.test")
|
|
bob, _ := accountClient(t, srv.URL, "ag-bob@superbrain.test")
|
|
|
|
spec := map[string]any{
|
|
"condition": map[string]any{"type": "url_watch", "url": "https://example.test/q", "question": "price?", "op": "lt", "threshold": 400},
|
|
"action": map[string]any{"type": "email", "to": "david@email.test", "title": "Deal", "body": "Under $400"},
|
|
"pollMinutes": 60, "once": false,
|
|
}
|
|
|
|
// Groups CRUD + version contract + ownership.
|
|
g := alice.request("POST", "/api/agent/groups", map[string]any{"name": "Travel"}, 201)
|
|
gid, _ := g["id"].(string)
|
|
alice.request("POST", "/api/agent/groups", map[string]any{"name": ""}, 400)
|
|
alice.request("PATCH", "/api/agent/groups/"+gid, map[string]any{"name": "Trips", "version": 99}, 409)
|
|
alice.request("PATCH", "/api/agent/groups/"+gid, map[string]any{"name": "Trips"}, 400) // version required
|
|
alice.request("PATCH", "/api/agent/groups/"+gid, map[string]any{"name": "Trips", "version": 1}, 200)
|
|
bob.request("PATCH", "/api/agent/groups/"+gid, map[string]any{"name": "stolen", "version": 2}, 404)
|
|
bob.request("DELETE", "/api/agent/groups/"+gid, nil, 404)
|
|
if gs := alice.getJSON("/api/agent/groups", 200); len(gs) != 1 || gs[0]["name"] != "Trips" {
|
|
t.Fatalf("groups list: %v", gs)
|
|
}
|
|
|
|
// Task carries prompt + groupId; projection exposes run stats.
|
|
task := alice.request("POST", "/api/agent/tasks", map[string]any{
|
|
"name": "Flights under 400", "spec": spec,
|
|
"prompt": "when the round trip drops below $400, email david@email.test", "groupId": gid,
|
|
}, 201)
|
|
tid, _ := task["id"].(string)
|
|
if task["prompt"] == "" || task["groupId"] != gid {
|
|
t.Fatalf("prompt/groupId not stored: %v", task)
|
|
}
|
|
alice.request("POST", "/api/agent/tasks", map[string]any{"name": "x", "spec": spec, "groupId": "nope"}, 400)
|
|
|
|
// Run rows (engine writes these; seed directly to test the read paths).
|
|
for _, oc := range []string{"checked", "fired", "failed"} {
|
|
if _, e := d.ExecContext(context.Background(),
|
|
`INSERT INTO "AgentRun" (id,"taskId","ownerId",outcome,detail) VALUES ($1,$2,$3,$4,$5)`,
|
|
db.NewID(), tid, aliceID, oc, "detail-"+oc); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
list := alice.getJSON("/api/agent/tasks", 200)
|
|
if len(list) != 1 || list[0]["runCount"] != float64(3) || list[0]["errorCount"] != float64(1) {
|
|
t.Fatalf("run stats wrong: %v", list)
|
|
}
|
|
// Named runs + per-task filter.
|
|
runs := alice.getJSON("/api/agent/runs?taskId="+tid, 200)
|
|
if len(runs) != 3 || runs[0]["taskName"] != "Flights under 400" {
|
|
t.Fatalf("named runs: %v", runs)
|
|
}
|
|
if n := len(alice.getJSON("/api/agent/runs", 200)); n != 3 {
|
|
t.Fatalf("global runs: %d", n)
|
|
}
|
|
|
|
// PATCH prompt + ungroup (null).
|
|
alice.request("PATCH", "/api/agent/tasks/"+tid, map[string]any{"prompt": "updated prompt", "version": 1}, 200)
|
|
alice.request("PATCH", "/api/agent/tasks/"+tid, map[string]any{"groupId": nil, "version": 2}, 200)
|
|
if ts := alice.getJSON("/api/agent/tasks", 200); ts[0]["groupId"] != nil || ts[0]["prompt"] != "updated prompt" {
|
|
t.Fatalf("patch prompt/group: %v", ts[0])
|
|
}
|
|
|
|
// Deleting a group ungroups its tasks (FK SET NULL), never deletes them.
|
|
g2 := alice.request("POST", "/api/agent/groups", map[string]any{"name": "Temp"}, 201)
|
|
g2id, _ := g2["id"].(string)
|
|
t2 := alice.request("POST", "/api/agent/tasks", map[string]any{"name": "In temp", "spec": spec, "groupId": g2id}, 201)
|
|
alice.request("DELETE", "/api/agent/groups/"+g2id, nil, 200)
|
|
found := false
|
|
for _, ts := range alice.getJSON("/api/agent/tasks", 200) {
|
|
if ts["id"] == t2["id"] {
|
|
found = true
|
|
if ts["groupId"] != nil {
|
|
t.Fatalf("task not ungrouped: %v", ts)
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Fatal("group delete removed the task")
|
|
}
|
|
}
|