|
1 | 1 | /* cSpell:disable */ |
2 | 2 |
|
3 | 3 | import { describe, expect, test } from 'vitest'; |
4 | | -import { encodeFilePath, formatFileName, getFileSize, getGitHash } from '$lib/services/utils/file'; |
| 4 | +import { |
| 5 | + encodeFilePath, |
| 6 | + formatFileName, |
| 7 | + getBlob, |
| 8 | + getFileSize, |
| 9 | + getGitHash, |
| 10 | +} from '$lib/services/utils/file'; |
5 | 11 |
|
6 | 12 | describe('Test encodeFilePath()', () => { |
7 | 13 | test('Encode', () => { |
@@ -137,6 +143,118 @@ describe('Test formatFileName()', () => { |
137 | 143 | }); |
138 | 144 | }); |
139 | 145 |
|
| 146 | +describe('Test getBlob()', () => { |
| 147 | + test('Convert string to Blob', () => { |
| 148 | + // Test with a simple string |
| 149 | + const content = 'hello world'; |
| 150 | + const result = getBlob(content); |
| 151 | + |
| 152 | + expect(result).toBeInstanceOf(Blob); |
| 153 | + expect(result.size).toBe(11); // "hello world" is 11 bytes |
| 154 | + expect(result.type).toBe('text/plain'); |
| 155 | + }); |
| 156 | + |
| 157 | + test('Convert empty string to Blob', () => { |
| 158 | + // Test with empty string |
| 159 | + const result = getBlob(''); |
| 160 | + |
| 161 | + expect(result).toBeInstanceOf(Blob); |
| 162 | + expect(result.size).toBe(0); |
| 163 | + expect(result.type).toBe('text/plain'); |
| 164 | + }); |
| 165 | + |
| 166 | + test('Convert UTF-8 string to Blob', () => { |
| 167 | + // Test with Unicode characters |
| 168 | + const content = '私の画像'; // Japanese characters |
| 169 | + const result = getBlob(content); |
| 170 | + |
| 171 | + expect(result).toBeInstanceOf(Blob); |
| 172 | + expect(result.type).toBe('text/plain'); |
| 173 | + expect(result.size).toBeGreaterThan(content.length); // More bytes than characters |
| 174 | + }); |
| 175 | + |
| 176 | + test('Return File object unchanged', () => { |
| 177 | + // Test with a File object - should return it unchanged |
| 178 | + const content = 'hello world\n'; |
| 179 | + const file = new File([content], 'test.txt', { type: 'text/plain' }); |
| 180 | + const result = getBlob(file); |
| 181 | + |
| 182 | + expect(result).toBe(file); // Should return the same File object |
| 183 | + expect(result).toBeInstanceOf(File); |
| 184 | + expect(result.size).toBe(12); |
| 185 | + expect(result.type).toBe('text/plain'); |
| 186 | + }); |
| 187 | + |
| 188 | + test('Return Blob object unchanged', () => { |
| 189 | + // Test with a Blob object - should return it unchanged |
| 190 | + const content = 'test content'; |
| 191 | + const blob = new Blob([content], { type: 'application/json' }); |
| 192 | + const result = getBlob(blob); |
| 193 | + |
| 194 | + expect(result).toBe(blob); // Should return the same Blob object |
| 195 | + expect(result).toBeInstanceOf(Blob); |
| 196 | + expect(result.size).toBe(12); |
| 197 | + expect(result.type).toBe('application/json'); |
| 198 | + }); |
| 199 | + |
| 200 | + test('Convert JSON string to Blob', () => { |
| 201 | + // Test with JSON content |
| 202 | + const jsonContent = JSON.stringify({ name: 'test', value: 123 }); |
| 203 | + const result = getBlob(jsonContent); |
| 204 | + |
| 205 | + expect(result).toBeInstanceOf(Blob); |
| 206 | + expect(result.type).toBe('text/plain'); |
| 207 | + expect(result.size).toBe(jsonContent.length); |
| 208 | + }); |
| 209 | + |
| 210 | + test('Convert large string to Blob', () => { |
| 211 | + // Test with larger content |
| 212 | + const largeContent = 'a'.repeat(10000); |
| 213 | + const result = getBlob(largeContent); |
| 214 | + |
| 215 | + expect(result).toBeInstanceOf(Blob); |
| 216 | + expect(result.type).toBe('text/plain'); |
| 217 | + expect(result.size).toBe(10000); |
| 218 | + }); |
| 219 | + |
| 220 | + test('Convert string with newlines to Blob', () => { |
| 221 | + // Test with various newline characters |
| 222 | + const content1 = 'line1\nline2'; // Unix newline |
| 223 | + const content2 = 'line1\r\nline2'; // Windows newline |
| 224 | + const content3 = 'line1\rline2'; // Old Mac newline |
| 225 | + const result1 = getBlob(content1); |
| 226 | + const result2 = getBlob(content2); |
| 227 | + const result3 = getBlob(content3); |
| 228 | + |
| 229 | + expect(result1.size).toBe(11); // 11 bytes |
| 230 | + expect(result2.size).toBe(12); // 12 bytes (extra \r) |
| 231 | + expect(result3.size).toBe(11); // 11 bytes |
| 232 | + expect(result1.type).toBe('text/plain'); |
| 233 | + expect(result2.type).toBe('text/plain'); |
| 234 | + expect(result3.type).toBe('text/plain'); |
| 235 | + }); |
| 236 | + |
| 237 | + test('Blob can be read back as text', async () => { |
| 238 | + // Test that the created Blob can be read back as text |
| 239 | + const originalContent = 'hello world test'; |
| 240 | + const blob = getBlob(originalContent); |
| 241 | + const readBackContent = await blob.text(); |
| 242 | + |
| 243 | + expect(readBackContent).toBe(originalContent); |
| 244 | + }); |
| 245 | + |
| 246 | + test('Binary Blob remains unchanged', () => { |
| 247 | + // Test with binary content in a Blob |
| 248 | + const binaryData = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0xff]); |
| 249 | + const binaryBlob = new Blob([binaryData], { type: 'application/octet-stream' }); |
| 250 | + const result = getBlob(binaryBlob); |
| 251 | + |
| 252 | + expect(result).toBe(binaryBlob); // Should return the same Blob object |
| 253 | + expect(result.size).toBe(5); |
| 254 | + expect(result.type).toBe('application/octet-stream'); |
| 255 | + }); |
| 256 | +}); |
| 257 | + |
140 | 258 | describe('Test getFileSize()', () => { |
141 | 259 | test('Get size of string content', () => { |
142 | 260 | // Test with a simple string |
|
0 commit comments