/** * Test environment, imported for its side effects BEFORE anything that pulls in * the DAL. * * This has to be a separate module rather than assignments at the top of the * test file: ES imports are hoisted and evaluated before any statement in the * importing module, so `process.env.X = …` written above `import { db }` still * runs after db_client has already read (and thrown on) it. * * DATABASE_URL points at a closed local port on purpose. This repo can reach * the production database from a developer machine, and node_health's rollup * path writes — so the tests are made structurally unable to touch it rather * than merely not calling the write path. * * ASSIGNED UNCONDITIONALLY, and that is the whole point. This used to be `||=`, * which defeated itself on exactly the machine it was written to protect: Bun * auto-loads the repo-root `.env`, so `DATABASE_URL` is ALREADY set — to * production — before this module runs, and `||=` left it there. Every test run * then dialled the production RDS, and `db.ts`'s module body (boot seeds, which * include an UPDATE over `tasks`) went with it. The visible symptom was a flaky * 30-second `Failed query: select count(*)::int from "node_clusters"` — that was * the production connection timing out, not a local port refusing. * * A test that genuinely wants a live database must say so explicitly by setting * TEST_DATABASE_URL; there is no way to reach production by accident. */ process.env.DATABASE_URL = process.env.TEST_DATABASE_URL || "postgres://test:test@127.0.0.1:1/streamline_test"; // Pinned so the tests assert against known thresholds rather than whatever a // developer happens to have exported. process.env.HEALTH_SUSTAIN_MS = "120000"; process.env.HEALTH_COOLDOWN_MS = "1800000"; process.env.HEALTH_DISK_FIRE_PCT = "90"; process.env.HEALTH_DISK_CLEAR_PCT = "85"; process.env.HEALTH_METRICS_STALE_MS = "180000"; // Nothing in a test run may reach a real person. Bun auto-loads .env from the // repo root, so a developer machine's real Lark credentials ARE in scope here — // belt and braces: // - no recipients, so pushAlerts returns before building a card // - no app credentials, so larkClient() is null and sendCard is a no-op anyway // The old guard deleted LARK_NOTIFY_CHAT_ID, which stopped mattering once alerts // moved from the shared group to direct messages. process.env.HEALTH_ALERT_EMAILS = ""; delete process.env.LARK_NOTIFY_CHAT_ID; delete process.env.LARK_APP_ID; delete process.env.LARK_APP_SECRET; // 代码仓注册表(shared/src/repos.ts)在 import 时读环境变量,而 Bun 会自动加载仓库根的 // `.env`。抹掉这三个 key 是为了把断言钉在注册表的**内置值**上 —— 否则某台机器的 .env // 里恰好有一个覆盖值,这些断言就在别人那儿静默漂移。space id 也要抹:翻到 Bits 通道之后 // 它决定断言里的 MR 详情 URL。 delete process.env.BITS_PROJECT_GITLAB_ID_TT_IM_MONOREPO; delete process.env.BITS_GROUP_NAME_TT_IM_MONOREPO; delete process.env.BITS_SPACE_ID_TT_IM_MONOREPO; delete process.env.BITS_DEV_TASK_TEAM_FLOW_ID_TT_IM_MONOREPO; delete process.env.BITS_DEV_TASK_WORKFLOW_ID_TT_IM_MONOREPO; delete process.env.BITS_DEV_TASK_REPO_PATH_TT_IM_MONOREPO; // bits.ts looks up a per-user token in settings before the service-account // fallback. Unit tests that stub fetch must not wait on a closed postgres // port for that lookup — skip it unless a test is specifically about tokens. process.env.BITS_SKIP_USER_TOKEN = "1";