Skip to content
Open
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
32 changes: 31 additions & 1 deletion src/core/http-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,37 @@ export class HttpProvider implements MemoryProvider {
);
}

const json = (await res.json()) as APIResponse;
let json: APIResponse;
const contentType = res.headers.get('content-type') ?? '';

if (!contentType.includes('application/json')) {
let text: string;
try {
text = await res.text();
} catch {
text = '(unable to read response body)';
}
throw new PowerMemAPIError(
`Server returned non-JSON response (${contentType || 'no content-type'}): ${text.slice(0, 200)}`,
res.status
);
}

try {
json = (await res.json()) as APIResponse;
} catch {
let bodyText: string;
try {
bodyText = await res.text();
} catch {
bodyText = '(unable to read response body)';
}
throw new PowerMemAPIError(
`Failed to parse server response as JSON: ${bodyText.slice(0, 200)}`,
res.status
);
}

if (!res.ok || !json.success) {
throw new PowerMemAPIError(json.message ?? 'Unknown API error', res.status);
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/memory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpProvider } from './http-provider.js';
import { NativeProvider } from './native-provider.js';
import { loadEnvFile } from '../utils/env.js';
import { PowerMemError } from '../errors/index.js';
import type { MemoryProvider } from './provider.js';
import type { VectorStore } from '../storage/base.js';
import type { InitOptions, MemoryOptions } from '../types/options.js';
Expand Down Expand Up @@ -55,6 +56,9 @@ export class Memory {
}

async add(content: string, options: Omit<AddParams, 'content'> = {}): Promise<AddResult> {
if (!content || !content.trim()) {
throw new PowerMemError('Cannot create memory with empty content', 'MEMORY_VALIDATION_ERROR');
}
return this.provider.add({ content, ...options });
}

Expand All @@ -67,6 +71,9 @@ export class Memory {
}

async update(memoryId: string, content: string, options: Omit<UpdateParams, 'content'> = {}): Promise<MemoryRecord> {
if (!content || !content.trim()) {
throw new PowerMemError('Cannot update memory with empty content', 'MEMORY_VALIDATION_ERROR');
}
return this.provider.update(memoryId, { content, ...options });
}

Expand Down
19 changes: 18 additions & 1 deletion src/core/native-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fs from 'node:fs';
import path from 'node:path';
import type { Embeddings } from '@langchain/core/embeddings';
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
import { PowerMemError } from '../errors/index.js';
import type { MemoryProvider } from './provider.js';
import type {
AddParams,
Expand Down Expand Up @@ -54,6 +55,15 @@ function nowISO(): string {
return new Date().toISOString();
}

function validateContent(content: string, operation: string): void {
if (!content || !content.trim()) {
throw new PowerMemError(
`Cannot ${operation} memory with empty content`,
'MEMORY_VALIDATION_ERROR'
);
}
}

function toMemoryRecord(rec: VectorStoreRecord): MemoryRecord {
return {
id: rec.id,
Expand Down Expand Up @@ -134,6 +144,7 @@ export class NativeProvider implements MemoryProvider {
// ─── Add ────────────────────────────────────────────────────────────────

async add(params: AddParams): Promise<AddResult> {
validateContent(params.content, 'create');
const shouldInfer = params.infer !== false && this.inferrer != null;
if (shouldInfer) {
return this.intelligentAdd(params);
Expand Down Expand Up @@ -348,7 +359,13 @@ export class NativeProvider implements MemoryProvider {

async update(memoryId: string, params: UpdateParams): Promise<MemoryRecord> {
const existing = await this.store.getById(memoryId);
if (!existing) throw new Error(`Memory not found: ${memoryId}`);
if (!existing) {
throw new PowerMemError(`Memory not found: ${memoryId}`, 'NOT_FOUND');
}

if (params.content !== undefined) {
validateContent(params.content, 'update');
}

const content = params.content ?? existing.content;
const metadata = params.metadata ?? existing.metadata;
Expand Down
59 changes: 59 additions & 0 deletions tests/regression/edge-cases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { NativeProvider } from '../../src/core/native-provider.js';
import { PowerMemError } from '../../src/errors/index.js';
import { MockEmbeddings } from '../mocks.js';

describe('edge cases and boundary conditions', () => {
Expand Down Expand Up @@ -178,4 +179,62 @@ describe('edge cases and boundary conditions', () => {
expect(mem!.content).toBe(special);
});
});

// ── Empty content validation ────────────────────────────────────────

describe('empty content validation', () => {
it('add rejects empty string', async () => {
await expect(provider.add({ content: '', infer: false }))
.rejects.toThrow(PowerMemError);
await expect(provider.add({ content: '', infer: false }))
.rejects.toThrow(/Cannot create memory with empty content/);
});

it('add rejects whitespace-only string', async () => {
await expect(provider.add({ content: ' \n\t ', infer: false }))
.rejects.toThrow(PowerMemError);
});

it('update rejects empty string content', async () => {
const res = await provider.add({ content: 'valid', infer: false });
const id = res.memories[0].id;
await expect(provider.update(id, { content: '' }))
.rejects.toThrow(PowerMemError);
await expect(provider.update(id, { content: '' }))
.rejects.toThrow(/Cannot update memory with empty content/);
});

it('update rejects whitespace-only content', async () => {
const res = await provider.add({ content: 'valid', infer: false });
const id = res.memories[0].id;
await expect(provider.update(id, { content: ' \t\n ' }))
.rejects.toThrow(PowerMemError);
});

it('update without content field does not trigger validation', async () => {
const res = await provider.add({ content: 'valid', infer: false });
const id = res.memories[0].id;
const updated = await provider.update(id, { metadata: { key: 'value' } });
expect(updated.content).toBe('valid');
});

it('addBatch rejects items with empty content', async () => {
await expect(
provider.addBatch([
{ content: 'good' },
{ content: '' },
])
).rejects.toThrow(PowerMemError);
});

it('update non-existent ID throws PowerMemError with NOT_FOUND code', async () => {
try {
await provider.update('999999999', { content: 'x' });
expect.fail('should have thrown');
} catch (err) {
expect(err).toBeInstanceOf(PowerMemError);
expect((err as PowerMemError).code).toBe('NOT_FOUND');
}
});
});
});
150 changes: 150 additions & 0 deletions tests/unit/core/http-provider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { HttpProvider } from '../../../src/core/http-provider.js';
import { PowerMemAPIError, PowerMemConnectionError } from '../../../src/errors/index.js';

function makeJsonResponse(body: object, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
headers: { 'content-type': 'application/json' },
});
}

function makeTextResponse(body: string, status: number, contentType: string): Response {
return new Response(body, {
status,
headers: { 'content-type': contentType },
});
}

describe('HttpProvider', () => {
const BASE_URL = 'http://localhost:8080';
let provider: HttpProvider;
let fetchSpy: ReturnType<typeof vi.fn>;

beforeEach(() => {
provider = new HttpProvider(BASE_URL, 'test-key');
fetchSpy = vi.fn();
vi.stubGlobal('fetch', fetchSpy);
});

afterEach(() => {
vi.restoreAllMocks();
});

describe('request – JSON response handling', () => {
it('should parse valid JSON response successfully', async () => {
fetchSpy.mockResolvedValue(
makeJsonResponse({
success: true,
data: [{ id: '1', content: 'hello', user_id: 'u1' }],
})
);

const result = await provider.add({ content: 'hello', userId: 'u1' });
expect(result.memories).toHaveLength(1);
expect(result.memories[0].id).toBe('1');
});

it('should throw PowerMemAPIError for non-JSON HTML response', async () => {
fetchSpy.mockResolvedValue(
makeTextResponse('<html><body>502 Bad Gateway</body></html>', 502, 'text/html')
);

await expect(provider.add({ content: 'test' })).rejects.toThrow(PowerMemAPIError);
await expect(provider.add({ content: 'test' })).rejects.toThrow(/non-JSON response/);
});

it('should include Content-Type and body excerpt in error for non-JSON response', async () => {
fetchSpy.mockResolvedValue(
makeTextResponse('Service Unavailable', 503, 'text/plain')
);

try {
await provider.add({ content: 'test' });
expect.fail('should have thrown');
} catch (err) {
expect(err).toBeInstanceOf(PowerMemAPIError);
const apiErr = err as PowerMemAPIError;
expect(apiErr.statusCode).toBe(503);
expect(apiErr.message).toContain('text/plain');
expect(apiErr.message).toContain('Service Unavailable');
}
});

it('should throw PowerMemAPIError when Content-Type is not JSON', async () => {
fetchSpy.mockResolvedValue(
makeTextResponse('Internal Server Error', 500, 'text/plain')
);

await expect(provider.add({ content: 'test' })).rejects.toThrow(PowerMemAPIError);
await expect(provider.add({ content: 'test' })).rejects.toThrow(/non-JSON response/);
});

it('should throw PowerMemAPIError for malformed JSON with correct Content-Type', async () => {
const res = new Response('not valid json {{{', {
status: 200,
headers: { 'content-type': 'application/json' },
});
fetchSpy.mockResolvedValue(res);

await expect(provider.add({ content: 'test' })).rejects.toThrow(PowerMemAPIError);
await expect(provider.add({ content: 'test' })).rejects.toThrow(/Failed to parse/);
});

it('should throw PowerMemAPIError when API returns success=false', async () => {
fetchSpy.mockResolvedValue(
makeJsonResponse({ success: false, data: null, message: 'Rate limit exceeded' }, 429)
);

try {
await provider.add({ content: 'test' });
expect.fail('should have thrown');
} catch (err) {
expect(err).toBeInstanceOf(PowerMemAPIError);
const apiErr = err as PowerMemAPIError;
expect(apiErr.statusCode).toBe(429);
expect(apiErr.message).toBe('Rate limit exceeded');
}
});
});

describe('request – connection errors', () => {
it('should throw PowerMemConnectionError when fetch fails', async () => {
fetchSpy.mockRejectedValue(new TypeError('fetch failed'));

await expect(provider.add({ content: 'test' })).rejects.toThrow(PowerMemConnectionError);
await expect(provider.add({ content: 'test' })).rejects.toThrow(
/Failed to connect/
);
});
});

describe('get – 404 handling', () => {
it('should return null for 404 response', async () => {
fetchSpy.mockResolvedValue(
makeJsonResponse({ success: false, data: null, message: 'Not found' }, 404)
);

const result = await provider.get('nonexistent');
expect(result).toBeNull();
});
});

describe('response body truncation', () => {
it('should truncate long response body to 200 chars in error message', async () => {
const longBody = 'x'.repeat(500);
fetchSpy.mockResolvedValue(
makeTextResponse(longBody, 500, 'text/plain')
);

try {
await provider.add({ content: 'test' });
expect.fail('should have thrown');
} catch (err) {
expect(err).toBeInstanceOf(PowerMemAPIError);
const msg = (err as PowerMemAPIError).message;
expect(msg.length).toBeLessThan(300);
}
});
});
});