diff --git a/src/types/zerodb.ts b/src/types/zerodb.ts index 5580832..a18a8bb 100644 --- a/src/types/zerodb.ts +++ b/src/types/zerodb.ts @@ -53,10 +53,31 @@ export interface SearchVectorRequest { filter?: Record; } +/** + * 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 { diff --git a/src/zerodb/index.ts b/src/zerodb/index.ts index 18590d6..ef8cb07 100644 --- a/src/zerodb/index.ts +++ b/src/zerodb/index.ts @@ -12,6 +12,7 @@ import type { UpsertVectorRequest, SearchVectorRequest, SearchVectorResponse, + SearchMemoryResponse, BatchUpsertRequest, Memory, StoreMemoryRequest, @@ -166,8 +167,10 @@ export class ZeroDBClient { /** * Search memories */ - search: async (projectId: string, data: SearchMemoryRequest): Promise> => { - return this.client.post(`/projects/${projectId}/database/memory/search`, data); + search: async (projectId: string, data: SearchMemoryRequest): Promise> => { + // The endpoint returns a MemorySearchResult object + // ({memories, total_count, search_time_ms}), not a bare array. Refs #3 + return this.client.post(`/projects/${projectId}/database/memory/search`, data); }, /** diff --git a/tests/search-response-shape.test.ts b/tests/search-response-shape.test.ts new file mode 100644 index 0000000..121fff4 --- /dev/null +++ b/tests/search-response-shape.test.ts @@ -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>`, 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]*)>>/, + ); + 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/); + }); +});