/** * 全链路(端到端)业务流测试 * 模拟真实用户路径:「浏览 → 登录 → 发布 → 支付 → 审核 → 查看 → 评价 → 会员 → 分销 → 提现 → 消息」 * 并联动后台管理端,验证前后端模块串通一致。 */ import { describe, it, expect, expectReject } from './harness.mjs' import { authApi, homeApi, postApi, memberApi, distributionApi, messageApi, adminApi } from '../common/api/index.js' describe('全链路 E2E · 用户端主路径', () => { it('STEP1 未登录可浏览:首页 → 分类 → 搜索', async () => { const [banners, categories] = await Promise.all([homeApi.banners(), homeApi.categories()]) expect(banners.length).toBeGreaterThan(0) expect(categories.length).toBe(8) const category = categories[0] const feed = await homeApi.feed({ categoryId: category.id, sort: 'new', page: 1, size: 10 }) expect(feed.list.every((p) => p.categoryId === category.id)).toBe(true) const search = await homeApi.feed({ keyword: '快递' }) expect(search.total).toBeGreaterThan(0) }) it('STEP2 登录与身份切换(个人 → 商家需资质)', async () => { const login = await authApi.wxLogin({ code: 'wx_code_e2e' }) expect(login.token).toBeTruthy() const bind = await authApi.bindPhone({ phone: '13800001111' }) expect(bind.phone).toBe('13800001111') await expectReject(() => authApi.switchType({ userType: 2 }), '需上传营业执照') const biz = await authApi.switchType({ userType: 2, license: 'license.jpg' }) expect(biz.needAudit).toBe(true) }) it('STEP3 发布付费信息并生成支付订单', async () => { const created = await postApi.create({ categoryId: 3, title: 'E2E 二手笔记本转让', content: '九成新,支持当面验机。', paid: true, amount: 5, payType: 'wxpay', }) expect(created.auditStatus).toBe(0) // 待审核 expect(created.order.orderNo).toContain('PO') expect(created.order.amount).toBe(5) }) it('STEP4 草稿箱:自动保存 → 恢复编辑', async () => { const saved = await postApi.saveDraft({ title: '未完成的草稿', content: '???', categoryId: 2 }) expect(saved.draftId).toBeDefined() const drafts = await postApi.drafts() expect(drafts.length).toBeGreaterThan(0) expect(drafts[0].savedAt).toBeDefined() }) it('STEP5 详情 → 付费解锁联系方式 → 评价互动', async () => { const detail = await postApi.detail(102) expect(detail.publisher.name).toBeTruthy() await expectReject(() => postApi.payView(102, { payType: 'alipay' }), '支付方式不支持') const unlocked = await postApi.payView(102, { payType: 'balance' }) expect(unlocked.unlocked).toBe(true) expect(unlocked.contact.phone).toMatch(/^1\d{10}$/) const cmt = await postApi.comment(102, { score: 5, content: '信息真实,联系很顺畅' }) expect(cmt.commentId).toBeDefined() await expectReject(() => postApi.comment(102, { score: 6, content: 'x' }), '评分需在 1-5 之间') }) it('STEP6 开通会员 → 订单与资产联动', async () => { const plans = await memberApi.plans() const year = plans.find((p) => p.name === '年卡') const order = await memberApi.open({ planId: year.planId, payType: 'wxpay' }) expect(order.status).toBe('paid') const orders = await memberApi.orders({ page: 1, size: 50 }) expect(orders.total).toBeGreaterThan(0) expect((await memberApi.assets()).balance).toBeDefined() }) it('STEP7 分销推广 → 团队业绩 → 申请提现', async () => { const overview = await distributionApi.overview() expect(overview.inviteCode).toBeTruthy() const team = await distributionApi.team({ page: 1, size: 50 }) expect(team.list.filter((t) => t.level === 1).length).toBeGreaterThan(0) const wd = await distributionApi.withdraw({ amount: overview.withdrawable, channel: 'wxpay' }) expect(wd.status).toBe('auditing') await expectReject( () => distributionApi.withdraw({ amount: overview.withdrawable + 1 }), '超过可提现余额' ) }) it('STEP8 消息中心:接收审核/到期/互动通知', async () => { const sys = await messageApi.list({ type: 'system' }) expect(sys.list.length).toBeGreaterThan(0) expect((await messageApi.read(sys.list[0].id)).read).toBe(true) }) }) describe('全链路 E2E · 后台端管理闭环', () => { it('STEP9 数据总览 → 待审核数量驱动审核队列', async () => { const overview = await adminApi.dashboardOverview() const pending = await adminApi.pendingPosts({ page: 1, size: 50 }) expect(pending.total).toBeGreaterThan(0) expect(overview.pendingAudit).toBeGreaterThan(0) }) it('STEP10 审核用户发布的信息(快速通过)', async () => { const pending = await adminApi.pendingPosts({ page: 1, size: 1 }) const target = pending.list[0] expect((await adminApi.auditPost(target.id, 1)).auditStatus).toBe(1) }) it('STEP11 商家资质审核 + 违规信息强制下架', async () => { await expectReject(() => adminApi.auditMerchant(2005, 3), '审核状态仅支持') expect((await adminApi.auditMerchant(2005, 1)).auditStatus).toBe(1) expect((await adminApi.offlinePost(106)).offline).toBe(true) }) it('STEP12 评价回复 + 举报处理', async () => { expect((await adminApi.replyComment(5003, '已核实并处理,感谢反馈')).reply).toBeTruthy() const reports = await adminApi.reports() expect(reports.some((r) => r.status === 'pending')).toBe(true) }) it('STEP13 分销提现审核打款(与 STEP7 联动)', async () => { const list = await adminApi.withdraws() const auditing = list.filter((w) => w.status === 'auditing') expect(auditing.length).toBeGreaterThan(0) expect((await adminApi.handleWithdraw(auditing[0].id, 'pay')).status).toBe('paid') }) it('STEP14 订单退款 + 财务报表核对', async () => { expect((await adminApi.refund(3001, 5, '用户申请退款')).refunded).toBe(true) const report = await adminApi.financeReport('day') const netSum = report.rows.reduce((s, r) => s + r.net, 0) expect(netSum).toBeGreaterThan(0) expect((await adminApi.financeFlows({ page: 1, size: 50 })).total).toBeGreaterThan(0) }) it('STEP15 权限与系统设置收口', async () => { const superRole = (await adminApi.roles()).find((r) => r.name === '超级管理员') expect(superRole.permissions).toContain('*') await expectReject(() => adminApi.saveDistSetting({ level1Rate: 90, level2Rate: 30 }), '不能超过 100%') expect((await adminApi.saveFeeSetting({ singlePost: 5 })).singlePost).toBe(5) expect((await adminApi.savePaySetting({ mchId: '1900000001', apiKey: 'secret' })).configured).toBe(true) const areas = await adminApi.areas() expect(areas[0].province).toBeDefined() expect((await adminApi.saveArea({ province: '贵州省', city: '贵阳市', district: '开阳县' })).fullName).toContain('开阳县') expect((await adminApi.logs({ page: 1, size: 5 })).list.length).toBeGreaterThan(0) }) }) describe('边界与异常(软件测试视角)', () => { it('未匹配的接口返回 404 语义错误', async () => { const { matchMock } = await import('../common/mock/index.js') await expectReject(() => matchMock({ url: '/nope', method: 'GET' }), '未匹配到路由') }) it('分页边界:超出范围返回空列表而非报错', async () => { const res = await homeApi.feed({ page: 9999, size: 10 }) expect(res.list).toHaveLength(0) expect(res.hasMore).toBe(false) }) it('空结果可正常返回(便于空状态渲染)', async () => { const res = await homeApi.feed({ keyword: '不存在的关键词XYZ' }) expect(res.list).toHaveLength(0) expect(res.total).toBe(0) }) it('金额换算精度:分↔元无损', async () => { const { fen2yuan, yuan2fen } = await import('../common/utils/format.js') expect(fen2yuan(5)).toBe('0.05') expect(yuan2fen(19.9)).toBe(1990) expect(fen2yuan(yuan2fen(168))).toBe('168.00') }) it('工具函数:时间与手机号脱敏', async () => { const { maskPhone, fromNow, formatTime } = await import('../common/utils/format.js') expect(maskPhone('13988888888')).toBe('139****8888') expect(fromNow(new Date())).toBe('刚刚') expect(formatTime('2024-01-05T10:00:00')).toMatch(/^2024-01-05 10:00$/) }) })