Browse Source

add test for truncate another peers file

ameba23 3 years ago
parent
commit
d5623607d2
1 changed files with 59 additions and 4 deletions
  1. 59
    4
      test/index.test.js

+ 59
- 4
test/index.test.js View File

@@ -73,12 +73,16 @@ describe('mount', (context) => {
73 73
       mount(drive, storage).then((destroy) => {
74 74
         fs.mkdir(path.join(storage, 'antelopes'), (err) => {
75 75
           assert.error(err, 'no error')
76
-          fs.writeFile(path.join(storage, 'antelopes', 'tree.txt'), 'flim', (err) => {
76
+          fs.readdir(path.join(storage, 'antelopes'), (err, files) => {
77 77
             assert.error(err, 'no error')
78
-            fs.readdir(path.join(storage, 'antelopes'), (err, files) => {
78
+            assert.same(files, [], 'drive.readdir returns empty array for empty directory')
79
+            fs.writeFile(path.join(storage, 'antelopes', 'tree.txt'), 'flim', (err) => {
79 80
               assert.error(err, 'no error')
80
-              assert.same(files, ['tree.txt'], 'drive.readdir returns directory name')
81
-              cleanup(destroy, storage).then(next)
81
+              fs.readdir(path.join(storage, 'antelopes'), (err, files) => {
82
+                assert.error(err, 'no error')
83
+                assert.same(files, ['tree.txt'], 'drive.readdir returns directory name')
84
+                cleanup(destroy, storage).then(next)
85
+              })
82 86
             })
83 87
           })
84 88
         })
@@ -326,6 +330,57 @@ describe('mount', (context) => {
326 330
     }
327 331
   })
328 332
 
333
+  context("replicate - truncate another peer's file - using unix commands", (assert, next) => {
334
+    var feedsOnDisk = {
335
+      orange: tmp(),
336
+      blue: tmp()
337
+    }
338
+    var orangeDrive = KappaDrive(feedsOnDisk.orange)
339
+    var blueDrive = KappaDrive(feedsOnDisk.blue)
340
+
341
+    var storage = {}
342
+    var destroy = {}
343
+
344
+    function setup (drive, colour, cb) {
345
+      storage[colour] = tmp()
346
+      mount(drive, storage[colour]).then((destroyFn) => {
347
+        destroy[colour] = destroyFn
348
+        drive.ready(cb)
349
+      })
350
+    }
351
+
352
+    setup(orangeDrive, 'orange', function () {
353
+      setup(blueDrive, 'blue', function () {
354
+        run(`echo "what noise does a dog make?" > ${storage.orange}/hello.txt`, (err, output) => {
355
+          assert.error(err, 'no error on write')
356
+          // 'seed' the blue drive with a file (TODO this is a hack)
357
+          run(`echo "this file is not needed" > ${storage.blue}/blue.txt`, (err, output) => {
358
+            assert.error(err, 'no error on echo to file')
359
+            replicate(orangeDrive, blueDrive, check)
360
+          })
361
+        })
362
+      })
363
+    })
364
+
365
+    function check (err) {
366
+      assert.error(err, 'no error on replicate')
367
+      run(`ls ${storage.blue}`, (err, output) => {
368
+        assert.error(err, 'no error on ls')
369
+        assert.true(output[0].split('\n').indexOf('hello.txt') > -1, 'file successfully replicated')
370
+        fs.truncate(path.join(storage.blue, 'hello.txt'), 10, (err) => {
371
+          assert.error(err, 'no error on truncate file')
372
+          fs.readFile(path.join(storage.blue, 'hello.txt'), 'utf-8', (err, data) => {
373
+            assert.error(err, 'no error on read file')
374
+            assert.same(data, 'what noise', 'returns expected contents')
375
+            cleanup(destroy.orange, storage.orange).then(() => {
376
+              cleanup(destroy.blue, storage.blue).then(next)
377
+            })
378
+          })
379
+        })
380
+      })
381
+    }
382
+  })
383
+
329 384
   context("replicate - append to another peer's empty file - using unix commands", (assert, next) => {
330 385
     var feedsOnDisk = {
331 386
       orange: tmp(),