|
| 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 | +} |
| 16 | + |
| 17 | +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 | + }); |
| 42 | + |
| 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 | + }); |
| 54 | + |
| 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}'; |
| 68 | + const encoder = new TextEncoder(); |
| 69 | + const bytes = encoder.encode(text); |
| 70 | + const binaryString = String.fromCharCode(...bytes); |
| 71 | + const base64 = btoa(binaryString); |
| 72 | + |
| 73 | + const result = decodeBase64WithUTF8(base64); |
| 74 | + expect(result).toBe(text); |
| 75 | + expect(result).toContain('👇'); |
| 76 | + }); |
| 77 | + |
| 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 | + |
| 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 | + |
| 98 | + const modernResult = decodeBase64WithUTF8(base64); |
| 99 | + |
| 100 | + // The deprecated pattern: decodeURIComponent(escape(atob(base64))) |
| 101 | + // escape() converts the incorrectly-decoded UTF-8 bytes to percent-encoding |
| 102 | + // decodeURIComponent() then interprets those percent-encoded bytes as UTF-8 |
| 103 | + const deprecatedResult = decodeURIComponent(escape(atob(base64))); |
| 104 | + |
| 105 | + expect(modernResult).toBe(deprecatedResult); |
| 106 | + expect(modernResult).toBe(text); |
| 107 | + }); |
| 108 | +}); |
0 commit comments