Jest Artifact Storage: Mocking File System Interactions
This challenge focuses on creating a robust and testable way to manage and verify file system interactions within your Jest tests. Often, tests need to read or write files, but directly interacting with the real file system can make tests slow, unreliable (due to external dependencies), and difficult to set up. This problem asks you to create a mock artifact storage system that allows you to simulate file operations within your Jest tests, enabling you to verify that your code interacts with the file system as expected.
Problem Description
You need to implement a MockArtifactStorage class in TypeScript. This class will simulate a simple file system for storing and retrieving "artifacts" (represented as strings). The class should provide the following methods:
storeArtifact(key: string, artifact: string): void: Stores an artifact associated with a given key.getArtifact(key: string): string | undefined: Retrieves the artifact associated with a given key. Returnsundefinedif the key doesn't exist.deleteArtifact(key: string): void: Removes the artifact associated with a given key.getAllArtifacts(): { [key: string]: string }: Returns a dictionary containing all stored artifacts (key-value pairs).
The MockArtifactStorage class should be designed to be easily mockable in Jest tests. This means you should be able to easily inspect its internal state (e.g., the stored artifacts) to verify that your code has interacted with it correctly. The internal storage should be a simple JavaScript object.
Key Requirements:
- The class must be written in TypeScript.
- The methods must behave as described above.
- The class should be designed for testability, allowing easy inspection of its internal state.
- Error handling is not required (e.g., no need to throw errors if a key doesn't exist).
Expected Behavior:
storeArtifactshould add a new key-value pair to the internal storage.getArtifactshould return the artifact associated with the key if it exists, andundefinedotherwise.deleteArtifactshould remove the key-value pair associated with the key.getAllArtifactsshould return a copy of the internal storage.
Edge Cases to Consider:
- Storing multiple artifacts with the same key (the last stored artifact should overwrite previous ones).
- Retrieving an artifact for a key that doesn't exist.
- Deleting an artifact for a key that doesn't exist (should be a no-op).
getAllArtifactsshould return a copy of the internal storage, not the original object, to prevent external modification of the storage.
Examples
Example 1:
Input:
- `mockStorage.storeArtifact('artifact1', 'data1');`
- `mockStorage.storeArtifact('artifact2', 'data2');`
- `mockStorage.getArtifact('artifact1');`
Output:
`'data1'`
Explanation: The first artifact is stored with key 'artifact1' and value 'data1'. Then, 'artifact2' is stored with 'data2'. Retrieving 'artifact1' returns 'data1'.
Example 2:
Input:
- `mockStorage.storeArtifact('artifact1', 'data1');`
- `mockStorage.storeArtifact('artifact1', 'data3');`
- `mockStorage.getArtifact('artifact1');`
Output:
`'data3'`
Explanation: The first artifact is stored with key 'artifact1' and value 'data1'. Then, 'artifact1' is overwritten with 'data3'. Retrieving 'artifact1' returns 'data3'.
Example 3:
Input:
- `mockStorage.storeArtifact('artifact1', 'data1');`
- `mockStorage.deleteArtifact('artifact1');`
- `mockStorage.getArtifact('artifact1');`
Output:
`undefined`
Explanation: An artifact is stored with key 'artifact1' and value 'data1'. Then, the artifact is deleted. Retrieving 'artifact1' returns `undefined`.
Constraints
- The internal storage must be a simple JavaScript object (
{ [key: string]: string }). - The
getAllArtifactsmethod must return a copy of the internal storage object. Do not return a reference to the original object. - The code must be written in TypeScript.
- No external libraries are allowed beyond those included with a standard TypeScript/Jest setup.
Notes
- Consider using the spread operator (
...) to create a copy of the internal storage when implementinggetAllArtifacts. - Think about how you can design the class to make it easy to inspect its internal state in your Jest tests. Directly exposing the internal storage might be a good approach.
- This problem is designed to test your understanding of TypeScript classes, object manipulation, and testability principles.