|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 8 | +import { readFileWithEncoding } from './fileUtils.js'; |
| 9 | +import { StandardFileSystemService } from '../services/fileSystemService.js'; |
| 10 | +import fs from 'node:fs'; |
| 11 | +import path from 'node:path'; |
| 12 | +import os from 'node:os'; |
| 13 | + |
| 14 | +describe('ISO-8859-1 Encoding Support', () => { |
| 15 | + let tmpDir: string; |
| 16 | + let filePath: string; |
| 17 | + const fileService = new StandardFileSystemService(); |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gemini-iso-test-')); |
| 21 | + filePath = path.join(tmpDir, 'test.txt'); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should read ISO-8859-1 file correctly', async () => { |
| 29 | + // "café na manhã" in ISO-8859-1 |
| 30 | + // c a f é _ n a _ m a n h ã |
| 31 | + // 63 61 66 e9 20 6e 61 20 6d 61 6e 68 e3 |
| 32 | + const buffer = Buffer.from([ |
| 33 | + 0x63, 0x61, 0x66, 0xe9, 0x20, 0x6e, 0x61, 0x20, 0x6d, 0x61, 0x6e, 0x68, |
| 34 | + 0xe3, |
| 35 | + ]); |
| 36 | + fs.writeFileSync(filePath, buffer); |
| 37 | + |
| 38 | + const content = await readFileWithEncoding(filePath); |
| 39 | + expect(content).toBe('café na manhã'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should preserve ISO-8859-1 encoding when writing', async () => { |
| 43 | + // "café na manhã" |
| 44 | + const buffer = Buffer.from([ |
| 45 | + 0x63, 0x61, 0x66, 0xe9, 0x20, 0x6e, 0x61, 0x20, 0x6d, 0x61, 0x6e, 0x68, |
| 46 | + 0xe3, |
| 47 | + ]); |
| 48 | + fs.writeFileSync(filePath, buffer); |
| 49 | + |
| 50 | + // Verify initial read |
| 51 | + const content = await readFileWithEncoding(filePath); |
| 52 | + expect(content).toBe('café na manhã'); |
| 53 | + |
| 54 | + // Write new content "café na manhã updated" |
| 55 | + await fileService.writeTextFile(filePath, 'café na manhã updated'); |
| 56 | + |
| 57 | + // Read back as buffer to check encoding |
| 58 | + const newBuffer = fs.readFileSync(filePath); |
| 59 | + |
| 60 | + // Expect "café na manhã updated" in ISO-8859-1 |
| 61 | + // Original: 13 bytes. " updated": 8 bytes. Total 21 bytes. |
| 62 | + expect(newBuffer.length).toBe(21); |
| 63 | + |
| 64 | + const str = newBuffer.toString('latin1'); |
| 65 | + expect(str).toBe('café na manhã updated'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should fallback to UTF-8 for new files', async () => { |
| 69 | + const newFilePath = path.join(tmpDir, 'new.txt'); |
| 70 | + await fileService.writeTextFile(newFilePath, 'café'); |
| 71 | + |
| 72 | + const buffer = fs.readFileSync(newFilePath); |
| 73 | + // UTF-8 for é is C3 A9. Total 5 bytes. |
| 74 | + expect(buffer.length).toBe(5); |
| 75 | + expect(buffer.includes(0xc3)).toBe(true); |
| 76 | + }); |
| 77 | +}); |
0 commit comments