import { expect, test, type Route } from "@playwright/test"; const TEST_USER = { token: "work-order-vendor-e2e-token", expiration: new Date(Date.now() + 60 * 60 * 1000).toISOString(), email: "admin@seahavenind.com", userRoles: "Admin", phoneNumber: "5551234567", fullname: "Work Order E2E Admin", id: "1", }; async function fulfillJson(route: Route, body: unknown, status = 200) { await route.fulfill({ status, contentType: "application/json", body: JSON.stringify(body) }); } test("edits only the vendor assigned to the current work order", async ({ page }) => { let updateRequest: { url: string; body: Record } | undefined; await page.addInitScript((user) => localStorage.setItem("auth", JSON.stringify(user)), TEST_USER); await page.route("**/api/WorkOrder/GetWorkorderById*", (route) => fulfillJson(route, { Id: 501, InternalWONumber: "WO-501", WorkerOrderNumber: "CUSTOMER-501", WorkerOrderTitle: "Emergency boiler repair", Status: "Dispatched", Comments: [], AuditLog: [], Dispatches: [ { Id: 801, DispatchNumber: "D-801", VendorId: 7, VendorName: "Gateway Plumbing", VendorEmail: "dispatch@gateway.test", Status: "Sent", DispatchedAt: "2026-07-23T12:00:00Z", }, ], }), ); await page.route("**/api/vendors/7/work-order-update", async (route) => { updateRequest = { url: route.request().url(), body: route.request().postDataJSON() as Record, }; await fulfillJson(route, { data: { id: 7 } }); }); await page.route("**/api/vendors/7", (route) => fulfillJson(route, { data: { Id: 7, CompanyName: "Gateway Plumbing", ContactName: "Adam Whyte", PreferredContact: "Email", Phone: "3145550198", Email: "dispatch@gateway.test", Notes: "Preferred after-hours vendor", IsActive: true, }, }), ); await page.route("**/api/workorders/lookups/dispatchers", (route) => fulfillJson(route, { data: [] }), ); await page.route("**/api/User*", (route) => fulfillJson(route, { data: [] })); await page.goto("/workorders/501"); await expect(page.getByRole("heading", { name: "Emergency boiler repair" })).toBeVisible(); await page.getByRole("button", { name: "Edit vendor" }).click(); await expect(page.getByRole("dialog", { name: "Edit assigned vendor" })).toBeVisible(); await expect(page.getByText("Preferred contact")).toHaveCount(0); await expect(page.getByLabel("Technician name")).toHaveValue("Adam Whyte"); await page.getByLabel("Technician name").fill("Taylor Tech"); await page.getByLabel("Phone").fill("3145550111"); await page.getByLabel("Email").fill("taylor@example.com"); await page.getByLabel("Notes").fill("Text before arrival"); await page.getByRole("button", { name: "Save" }).click(); await expect(page.getByRole("dialog", { name: "Edit assigned vendor" })).toBeHidden(); expect(updateRequest?.url).toMatch(/\/api\/vendors\/7\/work-order-update$/); expect(updateRequest?.body).toEqual({ workOrderId: 501, contactName: "Taylor Tech", phone: "(314) 555-0111", email: "taylor@example.com", notes: "Text before arrival", }); });