redeem proffered tickets, db schema 1.1.0
[squeep-indie-auther] / test / src / chores.js
index 51c015ceabae23661489713942ace123b19944f7..02729908033803e449d340c1a928cf4656a5a0f9 100644 (file)
@@ -13,12 +13,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 +29,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 +44,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 +59,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 +94,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 +109,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 +133,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 +145,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