import { expect, test } from './fixtures' test('creates an invoice through the wizard and downloads an editable Word file', async ({ page }) => { let savedPayload: Record | null = null await page.route('**/api/**', async (route) => { const request = route.request() const url = new URL(request.url()) if (!url.pathname.startsWith('/api/')) return route.continue() if (url.pathname === '/api/auth/me') return route.fulfill({ json: { data: { id: 1, name: 'فروشنده تست', email: 'seller@example.test', roles: ['admin'], permissions: ['view_admin_dashboard', 'view_invoices', 'create_invoices'], is_active: true, } } }) if (url.pathname === '/api/settings/public') return route.fulfill({ json: { data: {} } }) if (url.pathname === '/api/notifications/unread-count') return route.fulfill({ json: { data: 0 } }) if (url.pathname === '/api/leads/7') return route.fulfill({ json: { id: 7, first_name: 'علی', last_name: 'خریدار', company: 'شرکت نمونه', phone: '09120000000', email: 'buyer@example.test', national_code: '0012345678', province: 'تهران', city: 'تهران', address: 'خیابان نمونه', final_result: 'موفق', sold_product: 'خدمت مشاوره', deal_value: 500000, } }) if (url.pathname === '/api/leads/7/invoice' && request.method() === 'POST') { savedPayload = request.postDataJSON() return route.fulfill({ status: 201, json: { id: 51, number: 'INV-2026-000051', lead_id: 7, status: 'pending_approval', currency: 'IRR', customer_snapshot: (savedPayload as any).customer_snapshot, seller_snapshot: (savedPayload as any).seller_snapshot, items: (savedPayload as any).items, lead_snapshot: {}, resolved_fields: {}, subtotal: 500000, discount: 0, tax: 0, total: 500000, paid_amount: 0, payment_status: 'unpaid', balance_due: 500000, notes: null, version: 1, created_at: '', updated_at: '', capabilities: { update: true, approve: true, issue: true, void: false }, } }) } if (url.pathname === '/api/invoices/51/word') return route.fulfill({ status: 200, contentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', headers: { 'Content-Disposition': 'attachment; filename="invoice-INV-2026-000051.docx"' }, body: Buffer.from('PK-test-docx'), }) if (url.pathname === '/api/invoices-summary') return route.fulfill({ json: { total: 0, counts: {}, issued_total: 0, paid_total: 0, outstanding_total: 0, currency: 'IRR' } }) if (url.pathname === '/api/invoices') return route.fulfill({ json: paginated([]) }) return route.fulfill({ json: { data: [] } }) }) await page.goto('/invoices?create_from=7') await expect(page.getByRole('heading', { name: 'ایجاد فاکتور' })).toBeVisible() await expect(page.getByText('فروشنده تست')).toBeVisible() await page.getByRole('button', { name: 'ادامه' }).click() await expect(page.getByText('مشخصات خریدار')).toBeVisible() await expect(page.getByLabel('نام خریدار')).toHaveValue('علی خریدار') await page.getByRole('button', { name: 'ادامه' }).click() await expect(page.getByLabel('شرح کالا یا خدمت')).toHaveValue('خدمت مشاوره') await page.getByRole('button', { name: 'ادامه' }).click() await page.getByLabel('شرایط پرداخت').fill('پرداخت طی هفت روز') await page.getByRole('button', { name: 'ادامه' }).click() const download = page.waitForEvent('download') await page.getByRole('button', { name: 'ثبت و دریافت Word' }).click() const file = await download expect(file.suggestedFilename()).toContain('.docx') await expect.poll(() => savedPayload).not.toBeNull() expect((savedPayload as any).page_width_mm).toBe(210) expect((savedPayload as any).page_height_mm).toBe(297) expect((savedPayload as any).payment_terms).toBe('پرداخت طی هفت روز') expect((savedPayload as any).items[0].unit).toBe('عدد') }) function paginated(items: Record[]) { return { data: items, current_page: 1, last_page: 1, per_page: 15, total: items.length, from: items.length ? 1 : 0, to: items.length } }