2 /* eslint-disable node/no-unpublished-require */
5 const Chores
= require('../../lib/chores');
6 const { ChoreError
} = require('../../lib/errors');
7 const { StubLogger
} = require('@squeep/test-helper');
8 const assert
= require('assert');
9 const sinon
= require('sinon');
11 const snooze
= async (ms
) => new Promise((resolve
) => setTimeout(resolve
, ms
));
12 const expectedException
= new Error('oh no');
14 describe('Chores', function () {
15 let chores
, logger
, task
;
16 beforeEach(function () {
20 logger
= new StubLogger();
23 chores
= new Chores(logger
);
24 task
= sinon
.spy((...args
) => {
25 logger
.debug('task', 'called', { args
});
28 afterEach(function () {
29 chores
.stopAllChores();
33 describe('_getChore', function () {
34 it('covers failure', function () {
35 assert
.throws(() => chores
._getChore('no chore'), ChoreError
);
37 it('covers success', function () {
38 chores
.establishChore('chore1', task
, 0);
39 const chore
= chores
._getChore('chore1');
44 describe('establishChore', function () {
45 it('establishes non-recurring chore', async
function () {
46 chores
.establishChore('chore1', task
, 0);
48 assert(task
.notCalled
);
50 it('refuses to establish same chore twice', function () {
51 const choreName
= 'chore1';
52 chores
.establishChore(choreName
, task
, 0);
53 assert
.throws(() => chores
.establishChore(choreName
, task
, 0), ChoreError
);
55 it('establishes recurring chore', async
function () {
56 chores
.establishChore('chore1', task
, 10);
62 describe('runChore', function () {
63 it('requires existing chore', async
function () {
64 await assert
.rejects(chores
.runChore('missingChore'), ChoreError
);
66 it('runs non-recurring chore', async
function () {
67 chores
.establishChore('chore1', task
, 0);
68 chores
.runChore('chore1');
71 it('does not overlap runs', async
function () {
72 const longTask
= sinon
.spy(async (...args
) => {
74 logger
.debug('task', 'called', { args
});
76 chores
.establishChore('longChore', longTask
);
77 chores
.runChore('longChore');
78 chores
.runChore('longChore');
80 assert(longTask
.called
);
81 assert
.strictEqual(longTask
.callCount
, 1);
83 it('covers recurring task exception', async
function () {
84 const failTask
= sinon
.spy(function () {
85 throw expectedException
;
87 chores
.establishChore('failChore', failTask
, 10);
89 assert(failTask
.called
);
91 it('covers called task exception', async
function () {
92 const failTask
= sinon
.spy(function () {
93 throw expectedException
;
95 chores
.establishChore('failChore', failTask
, 0);
96 await assert
.rejects(chores
.runChore('failChore', 'argument', 'argument'), expectedException
);
100 describe('miscellaneous coverage', function () {
101 it('names error', function () {
102 const e
= new ChoreError();
103 assert
.strictEqual(e
.name
, 'ChoreError');