Form files: the visitor is told when the problem is the file — why and what to do; UTF-8 file names

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-13 16:51:05 +01:00
co-authored by Claude Fable 5.2
parent b6f9f34705
commit 97202ee03e
18 changed files with 335 additions and 128 deletions
+39 -13
View File
@@ -175,12 +175,15 @@ describe('the form in the browser', () => {
test('instant checks use the same messages as the server', () => {
const pdf = (name: string, size: number) => ({ name, size })
const hint = ' You can also send the form without the file.'
expect(fileFieldError(field, [pdf('cv.pdf', MB)], texts)).toBeUndefined()
expect(fileFieldError(field, [pdf('cv.pdf', 3 * MB)], texts)).toBe('"cv.pdf" is too large. The limit is 2 MB.')
expect(fileFieldError(field, [pdf('cv.png', 10)], texts)).toBe('"cv.png" is not an accepted file type. Accepted: PDF, DOCX.')
expect(fileFieldError(field, [pdf('a.pdf', 1), pdf('b.pdf', 1), pdf('c.pdf', 1)], texts)).toBe('Attach at most 2 files in "Attachment (optional)".')
expect(fileFieldError(field, [pdf('a.pdf', 1.6 * MB), pdf('b.pdf', 1.6 * MB)], texts)).toBe('"b.pdf" is too large. The limit is 3 MB.')
expect(fileFieldError(field, [pdf('cv.pdf', 3 * MB)], NOTIFICATION_FALLBACK_PL.form)).toBe('Plik „cv.pdf” jest za duży. Limit to 2 MB.')
expect(fileFieldError(field, [pdf('cv.pdf', 3 * MB)], texts)).toBe(`The file "cv.pdf" is too large. Choose a file of up to 2 MB.${hint}`)
expect(fileFieldError(field, [pdf('cv.png', 10)], texts)).toBe(`The file "cv.png" is in a format we do not accept. Choose one of: PDF, DOCX.${hint}`)
expect(fileFieldError(field, [pdf('a.pdf', 1), pdf('b.pdf', 1), pdf('c.pdf', 1)], texts)).toBe(`Too many files in "Attachment (optional)". Remove some: the limit is 2.${hint}`)
expect(fileFieldError(field, [pdf('a.pdf', 1.6 * MB), pdf('b.pdf', 1.6 * MB)], texts)).toBe(`The file "b.pdf" is too large. Choose a file of up to 3 MB.${hint}`)
expect(fileFieldError(field, [pdf('cv.pdf', 3 * MB)], NOTIFICATION_FALLBACK_PL.form)).toBe('Plik „cv.pdf” jest za duży. Wybierz plik do 2 MB. Możesz też wysłać formularz bez pliku.')
// A required field: no "send without the file".
expect(fileFieldError({ ...field, required: true }, [pdf('cv.pdf', 3 * MB)], texts)).toBe('The file "cv.pdf" is too large. Choose a file of up to 2 MB.')
// Older text objects without the file keys still give a message.
expect(fileFieldError(field, [pdf('cv.png', 10)], { validation: 'Check the field "{field}".' })).toBe('Check the field "Attachment (optional)".')
})
@@ -189,7 +192,7 @@ describe('the form in the browser', () => {
expect(initialBuilderValues(form.fields)).toEqual({ name: '', attachment: [] })
expect(validateBuilderFields([field], { attachment: [] }, texts)).toEqual({})
expect(validateBuilderFields([{ ...field, required: true }], { attachment: [] }, texts)).toEqual({ attachment: 'Check the field "Attachment (optional)".' })
expect(validateBuilderFields([field], { attachment: [{ name: 'x.exe', size: 5 }] }, texts).attachment).toContain('not an accepted file type')
expect(validateBuilderFields([field], { attachment: [{ name: 'x.exe', size: 5 }] }, texts).attachment).toContain('is in a format we do not accept')
})
test('markup: a native file input with a label, help text tied to it, accept and multiple', () => {
@@ -225,15 +228,38 @@ describe('messages and helpers', () => {
const texts = NOTIFICATION_FALLBACK.form
test('every file outcome has its own text; the placeholders are filled', () => {
const fail = (extra: Record<string, unknown>) => resolveFormMessage({ field: 'cv', label: 'CV', reason: 'validation', success: false, ...extra } as never, texts)
expect(fail({ file: 'a.pdf', kind: 'file_too_large', max: 10 })).toBe('"a.pdf" is too large. The limit is 10 MB.')
expect(fail({ file: 'a.exe', kind: 'file_type', types: 'PDF, DOCX' })).toBe('"a.exe" is not an accepted file type. Accepted: PDF, DOCX.')
expect(fail({ kind: 'file_count', max: 2 })).toBe('Attach at most 2 files in "CV".')
const fail = (extra: Record<string, unknown>, t = texts) => resolveFormMessage({ field: 'cv', label: 'CV', reason: 'validation', success: false, ...extra } as never, t)
// English: which file, why, what to do
expect(fail({ file: 'a.pdf', kind: 'file_too_large', max: 10 })).toBe('The file "a.pdf" is too large. Choose a file of up to 10 MB.')
expect(fail({ file: 'a.txt', kind: 'file_type', types: 'PDF, DOCX' })).toBe('The file "a.txt" is in a format we do not accept. Choose one of: PDF, DOCX.')
expect(fail({ file: 'a.exe', kind: 'file_refused', types: 'PDF, DOCX' })).toBe(
'The file "a.exe" was refused: it is a program or an archive, it holds macros, or its content does not match its type. Choose another file (PDF, DOCX).',
)
expect(fail({ kind: 'file_count', max: 2 })).toBe('Too many files in "CV". Remove some: the limit is 2.')
expect(resolveFormMessage({ field: 'cv', file: 'a.pdf', reason: 'delivery', success: false }, texts)).toBe(
'We could not send the file "a.pdf". Choose a smaller file or another format and try again.',
)
// an optional field: every file message adds that the form can go without the file
for (const extra of [{ file: 'a.pdf', kind: 'file_too_large', max: 10 }, { file: 'a.txt', kind: 'file_type', types: 'PDF' }, { file: 'a.exe', kind: 'file_refused', types: 'PDF' }, { kind: 'file_count', max: 2 }]) {
expect(fail({ ...extra, optional: true })).toMatch(/ You can also send the form without the file\.$/)
}
expect(resolveFormMessage({ field: 'cv', file: 'a.pdf', optional: true, reason: 'delivery', success: false }, texts)).toMatch(/try again\. You can also send the form without the file\.$/)
// Polish
const pl = NOTIFICATION_FALLBACK_PL.form
expect(fail({ file: 'a.pdf', kind: 'file_too_large', max: 10 }, pl)).toBe('Plik „a.pdf” jest za duży. Wybierz plik do 10 MB.')
expect(fail({ file: 'a.txt', kind: 'file_type', types: 'PDF, DOCX' }, pl)).toBe('Plik „a.txt” ma format, którego nie przyjmujemy. Wybierz jeden z: PDF, DOCX.')
expect(fail({ file: 'a.exe', kind: 'file_refused', types: 'PDF, DOCX' }, pl)).toBe(
'Plik „a.exe” został odrzucony: to program lub archiwum, zawiera makra albo jego zawartość nie odpowiada typowi. Wybierz inny plik (PDF, DOCX).',
)
expect(fail({ kind: 'file_count', max: 2, optional: true }, pl)).toBe('Za dużo plików w polu „CV”. Usuń część: limit to 2. Możesz też wysłać formularz bez pliku.')
expect(resolveFormMessage({ field: 'cv', file: 'a.pdf', reason: 'delivery', success: false }, pl)).toBe('Nie udało się wysłać pliku „a.pdf”. Wybierz mniejszy plik lub inny format i spróbuj ponownie.')
// Failures that are not about a file keep their own texts
expect(fail({ kind: 'required' })).toBe('Check the field "CV".')
expect(resolveFormMessage({ reason: 'delivery', success: false }, texts)).toBe('We could not send your file. Try again, or send it by e-mail.')
expect(resolveFormMessage({ reason: 'delivery', success: false }, NOTIFICATION_FALLBACK_PL.form)).toBe('Nie udało się wysłać pliku. Spróbuj ponownie lub wyślij go e-mailem.')
expect(fail({ kind: 'invalid_format', optional: true })).toBe('Check the field "CV".')
expect(resolveFormMessage({ reason: 'error', success: false }, texts)).toBe(texts.error)
expect(resolveFormMessage({ reason: 'rate_limited', success: false }, texts)).toBe(texts.rateLimited)
// Texts written before the file keys existed fall back to the general ones.
const { fileCount: _a, fileNotSent: _b, fileRemove: _c, fileTooLarge: _d, fileType: _e, ...old } = texts
const { fileCount: _a, fileNotSent: _b, fileOptional: _f, fileRefused: _g, fileRemove: _c, fileTooLarge: _d, fileType: _e, ...old } = texts
expect(resolveFormMessage({ reason: 'delivery', success: false }, old)).toBe(texts.error)
expect(resolveFormMessage({ field: 'cv', file: 'a.pdf', kind: 'file_too_large', max: 1, reason: 'validation', success: false }, old)).toBe('Check the field "cv".')
})