200 lines
6.0 KiB
TypeScript
200 lines
6.0 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { createAdminTelephonyActionsService, evaluateAdminOutboundGate, type AdminTelephonyActionsRepository } from "./admin-telephony-actions.server.js";
|
|
|
|
const ENABLED_POLICY = {
|
|
outboundEnabled: true,
|
|
dncEnforced: true,
|
|
dailyCallLimit: 20,
|
|
workDayStartMinutes: 9 * 60,
|
|
workDayEndMinutes: 17 * 60,
|
|
workDays: [1, 2, 3, 4, 5],
|
|
timeZone: "Europe/Berlin",
|
|
} as const;
|
|
|
|
function repository(overrides: Partial<AdminTelephonyActionsRepository> = {}): AdminTelephonyActionsRepository {
|
|
return {
|
|
saveOutcome: vi.fn().mockResolvedValue({
|
|
call: { id: "call_1" },
|
|
callbackTask: null,
|
|
}),
|
|
confirmMatch: vi.fn().mockResolvedValue({ id: "call_1" }),
|
|
updatePolicy: vi.fn().mockResolvedValue({ id: "policy_1" }),
|
|
createDialerList: vi.fn().mockResolvedValue({ id: "list_1" }),
|
|
activateNextDialerEntry: vi.fn().mockResolvedValue({
|
|
status: "completed",
|
|
list: { id: "list_1" },
|
|
}),
|
|
finishDialerEntry: vi.fn().mockResolvedValue({ id: "entry_1" }),
|
|
queueOutboundCall: vi.fn().mockResolvedValue({ id: "request_1" }),
|
|
...overrides,
|
|
} as never;
|
|
}
|
|
|
|
describe("admin outbound gate", () => {
|
|
const mondayAtTenBerlin = new Date("2026-07-20T08:00:00.000Z");
|
|
|
|
it("fails closed until every administrative outbound setting exists", () => {
|
|
expect(
|
|
evaluateAdminOutboundGate({
|
|
policy: null,
|
|
contactDoNotCall: false,
|
|
callsToday: 0,
|
|
now: mondayAtTenBerlin,
|
|
})
|
|
).toEqual({ allowed: false, reason: "policy_incomplete" });
|
|
expect(
|
|
evaluateAdminOutboundGate({
|
|
policy: { ...ENABLED_POLICY, dailyCallLimit: null },
|
|
contactDoNotCall: false,
|
|
callsToday: 0,
|
|
now: mondayAtTenBerlin,
|
|
})
|
|
).toEqual({ allowed: false, reason: "policy_incomplete" });
|
|
});
|
|
|
|
it("enforces DNC, daily limits and local working hours in that order", () => {
|
|
expect(
|
|
evaluateAdminOutboundGate({
|
|
policy: ENABLED_POLICY,
|
|
contactDoNotCall: true,
|
|
callsToday: 0,
|
|
now: mondayAtTenBerlin,
|
|
})
|
|
).toEqual({ allowed: false, reason: "do_not_call" });
|
|
expect(
|
|
evaluateAdminOutboundGate({
|
|
policy: ENABLED_POLICY,
|
|
contactDoNotCall: false,
|
|
callsToday: 20,
|
|
now: mondayAtTenBerlin,
|
|
})
|
|
).toEqual({ allowed: false, reason: "daily_limit_reached" });
|
|
expect(
|
|
evaluateAdminOutboundGate({
|
|
policy: ENABLED_POLICY,
|
|
contactDoNotCall: false,
|
|
callsToday: 0,
|
|
now: new Date("2026-07-20T05:00:00.000Z"),
|
|
})
|
|
).toEqual({ allowed: false, reason: "outside_working_hours" });
|
|
});
|
|
|
|
it("allows a configured in-budget call during local working hours", () => {
|
|
expect(
|
|
evaluateAdminOutboundGate({
|
|
policy: ENABLED_POLICY,
|
|
contactDoNotCall: false,
|
|
callsToday: 4,
|
|
now: mondayAtTenBerlin,
|
|
})
|
|
).toEqual({ allowed: true });
|
|
});
|
|
});
|
|
|
|
describe("admin telephony actions service", () => {
|
|
it("normalizes one atomic call outcome and callback task", async () => {
|
|
const repo = repository();
|
|
const authorize = vi.fn();
|
|
const service = createAdminTelephonyActionsService({
|
|
repository: repo,
|
|
authorize,
|
|
});
|
|
const dueAt = new Date("2026-07-21T08:00:00.000Z");
|
|
|
|
await service.saveOutcome({
|
|
adminUserId: "admin_1",
|
|
callSessionId: "call_1",
|
|
note: " Demo vereinbart ",
|
|
dispositionCode: " interested ",
|
|
callbackTask: {
|
|
title: " Demo bestätigen ",
|
|
dueAt,
|
|
timeZone: "Europe/Berlin",
|
|
priority: "high",
|
|
},
|
|
actor: { adminUserId: "admin_1", reason: " Ergebnis speichern " },
|
|
});
|
|
|
|
expect(authorize).toHaveBeenCalledWith("admin_sales.phone.call");
|
|
expect(repo.saveOutcome).toHaveBeenCalledWith({
|
|
adminUserId: "admin_1",
|
|
callSessionId: "call_1",
|
|
note: "Demo vereinbart",
|
|
dispositionCode: "interested",
|
|
callbackTask: {
|
|
title: "Demo bestätigen",
|
|
dueAt,
|
|
timeZone: "Europe/Berlin",
|
|
priority: "high",
|
|
},
|
|
actor: {
|
|
adminUserId: "admin_1",
|
|
reason: "Ergebnis speichern",
|
|
},
|
|
});
|
|
});
|
|
|
|
it("rejects an enabled policy with hidden defaults or disabled DNC", async () => {
|
|
const repo = repository();
|
|
const service = createAdminTelephonyActionsService({
|
|
repository: repo,
|
|
authorize: vi.fn(),
|
|
});
|
|
|
|
await expect(
|
|
service.updatePolicy({
|
|
adminUserId: "admin_1",
|
|
outboundEnabled: true,
|
|
dncEnforced: false,
|
|
dailyCallLimit: 20,
|
|
workDayStartMinutes: 540,
|
|
workDayEndMinutes: 1_020,
|
|
workDays: [1, 2, 3, 4, 5],
|
|
timeZone: "Europe/Berlin",
|
|
actor: { adminUserId: "admin_1", reason: "Limits setzen" },
|
|
})
|
|
).rejects.toMatchObject({ code: "policy_incomplete" });
|
|
expect(repo.updatePolicy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("deduplicates a dialer audience while preserving its order", async () => {
|
|
const repo = repository();
|
|
const service = createAdminTelephonyActionsService({
|
|
repository: repo,
|
|
authorize: vi.fn(),
|
|
});
|
|
|
|
await service.createDialerList({
|
|
adminUserId: "admin_1",
|
|
name: " Trial Follow-up ",
|
|
contactIds: ["contact_2", "contact_1", "contact_2"],
|
|
actor: { adminUserId: "admin_1", reason: "Liste starten" },
|
|
});
|
|
|
|
expect(repo.createDialerList).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
name: "Trial Follow-up",
|
|
contactIds: ["contact_2", "contact_1"],
|
|
})
|
|
);
|
|
});
|
|
|
|
it("requires exactly one contact or lead when confirming a match", async () => {
|
|
const service = createAdminTelephonyActionsService({
|
|
repository: repository(),
|
|
authorize: vi.fn(),
|
|
});
|
|
|
|
await expect(
|
|
service.confirmMatch({
|
|
adminUserId: "admin_1",
|
|
callSessionId: "call_1",
|
|
contactId: "contact_1",
|
|
leadId: "lead_1",
|
|
actor: { adminUserId: "admin_1", reason: "Zuordnen" },
|
|
})
|
|
).rejects.toMatchObject({ code: "match_requires_one_entity" });
|
|
});
|
|
});
|