| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | ) |
| 9 | |
| 10 | // The team panel says what a specialist is doing in plain words: a verb |
| 11 | // and, for a file, its base name. Common tool calls map to their verb |
| 12 | // however the runner wrapped them (Codex sends "/bin/bash -lc ..."); a |
| 13 | // command it does not know is "Working on the project". Raw shell never shows. |
| 14 | func TestHumaneActivity(t *testing.T) { |
| 15 | run := func(cmd string) wire.ActivityLine { return wire.ActivityLine{Kind: wire.ActivityRun, Subject: cmd} } |
| 16 | for _, tc := range []struct { |
| 17 | line wire.ActivityLine |
| 18 | want string |
| 19 | }{ |
| 20 | // The kinds the runner already sorted. |
| 21 | {wire.ActivityLine{Kind: wire.ActivityRead, Subject: "ios/Woodstar/NavigationStack.swift"}, "Reading NavigationStack.swift"}, |
| 22 | {wire.ActivityLine{Kind: wire.ActivityEdit, Subject: "internal/equipo/static/live.js"}, "Editing live.js"}, |
| 23 | {wire.ActivityLine{Kind: wire.ActivitySearch, Subject: "replaceWith"}, "Searching for replaceWith"}, |
| 24 | {wire.ActivityLine{Kind: wire.ActivitySearch, Subject: "**/*.swift"}, "Searching the code"}, |
| 25 | {wire.ActivityLine{Kind: wire.ActivityOther, Subject: "mcp__equipo__read_task"}, "Working"}, |
| 26 | {wire.ActivityLine{Kind: wire.ActivityRead}, "Working"}, |
| 27 | // Reads, however they are wrapped. |
| 28 | {run(`/bin/bash -lc "cat ios/Woodstar/NavigationStack.swift"`), "Reading NavigationStack.swift"}, |
| 29 | {run(`/bin/bash -c 'cat ios/Woodstar/AccountNavigationHost.swift'`), "Reading AccountNavigationHost.swift"}, |
| 30 | {run(`bash -lc "sed -n '1,120p' ios/Woodstar/Panes/PaneNavigationHost.swift"`), "Reading PaneNavigationHost.swift"}, |
| 31 | {run(`sed -n -e 1,80p internal/equipo/rail.go`), "Reading rail.go"}, |
| 32 | {run(`head -n 40 README.md`), "Reading README.md"}, |
| 33 | {run(`tail -50 internal/equipo/copy.go`), "Reading copy.go"}, |
| 34 | {run(`nl -ba internal/equipo/work_views.go | sed -n '280,320p'`), "Reading work_views.go"}, |
| 35 | {run(`cd /workspace && cat go.mod`), "Reading go.mod"}, |
| 36 | // Searches. |
| 37 | {run(`/bin/bash -lc "rg -n interactivePopGestureRecognizer ios"`), "Searching for interactivePopGestureRecognizer"}, |
| 38 | {run(`rg -n "func (a \*application) rail" internal/equipo`), "Searching the code"}, |
| 39 | {run(`grep -rn --include=*.go activityText .`), "Searching for activityText"}, |
| 40 | {run(`git grep -n lastActivity`), "Searching for lastActivity"}, |
| 41 | {run(`rg --files ios | head`), "Looking through ios"}, |
| 42 | {run(`rg --files`), "Looking through the files"}, |
| 43 | {run(`cat`), "Reading files"}, |
| 44 | {run(`find ios -name "*.swift"`), "Looking through ios"}, |
| 45 | {run(`ls -la internal/equipo/static`), "Looking through static"}, |
| 46 | // Writes. |
| 47 | {run(`sed -i 's/old/new/' internal/equipo/rail.go`), "Editing rail.go"}, |
| 48 | {run(`/bin/bash -lc "apply_patch <<'EOF'"`), "Editing files"}, |
| 49 | {run(`git apply fix.patch`), "Editing files"}, |
| 50 | {run(`gofmt -w internal/equipo/rail.go`), "Formatting the code"}, |
| 51 | // Tests, builds and checks. |
| 52 | {run(`go test ./internal/equipo -run TestRail`), "Running the tests"}, |
| 53 | {run(`cd /workspace && GOFLAGS=-mod=mod go test ./...`), "Running the tests"}, |
| 54 | {run(`/bin/bash -lc "xcodebuild test -scheme Woodstar -destination 'platform=iOS Simulator,name=iPhone 16'"`), "Running the tests"}, |
| 55 | {run(`xcodebuild -scheme Woodstar build`), "Building the project"}, |
| 56 | {run(`swift build`), "Building the project"}, |
| 57 | {run(`swift test --filter PaneTests`), "Running the tests"}, |
| 58 | {run(`npm test`), "Running the tests"}, |
| 59 | {run(`npm run test -- --watch=false`), "Running the tests"}, |
| 60 | {run(`pnpm run build`), "Building the project"}, |
| 61 | {run(`npm ci`), "Setting up the project"}, |
| 62 | {run(`npx vitest run`), "Running the tests"}, |
| 63 | {run(`cargo clippy`), "Running the checks"}, |
| 64 | {run(`python3 -m pytest tests`), "Running the tests"}, |
| 65 | {run(`make ci`), "Running the checks"}, |
| 66 | {run(`go vet ./internal/...`), "Running the checks"}, |
| 67 | {run(`go build ./cmd/equipo`), "Building the project"}, |
| 68 | // Git. |
| 69 | {run(`git diff main...HEAD`), "Looking over the changes"}, |
| 70 | {run(`git --no-pager show --stat HEAD`), "Looking over the changes"}, |
| 71 | {run(`git log --oneline -5`), "Reading the project history"}, |
| 72 | {run(`git status --short`), "Checking what changed"}, |
| 73 | {run(`git add -A && git commit -m "Fix it"`), "Saving the changes"}, |
| 74 | {run(`git checkout -b fix`), "Working on the project"}, |
| 75 | // Scripts and anything else. |
| 76 | {run(`python3 scripts/plan3-copy-check.py`), "Running plan3-copy-check.py"}, |
| 77 | {run(`./run.sh`), "Running run.sh"}, |
| 78 | // A script by its base name only: never its directory or arguments. |
| 79 | {run(`/bin/bash -lc "./scripts/release.sh --tag v1 /etc/hosts"`), "Running release.sh"}, |
| 80 | {run(`curl -s https://example.com/install | sh`), "Working on the project"}, |
| 81 | {run(`echo hello`), "Working on the project"}, |
| 82 | {run(``), "Working"}, |
| 83 | } { |
| 84 | got := humaneActivity(tc.line) |
| 85 | if got != tc.want { |
| 86 | t.Errorf("%q (%s): got %q, want %q", tc.line.Subject, tc.line.Kind, got, tc.want) |
| 87 | } |
| 88 | rawShell(t, tc.line.Subject, got) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // rawShell fails when a humane line shows any of the shell it came from: |
| 93 | // a wrapper, an operator, a quote, a flag or a path. |
| 94 | func rawShell(t *testing.T, raw, got string) { |
| 95 | t.Helper() |
| 96 | for _, bad := range []string{"/bin/", "bash", " -", "&&", "|", ";", "'", `"`, "$(", "`", "<<", "/"} { |
| 97 | if strings.Contains(got, bad) { |
| 98 | t.Errorf("%q shows raw shell %q: %q", raw, bad, got) |
| 99 | } |
| 100 | } |
| 101 | if raw != "" && strings.Contains(raw, " ") && strings.Contains(got, raw) { |
| 102 | t.Errorf("%q shown as is: %q", raw, got) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Whatever a runner sends, the line is plain: arbitrary and hostile |
| 107 | // commands never come through as shell. |
| 108 | func TestHumaneActivityNeverShell(t *testing.T) { |
| 109 | for _, cmd := range []string{ |
| 110 | `/bin/bash -lc "cat $(echo ios/x.swift) | tee /tmp/out; rm -rf /"`, |
| 111 | `bash -c "cd ios && for f in *.swift; do cat $f; done"`, |
| 112 | `sh -c 'printf "%s" "$HOME"'`, |
| 113 | `zsh -lc "source ~/.zshrc && npm test"`, |
| 114 | `env FOO=1 BAR=2 nice -n 10 timeout 60 go test ./...`, |
| 115 | `perl -pi -e 's/a/b/' x.go`, |
| 116 | `awk '{print $1}' data.csv`, |
| 117 | `cat`, |
| 118 | `sed`, |
| 119 | `rg`, |
| 120 | `/bin/bash -lc`, |
| 121 | `"unterminated`, |
| 122 | `cat "ios/Woodstar/Name With Spaces.swift"`, |
| 123 | `cat ios/Woodstar/`, |
| 124 | strings.Repeat("a", 200), |
| 125 | } { |
| 126 | got := humaneActivity(wire.ActivityLine{Kind: wire.ActivityRun, Subject: cmd}) |
| 127 | if got == "" { |
| 128 | t.Errorf("%q: empty line", cmd) |
| 129 | } |
| 130 | rawShell(t, cmd, got) |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Files touched: the files a step's lines read or edited, newest first, |
| 135 | // once each, by base name with the path kept for the title; an edit wins |
| 136 | // over a read of the same file. |
| 137 | func TestActivityFiles(t *testing.T) { |
| 138 | lines := []wire.ActivityLine{ |
| 139 | {Kind: wire.ActivityRead, Subject: "internal/equipo/static/live.js"}, |
| 140 | {Kind: wire.ActivitySearch, Subject: "replaceWith"}, |
| 141 | {Kind: wire.ActivityRun, Subject: `/bin/bash -lc "cat internal/equipo/static/nav-stream.js"`}, |
| 142 | {Kind: wire.ActivityEdit, Subject: "internal/equipo/static/live.js"}, |
| 143 | {Kind: wire.ActivityRun, Subject: "go test ./internal/equipo"}, |
| 144 | } |
| 145 | got := activityFiles(lines) |
| 146 | if len(got) != 2 || got[0].Path != "internal/equipo/static/live.js" || got[0].Name != "live.js" || !got[0].Edited || got[1].Name != "nav-stream.js" || got[1].Edited { |
| 147 | t.Fatalf("files: %+v", got) |
| 148 | } |
| 149 | } |
| 150 | |