From 6488907298bf9b28ed9a6fd9d53d90e6fea5e61f Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 10 Dec 2024 19:36:48 +0000 Subject: [PATCH] Harden tests to account for new fast path --- packages/lexicon/tests/_scaffolds/lexicons.ts | 18 ++++ packages/lexicon/tests/general.test.ts | 101 ++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/packages/lexicon/tests/_scaffolds/lexicons.ts b/packages/lexicon/tests/_scaffolds/lexicons.ts index d0cf414ccef..cc9cede6ef9 100644 --- a/packages/lexicon/tests/_scaffolds/lexicons.ts +++ b/packages/lexicon/tests/_scaffolds/lexicons.ts @@ -313,6 +313,24 @@ const lexicons: LexiconDoc[] = [ }, }, }, + { + lexicon: 1, + id: 'com.example.stringLengthNoMinLength', + defs: { + main: { + type: 'record', + record: { + type: 'object', + properties: { + string: { + type: 'string', + maxLength: 4, + }, + }, + }, + }, + }, + }, { lexicon: 1, id: 'com.example.stringLengthGrapheme', diff --git a/packages/lexicon/tests/general.test.ts b/packages/lexicon/tests/general.test.ts index 1055033bc80..abd337a7329 100644 --- a/packages/lexicon/tests/general.test.ts +++ b/packages/lexicon/tests/general.test.ts @@ -671,6 +671,107 @@ describe('Record validation', () => { ).toThrow('Record/string must not be longer than 4 characters') }) + it('Applies string length constraint (no minLength)', () => { + // Shorter than two UTF8 characters + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: '', + }) + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: 'a', + }) + + // Two to four UTF8 characters + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: 'ab', + }) + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: '\u0301', // Combining acute accent (2 bytes) + }) + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: 'a\u0301', // 'a' + combining acute accent (1 + 2 bytes = 3 bytes) + }) + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: 'aé', // 'a' (1 byte) + 'é' (2 bytes) = 3 bytes + }) + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: 'abc', + }) + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: '一', // CJK character (3 bytes) + }) + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: '\uD83D', // Unpaired high surrogate (3 bytes) + }) + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: 'abcd', + }) + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: 'éé', // 'é' + 'é' (2 + 2 bytes = 4 bytes) + }) + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: 'aaé', // 1 + 1 + 2 = 4 bytes + }) + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: '👋', // 4 bytes + }) + + expect(() => + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: 'abcde', + }), + ).toThrow('Record/string must not be longer than 4 characters') + expect(() => + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: 'a\u0301\u0301', // 1 + (2 * 2) = 5 bytes + }), + ).toThrow('Record/string must not be longer than 4 characters') + expect(() => + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: '\uD83D\uD83D', // Two unpaired high surrogates (3 * 2 = 6 bytes) + }), + ).toThrow('Record/string must not be longer than 4 characters') + expect(() => + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: 'ééé', // 2 + 2 + 2 bytes = 6 bytes + }), + ).toThrow('Record/string must not be longer than 4 characters') + expect(() => + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: '👋a', // 4 + 1 bytes = 5 bytes + }), + ).toThrow('Record/string must not be longer than 4 characters') + expect(() => + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: '👨👨', // 4 + 4 = 8 bytes + }), + ).toThrow('Record/string must not be longer than 4 characters') + expect(() => + lex.assertValidRecord('com.example.stringLengthNoMinLength', { + $type: 'com.example.stringLengthNoMinLength', + string: '👨‍👩‍👧‍👧', // 4 emojis × 4 bytes + 3 ZWJs × 3 bytes = 25 bytes + }), + ).toThrow('Record/string must not be longer than 4 characters') + }) + it('Applies grapheme string length constraint', () => { // Shorter than two graphemes expect(() =>