Improve v4 runtime env and test harness
Merged
MO
7mo ago+261+261+012345678901234567890123456789-192-192-012345678901234567890123456789
Why
Small cleanup / improvements that I wanted for adding drizzle cleanly - doing drizzle work in a separate PR for clarity - this PR is just the small improvements.
What Changed
- Use
t3-envto parse env vars into a strongly typed object so the app can rely on defaults instead of carrying optional/null checks through the code.- Added a nested constants.ts to centralize shared constants and path segments in one place.
- Run tests directly from source with
tsxso we do not need compiled test output trees likedist/tests/test, which keeps the upcoming DB work cleaner. - Split
server.tsintoapp.ts/server.tssplit so Fastify app construction is cleanly testable and separate from process startup. This follows the official fastify demo best practice. - Replaced deprecated z.string().url() usages with z.url().
- Turned Fastify’s built-in logger on so local startup is visible by default (will be replaced by flowlogger - didn't implement any custom logger)
Test plan
- Added simple t3 env tests that check the discriminated union logic works
Summary by cubic
Make server-v4 runtime env type-safe and run node:test directly from source via tsx. Centralize Fastify app bootstrap and streamline the test harness for local/SEA/remote targets.
-
Refactors
- Added
src/app.tsto build the Fastify app, register OpenAPI, routes, health/readiness, and dev-only Swagger UI. - Introduced
src/env.tsusing@t3-oss/env-core; defaultsBROWSERBASE_CONFIG_DIRto~/.stagehand,PORTto 3000,NODE_ENVto development; added unit tests. - Added
src/constants.tsto centralize shared constants (e.g., default config dir name). - Switched URL fields to
z.url()in LLM and Page schemas. - Reworked test runner to execute
.test.tsfrompackages/server-v4/test/**with--import=tsx; simplified path handling and reporter names; removed dist test build andtsconfig.tests.json. - Simplified scripts: added
start; updated test scripts to source paths; updatedturbo.jsonto dropbuild:esm-testsand ensure integration depends onbuild:server:dist; kept SEA support. - Bumped
@t3-oss/env-coreto^0.13.11.
- Added
-
Migration
- Start locally with
pnpm --filter @browserbasehq/stagehand-server-v4 start(ordev). - Run tests from source:
pnpm test:unitorpnpm test:integration. For SEA:STAGEHAND_SERVER_TARGET=sea pnpm test:integration(SEA build still required). - No more
build:esm-tests; integration runs depend onbuild:server:distautomatically.
- Start locally with