2e066d219315bf8a41286b90f23cd7f2a83b88db
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 { fileScope
} = require('../../lib/common');
9 const assert
= require('assert');
10 const sinon
= require('sinon');
12 const snooze
= async (ms
) => new Promise((resolve
) => setTimeout(resolve
, ms
));
13 const expectedException
= new Error('oh no');
15 describe('Chores', function () {
16 let chores
, logger
, task
;
17 beforeEach(function () {
21 logger
= new StubLogger();
24 chores
= new Chores(logger
);
25 task
= sinon
.spy((...args
) => {
26 logger
.debug('task', 'called', { args
});
29 afterEach(function () {
30 chores
.stopAllChores();
34 describe('_getChore', function () {
35 it('covers failure', function () {
36 assert
.throws(() => chores
._getChore('no chore'), ChoreError
);
38 it('covers success', function () {
39 chores
.establishChore('chore1', task
, 0);
40 const chore
= chores
._getChore('chore1');
45 describe('establishChore', function () {
46 it('establishes non-recurring chore', async
function () {
47 chores
.establishChore('chore1', task
, 0);
49 assert(task
.notCalled
);
51 it('refuses to establish same chore twice', function () {
52 const choreName
= 'chore1';
53 chores
.establishChore(choreName
, task
, 0);
54 assert
.throws(() => chores
.establishChore(choreName
, task
, 0), ChoreError
);
56 it('establishes recurring chore', async
function () {
57 chores
.establishChore('chore1', task
, 10);
63 describe('runChore', function () {
64 it('requires existing chore', async
function () {
65 await assert
.rejects(chores
.runChore('missingChore'), ChoreError
);
67 it('runs non-recurring chore', async
function () {
68 chores
.establishChore('chore1', task
, 0);
69 chores
.runChore('chore1');
72 it('does not overlap runs', async
function () {
73 const longTask
= sinon
.spy(async (...args
) => {
75 logger
.debug('task', 'called', { args
});
77 chores
.establishChore('longChore', longTask
);
78 chores
.runChore('longChore');
79 chores
.runChore('longChore');
81 assert(longTask
.called
);
82 assert
.strictEqual(longTask
.callCount
, 1);
84 it('covers recurring task exception', async
function () {
85 const failTask
= sinon
.spy(function () {
86 throw expectedException
;
88 chores
.establishChore('failChore', failTask
, 10);
90 assert(failTask
.called
);
92 it('covers called task exception', async
function () {
93 const failTask
= sinon
.spy(function () {
94 throw expectedException
;
96 chores
.establishChore('failChore', failTask
, 0);
97 await assert
.rejects(chores
.runChore('failChore', 'argument', 'argument'), expectedException
);
101 describe('miscellaneous coverage', function () {
102 it('names error', function () {
103 const e
= new ChoreError();
104 assert
.strictEqual(e
.name
, 'ChoreError');
106 it('covers unused log helper line', function () {
107 const fs
= fileScope('/foo/bar/index.js');
109 assert(s
.endsWith(':bar:m'), s
);