@@ -7,16 +7,16 @@ test('can write/read a file from a remote hyperdrive', async t => {
77 const { client, cleanup } = await createOne ( )
88
99 try {
10- const { opts , id } = await client . drive . get ( )
11- t . true ( opts . key )
12- t . same ( id , 1 )
10+ const drive = await client . drive . get ( )
11+ t . true ( drive . key )
12+ t . same ( drive . id , 1 )
1313
14- await client . drive . writeFile ( id , 'hello' , 'world' )
14+ await drive . writeFile ( 'hello' , 'world' )
1515
16- const contents = await client . drive . readFile ( id , 'hello' )
16+ const contents = await drive . readFile ( 'hello' )
1717 t . same ( contents , Buffer . from ( 'world' ) )
1818
19- await client . drive . close ( id )
19+ await drive . close ( )
2020 } catch ( err ) {
2121 t . fail ( err )
2222 }
@@ -31,16 +31,16 @@ test('can write/read a large file from a remote hyperdrive', async t => {
3131 const content = Buffer . alloc ( 3.9e6 * 10.11 ) . fill ( 'abcdefghi' )
3232
3333 try {
34- const { opts , id } = await client . drive . get ( )
35- t . true ( opts . key )
36- t . same ( id , 1 )
34+ const drive = await client . drive . get ( )
35+ t . true ( drive . key )
36+ t . same ( drive . id , 1 )
3737
38- await client . drive . writeFile ( id , 'hello' , content )
38+ await drive . writeFile ( 'hello' , content )
3939
40- const contents = await client . drive . readFile ( id , 'hello' )
40+ const contents = await drive . readFile ( 'hello' )
4141 t . same ( contents , content )
4242
43- await client . drive . close ( id )
43+ await drive . close ( )
4444 } catch ( err ) {
4545 t . fail ( err )
4646 }
@@ -53,11 +53,11 @@ test('can write/read a file from a remote hyperdrive using stream methods', asyn
5353 const { client, cleanup } = await createOne ( )
5454
5555 try {
56- const { opts , id } = await client . drive . get ( )
57- t . true ( opts . key )
58- t . same ( id , 1 )
56+ const drive = await client . drive . get ( )
57+ t . true ( drive . key )
58+ t . same ( drive . id , 1 )
5959
60- const writeStream = client . drive . createWriteStream ( id , 'hello' , { uid : 999 , gid : 999 } )
60+ const writeStream = drive . createWriteStream ( 'hello' , { uid : 999 , gid : 999 } )
6161 writeStream . write ( 'hello' )
6262 writeStream . write ( 'there' )
6363 writeStream . end ( 'friend' )
@@ -67,7 +67,7 @@ test('can write/read a file from a remote hyperdrive using stream methods', asyn
6767 writeStream . on ( 'finish' , resolve )
6868 } )
6969
70- const readStream = await client . drive . createReadStream ( id , 'hello' , { start : 5 , length : Buffer . from ( 'there' ) . length + 1 } )
70+ const readStream = await drive . createReadStream ( 'hello' , { start : 5 , length : Buffer . from ( 'there' ) . length + 1 } )
7171 const content = await new Promise ( ( resolve , reject ) => {
7272 collectStream ( readStream , ( err , bufs ) => {
7373 if ( err ) return reject ( err )
@@ -76,11 +76,11 @@ test('can write/read a file from a remote hyperdrive using stream methods', asyn
7676 } )
7777 t . same ( content , Buffer . from ( 'theref' ) )
7878
79- const stat = await client . drive . stat ( id , 'hello' )
79+ const stat = await drive . stat ( 'hello' )
8080 t . same ( stat . uid , 999 )
8181 t . same ( stat . gid , 999 )
8282
83- await client . drive . close ( id )
83+ await drive . close ( )
8484 } catch ( err ) {
8585 t . fail ( err )
8686 }
@@ -93,18 +93,16 @@ test('can stat a file from a remote hyperdrive', async t => {
9393 const { client, cleanup } = await createOne ( )
9494
9595 try {
96- const { opts, id } = await client . drive . get ( )
97- t . true ( opts . key )
98- t . same ( id , 1 )
96+ const drive = await client . drive . get ( )
9997
100- await client . drive . writeFile ( id , 'hello' , 'world' )
98+ await drive . writeFile ( 'hello' , 'world' )
10199
102- const stat = await client . drive . stat ( id , 'hello' )
100+ const stat = await drive . stat ( 'hello' )
103101 t . same ( stat . size , Buffer . from ( 'world' ) . length )
104102 t . same ( stat . uid , 0 )
105103 t . same ( stat . gid , 0 )
106104
107- await client . drive . close ( id )
105+ await drive . close ( )
108106 } catch ( err ) {
109107 t . fail ( err )
110108 }
@@ -117,22 +115,20 @@ test('can list a directory from a remote hyperdrive', async t => {
117115 const { client, cleanup } = await createOne ( )
118116
119117 try {
120- const { opts, id } = await client . drive . get ( )
121- t . true ( opts . key )
122- t . same ( id , 1 )
118+ const drive = await client . drive . get ( )
123119
124- await client . drive . writeFile ( id , 'hello' , 'world' )
125- await client . drive . writeFile ( id , 'goodbye' , 'dog' )
126- await client . drive . writeFile ( id , 'adios' , 'amigo' )
120+ await drive . writeFile ( 'hello' , 'world' )
121+ await drive . writeFile ( 'goodbye' , 'dog' )
122+ await drive . writeFile ( 'adios' , 'amigo' )
127123
128- const files = await client . drive . readdir ( id , '' )
124+ const files = await drive . readdir ( '' )
129125 t . same ( files . length , 4 )
130126 t . notEqual ( files . indexOf ( 'hello' ) , - 1 )
131127 t . notEqual ( files . indexOf ( 'goodbye' ) , - 1 )
132128 t . notEqual ( files . indexOf ( 'adios' ) , - 1 )
133129 t . notEqual ( files . indexOf ( '.key' ) , - 1 )
134130
135- await client . drive . close ( id )
131+ await drive . close ( )
136132 } catch ( err ) {
137133 t . fail ( err )
138134 }
@@ -151,21 +147,23 @@ test('can read/write multiple remote hyperdrives on one server', async t => {
151147 [ 'random' , 'file' ]
152148 ]
153149
150+ var drives = [ ]
154151 for ( const [ file , content ] of files ) {
155- await createAndWrite ( file , content )
152+ drives . push ( await createAndWrite ( file , content ) )
156153 }
157154
158- for ( let i = 1 ; i < files . length + 1 ; i ++ ) {
159- const [ file , content ] = files [ i - 1 ]
160- const readContent = await client . drive . readFile ( i , file )
155+ for ( let i = 0 ; i < files . length ; i ++ ) {
156+ const [ file , content ] = files [ i ]
157+ const drive = drives [ i ]
158+ const readContent = await drive . readFile ( file )
161159 t . same ( readContent , Buffer . from ( content ) )
162160 }
163161
164162 async function createAndWrite ( file , content ) {
165- const { opts , id } = await client . drive . get ( )
166- t . true ( opts . key )
167- t . same ( id , startingId ++ )
168- await client . drive . writeFile ( id , file , content )
163+ const drive = await client . drive . get ( )
164+ t . same ( drive . id , startingId ++ )
165+ await drive . writeFile ( file , content )
166+ return drive
169167 }
170168
171169 await cleanup ( )
@@ -176,31 +174,25 @@ test('can mount a drive within a remote hyperdrive', async t => {
176174 const { client, cleanup } = await createOne ( )
177175
178176 try {
179- const { opts : opts1 , id : id1 } = await client . drive . get ( )
180- t . true ( opts1 . key )
181- t . same ( id1 , 1 )
177+ const drive1 = await client . drive . get ( )
182178
183- const { opts : opts2 , id : id2 } = await client . drive . get ( )
184- t . true ( opts2 . key )
185- t . same ( id2 , 2 )
186- t . notEqual ( opts1 . key , opts2 . key )
179+ const drive2 = await client . drive . get ( )
180+ t . notEqual ( drive1 . key , drive2 . key )
187181
188- const noVersion = { ... opts2 , version : null }
182+ await drive1 . mount ( 'a' , { key : drive2 . key } )
189183
190- await client . drive . mount ( id1 , 'a' , noVersion )
184+ await drive1 . writeFile ( 'a/hello' , 'world' )
185+ await drive1 . writeFile ( 'a/goodbye' , 'dog' )
186+ await drive1 . writeFile ( 'adios' , 'amigo' )
187+ await drive2 . writeFile ( 'hamster' , 'wheel' )
191188
192- await client . drive . writeFile ( id1 , 'a/hello' , 'world' )
193- await client . drive . writeFile ( id1 , 'a/goodbye' , 'dog' )
194- await client . drive . writeFile ( id1 , 'adios' , 'amigo' )
195- await client . drive . writeFile ( id2 , 'hamster' , 'wheel' )
189+ t . same ( await drive1 . readFile ( 'adios' ) , Buffer . from ( 'amigo' ) )
190+ t . same ( await drive1 . readFile ( 'a/hello' ) , Buffer . from ( 'world' ) )
191+ t . same ( await drive2 . readFile ( 'hello' ) , Buffer . from ( 'world' ) )
192+ t . same ( await drive2 . readFile ( 'hamster' ) , Buffer . from ( 'wheel' ) )
196193
197- t . same ( await client . drive . readFile ( id1 , 'adios' ) , Buffer . from ( 'amigo' ) )
198- t . same ( await client . drive . readFile ( id1 , 'a/hello' ) , Buffer . from ( 'world' ) )
199- t . same ( await client . drive . readFile ( id2 , 'hello' ) , Buffer . from ( 'world' ) )
200- t . same ( await client . drive . readFile ( id2 , 'hamster' ) , Buffer . from ( 'wheel' ) )
201-
202- await client . drive . close ( id1 )
203- await client . drive . close ( id2 )
194+ await drive1 . close ( )
195+ await drive2 . close ( )
204196 } catch ( err ) {
205197 t . fail ( err )
206198 }
@@ -213,39 +205,32 @@ test('can unmount a drive within a remote hyperdrive', async t => {
213205 const { client, cleanup } = await createOne ( )
214206
215207 try {
216- const { opts : opts1 , id : id1 } = await client . drive . get ( )
217- t . true ( opts1 . key )
218- t . same ( id1 , 1 )
219-
220- const { opts : opts2 , id : id2 } = await client . drive . get ( )
221- t . true ( opts2 . key )
222- t . same ( id2 , 2 )
223- t . notEqual ( opts1 . key , opts2 . key )
208+ const drive1 = await client . drive . get ( )
209+ const drive2 = await client . drive . get ( )
210+ t . notEqual ( drive1 . key , drive2 . key )
224211
225- const noVersion = { ... opts2 , version : null }
212+ await drive1 . mount ( 'a' , { key : drive2 . key } )
226213
227- await client . drive . mount ( id1 , 'a' , noVersion )
214+ await drive1 . writeFile ( 'a/hello' , 'world' )
215+ await drive1 . writeFile ( 'a/goodbye' , 'dog' )
216+ await drive1 . writeFile ( 'adios' , 'amigo' )
217+ await drive2 . writeFile ( 'hamster' , 'wheel' )
228218
229- await client . drive . writeFile ( id1 , 'a/hello' , 'world' )
230- await client . drive . writeFile ( id1 , 'a/goodbye' , 'dog' )
231- await client . drive . writeFile ( id1 , 'adios' , 'amigo' )
232- await client . drive . writeFile ( id2 , 'hamster' , 'wheel' )
219+ t . same ( await drive1 . readFile ( 'adios' ) , Buffer . from ( 'amigo' ) )
220+ t . same ( await drive1 . readFile ( 'a/hello' ) , Buffer . from ( 'world' ) )
221+ t . same ( await drive2 . readFile ( 'hello' ) , Buffer . from ( 'world' ) )
222+ t . same ( await drive2 . readFile ( 'hamster' ) , Buffer . from ( 'wheel' ) )
233223
234- t . same ( await client . drive . readFile ( id1 , 'adios' ) , Buffer . from ( 'amigo' ) )
235- t . same ( await client . drive . readFile ( id1 , 'a/hello' ) , Buffer . from ( 'world' ) )
236- t . same ( await client . drive . readFile ( id2 , 'hello' ) , Buffer . from ( 'world' ) )
237- t . same ( await client . drive . readFile ( id2 , 'hamster' ) , Buffer . from ( 'wheel' ) )
238-
239- await client . drive . unmount ( id1 , 'a' )
224+ await drive1 . unmount ( 'a' )
240225 try {
241- await client . drive . readFile ( id1 , 'a/hello' )
226+ await drive1 . readFile ( 'a/hello' )
242227 } catch ( err ) {
243228 t . true ( err )
244229 t . same ( err . code , 2 )
245230 }
246231
247- await client . drive . close ( id1 )
248- await client . drive . close ( id2 )
232+ await drive1 . close ( )
233+ await drive2 . close ( )
249234 } catch ( err ) {
250235 t . fail ( err )
251236 }
@@ -260,17 +245,17 @@ test('can watch a remote hyperdrive', async t => {
260245 var triggered = 0
261246
262247 try {
263- const { id } = await client . drive . get ( )
248+ const drive = await client . drive . get ( )
264249
265- const unwatch = client . drive . watch ( id , '' , ( ) => {
250+ const unwatch = drive . watch ( '' , ( ) => {
266251 triggered ++
267252 } )
268253
269- await client . drive . writeFile ( id , 'hello' , 'world' )
254+ await drive . writeFile ( 'hello' , 'world' )
270255 await unwatch ( )
271- await client . drive . writeFile ( id , 'world' , 'hello' )
256+ await drive . writeFile ( 'world' , 'hello' )
272257
273- await client . drive . close ( id )
258+ await drive . close ( )
274259 } catch ( err ) {
275260 t . fail ( err )
276261 }
0 commit comments