shoc-frontend-new/src/test/domain/work-orders/utils/format-board-audit-action.test.ts

168 lines
4.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { formatBoardAuditAction } from "@/domain/work-orders/utils/format-board-audit-action";
describe("formatBoardAuditAction", () => {
it("keeps human sentences from action as-is", () => {
expect(
formatBoardAuditAction({
action: 'Changed status to "Incomplete"',
fieldName: "",
oldValue: "",
newValue: "",
}),
).toBe('Changed status to "Incomplete"');
});
it("formats lifecycle status from field/newValue", () => {
expect(
formatBoardAuditAction({
action: "StatusChanged",
fieldName: "LifecycleStatus",
oldValue: "Scheduled",
newValue: "Incomplete",
}),
).toBe('Changed status from "Scheduled" to "Incomplete"');
});
it("includes the previous status in the audit sentence when present", () => {
const sentence = formatBoardAuditAction({
action: "StatusChanged",
fieldName: "LifecycleStatus",
oldValue: "Pending",
newValue: "En Route",
});
expect(sentence).toBe('Changed status from "Pending" to "En Route"');
});
it("omits from when the previous status is empty", () => {
expect(
formatBoardAuditAction({
action: "StatusChanged",
fieldName: "LifecycleStatus",
oldValue: "",
newValue: "Scheduled",
}),
).toBe('Changed status to "Scheduled"');
});
it("formats WeekRolled / CarriedOver with increment", () => {
expect(
formatBoardAuditAction({
action: "WeekRolled",
fieldName: "CarriedOver",
oldValue: "0",
newValue: "1",
}),
).toBe("Carried over to the next week (increment #1)");
});
it("formats schedule changes", () => {
expect(
formatBoardAuditAction({
action: "Rescheduled",
fieldName: "ScheduledDate",
oldValue: "2026-07-10",
newValue: "2026-07-16",
}),
).toBe('Changed schedule to "07/16/2026"');
});
it("formats appt time ISO range as mm/dd/yyyy", () => {
expect(
formatBoardAuditAction({
action: "ApptTimeChanged",
fieldName: "ApptTime",
oldValue: "",
newValue: "2026-07-13T05:30:00.0000000|2026-07-13T07:00:00.0000000",
}),
).toBe('Changed appt time to "07/13/2026"');
});
it("formats past due system message", () => {
expect(
formatBoardAuditAction({
action: "MarkedPastDue",
fieldName: "IsPastDue",
oldValue: "false",
newValue: "true",
}),
).toBe("WO marked as Past Due. Schedule On has passed without completion.");
});
it("formats type correction as Changed Type to (AAP, new value only)", () => {
expect(
formatBoardAuditAction({
action: "FieldPatched",
fieldName: "WorkOrderType",
oldValue: "PM",
newValue: "Emergency",
}),
).toBe('Changed Type to "Emergency"');
});
it("formats severity as Changed Severity to SEV N", () => {
expect(
formatBoardAuditAction({
action: "FieldPatched",
fieldName: "Severity",
oldValue: "3",
newValue: "1",
}),
).toBe('Changed Severity to "SEV 1"');
});
it("falls back to from → to for unknown fields", () => {
expect(
formatBoardAuditAction({
action: "FieldPatched",
fieldName: "VendorNte",
oldValue: "100",
newValue: "250",
}),
).toBe('Changed vendor nte from "100" to "250"');
});
it("formats JSON array values as comma-separated quoted items", () => {
expect(
formatBoardAuditAction({
action: "FieldPatched",
fieldName: "ExtraServices",
oldValue: "",
newValue: '["Other","Installation","Preventive Maintenance"]',
}),
).toBe('Changed extra services to "Other", "Installation", "Preventive Maintenance"');
});
it("formats AssignmentChanged as AAP Assigned To copy", () => {
expect(
formatBoardAuditAction({
action: "AssignmentChanged",
fieldName: "AssignTo",
oldValue: "Unassigned",
newValue: "Morgan Blake",
}),
).toBe('Changed Assigned To to "Morgan Blake"');
});
it("formats empty AssignTo as an em dash", () => {
expect(
formatBoardAuditAction({
action: "AssignmentChanged",
fieldName: "AssignedTo",
oldValue: "Morgan Blake",
newValue: "",
}),
).toBe('Changed Assigned To to "—"');
});
it("falls back to Updated work order when empty", () => {
expect(
formatBoardAuditAction({
action: "",
fieldName: "Something",
oldValue: "",
newValue: "",
}),
).toBe("Updated work order");
});
});