From b0f6d388017e73ca58054636b008d75f4e96973a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 3 Oct 2025 22:19:17 -0500 Subject: [PATCH] Make JSRuntimeFS accept options object --- eslint.config.js | 2 +- src/lib/CapacitorFSAdapter.test.ts | 29 +- src/lib/CapacitorFSAdapter.ts | 262 +++++++++--------- src/lib/JSRuntime.ts | 10 +- src/lib/LightningFSAdapter.ts | 14 +- src/lib/ProjectsManager.test.ts | 10 +- src/lib/fileUtils.test.ts | 12 +- src/lib/tmpCleanup.test.ts | 12 +- .../tools/TextEditorStrReplaceTool.test.ts | 13 +- src/lib/tools/TextEditorViewTool.test.ts | 13 +- src/lib/tools/TextEditorWriteTool.test.ts | 13 +- 11 files changed, 219 insertions(+), 171 deletions(-) diff --git a/eslint.config.js b/eslint.config.js index 5ed852b0..647f40e3 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -8,7 +8,7 @@ import htmlParser from "@html-eslint/parser"; import customRules from "./eslint-rules/index.js"; export default tseslint.config( - { ignores: ["dist", "android/app/build/**"] }, + { ignores: ["dist", "android/app/build/**", "android/app/src/main/assets/**"] }, { extends: [js.configs.recommended, ...tseslint.configs.recommended], files: ["**/*.{ts,tsx}"], diff --git a/src/lib/CapacitorFSAdapter.test.ts b/src/lib/CapacitorFSAdapter.test.ts index 7e466b1c..6b314794 100644 --- a/src/lib/CapacitorFSAdapter.test.ts +++ b/src/lib/CapacitorFSAdapter.test.ts @@ -47,15 +47,19 @@ describe('CapacitorFSAdapter', () => { }); it('should read binary file and convert from base64', async () => { - // 'hello' in base64 + // Binary data returned as base64 when no encoding specified vi.mocked(Filesystem.Filesystem.readFile).mockResolvedValue({ - data: 'aGVsbG8=', + data: 'aGVsbG8=', // 'hello' in base64 }); const result = await adapter.readFile('/test.bin'); expect(result).toBeInstanceOf(Uint8Array); expect(Array.from(result as Uint8Array)).toEqual([104, 101, 108, 108, 111]); + expect(Filesystem.Filesystem.readFile).toHaveBeenCalledWith({ + path: 'test.bin', + directory: 'DATA', + }); }); it('should throw error for non-existent file', async () => { @@ -78,11 +82,10 @@ describe('CapacitorFSAdapter', () => { directory: 'DATA', data: 'hello world', encoding: 'utf8', - recursive: true, }); }); - it('should write binary file and convert to base64', async () => { + it('should write binary file as base64', async () => { vi.mocked(Filesystem.Filesystem.writeFile).mockResolvedValue({ uri: '' }); const data = new Uint8Array([104, 101, 108, 108, 111]); // 'hello' @@ -92,8 +95,19 @@ describe('CapacitorFSAdapter', () => { path: 'test.bin', directory: 'DATA', data: 'aGVsbG8=', // base64 encoded - encoding: undefined, - recursive: true, + }); + }); + + it('should normalize paths and remove trailing slashes', async () => { + vi.mocked(Filesystem.Filesystem.writeFile).mockResolvedValue({ uri: '' }); + + const data = new Uint8Array([104, 101, 108, 108, 111]); + await adapter.writeFile('/test.bin/', data); // Note the trailing slash + + expect(Filesystem.Filesystem.writeFile).toHaveBeenCalledWith({ + path: 'test.bin', // Trailing slash should be removed + directory: 'DATA', + data: 'aGVsbG8=', // base64 encoded }); }); }); @@ -264,7 +278,7 @@ describe('CapacitorFSAdapter', () => { describe('symlink operations', () => { it('should create symlink by copying file', async () => { vi.mocked(Filesystem.Filesystem.readFile).mockResolvedValue({ - data: 'aGVsbG8=', // 'hello' in base64 + data: 'aGVsbG8=', // base64 }); vi.mocked(Filesystem.Filesystem.writeFile).mockResolvedValue({ uri: '' }); @@ -273,7 +287,6 @@ describe('CapacitorFSAdapter', () => { expect(Filesystem.Filesystem.readFile).toHaveBeenCalledWith({ path: 'target.txt', directory: 'DATA', - encoding: undefined, }); expect(Filesystem.Filesystem.writeFile).toHaveBeenCalledTimes(2); diff --git a/src/lib/CapacitorFSAdapter.ts b/src/lib/CapacitorFSAdapter.ts index 87335660..13c9f6b0 100644 --- a/src/lib/CapacitorFSAdapter.ts +++ b/src/lib/CapacitorFSAdapter.ts @@ -1,12 +1,14 @@ /** * Capacitor Filesystem Adapter - * + * * Adapts Capacitor's Filesystem plugin to work with the JSRuntimeFS interface. * This enables native filesystem storage on Android/iOS while maintaining * compatibility with existing code. */ import { Filesystem, Directory, Encoding } from '@capacitor/filesystem'; +import { encodeBase64, decodeBase64 } from '@std/encoding/base64'; +import path from 'path-browserify'; import type { JSRuntimeFS, DirectoryEntry } from './JSRuntime'; export class CapacitorFSAdapter implements JSRuntimeFS { @@ -20,115 +22,102 @@ export class CapacitorFSAdapter implements JSRuntimeFS { * Convert absolute paths to relative paths for Capacitor * Capacitor expects paths relative to the base directory */ - private normalizePath(path: string): string { + private normalizePath(inputPath: string): string { + // Normalize the path using path-browserify + let normalized = path.normalize(inputPath); + // Remove leading slash for Capacitor - let normalized = path.startsWith('/') ? path.slice(1) : path; - + normalized = normalized.startsWith('/') ? normalized.slice(1) : normalized; + + // Remove ALL trailing slashes (important for file operations) + // Capacitor treats paths with trailing slashes as directories + // Use regex to remove all trailing slashes + normalized = normalized.replace(/\/+$/, ''); + // Ensure we don't have empty path - if (normalized === '') { + if (normalized === '' || normalized === '.') { normalized = '.'; } - + return normalized; } - /** - * Convert base64 string to Uint8Array - */ - private base64ToUint8Array(base64: string): Uint8Array { - const binaryString = atob(base64); - const bytes = new Uint8Array(binaryString.length); - for (let i = 0; i < binaryString.length; i++) { - bytes[i] = binaryString.charCodeAt(i); - } - return bytes; - } - - /** - * Convert Uint8Array to base64 string - */ - private uint8ArrayToBase64(bytes: Uint8Array): string { - let binary = ''; - for (let i = 0; i < bytes.length; i++) { - binary += String.fromCharCode(bytes[i]); - } - return btoa(binary); - } - /** * Read file as Uint8Array or string */ async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise; - async readFile(path: string, encoding?: string): Promise { - try { + async readFile(path: string, options: 'utf8'): Promise; + async readFile(path: string, options: string): Promise; + async readFile(path: string, options: { encoding: 'utf8' }): Promise; + async readFile(path: string, options: { encoding: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise { + const normalizedPath = this.normalizePath(path); + const encoding = typeof options === 'string' ? options : options?.encoding; + + if (encoding === 'utf8') { + // Read as UTF-8 text const result = await Filesystem.readFile({ - path: this.normalizePath(path), + path: normalizedPath, directory: this.baseDirectory, - encoding: encoding === 'utf8' ? Encoding.UTF8 : undefined, + encoding: Encoding.UTF8, }); - - if (encoding === 'utf8') { - return result.data as string; + if (result.data instanceof Blob) { + return await result.data.text(); + } else { + return result.data; + } + } else { + // Read as base64 (no encoding specified) + const result = await Filesystem.readFile({ + path: normalizedPath, + directory: this.baseDirectory, + }); + if (result.data instanceof Blob) { + const base64 = await result.data.text(); + return decodeBase64(base64); + } else { + return decodeBase64(result.data); } - - // Binary data is returned as base64, convert to Uint8Array - return this.base64ToUint8Array(result.data as string); - } catch (error) { - // Convert Capacitor errors to standard Node.js-style errors - const err = error as { message: string }; - throw new Error(`ENOENT: no such file or directory, open '${path}': ${err.message}`); } } /** * Write file from string or Uint8Array */ - async writeFile(path: string, data: string | Uint8Array, encoding?: string): Promise { - try { - // Ensure parent directory exists - const parentPath = path.split('/').slice(0, -1).join('/'); - if (parentPath && parentPath !== '.') { - await this.mkdir(parentPath, { recursive: true }); - } - - let writeData: string; - let writeEncoding: Encoding | undefined; - - if (data instanceof Uint8Array) { - // Convert binary data to base64 for Capacitor - writeData = this.uint8ArrayToBase64(data); - writeEncoding = undefined; // Base64 is the default - } else { - writeData = data; - writeEncoding = encoding === 'utf8' ? Encoding.UTF8 : undefined; - } + async writeFile(filePath: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise { + const normalizedPath = this.normalizePath(filePath); + if (data instanceof Uint8Array) { + // Write binary data as base64 (no encoding specified) + const base64Data = encodeBase64(data); await Filesystem.writeFile({ - path: this.normalizePath(path), + path: normalizedPath, directory: this.baseDirectory, - data: writeData, - encoding: writeEncoding, - recursive: true, // Create parent directories + data: base64Data, + }); + } else { + // Write string data as UTF-8 + await Filesystem.writeFile({ + path: normalizedPath, + directory: this.baseDirectory, + data: data, + encoding: Encoding.UTF8, }); - } catch (error) { - const err = error as { message: string }; - throw new Error(`Failed to write file '${path}': ${err.message}`); } } /** * Read directory contents */ - async readdir(path: string): Promise; - async readdir(path: string, options: { withFileTypes: true }): Promise; - async readdir(path: string, options?: { withFileTypes?: boolean }): Promise; - async readdir(path: string, options?: { withFileTypes?: boolean }): Promise { + async readdir(dirPath: string): Promise; + async readdir(dirPath: string, options: { withFileTypes: true }): Promise; + async readdir(dirPath: string, options?: { withFileTypes?: boolean }): Promise; + async readdir(dirPath: string, options?: { withFileTypes?: boolean }): Promise { try { + const normalizedPath = this.normalizePath(dirPath); const result = await Filesystem.readdir({ - path: this.normalizePath(path), + path: normalizedPath, directory: this.baseDirectory, }); @@ -137,8 +126,8 @@ export class CapacitorFSAdapter implements JSRuntimeFS { const entries: DirectoryEntry[] = []; for (const file of result.files) { - const fullPath = path === '/' ? `/${file.name}` : `${path}/${file.name}`; - + const fullPath = path.join(dirPath, file.name); + try { const stat = await this.stat(fullPath); entries.push({ @@ -160,9 +149,13 @@ export class CapacitorFSAdapter implements JSRuntimeFS { } return result.files.map(f => f.name); - } catch (error) { - const err = error as { message: string }; - throw new Error(`ENOENT: no such file or directory, scandir '${path}': ${err.message}`); + } catch { + const nodeError = new Error(`ENOENT: no such file or directory, scandir '${dirPath}'`) as NodeJS.ErrnoException; + nodeError.code = 'ENOENT'; + nodeError.errno = -2; + nodeError.syscall = 'scandir'; + nodeError.path = dirPath; + throw nodeError; } } @@ -178,9 +171,8 @@ export class CapacitorFSAdapter implements JSRuntimeFS { }); } catch (error) { // Ignore "directory exists" errors when recursive is true - if (options?.recursive) { - const err = error as { message: string }; - if (err.message.includes('exists') || err.message.includes('EEXIST')) { + if (options?.recursive && error instanceof Error) { + if (error.message.includes('exists') || error.message.includes('EEXIST')) { return; } } @@ -198,8 +190,9 @@ export class CapacitorFSAdapter implements JSRuntimeFS { mtimeMs?: number; }> { try { + const normalizedPath = this.normalizePath(path); const result = await Filesystem.stat({ - path: this.normalizePath(path), + path: normalizedPath, directory: this.baseDirectory, }); @@ -209,9 +202,14 @@ export class CapacitorFSAdapter implements JSRuntimeFS { size: result.size, mtimeMs: result.mtime, }; - } catch (error) { - const err = error as { message: string }; - throw new Error(`ENOENT: no such file or directory, stat '${path}': ${err.message}`); + } catch { + // Create a proper Node.js-style error + const nodeError = new Error(`ENOENT: no such file or directory, stat '${path}'`) as NodeJS.ErrnoException; + nodeError.code = 'ENOENT'; + nodeError.errno = -2; + nodeError.syscall = 'stat'; + nodeError.path = path; + throw nodeError; } } @@ -224,8 +222,28 @@ export class CapacitorFSAdapter implements JSRuntimeFS { size?: number; mtimeMs?: number; }> { - // Capacitor doesn't distinguish between stat and lstat - return this.stat(path); + try { + const normalizedPath = this.normalizePath(path); + const result = await Filesystem.stat({ + path: normalizedPath, + directory: this.baseDirectory, + }); + + return { + isDirectory: () => result.type === 'directory', + isFile: () => result.type === 'file', + size: result.size, + mtimeMs: result.mtime, + }; + } catch { + // Create a proper Node.js-style error + const nodeError = new Error(`ENOENT: no such file or directory, lstat '${path}'`) as NodeJS.ErrnoException; + nodeError.code = 'ENOENT'; + nodeError.errno = -2; + nodeError.syscall = 'lstat'; + nodeError.path = path; + throw nodeError; + } } /** @@ -233,13 +251,18 @@ export class CapacitorFSAdapter implements JSRuntimeFS { */ async unlink(path: string): Promise { try { + const normalizedPath = this.normalizePath(path); await Filesystem.deleteFile({ - path: this.normalizePath(path), + path: normalizedPath, directory: this.baseDirectory, }); - } catch (error) { - const err = error as { message: string }; - throw new Error(`ENOENT: no such file or directory, unlink '${path}': ${err.message}`); + } catch { + const nodeError = new Error(`ENOENT: no such file or directory, unlink '${path}'`) as NodeJS.ErrnoException; + nodeError.code = 'ENOENT'; + nodeError.errno = -2; + nodeError.syscall = 'unlink'; + nodeError.path = path; + throw nodeError; } } @@ -247,32 +270,22 @@ export class CapacitorFSAdapter implements JSRuntimeFS { * Remove directory */ async rmdir(path: string): Promise { - try { - await Filesystem.rmdir({ - path: this.normalizePath(path), - directory: this.baseDirectory, - recursive: false, // Only remove if empty - }); - } catch (error) { - const err = error as { message: string }; - throw new Error(`Failed to remove directory '${path}': ${err.message}`); - } + await Filesystem.rmdir({ + path: this.normalizePath(path), + directory: this.baseDirectory, + recursive: false, // Only remove if empty + }); } /** * Rename/move file or directory */ async rename(oldPath: string, newPath: string): Promise { - try { - await Filesystem.rename({ - from: this.normalizePath(oldPath), - to: this.normalizePath(newPath), - directory: this.baseDirectory, - }); - } catch (error) { - const err = error as { message: string }; - throw new Error(`Failed to rename '${oldPath}' to '${newPath}': ${err.message}`); - } + await Filesystem.rename({ + from: this.normalizePath(oldPath), + to: this.normalizePath(newPath), + directory: this.baseDirectory, + }); } /** @@ -297,17 +310,12 @@ export class CapacitorFSAdapter implements JSRuntimeFS { * by copying the file instead and storing symlink metadata */ async symlink(target: string, path: string): Promise { - try { - // Copy the target file to the symlink location - const data = await this.readFile(target); - await this.writeFile(path, data); - - // Store symlink metadata for readlink - const symlinkPath = `${path}.symlink`; - await this.writeFile(symlinkPath, target, 'utf8'); - } catch (error) { - const err = error as { message: string }; - throw new Error(`Failed to create symlink '${target}' -> '${path}': ${err.message}`); - } + // Copy the target file to the symlink location + const data = await this.readFile(target); + await this.writeFile(path, data); + + // Store symlink metadata for readlink + const symlinkPath = `${path}.symlink`; + await this.writeFile(symlinkPath, target); } } diff --git a/src/lib/JSRuntime.ts b/src/lib/JSRuntime.ts index 349d789c..54aefebd 100644 --- a/src/lib/JSRuntime.ts +++ b/src/lib/JSRuntime.ts @@ -5,11 +5,13 @@ export interface JSRuntimeFS { // Core file operations with proper overloads for type safety readFile(path: string): Promise; - readFile(path: string, encoding: 'utf8'): Promise; - readFile(path: string, encoding: string): Promise; - readFile(path: string, encoding?: string): Promise; + readFile(path: string, options: 'utf8'): Promise; + readFile(path: string, options: string): Promise; + readFile(path: string, options: { encoding: 'utf8' }): Promise; + readFile(path: string, options: { encoding: string }): Promise; + readFile(path: string, options?: string | { encoding?: string }): Promise; - writeFile(path: string, data: string | Uint8Array, encoding?: string): Promise; + writeFile(path: string, data: string | Uint8Array, options?: string | { encoding?: string }): Promise; // readdir overloads for different return types readdir(path: string): Promise; diff --git a/src/lib/LightningFSAdapter.ts b/src/lib/LightningFSAdapter.ts index 77dfedfb..413944b6 100644 --- a/src/lib/LightningFSAdapter.ts +++ b/src/lib/LightningFSAdapter.ts @@ -9,14 +9,18 @@ export class LightningFSAdapter implements JSRuntimeFS { constructor(private lightningFS: LightningFS.PromisifiedFS) {} async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise; - async readFile(path: string, encoding?: string): Promise { + async readFile(path: string, options: 'utf8'): Promise; + async readFile(path: string, options: string): Promise; + async readFile(path: string, options: { encoding: 'utf8' }): Promise; + async readFile(path: string, options: { encoding: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise { + const encoding = typeof options === 'string' ? options : options?.encoding; return this.lightningFS.readFile(path, encoding); } - async writeFile(path: string, data: string | Uint8Array, encoding?: string): Promise { + async writeFile(path: string, data: string | Uint8Array, options?: string | { encoding?: string }): Promise { + const encoding = typeof options === 'string' ? options : options?.encoding; return this.lightningFS.writeFile(path, data, encoding); } diff --git a/src/lib/ProjectsManager.test.ts b/src/lib/ProjectsManager.test.ts index 5e43cf2a..22b59ea6 100644 --- a/src/lib/ProjectsManager.test.ts +++ b/src/lib/ProjectsManager.test.ts @@ -41,15 +41,19 @@ class MockFS implements JSRuntimeFS { private dirs: Set = new Set(); async readFile(path: string): Promise; - async readFile(path: string, encoding: "utf8"): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise { + async readFile(path: string, options: 'utf8'): Promise; + async readFile(path: string, options: string): Promise; + async readFile(path: string, options: { encoding: 'utf8' }): Promise; + async readFile(path: string, options: { encoding: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise { const content = this.files.get(path); if (content === undefined) { const error = new Error(`ENOENT: no such file or directory, open '${path}'`) as NodeJS.ErrnoException; error.code = 'ENOENT'; throw error; } + const encoding = typeof options === 'string' ? options : options?.encoding; if (encoding === 'utf8') { if (typeof content === 'string') { return content; diff --git a/src/lib/fileUtils.test.ts b/src/lib/fileUtils.test.ts index e75495e1..801fe30d 100644 --- a/src/lib/fileUtils.test.ts +++ b/src/lib/fileUtils.test.ts @@ -13,12 +13,16 @@ class MockFS implements JSRuntimeFS { } async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise { + async readFile(path: string, options: 'utf8'): Promise; + async readFile(path: string, options: string): Promise; + async readFile(path: string, options: { encoding: 'utf8' }): Promise; + async readFile(path: string, options: { encoding: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise { const data = this.files.get(path); if (!data) throw new Error(`File not found: ${path}`); + const encoding = typeof options === 'string' ? options : options?.encoding; if (encoding === 'utf8') { return typeof data === 'string' ? data : new TextDecoder().decode(data); } @@ -26,7 +30,7 @@ class MockFS implements JSRuntimeFS { return data; } - async writeFile(path: string, data: string | Uint8Array): Promise { + async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise { this.files.set(path, data); } diff --git a/src/lib/tmpCleanup.test.ts b/src/lib/tmpCleanup.test.ts index 9ba27c96..eabf2f42 100644 --- a/src/lib/tmpCleanup.test.ts +++ b/src/lib/tmpCleanup.test.ts @@ -26,15 +26,19 @@ class MockFS implements JSRuntimeFS { } async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise { + async readFile(path: string, options: 'utf8'): Promise; + async readFile(path: string, options: string): Promise; + async readFile(path: string, options: { encoding: 'utf8' }): Promise; + async readFile(path: string, options: { encoding: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise { const file = this.files.get(path); if (!file || file.isDir) throw new Error(`File not found: ${path}`); + const encoding = typeof options === 'string' ? options : options?.encoding; return encoding === 'utf8' ? file.content.toString() : new Uint8Array(); } - async writeFile(path: string, data: string | Uint8Array): Promise { + async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise { this.files.set(path, { content: data, mtimeMs: Date.now(), isDir: false }); } diff --git a/src/lib/tools/TextEditorStrReplaceTool.test.ts b/src/lib/tools/TextEditorStrReplaceTool.test.ts index 3b7df782..b439282d 100644 --- a/src/lib/tools/TextEditorStrReplaceTool.test.ts +++ b/src/lib/tools/TextEditorStrReplaceTool.test.ts @@ -14,18 +14,21 @@ class MockFS implements JSRuntimeFS { } async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise; - async readFile(path: string, encoding?: string): Promise { + async readFile(path: string, options: 'utf8'): Promise; + async readFile(path: string, options: string): Promise; + async readFile(path: string, options: { encoding: 'utf8' }): Promise; + async readFile(path: string, options: { encoding: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise { const content = this.files.get(path); if (content === undefined) { throw new Error(`File not found: ${path}`); } + const encoding = typeof options === 'string' ? options : options?.encoding; return encoding ? content : new TextEncoder().encode(content); } - async writeFile(path: string, data: string | Uint8Array, _encoding?: string): Promise { + async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise { const content = typeof data === 'string' ? data : new TextDecoder().decode(data); this.files.set(path, content); } diff --git a/src/lib/tools/TextEditorViewTool.test.ts b/src/lib/tools/TextEditorViewTool.test.ts index 03e81b73..eddabc93 100644 --- a/src/lib/tools/TextEditorViewTool.test.ts +++ b/src/lib/tools/TextEditorViewTool.test.ts @@ -41,15 +41,18 @@ class MockFS implements JSRuntimeFS { } async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise; - async readFile(path: string, encoding?: string): Promise { + async readFile(path: string, options: 'utf8'): Promise; + async readFile(path: string, options: string): Promise; + async readFile(path: string, options: { encoding: 'utf8' }): Promise; + async readFile(path: string, options: { encoding: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise { const content = this.files.get(path); if (content === undefined) { throw new Error(`File not found: ${path}`); } + const encoding = typeof options === 'string' ? options : options?.encoding; // If no encoding specified, return Uint8Array if (!encoding) { // For binary files, return mock binary data with null bytes @@ -70,7 +73,7 @@ class MockFS implements JSRuntimeFS { return content; } - async writeFile(path: string, data: string | Uint8Array, _encoding?: string): Promise { + async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise { const content = typeof data === 'string' ? data : new TextDecoder().decode(data); this.files.set(path, content); } diff --git a/src/lib/tools/TextEditorWriteTool.test.ts b/src/lib/tools/TextEditorWriteTool.test.ts index 34c1ffc9..17169b4f 100644 --- a/src/lib/tools/TextEditorWriteTool.test.ts +++ b/src/lib/tools/TextEditorWriteTool.test.ts @@ -13,18 +13,21 @@ class MockFS implements JSRuntimeFS { } async readFile(path: string): Promise; - async readFile(path: string, encoding: 'utf8'): Promise; - async readFile(path: string, encoding: string): Promise; - async readFile(path: string, encoding?: string): Promise; - async readFile(path: string, encoding?: string): Promise { + async readFile(path: string, options: 'utf8'): Promise; + async readFile(path: string, options: string): Promise; + async readFile(path: string, options: { encoding: 'utf8' }): Promise; + async readFile(path: string, options: { encoding: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise; + async readFile(path: string, options?: string | { encoding?: string }): Promise { const content = this.files.get(path); if (content === undefined) { throw new Error(`File not found: ${path}`); } + const encoding = typeof options === 'string' ? options : options?.encoding; return encoding ? content : new TextEncoder().encode(content); } - async writeFile(path: string, data: string | Uint8Array, _encoding?: string): Promise { + async writeFile(path: string, data: string | Uint8Array, _options?: string | { encoding?: string }): Promise { const content = typeof data === 'string' ? data : new TextDecoder().decode(data); this.files.set(path, content); }