feat(sdk): add typescript types actionable errors and mock server tests
This commit is contained in:
@@ -3,10 +3,22 @@ import assert from 'node:assert/strict';
|
||||
import { CyberCoreSigner } from '../src/signer-sdk.js';
|
||||
import {
|
||||
CyberCoreError,
|
||||
AgentNotRunningError,
|
||||
UserCancelledError,
|
||||
CertificateNotFoundError,
|
||||
CertificateExpiredError,
|
||||
CertificateInvalidError,
|
||||
PrivateKeyNotAvailableError,
|
||||
PinRequiredError,
|
||||
PinIncorrectError,
|
||||
TokenNotFoundError,
|
||||
RequestExpiredError,
|
||||
ReplayDetectedError,
|
||||
InvalidOriginError,
|
||||
UnsupportedAlgorithmError,
|
||||
ProviderNotAvailableError,
|
||||
SignatureFailedError,
|
||||
TimeoutError,
|
||||
ApplicationNotAuthorizedError
|
||||
} from '../src/errors.js';
|
||||
|
||||
@@ -25,12 +37,25 @@ describe('@cybercore/signer-sdk - Testes Unitários do SDK', () => {
|
||||
const signer = new CyberCoreSigner({
|
||||
baseUrl: 'http://127.0.0.1:21325/',
|
||||
applicationId: customAppId,
|
||||
applicationName: 'Portal Minha Empresa'
|
||||
applicationName: 'Portal Minha Empresa',
|
||||
timeoutMs: 45000
|
||||
});
|
||||
|
||||
assert.equal(signer.baseUrl, 'http://127.0.0.1:21325');
|
||||
assert.equal(signer.applicationId, customAppId);
|
||||
assert.equal(signer.applicationName, 'Portal Minha Empresa');
|
||||
assert.equal(signer.timeoutMs, 45000);
|
||||
});
|
||||
|
||||
it('deve montar baseUrl automaticamente quando host e port forem especificados', () => {
|
||||
const signer = new CyberCoreSigner({
|
||||
host: 'localhost',
|
||||
port: 29999
|
||||
});
|
||||
|
||||
assert.equal(signer.host, 'localhost');
|
||||
assert.equal(signer.port, 29999);
|
||||
assert.equal(signer.baseUrl, 'http://localhost:29999');
|
||||
});
|
||||
|
||||
it('deve retornar as capacidades criptográficas do agente em getCapabilities()', async () => {
|
||||
@@ -69,6 +94,26 @@ describe('@cybercore/signer-sdk - Testes Unitários do SDK', () => {
|
||||
CertificateNotFoundError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('CERTIFICATE_EXPIRED', 'Certificado expirado'),
|
||||
CertificateExpiredError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('CERTIFICATE_INVALID', 'Certificado inválido'),
|
||||
CertificateInvalidError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('PRIVATE_KEY_NOT_AVAILABLE', 'Sem chave privada'),
|
||||
PrivateKeyNotAvailableError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('PIN_REQUIRED', 'PIN obrigatório'),
|
||||
PinRequiredError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('PIN_INCORRECT', 'PIN errado'),
|
||||
PinIncorrectError
|
||||
@@ -79,6 +124,46 @@ describe('@cybercore/signer-sdk - Testes Unitários do SDK', () => {
|
||||
TokenNotFoundError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('TOKEN_REMOVED', 'Token removido'),
|
||||
TokenNotFoundError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('REQUEST_EXPIRED', 'Expirou'),
|
||||
RequestExpiredError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('REPLAY_DETECTED', 'Replay'),
|
||||
ReplayDetectedError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('INVALID_ORIGIN', 'Origem inválida'),
|
||||
InvalidOriginError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('UNSUPPORTED_ALGORITHM', 'Algoritmo inválido'),
|
||||
UnsupportedAlgorithmError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('PROVIDER_NOT_AVAILABLE', 'Provider indisponível'),
|
||||
ProviderNotAvailableError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('SIGNATURE_FAILED', 'Falha na assinatura'),
|
||||
SignatureFailedError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('TIMEOUT_ERROR', 'Timeout'),
|
||||
TimeoutError
|
||||
);
|
||||
|
||||
assert.throws(
|
||||
() => signer._throwMappedError('APPLICATION_NOT_AUTHORIZED', 'Não autorizada'),
|
||||
ApplicationNotAuthorizedError
|
||||
@@ -90,6 +175,16 @@ describe('@cybercore/signer-sdk - Testes Unitários do SDK', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('deve fornecer orientação acionável rica em AgentNotRunningError', () => {
|
||||
const err = new AgentNotRunningError(undefined, { host: '127.0.0.1', port: 21325 });
|
||||
assert.equal(err.code, 'AGENT_NOT_RUNNING');
|
||||
assert.equal(err.host, '127.0.0.1');
|
||||
assert.equal(err.port, 21325);
|
||||
assert.ok(err.actionableGuidance.includes('bandeja do sistema'));
|
||||
assert.ok(err.actionableGuidance.includes('instalador oficial'));
|
||||
assert.ok(err.actionableGuidance.includes('21325'));
|
||||
});
|
||||
|
||||
it('deve validar parâmetros obrigatórios no método sign()', async () => {
|
||||
const signer = new CyberCoreSigner();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user