Files
zyt/admin/tests/wecom-promotion-theme.test.cjs
T

133 lines
6.3 KiB
JavaScript

const assert = require('node:assert/strict')
const test = require('node:test')
const fs = require('node:fs')
const path = require('node:path')
const postcss = require('postcss')
const sass = require('sass')
const { parseDOM } = require('htmlparser2')
const { selectAll, selectOne, compile } = require('css-select')
const { parse, compileStyle } = require('@vue/compiler-sfc')
const src = path.join(__dirname, '../src')
const base = path.join(src, 'views/first_visit/wecom_promotion')
const scope = 'data-v-c02'
// Use Vue's actual scope rewriting and template classes. No browser or live data.
function component(name) {
const filename = path.join(base, name)
const { descriptor, errors } = parse(fs.readFileSync(filename, 'utf8'), { filename })
assert.deepEqual(errors, [])
const rules = []
for (const block of descriptor.styles.slice(1)) {
const file = block.src ? path.resolve(base, block.src) : filename
const source = block.src ? fs.readFileSync(file, 'utf8') : block.content
const compiled = compileStyle({ filename: file, source, scoped: block.scoped, id: scope, preprocessLang: block.lang })
assert.deepEqual(compiled.errors, [])
postcss.parse(compiled.code).walkRules(rule => {
assert.ok(rule.selectors.every(selector => selector.startsWith('html[data-admin-ui=v2] ')), rule.selector)
rules.push({ rule, matches: compile(rule.selector.replaceAll(':focus-visible', '[data-focus-visible]')) })
})
}
const dom = parseDOM('<html data-admin-ui="v2"><body>' + descriptor.template.content + '</body></html>')
selectAll('body *', dom).forEach(el => { el.attribs[scope] = '' })
return { dom, rules }
}
const page = component('index.vue')
function properties(rules, node) {
const values = {}
for (const { rule, matches } of rules) {
if (matches(node)) {
rule.walkDecls(d => { values[d.prop] = d.value })
}
}
return values
}
const theme = postcss.parse(sass.compile(path.join(src, 'styles/admin-v2.scss')).css)
function themeColors(dark) {
const vars = {}
for (const selector of [':root[data-admin-ui=v2]', ...(dark ? [':root[data-admin-ui=v2].dark'] : [])]) {
theme.nodes.find(n => n.selector === selector).walkDecls(d => { vars[d.prop] = d.value })
}
function rgb(value) {
const ref = /^var\((--[\w-]+)\)$/.exec(value)
if (ref) return rgb(vars[ref[1]])
if (value.startsWith('rgb(')) return value.match(/[\d.]+/g).map(Number)
const mix = /^color-mix\(in srgb, (var\([^)]+\)) (\d+)%, (var\([^)]+\))\)$/.exec(value)
if (mix) {
const a = rgb(mix[1]), b = rgb(mix[3]), weight = Number(mix[2]) / 100
return a.map((v, i) => v * weight + b[i] * (1 - weight))
}
assert.match(value, /^#(?:[a-f\d]{3}|[a-f\d]{6})$/i)
const hex = value.slice(1)
return (hex.length === 3 ? [...hex].map(c => c + c) : hex.match(/../g)).map(c => parseInt(c, 16))
}
return rgb
}
function contrast(a, b) {
const luma = rgb => rgb.reduce((sum, byte, i) => {
const c = byte / 255
return sum + [0.2126, 0.7152, 0.0722][i] * (c <= 0.04045 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4)
}, 0)
const [high, low] = [luma(a), luma(b)].sort((x, y) => y - x)
return (high + .05) / (low + .05)
}
test('both sets of four counts share a neutral card and a legible hint; the active tab follows the primary action', () => {
const { dom, rules } = page
const cards = selectAll('.metric-card, .customer-metric-card', dom)
assert.equal(cards.length, 8)
for (const card of cards) {
const node = properties(rules, card), hint = properties(rules, selectOne('p', card))
const icon = properties(rules, selectOne('.metric-icon', card))
assert.equal(node.background, 'var(--zyt-surface)')
assert.equal(node['border-color'], 'var(--zyt-card-border)')
assert.equal(node['box-shadow'], 'none')
assert.equal(icon.background, 'var(--zyt-accent-soft)')
assert.ok(parseFloat(hint['font-size']) >= 12)
for (const rgb of [themeColors(false), themeColors(true)]) {
assert.ok(contrast(rgb(hint.color), rgb(node.background)) >= 4.5)
}
}
const tab = selectOne('.tab-nav button', dom)
tab.attribs.class = 'active'
assert.equal(properties(rules, tab)['border-bottom-color'], 'var(--zyt-primary)')
assert.ok(selectOne('.section-heading-actions el-button[type="primary"]', dom))
})
test('running, offline, waiting and error text keep distinct, legible color pairs in both themes', () => {
const { dom, rules } = page
const states = []
for (const className of ['status-tag is-online', 'status-tag is-offline', 'availability is-waiting', 'availability is-error']) {
const el = selectOne('.pool-title-row span', dom)
el.attribs.class = className
const { color, background } = properties(rules, el)
states.push(background)
for (const rgb of [themeColors(false), themeColors(true)]) {
assert.ok(contrast(rgb(color), rgb(background)) >= 4.5, className)
}
}
assert.equal(new Set(states).size, states.length)
})
test('classic is unchanged and V2 editor styles do not reach the customer previews, template swatches or copied code', () => {
const classicPage = component('index.vue')
selectOne('html', classicPage.dom).attribs['data-admin-ui'] = 'legacy'
for (const node of selectAll('body *', classicPage.dom)) assert.deepEqual(properties(classicPage.rules, node), {})
for (const [name, preview] of [
['components/WecomFloatingWidgetBuilder.vue', '.preview-canvas, .template-swatch, .code-card pre'],
['components/WelcomeMessageEditor.vue', '.welcome-preview'],
['components/PromotionAutomationForm.vue', '.remark-preview']
]) {
const { dom, rules } = component(name)
assert.ok(rules.length, name)
const roots = selectAll(preview, dom)
assert.ok(roots.length, name)
for (const node of roots.flatMap(n => [n, ...selectAll('*', n)])) {
assert.deepEqual(properties(rules, node), {}, name + ': ' + node.attribs.class)
}
selectOne('html', dom).attribs['data-admin-ui'] = 'legacy'
for (const node of selectAll('body *', dom)) assert.deepEqual(properties(rules, node), {})
}
})