/** * v1.0.7 新增能力用例:我的发布 / 后台手动置顶 / 免费-付费发布开关与套餐额度 * * 覆盖需求: * ① 发布后能看到发布记录(/post/mine + 状态计数 + 驳回原因) * ② 后台手动置顶 → 用户端主页置顶优先展示(/home/feed) * ③ 发布模式开关(免费/付费)+ 单条价格 + 月卡/年卡价格与含发布条数 * ④ 计费口径:金额只由后台配置决定,客户端传入的 amount 一律被忽略 */ import { describe, it, expect, expectReject } from './harness.mjs' import { homeApi, postApi, memberApi } from '../common/api/index.js' import { datasets } from '../common/mock/data.js' /** 每个用例前把 mock 状态复位,保证用例之间互不影响 */ function resetPublish() { datasets.publishRule.mode = 'paid' datasets.publishRule.singlePrice = 5 datasets.publishRule.monthPrice = 19.9 datasets.publishRule.monthQuota = 30 datasets.publishRule.yearPrice = 168 datasets.publishRule.yearQuota = 500 datasets.publishRule.quotaLeft = 30 setFreeQuota(2) datasets.publishRule.isMember = true } /** 设置免费发布条数(字段名与后端 Post::publishConfig 对齐:freeQuota/freeQuotaUsed/freeQuotaLeft) */ function setFreeQuota(total, used = 0) { datasets.publishRule.freeQuota = total datasets.publishRule.freeQuotaUsed = used datasets.publishRule.freeQuotaLeft = Math.max(0, total - used) datasets.publishRule.freeUnlimited = false } describe('发布配置 · 后台开关与价格下发', () => { it('返回发布模式、三档价格与剩余额度', async () => { resetPublish() const cfg = await postApi.publishConfig() expect(cfg.mode).toBe('paid') expect(cfg.singlePrice).toBe(5) expect(cfg.monthPrice).toBe(19.9) expect(cfg.monthQuota).toBe(30) expect(cfg.yearPrice).toBe(168) expect(cfg.yearQuota).toBe(500) expect(cfg.quotaLeft).toBe(30) expect(cfg.isMember).toBe(true) }) it('免费模式:所有人免费发布且不消耗额度', async () => { resetPublish() datasets.publishRule.mode = 'free' datasets.publishRule.quotaLeft = 0 const res = await postApi.create({ title: '免费模式发布', content: '内容', plan: 'free' }) expect(res.order).toBeFalsy() expect(res.plan).toBe('free') expect(res.amount).toBe(0) }) }) describe('发布计费 · 服务端定价与套餐额度', () => { it('套餐额度内免费发布:无订单且额度 -1', async () => { resetPublish() const res = await postApi.create({ title: '额度内发布', content: '内容', plan: 'free' }) expect(res.order).toBeFalsy() expect(res.plan).toBe('quota') expect(res.quotaLeft).toBe(29) const mine = await postApi.mine({ page: 1, size: 20 }) const created = mine.list.find((p) => p.title === '额度内发布') expect(created).toBeDefined() expect(created.auditStatus).toBe(0) }) it('额度与每日免费次数都用尽时,免费发布被拦截', async () => { resetPublish() datasets.publishRule.quotaLeft = 0 setFreeQuota(0) // 关闭免费额度,隔离出「必须付费」的场景 await expectReject(() => postApi.create({ title: '无额度', content: '内容', plan: 'free' }), '请选择发布方式') }) it('付费模式下仍保留每日免费额度(避免硬付费墙)', async () => { resetPublish() datasets.publishRule.quotaLeft = 0 setFreeQuota(2) const first = await postApi.create({ title: '今日免费第 1 条', content: '内容', plan: 'free' }) expect(first.plan).toBe('free') expect(first.order).toBeFalsy() const cfg = await postApi.publishConfig() expect(cfg.freeQuotaLeft).toBe(1) const second = await postApi.create({ title: '今日免费第 2 条', content: '内容', plan: 'free' }) expect(second.plan).toBe('free') // 第 3 条超出每日额度 → 必须付费 await expectReject(() => postApi.create({ title: '超出每日额度', content: '内容', plan: 'free' }), '请选择发布方式') }) it('单条付费:金额取后台配置,忽略客户端传入的 amount', async () => { resetPublish() datasets.publishRule.quotaLeft = 0 setFreeQuota(0) const res = await postApi.create({ title: '单条付费发布', content: '内容', plan: 'single', payType: 'wxpay', amount: 0.01, // 客户端篡改金额:应被无条件忽略 }) expect(res.order.amount).toBe(5) expect(res.order.orderNo).toContain('PO') }) it('单条付费缺少支付方式时报错', async () => { resetPublish() datasets.publishRule.quotaLeft = 0 setFreeQuota(0) await expectReject(() => postApi.create({ title: 'x', content: 'y', plan: 'single' }), '支付方式不支持') }) it('月卡/年卡走会员开通接口,开通后获得对应发布条数', async () => { resetPublish() datasets.publishRule.quotaLeft = 0 setFreeQuota(0) const opened = await memberApi.open({ planId: 'M1', payType: 'balance' }) expect(opened.postQuota).toBe(30) expect(opened.quotaLeft).toBe(30) const res = await postApi.create({ title: '月卡额度发布', content: '内容', plan: 'free' }) expect(res.plan).toBe('quota') expect(res.quotaLeft).toBe(29) // 年卡:含 500 条 const year = await memberApi.open({ planId: 'M12', payType: 'balance' }) expect(year.postQuota).toBe(500) expect(year.quotaLeft).toBe(529) }) it('会员套餐列表带出「含发布条数」字段', async () => { const plans = await memberApi.plans() expect(plans.length).toBe(3) plans.forEach((p) => expect(p.postQuota).toBeGreaterThan(0)) }) }) describe('我的发布 · 审核状态与计数', () => { it('列表返回本人信息与各状态计数', async () => { const res = await postApi.mine({ page: 1, size: 20 }) expect(res.list.length).toBeGreaterThan(0) expect(res.counts.all).toBeGreaterThan(0) expect(res.counts.pending + res.counts.passed + res.counts.rejected).toBeGreaterThan(0) res.list.forEach((p) => { expect(p.auditStatus).toBeDefined() expect(p.status).toBeDefined() expect(p.isTop).toBeDefined() }) }) it('按状态筛选:待审核 / 已驳回(含驳回原因)/ 已下架', async () => { const pending = await postApi.mine({ status: 'pending' }) expect(pending.list.every((p) => p.auditStatus === 0 && p.status === 1)).toBe(true) const rejected = await postApi.mine({ status: 'rejected' }) expect(rejected.list.every((p) => p.auditStatus === 2)).toBe(true) expect(rejected.list[0].auditReason).toBeTruthy() const offline = await postApi.mine({ status: 'offline' }) expect(offline.list.every((p) => p.status === 0)).toBe(true) }) it('置顶的发布记录排在最前', async () => { const res = await postApi.mine({ page: 1, size: 20 }) expect(res.list[0].isTop).toBe(true) }) it('可删除本人发布(删除后列表不再包含)', async () => { const before = await postApi.mine({ page: 1, size: 20 }) const target = before.list[0] await postApi.remove(target.id) const after = await postApi.mine({ page: 1, size: 20 }) expect(after.counts.all).toBe(before.counts.all - 1) expect(after.list.some((p) => p.id === target.id)).toBe(false) }) }) describe('后台置顶 → 用户端展示闭环', () => { it('置顶信息在首页信息流优先展示', async () => { const feed = await homeApi.feed({ page: 1, size: 10 }) expect(feed.list[0].isTop).toBe(true) // 置顶项之后才是非置顶项 const firstUnpinned = feed.list.findIndex((p) => !p.isTop) if (firstUnpinned > -1) { expect(feed.list.slice(firstUnpinned).every((p) => !p.isTop)).toBe(true) } }) })