fix(office): routine create/save persists every field and can arm a cron schedule (#3759)

Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com>
Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
This commit is contained in:
Henry
2026-09-17 21:34:22 +08:00
committed by GitHub
parent b99ac97a24
commit cf9c98ded0
37 changed files with 4127 additions and 286 deletions
+6
View File
@@ -722,6 +722,7 @@
"routeOutcomeSkippedUserAction": "skipped user action",
"routineActivated": "Routine activated",
"routineCreated": "Routine created",
"routineCreatedWithoutSchedule": "The routine was created without a schedule: {{error}}",
"routineDeleted": "Routine deleted",
"routineFired": "Routine fired",
"routineNotFiring": "Cannot run: routine status is {{status}}",
@@ -729,6 +730,11 @@
"routines": "Routines",
"routinesAutomaticallyCreateTasksOnA": "Routines automatically create tasks on a schedule or webhook trigger.",
"routineSaved": "Routine saved",
"routineSavedScheduleChangeFailed": "The routine was saved, but the schedule change failed: {{error}}",
"routineSavedScheduleDeleteFailed_one": "The routine was saved, but removing the old schedule failed: {{error}}. {{count}} cron schedule is now active.",
"routineSavedScheduleDeleteFailed_other": "The routine was saved, but removing the old schedule failed: {{error}}. {{count}} cron schedules are now active.",
"routineSavedScheduleMayBeStale": "The routine was saved. The displayed schedule may be stale until you reload the page.",
"routineScheduleFateUnknown": "The routine was saved, but the schedule's fate is unknown. Reload the page to see the current state.",
"routineStarted": "Routine started",
"routineStatusActive": "Active",
"routineStatusArchived": "Archived",
+6
View File
@@ -722,6 +722,7 @@
"routeOutcomeSkippedUserAction": "śķĩƥƥēď ũśēŕ àćţĩōń",
"routineActivated": "Ŕōũţĩńē àćţĩvàţēď",
"routineCreated": "Ŕōũţĩńē ćŕēàţēď",
"routineCreatedWithoutSchedule": "Ţĥē ŕōũţĩńē ŵàś ćŕēàţēď ŵĩţĥōũţ à śćĥēďũĺē: {{error}}",
"routineDeleted": "Ŕōũţĩńē ďēĺēţēď",
"routineFired": "Ŕōũţĩńē ƒĩŕēď",
"routineNotFiring": "Ćàńńōţ ŕũń: ŕōũţĩńē śţàţũś ĩś {{status}}",
@@ -729,6 +730,11 @@
"routines": "Ŕōũţĩńēś",
"routinesAutomaticallyCreateTasksOnA": "Ŕōũţĩńēś àũţōḿàţĩćàĺĺŷ ćŕēàţē ţàśķś ōń à śćĥēďũĺē ōŕ ŵēƀĥōōķ ţŕĩĝĝēŕ.",
"routineSaved": "Ŕōũţĩńē śàvēď",
"routineSavedScheduleChangeFailed": "Ţĥē ŕōũţĩńē ŵàś śàvēď, ƀũţ ţĥē śćĥēďũĺē ćĥàńĝē ƒàĩĺēď: {{error}}",
"routineSavedScheduleDeleteFailed_one": "Ţĥē ŕōũţĩńē ŵàś śàvēď, ƀũţ ŕēḿōvĩńĝ ţĥē ōĺď śćĥēďũĺē ƒàĩĺēď: {{error}}. {{count}} ćŕōń śćĥēďũĺē ĩś ńōŵ àćţĩvē.",
"routineSavedScheduleDeleteFailed_other": "Ţĥē ŕōũţĩńē ŵàś śàvēď, ƀũţ ŕēḿōvĩńĝ ţĥē ōĺď śćĥēďũĺē ƒàĩĺēď: {{error}}. {{count}} ćŕōń śćĥēďũĺēś àŕē ńōŵ àćţĩvē.",
"routineSavedScheduleMayBeStale": "Ţĥē ŕōũţĩńē ŵàś śàvēď. Ţĥē ďĩśƥĺàŷēď śćĥēďũĺē ḿàŷ ƀē śţàĺē ũńţĩĺ ŷōũ ŕēĺōàď ţĥē ƥàĝē.",
"routineScheduleFateUnknown": "Ţĥē ŕōũţĩńē ŵàś śàvēď, ƀũţ ţĥē śćĥēďũĺē'ś ƒàţē ĩś ũńķńōŵń. Ŕēĺōàď ţĥē ƥàĝē ţō śēē ţĥē ćũŕŕēńţ śţàţē.",
"routineStarted": "Ŕōũţĩńē śţàŕţēď",
"routineStatusActive": "Àćţĩvē",
"routineStatusArchived": "Àŕćĥĩvēď",
+6
View File
@@ -722,6 +722,7 @@
"routeOutcomeSkippedUserAction": "ignorado por ação do utilizador",
"routineActivated": "Rotina ativada",
"routineCreated": "Rotina criada",
"routineCreatedWithoutSchedule": "A rotina foi criada sem agendamento: {{error}}",
"routineDeleted": "Rotina eliminada",
"routineFired": "Rotina disparada",
"routineNotFiring": "Não é possível executar: o estado da rotina é {{status}}",
@@ -729,6 +730,11 @@
"routines": "Rotinas",
"routinesAutomaticallyCreateTasksOnA": "As rotinas criam tarefas automaticamente com base num agendamento ou num acionador de webhook.",
"routineSaved": "Rotina guardada",
"routineSavedScheduleChangeFailed": "A rotina foi guardada, mas a alteração do agendamento falhou: {{error}}",
"routineSavedScheduleDeleteFailed_one": "A rotina foi guardada, mas a remoção do agendamento anterior falhou: {{error}}. {{count}} agendamento cron está agora ativo.",
"routineSavedScheduleDeleteFailed_other": "A rotina foi guardada, mas a remoção do agendamento anterior falhou: {{error}}. {{count}} agendamentos cron estão agora ativos.",
"routineSavedScheduleMayBeStale": "A rotina foi guardada. O agendamento apresentado pode estar desatualizado até recarregar a página.",
"routineScheduleFateUnknown": "A rotina foi guardada, mas o estado do agendamento é desconhecido. Recarregue a página para ver o estado atual.",
"routineStarted": "Rotina iniciada",
"routineStatusActive": "Ativa",
"routineStatusArchived": "Arquivada",
+6
View File
@@ -722,6 +722,7 @@
"routeOutcomeSkippedUserAction": "已跳过(用户操作)",
"routineActivated": "例行程序已激活",
"routineCreated": "例行程序已创建",
"routineCreatedWithoutSchedule": "例行程序已创建,但未设置计划:{{error}}",
"routineDeleted": "例行程序已删除",
"routineFired": "例行程序已触发",
"routineNotFiring": "无法运行:例行程序状态为 {{status}}",
@@ -729,6 +730,11 @@
"routines": "例行程序",
"routinesAutomaticallyCreateTasksOnA": "例行程序按计划或 webhook 触发自动创建任务。",
"routineSaved": "例行程序已保存",
"routineSavedScheduleChangeFailed": "例行程序已保存,但计划变更失败:{{error}}",
"routineSavedScheduleDeleteFailed_one": "例行程序已保存,但删除旧计划失败:{{error}}。目前有 {{count}} 个 cron 计划处于活动状态。",
"routineSavedScheduleDeleteFailed_other": "例行程序已保存,但删除旧计划失败:{{error}}。目前有 {{count}} 个 cron 计划处于活动状态。",
"routineSavedScheduleMayBeStale": "例行程序已保存。显示的计划可能已过期,请重新加载页面。",
"routineScheduleFateUnknown": "例行程序已保存,但计划的状态未知。请重新加载页面以查看当前状态。",
"routineStarted": "例行程序已启动",
"routineStatusActive": "活动",
"routineStatusArchived": "已归档",
+6
View File
@@ -722,6 +722,7 @@
"routeOutcomeSkippedUserAction": "已跳過(用戶操作)",
"routineActivated": "例行程序已激活",
"routineCreated": "例行程序已建立",
"routineCreatedWithoutSchedule": "例行程序已建立,但未設定計劃:{{error}}",
"routineDeleted": "例行程序已刪除",
"routineFired": "例行程序已觸發",
"routineNotFiring": "無法執行:例行程序狀態為 {{status}}",
@@ -729,6 +730,11 @@
"routines": "例行程序",
"routinesAutomaticallyCreateTasksOnA": "例行程序按計劃或 webhook 觸發自動建立任務。",
"routineSaved": "例行程序已儲存",
"routineSavedScheduleChangeFailed": "例行程序已儲存,但計劃變更失敗:{{error}}",
"routineSavedScheduleDeleteFailed_one": "例行程序已儲存,但刪除舊計劃失敗:{{error}}。目前有 {{count}} 個 cron 計劃處於活動狀態。",
"routineSavedScheduleDeleteFailed_other": "例行程序已儲存,但刪除舊計劃失敗:{{error}}。目前有 {{count}} 個 cron 計劃處於活動狀態。",
"routineSavedScheduleMayBeStale": "例行程序已儲存。顯示的計劃可能已過期,請重新載入頁面。",
"routineScheduleFateUnknown": "例行程序已儲存,但計劃的狀態未知。請重新載入頁面以查看目前狀態。",
"routineStarted": "例行程序已啓動",
"routineStatusActive": "活動",
"routineStatusArchived": "已歸檔",
+6
View File
@@ -722,6 +722,7 @@
"routeOutcomeSkippedUserAction": "已跳過(使用者操作)",
"routineActivated": "例行程式已啟用",
"routineCreated": "例行程式已建立",
"routineCreatedWithoutSchedule": "例行程式已建立,但未設定計劃:{{error}}",
"routineDeleted": "例行程式已刪除",
"routineFired": "例行程式已觸發",
"routineNotFiring": "無法執行:例行程式狀態為 {{status}}",
@@ -729,6 +730,11 @@
"routines": "例行程式",
"routinesAutomaticallyCreateTasksOnA": "例行程式按計劃或 webhook 觸發自動建立任務。",
"routineSaved": "例行程式已儲存",
"routineSavedScheduleChangeFailed": "例行程式已儲存,但計劃變更失敗:{{error}}",
"routineSavedScheduleDeleteFailed_one": "例行程式已儲存,但刪除舊計劃失敗:{{error}}。目前有 {{count}} 個 cron 計劃處於活動狀態。",
"routineSavedScheduleDeleteFailed_other": "例行程式已儲存,但刪除舊計劃失敗:{{error}}。目前有 {{count}} 個 cron 計劃處於活動狀態。",
"routineSavedScheduleMayBeStale": "例行程式已儲存。顯示的計劃可能已過期,請重新載入頁面。",
"routineScheduleFateUnknown": "例行程式已儲存,但計劃的狀態未知。請重新載入頁面以檢視目前狀態。",
"routineStarted": "例行程式已啟動",
"routineStatusActive": "活動",
"routineStatusArchived": "已歸檔",
@@ -0,0 +1,94 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { cleanup, render, screen, waitFor } from "@testing-library/react";
import { StateProvider } from "@/components/state-provider";
import { defaultOfficeState } from "@/lib/state/slices/office/office-slice";
import type { Routine, RoutineTrigger } from "@/lib/state/slices/office/types";
import { RoutineDetailRoute } from "./office-routine-client-routes";
vi.mock("@/lib/routing/client-router", () => ({
useRouter: () => ({ refresh: vi.fn(), push: vi.fn(), replace: vi.fn() }),
}));
vi.mock("sonner", () => ({ toast: { success: vi.fn(), error: vi.fn() } }));
const { getRoutine, listRoutineTriggers } = vi.hoisted(() => ({
getRoutine: vi.fn(),
listRoutineTriggers: vi.fn(),
}));
vi.mock("@/lib/api/domains/office-api", async () => {
const actual = await vi.importActual<typeof import("@/lib/api/domains/office-api")>(
"@/lib/api/domains/office-api",
);
return { ...actual, getRoutine, listRoutineTriggers };
});
afterEach(() => {
cleanup();
vi.clearAllMocks();
});
const WORKSPACE_ID = "ws-1";
const TIMESTAMP = "2026-05-04T00:00:00Z";
const ROUTINE_ID = "routine-1";
const routine: Routine = {
id: ROUTINE_ID,
workspaceId: WORKSPACE_ID,
name: "Nightly sync",
taskTemplate: {},
status: "active",
concurrencyPolicy: "coalesce_if_active",
createdAt: TIMESTAMP,
updatedAt: TIMESTAMP,
};
const armedCronTrigger: RoutineTrigger = {
id: "trigger-1",
routineId: ROUTINE_ID,
kind: "cron",
cronExpression: "*/5 * * * *",
timezone: "UTC",
nextRunAt: "2026-05-05T00:00:00Z",
enabled: true,
createdAt: TIMESTAMP,
updatedAt: TIMESTAMP,
};
function renderRoute() {
return render(
<StateProvider
initialState={{
workspaces: { activeId: WORKSPACE_ID, items: [] },
office: { ...defaultOfficeState.office },
}}
>
<RoutineDetailRoute routineId={ROUTINE_ID} />
</StateProvider>,
);
}
describe("RoutineDetailRoute — trigger-list load failure", () => {
it("does not silently render as if the routine had no trigger when the trigger list fails to load", async () => {
getRoutine.mockResolvedValue(routine);
listRoutineTriggers.mockRejectedValue(new Error("network down"));
renderRoute();
await waitFor(() => expect(screen.getByText("network down")).toBeTruthy());
// The routine's own detail form (and its trigger-reconciliation state,
// which would otherwise treat the already-armed cron trigger as absent
// and let a later Save create a duplicate) must never mount on this path.
expect(screen.queryByDisplayValue(routine.name)).toBeNull();
});
it("still renders the routine's real trigger when both loads succeed", async () => {
getRoutine.mockResolvedValue(routine);
listRoutineTriggers.mockResolvedValue({ triggers: [armedCronTrigger] });
renderRoute();
await waitFor(() => expect(screen.getByDisplayValue(routine.name)).toBeTruthy());
expect(screen.getByDisplayValue(armedCronTrigger.cronExpression ?? "")).toBeTruthy();
});
});
@@ -19,16 +19,17 @@ export function RoutineDetailRoute({ routineId }: { routineId: string }) {
setState({ status: "loading" });
async function loadRoutineDetail(): Promise<RoutineDetailData> {
// A failed trigger list must not silently become "no trigger exists":
// save-time reconciliation (cron-reconcile.ts) trusts this initial list
// to detect an already-armed cron trigger, so swallowing a failure here
// would make a later Save create a duplicate schedule instead of
// replacing the one that failed to load. Let it fail the whole load
// instead, same as a `getRoutine` failure below.
const [routineResponse, triggersResponse] = await Promise.all([
getRoutine(routineId, { cache: "no-store" }),
listRoutineTriggers(routineId, { cache: "no-store" }).catch(() => ({
triggers: [] as RoutineTrigger[],
})),
listRoutineTriggers(routineId, { cache: "no-store" }),
]);
const routine =
(routineResponse as unknown as { routine?: Routine }).routine ??
(routineResponse as unknown as Routine);
return { routine, triggers: triggersResponse.triggers ?? [] };
return { routine: routineResponse, triggers: triggersResponse.triggers ?? [] };
}
loadRoutineDetail()