xref: /expo/apps/test-suite/tests/FileSystem.js (revision 19cbf4dc)
1740b3867SJames Ide'use strict';
2740b3867SJames Ide
3a47a1472SVille Immonenimport { Asset } from 'expo-asset';
48cb96155SEvan Baconimport Constants from 'expo-constants';
5*19cbf4dcSBartłomiej Bukowskiimport * as FS from 'expo-file-system';
6*19cbf4dcSBartłomiej Bukowskiimport { Platform } from 'expo-modules-core';
767f55e2bSStanisław Chmiela
867f55e2bSStanisław Chmielaexport const name = 'FileSystem';
9740b3867SJames Ide
108cb96155SEvan Baconexport async function test({ describe, expect, it, ...t }) {
118cb96155SEvan Bacon  describe('FileSystem', () => {
12740b3867SJames Ide    const throws = async run => {
13740b3867SJames Ide      let error = null;
14740b3867SJames Ide      try {
15740b3867SJames Ide        await run();
16740b3867SJames Ide      } catch (e) {
17740b3867SJames Ide        error = e;
18740b3867SJames Ide      }
198cb96155SEvan Bacon      expect(error).toBeTruthy();
20740b3867SJames Ide    };
21740b3867SJames Ide
228cb96155SEvan Bacon    if (Constants.appOwnership === 'expo') {
238cb96155SEvan Bacon      describe('managed workflow', () => {
248cb96155SEvan Bacon        it('throws out-of-scope exceptions', async () => {
258cb96155SEvan Bacon          const p = FS.documentDirectory;
268cb96155SEvan Bacon
278cb96155SEvan Bacon          await throws(() => FS.readAsStringAsync(p + '../hello/world'));
288cb96155SEvan Bacon          await throws(() => FS.writeAsStringAsync(p + '../hello/world', ''));
298cb96155SEvan Bacon          await throws(() => FS.deleteAsync(p + '../hello/world'));
308cb96155SEvan Bacon          await throws(() => FS.deleteAsync(p));
318cb96155SEvan Bacon          await throws(() => FS.deleteAsync(FS.cacheDirectory));
328cb96155SEvan Bacon          await throws(() => FS.moveAsync({ from: p + '../a/b', to: 'c' }));
338cb96155SEvan Bacon          await throws(() => FS.moveAsync({ from: 'c', to: p + '../a/b' }));
348cb96155SEvan Bacon          await throws(() => FS.copyAsync({ from: p + '../a/b', to: 'c' }));
358cb96155SEvan Bacon          await throws(() => FS.copyAsync({ from: 'c', to: p + '../a/b' }));
368cb96155SEvan Bacon          await throws(() => FS.makeDirectoryAsync(p + '../hello/world'));
378cb96155SEvan Bacon          await throws(() => FS.readDirectoryAsync(p + '../hello/world'));
388cb96155SEvan Bacon          await throws(() => FS.downloadAsync('http://www.google.com', p + '../hello/world'));
398cb96155SEvan Bacon          await throws(() => FS.readDirectoryAsync(p + '../'));
408cb96155SEvan Bacon          await throws(() => FS.downloadAsync('http://www.google.com', p + '../hello/world'));
418cb96155SEvan Bacon        });
428cb96155SEvan Bacon      });
438cb96155SEvan Bacon    }
448cb96155SEvan Bacon
458cb96155SEvan Bacon    if (Platform.OS === 'web') {
468cb96155SEvan Bacon      // Web doesn't support FileSystem
478cb96155SEvan Bacon      return;
488cb96155SEvan Bacon    }
498cb96155SEvan Bacon
508cb96155SEvan Bacon    it(
51740b3867SJames Ide      'delete(idempotent) -> !exists -> download(md5, uri) -> exists ' + '-> delete -> !exists',
52740b3867SJames Ide      async () => {
53740b3867SJames Ide        const localUri = FS.documentDirectory + 'download1.png';
54740b3867SJames Ide
55740b3867SJames Ide        const assertExists = async expectedToExist => {
56*19cbf4dcSBartłomiej Bukowski          const { exists } = await FS.getInfoAsync(localUri);
57740b3867SJames Ide          if (expectedToExist) {
588cb96155SEvan Bacon            expect(exists).toBeTruthy();
59740b3867SJames Ide          } else {
608cb96155SEvan Bacon            expect(exists).not.toBeTruthy();
61740b3867SJames Ide          }
62740b3867SJames Ide        };
63740b3867SJames Ide
64740b3867SJames Ide        await FS.deleteAsync(localUri, { idempotent: true });
65740b3867SJames Ide        await assertExists(false);
66740b3867SJames Ide
67d12a8680SŁukasz Kosmaty        const {
68d12a8680SŁukasz Kosmaty          md5,
69d12a8680SŁukasz Kosmaty          headers,
70d12a8680SŁukasz Kosmaty        } = await FS.downloadAsync(
71740b3867SJames Ide          'https://s3-us-west-1.amazonaws.com/test-suite-data/avatar2.png',
72740b3867SJames Ide          localUri,
73740b3867SJames Ide          { md5: true }
74740b3867SJames Ide        );
758cb96155SEvan Bacon        expect(md5).toBe('1e02045c10b8f1145edc7c8375998f87');
76740b3867SJames Ide        await assertExists(true);
778cb96155SEvan Bacon        expect(headers['Content-Type']).toBe('image/png');
78740b3867SJames Ide
79740b3867SJames Ide        await FS.deleteAsync(localUri);
80740b3867SJames Ide        await assertExists(false);
81740b3867SJames Ide      },
82740b3867SJames Ide      9000
83fec220cbSSergei Chestakov    );
84740b3867SJames Ide
858cb96155SEvan Bacon    it('Can read/write Base64', async () => {
862bd74c95SEvan Bacon      const asset = await Asset.fromModule(require('../assets/icons/app.png'));
872bd74c95SEvan Bacon      await asset.downloadAsync();
882bd74c95SEvan Bacon
8967f55e2bSStanisław Chmiela      for (let startingPosition = 0; startingPosition < 3; startingPosition++) {
902bd74c95SEvan Bacon        const options = {
91fec220cbSSergei Chestakov          encoding: FS.EncodingType.Base64,
9267f55e2bSStanisław Chmiela          position: startingPosition,
9367f55e2bSStanisław Chmiela          length: startingPosition + 1,
942bd74c95SEvan Bacon        };
952bd74c95SEvan Bacon
962bd74c95SEvan Bacon        const b64 = await FS.readAsStringAsync(asset.localUri, options);
978cb96155SEvan Bacon        expect(b64).toBeDefined();
988cb96155SEvan Bacon        expect(typeof b64).toBe('string');
998cb96155SEvan Bacon        expect(b64.length % 4).toBe(0);
1002bd74c95SEvan Bacon
1012bd74c95SEvan Bacon        const localUri = FS.documentDirectory + 'b64.png';
1022bd74c95SEvan Bacon
103fec220cbSSergei Chestakov        await FS.writeAsStringAsync(localUri, b64, { encoding: FS.EncodingType.Base64 });
1042bd74c95SEvan Bacon
1058cb96155SEvan Bacon        expect(await FS.readAsStringAsync(localUri, { encoding: FS.EncodingType.Base64 })).toBe(
10667f55e2bSStanisław Chmiela          b64
10767f55e2bSStanisław Chmiela        );
1082bd74c95SEvan Bacon      }
1092bd74c95SEvan Bacon    });
1102bd74c95SEvan Bacon
1118cb96155SEvan Bacon    it('delete(idempotent) -> delete[error]', async () => {
112740b3867SJames Ide      const localUri = FS.documentDirectory + 'willDelete.png';
113740b3867SJames Ide
114740b3867SJames Ide      await FS.deleteAsync(localUri, { idempotent: true });
115740b3867SJames Ide
116740b3867SJames Ide      let error;
117740b3867SJames Ide      try {
118740b3867SJames Ide        await FS.deleteAsync(localUri);
119740b3867SJames Ide      } catch (e) {
120740b3867SJames Ide        error = e;
121740b3867SJames Ide      }
1228cb96155SEvan Bacon      expect(error.message).toMatch(/not.*found/);
123740b3867SJames Ide    });
124740b3867SJames Ide
1258cb96155SEvan Bacon    it('download(md5, uri) -> read -> delete -> !exists -> read[error]', async () => {
126740b3867SJames Ide      const localUri = FS.documentDirectory + 'download1.txt';
127740b3867SJames Ide
128d12a8680SŁukasz Kosmaty      const {
129d12a8680SŁukasz Kosmaty        md5,
130d12a8680SŁukasz Kosmaty      } = await FS.downloadAsync(
131740b3867SJames Ide        'https://s3-us-west-1.amazonaws.com/test-suite-data/text-file.txt',
132740b3867SJames Ide        localUri,
133740b3867SJames Ide        { md5: true }
134740b3867SJames Ide      );
1358cb96155SEvan Bacon      expect(md5).toBe('86d73d2f11e507365f7ea8e7ec3cc4cb');
136740b3867SJames Ide
137740b3867SJames Ide      const string = await FS.readAsStringAsync(localUri);
1388cb96155SEvan Bacon      expect(string).toBe('hello, world\nthis is a test file\n');
139740b3867SJames Ide
140740b3867SJames Ide      await FS.deleteAsync(localUri, { idempotent: true });
141740b3867SJames Ide
142740b3867SJames Ide      let error;
143740b3867SJames Ide      try {
144740b3867SJames Ide        await FS.readAsStringAsync(localUri);
145740b3867SJames Ide      } catch (e) {
146740b3867SJames Ide        error = e;
147740b3867SJames Ide      }
1488cb96155SEvan Bacon      expect(error).toBeTruthy();
1498cb96155SEvan Bacon    }, 9000);
150740b3867SJames Ide
1518cb96155SEvan Bacon    it('delete(idempotent) -> !exists -> write -> read -> write -> read', async () => {
152740b3867SJames Ide      const localUri = FS.documentDirectory + 'write1.txt';
153740b3867SJames Ide
154740b3867SJames Ide      await FS.deleteAsync(localUri, { idempotent: true });
155740b3867SJames Ide
156740b3867SJames Ide      const { exists } = await FS.getInfoAsync(localUri);
1578cb96155SEvan Bacon      expect(exists).not.toBeTruthy();
158740b3867SJames Ide
159740b3867SJames Ide      const writeAndVerify = async expected => {
160740b3867SJames Ide        await FS.writeAsStringAsync(localUri, expected);
161740b3867SJames Ide        const string = await FS.readAsStringAsync(localUri);
1628cb96155SEvan Bacon        expect(string).toBe(expected);
163740b3867SJames Ide      };
164740b3867SJames Ide
165740b3867SJames Ide      await writeAndVerify('hello, world');
166740b3867SJames Ide      await writeAndVerify('hello, world!!!!!!');
167740b3867SJames Ide    });
168740b3867SJames Ide
1698cb96155SEvan Bacon    it('delete(new) -> 2 * [write -> move -> !exists(orig) -> read(new)]', async () => {
170740b3867SJames Ide      const from = FS.documentDirectory + 'from.txt';
171740b3867SJames Ide      const to = FS.documentDirectory + 'to.txt';
172740b3867SJames Ide      const contents = ['contents 1', 'contents 2'];
173740b3867SJames Ide
174740b3867SJames Ide      await FS.deleteAsync(to, { idempotent: true });
175740b3867SJames Ide
176740b3867SJames Ide      // Move twice to make sure we can overwrite
177740b3867SJames Ide      for (let i = 0; i < 2; ++i) {
178740b3867SJames Ide        await FS.writeAsStringAsync(from, contents[i]);
179740b3867SJames Ide
180740b3867SJames Ide        await FS.moveAsync({ from, to });
181740b3867SJames Ide
182740b3867SJames Ide        const { exists } = await FS.getInfoAsync(from);
1838cb96155SEvan Bacon        expect(exists).not.toBeTruthy();
184740b3867SJames Ide
1858cb96155SEvan Bacon        expect(await FS.readAsStringAsync(to)).toBe(contents[i]);
186740b3867SJames Ide      }
187740b3867SJames Ide    });
188740b3867SJames Ide
1898cb96155SEvan Bacon    it('delete(new) -> 2 * [write -> copy -> exists(orig) -> read(new)]', async () => {
190740b3867SJames Ide      const from = FS.documentDirectory + 'from.txt';
191740b3867SJames Ide      const to = FS.documentDirectory + 'to.txt';
192740b3867SJames Ide      const contents = ['contents 1', 'contents 2'];
193740b3867SJames Ide
194740b3867SJames Ide      await FS.deleteAsync(to, { idempotent: true });
195740b3867SJames Ide
196740b3867SJames Ide      // Copy twice to make sure we can overwrite
197740b3867SJames Ide      for (let i = 0; i < 2; ++i) {
198740b3867SJames Ide        await FS.writeAsStringAsync(from, contents[i]);
199740b3867SJames Ide
200740b3867SJames Ide        await FS.copyAsync({ from, to });
201740b3867SJames Ide
202740b3867SJames Ide        const { exists } = await FS.getInfoAsync(from);
2038cb96155SEvan Bacon        expect(exists).toBeTruthy();
204740b3867SJames Ide
2058cb96155SEvan Bacon        expect(await FS.readAsStringAsync(to)).toBe(contents[i]);
206740b3867SJames Ide      }
207740b3867SJames Ide    });
208740b3867SJames Ide
2098cb96155SEvan Bacon    it(
210740b3867SJames Ide      'delete(dir) -> write(dir/file)[error] -> mkdir(dir) ->' +
211740b3867SJames Ide        'mkdir(dir)[error] -> write(dir/file) -> read',
212740b3867SJames Ide      async () => {
213740b3867SJames Ide        let error;
214740b3867SJames Ide        const path = FS.documentDirectory + 'dir/file';
215740b3867SJames Ide        const dir = FS.documentDirectory + 'dir';
216740b3867SJames Ide        const contents = 'hello, world';
217740b3867SJames Ide
218740b3867SJames Ide        await FS.deleteAsync(dir, { idempotent: true });
219740b3867SJames Ide
220740b3867SJames Ide        error = null;
221740b3867SJames Ide        try {
222740b3867SJames Ide          await FS.writeAsStringAsync(path, contents);
223740b3867SJames Ide        } catch (e) {
224740b3867SJames Ide          error = e;
225740b3867SJames Ide        }
2268cb96155SEvan Bacon        expect(error).toBeTruthy();
227740b3867SJames Ide
228740b3867SJames Ide        await FS.makeDirectoryAsync(dir);
229740b3867SJames Ide
230740b3867SJames Ide        error = null;
231740b3867SJames Ide        try {
232740b3867SJames Ide          await FS.makeDirectoryAsync(dir);
233740b3867SJames Ide        } catch (e) {
234740b3867SJames Ide          error = e;
235740b3867SJames Ide        }
2368cb96155SEvan Bacon        expect(error).toBeTruthy();
237740b3867SJames Ide
238740b3867SJames Ide        await FS.writeAsStringAsync(path, contents);
239740b3867SJames Ide
2408cb96155SEvan Bacon        expect(await FS.readAsStringAsync(path)).toBe(contents);
241740b3867SJames Ide      }
242740b3867SJames Ide    );
243740b3867SJames Ide
2448cb96155SEvan Bacon    it(
245740b3867SJames Ide      'delete(dir) -> write(dir/dir2/file)[error] -> ' +
246740b3867SJames Ide        'mkdir(dir/dir2, intermediates) -> ' +
247740b3867SJames Ide        'mkdir(dir/dir2, intermediates) -> write(dir/dir2/file) -> read',
248740b3867SJames Ide      async () => {
249740b3867SJames Ide        let error;
250740b3867SJames Ide        const path = FS.documentDirectory + 'dir/dir2/file';
251740b3867SJames Ide        const dir = FS.documentDirectory + 'dir/dir2';
252740b3867SJames Ide        const contents = 'hello, world';
253740b3867SJames Ide
254740b3867SJames Ide        await FS.deleteAsync(dir, { idempotent: true });
255740b3867SJames Ide
256740b3867SJames Ide        error = null;
257740b3867SJames Ide        try {
258740b3867SJames Ide          await FS.writeAsStringAsync(path, contents);
259740b3867SJames Ide        } catch (e) {
260740b3867SJames Ide          error = e;
261740b3867SJames Ide        }
2628cb96155SEvan Bacon        expect(error).toBeTruthy();
263740b3867SJames Ide
264740b3867SJames Ide        await FS.makeDirectoryAsync(dir, {
265740b3867SJames Ide          intermediates: true,
266740b3867SJames Ide        });
267740b3867SJames Ide
268740b3867SJames Ide        error = null;
269740b3867SJames Ide        try {
270740b3867SJames Ide          await FS.makeDirectoryAsync(dir);
271740b3867SJames Ide        } catch (e) {
272740b3867SJames Ide          error = e;
273740b3867SJames Ide        }
2748cb96155SEvan Bacon        expect(error).toBeTruthy();
275740b3867SJames Ide
2766cfa9c5fSSergei Chestakov        error = null;
2776cfa9c5fSSergei Chestakov        try {
2786cfa9c5fSSergei Chestakov          await FS.makeDirectoryAsync(dir, {
2796cfa9c5fSSergei Chestakov            intermediates: true,
2806cfa9c5fSSergei Chestakov          });
2816cfa9c5fSSergei Chestakov        } catch (e) {
2826cfa9c5fSSergei Chestakov          error = e;
2836cfa9c5fSSergei Chestakov        }
2848cb96155SEvan Bacon        expect(error).toBe(null);
2856cfa9c5fSSergei Chestakov
286740b3867SJames Ide        await FS.writeAsStringAsync(path, contents);
287740b3867SJames Ide
2888cb96155SEvan Bacon        expect(await FS.readAsStringAsync(path)).toBe(contents);
289740b3867SJames Ide      }
290740b3867SJames Ide    );
291740b3867SJames Ide
292d12a8680SŁukasz Kosmaty    it('getInfo(dirPath)', async () => {
293d12a8680SŁukasz Kosmaty      const dir = FS.documentDirectory + 'dir';
294d12a8680SŁukasz Kosmaty      const path = FS.documentDirectory + 'dir/file.txt';
295d12a8680SŁukasz Kosmaty
296d12a8680SŁukasz Kosmaty      await FS.deleteAsync(dir, { idempotent: true });
297d12a8680SŁukasz Kosmaty      await FS.makeDirectoryAsync(dir, {
298d12a8680SŁukasz Kosmaty        intermediates: true,
299d12a8680SŁukasz Kosmaty      });
300d12a8680SŁukasz Kosmaty      await FS.writeAsStringAsync(path, 'Expo is awesome ������');
301d12a8680SŁukasz Kosmaty      const info = await FS.getInfoAsync(dir);
302d12a8680SŁukasz Kosmaty
303d12a8680SŁukasz Kosmaty      expect(info).toBeDefined();
304d12a8680SŁukasz Kosmaty      expect(info.exists).toBe(true);
305d12a8680SŁukasz Kosmaty      expect(info.isDirectory).toBe(true);
306d12a8680SŁukasz Kosmaty      expect(info.size).toBe(28);
307d12a8680SŁukasz Kosmaty    });
308d12a8680SŁukasz Kosmaty
309fec220cbSSergei Chestakov    /*
310fec220cbSSergei Chestakov    This test fails in CI because of an exception being thrown by deleteAsync in the nativeModule.
311fec220cbSSergei Chestakov    I traced it down to the FileUtils.forceDelete call here:
312fec220cbSSergei Chestakov    https://github.com/expo/expo/blob/bcd136b096df84e0b0f72a15acbda45491de8201/packages/expo-file-system/android/src/main/java/expo/modules/filesystem/FileSystemModule.java#L294
3138cb96155SEvan Bacon    it(
314740b3867SJames Ide      'delete(dir, idempotent) -> make tree -> check contents ' +
315740b3867SJames Ide        '-> check directory listings' +
316740b3867SJames Ide        '-> move -> check directory listings' +
317740b3867SJames Ide        '-> copy -> check directory listings',
318740b3867SJames Ide      async () => {
319740b3867SJames Ide        let error;
320740b3867SJames Ide        const dir = FS.documentDirectory + 'dir';
321740b3867SJames Ide
322740b3867SJames Ide        await FS.deleteAsync(dir, { idempotent: true });
323740b3867SJames Ide
324740b3867SJames Ide        await FS.makeDirectoryAsync(dir + '/child1', {
325740b3867SJames Ide          intermediates: true,
326740b3867SJames Ide        });
327740b3867SJames Ide        await FS.makeDirectoryAsync(dir + '/child2', {
328740b3867SJames Ide          intermediates: true,
329740b3867SJames Ide        });
330740b3867SJames Ide
331740b3867SJames Ide        await FS.writeAsStringAsync(dir + '/file1', 'contents1');
332740b3867SJames Ide        await FS.writeAsStringAsync(dir + '/file2', 'contents2');
333740b3867SJames Ide
334740b3867SJames Ide        await FS.writeAsStringAsync(dir + '/child1/file3', 'contents3');
335740b3867SJames Ide
336740b3867SJames Ide        await FS.writeAsStringAsync(dir + '/child2/file4', 'contents4');
337740b3867SJames Ide        await FS.writeAsStringAsync(dir + '/child2/file5', 'contents5');
338740b3867SJames Ide
339740b3867SJames Ide        const checkContents = async (path, contents) =>
3408cb96155SEvan Bacon          expect(await FS.readAsStringAsync(path)).toBe(contents);
341740b3867SJames Ide
342740b3867SJames Ide        await checkContents(dir + '/file1', 'contents1');
343740b3867SJames Ide        await checkContents(dir + '/file2', 'contents2');
344740b3867SJames Ide        await checkContents(dir + '/child1/file3', 'contents3');
345740b3867SJames Ide        await checkContents(dir + '/child2/file4', 'contents4');
346740b3867SJames Ide        await checkContents(dir + '/child2/file5', 'contents5');
347740b3867SJames Ide
348740b3867SJames Ide        const checkDirectory = async (path, expected) => {
349740b3867SJames Ide          const list = await FS.readDirectoryAsync(path);
3508cb96155SEvan Bacon          expect(list.sort()).toEqual(expected.sort());
351740b3867SJames Ide        };
352740b3867SJames Ide
353740b3867SJames Ide        const checkRoot = async root => {
354740b3867SJames Ide          await checkDirectory(root, ['file1', 'file2', 'child1', 'child2']);
355740b3867SJames Ide          await checkDirectory(root + '/child1', ['file3']);
356740b3867SJames Ide          await checkDirectory(root + '/child2', ['file4', 'file5']);
357740b3867SJames Ide
358740b3867SJames Ide          error = null;
359740b3867SJames Ide          try {
360740b3867SJames Ide            await checkDirectory(root + '/file1', ['nope']);
361740b3867SJames Ide          } catch (e) {
362740b3867SJames Ide            error = e;
363740b3867SJames Ide          }
3648cb96155SEvan Bacon          expect(error).toBeTruthy();
365740b3867SJames Ide        };
366740b3867SJames Ide
367740b3867SJames Ide        await checkRoot(dir);
368740b3867SJames Ide
369740b3867SJames Ide        await FS.deleteAsync(FS.documentDirectory + 'moved', {
370740b3867SJames Ide          idempotent: true,
371740b3867SJames Ide        });
372740b3867SJames Ide        await FS.moveAsync({ from: dir, to: FS.documentDirectory + 'moved' });
373740b3867SJames Ide        await checkRoot(FS.documentDirectory + 'moved');
374740b3867SJames Ide        await FS.copyAsync({
375740b3867SJames Ide          from: FS.documentDirectory + 'moved',
376740b3867SJames Ide          to: FS.documentDirectory + 'copied',
377740b3867SJames Ide        });
378740b3867SJames Ide        await checkRoot(FS.documentDirectory + 'copied');
379740b3867SJames Ide      }
380740b3867SJames Ide    );
381fec220cbSSergei Chestakov    */
382740b3867SJames Ide
3838cb96155SEvan Bacon    it('delete(idempotent) -> download(md5) -> getInfo(size)', async () => {
384740b3867SJames Ide      const localUri = FS.documentDirectory + 'download1.png';
385740b3867SJames Ide
386740b3867SJames Ide      await FS.deleteAsync(localUri, { idempotent: true });
387740b3867SJames Ide
388d12a8680SŁukasz Kosmaty      const {
389d12a8680SŁukasz Kosmaty        md5,
390d12a8680SŁukasz Kosmaty      } = await FS.downloadAsync(
391740b3867SJames Ide        'https://s3-us-west-1.amazonaws.com/test-suite-data/avatar2.png',
392740b3867SJames Ide        localUri,
393740b3867SJames Ide        { md5: true }
394740b3867SJames Ide      );
3958cb96155SEvan Bacon      expect(md5).toBe('1e02045c10b8f1145edc7c8375998f87');
396740b3867SJames Ide
397740b3867SJames Ide      const { size, modificationTime } = await FS.getInfoAsync(localUri);
3988cb96155SEvan Bacon      expect(size).toBe(3230);
399740b3867SJames Ide      const nowTime = 0.001 * new Date().getTime();
4008cb96155SEvan Bacon      expect(nowTime - modificationTime).toBeLessThan(3600);
401740b3867SJames Ide
402740b3867SJames Ide      await FS.deleteAsync(localUri);
4038cb96155SEvan Bacon    }, 30000);
404740b3867SJames Ide
4058cb96155SEvan Bacon    it('missing parameters', async () => {
406740b3867SJames Ide      const p = FS.documentDirectory + 'test';
407740b3867SJames Ide
408740b3867SJames Ide      await throws(() => FS.moveAsync({ from: p }));
409740b3867SJames Ide      await throws(() => FS.moveAsync({ to: p }));
410740b3867SJames Ide      await throws(() => FS.copyAsync({ from: p }));
411740b3867SJames Ide      await throws(() => FS.copyAsync({ to: p }));
412740b3867SJames Ide    });
413740b3867SJames Ide
4148cb96155SEvan Bacon    it('can read root directories', async () => {
415740b3867SJames Ide      await FS.readDirectoryAsync(FS.documentDirectory);
416740b3867SJames Ide      await FS.readDirectoryAsync(FS.cacheDirectory);
417740b3867SJames Ide    });
418740b3867SJames Ide
4198cb96155SEvan Bacon    it('download(network failure)', async () => {
420740b3867SJames Ide      const localUri = FS.documentDirectory + 'download1.png';
421740b3867SJames Ide
422740b3867SJames Ide      const assertExists = async expectedToExist => {
423*19cbf4dcSBartłomiej Bukowski        const { exists } = await FS.getInfoAsync(localUri);
424740b3867SJames Ide        if (expectedToExist) {
4258cb96155SEvan Bacon          expect(exists).toBeTruthy();
426740b3867SJames Ide        } else {
4278cb96155SEvan Bacon          expect(exists).not.toBeTruthy();
428740b3867SJames Ide        }
429740b3867SJames Ide      };
430740b3867SJames Ide
431740b3867SJames Ide      await FS.deleteAsync(localUri, { idempotent: true });
432740b3867SJames Ide      await assertExists(false);
433740b3867SJames Ide
434740b3867SJames Ide      let error;
435740b3867SJames Ide      try {
4363ba2b94bSŁukasz Kosmaty        await FS.downloadAsync('https://nonexistent-subdomain.expo.io', localUri, {
4373ba2b94bSŁukasz Kosmaty          md5: true,
4383ba2b94bSŁukasz Kosmaty          sessionType: FS.FileSystemSessionType.FOREGROUND,
4393ba2b94bSŁukasz Kosmaty        });
440740b3867SJames Ide      } catch (e) {
441740b3867SJames Ide        error = e;
442740b3867SJames Ide      }
4438cb96155SEvan Bacon      expect(error).toBeTruthy();
444740b3867SJames Ide      await assertExists(false);
445740b3867SJames Ide      await FS.deleteAsync(localUri, { idempotent: true });
4468cb96155SEvan Bacon    }, 30000);
447740b3867SJames Ide
4488cb96155SEvan Bacon    it('download(404)', async () => {
449740b3867SJames Ide      const localUri = FS.documentDirectory + 'download1.png';
450740b3867SJames Ide
451740b3867SJames Ide      const assertExists = async expectedToExist => {
452*19cbf4dcSBartłomiej Bukowski        const { exists } = await FS.getInfoAsync(localUri);
453740b3867SJames Ide        if (expectedToExist) {
4548cb96155SEvan Bacon          expect(exists).toBeTruthy();
455740b3867SJames Ide        } else {
4568cb96155SEvan Bacon          expect(exists).not.toBeTruthy();
457740b3867SJames Ide        }
458740b3867SJames Ide      };
459740b3867SJames Ide
460740b3867SJames Ide      await FS.deleteAsync(localUri, { idempotent: true });
461740b3867SJames Ide      await assertExists(false);
462740b3867SJames Ide
463df3ae3b5Sandy      const { status } = await FS.downloadAsync('https://github.com/omg1231sdfaljs', localUri, {
464740b3867SJames Ide        md5: true,
465740b3867SJames Ide      });
466740b3867SJames Ide      await assertExists(true);
4678cb96155SEvan Bacon      expect(status).toBe(404);
468740b3867SJames Ide
469740b3867SJames Ide      await FS.deleteAsync(localUri);
470740b3867SJames Ide      await assertExists(false);
4718cb96155SEvan Bacon    }, 30000);
472d7aba270SDominik Sokal
4738cb96155SEvan Bacon    it('download(nonexistent local path)', async () => {
474d7aba270SDominik Sokal      try {
475d7aba270SDominik Sokal        const remoteUrl = 'https://s3-us-west-1.amazonaws.com/test-suite-data/avatar2.png';
476d7aba270SDominik Sokal        const localUri = FS.documentDirectory + 'doesnt/exists/download1.png';
477d7aba270SDominik Sokal        await FS.downloadAsync(remoteUrl, localUri);
478d7aba270SDominik Sokal      } catch (err) {
4798cb96155SEvan Bacon        expect(err.message).toMatch(/Directory for .* doesn't exist/);
480d7aba270SDominik Sokal      }
4818cb96155SEvan Bacon    }, 30000);
482d7aba270SDominik Sokal
4838cb96155SEvan Bacon    it('mkdir(multi-level) + download(multi-level local path)', async () => {
484d7aba270SDominik Sokal      const remoteUrl = 'https://s3-us-west-1.amazonaws.com/test-suite-data/avatar2.png';
485d7aba270SDominik Sokal      const localDirUri = FS.documentDirectory + 'foo/bar/baz';
486d7aba270SDominik Sokal      const localFileUri = localDirUri + 'download1.png';
487d7aba270SDominik Sokal
488d7aba270SDominik Sokal      await FS.makeDirectoryAsync(localDirUri, { intermediates: true });
489d7aba270SDominik Sokal
490d7aba270SDominik Sokal      await FS.downloadAsync(remoteUrl, localFileUri);
4918cb96155SEvan Bacon    }, 30000);
492740b3867SJames Ide  });
493740b3867SJames Ide}
494