import { describe, expect, it, vi } from 'vitest';
import { createMemoryTools } from './memory-tools.js';

describe('createMemoryTools operator retrieval binding', () => {
  const memory = {
    insights: { searchByEmbedding: vi.fn(), create: vi.fn() },
    preferences: { findByUserAndCategory: vi.fn(), findByUser: vi.fn(), upsert: vi.fn() },
  };
  const scope = { tenantId: 'tenant-a', ownerId: 'owner-a', sessionId: 'session-a' };

  it('uses the configured plugin with the server-derived scope for retrieval and capture', async () => {
    const plugin = {
      search: vi.fn(async () => []),
      capture: vi.fn(async () => ({ id: 'insight-1' })),
    };
    const tools = createMemoryTools(memory as never, null, 'owner-a', {
      plugin: plugin as never,
      scope,
    });

    await tools
      .find((tool) => tool.name === 'memory_search')!
      .execute('call-1', { query: 'plans' }, undefined, undefined, {} as never);
    await tools
      .find((tool) => tool.name === 'memory_save_insight')!
      .execute(
        'call-2',
        { content: 'secret', category: 'decision' },
        undefined,
        undefined,
        {} as never,
      );

    expect(plugin.search).toHaveBeenCalledWith(scope, 'plans', 5);
    expect(plugin.capture).toHaveBeenCalledWith(scope, {
      content: 'secret',
      source: 'agent',
      category: 'decision',
    });
  });
});
