// profit-view.spec.ts S3·FE-D34:利润分析页(msw 局部 handler,不动全局种子—— // 与 settle 页同策略:结算域端点走真库联调,组件测试用 server.use 注入契约)。 // 锁定:双端点参数契约(date_from/date_to/parent_id/channel_id)、汇总条渲染、 // 利润正负着色、口径说明常驻(诚实标注)。 import { describe, expect, it, afterEach } from 'vitest' import { http, HttpResponse } from 'msw' import { mount, flushPromises } from '@vue/test-utils' import ElementPlus from 'element-plus' import { server } from '../mocks/node' import StatsProfitView from '../src/views/stats/profit/index.vue' import { settleApi } from '../src/api/settle' const usersData = { list: [ { account_id: 1, username: 'root', role: 'group', success_count: 100, revenue: 10, cost: 4, rebate: 1.0, profit: 7 }, { account_id: 12, username: 'a12', role: 'customer', success_count: 10, revenue: 0, cost: 0.4, rebate: 0, profit: -0.4 }, ], total: 2, summary: { count: 2, total_revenue: 10, total_cost: 4.4, total_rebate: 1.0, platform_profit: 4.6 }, } const channelsData = { list: [{ channel_id: 9101, name: 'c1', upstream_id: 7, success_count: 150, revenue: 24, cost: 6, profit: 18 }], total: 1, } let lastUsersQuery = '' let lastChannelsQuery = '' function stubProfit() { server.use( http.get('*/api/v1/stats/profit/users', ({ request }) => { lastUsersQuery = request.url return HttpResponse.json({ code: 'ISC:000', msg: '成功', data: usersData }) }), http.get('*/api/v1/stats/profit/channels', ({ request }) => { lastChannelsQuery = request.url return HttpResponse.json({ code: 'ISC:000', msg: '成功', data: channelsData }) }), ) } afterEach(() => server.resetHandlers()) describe('FE-D34 利润分析', () => { it('api 契约:用户/通道端点参数与响应结构', async () => { stubProfit() const users = await settleApi.profitUsers({ date_from: '2026-11-01', date_to: '2026-11-30', parent_id: 10 }) expect(users.summary.platform_profit).toBe(4.6) expect(users.list[0].rebate).toBe(1.0) expect(lastUsersQuery).toContain('date_from=2026-11-01') expect(lastUsersQuery).toContain('parent_id=10') const channels = await settleApi.profitChannels({ date_from: '2026-11-01', date_to: '2026-11-30', channel_id: 2 }) expect(channels.list[0].profit).toBe(18) expect(lastChannelsQuery).toContain('channel_id=2') }) it('组件:双 Tab 数据渲染+汇总条+利润正负着色+口径说明', async () => { stubProfit() const w = mount(StatsProfitView, { global: { plugins: [ElementPlus] } }) await flushPromises() const text = w.text() // 汇总条:平台净利 4.60 与账号数。 expect(text).toContain('平台净利') expect(text).toContain('4.60') // 用户行:正利润 7.00 / 负利润 -0.40(无价账号)。 expect(text).toContain('7.00') expect(text).toContain('-0.40') // 口径说明常驻(分摊/阶梯不进分析/返点 R4)。 expect(text).toContain('占比分摊') expect(text).toContain('不进分析') expect(w.find('.pf-neg').exists()).toBe(true) // 通道 Tab 数据在挂载时一并加载。 expect(w.html()).toContain('18.00') }) })