Skip to content

test: add unit tests for env config and route schemas#44

Merged
alegerber merged 3 commits into
mainfrom
test/unit-tests-env-and-schemas
Jun 20, 2026
Merged

test: add unit tests for env config and route schemas#44
alegerber merged 3 commits into
mainfrom
test/unit-tests-env-and-schemas

Conversation

@alegerber

@alegerber alegerber commented Jun 20, 2026

Copy link
Copy Markdown
Owner

Was

Zwei Dinge:

  1. Neue Unit-Tests für bisher nur indirekt (über Integrationstests) getestete Logik — env-Config, Route-Schemas und Handler-Verzweigungen.
  2. Konsolidierung: Alle Unit-Tests leben jetzt in einem eigenen test/unit/-Verzeichnis (flach, importiert via ../../src/...), konsistent mit test/integration/. Vorher waren sie zwischen src/ (co-located) und neu verteilt.

Neue Tests

Datei Deckt ab
test/unit/env.test.ts Prod-API_KEY-Pflicht, SSRF_PROTECTION secure-by-default, coerce-Defaults & Positiv-Int-Regeln, S3-Bucket-/AWS-Region-Regex, Endpoint-URLs, Fail-Fast process.exit(1)
test/unit/pdf-schema.test.ts generate-Grenzen & Defaults, Paper/Scale, Cookies/Headers, UUID-Params, merge/split/compress/pdfa, $schema-Stripping
test/unit/screenshot-schema.test.ts Format/Quality/Viewport/Clip-Grenzen, Defaults
test/unit/pdf-handler.test.ts Handler-Logik: html XOR url, SSRF-Block (400 + skip), non-SsrfError re-throw, Stream-vs-Upload, success/error-Metrics
test/unit/screenshot-handler.test.ts Handler-Logik: html XOR url, SSRF-Block, Stream-vs-Upload, png-Default beim Upload

Verschobene Tests (unverändert, nur Pfade)

auth, ssrf, MetricsService, PdfService, PdfOperationsService, ScreenshotService, StorageService → von src/ nach test/unit/. Relative Imports und relative Modul-Mocks (env in StorageService/auth) auf den neuen Ort angepasst.

Produktionsänderung

Eine einzige, minimale Änderung: const envSchemaexport const envSchema in src/config/env.ts, damit das Schema isoliert geparst werden kann, ohne den Import-Zeit-Validierungs-Side-Effect auszulösen.

Verifikation

  • Volle Suite: 286 passed (24 Dateien) — davon 12 Unit-Test-Dateien in test/unit/
  • tsc --noEmit: sauber · eslint src test: sauber
  • Keine *.test.ts mehr in src/

Notierenswert

  • Die Handler-Tests mocken env, assertSafeUrl und die Services, um die Verzweigungen deterministisch ohne DNS/Browser/S3 durchzuspielen.
  • Tests pinnen sicherheitsrelevantes Verhalten direkt fest (Prod-API_KEY-Pflicht, SSRF-Schutz an by default, SSRF-Block im Handler) statt nur implizit beim Server-Boot.
  • Zwei Tests dokumentieren bewusste Design-Entscheidungen als ausführbare Spezifikation: leeres Body geht am Schema durch (die „html XOR url"-Regel lebt im Handler), und z.toJSONSchema muss das $schema-Feld gestrippt bekommen (sonst scheitert Fastifys AJV an der draft/2020-12-Metaschema-Auflösung).

Cover validation logic that was previously only exercised indirectly
through integration tests (which need a mocked browser/S3):

- env.ts: production API_KEY guard, SSRF_PROTECTION secure-by-default
  transform, numeric coercion/defaults, S3 bucket and AWS region rules,
  and the fail-fast process.exit(1) on invalid configuration
- pdf/schema.ts and screenshot/schema.ts: content bounds, defaults,
  enums, UUID params, and the $schema stripping that keeps Fastify's
  AJV (draft-07) able to consume the generated JSON schemas

Export envSchema from env.ts so it can be parsed in isolation without
triggering the module's import-time validation side effect.
Move the env/schema unit tests out of src into a dedicated test/unit
directory, mirroring the existing test/integration layout (flat, importing
from ../../src). Import paths updated accordingly.

Add variant C — handler unit tests for the generate and screenshot routes
with mocked services, env, and the SSRF guard. These cover the branching
logic that lives in the handler rather than the schema:
- html XOR url validation (neither / both)
- SSRF block returns 400 and skips the service; non-SsrfError re-throws
- stream vs. upload paths
- success/error metrics recording (generate)
Relocate the last co-located unit tests (auth, ssrf, and the metrics/pdf/
screenshot/storage services) from src into test/unit so there is a single
home for unit tests, alongside test/integration. Relative imports and the
relative module mocks (env in StorageService and auth) updated to resolve
from the new location.
@alegerber alegerber merged commit 66fb965 into main Jun 20, 2026
7 checks passed
@alegerber alegerber deleted the test/unit-tests-env-and-schemas branch June 20, 2026 11:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant