/** * 首页轮播配图(Banner v2 · 绿底 + 白色插画)测试 * 覆盖: * 1) static/banners 白色插画资源完整性(PNG 魔数 / 192×192 / 真实透明 / 体积) * 2) common/assets/banners.js 的取图、金色点缀与底色回落规则 * 3) common/mock/data.js 轮播数据与「绿=平台、金=价值」主题规则一致 */ import fs from 'node:fs' import path from 'node:path' import url from 'node:url' import { describe, it, expect } from './harness.mjs' import { BANNER_BASE, BANNER_ART, BANNER_BG, bannerArtUrl, bannerHasValue, bannerBg, } from '../common/assets/banners.js' import { datasets } from '../common/mock/data.js' import { decodePng, transparentRatio } from '../tools/png-util.mjs' const ROOT = path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), '..') const ART_DIR = path.join(ROOT, 'static', 'banners') describe('轮播配图 · 资源完整性', () => { it('内置插画共 3 套,注册表与文件一一对应', () => { expect(BANNER_ART.length).toBe(3) const missing = BANNER_ART.filter((f) => !fs.existsSync(path.join(ART_DIR, f))) expect(missing).toEqual([]) expect(BANNER_BASE).toBe('/static/banners/') }) it('每张插画都是有效 PNG 且体积可控(单张 < 8KB)', () => { for (const f of BANNER_ART) { const buf = fs.readFileSync(path.join(ART_DIR, f)) expect(buf.slice(0, 8).toString('hex')).toBe('89504e470d0a1a0a') expect(buf.length).toBeLessThanOrEqual(8 * 1024) } const total = BANNER_ART.reduce((s, f) => s + fs.statSync(path.join(ART_DIR, f)).size, 0) expect(total).toBeLessThanOrEqual(24 * 1024) // 3 张总计不超过 24KB,不给主包添负担 }) it('每张插画都是 192×192 且具备真实透明通道', () => { for (const f of BANNER_ART) { const img = decodePng(fs.readFileSync(path.join(ART_DIR, f))) expect(img.width).toBe(192) expect(img.height).toBe(192) expect(img.rgba[3]).toBe(0) // 左上角必须完全透明 expect(transparentRatio(img) >= 0.1).toBeTruthy() } }) it('插画图形为白色/薄荷系(暗像素占比极低,说明压在绿底上够亮)', () => { for (const f of BANNER_ART) { const img = decodePng(fs.readFileSync(path.join(ART_DIR, f))) let dark = 0 let visible = 0 for (let i = 0; i < img.rgba.length; i += 4) { if (img.rgba[i + 3] < 32) continue visible++ const lum = (img.rgba[i] * 299 + img.rgba[i + 1] * 587 + img.rgba[i + 2] * 114) / 1000 if (lum < 150) dark++ } expect(visible).toBeGreaterThan(0) expect(dark / visible < 0.02).toBeTruthy() } }) }) describe('轮播配图 · 取图与主题规则', () => { it('按位置循环取插画(第 4 条回到第 1 套)', () => { expect(bannerArtUrl(0)).toBe('/static/banners/banner-art-1.png') expect(bannerArtUrl(1)).toBe('/static/banners/banner-art-2.png') expect(bannerArtUrl(2)).toBe('/static/banners/banner-art-3.png') expect(bannerArtUrl(3)).toBe('/static/banners/banner-art-1.png') }) it('金色价值点缀只给「直降 / 免费」两条(第 3 条为平台服务,不带金色)', () => { expect(bannerHasValue(0)).toBe(true) expect(bannerHasValue(1)).toBe(true) expect(bannerHasValue(2)).toBe(false) expect(bannerHasValue(3)).toBe(true) }) it('后台下发渐变时原样生效', () => { const custom = 'linear-gradient(90deg, #04302a 0%, #0e7c66 100%)' expect(bannerBg({ color: custom }, 0)).toBe(custom) }) it('后台想「纯色」时用同色双色标渐变表达,同样原样生效', () => { const solid = 'linear-gradient(135deg, #0E7C66 0%, #0E7C66 100%)' expect(bannerBg({ color: solid }, 0)).toBe(solid) }) it('rgba 色标的渐变也被接受(不局限于 hex)', () => { const rgba = 'linear-gradient(135deg, rgba(24,165,135,.9) 0%, #0a5d4d 100%)' expect(bannerBg({ color: rgba }, 0)).toBe(rgba) }) it('旧暖色纯色 / 空底色 / 被截断的半截值一律回落到内置生态绿渐变(主题护栏)', () => { expect(bannerBg({ color: '#FF8A5B' }, 0)).toBe(BANNER_BG[0]) expect(bannerBg({ color: '#FFB86B' }, 1)).toBe(BANNER_BG[1]) expect(bannerBg({}, 2)).toBe(BANNER_BG[2]) expect(bannerBg(null, 3)).toBe(BANNER_BG[0]) expect(bannerBg({ color: '' }, 4)).toBe(BANNER_BG[1]) // 旧库 color 列只有 varchar(16),渐变会被截断成半截值(渲染成非法样式 → 卡片空白),必须拦下 expect(bannerBg({ color: 'linear-gradient(' }, 5)).toBe(BANNER_BG[2]) expect(bannerBg({ color: 'linear-gradient(135deg' }, 6)).toBe(BANNER_BG[0]) expect(bannerBg({ color: 'linear-gradient(135deg)' }, 7)).toBe(BANNER_BG[1]) }) it('内置底色的 3 条渐变必须是春绿系(105deg 亮绿起点 → 深绿收尾,不含暖色)', () => { expect(BANNER_BG.length).toBe(3) // 起点落在品牌亮绿族(#00e070 / #00c860 / #049e52 / #04874a),收尾必须落到深绿(白字区) const START_OK = /linear-gradient\(105deg, #(00e070|00d96c|00e878|00c860|049e52|04874a|00d26a|10b981|059669) / const END_OK = /#(046b3b|045c34|023a21|03512e|34d399|6ee7b7|10b981) 100%\)$/ for (const bg of BANNER_BG) { expect(START_OK.test(bg)).toBeTruthy() expect(END_OK.test(bg)).toBeTruthy() } }) }) describe('轮播配图 · Mock 数据契约', () => { it('轮播 3 条,字段与后端 /home/banners 一致(id/title/image/link/color)', () => { expect(datasets.banners.length).toBe(3) for (const b of datasets.banners) { expect(typeof b.id === 'number').toBeTruthy() expect(b.title.length > 0).toBeTruthy() expect(typeof b.image).toBe('string') expect(b.link.indexOf('/pages/')).toBe(0) expect(b.color.indexOf('gradient(') !== -1).toBeTruthy() } }) it('每条轮播的底色都能直接生效(不再走回落分支)', () => { datasets.banners.forEach((b, i) => { expect(bannerBg(b, i)).toBe(b.color) expect(b.color.indexOf('linear-gradient(')).toBe(0) }) }) it('轮播标题与后台种子一致(前端展示与安装脚本同一份文案)', () => { expect(datasets.banners.map((b) => b.title)).toEqual([ '同城好物节 · 二手直降', '招聘旺季 · 免费发布', '本地服务 · 上门更省心', ]) }) describe('春绿主题规则 · 亮绿面与白字/墨绿字', () => { const read = (p) => fs.readFileSync(path.join(ROOT, p), 'utf8') const vueFiles = () => { const out = [] const walk = (dir) => { for (const name of fs.readdirSync(dir)) { const full = path.join(dir, name) if (fs.statSync(full).isDirectory()) walk(full) else if (name.endsWith('.vue')) out.push(full) } } walk(path.join(ROOT, 'pages')) walk(path.join(ROOT, 'components')) return out } it('亮绿面($fc-gradient-primary / $fc-primary)上不得直接配白字(白字压亮绿仅 1.8:1)', () => { const bad = [] for (const f of vueFiles()) { const lines = read(path.relative(ROOT, f)).split(/\r?\n/) lines.forEach((line, i) => { if (!/background(-color)?: \$fc-(gradient-primary|primary);/.test(line)) return const next = lines.slice(i + 1, i + 4).join('\n') if (/color: #ffffff;/.test(next)) bad.push(`${path.relative(ROOT, f)}:${i + 1}`) }) } expect(bad).toEqual([]) }) it('白字只落在深绿面($fc-gradient-hero)上:hero 仍保留白字', () => { const heroFiles = [ ['pages/index/index.vue', '.home__header'], ['pages/mine/index.vue', '.mine__header'], ['pages/member/index.vue', '.vip__backdrop'], ['pages/distribution/index.vue', '.dist__hero'], ] for (const [file, sel] of heroFiles) { const src = read(file) const i = src.indexOf(sel) expect(i > -1).toBeTruthy() expect(src.slice(i, i + 120).indexOf('$fc-gradient-hero') > -1).toBeTruthy() } }) it('轮播卡为规范化纯图片卡片容器(702×280rpx、aspectFill)', () => { const src = read('pages/index/index.vue') expect(src.indexOf('.home__banner-img') > -1).toBeTruthy() expect(src.indexOf('height: 280rpx;') > -1).toBeTruthy() for (const bg of BANNER_BG) expect(bg.indexOf('105deg') > -1).toBeTruthy() }) }) })