Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/types/zerodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,31 @@ export interface SearchVectorRequest {
filter?: Record<string, any>;
}

/**
* Response from POST .../database/vectors/search (schema: VectorSearchResult).
*
* Field names are snake_case because that is what the API sends and this SDK
* performs no case conversion. They were previously declared as `totalCount`
* and `searchTimeMs`, which are never present on the wire, so both read
* `undefined` at runtime while the compiler guaranteed `number`. Refs #3
*/
export interface SearchVectorResponse {
vectors: Vector[];
totalCount: number;
searchTimeMs: number;
total_count: number;
search_time_ms: number;
}

/**
* Response from POST .../database/memory/search (schema: MemorySearchResult).
*
* Note the collection key is `memories`, NOT `vectors` — the two search
* endpoints return different keys, so these interfaces must stay distinct.
* Refs #3
*/
export interface SearchMemoryResponse {
memories: Memory[];
total_count: number;
search_time_ms: number;
}

export interface BatchUpsertRequest {
Expand Down
7 changes: 5 additions & 2 deletions src/zerodb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
UpsertVectorRequest,
SearchVectorRequest,
SearchVectorResponse,
SearchMemoryResponse,
BatchUpsertRequest,
Memory,
StoreMemoryRequest,
Expand Down Expand Up @@ -166,8 +167,10 @@ export class ZeroDBClient {
/**
* Search memories
*/
search: async (projectId: string, data: SearchMemoryRequest): Promise<ApiResponse<Memory[]>> => {
return this.client.post<Memory[]>(`/projects/${projectId}/database/memory/search`, data);
search: async (projectId: string, data: SearchMemoryRequest): Promise<ApiResponse<SearchMemoryResponse>> => {
// The endpoint returns a MemorySearchResult object
// ({memories, total_count, search_time_ms}), not a bare array. Refs #3
return this.client.post<SearchMemoryResponse>(`/projects/${projectId}/database/memory/search`, data);
},

/**
Expand Down
134 changes: 134 additions & 0 deletions tests/search-response-shape.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* Search response shapes must match what the API actually sends — Refs #3.
*
* Two type-level defects. The request paths are correct, so these fail silently
* at the field level rather than with an HTTP error.
*
* 1. `SearchVectorResponse` declared `totalCount` / `searchTimeMs`, but the API
* sends `total_count` / `search_time_ms`. There is no case-conversion layer
* anywhere in `src/` — verified by searching for camel/snake/humps/
* decamelize/transformResponse — so the body is passed through untouched and
* both fields read `undefined` at runtime while TypeScript guarantees
* `number`. Any pagination or timing logic built on them breaks silently.
* `vectors` itself was correct, so results came through and only the
* metadata was lost, which is what made this survive review.
*
* 2. `memory.search` was typed `Promise<ApiResponse<Memory[]>>`, but the
* endpoint returns an object, not an array.
*
* The real shapes, from the published OpenAPI schema:
*
* .../database/vectors/search -> VectorSearchResult
* { vectors, total_count, search_time_ms }
*
* .../database/memory/search -> MemorySearchResult
* { memories, total_count, search_time_ms }
*
* Note the collection keys DIFFER — `vectors` vs `memories` — so no shared
* response interface or single normalising constant is correct for both.
*
* Why the fix corrects the types rather than adding a runtime transform:
* introducing snake-to-camel conversion in the client would silently change the
* shape of every response across the whole SDK, including endpoints nobody has
* audited. Aligning the declared types with the wire format is the surgical
* change, and it makes the compiler tell the truth about what arrives.
*
* These tests assert against the schema's field names directly, so they cannot
* be satisfied by whatever the types happen to say.
*/

import { readFileSync } from 'fs';
import { join } from 'path';

const root = join(__dirname, '..');

function source(relPath: string): string {
// Strip comment lines so this file's own narrative — which names the wrong
// field names on purpose — cannot satisfy or trip the assertions.
return readFileSync(join(root, relPath), 'utf8')
.split('\n')
.filter((line) => {
const t = line.trimStart();
return !t.startsWith('*') && !t.startsWith('/*') && !t.startsWith('//');
})
.join('\n');
}

/** The exact properties each schema declares. */
const VECTOR_SEARCH_FIELDS = ['vectors', 'total_count', 'search_time_ms'];
const MEMORY_SEARCH_FIELDS = ['memories', 'total_count', 'search_time_ms'];

describe('SearchVectorResponse matches VectorSearchResult', () => {
const types = () => source('src/types/zerodb.ts');

it('declares the snake_case fields the API sends', () => {
const block = types().match(
/export interface SearchVectorResponse \{([^}]*)\}/,
);
expect(block).not.toBeNull();

for (const field of VECTOR_SEARCH_FIELDS) {
expect(block![1]).toContain(field);
}
});

it('does not declare camelCase fields that are always undefined', () => {
const block = types().match(
/export interface SearchVectorResponse \{([^}]*)\}/,
);
expect(block).not.toBeNull();

// These are the specific lies: declared `number`, undefined at runtime.
expect(block![1]).not.toMatch(/\btotalCount\b/);
expect(block![1]).not.toMatch(/\bsearchTimeMs\b/);
});
});

describe('memory.search return type matches MemorySearchResult', () => {
const zerodb = () => source('src/zerodb/index.ts');

it('is not typed as a bare array', () => {
const line = zerodb()
.split('\n')
.find((l) => l.includes('database/memory/search'));
expect(line).toBeDefined();

// Find the declared generic on the search method.
const decl = zerodb().match(
/search: async \(projectId: string, data: SearchMemoryRequest\): Promise<ApiResponse<([^>]*)>>/,
);
expect(decl).not.toBeNull();
expect(decl![1]).not.toBe('Memory[]');
});

it('declares a response interface with the memories key', () => {
const block = source('src/types/zerodb.ts').match(
/export interface SearchMemoryResponse \{([^}]*)\}/,
);
expect(block).not.toBeNull();

for (const field of MEMORY_SEARCH_FIELDS) {
expect(block![1]).toContain(field);
}
});
});

describe('the two search envelopes stay distinct', () => {
it('vector response uses vectors, memory response uses memories', () => {
const types = source('src/types/zerodb.ts');

const vec = types.match(/export interface SearchVectorResponse \{([^}]*)\}/);
const mem = types.match(/export interface SearchMemoryResponse \{([^}]*)\}/);
expect(vec).not.toBeNull();
expect(mem).not.toBeNull();

// The collection keys are not interchangeable. If a future change
// consolidates these into one interface, this fails — which is correct,
// because the API returns different keys for the two endpoints.
expect(vec![1]).toContain('vectors');
expect(vec![1]).not.toMatch(/\bmemories\b/);

expect(mem![1]).toContain('memories');
expect(mem![1]).not.toMatch(/\bvectors\b/);
});
});
Loading