Skip to content

Commit c7dc132

Browse files
refactor(tui): simplify stale skill result handling tests
Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: Cai-Tang-www <106404101+Cai-Tang-www@users.noreply.github.com>
1 parent 9540a04 commit c7dc132

2 files changed

Lines changed: 59 additions & 4 deletions

File tree

internal/tui/core/app/update.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ func (a App) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
225225
requestSessionID := strings.TrimSpace(typed.RequestSessionID)
226226
activeSessionID := strings.TrimSpace(a.state.ActiveSessionID)
227227
if requestSessionID != "" && !strings.EqualFold(requestSessionID, activeSessionID) {
228+
a.recordStaleSkillCommandResult(requestSessionID, activeSessionID, typed.Err)
228229
return a, tea.Batch(cmds...)
229230
}
230231
if typed.Err != nil {
@@ -1701,6 +1702,15 @@ func (a *App) applyInlineCommandError(message string) {
17011702
a.rebuildTranscript()
17021703
}
17031704

1705+
// recordStaleSkillCommandResult 记录来自旧会话的技能命令结果,避免在会话切换后错误被静默丢弃。
1706+
func (a *App) recordStaleSkillCommandResult(requestSessionID, activeSessionID string, runErr error) {
1707+
detail := fmt.Sprintf("result from session %q ignored after switching to %q", requestSessionID, activeSessionID)
1708+
if runErr != nil {
1709+
detail = fmt.Sprintf("%s; original error: %s", detail, runErr.Error())
1710+
}
1711+
a.appendActivity("skills", "Ignored stale skill command result", detail, runErr != nil)
1712+
}
1713+
17041714
func (a *App) appendActivity(kind string, title string, detail string, isError bool) {
17051715
previousCount := len(a.activities)
17061716
title = strings.TrimSpace(title)

internal/tui/core/app/update_test.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4189,22 +4189,46 @@ func TestRebuildActivityWithHeightAndPersistPathGuard(t *testing.T) {
41894189
app.persistLogEntriesForActiveSession()
41904190
}
41914191

4192+
func updateWithSkillCommandResult(t *testing.T, app App, result skillCommandResultMsg) App {
4193+
t.Helper()
4194+
4195+
model, _ := app.Update(result)
4196+
return model.(App)
4197+
}
4198+
4199+
func assertIgnoredStaleSkillResultActivity(t *testing.T, app App, beforeActivities int, wantError bool) tuistate.ActivityEntry {
4200+
t.Helper()
4201+
4202+
if len(app.activities) != beforeActivities+1 {
4203+
t.Fatalf("expected stale skill result to be logged, got %d activities", len(app.activities))
4204+
}
4205+
last := app.activities[len(app.activities)-1]
4206+
if last.Title != "Ignored stale skill command result" {
4207+
t.Fatalf("expected stale result activity title, got %q", last.Title)
4208+
}
4209+
if last.IsError != wantError {
4210+
t.Fatalf("expected stale result error flag=%v, got %v", wantError, last.IsError)
4211+
}
4212+
return last
4213+
}
4214+
41924215
func TestUpdateIgnoresStaleSkillCommandResultBySession(t *testing.T) {
41934216
t.Parallel()
41944217

41954218
app, _ := newTestApp(t)
41964219
app.state.ActiveSessionID = "session-current"
41974220
app.state.StatusText = "before"
4221+
beforeActivities := len(app.activities)
41984222

4199-
model, _ := app.Update(skillCommandResultMsg{
4223+
app = updateWithSkillCommandResult(t, app, skillCommandResultMsg{
42004224
Notice: "should be ignored",
42014225
RequestSessionID: "session-old",
42024226
})
4203-
app = model.(App)
42044227

42054228
if app.state.StatusText != "before" {
42064229
t.Fatalf("expected stale skill result to be ignored, got status %q", app.state.StatusText)
42074230
}
4231+
assertIgnoredStaleSkillResultActivity(t, app, beforeActivities, false)
42084232
}
42094233

42104234
func TestUpdateAcceptsSkillCommandResultForCurrentSession(t *testing.T) {
@@ -4213,13 +4237,34 @@ func TestUpdateAcceptsSkillCommandResultForCurrentSession(t *testing.T) {
42134237
app, _ := newTestApp(t)
42144238
app.state.ActiveSessionID = "session-current"
42154239

4216-
model, _ := app.Update(skillCommandResultMsg{
4240+
app = updateWithSkillCommandResult(t, app, skillCommandResultMsg{
42174241
Notice: "Skill command completed.",
42184242
RequestSessionID: "session-current",
42194243
})
4220-
app = model.(App)
42214244

42224245
if app.state.StatusText != "Skill command completed." {
42234246
t.Fatalf("expected status to be updated, got %q", app.state.StatusText)
42244247
}
42254248
}
4249+
4250+
func TestUpdateLogsStaleSkillCommandErrorBySession(t *testing.T) {
4251+
t.Parallel()
4252+
4253+
app, _ := newTestApp(t)
4254+
app.state.ActiveSessionID = "session-current"
4255+
app.state.StatusText = "before"
4256+
beforeActivities := len(app.activities)
4257+
4258+
app = updateWithSkillCommandResult(t, app, skillCommandResultMsg{
4259+
Err: errors.New("activate failed"),
4260+
RequestSessionID: "session-old",
4261+
})
4262+
4263+
if app.state.StatusText != "before" {
4264+
t.Fatalf("expected stale skill error to keep current status, got %q", app.state.StatusText)
4265+
}
4266+
last := assertIgnoredStaleSkillResultActivity(t, app, beforeActivities, true)
4267+
if !strings.Contains(last.Detail, "activate failed") {
4268+
t.Fatalf("expected stale error detail to include original error, got %q", last.Detail)
4269+
}
4270+
}

0 commit comments

Comments
 (0)