@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 86 lines 3.32 KB
Raw
1 import fc from 'fast-check';
2 import { describe, expect, it } from 'vitest';
3
4 import { toUnicodePattern } from '../src/utils/unicode-pattern';
5
6 const compiles = (pattern: string, flags?: string): boolean => {
7 try {
8 new RegExp(pattern, flags);
9 return true;
10 } catch {
11 return false;
12 }
13 };
14
15 const legacyCases: ReadonlyArray<{ pattern: string; matches: string[]; rejects: string[] }> = [
16 { pattern: '^[a-z\\_]+$', matches: ['a_b'], rejects: ['A'] },
17 { pattern: '^\\k_$', matches: ['k_'], rejects: ['wrong', '\\k_'] },
18 { pattern: '^a\\:b\\@c$', matches: ['a:b@c'], rejects: ['ab'] },
19 { pattern: '^a{$', matches: ['a{'], rejects: ['a'] },
20 { pattern: '^}]$', matches: ['}]'], rejects: [']'] },
21 { pattern: '^\\x4$', matches: ['x4'], rejects: ['\x04'] },
22 { pattern: '^\\u12$', matches: ['u12'], rejects: ['\u0012'] },
23 { pattern: '^\\p{L}$', matches: ['p{L}'], rejects: ['a'] },
24 { pattern: '^\\u{2}$', matches: ['uu'], rejects: ['\u0002'] },
25 { pattern: '^\\c$', matches: ['\\c'], rejects: ['c'] },
26 { pattern: '^[\\c1]$', matches: ['\x11'], rejects: ['1'] },
27 { pattern: '^\\1$', matches: ['\x01'], rejects: ['1'] },
28 { pattern: '^(a)\\1\\2$', matches: ['aa\x02'], rejects: ['aa'] },
29 { pattern: '^\\8$', matches: ['8'], rejects: ['\\8'] },
30 { pattern: '^[\\w-.]+$', matches: ['a-b.c'], rejects: ['a b'] },
31 { pattern: '^[.-\\d]$', matches: ['-', '5'], rejects: ['/'] },
32 { pattern: '^[\\B]$', matches: ['B'], rejects: ['b'] },
33 ];
34
35 describe('toUnicodePattern', () => {
36 it.each(legacyCases)('gives $pattern its legacy meaning', ({ pattern, matches, rejects }) => {
37 const unicode = toUnicodePattern(pattern);
38 expect(unicode).toBeDefined();
39
40 for (const value of [...matches, ...rejects]) {
41 expect(new RegExp(unicode!, 'u').test(value), value).toBe(new RegExp(pattern).test(value));
42 }
43 expect(matches.every(value => new RegExp(pattern).test(value))).toBe(true);
44 expect(rejects.some(value => new RegExp(pattern).test(value))).toBe(false);
45 });
46
47 it('keeps patterns that already mean the same in both grammars', () => {
48 expect(toUnicodePattern('^[a-z]+(?:-[a-z]+)*$')).toBe('^[a-z]+(?:-[a-z]+)*$');
49 expect(toUnicodePattern('^(?<year>\\d{4})-\\k<year>$')).toBe('^(?<year>\\d{4})-\\k<year>$');
50 });
51
52 it('returns undefined for a quantified lookahead, which has no Unicode spelling', () => {
53 expect(compiles('^(?=a){2}a$')).toBe(true);
54 expect(toUnicodePattern('^(?=a){2}a$')).toBeUndefined();
55 });
56
57 it('matches exactly what the legacy pattern matches on ASCII input', () => {
58 const pattern = fc
59 .array(
60 fc.constantFrom(
61 ...'ab_:-.^$*+?()[]{}|/\\0189cdkpuxBDSWw,'.split(''),
62 '\\',
63 '\\\\',
64 '{2}',
65 '{1,3}'
66 ),
67 { maxLength: 12 }
68 )
69 .map(parts => parts.join(''))
70 .filter(candidate => compiles(candidate));
71 const input = fc.string({ unit: fc.constantFrom(...'ab_:-.^${}[]\\/ 019cdkpuxBw,'.split('')) });
72
73 fc.assert(
74 fc.property(pattern, fc.array(input, { maxLength: 8 }), (candidate, values) => {
75 const unicode = toUnicodePattern(candidate);
76 if (unicode === undefined) {
77 return;
78 }
79 for (const value of values) {
80 expect(new RegExp(unicode, 'u').test(value)).toBe(new RegExp(candidate).test(value));
81 }
82 }),
83 { numRuns: 2000 }
84 );
85 });
86 });