Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/services/automation/automation.template-i18n.test.ts
T

199 lines
7.4 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
t: vi.fn((key: string) => `en:${key}`),
tx: {
automationTemplate: { findMany: vi.fn(), findFirst: vi.fn() },
automationRule: { findMany: vi.fn(), findFirst: vi.fn(), create: vi.fn() },
automationExecution: { findFirst: vi.fn(), updateMany: vi.fn() },
},
}));
vi.mock("~/locale/i18next.server", () => ({
getTranslations: async () => ({ t: mocks.t, translations: {}, locale: "en" }),
}));
vi.mock("~/utils/loaders.middleware", () => ({
requireAuth: vi.fn(),
}));
vi.mock("~/utils/services/.server/urlService", () => ({
getTenantIdFromUrl: async () => "tenant-1",
}));
vi.mock("~/utils/session.server", () => ({
getUserInfo: async () => ({ userId: "user-1" }),
}));
vi.mock("~/utils/connector/tenantConnector.server", () => ({
tenantHasModule: async () => true,
}));
vi.mock("~/utils/db/permissions/userRoles.db.server", () => ({
getPermissionsByUser: async () => ["automation.read", "automation.manage"],
}));
vi.mock("~/utils/db/.server/tenantRls.server", () => ({
withTenantRLS: async (_tenantId: string, callback: (tx: typeof mocks.tx) => Promise<unknown>) => callback(mocks.tx),
}));
import { automationTemplateCatalog, automationTemplateFlowData } from "./automation.registry";
import {
createAutomationRuleFromTemplate,
handleAutomationExecutionDetailAction,
loadAutomationExecutionDetailVM,
loadAutomationTemplateCatalogVM,
} from "./automation.server";
const platformTemplate = automationTemplateCatalog[0];
const platformFlowData = automationTemplateFlowData(platformTemplate);
describe("automation platform template i18n", () => {
beforeEach(() => {
vi.clearAllMocks();
mocks.tx.automationRule.findMany.mockResolvedValue([]);
mocks.tx.automationRule.create.mockResolvedValue({ id: "rule-1" });
mocks.tx.automationExecution.updateMany.mockResolvedValue({ count: 1 });
});
it("localizes platform cards while preserving tenant-authored copy", async () => {
mocks.tx.automationTemplate.findMany.mockResolvedValue([
{
id: "platform-template",
tenantId: null,
name: platformTemplate.defaultName,
description: platformTemplate.defaultDescription,
category: platformTemplate.category,
createdFromRuleId: null,
flowData: platformFlowData,
},
{
id: "tenant-template",
tenantId: "tenant-1",
name: "Customer-authored template.key",
description: "Customer-authored description.key",
category: "operations",
createdFromRuleId: null,
flowData: platformFlowData,
},
]);
const vm = await loadAutomationTemplateCatalogVM({ request: new Request("http://app.localhost/templates"), params: { tenant: "demo" } });
expect(vm.templates[0]).toMatchObject({
name: `en:${platformTemplate.nameKey}`,
description: `en:${platformTemplate.descriptionKey}`,
isTenantTemplate: false,
});
expect(vm.templates[1]).toMatchObject({
name: "Customer-authored template.key",
description: "Customer-authored description.key",
isTenantTemplate: true,
});
expect(mocks.t).not.toHaveBeenCalledWith("Customer-authored template.key");
expect(mocks.t).not.toHaveBeenCalledWith("Customer-authored description.key");
});
it("localizes platform rule names, descriptions and executable config", async () => {
mocks.tx.automationTemplate.findFirst.mockResolvedValue({
tenantId: null,
name: platformTemplate.defaultName,
description: platformTemplate.defaultDescription,
flowData: platformFlowData,
});
await createAutomationRuleFromTemplate({
request: new Request("http://app.localhost/templates"),
params: { tenant: "demo" },
templateId: "platform-template",
});
const createInput = mocks.tx.automationRule.create.mock.calls[0]?.[0];
expect(createInput.data).toMatchObject({
name: `en:${platformTemplate.nameKey}`,
description: `en:${platformTemplate.descriptionKey}`,
});
expect(createInput.data.steps.create.find((step: { stepKey: string }) => step.stepKey === "create-task")?.config).toMatchObject({
titel: "en:automation.templates.leadQualifiedFollowUp.taskTitle",
zustaendig: "besitzerDerAutomation",
});
expect(createInput.data.steps.create.find((step: { stepKey: string }) => step.stepKey === "notify-office")?.config).toMatchObject({
betreff: "en:automation.templates.leadQualifiedFollowUp.notificationSubject",
nachricht: "en:automation.templates.leadQualifiedFollowUp.notificationMessage",
});
});
it("copies tenant-authored templates byte-for-byte instead of translating key-like text", async () => {
mocks.tx.automationTemplate.findFirst.mockResolvedValue({
tenantId: "tenant-1",
name: "Customer-authored template.key",
description: "Customer-authored description.key",
flowData: platformFlowData,
});
await createAutomationRuleFromTemplate({
request: new Request("http://app.localhost/templates"),
params: { tenant: "demo" },
templateId: "tenant-template",
});
expect(mocks.tx.automationRule.create.mock.calls[0]?.[0].data).toMatchObject({
name: "Customer-authored template.key",
description: "Customer-authored description.key",
});
expect(mocks.t).not.toHaveBeenCalledWith("Customer-authored template.key");
expect(mocks.t).not.toHaveBeenCalledWith("Customer-authored description.key");
});
it("returns the reverse confirmation phrase in the request locale", async () => {
mocks.tx.automationExecution.findFirst.mockResolvedValue({
id: "execution-1",
status: "success",
dryRun: false,
triggerKey: "forms.submitted",
summaryText: null,
errorText: null,
createdAt: new Date("2026-07-02T09:00:00.000Z"),
startedAt: new Date("2026-07-02T09:00:00.000Z"),
endedAt: new Date("2026-07-02T09:01:00.000Z"),
durationMs: 60_000,
reverseWindowExpiresAt: new Date("2099-07-03T09:00:00.000Z"),
rule: { id: "rule-1", name: "Follow-up" },
stepExecutions: [],
});
const vm = await loadAutomationExecutionDetailVM({
request: new Request("http://app.localhost/demo/automations/rule-1/executions/execution-1"),
params: { tenant: "demo", id: "rule-1", executionId: "execution-1" },
});
expect(vm.reversePlan.confirmationPhrase).toBe("en:automation.detail.reverse.confirmationPhrase");
});
it("validates reverse confirmation against the same request-localized phrase", async () => {
const params = { tenant: "demo", id: "rule-1", executionId: "execution-1" };
const submit = (confirmationPhrase: string) =>
handleAutomationExecutionDetailAction({
request: new Request("http://app.localhost/demo/automations/rule-1/executions/execution-1", {
method: "POST",
body: new URLSearchParams({ intent: "reverse", confirmationPhrase }),
}),
params,
});
await expect(submit("AUTOMATION RUECKGAENGIG")).resolves.toEqual({
ok: false,
errorKey: "automation.detail.reverse.confirmationRequired",
});
mocks.tx.automationExecution.findFirst.mockResolvedValue({
reverseWindowExpiresAt: new Date("2099-07-03T09:00:00.000Z"),
stepExecutions: [],
});
await expect(submit("en:automation.detail.reverse.confirmationPhrase")).resolves.toEqual({
ok: false,
errorKey: "automation.detail.reverse.nothingReversible",
});
});
});