Hone logo
Hone
Problems

In-Memory Filesystem with Jest Testing

This challenge asks you to build a simplified, in-memory filesystem in TypeScript. Creating such a system is a fundamental exercise in data structures and system design, and understanding how to test it thoroughly using Jest is crucial for building robust software. This exercise will help you solidify your understanding of file system concepts and testing methodologies.

Problem Description

You are tasked with creating a basic in-memory filesystem. This filesystem should allow you to:

  1. Create Files: Add new files to the filesystem, storing their content as strings.
  2. Read Files: Retrieve the content of a file by its name.
  3. Update Files: Modify the content of an existing file.
  4. Delete Files: Remove a file from the filesystem.
  5. Check for File Existence: Determine if a file exists in the filesystem.

The filesystem should be implemented as a class named InMemoryFileSystem. It should have the following methods:

  • createFile(filename: string, content: string): void: Creates a new file with the given filename and content.
  • readFile(filename: string): string | undefined: Reads the content of the file. Returns undefined if the file doesn't exist.
  • updateFile(filename: string, content: string): void: Updates the content of the file. Throws an error if the file doesn't exist.
  • deleteFile(filename: string): void: Deletes the file. Throws an error if the file doesn't exist.
  • fileExists(filename: string): boolean: Checks if the file exists.

Expected Behavior:

  • The filesystem should start empty.
  • Creating a file with an existing name should overwrite the previous content.
  • Reading a non-existent file should return undefined.
  • Updating or deleting a non-existent file should throw an error.
  • The filesystem should not persist data between instances. Each InMemoryFileSystem object should have its own isolated state.

Examples

Example 1:

Input:
const fs = new InMemoryFileSystem();
fs.createFile("file1.txt", "Hello");
fs.createFile("file2.txt", "World");

Output:
fs.readFile("file1.txt") // Returns "Hello"
fs.readFile("file2.txt") // Returns "World"
fs.readFile("file3.txt") // Returns undefined

Explanation: Two files are created, and their contents are successfully read. Attempting to read a non-existent file returns undefined.

Example 2:

Input:
const fs = new InMemoryFileSystem();
fs.createFile("file1.txt", "Initial Content");
fs.updateFile("file1.txt", "Updated Content");
fs.readFile("file1.txt")

Output:
"Updated Content"

Explanation: A file is created, updated, and the updated content is successfully read.

Example 3: (Edge Case - File Deletion)

Input:
const fs = new InMemoryFileSystem();
fs.createFile("file1.txt", "Some Content");
fs.deleteFile("file1.txt");
fs.readFile("file1.txt");

Output:
undefined

Explanation: A file is created, then deleted. Attempting to read the deleted file returns undefined.

Constraints

  • Filenames and content are strings.
  • The filesystem should be implemented using a simple data structure (e.g., a JavaScript object or Map) for storing files.
  • Error handling: updateFile and deleteFile must throw an error if the file does not exist. The error message should be descriptive (e.g., "File 'filename' not found").
  • Performance: The operations (create, read, update, delete, exists) should have an average time complexity of O(1).
  • You must provide a Jest test suite with at least 5 tests covering the core functionalities and edge cases.

Notes

  • Consider using a Map for efficient key-value storage of filenames and content.
  • Think about how to handle errors gracefully and provide informative error messages.
  • Focus on writing clean, readable, and well-documented code.
  • The Jest tests should cover various scenarios, including creating, reading, updating, deleting files, and handling non-existent files. Ensure your tests are well-structured and easy to understand.
  • No external libraries are allowed beyond Jest and TypeScript's standard library.
Loading editor...
typescript