async function testAnalytics() {  
  let alice = await insertUser("Alice", "testPassword", "normal");
  let link1 = "https://otee.dev/2021/11/03/diwali-hack-week.html";
  let link2 = "https://github.com/oitee";
  let link3 = "https://google.com";
  let shortLink1Alice = await shortenLink(alice, link1);
  let shortLink3Alice = await shortenLink(alice, link3);
 
  for (let i = 0; i < 10; i++) {
    await expandLink(shortLink3Alice.shortLink);
  }

  // The idea, here, is to shorten link2 last.
  // SetTimeout does not return a promise
  // Promise constructor accepts a call back function
  // this call-back fn expects two functions, res and rej
  // the invocation of the res fn, implies completion (or resulution) of the promise
  // this is why, we pass the result of shortening to res, which is inside setTimeout

  await shortenLink(alice, link2)

  let mockReq = { twirlUser: { id: alice } };
  let analyticsResult;
  let mockRes = { send: (object) => (analyticsResult = object.data) };

  await analytics(mockReq, mockRes);

  assert.equal(analyticsResult[0]["original_link"], link3);
  assert.equal(analyticsResult[1]["original_link"], link2);
  assert.equal(analyticsResult[2]["original_link"], link1);
  assert.equal(analyticsResult[0]["accessed_count"], 10);
  assert.equal(analyticsResult[1]["accessed_count"], 0);
  assert.equal(analyticsResult[2]["accessed_count"], 0);

  //----------------------------------------------------------------
  // ENABLE/DISABLE LINKS
  //----------------------------------------------------------------

  let updateStatus;

  let mockReq2 = {
    params: { id: shortLink3Alice.shortLink },
    twirlUser: { id: alice },
  };
  let mockRes2 = { send: (object) => (updateStatus = object.status) };

  await disableLink(mockReq2, mockRes2);
  assert(updateStatus, "Status for disabling shortened link should be true");

  await analytics(mockReq, mockRes);
  assert(!analyticsResult[0]["enabled"]);
  assert(analyticsResult[1]["enabled"]);
  assert(analyticsResult[2]["enabled"]);

  mockReq2.params.id = "abcd1??";
  await disableLink(mockReq2, mockRes2);
  assert(!updateStatus, "Status for disabling invalid link should be false");

  mockReq2.params.id = shortLink3Alice.shortLink;
  await enableLink(mockReq2, mockRes2);
  assert(updateStatus, "Status for enabling shortened link should be true");

  await analytics(mockReq, mockRes);
  assert(analyticsResult[0]["enabled"]);
  assert(analyticsResult[1]["enabled"]);
  assert(analyticsResult[2]["enabled"]);

  mockReq2.params.id = "abcd1??";
  await enableLink(mockReq2, mockRes2);
  assert(!updateStatus, "Status for enabling invalid link should be false");
}
/*
  Create two users
  For user1, shorten link1
  For user1, shorten link1 again
  Check count is zero for link 1
  Expand link1
  Check count of link1 = 1
  Check user2 can shorten link1 and shortened Links are not equal
  Check second shorten link has count zero
  Check that user 1 can shorten link2 with count zero
  Expand short link1 and short link2
*/
Create two users
For user1, shorten link1
For user1, shorten link1 again
Check count is zero for link 1
Expand link1
Check count of link1 = 1
Check user2 can shorten link1 and shortened Links are not equal
Check second shorten link has count zero
Check that user 1 can shorten link2 with count zero
Expand short link1 and short link2
Create two users For user1 shorten link1 For user1 shorten link1 again Check count is zero for link 1 Expand link1 Check count of link1 = 1 Check user2 can shorten link1 and shortened Links are not equal Check second shorten link has count zero Check that user 1 can shorten link2 with count zero Expand short link1 and short link2
async function testAnalytics() {  
  let alice = await insertUser("Alice", "testPassword", "normal");
  let link1 = "https://otee.dev/2021/11/03/diwali-hack-week.html";
  let link2 = "https://github.com/oitee";
  let link3 = "https://google.com";
  let shortLink1Alice = await shortenLink(alice, link1);
  let shortLink3Alice = await shortenLink(alice, link3);
  for (let i = 0; i < 10; i++) {
    await expandLink(shortLink3Alice.shortLink);
  }
  await shortenLink(alice, link2)
  let mockReq = { twirlUser: { id: alice } };
  let analyticsResult;
  let mockRes = { send: (object) => (analyticsResult = object.data) };
  await analytics(mockReq, mockRes);
  assert.equal(analyticsResult[0]["original_link"], link3);
  assert.equal(analyticsResult[1]["original_link"], link2);
  assert.equal(analyticsResult[2]["original_link"], link1);
  assert.equal(analyticsResult[0]["accessed_count"], 10);
  assert.equal(analyticsResult[1]["accessed_count"], 0);
  assert.equal(analyticsResult[2]["accessed_count"], 0);
  let updateStatus;
  let mockReq2 = {
    params: { id: shortLink3Alice.shortLink },
    twirlUser: { id: alice },
  };
  let mockRes2 = { send: (object) => (updateStatus = object.status) };
  await disableLink(mockReq2, mockRes2);
  assert(updateStatus, "Status for disabling shortened link should be true");
  await analytics(mockReq, mockRes);
  assert(!analyticsResult[0]["enabled"]);
  assert(analyticsResult[1]["enabled"]);
  assert(analyticsResult[2]["enabled"]);
  mockReq2.params.id = "abcd1??";
  await disableLink(mockReq2, mockRes2);
  assert(!updateStatus, "Status for disabling invalid link should be false");
  mockReq2.params.id = shortLink3Alice.shortLink;
  await enableLink(mockReq2, mockRes2);
  assert(updateStatus, "Status for enabling shortened link should be true");
  await analytics(mockReq, mockRes);
  assert(analyticsResult[0]["enabled"]);
  assert(analyticsResult[1]["enabled"]);
  assert(analyticsResult[2]["enabled"]);
  mockReq2.params.id = "abcd1??";
  await enableLink(mockReq2, mockRes2);
  assert(!updateStatus, "Status for enabling invalid link should be false");
}
async function testAnalytics ( ) { let alice = await insertUser ( "Alice" , "testPassword" , "normal" ) ; let link1 = "https://otee.dev/2021/11/03/diwali-hack-week.html" ; let link2 = "https://github.com/oitee" ; let link3 = "https://google.com" ; let shortLink1Alice = await shortenLink ( alice , link1 ) ; let shortLink3Alice = await shortenLink ( alice , link3 ) ; for ( let i = 0 ; i < 10 ; i ++ ) { await expandLink ( shortLink3Alice . shortLink ) ; } await shortenLink ( alice , link2 ) let mockReq = { twirlUser : { id : alice } } ; let analyticsResult ; let mockRes = { send : ( object ) => ( analyticsResult = object . data ) } ; await analytics ( mockReq , mockRes ) ; assert . equal ( analyticsResult [ 0 ] [ "original_link" ] , link3 ) ; assert . equal ( analyticsResult [ 1 ] [ "original_link" ] , link2 ) ; assert . equal ( analyticsResult [ 2 ] [ "original_link" ] , link1 ) ; assert . equal ( analyticsResult [ 0 ] [ "accessed_count" ] , 10 ) ; assert . equal ( analyticsResult [ 1 ] [ "accessed_count" ] , 0 ) ; assert . equal ( analyticsResult [ 2 ] [ "accessed_count" ] , 0 ) ; let updateStatus ; let mockReq2 = { params : { id : shortLink3Alice . shortLink } , twirlUser : { id : alice } , } ; let mockRes2 = { send : ( object ) => ( updateStatus = object . status ) } ; await disableLink ( mockReq2 , mockRes2 ) ; assert ( updateStatus , "Status for disabling shortened link should be true" ) ; await analytics ( mockReq , mockRes ) ; assert ( ! analyticsResult [ 0 ] [ "enabled" ] ) ; assert ( analyticsResult [ 1 ] [ "enabled" ] ) ; assert ( analyticsResult [ 2 ] [ "enabled" ] ) ; mockReq2 . params . id = "abcd1??" ; await disableLink ( mockReq2 , mockRes2 ) ; assert ( ! updateStatus , "Status for disabling invalid link should be false" ) ; mockReq2 . params . id = shortLink3Alice . shortLink ; await enableLink ( mockReq2 , mockRes2 ) ; assert ( updateStatus , "Status for enabling shortened link should be true" ) ; await analytics ( mockReq , mockRes ) ; assert ( analyticsResult [ 0 ] [ "enabled" ] ) ; assert ( analyticsResult [ 1 ] [ "enabled" ] ) ; assert ( analyticsResult [ 2 ] [ "enabled" ] ) ; mockReq2 . params . id = "abcd1??" ; await enableLink ( mockReq2 , mockRes2 ) ; assert ( ! updateStatus , "Status for enabling invalid link should be false" ) ; }
Create two users
For user1, shorten link1
For user1, shorten link1 again
Check count is zero for link 1
Expand link1
Check count of link1 = 1
Check user2 can shorten link1 and shortened Links are not equal
Check second shorten link has count zero
Check that user 1 can shorten link2 with count zero
Expand short link1 and short link2
Create two users For user1 shorten link1 For user1 shorten link1 again Check count is zero for link 1 Expand link1 Check count of link1 = 1 Check user2 can shorten link1 and shortened Links are not equal Check second shorten link has count zero Check that user 1 can shorten link2 with count zero Expand short link1 and short link2
// The idea, here, is to shorten link2 last. // SetTimeout does not return a promise // Promise constructor accepts a call back function // this call-back fn expects two functions, res and rej // the invocation of the res fn, implies completion (or resulution) of the promise // this is why, we pass the result of shortening to res, which is inside setTimeout //---------------------------------------------------------------- // ENABLE/DISABLE LINKS //----------------------------------------------------------------