1
1
import { Superblock } from '../../core/Superblock' ;
2
- import { coreToFsa } from '../index' ;
2
+ import { coreToFsa , fsa } from '../index' ;
3
3
import { CoreFileSystemDirectoryHandle } from '../CoreFileSystemDirectoryHandle' ;
4
4
import { onlyOnNode20 } from '../../__tests__/util' ;
5
+ import { fileURLToPath } from 'url' ;
5
6
6
7
onlyOnNode20 ( 'coreToFsa scenarios' , ( ) => {
7
8
test ( 'can create FSA from empty Superblock' , ( ) => {
@@ -21,6 +22,38 @@ onlyOnNode20('coreToFsa scenarios', () => {
21
22
expect ( fsa ) . toBeInstanceOf ( CoreFileSystemDirectoryHandle ) ;
22
23
} ) ;
23
24
25
+ test ( 'can create FSA using fsa() helper' , async ( ) => {
26
+ const { dir, core} = fsa ( { mode : 'readwrite' } ) ;
27
+ core . fromJSON ( {
28
+ 'documents/readme.txt' : 'Welcome!' ,
29
+ 'photos/vacation.jpg' : Buffer . from ( 'fake-jpg-data' ) ,
30
+ 'empty-folder' : null ,
31
+ } , '/' ) ;
32
+ expect ( dir ) . toBeInstanceOf ( CoreFileSystemDirectoryHandle ) ;
33
+ expect ( dir . name ) . toBe ( '' ) ;
34
+ const dir2 = await dir . getDirectoryHandle ( 'documents' ) ;
35
+ const file = await dir2 . getFileHandle ( 'readme.txt' ) ;
36
+ const fileContent = await file . getFile ( ) ;
37
+ expect ( await fileContent . text ( ) ) . toBe ( 'Welcome!' ) ;
38
+ } ) ;
39
+
40
+ test ( 'can create a a file folder in empty filesystem' , async ( ) => {
41
+ const { dir, core} = fsa ( { mode : 'readwrite' } ) ;
42
+ expect ( core . toJSON ( ) ) . toEqual ( { } ) ;
43
+ const dir2 = await dir . getDirectoryHandle ( 'new-folder' , { create : true } ) ;
44
+ expect ( core . toJSON ( ) ) . toEqual ( {
45
+ '/new-folder' : null ,
46
+ } ) ;
47
+ const file = await dir2 . getFileHandle ( 'file.txt' , { create : true } ) ;
48
+ expect ( core . toJSON ( ) ) . toEqual ( {
49
+ '/new-folder/file.txt' : '' ,
50
+ } ) ;
51
+ await ( await file . createWritable ( ) ) . write ( 'Hello, world!' ) ;
52
+ expect ( core . toJSON ( ) ) . toEqual ( {
53
+ '/new-folder/file.txt' : 'Hello, world!' ,
54
+ } ) ;
55
+ } ) ;
56
+
24
57
test ( 'can navigate filesystem structure' , async ( ) => {
25
58
const core = Superblock . fromJSON (
26
59
{
0 commit comments