forked from ZenUml/core
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest-compression.html
More file actions
274 lines (240 loc) · 7.6 KB
/
test-compression.html
File metadata and controls
274 lines (240 loc) · 7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ZenUML Compression Test</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
textarea {
width: 100%;
height: 200px;
margin: 10px 0;
font-family: monospace;
}
button {
background: #007bff;
color: white;
border: none;
padding: 10px 20px;
margin: 5px;
cursor: pointer;
border-radius: 4px;
}
button:hover {
background: #0056b3;
}
.output {
background: #f4f4f4;
padding: 10px;
margin: 10px 0;
border-radius: 4px;
word-break: break-all;
}
.error {
color: red;
background: #fee;
padding: 10px;
margin: 10px 0;
border-radius: 4px;
}
.success {
color: green;
background: #efe;
padding: 10px;
margin: 10px 0;
border-radius: 4px;
}
</style>
</head>
<body>
<h1>ZenUML Compression Test</h1>
<h2>Test URL Parameter Encoding/Decoding</h2>
<textarea id="zenUmlCode" placeholder="Enter ZenUML code here...">title Authentication Flow
RET ret = A.methodA() {
B.method() {
if (X) {
C.methodC() {
a = A.methodA() {
D.method()
}
}
}
while (Y) {
C.methodC() {
A.methodA()
}
}
}
}
</textarea>
<div>
<button onclick="encodeAndCompress()">Encode (Gzip + Base64)</button>
<button onclick="testDecode()">Test Decode</button>
<button onclick="generateUrl()">Generate URL</button>
<button onclick="clearAll()">Clear</button>
</div>
<h3>Encoded Output:</h3>
<div id="encodedOutput" class="output"></div>
<h3>URL:</h3>
<div id="urlOutput" class="output"></div>
<h3>Test Results:</h3>
<div id="testResults"></div>
<script type="module">
import pako from 'pako';
// Helper function to escape HTML special characters
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
// Compression and encoding functions
function compressAndEncode(text) {
try {
// Step 1: Compress with gzip
const compressed = pako.gzip(text);
// Step 2: Convert to base64
let binary = '';
const bytes = new Uint8Array(compressed);
for (let i = 0; i < bytes.byteLength; i++) {
binary += String.fromCharCode(bytes[i]);
}
const base64 = btoa(binary);
return base64;
} catch (error) {
console.error('Compression error:', error);
throw error;
}
}
// Decompression and decoding functions
function decodeAndDecompress(base64String) {
try {
// Step 1: Decode base64
const binaryString = atob(base64String);
// Step 2: Convert to Uint8Array
const bytes = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
// Step 3: Decompress
const decompressed = pako.ungzip(bytes, { to: 'string' });
return decompressed;
} catch (error) {
console.error('Decompression error:', error);
throw error;
}
}
function encodeAndCompress() {
const zenUmlCode = document.getElementById('zenUmlCode').value;
const resultsDiv = document.getElementById('testResults');
resultsDiv.innerHTML = '';
try {
const encoded = compressAndEncode(zenUmlCode);
document.getElementById('encodedOutput').textContent = encoded;
// Show compression ratio
const originalSize = new Blob([zenUmlCode]).size;
const compressedSize = new Blob([encoded]).size;
const ratio = ((originalSize - compressedSize) / originalSize * 100).toFixed(2);
resultsDiv.innerHTML = `
<div class="success">
Encoding successful!<br>
Original size: ${originalSize} bytes<br>
Encoded size: ${compressedSize} bytes<br>
Compression: ${ratio}% smaller
</div>
`;
} catch (error) {
resultsDiv.innerHTML = `<div class="error">Encoding failed: ${escapeHtml(error.message)}</div>`;
}
}
function testDecode() {
const encoded = document.getElementById('encodedOutput').textContent;
const resultsDiv = document.getElementById('testResults');
if (!encoded) {
resultsDiv.innerHTML = '<div class="error">No encoded data to decode. Please encode first.</div>';
return;
}
try {
const decoded = decodeAndDecompress(encoded);
const original = document.getElementById('zenUmlCode').value;
if (decoded === original) {
resultsDiv.innerHTML = `
<div class="success">
Decoding successful! The decoded content matches the original.<br>
<pre>${decoded}</pre>
</div>
`;
} else {
resultsDiv.innerHTML = `
<div class="error">
Decoding mismatch!<br>
Original length: ${original.length}<br>
Decoded length: ${decoded.length}
</div>
`;
}
} catch (error) {
resultsDiv.innerHTML = `<div class="error">Decoding failed: ${error.message}</div>`;
}
}
function generateUrl() {
const encoded = document.getElementById('encodedOutput').textContent;
if (!encoded) {
document.getElementById('urlOutput').innerHTML = '<span style="color: red;">Please encode first</span>';
return;
}
const baseUrl = window.location.origin + '/renderer.html';
const url = `${baseUrl}?code=${encodeURIComponent(encoded)}`;
document.getElementById('urlOutput').innerHTML = `
<a href="${url}" target="_blank">${url}</a><br>
<small>URL length: ${url.length} characters</small>
`;
}
function clearAll() {
document.getElementById('zenUmlCode').value = '';
document.getElementById('encodedOutput').textContent = '';
document.getElementById('urlOutput').textContent = '';
document.getElementById('testResults').innerHTML = '';
}
// Test some edge cases
function runTests() {
const testCases = [
{ name: 'Empty string', input: '' },
{ name: 'Simple text', input: 'Hello World' },
{ name: 'Unicode', input: '你好世界 🌍' },
{ name: 'Special characters', input: '!@#$%^&*()_+-=[]{}|;:,.<>?' }
];
let results = '<h3>Automated Tests:</h3>';
for (const test of testCases) {
try {
const encoded = compressAndEncode(test.input);
const decoded = decodeAndDecompress(encoded);
if (decoded === test.input) {
results += `<div class="success">✓ ${test.name}: PASSED</div>`;
} else {
results += `<div class="error">✗ ${test.name}: FAILED - Mismatch</div>`;
}
} catch (error) {
results += `<div class="error">✗ ${test.name}: ERROR - ${error.message}</div>`;
}
}
document.getElementById('testResults').innerHTML = results;
}
// Make functions available globally for onclick handlers
window.encodeAndCompress = encodeAndCompress;
window.testDecode = testDecode;
window.generateUrl = generateUrl;
window.clearAll = clearAll;
// Run tests on page load
runTests();
</script>
</body>
</html>