qazaq-legacy-foundation-web/src/lib/hero-contrast.test.ts
2026-11-17 18:37:44 +05:00

57 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { readFileSync } from 'node:fs'
import { describe, expect, it } from 'vitest'
import { getDict } from '@/i18n'
// QLFW-24: первый экран контрастный (заголовок, подзаголовок, кнопка «Направления»).
const css = readFileSync('src/app/(site)/globals.css', 'utf8')
const page = readFileSync('src/app/(site)/[locale]/page.tsx', 'utf8')
const color = (name: string) => css.match(new RegExp(`--color-${name}:\\s*(#[0-9a-f]{6})`, 'i'))![1]
const lum = (hex: string) => {
const c = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16) / 255)
.map((v) => (v <= 0.03928 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4))
return 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]
}
const ratio = (a: string, b: string) => {
const [x, y] = [lum(a), lum(b)].sort((p, q) => q - p)
return (x + 0.05) / (y + 0.05)
}
describe('QLFW-24: контраст первого экрана', () => {
it('заголовок и подзаголовок тёмные на светлом фоне (не ниже 7:1)', () => {
expect(ratio(color('graphite'), color('paper'))).toBeGreaterThanOrEqual(7)
expect(page).toMatch(/<h1[^>]*text-graphite/)
expect(page).toMatch(/<p className="reveal reveal-2[^"]*text-graphite/)
})
it('кнопка «Направления» бордовая с белым текстом (не ниже 4.5:1)', () => {
expect(ratio(color('burgundy'), '#ffffff')).toBeGreaterThanOrEqual(4.5)
expect(page).toMatch(/href="#directions"[^>]*bg-burgundy[^>]*text-white/)
})
it('анимация не меняет прозрачность текста', () => {
expect(css).not.toMatch(/@keyframes rise[^}]*opacity/)
})
})
// QLFW-48: телефон и все три языка.
describe('QLFW-48: первый экран на телефоне и на RU, KK, EN', () => {
it.each(['ru', 'kk', 'en'] as const)('%s: девиз, подзаголовок и кнопка заполнены', (l) => {
const t = getDict(l)
expect(t.motto.trim()).not.toBe('')
expect(t.heroLead.trim()).not.toBe('')
expect(t.nav.directions.trim()).not.toBe('')
})
it('самое длинное слово девиза помещается на 375px при минимальном размере шрифта', () => {
// 2.4rem = 38.4px, средняя ширина буквы serif ≈ 0.6em; поля по 20px
const avail = 375 - 40
for (const l of ['ru', 'kk', 'en'] as const) {
const longest = Math.max(...getDict(l).motto.split(/\s+/).map((w) => w.length))
expect(longest * 38.4 * 0.6).toBeLessThanOrEqual(avail)
}
})
it('узор скрыт на телефоне и не перехватывает нажатия; заголовок не обрезается', () => {
expect(page).toMatch(/pointer-events-none[^"]*\bhidden\b[^"]*sm:block/)
expect(page).not.toMatch(/<h1[^>]*(truncate|overflow-hidden|line-clamp)/)
expect(page).toMatch(/<h1[^>]*clamp\(2\.4rem/)
})
})