import { describe, it, expect } from 'vitest';
import { summarizeMemory, filterMemory, type MemoryEntryLike } from './memorySummary';

const E = (name: string, type: MemoryEntryLike['type'], description = '', body = ''): MemoryEntryLike =>
  ({ name, type, description, body });

const entries: MemoryEntryLike[] = [
  E('a', 'user', 'who I am', 'role: admin'),
  E('b', 'feedback', 'no filler', 'avoid agreement'),
  E('c', 'feedback', 'investigate first', 'check config'),
  E('d', 'project', 'maestro rename', 'PR #385'),
  E('e', 'reference', 'gitea token', 'in ~/.gitea-token'),
];

describe('summarizeMemory', () => {
  it('counts total and per type', () => {
    expect(summarizeMemory(entries)).toEqual({
      total: 5,
      byType: { user: 1, feedback: 2, project: 1, reference: 1 },
    });
  });
  it('returns zeroed counts for an empty list', () => {
    expect(summarizeMemory([])).toEqual({
      total: 0,
      byType: { user: 0, feedback: 0, project: 0, reference: 0 },
    });
  });
});

describe('filterMemory', () => {
  it('returns all when no query/type', () => {
    expect(filterMemory(entries, {})).toHaveLength(5);
  });
  it('filters by type', () => {
    expect(filterMemory(entries, { type: 'feedback' }).map(e => e.name)).toEqual(['b', 'c']);
  });
  it('searches name, description and body (case-insensitive)', () => {
    expect(filterMemory(entries, { query: 'ADMIN' }).map(e => e.name)).toEqual(['a']); // body
    expect(filterMemory(entries, { query: 'rename' }).map(e => e.name)).toEqual(['d']); // description
    expect(filterMemory(entries, { query: 'gitea' }).map(e => e.name)).toEqual(['e']); // name+body
  });
  it('combines type and query', () => {
    expect(filterMemory(entries, { type: 'feedback', query: 'config' }).map(e => e.name)).toEqual(['c']);
  });
  it('ignores a null type', () => {
    expect(filterMemory(entries, { type: null, query: '' })).toHaveLength(5);
  });
});
