shoc-frontend-new/src/test/domain/work-orders/utils/completion-doc-auto-complete.test.ts
Codex Review Integration 09636ef590 fix(work-orders): complete the work order when its completion document is generated (SH-341)
Generating or uploading the completion document now moves a closable work
order to Completed without the manual confirmation, as SH-115 requires. The
SH-114 closability gate is evaluated on the row the server returned after the
document save; Save draft, Not required, Past Due, Completed and Canceled rows
are never auto-completed. The follow-up status patch uses that row's
rowVersion so it does not conflict with the document save.
2026-09-16 11:43:33 -03:00

67 lines
2.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import type { WorkOrderTableRow } from "@/domain/work-orders/types/work-order-table-row";
import { shouldCompleteAfterCompletionDoc } from "@/domain/work-orders/utils/completion-doc-auto-complete";
function closablePmRow(overrides: Partial<WorkOrderTableRow> = {}): WorkOrderTableRow {
return {
id: 42,
woNumber: "24866",
type: "PM",
dispatcherId: "u1",
dispatcherName: "Dispatcher",
company: "Vendor Co",
vendorId: "7",
tech: "Tech One",
completedDate: "2026-09-15",
pm: "HVAC",
status: "In Progress",
docStatus: "Yes",
mediaCount: 0,
isPastDue: false,
upliftSummary: { hasUplift: false, pendingCount: 0 },
...overrides,
} as WorkOrderTableRow;
}
describe("shouldCompleteAfterCompletionDoc (SH-341)", () => {
it("completes a closable PM once the generated document is persisted", () => {
expect(shouldCompleteAfterCompletionDoc("Yes", closablePmRow())).toBe(true);
});
it("never completes on Save draft or Not required", () => {
expect(shouldCompleteAfterCompletionDoc("No", closablePmRow({ docStatus: "No" }))).toBe(false);
expect(shouldCompleteAfterCompletionDoc("NN", closablePmRow({ docStatus: "NN" }))).toBe(false);
});
it("does not complete when the server did not persist the document as generated", () => {
expect(shouldCompleteAfterCompletionDoc("Yes", closablePmRow({ docStatus: "No" }))).toBe(false);
});
it.each([
["dispatcher", { dispatcherId: "", dispatcherName: "" }],
["vendor", { company: "", vendorId: "" }],
["completed date", { completedDate: "" }],
["real WO number", { woNumber: "SH02137" }],
["service", { pm: "" }],
["pending uplift", { upliftSummary: { hasUplift: true, pendingCount: 1 } }],
["photo on a Reactive WO", { type: "Reactive" as const, mediaCount: 0 }],
])("keeps the SH-114 closability gate: missing %s blocks completion", (_label, overrides) => {
expect(shouldCompleteAfterCompletionDoc("Yes", closablePmRow(overrides))).toBe(false);
});
it("completes a Reactive WO that has its completion photo", () => {
expect(
shouldCompleteAfterCompletionDoc("Yes", closablePmRow({ type: "Reactive", mediaCount: 1 })),
).toBe(true);
});
it("leaves Completed, Canceled, and Past Due rows unchanged", () => {
expect(shouldCompleteAfterCompletionDoc("Yes", closablePmRow({ status: "Completed" }))).toBe(
false,
);
expect(shouldCompleteAfterCompletionDoc("Yes", closablePmRow({ status: "Canceled" }))).toBe(
false,
);
expect(shouldCompleteAfterCompletionDoc("Yes", closablePmRow({ isPastDue: true }))).toBe(false);
});
});