tests — codebuddy

Module: tests-codebuddy Cohesion: 0.80 Members: 0

tests — codebuddy

This document provides an overview of the tests/codebuddy/client-gemini-vision.test.ts module, detailing its purpose, structure, and how it validates the content conversion logic within the CodeBuddyClient for Gemini Vision API interactions.

Module Purpose

The primary purpose of client-gemini-vision.test.ts is to thoroughly unit test the content conversion mechanisms implemented in the CodeBuddyClient class, specifically those responsible for preparing user input (text, images, mixed content) into the format required by the Google Gemini Vision API.

It focuses on two key private methods:

  1. convertContentToGeminiParts: Transforms various input content types into an array of Gemini-compatible Part objects.
  2. parseDataUrl: Extracts MIME type and base64 data from data URLs, with a fallback for non-data URLs.

By testing these methods, the module ensures that the CodeBuddyClient can correctly handle diverse user prompts, including those with inline images, before sending them to the Gemini API.

Module Structure and Key Components

This test module is structured using standard testing framework constructs (describe, it, beforeEach) to organize tests logically.

Test Setup

Each test suite initializes a new instance of CodeBuddyClient using a beforeEach hook:

import { CodeBuddyClient } from '../../src/codebuddy/client.js';

describe('Gemini Vision - convertContentToGeminiParts', () => {
  let client: CodeBuddyClient;

  beforeEach(() => {
    client = new CodeBuddyClient('test-key');
  });
  class="hl-cmt">// ... tests ...
});

Private Method Access Helpers

Since convertContentToGeminiParts and parseDataUrl are private methods of CodeBuddyClient, the tests employ type assertion to access them for direct testing. This pattern allows for isolated unit testing of internal logic.

  const callConvert = (client: CodeBuddyClient, content: unknown) =>
    (client as unknown as { convertContentToGeminiParts: (c: unknown) => unknown[] }).convertContentToGeminiParts(content);

  const callParseDataUrl = (client: CodeBuddyClient, url: string) =>
    (client as unknown as { parseDataUrl: (u: string) => { mimeType: string; data: string } }).parseDataUrl(url);

convertContentToGeminiParts Test Suite

This suite covers various scenarios for converting different content types into Gemini API parts:

parseDataUrl Test Suite

A nested describe block specifically tests the parseDataUrl helper method:

Tested Production Code

This test module directly targets and validates the following components within src/codebuddy/client.ts:

Execution Flow

The tests instantiate CodeBuddyClient and then use the callConvert and callParseDataUrl helper functions to invoke the private methods under test. These helpers act as a bridge to the internal logic of the CodeBuddyClient.

graph TD
    A[client-gemini-vision.test.ts] --> B{callConvert}
    A --> C{callParseDataUrl}
    B --> D[CodeBuddyClient.convertContentToGeminiParts]
    C --> E[CodeBuddyClient.parseDataUrl]

Contribution Guidelines

When contributing to this module: