Files
magpie/internal/gui/turn_done_test.go
T
yetone 7cba316b90 gateway, gui: a notification when an agent's long task is done, opt-in in Settings › Usage (itieniu on X)
itieniu asked for a "ding" when a long Claude Code or OpenCode task
finishes. magpie sees every request an agent sends through it, so the
gateway now follows each agent session's turn and the app notifies at
its end.

Gateway (turndone.go): a request is of the turn unless it is one the
agent makes on the side (Kind) or a Claude Code subagent's. Its reply is
scanned on the way to the agent for a tool call in every wire format
(Anthropic tool_use, Chat tool_calls/function_call, Responses
function_call/custom_tool_call/local_shell_call/..., Gemini
functionCall), as a JSON value, never a key ("tool_calls":[]) or text.
The turn is done when no request of it is going, the last reply was
whole, under 400 and called no tool, and nothing more came for 4s; a
session idle past 30m starts over. The ChatGPT backend path is followed
too. Only a turn at least Settings' TurnDoneAlert seconds long is told,
and only when the app set gateway.TurnDone: magpie serve follows nothing.

Settings: turnDoneAlert (seconds, 0 = off, the default, at most a day).
GUI: Settings › Usage "Task done alert": Off / 1 / 3 / 10 min; the
notification says "Codex is done", "Took 1m 8s · folder", in en, zh,
zh-TW, ja and de. A click on it doesn't open the Usage page.

Tests: TestTurnDoneSaidAtTheTurnsEnd (chat and anthropic through the
gateway: a tool reply, a subagent's last reply and a short turn don't
fire; a final reply fires once with session and folder),
TestTurnDoneCodexBackend, TestTurnDoneWaitsForEveryRequest,
TestTurnDoneOff, TestToolScan (every split point), TestTurnDoneText,
settings' range. Broken by hand: without the capture's scan, without the
subagent exclusion, or without the codex backend's scan, they go red.
turn-done.test.cjs: every language, 900 and 380px, chromium and webkit.
The new row moved the Usage tab's geometry: usage-alert and currency
now wheel their row clear of the header before clicking it, as a reader
would (both green 5/5 here; on base they were green as before).

Run: gofmt -l; go vet darwin, GOOS=windows ./..., GOOS=linux gateway and
settings; go test ./... (gateway alone: ok, 863s under a GUI suite's
load); the full GUI suite in both engines. codex-credits and
gateway-key-models fail now and then here on base 398f8469b too
(codex-credits filed as #1622).

Real use: a sandbox HOME, the app's dev build on 3486/3487, "1 min"
clicked in its Settings › Usage (settings.json got turnDoneAlert 60);
then the gateway in that HOME with a TurnDone that logs, and real Codex (codex exec, ChatGPT sign-in, openai_base_url at the
sandbox gateway) running `sleep 62` then answering: the turn's end came
once, agent codex (Codex), its session, folder turnproj, took 1m8s; a
short "reply hi" turn after it didn't fire. The macOS notification
itself wasn't seen: an unbundled dev binary can't post one ("require a
valid bundle identifier"); the notifier is the one usage alerts use.
Claude Code and OpenCode weren't run live; their wire formats are in
the tests.
2026-10-11 21:59:10 +08:00

39 lines
1.4 KiB
Go

package gui
import (
"testing"
"time"
"github.com/yetone/magpie/internal/gateway"
)
// A turn's end is said in the page's language: the agent, how long the
// turn took, and the folder's own name when the agent says one.
func TestTurnDoneText(t *testing.T) {
long := gateway.TurnEnd{Agent: "claude", Session: "s", Folder: "/Users/u/work/magpie/", Took: 3*time.Minute + 12*time.Second}
for _, c := range []struct{ lang, title, body string }{
{"en", "Claude Code is done", "Took 3m 12s · magpie"},
{"zh", "Claude Code 已完成", "用时 3 分 12 秒 · magpie"},
{"zh-TW", "Claude Code 已完成", "用時 3 分 12 秒 · magpie"},
{"ja", "Claude Code が完了しました", "所要時間 3 分 12 秒 · magpie"},
{"de", "Claude Code ist fertig", "Dauer: 3 Min. 12 Sek. · magpie"},
} {
if title, body := turnDoneText(c.lang, "Claude Code", long); title != c.title || body != c.body {
t.Errorf("%s: %q / %q", c.lang, title, body)
}
}
for _, c := range []struct {
lang string
e gateway.TurnEnd
body string
}{
{"en", gateway.TurnEnd{Took: 45 * time.Second}, "Took 45s"},
{"en", gateway.TurnEnd{Took: 2*time.Hour + 5*time.Minute, Folder: `C:\Users\u\proj`}, "Took 2h 5m · proj"},
{"zh", gateway.TurnEnd{Took: time.Hour + 30*time.Second}, "用时 1 小时 0 分"},
} {
if _, body := turnDoneText(c.lang, "Codex", c.e); body != c.body {
t.Errorf("%s %+v: %q", c.lang, c.e, body)
}
}
}