| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/equipo" |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | func activityEvents(t *testing.T, r *toolRig) []wire.Activity { |
| 13 | t.Helper() |
| 14 | events, err := r.inst.Store.Chat().Events(r.ctx, Owner, 0, 1000) |
| 15 | if err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | var out []wire.Activity |
| 19 | for _, e := range events { |
| 20 | if e.Type == wire.EventActivity && e.ID == r.turn { |
| 21 | var a wire.Activity |
| 22 | if err := json.Unmarshal(e.Data, &a); err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | out = append(out, a) |
| 26 | } |
| 27 | } |
| 28 | return out |
| 29 | } |
| 30 | |
| 31 | func TestContractActivitySubjects(t *testing.T) { |
| 32 | r := newToolRig(t) // An unchanged protocol-1 runner gets named activities too. |
| 33 | other := personToolRig(t, r, "second@example.com", equipo.RoleOwner) |
| 34 | own := taskID(t, r.call("record", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Project threads"})) |
| 35 | secret := taskID(t, other.call("record", wire.ToolRecordTask, map[string]string{"repo": "oficina/sample", "title": "Someone else's private title"})) |
| 36 | cases := []struct { |
| 37 | name, tool string |
| 38 | args any |
| 39 | want string |
| 40 | }{ |
| 41 | {"own", wire.ToolReadTask, map[string]string{"task": own}, "Checking ‘Project threads’…"}, |
| 42 | {"other", wire.ToolReadTask, map[string]string{"task": secret}, "Checking the task…"}, |
| 43 | {"unknown", wire.ToolReadTask, map[string]string{"task": "/private/raw-input"}, "Checking the task…"}, |
| 44 | {"report", wire.ToolAddReport, map[string]string{"task": own, "text": "/private/raw-input"}, "Adding to ‘Project threads’…"}, |
| 45 | {"hold", wire.ToolHoldMerge, map[string]string{"task": own}, "Holding the merge for ‘Project threads’…"}, |
| 46 | {"read", wire.ToolReadFile, map[string]string{"repo": "oficina/sample", "path": "/private/raw-input"}, "Reading oficina/sample…"}, |
| 47 | {"search", wire.ToolSearchCode, map[string]string{"repo": "oficina/sample", "query": "/private/raw-input"}, "Searching oficina/sample…"}, |
| 48 | {"unreachable", wire.ToolSearchCode, map[string]string{"repo": "/private/raw-input", "query": "secret"}, "Searching the code…"}, |
| 49 | {"add", wire.ToolAddProject, map[string]string{"repo": "oficina/sample"}, "Adding oficina/sample…"}, |
| 50 | {"star", wire.ToolStarProject, map[string]any{"project": "oficina/sample", "starred": true}, "Starring oficina/sample…"}, |
| 51 | {"create-known", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/oficina/sample"}, "Creating oficina/sample…"}, |
| 52 | {"create-new", wire.ToolCreateProject, map[string]string{"repo": "amadan.net/oficina/raw-input"}, "Creating a repository…"}, |
| 53 | {"start", wire.ToolStartTask, map[string]string{"task": own}, "Handing ‘Project threads’ to Engineering…"}, |
| 54 | {"cancel", wire.ToolCancelTask, map[string]string{"task": own}, "Cancelling ‘Project threads’…"}, |
| 55 | } |
| 56 | for _, tc := range cases { |
| 57 | t.Run(tc.name, func(t *testing.T) { |
| 58 | before := len(activityEvents(t, r)) |
| 59 | r.call(tc.name, tc.tool, tc.args) |
| 60 | acts := activityEvents(t, r) |
| 61 | if len(acts) != before+1 { |
| 62 | t.Fatalf("got %d new activities, want 1", len(acts)-before) |
| 63 | } |
| 64 | got := acts[len(acts)-1] |
| 65 | if got.Tool != tc.tool || got.Label != tc.want { |
| 66 | t.Fatalf("activity %+v, want %q", got, tc.want) |
| 67 | } |
| 68 | raw, _ := json.Marshal(got) |
| 69 | if strings.Contains(string(raw), "raw-input") || strings.Contains(string(raw), "private title") { |
| 70 | t.Fatalf("input or another person's subject leaked: %s", raw) |
| 71 | } |
| 72 | }) |
| 73 | } |
| 74 | // An old attempt cannot publish a new subject. |
| 75 | before := len(activityEvents(t, r)) |
| 76 | r.attempt = 2 |
| 77 | r.call("stale", wire.ToolReadTask, map[string]string{"task": own}) |
| 78 | if len(activityEvents(t, r)) != before { |
| 79 | t.Fatal("stale call published activity") |
| 80 | } |
| 81 | } |
| 82 | |