|
| 1 | +import { BrowserFileSystem } from '../../src/storage/BrowserFileSystem.js'; |
| 2 | + |
| 3 | +describe('BrowserFileSystem', () => { |
| 4 | + beforeEach(() => { |
| 5 | + localStorage.clear(); |
| 6 | + }); |
| 7 | + |
| 8 | + describe('readDir', () => { |
| 9 | + it('should return all values in path', () => { |
| 10 | + localStorage.setItem('backtrace__/dir/1', 'test'); |
| 11 | + localStorage.setItem('backtrace__/dir/2', 'test'); |
| 12 | + |
| 13 | + const fs = new BrowserFileSystem(localStorage); |
| 14 | + const files = fs.readDirSync('dir'); |
| 15 | + expect(files).toEqual(['1', '2']); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should return all values in absolute path', () => { |
| 19 | + localStorage.setItem('backtrace__/dir1/dir2/1', 'test'); |
| 20 | + localStorage.setItem('backtrace__/dir1/dir2/2', 'test'); |
| 21 | + |
| 22 | + const fs = new BrowserFileSystem(localStorage); |
| 23 | + const files = fs.readDirSync('/dir1/dir2/'); |
| 24 | + expect(files).toEqual(['1', '2']); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should return no values for non-existing keys', () => { |
| 28 | + const fs = new BrowserFileSystem(localStorage); |
| 29 | + const files = fs.readDirSync('/dir1/dir2/'); |
| 30 | + expect(files).toEqual([]); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should not return values not prefixed by backtrace__', () => { |
| 34 | + localStorage.setItem('test__/dir1/dir2/1', 'test'); |
| 35 | + localStorage.setItem('backtrace__/dir1/dir2/2', 'test'); |
| 36 | + |
| 37 | + const fs = new BrowserFileSystem(localStorage); |
| 38 | + const files = fs.readDirSync('/dir1/dir2/'); |
| 39 | + expect(files).toEqual(['2']); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('createDir', () => { |
| 44 | + it('should do nothing', () => { |
| 45 | + localStorage.setItem('backtrace__/dir1/dir2/1', 'test'); |
| 46 | + |
| 47 | + const fs = new BrowserFileSystem(localStorage); |
| 48 | + fs.createDirSync('/a/b/c'); |
| 49 | + |
| 50 | + expect(Object.keys(localStorage)).toEqual(['backtrace__/dir1/dir2/1']); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('readFile', () => { |
| 55 | + it('should return key contents', () => { |
| 56 | + localStorage.setItem('backtrace__/dir1/dir2/1', 'test'); |
| 57 | + |
| 58 | + const fs = new BrowserFileSystem(localStorage); |
| 59 | + const actual = fs.readFileSync('/dir1/dir2/1'); |
| 60 | + |
| 61 | + expect(actual).toEqual('test'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should throw if key does not exist', () => { |
| 65 | + const fs = new BrowserFileSystem(localStorage); |
| 66 | + expect(() => fs.readFileSync('/dir1/dir2/1')).toThrow('path does not exist'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('writeFile', () => { |
| 71 | + it('should write key contents', () => { |
| 72 | + const fs = new BrowserFileSystem(localStorage); |
| 73 | + fs.writeFileSync('/dir1/dir2/1', 'test'); |
| 74 | + |
| 75 | + expect(localStorage.getItem('backtrace__/dir1/dir2/1')).toEqual('test'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should write key contents with relative path', () => { |
| 79 | + const fs = new BrowserFileSystem(localStorage); |
| 80 | + fs.writeFileSync('dir1/dir2/1', 'test'); |
| 81 | + |
| 82 | + expect(localStorage.getItem('backtrace__/dir1/dir2/1')).toEqual('test'); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('unlink', () => { |
| 87 | + it('should remove file from storage', () => { |
| 88 | + localStorage.setItem('backtrace__/dir1/dir2/1', 'test'); |
| 89 | + |
| 90 | + const fs = new BrowserFileSystem(localStorage); |
| 91 | + fs.unlinkSync('/dir1/dir2/1'); |
| 92 | + |
| 93 | + expect(localStorage.getItem('backtrace__/dir1/dir2/1')).toBeNull(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should remove file from storage with relative path', () => { |
| 97 | + localStorage.setItem('backtrace__/dir1/dir2/1', 'test'); |
| 98 | + |
| 99 | + const fs = new BrowserFileSystem(localStorage); |
| 100 | + fs.unlinkSync('dir1/dir2/1'); |
| 101 | + |
| 102 | + expect(localStorage.getItem('backtrace__/dir1/dir2/1')).toBeNull(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should not throw if file does not exist', () => { |
| 106 | + const fs = new BrowserFileSystem(localStorage); |
| 107 | + expect(() => fs.unlinkSync('/dir1/dir2/1')).not.toThrow(); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('existsSync', () => { |
| 112 | + it('should return true if file exists', () => { |
| 113 | + localStorage.setItem('backtrace__/dir1/dir2/1', 'test'); |
| 114 | + |
| 115 | + const fs = new BrowserFileSystem(localStorage); |
| 116 | + const exists = fs.existsSync('/dir1/dir2/1'); |
| 117 | + |
| 118 | + expect(exists).toBe(true); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should return true if file exists wit relative path', () => { |
| 122 | + localStorage.setItem('backtrace__/dir1/dir2/1', 'test'); |
| 123 | + |
| 124 | + const fs = new BrowserFileSystem(localStorage); |
| 125 | + const exists = fs.existsSync('dir1/dir2/1'); |
| 126 | + |
| 127 | + expect(exists).toBe(true); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should return false if file does not exist', () => { |
| 131 | + const fs = new BrowserFileSystem(localStorage); |
| 132 | + const exists = fs.existsSync('/dir1/dir2/1'); |
| 133 | + |
| 134 | + expect(exists).toBe(false); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('createAttachment', () => { |
| 139 | + it('should create attachment from file contents', () => { |
| 140 | + localStorage.setItem('backtrace__/path/to/file.txt', 'file content'); |
| 141 | + |
| 142 | + const fs = new BrowserFileSystem(localStorage); |
| 143 | + const attachment = fs.createAttachment('/path/to/file.txt'); |
| 144 | + |
| 145 | + expect(attachment.name).toBe('file.txt'); |
| 146 | + expect(attachment.get()).toBe('file content'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should create attachment from file contents with relative path', () => { |
| 150 | + localStorage.setItem('backtrace__/path/to/file.txt', 'file content'); |
| 151 | + |
| 152 | + const fs = new BrowserFileSystem(localStorage); |
| 153 | + const attachment = fs.createAttachment('path/to/file.txt'); |
| 154 | + |
| 155 | + expect(attachment.name).toBe('file.txt'); |
| 156 | + expect(attachment.get()).toBe('file content'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should create attachment with custom name', () => { |
| 160 | + localStorage.setItem('backtrace__/path/to/file.txt', 'file content'); |
| 161 | + |
| 162 | + const fs = new BrowserFileSystem(localStorage); |
| 163 | + const attachment = fs.createAttachment('/path/to/file.txt', 'custom-name.txt'); |
| 164 | + |
| 165 | + expect(attachment.name).toBe('custom-name.txt'); |
| 166 | + expect(attachment.get()).toBe('file content'); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should throw if file does not exist', () => { |
| 170 | + const fs = new BrowserFileSystem(localStorage); |
| 171 | + expect(() => fs.createAttachment('/nonexistent/file.txt')).toThrow('path does not exist'); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments