package app import ( "testing" ) // TestOnChildSpawnedAgentChildKeepsFocus verifies that when a child is // spawned with a ParentID set (i.e. a patterm-managed agent caused the // spawn over MCP), OnChildSpawned does NOT steal viewport focus from // the currently focused child. func TestOnChildSpawnedAgentChildKeepsFocus(t *testing.T) { sess := NewSession(t.TempDir(), "test") st := &uiState{sess: sess} parent := newChildEntry("p_parent", "parent", KindAgent, nil, nil, "", "", "") st.focusedID = parent.ID st.focusedName = parent.Name subAgent := newChildEntry("p_sub", "sub", KindAgent, nil, nil, parent.ID, "", "") st.OnChildSpawned(subAgent) if got := st.focusedID; got != parent.ID { t.Fatalf("agent-initiated spawn should not change focusedID: want %q, got %q", parent.ID, got) } if got := st.focusedName; got != parent.Name { t.Fatalf("focusedName changed: want %q, got %q", parent.Name, got) } } // TestOnChildSpawnedPaletteChildTakesFocus verifies the legacy path is // preserved: spawns with an empty ParentID (palette, restore, external // MCP caller) still auto-focus the new child. func TestOnChildSpawnedPaletteChildTakesFocus(t *testing.T) { sess := NewSession(t.TempDir(), "test") st := &uiState{sess: sess} st.lastExit.Store(-1) c := newChildEntry("p_new", "newchild", KindAgent, nil, nil, "", "", "") st.OnChildSpawned(c) if got := st.focusedID; got != c.ID { t.Fatalf("palette-initiated spawn should auto-focus: want %q, got %q", c.ID, got) } }