Skip to content

Commit a91ced0

Browse files
committed
Update test
1 parent 373b4fa commit a91ced0

2 files changed

Lines changed: 12 additions & 89 deletions

File tree

src/library/config/defaultFunctions.test.ts

Lines changed: 9 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,28 @@
11
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';
163

174
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';
427

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)));
5410

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
6812
const encoder = new TextEncoder();
6913
const bytes = encoder.encode(text);
7014
const binaryString = String.fromCharCode(...bytes);
7115
const base64 = btoa(binaryString);
72-
73-
const result = decodeBase64WithUTF8(base64);
74-
expect(result).toBe(text);
75-
expect(result).toContain('👇');
76-
});
7716

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-
});
8917

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);
9819
const modernResult = decodeBase64WithUTF8(base64);
99-
20+
10021
// The deprecated pattern: decodeURIComponent(escape(atob(base64)))
10122
// escape() converts the incorrectly-decoded UTF-8 bytes to percent-encoding
10223
// decodeURIComponent() then interprets those percent-encoded bytes as UTF-8
10324
const deprecatedResult = decodeURIComponent(escape(atob(base64)));
104-
25+
10526
expect(modernResult).toBe(deprecatedResult);
10627
expect(modernResult).toBe(text);
10728
});

src/library/config/defaultFunctions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { ErrorResponses } from "./GithubPermalinkContext";
66
/**
77
* This is AI generated code from GitHub Copilot.
88
* See: https://github.com/dwjohnston/react-github-permalink/pull/79
9+
* But based on my reading of this:https://stackoverflow.com/a/56647993/1068446
10+
* But the suggested answer is using deprecated functions (escape/unescape)
911
*
1012
* Properly decode base64 string with UTF-8 support.
1113
* GitHub API returns base64-encoded content that may contain UTF-8 characters like emojis.
@@ -16,7 +18,7 @@ import { ErrorResponses } from "./GithubPermalinkContext";
1618
* The solution: Convert the binary string to a byte array, then use TextDecoder to properly
1719
* interpret those bytes as UTF-8.
1820
*/
19-
function decodeBase64WithUTF8(base64: string): string {
21+
export function decodeBase64WithUTF8(base64: string): string {
2022
// Remove whitespace that GitHub API might include
2123
const cleanedBase64 = base64.replace(/\s/g, '');
2224

0 commit comments

Comments
 (0)