|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { describe, expect, it } from 'vitest' |
| 7 | + |
| 8 | +import { |
| 9 | + buildSubmitSignaturePayload, |
| 10 | + createBaseSubmitSignaturePayload, |
| 11 | + getEnvelopeSubmitRequests, |
| 12 | + resolveSignSubmissionOutcome, |
| 13 | +} from '../../services/signSubmit' |
| 14 | + |
| 15 | +describe('signSubmit service', () => { |
| 16 | + it('builds payload elements with profileNodeId only when signature creation is enabled', () => { |
| 17 | + const basePayload = createBaseSubmitSignaturePayload({ method: 'clickToSign' }) |
| 18 | + |
| 19 | + const payload = buildSubmitSignaturePayload({ |
| 20 | + basePayload, |
| 21 | + elements: [ |
| 22 | + { elementId: 10, signRequestId: 100, type: 'signature' }, |
| 23 | + { elementId: 20, signRequestId: 200 }, |
| 24 | + ], |
| 25 | + canCreateSignature: true, |
| 26 | + signatures: { |
| 27 | + signature: { |
| 28 | + file: { |
| 29 | + nodeId: 42, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }) |
| 34 | + |
| 35 | + expect(payload).toEqual({ |
| 36 | + method: 'clickToSign', |
| 37 | + elements: [ |
| 38 | + { documentElementId: 10, profileNodeId: 42 }, |
| 39 | + { documentElementId: 20 }, |
| 40 | + ], |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('creates one envelope request per current signer with only matching elements', () => { |
| 45 | + const requests = getEnvelopeSubmitRequests({ |
| 46 | + document: { |
| 47 | + nodeType: 'envelope', |
| 48 | + signers: [ |
| 49 | + { me: true, signRequestId: 10, sign_request_uuid: 'uuid-a' }, |
| 50 | + { me: false, signRequestId: 20, sign_request_uuid: 'uuid-b' }, |
| 51 | + { me: true, signRequestId: 30, sign_request_uuid: 'uuid-c' }, |
| 52 | + ], |
| 53 | + }, |
| 54 | + basePayload: createBaseSubmitSignaturePayload({ method: 'clickToSign' }), |
| 55 | + elements: [ |
| 56 | + { elementId: 101, signRequestId: 10 }, |
| 57 | + { elementId: 202, signRequestId: 20 }, |
| 58 | + { elementId: 303, signRequestId: 30 }, |
| 59 | + ], |
| 60 | + canCreateSignature: false, |
| 61 | + signatures: {}, |
| 62 | + }) |
| 63 | + |
| 64 | + expect(requests).toEqual([ |
| 65 | + { |
| 66 | + signRequestUuid: 'uuid-a', |
| 67 | + payload: { |
| 68 | + method: 'clickToSign', |
| 69 | + elements: [{ documentElementId: 101 }], |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + signRequestUuid: 'uuid-c', |
| 74 | + payload: { |
| 75 | + method: 'clickToSign', |
| 76 | + elements: [{ documentElementId: 303 }], |
| 77 | + }, |
| 78 | + }, |
| 79 | + ]) |
| 80 | + }) |
| 81 | + |
| 82 | + it('creates envelope requests from child file signers when top-level signers are absent', () => { |
| 83 | + const requests = getEnvelopeSubmitRequests({ |
| 84 | + document: { |
| 85 | + nodeType: 'envelope', |
| 86 | + signers: [], |
| 87 | + files: [ |
| 88 | + { |
| 89 | + signers: [ |
| 90 | + { me: true, signRequestId: 10, sign_request_uuid: 'uuid-a' }, |
| 91 | + ], |
| 92 | + }, |
| 93 | + { |
| 94 | + signers: [ |
| 95 | + { me: true, signRequestId: 30, sign_request_uuid: 'uuid-c' }, |
| 96 | + ], |
| 97 | + }, |
| 98 | + ], |
| 99 | + }, |
| 100 | + basePayload: createBaseSubmitSignaturePayload({ method: 'clickToSign' }), |
| 101 | + elements: [ |
| 102 | + { elementId: 101, signRequestId: 10 }, |
| 103 | + { elementId: 303, signRequestId: 30 }, |
| 104 | + ], |
| 105 | + canCreateSignature: false, |
| 106 | + signatures: {}, |
| 107 | + }) |
| 108 | + |
| 109 | + expect(requests).toEqual([ |
| 110 | + { |
| 111 | + signRequestUuid: 'uuid-a', |
| 112 | + payload: { |
| 113 | + method: 'clickToSign', |
| 114 | + elements: [{ documentElementId: 101 }], |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + signRequestUuid: 'uuid-c', |
| 119 | + payload: { |
| 120 | + method: 'clickToSign', |
| 121 | + elements: [{ documentElementId: 303 }], |
| 122 | + }, |
| 123 | + }, |
| 124 | + ]) |
| 125 | + }) |
| 126 | + |
| 127 | + it('prefers a signed outcome over signing in progress and uses the validation uuid from the API', () => { |
| 128 | + const outcome = resolveSignSubmissionOutcome([ |
| 129 | + { |
| 130 | + signRequestUuid: 'fallback-async-uuid', |
| 131 | + result: { |
| 132 | + status: 'signingInProgress', |
| 133 | + data: { |
| 134 | + job: { |
| 135 | + file: { uuid: 'async-validation-uuid' }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + { |
| 141 | + signRequestUuid: 'fallback-signed-uuid', |
| 142 | + result: { |
| 143 | + status: 'signed', |
| 144 | + data: { |
| 145 | + action: 3500, |
| 146 | + file: { uuid: 'validation-envelope-uuid' }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + ]) |
| 151 | + |
| 152 | + expect(outcome).toEqual({ |
| 153 | + type: 'signed', |
| 154 | + payload: { |
| 155 | + action: 3500, |
| 156 | + file: { uuid: 'validation-envelope-uuid' }, |
| 157 | + signRequestUuid: 'validation-envelope-uuid', |
| 158 | + }, |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + it('uses the async validation uuid when only signing in progress is available', () => { |
| 163 | + const outcome = resolveSignSubmissionOutcome([ |
| 164 | + { |
| 165 | + signRequestUuid: 'fallback-uuid', |
| 166 | + result: { |
| 167 | + status: 'signingInProgress', |
| 168 | + data: { |
| 169 | + job: { |
| 170 | + file: { uuid: 'async-validation-uuid' }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + ]) |
| 176 | + |
| 177 | + expect(outcome).toEqual({ |
| 178 | + type: 'signing-started', |
| 179 | + payload: { |
| 180 | + signRequestUuid: 'async-validation-uuid', |
| 181 | + async: true, |
| 182 | + }, |
| 183 | + }) |
| 184 | + }) |
| 185 | +}) |
0 commit comments