update dependencies and devDependencies, address lint issues
[squeep-indie-auther] / test / src / chores.js
index 51c015ceabae23661489713942ace123b19944f7..1656088f6e807e738686aba17d638c745f159488 100644 (file)
@@ -1,5 +1,3 @@
-/* eslint-env mocha */
-/* eslint-disable node/no-unpublished-require */
 'use strict';
 
 const assert = require('assert');
@@ -13,12 +11,15 @@ const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
 const expectedException = new Error('oh no');
 
 describe('Chores', function () {
-  let chores, stubLogger, stubDb, options;
+  let chores, stubLogger, stubDb, stubQueuePublisher, options;
   beforeEach(function () {
     stubLogger = new StubLogger();
     stubLogger._reset();
     stubDb = new StubDB();
     stubDb._reset();
+    stubQueuePublisher = {
+      publish: sinon.stub(),
+    };
   });
   afterEach(function () {
     chores?.stopAllChores();
@@ -26,12 +27,13 @@ describe('Chores', function () {
   });
 
   describe('constructor', function () {
-
+    this.slow(200);
     it('empty options, no cleaning', async function () {
       options = undefined;
-      chores = new Chores(stubLogger, stubDb, options);
+      chores = new Chores(stubLogger, stubDb, stubQueuePublisher, options);
       assert.strictEqual(chores.chores.cleanTokens.timeoutObj, undefined);
       assert.strictEqual(chores.chores.cleanScopes.timeoutObj, undefined);
+      assert.strictEqual(chores.chores.publishTickets.timeoutObj, undefined);
     });
 
     it('cleans scopes', async function () {
@@ -40,7 +42,7 @@ describe('Chores', function () {
           scopeCleanupMs: 1,
         },
       };
-      chores = new Chores(stubLogger, stubDb, options);
+      chores = new Chores(stubLogger, stubDb, stubQueuePublisher, options);
       await snooze(50);
       assert(chores.chores.cleanScopes.timeoutObj);
       assert(chores.db.scopeCleanup.called);
@@ -55,12 +57,27 @@ describe('Chores', function () {
           codeValidityTimeoutMs: 10,
         },
       };
-      chores = new Chores(stubLogger, stubDb, options);
+      chores = new Chores(stubLogger, stubDb, stubQueuePublisher, options);
       await snooze(50);
       assert(chores.chores.cleanTokens.timeoutObj);
       assert(chores.db.tokenCleanup.called);
     });
 
+    it('publishes tickets', async function () {
+      options = {
+        chores: {
+          publishTicketsMs: 1,
+        },
+        queues: {
+          ticketRedeemedName: 'queue',
+        },
+      };
+      chores = new Chores(stubLogger, stubDb, stubQueuePublisher, options);
+      await snooze(50);
+      assert(chores.chores.publishTickets.timeoutObj);
+      assert(chores.db.ticketTokenGetUnpublished.called);
+    });
+
   }); // constructor
 
   describe('cleanTokens', function () {
@@ -75,7 +92,7 @@ describe('Chores', function () {
         },
       };
       stubDb.tokenCleanup.resolves(cleaned);
-      chores = new Chores(stubLogger, stubDb, options);
+      chores = new Chores(stubLogger, stubDb, stubQueuePublisher, options);
       clearTimeout(chores.cleanTokensTimeout);
       await chores.cleanTokens();
       assert(stubLogger.info.called);
@@ -90,12 +107,12 @@ describe('Chores', function () {
         },
       };
       stubDb.tokenCleanup.rejects(expectedException);
-      chores = new Chores(stubLogger, stubDb, options);
+      chores = new Chores(stubLogger, stubDb, stubQueuePublisher, options);
       await assert.rejects(() => chores.cleanTokens(), expectedException);
     });
     it('covers default', async function () {
       stubDb.tokenCleanup.resolves(0);
-      chores = new Chores(stubLogger, stubDb, {
+      chores = new Chores(stubLogger, stubDb, stubQueuePublisher, {
         manager: {
           codeValidityTimeoutMs: 10,
         },
@@ -114,7 +131,7 @@ describe('Chores', function () {
         },
       };
       stubDb.scopeCleanup.resolves(cleaned);
-      chores = new Chores(stubLogger, stubDb, options);
+      chores = new Chores(stubLogger, stubDb, stubQueuePublisher, options);
       clearTimeout(chores.cleanScopesTimeout);
       await chores.cleanScopes();
       assert(stubLogger.info.called);
@@ -126,15 +143,43 @@ describe('Chores', function () {
         },
       };
       stubDb.scopeCleanup.rejects(expectedException);
-      chores = new Chores(stubLogger, stubDb, options);
+      chores = new Chores(stubLogger, stubDb, stubQueuePublisher, options);
       await assert.rejects(() => chores.cleanScopes(), expectedException);
     });
     it('covers default', async function () {
       stubDb.scopeCleanup.resolves(0);
-      chores = new Chores(stubLogger, stubDb, {});
+      chores = new Chores(stubLogger, stubDb, stubQueuePublisher, {});
       await chores.cleanScopes();
       assert(stubDb.scopeCleanup.called);
     });
   }); // cleanScopes
 
-}); // Chores
\ No newline at end of file
+  describe('publishTickets', function () {
+    beforeEach(function () {
+      options = {
+        queues: {
+          ticketRedeemedName: 'queue',
+        },
+      };
+      stubDb.ticketTokenGetUnpublished.resolves([{
+        ticket: 'xxxTICKETxxx',
+        resource: 'https://resource.example.com/',
+        subject: 'https://subject.example.com/',
+        iss: null,
+      }]);
+      chores = new Chores(stubLogger, stubDb, stubQueuePublisher, options);
+    });
+    it('publishes a ticket', async function () {
+      await chores.publishTickets();
+      assert(stubQueuePublisher.publish.called);
+      assert(stubDb.ticketTokenPublished.called);
+    });
+    it('covers error', async function () {
+      stubQueuePublisher.publish.rejects(expectedException);
+      await chores.publishTickets();
+      assert(stubQueuePublisher.publish.called);
+      assert(stubDb.ticketTokenPublished.notCalled);
+    });
+  }); // publishTickets
+
+}); // Chores