|
| 1 | +import chai, { expect } from 'chai' |
| 2 | +import chaiAsPromised from 'chai-as-promised' |
| 3 | +import Sinon from 'sinon' |
| 4 | + |
| 5 | +import { IKPICollector, SnapshotService } from '../../../src/dashboard-service/services/snapshot-service' |
| 6 | +import { DashboardMetrics } from '../../../src/dashboard-service/types' |
| 7 | + |
| 8 | +chai.use(chaiAsPromised) |
| 9 | + |
| 10 | +const createMetrics = (overrides: Partial<DashboardMetrics> = {}): DashboardMetrics => ({ |
| 11 | + eventsByKind: [], |
| 12 | + admittedUsers: 0, |
| 13 | + satsPaid: 0, |
| 14 | + topTalkers: { |
| 15 | + allTime: [], |
| 16 | + recent: [], |
| 17 | + }, |
| 18 | + ...overrides, |
| 19 | +}) |
| 20 | + |
| 21 | +const makeCollector = (stub: Sinon.SinonStub): IKPICollector => ({ |
| 22 | + collectMetrics: stub, |
| 23 | +}) |
| 24 | + |
| 25 | +describe('SnapshotService', () => { |
| 26 | + let sandbox: Sinon.SinonSandbox |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + sandbox = Sinon.createSandbox() |
| 30 | + }) |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + sandbox.restore() |
| 34 | + }) |
| 35 | + |
| 36 | + it('updates snapshot when collected metrics change', async () => { |
| 37 | + const firstMetrics = createMetrics({ admittedUsers: 1 }) |
| 38 | + const nextMetrics = createMetrics({ admittedUsers: 2 }) |
| 39 | + |
| 40 | + const stub = sandbox.stub() |
| 41 | + .onFirstCall().resolves(firstMetrics) |
| 42 | + .onSecondCall().resolves(firstMetrics) |
| 43 | + .onThirdCall().resolves(nextMetrics) |
| 44 | + |
| 45 | + const service = new SnapshotService(makeCollector(stub)) |
| 46 | + |
| 47 | + const first = await service.refresh() |
| 48 | + expect(first.changed).to.equal(true, 'first refresh should report changed') |
| 49 | + expect(first.snapshot.sequence).to.equal(1) |
| 50 | + expect(first.snapshot.status).to.equal('live') |
| 51 | + |
| 52 | + const second = await service.refresh() |
| 53 | + expect(second.changed).to.equal(false, 'second refresh with same metrics should not change') |
| 54 | + expect(second.snapshot.sequence).to.equal(1, 'sequence must not advance when metrics are unchanged') |
| 55 | + |
| 56 | + const third = await service.refresh() |
| 57 | + expect(third.changed).to.equal(true, 'third refresh with new metrics should report changed') |
| 58 | + expect(third.snapshot.sequence).to.equal(2) |
| 59 | + expect(third.snapshot.metrics.admittedUsers).to.equal(2) |
| 60 | + }) |
| 61 | + |
| 62 | + it('does not advance sequence when metrics are identical across refreshes', async () => { |
| 63 | + const metrics = createMetrics({ satsPaid: 100 }) |
| 64 | + const stub = sandbox.stub().resolves(metrics) |
| 65 | + |
| 66 | + const service = new SnapshotService(makeCollector(stub)) |
| 67 | + |
| 68 | + const first = await service.refresh() |
| 69 | + expect(first.changed).to.equal(true) |
| 70 | + expect(first.snapshot.sequence).to.equal(1) |
| 71 | + |
| 72 | + const second = await service.refresh() |
| 73 | + expect(second.changed).to.equal(false) |
| 74 | + expect(second.snapshot.sequence).to.equal(1) |
| 75 | + }) |
| 76 | + |
| 77 | + it('propagates collector errors to the caller', async () => { |
| 78 | + const stub = sandbox.stub().rejects(new Error('db down')) |
| 79 | + |
| 80 | + const service = new SnapshotService(makeCollector(stub)) |
| 81 | + |
| 82 | + await expect(service.refresh()).to.be.rejectedWith('db down') |
| 83 | + }) |
| 84 | + |
| 85 | + it('returns the last known snapshot via getSnapshot()', async () => { |
| 86 | + const metrics = createMetrics({ admittedUsers: 5 }) |
| 87 | + const stub = sandbox.stub().resolves(metrics) |
| 88 | + |
| 89 | + const service = new SnapshotService(makeCollector(stub)) |
| 90 | + |
| 91 | + await service.refresh() |
| 92 | + |
| 93 | + const snap = service.getSnapshot() |
| 94 | + expect(snap.sequence).to.equal(1) |
| 95 | + expect(snap.status).to.equal('live') |
| 96 | + expect(snap.metrics.admittedUsers).to.equal(5) |
| 97 | + }) |
| 98 | + |
| 99 | + it('sets status to live after a successful refresh', async () => { |
| 100 | + const stub = sandbox.stub().resolves(createMetrics()) |
| 101 | + |
| 102 | + const service = new SnapshotService(makeCollector(stub)) |
| 103 | + |
| 104 | + const { snapshot } = await service.refresh() |
| 105 | + expect(snapshot.status).to.equal('live') |
| 106 | + }) |
| 107 | +}) |
0 commit comments