Advertisement
Guest User

RAD Test 2

a guest
Sep 25th, 2021
24
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2. const fs = require('fs');
  3. const tape = require('tape');
  4. const _test = require('tape-promise').default // <---- notice 'default'
  5. const test = _test(tape) // decorate tape
  6. const Gun = require('../gun')
  7. const SEA = require('../gun/sea')
  8. require('../gun/lib/then.js')
  9. require('../gun/lib/promise.js')
  10. const RAD = require('../gun/lib/radisk.js')
  11. const mkdirp = require('mkdirp');
  12. const { Random } = require('random-js');
  13. const random = new Random();
  14. const _ = require('lodash');
  15. const sleep = (milliseconds) => {
  16.   return new Promise(resolve => setTimeout(resolve, milliseconds))
  17. }
  18. Gun.log.once=function(...args){ console.log(...args)}
  19. test('test gun.user().get(key)', async function(t) {
  20. //console.log(Gun.SEA);
  21. Gun.SEA.throw = true;
  22. //let t = async function(){
  23.   const key = 'chat';
  24.   const value = {'2021/05/31':{stuff: 'donotshow'}};
  25.   const value2 = {'2021/06/31 01:00:00':{stuff: 'show'}};
  26.   const value3 = {'2021/06/31 01:30:00':{stuff: 'more'}};
  27.   const username = 'paul@example.com';
  28.   const password = 'badpassword';
  29.  
  30.   mkdirp.sync('testdb');  
  31.   // don't use gun's default database, else state is saved across tests, which is an anti-pattern
  32.   const dbfilename = "./testdb/"+random.string(16) + '.json';
  33.   //fs.closeSync(fs.openSync(dbfilename, 'w'));
  34.   console.log('writing to '+dbfilename);
  35.   //await sleep(250);
  36.   let gun = Gun({peers: [], axe:false, multicast: false,  file: dbfilename, web: undefined});
  37.   //let peergun1 = Gun({peers: [], multicast: false, file: dbfilename, web: undefined });
  38.   //let peergun2 = Gun({peers: [], multicast: false, file: dbfilename, web: undefined });
  39.   // put some random data to possibly work around abug
  40.   let result = await gun.get(key).put( value).then();
  41.   let result2 = await gun.get(key).put(value2).then()
  42.   let result3 = await gun.get(key).put(value3).then();
  43.   console.log("wrote","result1",result,"value1",value, "result2", result2, "value2",value2, "result3", result3, "value3", value3);
  44.   //await sleep(265);
  45.  
  46.   // create user doesn't have a once() so then() doesn't work.  So wrap in a promise
  47.   /*const ack = await new Promise((res, rej) => {
  48.     gun.user().create(username, password, ack => {
  49.       console.log(ack);
  50.       gun.user().leave();
  51.       res(ack);
  52.       //gun.user().leave();
  53.     });
  54.   });*/
  55.   //console.log("awaited ack",ack,"sleeping");
  56.   //const theSecondDelayResult = await sleep(265);
  57.   //console.log('delay ended.  Now attempting to gun.user().get(key).then');
  58.  
  59.   /*gun.on('auth',async function(){
  60.     let ref = gun.user().get(key);
  61.     console.log("ref",ref);
  62.     let v = await ref.put({property:value},function(ack){ console.log("put ack",ack); }).then();
  63.     console.log("val",v);
  64.     //v;
  65.     const result2 = await gun.user().get(key).then();
  66.     t.equal(result2.property, value, 'result2  was what we put in');    
  67.     t.end();
  68.   }); */
  69.   console.log("testing get {'.':{'>':'2021/06'} expecting only one ourput")
  70.   var i = 0;
  71.   var raddy = gun.get(key).get({'.':{'>':'2021/06'},'%':50000}).map(data=>{
  72.     i++;
  73.     console.log("getter found data",data);
  74.     if(i==1) t.equal(data.stuff, "show", "expected result1 to be `show` correct")
  75.     if(i==2) t.equal(data.stuff, "more", "expected result2 to be `more` correct")
  76.   }).once();
  77.   t.end();
  78.   //})
  79.   //await gun.user().auth(username,password,function(ack){ console.log("auth",ack) }).then();
  80.   //return val?true:false; 
  81. });
  82.  
  83. // gundb doesn't unref its internal timers and also multicast
  84. // so the process to exit.
  85. test('force test to end', async function(t) {
  86.   t.ok(true, 'forcing test to end with process.exit');
  87.   t.end();
  88.   process.exit();
  89. });
  90.  
Advertisement
RAW Paste Data Copied
Advertisement