import { expect } from '@playwright/test'; import { MockApi, MOCK, Persona } from '../../api'; import { DATA, PROCESS } from './labels'; export type CardTxn = { trxId: string; amount: number; date: string; merchant?: string }; /** EXP data recipes (tests/data-recipes/exp) through the MOCK EXP pack's internal API (/api/exp), /api/processes and the REST facade. */ export class EXPApi { constructor(public api: MockApi) {} refs(): Promise<{ otherEmployeeReport: string; cardProgram: string; payGroup: string }> { return this.api.ok('employee', 'GET', '/exp/refs'); } async stage(name: string, content: string) { return this.api.ok('card_admin', 'POST', '/exp/files', { name, content }); } /** Synthetic card file stand-in (VCF4-like CSV): synthetic test card number, Luhn not relied on. */ cardCsv(txns: CardTxn[], cardNumber: string = DATA.testCardNumber) { return ['CardNumber,TransactionId,Amount,Date,Merchant', ...txns.map((t) => `${cardNumber},${t.trxId},${t.amount.toFixed(2)},${t.date},${t.merchant ?? 'ZZ Card Merchant'}`)].join('\n'); } async process(as: Persona, name: string, params: Record) { const p = await this.api.ok(as, 'POST', '/processes', { name, params }); expect(p.status, `${name}: ${p.log}`).toBe('SUCCEEDED'); return p; } /** DR-EXP-CC-001 via services: stage + upload + validate. */ async loadCards(file: string, txns: CardTxn[]) { await this.stage(file, this.cardCsv(txns)); await this.process('card_admin', PROCESS.uploadCards, { cardProgram: DATA.cardProgram, dataFile: file }); return this.process('card_admin', PROCESS.validateCards, { cardProgram: DATA.cardProgram }); } async rest(as: Persona, method: string, path: string, data?: unknown) { return this.api.call(as, method, path, data, MOCK.restBase); } async restQ(as: Persona, resource: string, q: string) { const r = await this.rest(as, 'GET', `/${resource}?q=${encodeURIComponent(q)}`); expect(r.status, `GET ${resource}?q=${q}`).toBe(200); return r.body as { items: any[]; count: number }; } async report(as: Persona, number: string) { const b = await this.restQ(as, 'expenseReports', `ExpenseReportNumber='${number}'`); expect(b.count, `report ${number}`).toBe(1); return b.items[0]; } }