var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { expect } from 'chai';
import { TestDataGenerator } from '../../../utils/test-data-generator.js';
import { DataStoreLevel, DidKeyResolver, DidResolver, Dwn, EventLogLevel, MessageStoreLevel, } from '../../../../src/index.js';
import { Message } from '../../../../src/core/message.js';
describe('EventsGetHandler.handle()', () => {
    if (process.env.TESTSOFF === 'true') {
        return;
    }
    let didResolver;
    let messageStore;
    let dataStore;
    let eventLog;
    let dwn;
    before(() => __awaiter(void 0, void 0, void 0, function* () {
        didResolver = new DidResolver([new DidKeyResolver()]);
        // important to follow this pattern to initialize and clean the message and data store in tests
        // so that different suites can reuse the same block store and index location for testing
        messageStore = new MessageStoreLevel({
            blockstoreLocation: 'tests/data/TEST-MESSAGESTORE',
            indexLocation: 'tests/data/TEST-INDEX'
        });
        dataStore = new DataStoreLevel({
            blockstoreLocation: 'tests/data/TEST-DATASTORE'
        });
        eventLog = new EventLogLevel({
            location: 'tests/data/TEST-EVENTLOG'
        });
        dwn = yield Dwn.create({ didResolver, messageStore, dataStore, eventLog });
    }));
    beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
        // clean up before each test rather than after so that a test does not depend on other tests to do the clean up
        yield messageStore.clear();
        yield dataStore.clear();
        yield eventLog.clear();
    }));
    after(() => __awaiter(void 0, void 0, void 0, function* () {
        yield dwn.close();
    }));
    it('returns a 401 if tenant is not author', () => __awaiter(void 0, void 0, void 0, function* () {
        const alice = yield DidKeyResolver.generate();
        const bob = yield DidKeyResolver.generate();
        const { message } = yield TestDataGenerator.generateEventsGet({ author: alice });
        const reply = yield dwn.processMessage(bob.did, message);
        expect(reply.status.code).to.equal(401);
        expect(reply.entries).to.not.exist;
        expect(reply.data).to.not.exist;
    }));
    it('returns a 400 if message is invalid', () => __awaiter(void 0, void 0, void 0, function* () {
        const alice = yield DidKeyResolver.generate();
        const { message } = yield TestDataGenerator.generateEventsGet({ author: alice });
        // message['descriptor']['troll'] = 'hehe';
        message['descriptor']['troll'] = 'hehe';
        const reply = yield dwn.processMessage(alice.did, message);
        expect(reply.status.code).to.equal(400);
        expect(reply.entries).to.not.exist;
        expect(reply.data).to.not.exist;
    }));
    it('returns all events for a tenant if watermark is not provided', () => __awaiter(void 0, void 0, void 0, function* () {
        var _a;
        const alice = yield DidKeyResolver.generate();
        const expectedCids = [];
        for (let i = 0; i < 5; i += 1) {
            const { message, dataStream } = yield TestDataGenerator.generateRecordsWrite({ author: alice });
            const reply = yield dwn.processMessage(alice.did, message, dataStream);
            expect(reply.status.code).to.equal(202);
            const messageCid = yield Message.getCid(message);
            expectedCids.push(messageCid);
        }
        const { message } = yield TestDataGenerator.generateEventsGet({ author: alice });
        const reply = yield dwn.processMessage(alice.did, message);
        expect(reply.status.code).to.equal(200);
        // expect(reply['data']).to.not.exist;
        expect(reply.data).to.not.exist;
        expect((_a = reply.events) === null || _a === void 0 ? void 0 : _a.length).to.equal(expectedCids.length);
        for (let i = 0; i < reply.events.length; i += 1) {
            expect(reply.events[i].messageCid).to.equal(expectedCids[i]);
        }
    }));
    it('returns all events after watermark if watermark is provided', () => __awaiter(void 0, void 0, void 0, function* () {
        const alice = yield DidKeyResolver.generate();
        for (let i = 0; i < 5; i += 1) {
            const { message, dataStream } = yield TestDataGenerator.generateRecordsWrite({ author: alice });
            const reply = yield dwn.processMessage(alice.did, message, dataStream);
            expect(reply.status.code).to.equal(202);
        }
        const { message } = yield TestDataGenerator.generateEventsGet({ author: alice });
        let reply = yield dwn.processMessage(alice.did, message);
        expect(reply.status.code).to.equal(200);
        const watermark = reply.events[reply.events.length - 1].watermark;
        const expectedCids = [];
        for (let i = 0; i < 3; i += 1) {
            const { message, dataStream } = yield TestDataGenerator.generateRecordsWrite({ author: alice });
            const reply = yield dwn.processMessage(alice.did, message, dataStream);
            expect(reply.status.code).to.equal(202);
            const messageCid = yield Message.getCid(message);
            expectedCids.push(messageCid);
        }
        const { message: m } = yield TestDataGenerator.generateEventsGet({ author: alice, watermark });
        reply = yield dwn.processMessage(alice.did, m);
        expect(reply.status.code).to.equal(200);
        // expect(reply['data']).to.not.exist;
        expect(reply.data).to.not.exist;
        expect(reply.events.length).to.equal(expectedCids.length);
        for (let i = 0; i < reply.events.length; i += 1) {
            expect(reply.events[i].messageCid).to.equal(expectedCids[i]);
        }
    }));
});
//# sourceMappingURL=events-get.spec.js.map