Skip to content

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

Open
kafeelraza wants to merge 1 commit into
RocketChat:developfrom
kafeelraza:test/ai-adapter-test-suite
Open

kafeelraza wants to merge 1 commit 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! 🚀

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.

1 participant