|
| 1 | +import fs from 'fs/promises'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import t from 'libtap'; |
| 5 | +import {IntegrationInstanceBase} from '@cfware/integration-instance-base'; |
| 6 | + |
| 7 | +class CounterInstance { |
| 8 | + counts = { |
| 9 | + build: 0, |
| 10 | + start: [], |
| 11 | + stop: 0, |
| 12 | + checkStopped: 0 |
| 13 | + }; |
| 14 | + |
| 15 | + async build() { |
| 16 | + this.counts.build++; |
| 17 | + } |
| 18 | + |
| 19 | + async start(...args) { |
| 20 | + this.counts.start.push(args); |
| 21 | + } |
| 22 | + |
| 23 | + async stop() { |
| 24 | + this.counts.stop++; |
| 25 | + } |
| 26 | + |
| 27 | + async checkStopped() { |
| 28 | + this.counts.checkStopped++; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +t.type(IntegrationInstanceBase, 'function'); |
| 33 | + |
| 34 | +t.test('counters', async t => { |
| 35 | + const statuses = []; |
| 36 | + await fs.mkdir('instances', {recursive: true}); |
| 37 | + |
| 38 | + const counter1 = new CounterInstance(); |
| 39 | + const counter2 = new CounterInstance(); |
| 40 | + const integration = new IntegrationInstanceBase(path.resolve('instances'), {counter1, counter2}); |
| 41 | + integration.on('status', status => statuses.push(status)); |
| 42 | + t.same(integration.instances, ['counter1', 'counter2']); |
| 43 | + t.equal(integration.counter1, counter1); |
| 44 | + t.equal(integration.counter2, counter2); |
| 45 | + t.equal(integration.startedMessage, 'Started daemons'); |
| 46 | + // eslint-disable-next-line promise/prefer-await-to-then |
| 47 | + integration.stopped.then(() => { |
| 48 | + statuses.push('resolved stopped'); |
| 49 | + }); |
| 50 | + |
| 51 | + const counts = { |
| 52 | + ...integration.counter1.counts, |
| 53 | + start: [] |
| 54 | + }; |
| 55 | + const checkCounts = () => { |
| 56 | + t.same(integration.counter1.counts, counts); |
| 57 | + t.same(integration.counter2.counts, counts); |
| 58 | + }; |
| 59 | + |
| 60 | + checkCounts(); |
| 61 | + |
| 62 | + await integration.build(); |
| 63 | + counts.build++; |
| 64 | + checkCounts(); |
| 65 | + t.same(statuses, ['Building daemons']); |
| 66 | + statuses.length = 0; |
| 67 | + |
| 68 | + await integration.start('arg1', 'arg2'); |
| 69 | + counts.build++; |
| 70 | + counts.start.push(['arg1', 'arg2']); |
| 71 | + checkCounts(); |
| 72 | + t.same(statuses, [ |
| 73 | + 'Building daemons', |
| 74 | + 'Starting daemons', |
| 75 | + integration.startedMessage |
| 76 | + ]); |
| 77 | + statuses.length = 0; |
| 78 | + |
| 79 | + await integration.stop(); |
| 80 | + counts.stop++; |
| 81 | + checkCounts(); |
| 82 | + t.same(statuses, [ |
| 83 | + 'Stopping daemons', |
| 84 | + 'Stopped daemons', |
| 85 | + 'resolved stopped' |
| 86 | + ]); |
| 87 | + statuses.length = 0; |
| 88 | + |
| 89 | + await integration.checkStopped(); |
| 90 | + counts.checkStopped++; |
| 91 | + checkCounts(); |
| 92 | + t.same(statuses, []); |
| 93 | +}); |
0 commit comments