51c015ceabae23661489713942ace123b19944f7
[squeep-indie-auther] / test / src / chores.js
1 /* eslint-env mocha */
2 /* eslint-disable node/no-unpublished-require */
3 'use strict';
4
5 const assert = require('assert');
6 const sinon = require('sinon');
7 const StubDB = require('../stub-db');
8 const StubLogger = require('../stub-logger');
9 const Chores = require('../../src/chores');
10
11 const snooze = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
12
13 const expectedException = new Error('oh no');
14
15 describe('Chores', function () {
16 let chores, stubLogger, stubDb, options;
17 beforeEach(function () {
18 stubLogger = new StubLogger();
19 stubLogger._reset();
20 stubDb = new StubDB();
21 stubDb._reset();
22 });
23 afterEach(function () {
24 chores?.stopAllChores();
25 sinon.restore();
26 });
27
28 describe('constructor', function () {
29
30 it('empty options, no cleaning', async function () {
31 options = undefined;
32 chores = new Chores(stubLogger, stubDb, options);
33 assert.strictEqual(chores.chores.cleanTokens.timeoutObj, undefined);
34 assert.strictEqual(chores.chores.cleanScopes.timeoutObj, undefined);
35 });
36
37 it('cleans scopes', async function () {
38 options = {
39 chores: {
40 scopeCleanupMs: 1,
41 },
42 };
43 chores = new Chores(stubLogger, stubDb, options);
44 await snooze(50);
45 assert(chores.chores.cleanScopes.timeoutObj);
46 assert(chores.db.scopeCleanup.called);
47 });
48
49 it('cleans tokens', async function () {
50 options = {
51 chores: {
52 tokenCleanupMs: 1,
53 },
54 manager: {
55 codeValidityTimeoutMs: 10,
56 },
57 };
58 chores = new Chores(stubLogger, stubDb, options);
59 await snooze(50);
60 assert(chores.chores.cleanTokens.timeoutObj);
61 assert(chores.db.tokenCleanup.called);
62 });
63
64 }); // constructor
65
66 describe('cleanTokens', function () {
67 it('logs cleaning', async function () {
68 const cleaned = 10;
69 options = {
70 chores: {
71 tokenCleanupMs: 100,
72 },
73 manager: {
74 codeValidityTimeoutMs: 10,
75 },
76 };
77 stubDb.tokenCleanup.resolves(cleaned);
78 chores = new Chores(stubLogger, stubDb, options);
79 clearTimeout(chores.cleanTokensTimeout);
80 await chores.cleanTokens();
81 assert(stubLogger.info.called);
82 });
83 it('covers failure', async function () {
84 options = {
85 chores: {
86 tokenCleanupMs: 1,
87 },
88 manager: {
89 codeValidityTimeoutMs: 10,
90 },
91 };
92 stubDb.tokenCleanup.rejects(expectedException);
93 chores = new Chores(stubLogger, stubDb, options);
94 await assert.rejects(() => chores.cleanTokens(), expectedException);
95 });
96 it('covers default', async function () {
97 stubDb.tokenCleanup.resolves(0);
98 chores = new Chores(stubLogger, stubDb, {
99 manager: {
100 codeValidityTimeoutMs: 10,
101 },
102 });
103 await chores.cleanTokens();
104 assert(stubDb.tokenCleanup.called);
105 });
106 }); // cleanTokens
107
108 describe('cleanScopes', function () {
109 it('logs cleaning', async function () {
110 const cleaned = 10;
111 options = {
112 chores: {
113 scopeCleanupMs: 100,
114 },
115 };
116 stubDb.scopeCleanup.resolves(cleaned);
117 chores = new Chores(stubLogger, stubDb, options);
118 clearTimeout(chores.cleanScopesTimeout);
119 await chores.cleanScopes();
120 assert(stubLogger.info.called);
121 });
122 it('covers failure', async function () {
123 options = {
124 chores: {
125 scopeCleanupMs: 1,
126 },
127 };
128 stubDb.scopeCleanup.rejects(expectedException);
129 chores = new Chores(stubLogger, stubDb, options);
130 await assert.rejects(() => chores.cleanScopes(), expectedException);
131 });
132 it('covers default', async function () {
133 stubDb.scopeCleanup.resolves(0);
134 chores = new Chores(stubLogger, stubDb, {});
135 await chores.cleanScopes();
136 assert(stubDb.scopeCleanup.called);
137 });
138 }); // cleanScopes
139
140 }); // Chores