Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
6.3 KB
Raw Blame
import { describe, test, expect } from 'bun:test';
import { slugifyEntity, entityPagePath, extractEntities } from '../src/core/enrichment-service.ts';

describe('enrichment-service', () => {
  describe('slugifyEntity', () => {
    test('person names → people/ prefix', () => {
      expect(slugifyEntity('Jane Doe', 'person')).toBe('people/jane-doe');
    });

    test('company names → companies/ prefix', () => {
      expect(slugifyEntity('Acme Corp', 'company')).toBe('companies/acme-corp');
    });

    test('handles apostrophes', () => {
      expect(slugifyEntity("O'Brien", 'person')).toBe('people/obrien');
    });

    test('folds Latin accents to their base letter', () => {
      expect(slugifyEntity('José García', 'person')).toBe('people/jose-garcia');
    });

    test('preserves non-Latin scripts instead of dropping them', () => {
      // Was ASCII-only ([a-z0-9]): Cyrillic names slugified to '' or a bare
      // '-', so every non-Latin person/company collided on the same empty
      // slug. Assert parsed structure (exact slug), not just non-emptiness.
      expect(slugifyEntity('Иван Петров', 'person')).toBe('people/иван-петров');
      expect(slugifyEntity('Тестовая Компания', 'company')).toBe('companies/тестовая-компания');
    });

    test('trims leading/trailing hyphens', () => {
      expect(slugifyEntity('  Test Name  ', 'person')).toBe('people/test-name');
    });

    test('collapses multiple hyphens', () => {
      expect(slugifyEntity('Test--Name', 'person')).toBe('people/test-name');
    });
  });

  describe('entityPagePath', () => {
    test('returns same result as slugifyEntity', () => {
      expect(entityPagePath('Jane Doe', 'person')).toBe(slugifyEntity('Jane Doe', 'person'));
    });
  });

  describe('extractEntities', () => {
    test('extracts capitalized multi-word names', () => {
      const entities = extractEntities('I met with John Smith and Sarah Connor yesterday.');
      expect(entities.length).toBeGreaterThanOrEqual(2);
      const names = entities.map(e => e.name);
      expect(names).toContain('John Smith');
      expect(names).toContain('Sarah Connor');
    });

    test('extracts capitalized multi-word names in other scripts', () => {
      // Was ASCII-only ([A-Z][a-z]+): every non-Latin name-shaped token was
      // invisible to the extractor, so extract_entities found zero entities
      // in non-Latin text regardless of how well-formed the names were.
      const entities = extractEntities('Вчера встретился с Иван Петров и Анна Смирнова.');
      const names = entities.map(e => e.name);
      expect(names).toContain('Иван Петров');
      expect(names).toContain('Анна Смирнова');
    });

    test('classifies company names with Corp/Inc/Labs', () => {
      const entities = extractEntities('We visited Acme Corp and Beta Labs.');
      const acme = entities.find(e => e.name.includes('Acme'));
      const beta = entities.find(e => e.name.includes('Beta'));
      expect(acme?.type).toBe('company');
      expect(beta?.type).toBe('company');
    });

    test('classifies other multi-word names as person', () => {
      const entities = extractEntities('Talked to Jane Doe about the project.');
      const jane = entities.find(e => e.name === 'Jane Doe');
      expect(jane?.type).toBe('person');
    });

    test('deduplicates by name (case-insensitive)', () => {
      const entities = extractEntities('John Smith said hello. Then John Smith left.');
      const johns = entities.filter(e => e.name === 'John Smith');
      expect(johns.length).toBe(1);
    });

    test('returns empty array for text with no entities', () => {
      const entities = extractEntities('this is all lowercase text with no names');
      expect(entities.length).toBe(0);
    });

    test('includes context around each entity', () => {
      const entities = extractEntities('The CEO of StartupX, John Smith, announced the deal.');
      const john = entities.find(e => e.name === 'John Smith');
      expect(john?.context.length).toBeGreaterThan(10);
    });

    test('handles 3-4 word names', () => {
      const entities = extractEntities('Mary Jane Watson Parker joined the team.');
      expect(entities.some(e => e.name.split(' ').length >= 3)).toBe(true);
    });

    test('does not merge capitalized words across a paragraph break', () => {
      const entities = extractEntities('We spoke with Winters\n\nReyes continued the analysis.');
      expect(entities.some(e => e.name.includes('\n'))).toBe(false);
      expect(entities.some(e => e.name.replace(/\s+/g, ' ') === 'Winters Reyes')).toBe(false);
    });

    test('does not merge capitalized words across a single line break', () => {
      const entities = extractEntities('Winters\nReyes discussed the proof.');
      expect(entities.some(e => e.name.includes('\n'))).toBe(false);
    });

    test('still matches same-line multi-word capitalized names', () => {
      const entities = extractEntities('Casey Morgan visited the lab.');
      expect(entities.some(e => e.name === 'Casey Morgan')).toBe(true);
    });

    test('still matches names separated by a non-breaking space on the same line', () => {
      const entities = extractEntities('Jordan Blake requested access.');
      expect(entities.some(e => e.name === 'Jordan Blake')).toBe(true);
    });

    test('does not merge capitalized words across a Unicode line separator (U+2028)', () => {
      const entities = extractEntities('Winters\u2028Reyes discussed the proof.');
      expect(entities.some(e => e.name.includes('\u2028'))).toBe(false);
    });

    test('does not merge capitalized words across a Unicode paragraph separator (U+2029)', () => {
      const entities = extractEntities('Winters\u2029Reyes discussed the proof.');
      expect(entities.some(e => e.name.includes('\u2029'))).toBe(false);
    });

    test('does not merge capitalized words across a vertical tab (U+000B)', () => {
      const entities = extractEntities('Winters\vReyes discussed the proof.');
      expect(entities.some(e => e.name.includes('\v'))).toBe(false);
    });

    test('does not merge capitalized words across a form feed (U+000C)', () => {
      const entities = extractEntities('Winters\fReyes discussed the proof.');
      expect(entities.some(e => e.name.includes('\f'))).toBe(false);
    });
  });
});