Skip to content

Tests

Test infrastructure for ksef-client-ts: unit tests (vitest, mocked) and E2E tests (against live KSeF TEST environment).

Overview

All tests use vitest and live in the tests/ directory:

  • Unit tests (tests/unit/) — mocked, no network access. Cover services, HTTP layer, crypto, CLI, builders, validation, workflows, and error hierarchy.
  • E2E tests (tests/e2e/) — run against the live KSeF TEST environment (api-test.ksef.mf.gov.pl). No secrets needed — all auth uses self-signed certificates.

Running

bash
yarn test                # Unit tests (tests/unit)
yarn test:watch          # Unit tests in watch mode
yarn test:e2e            # E2E tests (tests/e2e)
yarn vitest run tests/unit/services/auth.test.ts   # Single file

Unit Tests

68 test files, 961 tests. Located in tests/unit/. All service/HTTP calls are mocked — no network access, fast execution.

Coverage by Area

AreaFilesWhat is tested
cli20All 14 command groups, client factory, config/session store, error handler, output formatting
services13All 13 API services — request construction, response parsing, error propagation
http8RestClient, RetryPolicy (backoff, jitter), RateLimitPolicy (token bucket), PresignedUrlPolicy, AuthManager (401 refresh), transport, RestRequest builder, KSeF feature constants
workflows5Auth workflow, online/batch session, invoice export, polling utility
errors5Full error hierarchy — KSeFError, ApiError, RateLimitError, UnauthorizedError (ProblemDetails), ForbiddenError (reasonCode)
crypto5CryptographyService (AES, RSA, ECDH, CSR), SignatureService (XAdES), CertificateService (self-signed), CertificateFetcher, PKCS#12 loader
builders4AuthTokenRequest, AuthKsefTokenRequest, InvoiceQueryFilter, Permissions (person/entity/authorization)
qr3QrCodeService (PNG/SVG), VerificationLinkService (Code I/II URLs), environment QR URLs
validation2Regex patterns + checksum validators (NIP, PESEL, KSeF number CRC-8), constraints
config2Environment resolution, options defaults
client1KSeFClient initialization, service wiring, login/logout orchestration

Conventions

  • Mock RestClient.execute or RestClient.executeRaw to verify request construction
  • Test both success paths and error paths (wrong status codes, missing fields)
  • Builders test validation: required fields, constraints, edge cases
  • Crypto tests use real crypto operations (key generation, signing, encrypt/decrypt)

E2E Tests

18 test files running against the live KSeF TEST environment. No API tokens or env vars needed.

Zero Secrets

All tests use self-signed certificate authentication:

  1. Generate a random valid NIP
  2. Generate a self-signed RSA company seal certificate with VATPL-{NIP} in Subject DN
  3. Sign an XAdES auth request and submit to KSeF
  4. KSeF TEST accepts self-signed certs (verifyCertificateChain=false)

This means tests can run on any machine, any CI, without configuring credentials.

Test Suites

#FileWhat it testsAuthTimeout
0101-lighthouse.test.tsSystem status, messagesNone30s
0202-auth-token.test.tsToken auth flow (challenge, encrypt, submit, access token, refresh)Cert (bootstrap) + Token60s
0303-auth-xades.test.tsCert generation (RSA/ECDSA), XAdES signing, full cert authCert60s
0404-session-online.test.tsOnline session lifecycle: open, send, poll, close, UPO, get invoice. Negative: wrong NIP (445)Cert + Crypto180s
0505-session-batch.test.tsBatch session: 5 invoices in ZIP, upload, poll, verify all processedCert + Crypto300s
0606-invoices.test.tsQuery metadata, get by KSeF number, async export with decryptCert + Crypto300s
0707-permissions.test.tsPersonal grants query, grant/query/revoke person permissions cycleCert120s
0808-tokens.test.tsToken generate/query/get/revoke lifecycleCert120s
0909-certificates.test.tsCertificate limits, full enrollment lifecycle (CSR, enroll, retrieve, revoke)Cert120s
1010-limits.test.tsContext/subject/rate limitsCert60s
1111-active-sessions.test.tsList active sessions, revoke current sessionCert120s
1212-test-data.test.tsTestData API: create/remove subjects and persons, grant/revoke permissionsCert120s
1313-peppol.test.tsQuery Peppol providersCert60s

Plus 5 permission test suites (07a-07e) that test entity, authorization, and subunit permission flows.

Auth Helpers

Located in tests/e2e/helpers/auth.ts:

  • authenticateWithCert(nip?) — Primary method. Generates random NIP + self-signed cert, calls loginWithCertificate. Returns { client, nip }.
  • authenticateWithCertAndCrypto(nip?) — Same + calls crypto.init() and returns encryptionData (cipherKey, cipherIv, encryptionInfo). Required for invoice send/export operations.
  • createTestClient() — Creates KSeFClient({ environment: 'TEST' }) without auth.

Other Helpers

  • helpers/identifiers.tsgenerateRandomNip() (valid checksum, XSD-compliant), generateRandomPesel(), generateUniqueInvoiceNumber()
  • helpers/invoices.tsprepareInvoiceXml() loads FA_2/FA_3 XML templates from fixtures/, replaces #nip#, #invoicing_date#, #invoice_number#. prepareAndEncryptInvoice() also encrypts with AES-256-CBC.
  • helpers/polling.tspollUntil(action, condition, options) — generic async poller for session status, auth status, export completion, etc.
  • helpers/env.tsFIXTURE_DIR path, hasTokenAuth() (only used internally).

Fixtures

  • fixtures/invoice-fa2.xml — FA(2) v1-0E invoice template
  • fixtures/invoice-fa3.xml — FA(3) v1-0E invoice template

Placeholders: #nip#, #invoicing_date#, #invoice_number#.

How Token Auth Test Works (02)

Since no env vars are available, the test bootstraps its own token:

  1. beforeAll: authenticate via cert, call tokens.generateToken(), save the token string
  2. Tests use the generated token for loginWithToken, manual step-by-step flow, and refresh

Key Implementation Details

  • Auth status polling: Both loginWithToken() and loginWithCertificate() in src/client.ts poll getAuthStatus() until status code is 200 before calling getAccessToken(). This prevents race conditions when KSeF is still processing the auth request.
  • executeVoid(): RestClient has a dedicated executeVoid() method for API endpoints that return empty bodies (closeSession, revokeToken, TestData operations). This avoids JSON.parse errors on empty responses.
  • NIP validation: generateRandomNip() follows the XSD TNIP pattern [1-9]((\d[1-9])|([1-9]\d))\d{7} — digits 2-3 cannot both be zero.
  • crypto.init() is NOT called by loginWithCertificate() — tests that need encryption must call it explicitly via authenticateWithCertAndCrypto().

CI

GitHub Actions workflow at .github/workflows/ci.yml (consolidated):

  • Triggers: push to main (src/tests changes), pull requests, manual dispatch
  • Unit tests: Node 18/20/22 matrix, coverage badge via gist
  • E2E tests: against KSeF TEST environment, 30min timeout, no secrets required

Released under the MIT License.