import { fireEvent, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { PMTypeCell } from "@/app/(protected)/workorders/_components/list/table/cells/pm-type-cell";
import { ServiceNotesDialog } from "@/app/(protected)/workorders/_components/list/table/cells/service-notes-dialog";
import { serviceIdentityPatch } from "@/domain/work-orders/utils/service-identity-patch";
import { renderWithProviders } from "@/test/test-utils";
describe("Aveta Service & Notes", () => {
it("shows eligible registry services and saves the selected service identity", () => {
const onSave = vi.fn();
renderWithProviders(
,
);
fireEvent.click(screen.getByRole("button", { name: /Old snapshot/i }));
expect(screen.getByRole("button", { name: "QA Test Service" })).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Leak" })).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Reactive Only" })).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "QA Test Service" }));
expect(onSave).toHaveBeenCalledWith("QA Test Service", 701);
});
it("keeps the historical label when its linked registry service is inactive", () => {
renderWithProviders(
,
);
expect(screen.getByRole("button", { name: /Historic Service Name/i })).toBeInTheDocument();
expect(screen.queryByRole("button", { name: /Renamed Service/i })).not.toBeInTheDocument();
});
it("preserves the historic identity when only service notes change", () => {
const onSaveDetails = vi.fn();
renderWithProviders(
,
);
fireEvent.click(screen.getByRole("button", { name: /Historic Service Name/i }));
fireEvent.click(screen.getByRole("button", { name: /Add service and notes/i }));
fireEvent.change(
screen.getByPlaceholderText(
"Describe the specific issue, location details, door number, etc.",
),
{ target: { value: "Changed detail only" } },
);
fireEvent.click(screen.getByRole("button", { name: "Save" }));
const saved = onSaveDetails.mock.calls[0]![0];
expect(saved).toMatchObject({ pm: "Historic Service Name", serviceId: 701 });
expect(serviceIdentityPatch(701, "Historic Service Name", saved.pm, saved.serviceId)).toEqual(
{},
);
});
it("shows the Aveta shield on the Service cell when required", () => {
renderWithProviders(
,
);
expect(screen.getByLabelText("Aveta required")).toBeInTheDocument();
});
it("shows the green Aveta shield when the document is attached", () => {
renderWithProviders(
,
);
expect(screen.getByLabelText("Aveta attached")).toBeInTheDocument();
});
it("persists avetaRequired from Service & Notes", () => {
const onSave = vi.fn();
renderWithProviders(
,
);
fireEvent.click(screen.getByRole("checkbox", { name: /Requires Aveta documentation/i }));
fireEvent.click(screen.getByRole("button", { name: "Save" }));
expect(onSave).toHaveBeenCalledWith(
expect.objectContaining({
pm: "HVAC",
avetaRequired: true,
}),
);
});
});