const expect = require('chai').expect; const sandbox = require("sinon").createSandbox(); const ChatMessages = require('../dist/build'); const rsClient = { declareType: function() {}, getObject: function() {}, getListing: function() {}, storeObject: function() {}, remove: function() {} } describe('ChatMessages', function () { describe('constructor', function () { let chatMessages; before(function() { chatMessages = new ChatMessages.builder(rsClient, rsClient); }); it('behaves like a remoteStorage module', function () { expect(chatMessages).to.be.an('object'); expect(chatMessages.exports).to.be.an('object'); }); it('exports the desired functionality', function () { expect(chatMessages.exports.DailyArchive).to.be.a('function'); }); }); describe('DailyArchive', function () { let archive; before(function() { chatMessages = (new ChatMessages.builder(rsClient, rsClient)).exports; archive = new chatMessages.DailyArchive({ service: { protocol: 'IRC', domain: 'irc.libera.chat' }, channelName: '#kosmos', date: new Date('2022-08-11') }); }); describe('constructor', function () { it('creates an archive instance with the desired properties', function () { expect(archive).to.be.an('object'); expect(archive.service.protocol).to.eq('IRC'); expect(archive.service.domain).to.eq('irc.libera.chat'); expect(archive.channelName).to.eq('#kosmos'); expect(archive.channelType).to.eq('room'); expect(archive.date).to.be.a('date'); expect(archive.parsedDate.year).to.eq(2022); expect(archive.parsedDate.month).to.eq('08'); expect(archive.parsedDate.day).to.eq('11'); expect(archive.dateId).to.eq('2022/08/11'); expect(archive.isPublic).to.eq(false); expect(archive.channelPath).to.eq('irc.libera.chat/channels/kosmos'); expect(archive.path).to.eq('irc.libera.chat/channels/kosmos/2022/08/11'); expect(archive.metaPath).to.eq('irc.libera.chat/channels/kosmos/meta'); expect(archive.client).to.eq(rsClient); expect(archive.previous).to.be.an('undefined'); expect(archive.next).to.be.an('undefined'); }); }); describe('#_updateArchiveMetaDocument', function () { describe('meta up to date', function () { before(function() { sandbox.stub(archive.client, 'getObject').withArgs(archive.metaPath) .returns({ '@id': `chat-messages/irc.libera.chat/channels/kosmos/meta`, '@type': 'ChatChannelMeta', first: '2021/01/01', last: '2022/08/11' }) sandbox.stub(archive.client, 'storeObject'); }); it('does not store a new archive', async function () { await archive._updateArchiveMetaDocument(); sandbox.assert.notCalled(archive.client.storeObject); }); after(function() { sandbox.restore() }); }); describe('meta needs updating', function () { before(function() { sandbox.stub(archive.client, 'getObject').withArgs(archive.metaPath) .returns({ '@id': archive.metaPath, '@type': 'ChatChannelMeta', first: '2021/01/01', last: '2022/08/10' }) sandbox.stub(archive.client, 'storeObject'); }); it('stores a new archive', async function () { await archive._updateArchiveMetaDocument(); sandbox.assert.calledWithMatch( archive.client.storeObject, 'daily-archive-meta', archive.metaPath, { '@id': archive.metaPath, '@type': 'ChatChannelMeta', first: '2021/01/01', last: '2022/08/11' } ); }); after(function() { sandbox.restore() }); }); }); }); });