Skip to content

test(ai-adapter): add comprehensive unit test suite covering all adapters and validation - #1457

Open
kafeelraza wants to merge 2 commits into
RocketChat:developfrom
kafeelraza:test/ai-adapter-test-suite
Open

kafeelraza wants to merge 2 commits into
RocketChat:developfrom
kafeelraza:test/ai-adapter-test-suite

Conversation

@kafeelraza

Copy link
Copy Markdown

Description

Prior to this PR, @embeddedchat/ai-adapter had no test suite configured ("test": "echo \"Error: no test specified\" && exit 1").

This PR establishes a zero-dependency, lightning-fast unit testing suite using Node.js's native test runner (node:test and node:assert/strict), matching the testing pattern used in packages/layout_editor.

Test Coverage (39 tests, 100% pass)

  • BaseAIAdapter:
    • buildChatMessages system prompt prepending and assistant role mapping
    • Consecutive message turn merging for identical roles
    • History truncation to last 10 messages
    • getSuggestions cleaning (stripping bullets, numbers, quotes, and participant labels)
    • summarize transcript formatting
  • MockAdapter:
    • Name and availability report
    • sendPrompt mock response and suggestions
    • generateUIBlocks for forms, profiles, galleries, CTAs, and info fallbacks
  • GeminiAdapter:
    • Endpoint construction with API key parameter
    • Request payload structure (contents array with roles and parts)
    • Task configs mapping (systemInstruction, generationConfig)
    • Error handling for non-200 HTTP responses
    • isAvailable models ping check and network failure resilience
  • OpenAIAdapter:
    • Bearer token header injection and request payload
    • buildResponseFormat (json_schema strict mode for GPT-4/GPT-4o vs json_object fallback)
    • generateUIBlocks schema validation
    • isAvailable models endpoint ping
  • OllamaAdapter:
    • /api/chat request body format (stream: false, num_predict, temperature)
    • generateUIBlocks with format: UI_KIT_JSON_SCHEMA
    • isAvailable tags endpoint ping
  • validation:
    • validateAndExtractBlocks JSON parsing and schema validation
    • Unpacking stringified blocks within layout arrays
    • Error handling on malformed JSON or schema violation

Verification

  • Ran yarn test in packages/ai-adapter: all 39 tests passed in ~312ms.
  • Ran yarn format:check: all files formatted according to Prettier.
  • Included changeset patch for @embeddedchat/ai-adapter.

@kafeelraza

Copy link
Copy Markdown
Author

Hi @Aryan-Verma-999 👋
Since you authored the initial @embeddedchat/ai-adapter package (#1334), I put together this comprehensive, zero-dependency unit test suite using Node's native node:test (matching the pattern in layout_editor).

It covers all adapters (Base, Mock, Gemini, OpenAI, Ollama) and UI-Kit block extraction with 39 tests (100% pass, ~312ms offline). Would love your feedback and review when you have some time! Thank you! 🚀

@swarnendusahu10-dotcom

Copy link
Copy Markdown
Contributor

Hi @kafeelraza! Great job putting together a comprehensive unit test suite for the AI adapters — having test coverage here is a huge plus!

A few technical observations while testing the suite locally:

  1. Yarn 3 pretest hook: Because EmbeddedChat uses Yarn Berry (v3.6.4), pretest lifecycle scripts are not executed automatically. Running yarn test skips yarn build, which causes the tests to run against missing/stale dist/index.mjs artifacts and fail on imports. Chaining the build directly into the test command (e.g. "test": "yarn build && node --test tests/*.test.js") ensures it always builds before testing.
  2. Global fetch mocking: Mutating globalThis.fetch directly across multiple test files can introduce race conditions during concurrent test execution. Using mock.method(globalThis, 'fetch', ...) from node:test would provide cleaner, isolated test scopes.
  3. CI Integration: Since .github/workflows/build-and-lint.yml doesn't currently execute unit tests, it would be great to wire a test step into the root monorepo scripts and CI so these run automatically on future PRs.

@kafeelraza

Copy link
Copy Markdown
Author

Hi @swarnendusahu10-dotcom, thank you so much for testing the suite locally and for the insightful feedback! 🙌

I've addressed all three observations in the latest commit:

  1. Yarn Berry pretest: Chained yarn build directly into the package test script ("test": "yarn build && node --test tests/*.test.js") so the dist artifacts are guaranteed to be built on clean runs.
  2. Fetch mock isolation: Replaced manual globalThis.fetch mutations with node:test's native mock.method(globalThis, 'fetch', ...) and mock.reset() in afterEach across the adapter tests.
  3. CI & Root Integration: Added a root test script running the workspace unit tests and added a Run unit tests step in .github/workflows/build-and-lint.yml so tests run automatically on CI.

All tests are passing cleanly. Would love to know if there's anything else to polish!

This branch has not been deployed

No deployments
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.

2 participants