|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 LibreSign contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { describe, expect, it } from 'vitest' |
| 7 | + |
| 8 | +import { |
| 9 | + isLoadedValidationEnvelopeDocument, |
| 10 | + isLoadedValidationFileDocument, |
| 11 | + toValidationDocument, |
| 12 | +} from '../../services/validationDocument' |
| 13 | + |
| 14 | +function createValidationPayload(patch: Record<string, unknown> = {}): Record<string, unknown> { |
| 15 | + return { |
| 16 | + id: 100, |
| 17 | + uuid: '550e8400-e29b-41d4-a716-446655440000', |
| 18 | + name: 'contract.pdf', |
| 19 | + statusText: 'Pending', |
| 20 | + nodeId: 100, |
| 21 | + nodeType: 'file', |
| 22 | + signatureFlow: 0, |
| 23 | + docmdpLevel: 0, |
| 24 | + filesCount: 1, |
| 25 | + files: [{ |
| 26 | + id: 100, |
| 27 | + uuid: '550e8400-e29b-41d4-a716-446655440000', |
| 28 | + name: 'contract.pdf', |
| 29 | + status: 1, |
| 30 | + statusText: 'Pending', |
| 31 | + nodeId: 100, |
| 32 | + totalPages: 1, |
| 33 | + size: 10, |
| 34 | + pdfVersion: '1.7', |
| 35 | + signers: [], |
| 36 | + file: '/apps/libresign/p/pdf/550e8400-e29b-41d4-a716-446655440000', |
| 37 | + metadata: { extension: 'pdf', p: 1 }, |
| 38 | + }], |
| 39 | + totalPages: 1, |
| 40 | + size: 10, |
| 41 | + pdfVersion: '1.7', |
| 42 | + created_at: '2026-01-01T00:00:00Z', |
| 43 | + requested_by: { userId: 'creator-user', displayName: 'Creator User' }, |
| 44 | + status: 1, |
| 45 | + signers: [], |
| 46 | + ...patch, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +describe('validationDocument', () => { |
| 51 | + it('normalizes a valid payload to internal document state', () => { |
| 52 | + const normalized = toValidationDocument(createValidationPayload()) |
| 53 | + |
| 54 | + expect(normalized).not.toBeNull() |
| 55 | + expect(normalized?.uuid).toBe('550e8400-e29b-41d4-a716-446655440000') |
| 56 | + expect(normalized?.metadata).toEqual({ extension: 'pdf', p: 1 }) |
| 57 | + expect(normalized?.settings).toEqual(expect.objectContaining({ |
| 58 | + canSign: false, |
| 59 | + canRequestSign: false, |
| 60 | + phoneNumber: '', |
| 61 | + hasSignatureFile: false, |
| 62 | + needIdentificationDocuments: false, |
| 63 | + identificationDocumentsWaitingApproval: false, |
| 64 | + })) |
| 65 | + expect(normalized?.signers).toEqual([]) |
| 66 | + }) |
| 67 | + |
| 68 | + it('fills missing optional fields with internal defaults', () => { |
| 69 | + const payload = createValidationPayload() |
| 70 | + delete payload.signers |
| 71 | + delete payload.settings |
| 72 | + delete payload.metadata |
| 73 | + |
| 74 | + const normalized = toValidationDocument(payload) |
| 75 | + |
| 76 | + expect(normalized).not.toBeNull() |
| 77 | + expect(normalized?.signers).toEqual([]) |
| 78 | + expect(normalized?.metadata).toEqual({ extension: 'pdf', p: 1 }) |
| 79 | + expect(normalized?.settings).toEqual(expect.objectContaining({ |
| 80 | + canSign: false, |
| 81 | + canRequestSign: false, |
| 82 | + phoneNumber: '', |
| 83 | + hasSignatureFile: false, |
| 84 | + needIdentificationDocuments: false, |
| 85 | + identificationDocumentsWaitingApproval: false, |
| 86 | + })) |
| 87 | + }) |
| 88 | + |
| 89 | + it('rejects payload with invalid signer status', () => { |
| 90 | + const normalized = toValidationDocument(createValidationPayload({ |
| 91 | + signers: [{ |
| 92 | + signRequestId: 1, |
| 93 | + displayName: 'Signer', |
| 94 | + email: 'signer@example.com', |
| 95 | + signed: null, |
| 96 | + status: 99, |
| 97 | + statusText: 'Invalid', |
| 98 | + description: null, |
| 99 | + request_sign_date: '2026-01-01T00:00:00Z', |
| 100 | + me: false, |
| 101 | + visibleElements: [], |
| 102 | + }], |
| 103 | + })) |
| 104 | + |
| 105 | + expect(normalized).toBeNull() |
| 106 | + }) |
| 107 | + |
| 108 | + it('narrows loaded document types by nodeType', () => { |
| 109 | + const envelope = toValidationDocument(createValidationPayload({ nodeType: 'envelope' })) |
| 110 | + const file = toValidationDocument(createValidationPayload({ nodeType: 'file' })) |
| 111 | + |
| 112 | + expect(isLoadedValidationEnvelopeDocument(envelope)).toBe(true) |
| 113 | + expect(isLoadedValidationFileDocument(envelope)).toBe(false) |
| 114 | + expect(isLoadedValidationFileDocument(file)).toBe(true) |
| 115 | + expect(isLoadedValidationEnvelopeDocument(file)).toBe(false) |
| 116 | + }) |
| 117 | +}) |
0 commit comments