Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict'
- const fs = require('fs');
- const tape = require('tape');
- const _test = require('tape-promise').default // <---- notice 'default'
- const test = _test(tape) // decorate tape
- const Gun = require('../gun')
- const SEA = require('../gun/sea')
- require('../gun/lib/then.js')
- require('../gun/lib/promise.js')
- const RAD = require('../gun/lib/radisk.js')
- const mkdirp = require('mkdirp');
- const { Random } = require('random-js');
- const random = new Random();
- const _ = require('lodash');
- const sleep = (milliseconds) => {
- return new Promise(resolve => setTimeout(resolve, milliseconds))
- }
- Gun.log.once=function(...args){ console.log(...args)}
- test('test gun.user().get(key)', async function(t) {
- //console.log(Gun.SEA);
- Gun.SEA.throw = true;
- //let t = async function(){
- const key = 'chat';
- const value = {'2021/05/31':{stuff: 'donotshow'}};
- const value2 = {'2021/06/31 01:00:00':{stuff: 'show'}};
- const value3 = {'2021/06/31 01:30:00':{stuff: 'more'}};
- const username = 'paul@example.com';
- const password = 'badpassword';
- mkdirp.sync('testdb');
- // don't use gun's default database, else state is saved across tests, which is an anti-pattern
- const dbfilename = "./testdb/"+random.string(16) + '.json';
- //fs.closeSync(fs.openSync(dbfilename, 'w'));
- console.log('writing to '+dbfilename);
- //await sleep(250);
- let gun = Gun({peers: [], axe:false, multicast: false, file: dbfilename, web: undefined});
- //let peergun1 = Gun({peers: [], multicast: false, file: dbfilename, web: undefined });
- //let peergun2 = Gun({peers: [], multicast: false, file: dbfilename, web: undefined });
- // put some random data to possibly work around abug
- let result = await gun.get(key).put( value).then();
- let result2 = await gun.get(key).put(value2).then()
- let result3 = await gun.get(key).put(value3).then();
- console.log("wrote","result1",result,"value1",value, "result2", result2, "value2",value2, "result3", result3, "value3", value3);
- //await sleep(265);
- // create user doesn't have a once() so then() doesn't work. So wrap in a promise
- /*const ack = await new Promise((res, rej) => {
- gun.user().create(username, password, ack => {
- console.log(ack);
- gun.user().leave();
- res(ack);
- //gun.user().leave();
- });
- });*/
- //console.log("awaited ack",ack,"sleeping");
- //const theSecondDelayResult = await sleep(265);
- //console.log('delay ended. Now attempting to gun.user().get(key).then');
- /*gun.on('auth',async function(){
- let ref = gun.user().get(key);
- console.log("ref",ref);
- let v = await ref.put({property:value},function(ack){ console.log("put ack",ack); }).then();
- console.log("val",v);
- //v;
- const result2 = await gun.user().get(key).then();
- t.equal(result2.property, value, 'result2 was what we put in');
- t.end();
- }); */
- console.log("testing get {'.':{'>':'2021/06'} expecting only one ourput")
- var i = 0;
- var raddy = gun.get(key).get({'.':{'>':'2021/06'},'%':50000}).map(data=>{
- i++;
- console.log("getter found data",data);
- if(i==1) t.equal(data.stuff, "show", "expected result1 to be `show` correct")
- if(i==2) t.equal(data.stuff, "more", "expected result2 to be `more` correct")
- }).once();
- t.end();
- //})
- //await gun.user().auth(username,password,function(ack){ console.log("auth",ack) }).then();
- //return val?true:false;
- });
- // gundb doesn't unref its internal timers and also multicast
- // so the process to exit.
- test('force test to end', async function(t) {
- t.ok(true, 'forcing test to end with process.exit');
- t.end();
- process.exit();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement