Skip to content

refactor: ai and tests#136

Closed
gaboesquivel wants to merge 3 commits intomainfrom
refactor/fastify-tests
Closed

refactor: ai and tests#136
gaboesquivel wants to merge 3 commits intomainfrom
refactor/fastify-tests

Conversation

@gaboesquivel
Copy link
Member

@gaboesquivel gaboesquivel commented Mar 11, 2026

Summary by CodeRabbit

  • New Features

    • Added /ai/generate endpoint for text generation with streaming support
    • Changed default AI provider to Open Router with Ollama available as alternative for privacy
    • Updated default AI model to meta-llama/llama-3.3-70b-instruct:free
    • Added configurable upstream request timeout
  • Documentation

    • Updated deployment guidance emphasizing Open Router as default with Ollama privacy option

…hase 1

- Replace createOpenAI+v1 with createOllama (ai-sdk-ollama) for Ollama
- Extract provider.ts: getResolvedProvider, getProvider, default models
- Add POST /ai/generate: single prompt, plain text SSE, CLI-friendly
- Phase 1: chat uses provider, plain chat only (tools disabled for Phase 3)
- OLLAMA_BASE_URL default https://ollama.gaboesquivel.com
- Contract tests (schema, auth) run always; remote gated by OLLAMA_REMOTE_TESTS
- Remove throttleAiRequests from chat tests
- Regenerate OpenAPI and @repo/core client
- Update self-hosted-llm docs with route split
…EMOTE_TESTS

- Delete db.spec.ts, db-integration.spec.ts, reference.test.ts, reference.spec.ts
- Remove it.skip from revoke.test.ts (API key auth test)
- Extract AI skip helpers to test/utils/ai-remote.ts
- Run AI remote tests by default (remove OLLAMA_REMOTE_TESTS flag)
- Remove DEBUG_TEST, OLLAMA_REMOTE_TESTS from turbo.json, .env.test.example
- Use logger: silent always in fastify test util
- Consolidate auth-helpers extractToken/extractMagicLinkData (next e2e)
@vercel
Copy link

vercel bot commented Mar 11, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
basilic-docs Building Building Preview, Comment Mar 11, 2026 7:16am
basilic-fastify Building Building Preview, Comment Mar 11, 2026 7:16am
basilic-next Ready Ready Preview, Comment Mar 11, 2026 7:16am

@coderabbitai
Copy link

coderabbitai bot commented Mar 11, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 3ef81c56-5a0b-4fe7-96ee-3198c051204a

📥 Commits

Reviewing files that changed from the base of the PR and between 69bf538 and 775edc9.

⛔ Files ignored due to path filters (9)
  • apps/fastify/.env-sample is excluded by !**/.env*
  • apps/fastify/.env.test.example is excluded by !**/.env*
  • apps/fastify/tsconfig.json is excluded by !**/tsconfig*.json
  • packages/core/src/api-client.gen.ts is excluded by !**/*.gen.ts
  • packages/core/src/api-wrapper.gen.ts is excluded by !**/*.gen.ts
  • packages/core/src/gen/index.ts is excluded by !**/gen/**, !**/gen/**
  • packages/core/src/gen/sdk.gen.ts is excluded by !**/gen/**, !**/gen/**, !**/*.gen.ts
  • packages/core/src/gen/types.gen.ts is excluded by !**/gen/**, !**/gen/**, !**/*.gen.ts
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml, !**/pnpm-lock.yaml
📒 Files selected for processing (50)
  • apps/docu/content/docs/deployment/self-hosted-llm.mdx
  • apps/docu/content/docs/testing/index.mdx
  • apps/fastify/openapi/openapi.json
  • apps/fastify/package.json
  • apps/fastify/src/lib/env.ts
  • apps/fastify/src/lib/username.ts
  • apps/fastify/src/plugins/error-handler.ts
  • apps/fastify/src/routes/account/account.spec.ts
  • apps/fastify/src/routes/account/apikeys/create.test.ts
  • apps/fastify/src/routes/account/apikeys/list.test.ts
  • apps/fastify/src/routes/account/apikeys/revoke.test.ts
  • apps/fastify/src/routes/account/email/change/request.test.ts
  • apps/fastify/src/routes/account/email/change/verify.test.ts
  • apps/fastify/src/routes/account/link/email/request.test.ts
  • apps/fastify/src/routes/account/link/email/verify.test.ts
  • apps/fastify/src/routes/account/link/oauth/unlink.test.ts
  • apps/fastify/src/routes/account/link/passkey/delete.test.ts
  • apps/fastify/src/routes/account/link/passkey/finish.test.ts
  • apps/fastify/src/routes/account/link/passkey/start.test.ts
  • apps/fastify/src/routes/account/link/totp/setup.test.ts
  • apps/fastify/src/routes/account/link/totp/unlink.test.ts
  • apps/fastify/src/routes/account/link/totp/verify.test.ts
  • apps/fastify/src/routes/account/link/wallet/unlink.test.ts
  • apps/fastify/src/routes/account/link/wallet/verify.test.ts
  • apps/fastify/src/routes/account/passkeys/list.test.ts
  • apps/fastify/src/routes/account/profile/update.test.ts
  • apps/fastify/src/routes/ai/ai.spec.ts
  • apps/fastify/src/routes/ai/chat.test.ts
  • apps/fastify/src/routes/ai/chat.ts
  • apps/fastify/src/routes/ai/generate.test.ts
  • apps/fastify/src/routes/ai/generate.ts
  • apps/fastify/src/routes/ai/provider.ts
  • apps/fastify/src/routes/auth/magiclink/request.ts
  • apps/fastify/src/routes/auth/session/user.test.ts
  • apps/fastify/src/routes/reference.spec.ts
  • apps/fastify/src/routes/reference.test.ts
  • apps/fastify/src/routes/test/authed.test.ts
  • apps/fastify/src/routes/test/totp.test.ts
  • apps/fastify/test/db-integration.spec.ts
  • apps/fastify/test/utils/ai-remote.ts
  • apps/fastify/test/utils/auth-helper.ts
  • apps/fastify/test/utils/db.spec.ts
  • apps/fastify/test/utils/db.ts
  • apps/fastify/test/utils/fake-email.ts
  • apps/fastify/test/utils/fastify.ts
  • apps/fastify/vitest.config.ts
  • apps/fastify/vitest.setup.ts
  • apps/next/e2e/auth-helpers.ts
  • apps/next/e2e/chat-assistant.spec.ts
  • turbo.json

Walkthrough

This PR refactors the AI provider infrastructure to prioritize Open Router as the default provider while supporting Ollama for self-hosting, introduces a new /ai/generate endpoint, centralizes provider resolution logic, overhauls test utilities with session caching for improved test efficiency, and expands test coverage across account and authentication endpoints.

Changes

Cohort / File(s) Summary
AI Provider Defaults & Configuration
apps/docu/content/docs/deployment/self-hosted-llm.mdx, apps/docu/content/docs/testing/index.mdx
Updated documentation to reflect Open Router as default provider, Ollama as privacy-focused alternative; documented new test utility functions for session management and API key generation.
AI Provider Infrastructure
apps/fastify/src/routes/ai/provider.ts, apps/fastify/src/routes/ai/chat.ts, apps/fastify/src/lib/env.ts
Introduced centralized AI provider resolution module with default model constants and provider instantiation; refactored chat route to use external provider logic; added AI_UPSTREAM_TIMEOUT_MS environment variable.
AI Endpoints & API Spec
apps/fastify/src/routes/ai/generate.ts, apps/fastify/openapi/openapi.json, apps/fastify/package.json
Added new POST /ai/generate endpoint supporting streaming and non-streaming text generation; updated OpenAPI spec with /ai/generate schema and updated /ai/chat defaults; added ai-sdk-ollama dependency.
AI Endpoint Tests
apps/fastify/src/routes/ai/chat.test.ts, apps/fastify/src/routes/ai/generate.test.ts, apps/fastify/src/routes/ai/ai.spec.ts
Reorganized chat tests into contract/remote blocks with external skip helpers; added comprehensive generate endpoint tests covering auth, validation, streaming, and remote provider scenarios.
Test Authentication Infrastructure
apps/fastify/test/utils/auth-helper.ts, apps/fastify/test/utils/ai-remote.ts
Introduced session caching (getOrCreateSession, clearSessionPool), passkey insertion utility, API key token generation; added AI provider failure detection and test-skip utilities (skipIfInsufficientCredits, skipIfProviderUnavailable).
Account & Auth Endpoint Tests
apps/fastify/src/routes/account/.../...test.ts, apps/fastify/src/routes/account/profile/update.test.ts, apps/fastify/src/routes/account/passkeys/list.test.ts
Added comprehensive test suites for email change, OAuth unlinking, passkey management (create, delete, verify), TOTP setup/unlink/verify, and profile updates; all using new session caching utilities.
Username & Magic Link Flow
apps/fastify/src/lib/username.ts, apps/fastify/src/routes/auth/magiclink/request.ts
Added deterministic test username generation and session-aware username generation; refactored collision detection and candidate generation with up to 10 iteration attempts; updated magic link request to use new username generator.
Test Infrastructure & Configuration
apps/fastify/test/utils/db.ts, apps/fastify/test/utils/fake-email.ts, apps/fastify/test/utils/fastify.ts, apps/fastify/vitest.config.ts, apps/fastify/vitest.setup.ts
Updated email token extraction to prioritize URL-based extraction; silenced Fastify test logger; added Vitest alias for @test/utils and environment defaults; updated provider unavailability comment.
Import Path Consolidation
apps/fastify/src/routes/account/apikeys/...test.ts, apps/fastify/src/routes/account/link/.../...test.ts, apps/fastify/src/routes/auth/session/user.test.ts, apps/fastify/src/routes/test/...test.ts, apps/fastify/src/routes/account/link/wallet/...test.ts
Standardized relative imports to use @test/utils alias for auth helpers across 10+ test files; updated helper function calls to use new session utilities (getOrCreateSession replacing getSessionToken).
Test Coverage Cleanup
apps/fastify/src/routes/reference.spec.ts, apps/fastify/src/routes/reference.test.ts, apps/fastify/test/db-integration.spec.ts, apps/fastify/test/utils/db.spec.ts
Removed reference endpoint test harness scaffolding, deleted reference test suite, and removed database integration test files; consolidated test structure.
E2E Test Updates
apps/next/e2e/auth-helpers.ts, apps/next/e2e/chat-assistant.spec.ts
Refactored magic link token extraction to centralize retry logic; unskipped chat assistant e2e test with improved error handling for insufficient credits and provider unavailability scenarios.
Error Handling & Logging
apps/fastify/src/plugins/error-handler.ts
Introduced local constant for error message to improve readability; no behavioral changes to error handling.
Build & Test Configuration
turbo.json
Removed DEBUG_TEST from environment lists; added SENTRY_AUTH_TOKEN, WEBAUTHN_RP_NAME, TOTP_ISSUER, RATE_LIMIT_MAX to task configurations.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • PR #81 — Directly overlapping changes to Fastify AI provider stack, provider resolution, chat/generate endpoints, and OpenAPI spec updates to prioritize Open Router as default.
  • PR #124 — Modifies AI provider plumbing, OpenAPI AI endpoints, and provider-resolve logic in Fastify routes with similar refactoring of chat/generate infrastructure.
  • PR #132 — Parallel modifications to AI/chat backend route, provider selection with environment keys, and test infrastructure wiring for AI functionality.

Poem

🐰 hops excitedly
Open Router now leads the way, so bright,
While Ollama stands ready for privacy's flight,
Tests cache sessions with fluffy delight,
New endpoints for generation, smooth and just right,
The provider now unified—everything's tight! ✨

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch refactor/fastify-tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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