---
globs: **/*.spec.ts, **/*.test.ts, vitest.config.ts, packages/**/vitest.config.ts
---

## Testing (Vitest)

- **Goal**: Fast, reliable tests that validate behavior at the event and DB boundary, not implementation details.

### General Guidelines

- Use **Vitest** consistently; avoid mixing in Jest-only APIs.
- Prefer:
  - Clear Arrange–Act–Assert structure.
  - One behavior/assertion cluster per test.
  - Descriptive test names tied to domain language (e.g. "message query returns latest by ID").
- Avoid:
  - Sleeping or relying on real timeouts unless absolutely necessary.
  - Network calls to real Telegram or external APIs; use mocks or fixtures instead.

### Core & DB Tests

- For `packages/core`:
  - Test at the level of events and side effects (DB writes, emitted events), not private helpers.
  - Use `setDbInstanceForTests` and/or in-memory PGlite where appropriate to isolate tests from production DBs.
- For DB-related behavior:
  - Prefer migrations + schema fixtures over ad‑hoc `CREATE TABLE` statements inside tests.
  - Clean up state between tests; do not rely on test ordering.

### Client & Web Tests

- For `packages/client`:
  - Focus on verifying that specific inputs/events produce the correct store updates and outgoing events.
  - Mock `CoreContext` or WebSocket layers rather than hitting real servers.
- For `apps/web`:
  - Keep component tests focused and shallow where possible.
  - Only write more integrated UI tests when they provide clear regression protection.

