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 { Message } from '../../../../src/core/message.js';
describe('RecordsWrite schema definition', () => {
    if (process.env.TESTSOFF === 'true') {
        return;
    }
    it('should allow descriptor with only required properties', () => __awaiter(void 0, void 0, void 0, function* () {
        const validMessage = {
            recordId: 'anyRecordId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z',
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            },
        };
        Message.validateJsonSchema(validMessage);
    }));
    it('should throw if `recordId` is missing', () => __awaiter(void 0, void 0, void 0, function* () {
        const message = {
            descriptor: {
                interface: 'Records',
                method: 'Write',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            },
        };
        expect(() => {
            Message.validateJsonSchema(message);
        }).throws('must have required property \'recordId\'');
    }));
    it('should throw if `authorization` is missing', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('must have required property \'authorization\'');
    });
    it('should throw if unknown property is given in message', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            },
            unknownProperty: 'unknownProperty' // unknown property
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('must NOT have additional properties');
    });
    it('should throw if unknown property is given in the `descriptor`', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z',
                unknownProperty: 'unknownProperty' // unknown property
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            },
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('must NOT have additional properties');
    });
    it('should pass if `protocol` exists and its related properties are all present', () => {
        const validMessage = {
            recordId: 'anyRecordId',
            contextId: 'someContext',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                protocol: 'someProtocolId',
                protocolPath: 'foo/bar',
                schema: 'http://foo.bar/schema',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        Message.validateJsonSchema(validMessage);
    });
    it('should throw if `protocolPath` contains invalid characters', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            contextId: 'someContext',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                protocol: 'http://foo.bar',
                protocolPath: 'invalid:path',
                schema: 'http://foo.bar/schema',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('protocolPath: must match pattern "^[a-zA-Z]+(/[a-zA-Z]+)*$');
    });
    it('should pass if none of `protocol` related properties are present', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        Message.validateJsonSchema(invalidMessage);
    });
    it('should throw if `contextId` is defined but `protocol` is missing', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            contextId: 'invalid',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('must have required property \'protocol\'');
    });
    it('should throw if `protocol` is defined but `contextId` is missing', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                protocol: 'invalid',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('must have required property \'contextId\'');
    });
    it('should throw if `parentId` is defined without other protocol related properties', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            // contextId  : 'anyContextId', // intentionally missing
            descriptor: {
                interface: 'Records',
                method: 'Write',
                // protocol         : 'http://foo.bar', // intentionally missing
                // protocolPath     : 'foo/bar', // intentionally missing
                parentId: 'anyParentId',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('must have property protocol when property parentId is present');
    });
    it('should throw if `protocol` is defined but `protocolPath` is missing', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            contextId: 'anyContextId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                protocol: 'http://foo.bar',
                // protocolPath : 'foo/bar', // intentionally missing
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('descriptor: must have required property \'protocolPath\'');
    });
    it('should throw if `protocolPath` is defined but `protocol` is missing', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            contextId: 'anyContextId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                // protocol     : 'http://foo.bar', // intentionally missing
                protocolPath: 'foo/bar',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('descriptor: must have required property \'protocol\'');
    });
    it('should throw if `protocol` is defined but `schema` is missing', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            contextId: 'anyContextId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                protocol: 'http://foo.bar',
                protocolPath: 'foo/bar',
                // schema       : 'http://foo.bar/schema', // intentionally missing
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('descriptor: must have required property \'schema\'');
    });
    it('should throw if `protocol` is undefined but `recipient` is defined', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            contextId: 'anyContextId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                // protocol     : 'http://foo.bar', // intentionally missing
                recipient: 'did:example:anyone',
                // protocolPath : 'foo/bar', // intentionally missing
                schema: 'http://foo.bar/schema',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('descriptor: must have required property \'protocol\'');
    });
    it('should pass if `protocol` is defined but `recipient` undefined', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            contextId: 'anyContextId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                protocol: 'http://foo.bar',
                // recipient    : 'did:example:anyone', // intentionally missing
                protocolPath: 'foo/bar',
                schema: 'http://foo.bar/schema',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        Message.validateJsonSchema(invalidMessage);
    });
    it('should pass if `protocol` and `recipient` are both defined', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            contextId: 'anyContextId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                protocol: 'http://foo.bar',
                recipient: 'did:example:anyone',
                protocolPath: 'foo/bar',
                schema: 'http://foo.bar/schema',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z'
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        Message.validateJsonSchema(invalidMessage);
    });
    it('should throw if published is false but datePublished is present', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                messageTimestamp: '2022-12-19T10:20:30.123456Z',
                published: false,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                datePublished: '2022-12-19T10:20:30.123456Z' // must not be present when not published
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('published: must be equal to one of the allowed values');
    });
    it('should throw if published is true but datePublished is missing', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z',
                published: true //datePublished must be present
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('must have required property \'datePublished\'');
    });
    it('should throw if published is missing and datePublished is present', () => {
        const invalidMessage = {
            recordId: 'anyRecordId',
            descriptor: {
                interface: 'Records',
                method: 'Write',
                dataCid: 'anyCid',
                dataFormat: 'application/json',
                dataSize: 123,
                dateCreated: '2022-12-19T10:20:30.123456Z',
                messageTimestamp: '2022-12-19T10:20:30.123456Z',
                datePublished: '2022-12-19T10:20:30.123456Z' //published must be present
            },
            authorization: {
                payload: 'anyPayload',
                signatures: [{
                        protected: 'anyProtectedHeader',
                        signature: 'anySignature'
                    }]
            }
        };
        expect(() => {
            Message.validateJsonSchema(invalidMessage);
        }).throws('must have required property \'published\'');
    });
});
//# sourceMappingURL=records-write.spec.js.map