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 { Jws } from '../../../../src/index.js';
import { Message } from '../../../../src/core/message.js';
import { MessagesGet } from '../../../../src/index.js';
import { TestDataGenerator } from '../../../utils/test-data-generator.js';
describe('MessagesGet Message', () => {
    if (process.env.TESTSOFF === 'true') {
        return;
    }
    describe('create', () => {
        it('creates a MessagesGet message', () => __awaiter(void 0, void 0, void 0, function* () {
            const { author, message } = yield TestDataGenerator.generateRecordsWrite();
            const messageCid = yield Message.getCid(message);
            const messagesGet = yield MessagesGet.create({
                authorizationSignatureInput: yield Jws.createSignatureInput(author),
                messageCids: [messageCid]
            });
            expect(messagesGet.message.authorization).to.exist;
            expect(messagesGet.message.descriptor).to.exist;
            expect(messagesGet.message.descriptor.messageCids.length).to.equal(1);
            expect(messagesGet.message.descriptor.messageCids).to.include(messageCid);
        }));
        it('throws an error if at least 1 message cid isnt provided', () => __awaiter(void 0, void 0, void 0, function* () {
            const alice = yield TestDataGenerator.generatePersona();
            try {
                yield MessagesGet.create({
                    authorizationSignatureInput: yield Jws.createSignatureInput(alice),
                    messageCids: []
                });
                expect.fail();
            }
            catch (e) {
                // error message auto-generated by AJV
                expect(e.message).to.include('/descriptor/messageCids: must NOT have fewer than 1 items');
            }
        }));
        it('throws an error if an invalid CID is provided', () => __awaiter(void 0, void 0, void 0, function* () {
            const alice = yield TestDataGenerator.generatePersona();
            try {
                yield MessagesGet.create({
                    authorizationSignatureInput: yield Jws.createSignatureInput(alice),
                    messageCids: ['abcd']
                });
                expect.fail();
            }
            catch (e) {
                expect(e.message).to.include('is not a valid CID');
            }
        }));
    });
    describe('parse', () => {
        it('parses a message into a MessagesGet instance', () => __awaiter(void 0, void 0, void 0, function* () {
            const { author, message } = yield TestDataGenerator.generateRecordsWrite();
            let messageCid = yield Message.getCid(message);
            const messagesGet = yield MessagesGet.create({
                authorizationSignatureInput: yield Jws.createSignatureInput(author),
                messageCids: [messageCid]
            });
            const parsed = yield MessagesGet.parse(messagesGet.message);
            expect(parsed).to.be.instanceof(MessagesGet);
            const expectedMessageCid = yield Message.getCid(messagesGet.message);
            messageCid = yield Message.getCid(parsed.message);
            expect(messageCid).to.equal(expectedMessageCid);
        }));
        it('throws an exception if messageCids contains an invalid cid', () => __awaiter(void 0, void 0, void 0, function* () {
            const { author, message: recordsWriteMessage } = yield TestDataGenerator.generateRecordsWrite();
            const messageCid = yield Message.getCid(recordsWriteMessage);
            const messagesGet = yield MessagesGet.create({
                authorizationSignatureInput: yield Jws.createSignatureInput(author),
                messageCids: [messageCid]
            });
            const message = messagesGet.toJSON();
            message.descriptor.messageCids = ['abcd'];
            try {
                yield MessagesGet.parse(message);
                expect.fail();
            }
            catch (e) {
                expect(e.message).to.include('is not a valid CID');
            }
        }));
    });
});
//# sourceMappingURL=messages-get.spec.js.map