Files
kra-new/scripts/sw_activation_smoke.cjs
T

76 lines
5.5 KiB
JavaScript

// 核对渠道激活页面的真实渲染;OA 数据与流水查询使用受控响应。
const fs = require('node:fs/promises')
const path = require('node:path')
const assert = require('node:assert/strict')
const { chromium } = require(process.env.SW_PLAYWRIGHT_MODULE || 'playwright')
;(async () => {
const token = (await fs.readFile(path.join(process.env.TEMP, 'sw-kra-http-test-token'), 'utf8')).trim()
const browser = await chromium.launch({ channel: 'chrome', headless: true })
try {
const context = await browser.newContext({ viewport: { width: 1500, height: 1000 } })
await context.addInitScript(value => localStorage.setItem('token', value), token)
const page = await context.newPage()
const errors = []
const queries = []
let unavailable = false
const rows = ['pending', 'in_progress', 'reached', 'rejected', 'unbound'].map((status, i) => ({
id: String(i + 1), promoteAccount: `测试渠道-${i + 1}`, busierAccount: '测试商务', platformName: 'lx', platformTitle: '龙翔',
bindAt: ['pending', 'rejected'].includes(status) ? null : '2026-11-01 00:00:00',
unbindAt: status === 'unbound' ? '2026-11-03 12:00:00' : null,
endAt: ['reached', 'unbound'].includes(status) ? '2026-11-03 12:00:00' : null,
accumAmount: '30.30', status, statusText: { pending: '待审核', in_progress: '进行中', reached: '已达标', rejected: '已驳回', unbound: '已解绑' }[status]
}))
page.on('pageerror', error => errors.push(error.message))
await page.route('http://127.0.0.1:8080/api/**', async route => {
const request = route.request()
const url = new URL(request.url())
const api = url.pathname.slice(4)
if (api === '/sw/promoteActivePool/bindList') {
queries.push(url.search)
return route.fulfill({ json: { code: 0, data: { rows: unavailable ? [] : rows, total: unavailable ? 0 : rows.length, available: !unavailable } } })
}
if (api === '/sw/promoteActivePool/bindSpendDetail') {
if (url.searchParams.get('id') === '1') return route.fulfill({ json: { code: 7, msg: '审核通过后可查看', data: {} } })
const row = rows.find(item => item.id === url.searchParams.get('id'))
return route.fulfill({ json: { code: 0, data: { promoteAccount: row.promoteAccount, bindAt: row.bindAt, endAt: row.endAt, list: [{ platformTitle: '龙翔', gameName: '测试游戏', totalAmount: '9007199254740993.10' }], totalAmount: '9007199254740993.10' } } })
}
const response = await route.fetch({ url: 'http://127.0.0.1:18000' + api + url.search, headers: { ...request.headers(), 'x-token': token } })
return route.fulfill({ response })
})
await page.goto('http://127.0.0.1:8080/#/layout/sw-BusMgmt/ChannelActivationList')
await page.getByText('测试渠道-1', { exact: true }).waitFor()
const row = id => page.locator('.el-table__body-wrapper tr').filter({ hasText: `测试渠道-${id}` })
assert.equal(await row(4).getByRole('button', { name: '流水明细', exact: true }).count(), 0)
for (const id of [1, 2, 3, 5]) assert.equal(await row(id).getByRole('button', { name: '流水明细', exact: true }).count(), 1)
const dialog = page.getByRole('dialog', { name: '流水明细', exact: true })
await row(3).getByRole('button', { name: '流水明细', exact: true }).click()
await dialog.getByText('测试游戏', { exact: true }).waitFor()
await dialog.getByText('2026-11-01 至 2026-11-03', { exact: true }).waitFor()
assert.equal(await dialog.getByText('¥9,007,199,254,740,993.10', { exact: true }).count(), 2)
await dialog.locator('.el-loading-mask').waitFor({ state: 'hidden' })
await page.screenshot({ path: path.join(process.env.TEMP, 'sw-activation-flow.png'), animations: 'disabled' })
await dialog.getByRole('button', { name: '关闭', exact: true }).click()
await dialog.waitFor({ state: 'hidden' })
await row(1).getByRole('button', { name: '流水明细', exact: true }).click()
await dialog.getByText('审核通过后可查看', { exact: true }).waitFor()
assert.equal(await dialog.getByText('绑定期间累计流水:', { exact: false }).count(), 0)
await dialog.getByRole('button', { name: '关闭', exact: true }).click()
await dialog.waitFor({ state: 'hidden' })
const form = page.locator('.kra-search-box')
await form.getByPlaceholder('渠道账号', { exact: true }).fill(' 渠道 ')
await form.getByRole('button', { name: '查询', exact: true }).click()
await page.waitForResponse(response => response.url().includes('/sw/promoteActivePool/bindList') && response.url().includes('promoteAccount='))
assert.equal(new URLSearchParams(queries.at(-1)).get('promoteAccount'), '渠道')
unavailable = true
await form.getByRole('button', { name: '重置', exact: true }).click()
await page.getByText('OA 渠道激活数据源尚未接入或暂不可用。', { exact: true }).waitFor()
assert.equal(new URLSearchParams(queries.at(-1)).get('promoteAccount'), '')
assert.deepEqual(errors, [])
const result = { passed: true, rejectedHidesFlow: true, pendingShowsReasonWithoutFakeTotal: true, reachedCutoffDisplayed: true, exactLargeAmount: true, filtersAndReset: true, unavailableSourceNotice: true, pageErrors: errors }
await fs.writeFile('docs/sw6-to-kra/activation-page-2026-11-03.json', JSON.stringify({ method: '真实 Chrome,受控 OA 和流水响应,无写入', ...result }, null, 2) + '\n')
console.log(JSON.stringify(result))
await context.close()
} finally { await browser.close() }
})().catch(error => { console.error(error); process.exitCode = 1 })