|
1 | 1 | import { describe, it, expect } from 'vitest'; |
2 | | - |
3 | | -/** |
4 | | - * This is a copy of the helper function from defaultFunctions.ts for testing purposes. |
5 | | - */ |
6 | | -function decodeBase64WithUTF8(base64: string): string { |
7 | | - const cleanedBase64 = base64.replace(/\s/g, ''); |
8 | | - const binaryString = atob(cleanedBase64); |
9 | | - const bytes = new Uint8Array(binaryString.length); |
10 | | - for (let i = 0; i < binaryString.length; i++) { |
11 | | - bytes[i] = binaryString.charCodeAt(i); |
12 | | - } |
13 | | - const decoder = new TextDecoder('utf-8'); |
14 | | - return decoder.decode(bytes); |
15 | | -} |
| 2 | +import { decodeBase64WithUTF8 } from './defaultFunctions'; |
16 | 3 |
|
17 | 4 | describe('Base64 UTF-8 Decoding', () => { |
18 | | - it('should correctly decode base64 with ASCII text', () => { |
19 | | - // Simple ASCII text |
20 | | - const text = 'Hello World'; |
21 | | - const encoder = new TextEncoder(); |
22 | | - const bytes = encoder.encode(text); |
23 | | - const binaryString = String.fromCharCode(...bytes); |
24 | | - const base64 = btoa(binaryString); |
25 | | - |
26 | | - const result = decodeBase64WithUTF8(base64); |
27 | | - expect(result).toBe(text); |
28 | | - }); |
29 | | - |
30 | | - it('should correctly decode base64 with emoji', () => { |
31 | | - // Text with emoji - the actual issue from GitHub |
32 | | - const text = '// 👇 Check this out'; |
33 | | - const encoder = new TextEncoder(); |
34 | | - const bytes = encoder.encode(text); |
35 | | - const binaryString = String.fromCharCode(...bytes); |
36 | | - const base64 = btoa(binaryString); |
37 | | - |
38 | | - const result = decodeBase64WithUTF8(base64); |
39 | | - expect(result).toBe(text); |
40 | | - expect(result).toContain('👇'); |
41 | | - }); |
| 5 | + it('should match the deprecated escape/unescape pattern behavior', () => { |
| 6 | + const text = '// 👇 This is a comment with emoji'; |
42 | 7 |
|
43 | | - it('should correctly decode base64 with pointing up emoji', () => { |
44 | | - // The specific emoji mentioned in the user's comment |
45 | | - const text = '☝️'; |
46 | | - const encoder = new TextEncoder(); |
47 | | - const bytes = encoder.encode(text); |
48 | | - const binaryString = String.fromCharCode(...bytes); |
49 | | - const base64 = btoa(binaryString); |
50 | | - |
51 | | - const result = decodeBase64WithUTF8(base64); |
52 | | - expect(result).toBe(text); |
53 | | - }); |
| 8 | + // We're using the deprecated pattern for a sanity check to see that the AI generated version is correct. |
| 9 | + const deprecatedEncodedString = btoa(unescape(encodeURIComponent(text))); |
54 | 10 |
|
55 | | - it('should correctly decode base64 with multiple emojis', () => { |
56 | | - const text = '👍 👎 😕 ❤️ 🎉'; |
57 | | - const encoder = new TextEncoder(); |
58 | | - const bytes = encoder.encode(text); |
59 | | - const binaryString = String.fromCharCode(...bytes); |
60 | | - const base64 = btoa(binaryString); |
61 | | - |
62 | | - const result = decodeBase64WithUTF8(base64); |
63 | | - expect(result).toBe(text); |
64 | | - }); |
65 | | - |
66 | | - it('should correctly decode base64 with mixed content', () => { |
67 | | - const text = 'export function Example() {\n // 👇 This is a comment\n return <div>Hello</div>;\n}'; |
| 11 | + // Verify our solution gives the same result as the deprecated method |
68 | 12 | const encoder = new TextEncoder(); |
69 | 13 | const bytes = encoder.encode(text); |
70 | 14 | const binaryString = String.fromCharCode(...bytes); |
71 | 15 | const base64 = btoa(binaryString); |
72 | | - |
73 | | - const result = decodeBase64WithUTF8(base64); |
74 | | - expect(result).toBe(text); |
75 | | - expect(result).toContain('👇'); |
76 | | - }); |
77 | 16 |
|
78 | | - it('should handle base64 with whitespace (as GitHub API might return)', () => { |
79 | | - const text = 'Test 👍'; |
80 | | - const encoder = new TextEncoder(); |
81 | | - const bytes = encoder.encode(text); |
82 | | - const binaryString = String.fromCharCode(...bytes); |
83 | | - const base64 = btoa(binaryString); |
84 | | - const base64WithWhitespace = base64.slice(0, 5) + '\n' + base64.slice(5); |
85 | | - |
86 | | - const result = decodeBase64WithUTF8(base64WithWhitespace); |
87 | | - expect(result).toBe(text); |
88 | | - }); |
89 | 17 |
|
90 | | - it('should match the deprecated escape/unescape pattern behavior', () => { |
91 | | - // Verify our solution gives the same result as the deprecated method |
92 | | - const text = '// 👇 This is a comment with emoji'; |
93 | | - const encoder = new TextEncoder(); |
94 | | - const bytes = encoder.encode(text); |
95 | | - const binaryString = String.fromCharCode(...bytes); |
96 | | - const base64 = btoa(binaryString); |
97 | | - |
| 18 | + expect(deprecatedEncodedString).toBe(base64); |
98 | 19 | const modernResult = decodeBase64WithUTF8(base64); |
99 | | - |
| 20 | + |
100 | 21 | // The deprecated pattern: decodeURIComponent(escape(atob(base64))) |
101 | 22 | // escape() converts the incorrectly-decoded UTF-8 bytes to percent-encoding |
102 | 23 | // decodeURIComponent() then interprets those percent-encoded bytes as UTF-8 |
103 | 24 | const deprecatedResult = decodeURIComponent(escape(atob(base64))); |
104 | | - |
| 25 | + |
105 | 26 | expect(modernResult).toBe(deprecatedResult); |
106 | 27 | expect(modernResult).toBe(text); |
107 | 28 | }); |
|
0 commit comments