🙌 OpenHands: AI-Driven Development
fix: toast on run now success and failure (automation) (#759)
Hiep Le0742445728e8
diff --git a/__tests__/routes/automations-list.test.tsx b/__tests__/routes/automations-list.test.tsxindex ea19ad6a32..009c500d28 100644--- a/__tests__/routes/automations-list.test.tsx+++ b/__tests__/routes/automations-list.test.tsx@@ -16,7 +16,11 @@ import {import { ActiveBackendProvider } from "#/contexts/active-backend-context";import AutomationsList from "#/routes/automations-list";import type { Backend } from "#/api/backend-registry/types";-import type { Automation, AutomationsResponse } from "#/types/automation";+import {+ AutomationRunStatus,+ type Automation,+ type AutomationsResponse,+} from "#/types/automation";vi.mock("#/api/automation-service/automation-service.api", () => ({default: {@@ -24,10 +28,16 @@ vi.mock("#/api/automation-service/automation-service.api", () => ({updateAutomation: vi.fn(),toggleAutomation: vi.fn(),deleteAutomation: vi.fn(),+ dispatchAutomation: vi.fn(),checkHealth: vi.fn(),},}));+vi.mock("#/utils/custom-toast-handlers", () => ({+ displaySuccessToast: vi.fn(),+ displayErrorToast: vi.fn(),+}));+const localBackend: Backend = {id: "local-1",name: "Local 1",@@ -84,6 +94,7 @@ beforeEach(() => {vi.mocked(AutomationService.getAutomations).mockReset();vi.mocked(AutomationService.getAutomations).mockResolvedValue(listResponse);vi.mocked(AutomationService.updateAutomation).mockReset();+ vi.mocked(AutomationService.dispatchAutomation).mockReset();setRegisteredBackends([localBackend, cloudBackend]);setActiveSelection({ backendId: localBackend.id });});@@ -161,7 +172,9 @@ describe("AutomationsList — view mode toggle", () => {expect(screen.queryByTestId("automation-card-auto-1"),).not.toBeInTheDocument();- expect(screen.getByTestId("automation-list-row-auto-1")).toBeInTheDocument();+ expect(+ screen.getByTestId("automation-list-row-auto-1"),+ ).toBeInTheDocument();expect(window.localStorage.getItem("openhands-automations-view")).toBe("list",);@@ -190,3 +203,67 @@ describe("AutomationsList — view mode toggle", () => {).not.toBeInTheDocument();});});++describe("AutomationsList — Run now toasts", () => {+ beforeEach(async () => {+ const { displaySuccessToast, displayErrorToast } =+ await import("#/utils/custom-toast-handlers");+ vi.mocked(displaySuccessToast).mockClear();+ vi.mocked(displayErrorToast).mockClear();+ });++ it("shows a success toast after the dispatch API resolves", async () => {+ // Arrange — service resolves with a fresh run record.+ vi.mocked(AutomationService.dispatchAutomation).mockResolvedValue({+ id: "run-1",+ status: AutomationRunStatus.PENDING,+ conversation_id: null,+ bash_command_id: null,+ error_detail: null,+ started_at: "2026-01-02T00:00:00Z",+ completed_at: null,+ });+ const { displaySuccessToast, displayErrorToast } =+ await import("#/utils/custom-toast-handlers");+ const user = userEvent.setup();+ renderList();+ await screen.findByText(automation.name);++ // Act — click the row's "Run now" button.+ await user.click(screen.getByTestId(`automation-run-now-${automation.id}`));++ // Assert — dispatch was called and success toast fired with the i18n key.+ await waitFor(() => {+ expect(AutomationService.dispatchAutomation).toHaveBeenCalledWith(+ automation.id,+ );+ });+ await waitFor(() => {+ expect(displaySuccessToast).toHaveBeenCalledWith(+ I18nKey.AUTOMATIONS$RUN_NOW_SUCCESS,+ );+ });+ expect(displayErrorToast).not.toHaveBeenCalled();+ });++ it("shows an error toast when the dispatch API rejects", async () => {+ // Arrange — service rejects with a plain Error so the fallback branch fires.+ vi.mocked(AutomationService.dispatchAutomation).mockRejectedValue(+ new Error("dispatch failed"),+ );+ const { displaySuccessToast, displayErrorToast } =+ await import("#/utils/custom-toast-handlers");+ const user = userEvent.setup();+ renderList();+ await screen.findByText(automation.name);++ // Act — click the row's "Run now" button.+ await user.click(screen.getByTestId(`automation-run-now-${automation.id}`));++ // Assert — error toast surfaces the rejection message; success toast never fires.+ await waitFor(() => {+ expect(displayErrorToast).toHaveBeenCalledWith("dispatch failed");+ });+ expect(displaySuccessToast).not.toHaveBeenCalled();+ });+});diff --git a/src/i18n/translation.json b/src/i18n/translation.jsonindex fd41b32328..6a8507903c 100644--- a/src/i18n/translation.json+++ b/src/i18n/translation.json@@ -12443,6 +12443,40 @@"uk": "Не вдалося оновити автоматизацію","ca": "No s'ha pogut actualitzar l'automatització"},+ "AUTOMATIONS$RUN_NOW_SUCCESS": {+ "en": "Automation dispatched",+ "ja": "オートメーションを実行しました",+ "zh-CN": "自动化已触发",+ "zh-TW": "自動化已觸發",+ "ko-KR": "자동화가 실행되었습니다",+ "no": "Automatisering startet",+ "it": "Automazione avviata",+ "pt": "Automação disparada",+ "es": "Automatización ejecutada",+ "ar": "تم تشغيل الأتمتة",+ "fr": "Automatisation lancée",+ "tr": "Otomasyon başlatıldı",+ "de": "Automatisierung gestartet",+ "uk": "Автоматизацію запущено",+ "ca": "S'ha executat l'automatització"+ },+ "AUTOMATIONS$RUN_NOW_ERROR": {+ "en": "Could not dispatch automation",+ "ja": "オートメーションを実行できませんでした",+ "zh-CN": "无法触发自动化",+ "zh-TW": "無法觸發自動化",+ "ko-KR": "자동화를 실행할 수 없습니다",+ "no": "Kunne ikke starte automatiseringen",+ "it": "Impossibile avviare l'automazione",+ "pt": "Não foi possível disparar a automação",+ "es": "No se pudo ejecutar la automatización",+ "ar": "تعذر تشغيل الأتمتة",+ "fr": "Impossible de lancer l'automatisation",+ "tr": "Otomasyon başlatılamadı",+ "de": "Automatisierung konnte nicht gestartet werden",+ "uk": "Не вдалося запустити автоматизацію",+ "ca": "No s'ha pogut executar l'automatització"+ },"AUTOMATIONS$NAME_REQUIRED": {"en": "Name is required","ja": "名前は必須です",diff --git a/src/routes/automation-detail.tsx b/src/routes/automation-detail.tsxindex adf956b490..327e4de397 100644--- a/src/routes/automation-detail.tsx+++ b/src/routes/automation-detail.tsx@@ -1,6 +1,12 @@import { useRef, useState } from "react";import { useParams } from "react-router";import { isAxiosError } from "axios";+import { useTranslation } from "react-i18next";+import { I18nKey } from "#/i18n/declaration";+import {+ displaySuccessToast,+ displayErrorToast,+} from "#/utils/custom-toast-handlers";import { useAutomationDetail } from "#/hooks/query/use-automation-detail";import {useToggleAutomation,@@ -25,6 +31,7 @@ import { DeleteConfirmationModal } from "#/components/features/automations/deletimport { EditAutomationModal } from "#/components/features/automations/detail/edit-automation-modal";export default function AutomationDetail() {+ const { t } = useTranslation("openhands");const { automationId } = useParams();const { navigate } = useNavigation();const [showDeleteModal, setShowDeleteModal] = useState(false);@@ -134,7 +141,20 @@ export default function AutomationDetail() {};const handleRunNow = () => {- dispatchMutation.mutate(automation.id);+ dispatchMutation.mutate(automation.id, {+ onSuccess: () => {+ displaySuccessToast(t(I18nKey.AUTOMATIONS$RUN_NOW_SUCCESS));+ },+ onError: (error) => {+ const message = isAxiosError(error)+ ? (error.response?.data as { message?: string } | undefined)+ ?.message ||+ error.message ||+ t(I18nKey.AUTOMATIONS$RUN_NOW_ERROR)+ : (error as Error).message || t(I18nKey.AUTOMATIONS$RUN_NOW_ERROR);+ displayErrorToast(message);+ },+ });};// Edit is a local-backend-only feature in MVP — cloud automationsdiff --git a/src/routes/automations-list.tsx b/src/routes/automations-list.tsxindex 349cf2345f..7663c25bad 100644--- a/src/routes/automations-list.tsx+++ b/src/routes/automations-list.tsx@@ -1,6 +1,11 @@import { useState, useMemo, useCallback } from "react";import { useTranslation } from "react-i18next";+import { isAxiosError } from "axios";import { I18nKey } from "#/i18n/declaration";+import {+ displaySuccessToast,+ displayErrorToast,+} from "#/utils/custom-toast-handlers";import {useAutomations,useToggleAutomation,@@ -94,7 +99,20 @@ export default function AutomationsList() {};const handleRunNow = (id: string) => {- dispatchMutation.mutate(id);+ dispatchMutation.mutate(id, {+ onSuccess: () => {+ displaySuccessToast(t(I18nKey.AUTOMATIONS$RUN_NOW_SUCCESS));+ },+ onError: (error) => {+ const message = isAxiosError(error)+ ? (error.response?.data as { message?: string } | undefined)+ ?.message ||+ error.message ||+ t(I18nKey.AUTOMATIONS$RUN_NOW_ERROR)+ : (error as Error).message || t(I18nKey.AUTOMATIONS$RUN_NOW_ERROR);+ displayErrorToast(message);+ },+ });};const handleDeleteRequest = (id: string) => {