Move MockFS to a separate file (with its own tests)

This commit is contained in:
Alex Gleason committed 2025-12-25 22:06:19 -06:00
1 parent 9987cfca6d
commit 4e68ee57d6
3 files changed
+568 -202

No files matched your search

+2 -202
View File
@@ -1,210 +1,10 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { Git } from './git';
import type { JSRuntimeFS, DirectoryEntry } from './JSRuntime';
import type { JSRuntimeFS } from './JSRuntime';
import type { NPool, NostrEvent } from '@nostrify/nostrify';
import { NSecSigner } from '@nostrify/nostrify';
import { nip19 } from 'nostr-tools';
/**
* Minimal mock filesystem implementation for testing
* Implements only the essential methods needed by isomorphic-git
*/
class MockFS implements JSRuntimeFS {
private files: Map<string, string | Uint8Array> = new Map();
private dirs: Set<string> = new Set(['/']);
// Expose promises property for isomorphic-git compatibility
promises = this;
async readFile(path: string): Promise<Uint8Array>;
async readFile(path: string, options: 'utf8'): Promise<string>;
async readFile(path: string, options: string): Promise<string>;
async readFile(path: string, options: { encoding: 'utf8' }): Promise<string>;
async readFile(path: string, options: { encoding: string }): Promise<string>;
async readFile(path: string, options?: string | { encoding?: string }): Promise<string | Uint8Array>;
async readFile(path: string, options?: string | { encoding?: string }): Promise<string | Uint8Array> {
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;
return new TextDecoder('utf-8').decode(content as Uint8Array);
}
if (typeof content === 'string') return new TextEncoder().encode(content);
return content as Uint8Array;
}
async writeFile(path: string, data: string | Uint8Array): Promise<void> {
this.files.set(path, data);
const dir = path.split('/').slice(0, -1).join('/') || '/';
this.dirs.add(dir);
}
readdir(path: string): Promise<string[]>;
readdir(path: string, options: { withFileTypes: true }): Promise<DirectoryEntry[]>;
readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]>;
async readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]> {
if (!this.dirs.has(path)) {
const error = new Error(`ENOENT: no such file or directory, scandir '${path}'`) as NodeJS.ErrnoException;
error.code = 'ENOENT';
throw error;
}
const entries: string[] = [];
for (const dir of this.dirs) {
if (dir.startsWith(path + '/') && !dir.slice(path.length + 1).includes('/')) {
entries.push(dir.slice(path.length + 1));
}
}
for (const file of this.files.keys()) {
if (file.startsWith(path + '/') && !file.slice(path.length + 1).includes('/')) {
entries.push(file.slice(path.length + 1));
}
}
const uniqueEntries = [...new Set(entries)];
if (options?.withFileTypes) {
return uniqueEntries.map(name => {
const fullPath = path.endsWith('/') ? path + name : path + '/' + name;
const isDir = this.dirs.has(fullPath);
return {
name,
isDirectory: () => isDir,
isFile: () => !isDir,
isSymbolicLink: () => false,
} as unknown as DirectoryEntry;
});
}
return uniqueEntries;
}
async mkdir(path: string): Promise<void> {
this.dirs.add(path);
}
async stat(path: string): Promise<{
isDirectory(): boolean;
isFile(): boolean;
isSymbolicLink(): boolean;
mtimeMs?: number;
mtime?: Date;
ctimeMs?: number;
ctime?: Date;
mode?: number;
size?: number;
ino?: number;
uid?: number;
gid?: number;
}> {
const now = new Date();
const nowMs = now.getTime();
if (this.dirs.has(path)) {
return {
isDirectory: () => true,
isFile: () => false,
isSymbolicLink: () => false,
mtimeMs: nowMs,
mtime: now,
ctimeMs: nowMs,
ctime: now,
mode: 0o40755,
size: 0,
ino: 1,
uid: 1000,
gid: 1000,
};
}
if (this.files.has(path)) {
const content = this.files.get(path)!;
const size = typeof content === 'string' ? content.length : content.byteLength;
return {
isDirectory: () => false,
isFile: () => true,
isSymbolicLink: () => false,
mtimeMs: nowMs,
mtime: now,
ctimeMs: nowMs,
ctime: now,
mode: 0o100644,
size,
ino: 1,
uid: 1000,
gid: 1000,
};
}
const error = new Error(`ENOENT: no such file or directory, stat '${path}'`) as NodeJS.ErrnoException;
error.code = 'ENOENT';
throw error;
}
async lstat(path: string): Promise<{
isDirectory(): boolean;
isFile(): boolean;
isSymbolicLink(): boolean;
mtimeMs?: number;
mtime?: Date;
ctimeMs?: number;
ctime?: Date;
mode?: number;
size?: number;
ino?: number;
uid?: number;
gid?: number;
}> {
return this.stat(path);
}
async readlink(_path: string): Promise<string> {
const error = new Error('EINVAL: invalid argument') as NodeJS.ErrnoException;
error.code = 'EINVAL';
throw error;
}
async symlink(_target: string, _path: string): Promise<void> {
// No-op for mock
}
async unlink(path: string): Promise<void> {
this.files.delete(path);
}
async rmdir(path: string): Promise<void> {
this.dirs.delete(path);
}
async rename(oldPath: string, newPath: string): Promise<void> {
if (this.files.has(oldPath)) {
const content = this.files.get(oldPath);
this.files.delete(oldPath);
this.files.set(newPath, content!);
}
if (this.dirs.has(oldPath)) {
this.dirs.delete(oldPath);
this.dirs.add(newPath);
}
}
async chmod(_path: string, _mode: number): Promise<void> {
// No-op for mock
}
async rm(path: string, options?: { recursive?: boolean }): Promise<void> {
if (options?.recursive) {
for (const file of [...this.files.keys()]) {
if (file.startsWith(path + '/') || file === path) this.files.delete(file);
}
for (const dir of [...this.dirs]) {
if (dir.startsWith(path + '/') || dir === path) this.dirs.delete(dir);
}
} else {
await this.unlink(path);
}
}
}
import { MockFS } from '@/test/MockFS';
/**
* Mock Nostr pool
+355
View File
@@ -0,0 +1,355 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { MockFS } from './MockFS';
describe('MockFS', () => {
let fs: MockFS;
beforeEach(() => {
fs = new MockFS();
});
describe('File Operations', () => {
describe('writeFile and readFile', () => {
it('should write and read string files', async () => {
await fs.writeFile('/test.txt', 'Hello World');
const content = await fs.readFile('/test.txt', 'utf8');
expect(content).toBe('Hello World');
});
it('should write and read binary files', async () => {
const data = new Uint8Array([1, 2, 3, 4, 5]);
await fs.writeFile('/test.bin', data);
const content = await fs.readFile('/test.bin');
expect(content).toEqual(data);
});
it('should read string as binary', async () => {
await fs.writeFile('/test.txt', 'Hello');
const content = await fs.readFile('/test.txt');
// When no encoding is specified, should return Uint8Array
expect(ArrayBuffer.isView(content)).toBe(true);
expect(content).toHaveProperty('byteLength');
const decoded = new TextDecoder().decode(content as Uint8Array);
expect(decoded).toBe('Hello');
});
it('should read binary as string with utf8 encoding', async () => {
const data = new TextEncoder().encode('Hello World');
await fs.writeFile('/test.bin', data);
const content = await fs.readFile('/test.bin', 'utf8');
expect(content).toBe('Hello World');
});
it('should support encoding object option', async () => {
await fs.writeFile('/test.txt', 'Hello');
const content = await fs.readFile('/test.txt', { encoding: 'utf8' });
expect(content).toBe('Hello');
});
it('should throw ENOENT when reading non-existent file', async () => {
await expect(fs.readFile('/nonexistent.txt')).rejects.toThrow('ENOENT');
await expect(fs.readFile('/nonexistent.txt')).rejects.toMatchObject({
code: 'ENOENT',
});
});
it('should automatically create parent directory when writing file', async () => {
await fs.writeFile('/dir/subdir/test.txt', 'Hello');
const dirStat = await fs.stat('/dir/subdir');
expect(dirStat.isDirectory()).toBe(true);
});
});
describe('unlink', () => {
it('should delete files', async () => {
await fs.writeFile('/test.txt', 'Hello');
await fs.unlink('/test.txt');
await expect(fs.readFile('/test.txt')).rejects.toThrow('ENOENT');
});
it('should not throw when unlinking non-existent file', async () => {
await expect(fs.unlink('/nonexistent.txt')).resolves.toBeUndefined();
});
});
describe('rename', () => {
it('should rename files', async () => {
await fs.writeFile('/old.txt', 'Hello');
await fs.rename('/old.txt', '/new.txt');
const content = await fs.readFile('/new.txt', 'utf8');
expect(content).toBe('Hello');
await expect(fs.readFile('/old.txt')).rejects.toThrow('ENOENT');
});
it('should preserve file content when renaming', async () => {
const data = new Uint8Array([1, 2, 3, 4, 5]);
await fs.writeFile('/old.bin', data);
await fs.rename('/old.bin', '/new.bin');
const content = await fs.readFile('/new.bin');
expect(content).toEqual(data);
});
});
});
describe('Directory Operations', () => {
describe('mkdir', () => {
it('should create directories', async () => {
await fs.mkdir('/testdir');
const stat = await fs.stat('/testdir');
expect(stat.isDirectory()).toBe(true);
});
it('should create nested directories', async () => {
await fs.mkdir('/parent');
await fs.mkdir('/parent/child');
const stat = await fs.stat('/parent/child');
expect(stat.isDirectory()).toBe(true);
});
});
describe('readdir', () => {
it('should list files and directories', async () => {
await fs.mkdir('/testdir');
await fs.writeFile('/testdir/file1.txt', 'Hello');
await fs.writeFile('/testdir/file2.txt', 'World');
await fs.mkdir('/testdir/subdir');
const entries = await fs.readdir('/testdir');
expect(entries).toHaveLength(3);
expect(entries).toContain('file1.txt');
expect(entries).toContain('file2.txt');
expect(entries).toContain('subdir');
});
it('should return empty array for empty directory', async () => {
await fs.mkdir('/emptydir');
const entries = await fs.readdir('/emptydir');
expect(entries).toEqual([]);
});
it('should list root directory', async () => {
await fs.writeFile('/root-file.txt', 'Hello');
await fs.mkdir('/root-dir');
const entries = await fs.readdir('/');
expect(entries).toContain('root-file.txt');
expect(entries).toContain('root-dir');
});
it('should support withFileTypes option', async () => {
await fs.mkdir('/testdir');
await fs.writeFile('/testdir/file.txt', 'Hello');
await fs.mkdir('/testdir/subdir');
const entries = await fs.readdir('/testdir', { withFileTypes: true });
expect(entries).toHaveLength(2);
const fileEntry = entries.find(e => e.name === 'file.txt');
expect(fileEntry?.isFile()).toBe(true);
expect(fileEntry?.isDirectory()).toBe(false);
const dirEntry = entries.find(e => e.name === 'subdir');
expect(dirEntry?.isDirectory()).toBe(true);
expect(dirEntry?.isFile()).toBe(false);
});
it('should throw ENOENT when reading non-existent directory', async () => {
await expect(fs.readdir('/nonexistent')).rejects.toThrow('ENOENT');
await expect(fs.readdir('/nonexistent')).rejects.toMatchObject({
code: 'ENOENT',
});
});
it('should not list files from nested directories', async () => {
await fs.mkdir('/parent');
await fs.mkdir('/parent/child');
await fs.writeFile('/parent/file1.txt', 'Hello');
await fs.writeFile('/parent/child/file2.txt', 'World');
const entries = await fs.readdir('/parent');
expect(entries).toHaveLength(2);
expect(entries).toContain('file1.txt');
expect(entries).toContain('child');
expect(entries).not.toContain('file2.txt');
});
});
describe('rmdir', () => {
it('should remove directories', async () => {
await fs.mkdir('/testdir');
await fs.rmdir('/testdir');
await expect(fs.stat('/testdir')).rejects.toThrow('ENOENT');
});
it('should not throw when removing non-existent directory', async () => {
await expect(fs.rmdir('/nonexistent')).resolves.toBeUndefined();
});
});
describe('rename directories', () => {
it('should rename directories', async () => {
await fs.mkdir('/olddir');
await fs.rename('/olddir', '/newdir');
const stat = await fs.stat('/newdir');
expect(stat.isDirectory()).toBe(true);
await expect(fs.stat('/olddir')).rejects.toThrow('ENOENT');
});
});
});
describe('stat and lstat', () => {
it('should stat files', async () => {
await fs.writeFile('/test.txt', 'Hello World');
const stat = await fs.stat('/test.txt');
expect(stat.isFile()).toBe(true);
expect(stat.isDirectory()).toBe(false);
expect(stat.isSymbolicLink()).toBe(false);
expect(stat.size).toBe(11);
expect(stat.mode).toBe(0o100644);
expect(stat.mtimeMs).toBeDefined();
expect(stat.mtime).toBeInstanceOf(Date);
expect(stat.ctimeMs).toBeDefined();
expect(stat.ctime).toBeInstanceOf(Date);
expect(stat.ino).toBe(1);
expect(stat.uid).toBe(1000);
expect(stat.gid).toBe(1000);
});
it('should stat directories', async () => {
await fs.mkdir('/testdir');
const stat = await fs.stat('/testdir');
expect(stat.isDirectory()).toBe(true);
expect(stat.isFile()).toBe(false);
expect(stat.isSymbolicLink()).toBe(false);
expect(stat.size).toBe(0);
expect(stat.mode).toBe(0o40755);
});
it('should stat binary files with correct size', async () => {
const data = new Uint8Array([1, 2, 3, 4, 5]);
await fs.writeFile('/test.bin', data);
const stat = await fs.stat('/test.bin');
expect(stat.size).toBe(5);
});
it('should throw ENOENT for non-existent paths', async () => {
await expect(fs.stat('/nonexistent')).rejects.toThrow('ENOENT');
await expect(fs.stat('/nonexistent')).rejects.toMatchObject({
code: 'ENOENT',
});
});
it('should have lstat behave identically to stat', async () => {
await fs.writeFile('/test.txt', 'Hello');
const statResult = await fs.stat('/test.txt');
const lstatResult = await fs.lstat('/test.txt');
// Compare the key properties (dates will differ slightly due to timing)
expect(lstatResult.isFile()).toBe(statResult.isFile());
expect(lstatResult.isDirectory()).toBe(statResult.isDirectory());
expect(lstatResult.isSymbolicLink()).toBe(statResult.isSymbolicLink());
expect(lstatResult.size).toBe(statResult.size);
expect(lstatResult.mode).toBe(statResult.mode);
});
});
describe('rm', () => {
it('should remove files non-recursively', async () => {
await fs.writeFile('/test.txt', 'Hello');
await fs.rm('/test.txt');
await expect(fs.readFile('/test.txt')).rejects.toThrow('ENOENT');
});
it('should remove files and directories recursively', async () => {
await fs.mkdir('/parent');
await fs.mkdir('/parent/child');
await fs.writeFile('/parent/file1.txt', 'Hello');
await fs.writeFile('/parent/child/file2.txt', 'World');
await fs.rm('/parent', { recursive: true });
await expect(fs.stat('/parent')).rejects.toThrow('ENOENT');
await expect(fs.stat('/parent/child')).rejects.toThrow('ENOENT');
await expect(fs.readFile('/parent/file1.txt')).rejects.toThrow('ENOENT');
await expect(fs.readFile('/parent/child/file2.txt')).rejects.toThrow('ENOENT');
});
it('should remove only matching paths when recursive', async () => {
await fs.mkdir('/keep');
await fs.mkdir('/remove');
await fs.writeFile('/keep/file.txt', 'Keep');
await fs.writeFile('/remove/file.txt', 'Remove');
await fs.rm('/remove', { recursive: true });
const keepContent = await fs.readFile('/keep/file.txt', 'utf8');
expect(keepContent).toBe('Keep');
await expect(fs.readFile('/remove/file.txt')).rejects.toThrow('ENOENT');
});
});
describe('Symbolic Links (No-op)', () => {
it('should not throw when creating symlinks', async () => {
await expect(fs.symlink('/target', '/link')).resolves.toBeUndefined();
});
it('should throw EINVAL when reading symlinks', async () => {
await expect(fs.readlink('/link')).rejects.toThrow('EINVAL');
await expect(fs.readlink('/link')).rejects.toMatchObject({
code: 'EINVAL',
});
});
});
describe('chmod (No-op)', () => {
it('should not throw when changing permissions', async () => {
await fs.writeFile('/test.txt', 'Hello');
await expect(fs.chmod('/test.txt', 0o755)).resolves.toBeUndefined();
});
});
describe('promises property', () => {
it('should expose promises property for isomorphic-git compatibility', () => {
expect(fs.promises).toBe(fs);
});
});
describe('Edge Cases', () => {
it('should handle paths with multiple slashes', async () => {
await fs.writeFile('/dir//file.txt', 'Hello');
// The mock doesn't normalize paths, so this creates a literal path
const content = await fs.readFile('/dir//file.txt', 'utf8');
expect(content).toBe('Hello');
});
it('should handle empty file content', async () => {
await fs.writeFile('/empty.txt', '');
const content = await fs.readFile('/empty.txt', 'utf8');
expect(content).toBe('');
});
it('should handle empty binary content', async () => {
await fs.writeFile('/empty.bin', new Uint8Array(0));
const content = await fs.readFile('/empty.bin');
expect(content).toEqual(new Uint8Array(0));
});
it('should handle deeply nested directories', async () => {
await fs.mkdir('/a');
await fs.mkdir('/a/b');
await fs.mkdir('/a/b/c');
await fs.mkdir('/a/b/c/d');
await fs.writeFile('/a/b/c/d/file.txt', 'Deep');
const content = await fs.readFile('/a/b/c/d/file.txt', 'utf8');
expect(content).toBe('Deep');
const stat = await fs.stat('/a/b/c/d');
expect(stat.isDirectory()).toBe(true);
});
it('should handle root directory stat', async () => {
const stat = await fs.stat('/');
expect(stat.isDirectory()).toBe(true);
expect(stat.isFile()).toBe(false);
});
});
});
+211
View File
@@ -0,0 +1,211 @@
import type { JSRuntimeFS, DirectoryEntry } from '@/lib/JSRuntime';
/**
* Minimal mock filesystem implementation for testing
* Implements only the essential methods needed by isomorphic-git
*/
export class MockFS implements JSRuntimeFS {
private files: Map<string, string | Uint8Array> = new Map();
private dirs: Set<string> = new Set(['/']);
// Expose promises property for isomorphic-git compatibility
promises = this;
async readFile(path: string): Promise<Uint8Array>;
async readFile(path: string, options: 'utf8'): Promise<string>;
async readFile(path: string, options: string): Promise<string>;
async readFile(path: string, options: { encoding: 'utf8' }): Promise<string>;
async readFile(path: string, options: { encoding: string }): Promise<string>;
async readFile(path: string, options?: string | { encoding?: string }): Promise<string | Uint8Array>;
async readFile(path: string, options?: string | { encoding?: string }): Promise<string | Uint8Array> {
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;
return new TextDecoder('utf-8').decode(content as Uint8Array);
}
if (typeof content === 'string') return new TextEncoder().encode(content);
return content as Uint8Array;
}
async writeFile(path: string, data: string | Uint8Array): Promise<void> {
this.files.set(path, data);
const dir = path.split('/').slice(0, -1).join('/') || '/';
this.dirs.add(dir);
}
readdir(path: string): Promise<string[]>;
readdir(path: string, options: { withFileTypes: true }): Promise<DirectoryEntry[]>;
readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]>;
async readdir(path: string, options?: { withFileTypes?: boolean }): Promise<string[] | DirectoryEntry[]> {
if (!this.dirs.has(path)) {
const error = new Error(`ENOENT: no such file or directory, scandir '${path}'`) as NodeJS.ErrnoException;
error.code = 'ENOENT';
throw error;
}
const entries: string[] = [];
const searchPrefix = path === '/' ? '/' : path + '/';
const prefixLength = path === '/' ? 1 : path.length + 1;
for (const dir of this.dirs) {
if (dir !== path && dir.startsWith(searchPrefix)) {
const relativePath = dir.slice(prefixLength);
if (!relativePath.includes('/')) {
entries.push(relativePath);
}
}
}
for (const file of this.files.keys()) {
if (file.startsWith(searchPrefix)) {
const relativePath = file.slice(prefixLength);
if (!relativePath.includes('/')) {
entries.push(relativePath);
}
}
}
const uniqueEntries = [...new Set(entries)];
if (options?.withFileTypes) {
return uniqueEntries.map(name => {
const fullPath = path.endsWith('/') ? path + name : path + '/' + name;
const isDir = this.dirs.has(fullPath);
return {
name,
isDirectory: () => isDir,
isFile: () => !isDir,
isSymbolicLink: () => false,
} as unknown as DirectoryEntry;
});
}
return uniqueEntries;
}
async mkdir(path: string): Promise<void> {
this.dirs.add(path);
}
async stat(path: string): Promise<{
isDirectory(): boolean;
isFile(): boolean;
isSymbolicLink(): boolean;
mtimeMs?: number;
mtime?: Date;
ctimeMs?: number;
ctime?: Date;
mode?: number;
size?: number;
ino?: number;
uid?: number;
gid?: number;
}> {
const now = new Date();
const nowMs = now.getTime();
if (this.dirs.has(path)) {
return {
isDirectory: () => true,
isFile: () => false,
isSymbolicLink: () => false,
mtimeMs: nowMs,
mtime: now,
ctimeMs: nowMs,
ctime: now,
mode: 0o40755,
size: 0,
ino: 1,
uid: 1000,
gid: 1000,
};
}
if (this.files.has(path)) {
const content = this.files.get(path)!;
const size = typeof content === 'string' ? content.length : content.byteLength;
return {
isDirectory: () => false,
isFile: () => true,
isSymbolicLink: () => false,
mtimeMs: nowMs,
mtime: now,
ctimeMs: nowMs,
ctime: now,
mode: 0o100644,
size,
ino: 1,
uid: 1000,
gid: 1000,
};
}
const error = new Error(`ENOENT: no such file or directory, stat '${path}'`) as NodeJS.ErrnoException;
error.code = 'ENOENT';
throw error;
}
async lstat(path: string): Promise<{
isDirectory(): boolean;
isFile(): boolean;
isSymbolicLink(): boolean;
mtimeMs?: number;
mtime?: Date;
ctimeMs?: number;
ctime?: Date;
mode?: number;
size?: number;
ino?: number;
uid?: number;
gid?: number;
}> {
return this.stat(path);
}
async readlink(_path: string): Promise<string> {
const error = new Error('EINVAL: invalid argument') as NodeJS.ErrnoException;
error.code = 'EINVAL';
throw error;
}
async symlink(_target: string, _path: string): Promise<void> {
// No-op for mock
}
async unlink(path: string): Promise<void> {
this.files.delete(path);
}
async rmdir(path: string): Promise<void> {
this.dirs.delete(path);
}
async rename(oldPath: string, newPath: string): Promise<void> {
if (this.files.has(oldPath)) {
const content = this.files.get(oldPath);
this.files.delete(oldPath);
this.files.set(newPath, content!);
}
if (this.dirs.has(oldPath)) {
this.dirs.delete(oldPath);
this.dirs.add(newPath);
}
}
async chmod(_path: string, _mode: number): Promise<void> {
// No-op for mock
}
async rm(path: string, options?: { recursive?: boolean }): Promise<void> {
if (options?.recursive) {
for (const file of [...this.files.keys()]) {
if (file.startsWith(path + '/') || file === path) this.files.delete(file);
}
for (const dir of [...this.dirs]) {
if (dir.startsWith(path + '/') || dir === path) this.dirs.delete(dir);
}
} else {
await this.unlink(path);
}
}
}