qazaq-legacy-foundation-web/src/lib/inquiry.test.ts
Peppa 7e8dfa000b
All checks were successful
peppa/lint lint: success за 1с
peppa/typecheck typecheck: success за 3с
peppa/test test: success за 1с
peppa/build build: success за 48с
ci / checks (pull_request) Successful in 1m31s
deploy / deploy (pull_request) Successful in 18s
QLFW-15: форма обращения, страница контактов, сохранение и отправка обращений
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-17 17:14:29 +05:00

41 lines
2.3 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 { beforeEach, describe, expect, it } from 'vitest'
import { rateLimited, resetRateLimit, validateInquiry } from './inquiry'
const good = { name: 'Анна', email: 'anna@example.com', organization: '', message: 'Здравствуйте', consent: 'on', website: '', openedAt: 1000 }
const later = 1000 + 10_000
describe('Сценарий: проверка обращения', () => {
it('принимает заполненную форму с согласием; организация необязательна', () => {
const r = validateInquiry(good, later)
expect(r).toEqual({ ok: true, data: { name: 'Анна', email: 'anna@example.com', organization: '', message: 'Здравствуйте' } })
})
it('без согласия отправить нельзя', () => {
const r = validateInquiry({ ...good, consent: undefined }, later)
expect(r).toEqual({ ok: false, errors: ['consent'] })
})
it('пустые обязательные поля и плохая почта дают подсказки по каждому полю', () => {
const r = validateInquiry({ ...good, name: ' ', email: 'нет', message: '' }, later)
expect(r).toEqual({ ok: false, errors: ['name', 'email', 'message'] })
})
it('слишком длинный текст отклоняется', () => {
const r = validateInquiry({ ...good, message: 'а'.repeat(5001) }, later)
expect(r).toEqual({ ok: false, errors: ['tooLong'] })
})
it('заполненная скрытая ловушка — спам', () => {
expect(validateInquiry({ ...good, website: 'http://spam' }, later)).toEqual({ ok: false, spam: true })
})
it('отправка быстрее 2,5 секунд — спам', () => {
expect(validateInquiry(good, 1500)).toEqual({ ok: false, spam: true })
})
})
describe('Сценарий: ограничение частоты', () => {
beforeEach(resetRateLimit)
it('четвёртое обращение за 10 минут с одного адреса блокируется, потом снова можно', () => {
expect([1, 2, 3].map((i) => rateLimited('a', i))).toEqual([false, false, false])
expect(rateLimited('a', 4)).toBe(true)
expect(rateLimited('b', 4)).toBe(false)
expect(rateLimited('a', 11 * 60 * 1000)).toBe(false)
})
})