statement.spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import { expect, type Page, test } from '@playwright/test'
  2. import { forgetWorkshopSetting, ownerOrganizationId, workshopSetting } from '../../support/db'
  3. import { settle } from '../../support/hydration'
  4. import { type PdfContent, pdfContent } from '../../support/pdf'
  5. import { addQuotePart, newQuote, quotePdfUrl, quoteShareLink, saveQuote } from '../../support/quote'
  6. import { setWarrantyDefaults } from '../../support/settings'
  7. import { chooseWarrantyStatement, expectWarrantyStatement } from '../../support/warranty'
  8. import { newWorkOrder, saveWorkOrder, seededVehicleUrl } from '../../support/work-order'
  9. /**
  10. * What a customer is told about the workshop's warranty, from the quote they
  11. * accept to the invoice they pay.
  12. *
  13. * A workshop writes its standing answer once (Settings → Warranty). A new
  14. * quote starts from it, the customer reads it on the public link and in the
  15. * PDF before accepting, and the work order made from that quote carries the
  16. * same words onto the invoice. "Not included" is a statement in its own right
  17. * and travels the same way, with the line that the customer's statutory rights
  18. * stand whenever the workshop wrote no sentence of its own.
  19. */
  20. test.describe.configure({ mode: 'serial', timeout: 180_000 })
  21. const stamp = Date.now()
  22. const TERMS = `E2E terms ${stamp}: wear parts are not covered.`
  23. const NO_WARRANTY = `E2E statement ${stamp}: no workshop warranty on customer-supplied parts.`
  24. const PART = `E2E brake disc ${stamp}`
  25. const WARRANTY_KEYS = [
  26. 'warranty.defaultStatus',
  27. 'warranty.defaultMonths',
  28. 'warranty.defaultMileage',
  29. 'warranty.defaultTerms',
  30. 'warranty.notIncludedText',
  31. 'warranty.applyToQuotes',
  32. 'warranty.applyToWorkOrders',
  33. ]
  34. let organizationId = ''
  35. /** 'km' or 'mi', whichever the workshop under test measures in. */
  36. let unit = ''
  37. let includedQuoteUrl = ''
  38. let notIncludedQuoteUrl = ''
  39. async function quotePdf(page: Page, quoteUrl: string): Promise<PdfContent> {
  40. const id = quoteUrl.split('/').pop()
  41. const response = await page.request.get(`/api/protected/quotes/${id}/pdf`, { timeout: 60_000 })
  42. expect(response.status()).toBe(200)
  43. return pdfContent(await response.body())
  44. }
  45. async function invoicePdf(page: Page, workOrderUrl: string): Promise<PdfContent> {
  46. const id = workOrderUrl.split('/').pop()
  47. const response = await page.request.get(`/api/protected/services/${id}/pdf`, {
  48. timeout: 60_000,
  49. })
  50. expect(response.status()).toBe(200)
  51. return pdfContent(await response.body())
  52. }
  53. /** "12 months / 20,000 km", whatever thousands separator the server prints. */
  54. function period(months: number, thousands: number): RegExp {
  55. return new RegExp(`Duration: ${months} months / ${thousands}[,.\\s\\u00a0\\u202f]?000 ${unit}`)
  56. }
  57. /** Converts the open quote and lands on the work order made from it. */
  58. async function convertToWorkOrder(page: Page): Promise<string> {
  59. const convert = page.getByRole('button', { name: 'Convert to Work Order', exact: true })
  60. const dialog = page.getByRole('dialog', { name: 'Convert Quote to Work Order' })
  61. await expect(async () => {
  62. await convert.click()
  63. await expect(dialog).toBeVisible({ timeout: 2_000 })
  64. }).toPass({ timeout: 30_000 })
  65. const confirm = dialog.getByRole('button', { name: 'Convert', exact: true })
  66. await expect(confirm).toBeEnabled()
  67. await confirm.click()
  68. await page.waitForURL(/\/vehicles\/[^/]+\/service\/[^/]+$/, { timeout: 60_000 })
  69. await settle(page)
  70. await expect(page.getByPlaceholder('Name *').first()).toHaveValue(PART)
  71. return page.url()
  72. }
  73. test.beforeAll(async ({ browser }) => {
  74. organizationId = await ownerOrganizationId()
  75. unit = (await workshopSetting(organizationId, 'workshop.unitSystem')) === 'metric' ? 'km' : 'mi'
  76. const page = await browser.newPage({ storageState: 'e2e/.auth/owner.json' })
  77. await setWarrantyDefaults(page, {
  78. newDocumentsSay: 'Warranty included',
  79. months: 12,
  80. distance: 20000,
  81. terms: TERMS,
  82. notIncludedText: NO_WARRANTY,
  83. })
  84. await page.close()
  85. })
  86. test.afterAll(async () => {
  87. // Every other quote and invoice in the suite prints without a warranty panel.
  88. for (const key of WARRANTY_KEYS) await forgetWorkshopSetting(organizationId, key)
  89. })
  90. test.describe('the workshop warranty, from quote to invoice', () => {
  91. test('the settings page keeps what was saved', async ({ page }) => {
  92. await page.goto('/settings/warranty')
  93. await settle(page)
  94. await expect(
  95. page.getByRole('radio', { name: 'Warranty included', exact: true })
  96. ).toHaveAttribute('aria-checked', 'true')
  97. await expect(page.locator('#warrantyDefaultMonths')).toHaveValue('12')
  98. await expect(page.locator('#warrantyDefaultMileage')).toHaveValue('20000')
  99. await expect(page.locator('#warrantyDefaultTerms')).toHaveValue(TERMS)
  100. await expect(page.locator('#warrantyNotIncludedText')).toHaveValue(NO_WARRANTY)
  101. // The distance limit is labelled in the workshop's own unit.
  102. await expect(page.getByText(`Distance limit (${unit})`, { exact: true })).toBeVisible()
  103. })
  104. test("a new quote starts from the workshop's warranty, and prints it", async ({ page }) => {
  105. includedQuoteUrl = await newQuote(page, `E2E covered quote ${stamp}`)
  106. await addQuotePart(page, { name: PART, quantity: 1, unitPrice: 500 })
  107. await saveQuote(page)
  108. const panel = await expectWarrantyStatement(page, 'Included')
  109. await expect(panel.locator('#warrantyMonths')).toHaveValue('12')
  110. await expect(panel.locator('#warrantyMileage')).toHaveValue('20000')
  111. await expect(panel.locator('#warrantyNotes')).toHaveValue(TERMS)
  112. // No expiry on a quote: the period starts with the work, which has no date yet.
  113. await expect(panel.getByText('Expires', { exact: true })).toHaveCount(0)
  114. // The titles in this file avoid the word, so this is the panel's heading.
  115. const pdf = await quotePdf(page, includedQuoteUrl)
  116. expect(pdf.flat).toMatch(/warranty/i)
  117. expect(pdf.flat).toMatch(period(12, 20))
  118. expect(pdf.flat).toContain(TERMS)
  119. expect(pdf.flat).not.toContain('Expires:')
  120. })
  121. test('the customer reads the same warranty on the link and in its PDF', async ({ page }) => {
  122. await page.goto(includedQuoteUrl)
  123. const shareUrl = await quoteShareLink(page)
  124. // Signed out, the way a customer opens a link.
  125. const customer = await page.context().browser()?.newContext()
  126. const customerPage = await (customer as NonNullable<typeof customer>).newPage()
  127. await customerPage.goto(shareUrl)
  128. await expect(
  129. customerPage.getByText(period(12, 20)).filter({ visible: true }).first()
  130. ).toBeVisible()
  131. await expect(customerPage.getByText(TERMS).filter({ visible: true }).first()).toBeVisible()
  132. // Absolute: a context made by hand has no base URL to resolve against.
  133. const response = await customerPage.request.get(
  134. new URL(quotePdfUrl(shareUrl), shareUrl).toString()
  135. )
  136. expect(response.status()).toBe(200)
  137. const shared = await pdfContent(await response.body())
  138. await customer?.close()
  139. const downloaded = await quotePdf(page, includedQuoteUrl)
  140. expect(shared.flat).toBe(downloaded.flat)
  141. })
  142. test('a period changed on the quote is the period on the work order and the invoice', async ({
  143. page,
  144. }) => {
  145. await page.goto(includedQuoteUrl)
  146. await settle(page)
  147. const panel = await expectWarrantyStatement(page, 'Included')
  148. await panel.getByRole('button', { name: '6 mo', exact: true }).click()
  149. await expect(panel.locator('#warrantyMonths')).toHaveValue('6')
  150. await saveQuote(page)
  151. const workOrderUrl = await convertToWorkOrder(page)
  152. const onJob = await expectWarrantyStatement(page, 'Included')
  153. await expect(onJob.locator('#warrantyMonths')).toHaveValue('6')
  154. await expect(onJob.locator('#warrantyMileage')).toHaveValue('20000')
  155. await expect(onJob.locator('#warrantyNotes')).toHaveValue(TERMS)
  156. // The job has a date, so the period now has an end.
  157. await expect(onJob.getByText('Expires', { exact: true })).toBeVisible()
  158. const invoice = await invoicePdf(page, workOrderUrl)
  159. expect(invoice.flat).toMatch(period(6, 20))
  160. expect(invoice.flat).toContain(TERMS)
  161. expect(invoice.flat).toContain('Expires:')
  162. })
  163. test('"not included" swaps in the workshop\'s statement and drops the period', async ({
  164. page,
  165. }) => {
  166. notIncludedQuoteUrl = await newQuote(page, `E2E uncovered quote ${stamp}`)
  167. await addQuotePart(page, { name: PART, quantity: 1, unitPrice: 500 })
  168. const panel = await chooseWarrantyStatement(page, 'Not included')
  169. await expect(panel.locator('#warrantyMonths')).toHaveCount(0)
  170. await expect(panel.locator('#warrantyMileage')).toHaveCount(0)
  171. // The stock terms gave way to the stock statement; nobody had typed over them.
  172. await expect(panel.locator('#warrantyNotes')).toHaveValue(NO_WARRANTY)
  173. await saveQuote(page)
  174. // Saved, not just on screen.
  175. await page.reload()
  176. await settle(page)
  177. const reloaded = await expectWarrantyStatement(page, 'Not included')
  178. await expect(reloaded.locator('#warrantyNotes')).toHaveValue(NO_WARRANTY)
  179. const pdf = await quotePdf(page, notIncludedQuoteUrl)
  180. expect(pdf.flat).toContain('No workshop warranty is included')
  181. expect(pdf.flat).toContain(NO_WARRANTY)
  182. expect(pdf.flat).not.toContain('Duration:')
  183. expect(pdf.flat).not.toContain(TERMS)
  184. })
  185. test('with no sentence of its own, the document says statutory rights stand', async ({
  186. page,
  187. }) => {
  188. await page.goto(notIncludedQuoteUrl)
  189. await settle(page)
  190. const panel = await expectWarrantyStatement(page, 'Not included')
  191. await panel.locator('#warrantyNotes').fill('')
  192. await saveQuote(page)
  193. const pdf = await quotePdf(page, notIncludedQuoteUrl)
  194. expect(pdf.flat).toContain('No workshop warranty is included')
  195. expect(pdf.flat).toContain('Your statutory rights are not affected.')
  196. expect(pdf.flat).not.toContain(NO_WARRANTY)
  197. })
  198. test('a quote that said "not included" makes a work order that says it too', async ({ page }) => {
  199. await page.goto(notIncludedQuoteUrl)
  200. await settle(page)
  201. const workOrderUrl = await convertToWorkOrder(page)
  202. // Not the workshop's default of twelve months: the customer accepted "none".
  203. const onJob = await expectWarrantyStatement(page, 'Not included')
  204. await expect(onJob.locator('#warrantyMonths')).toHaveCount(0)
  205. const invoice = await invoicePdf(page, workOrderUrl)
  206. expect(invoice.flat).toContain('No workshop warranty is included')
  207. expect(invoice.flat).toContain('Your statutory rights are not affected.')
  208. expect(invoice.flat).not.toContain('Duration:')
  209. expect(invoice.flat).not.toContain('Expires:')
  210. })
  211. test('a new work order starts from the warranty too, and "not stated" prints nothing', async ({
  212. page,
  213. }) => {
  214. const vehicleUrl = await seededVehicleUrl(page)
  215. const workOrderUrl = await newWorkOrder(page, vehicleUrl, `E2E covered job ${stamp}`)
  216. const panel = await expectWarrantyStatement(page, 'Included')
  217. await expect(panel.locator('#warrantyMonths')).toHaveValue('12')
  218. await expect(panel.locator('#warrantyNotes')).toHaveValue(TERMS)
  219. await saveWorkOrder(page)
  220. expect((await invoicePdf(page, workOrderUrl)).flat).toMatch(period(12, 20))
  221. const cleared = await chooseWarrantyStatement(page, 'Not stated')
  222. await expect(cleared.locator('#warrantyNotes')).toHaveCount(0)
  223. await saveWorkOrder(page)
  224. await page.reload()
  225. await settle(page)
  226. // Folded shut again: a document that says nothing has nothing to show.
  227. await expectWarrantyStatement(page, 'Not stated')
  228. const silent = await invoicePdf(page, workOrderUrl)
  229. expect(silent.flat).not.toMatch(/warranty/i)
  230. expect(silent.flat).not.toContain(TERMS)
  231. })
  232. test('the defaults can be kept off new work orders and still reach new quotes', async ({
  233. page,
  234. }) => {
  235. await setWarrantyDefaults(page, {
  236. newDocumentsSay: 'Warranty included',
  237. months: 12,
  238. distance: 20000,
  239. terms: TERMS,
  240. notIncludedText: NO_WARRANTY,
  241. onWorkOrders: false,
  242. })
  243. // `/service/new` hands back an untouched draft younger than five seconds.
  244. await page.waitForTimeout(5_500)
  245. const vehicleUrl = await seededVehicleUrl(page)
  246. await newWorkOrder(page, vehicleUrl, `E2E bare job ${stamp}`)
  247. const onJob = await expectWarrantyStatement(page, 'Not stated')
  248. // The texts are still one click away.
  249. const filled = await chooseWarrantyStatement(page, 'Included')
  250. await expect(filled.locator('#warrantyMonths')).toHaveValue('12')
  251. await expect(filled.locator('#warrantyNotes')).toHaveValue(TERMS)
  252. await expect(onJob).toBeVisible()
  253. // Saved, so the page is left clean rather than with an autosave pending.
  254. await saveWorkOrder(page)
  255. await newQuote(page, `E2E still-covered quote ${stamp}`)
  256. await expectWarrantyStatement(page, 'Included')
  257. })
  258. })