shoc-frontend-new/src/test/domain/work-orders/utils/format-board-audit-action.test.ts
Codex Review Integration e46e42df69 fix(work-orders): show full audit timestamps and previous type values (SH-361, SH-363)
Audit Log entries now show date and time for every entry instead of a
time-only stamp for manual changes. Type and Severity entries read
'Changed Type from X to Y', a severity cleared by a type correction reads
'from SEV N to —', and the generic fallback no longer renders a raw
FieldChanged action name.
2026-09-16 11:43:34 -03:00

209 lines
5.8 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 with the previous and new type (SH-363)", () => {
expect(
formatBoardAuditAction({
action: "FieldChanged",
fieldName: "WorkOrderType",
oldValue: "PM",
newValue: "Reactive",
}),
).toBe('Changed Type from "PM" to "Reactive"');
});
it("formats a first type assignment without a from clause", () => {
expect(
formatBoardAuditAction({
action: "FieldChanged",
fieldName: "WorkOrderType",
oldValue: "",
newValue: "Emergency",
}),
).toBe('Changed Type to "Emergency"');
});
it("formats severity with the previous and new SEV labels", () => {
expect(
formatBoardAuditAction({
action: "FieldChanged",
fieldName: "Severity",
oldValue: "3",
newValue: "1",
}),
).toBe('Changed Severity from "SEV 3" to "SEV 1"');
});
it("formats severity cleared by a type correction instead of a raw FieldChanged (SH-363)", () => {
expect(
formatBoardAuditAction({
action: "FieldChanged",
fieldName: "Severity",
oldValue: "3",
newValue: "",
}),
).toBe('Changed Severity from "SEV 3" to "—"');
});
it("never renders a raw audit action enum for a cleared unknown field", () => {
const cleared = formatBoardAuditAction({
action: "FieldChanged",
fieldName: "VendorNte",
oldValue: "100",
newValue: "",
});
const empty = formatBoardAuditAction({
action: "FieldChanged",
fieldName: "VendorNte",
oldValue: "",
newValue: "",
});
expect(cleared).toBe('Changed vendor nte from "100" to "—"');
expect(empty).toBe("Updated vendor nte");
expect([cleared, empty]).not.toContain("FieldChanged");
});
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");
});
});