import { describe, it, expect } from 'vitest';

describe('Dynamic Affinity', () => {
  describe('parseAffinityDelta', () => {
    it('should extract affinityDelta from JSON response', () => {
      const json = { message: 'hey!', sentiment: 'positive', affinityDelta: 12 };
      expect(json.affinityDelta).toBe(12);
    });

    it('should clamp affinityDelta to -50..+30 range', () => {
      const clamp = (delta: number) => Math.max(-50, Math.min(30, delta));
      expect(clamp(100)).toBe(30);
      expect(clamp(-100)).toBe(-50);
      expect(clamp(15)).toBe(15);
      expect(clamp(-25)).toBe(-25);
    });

    it('should apply affinityDelta to character affinity clamped 0-100', () => {
      const apply = (current: number, delta: number) =>
        Math.max(0, Math.min(100, current + Math.max(-50, Math.min(30, delta))));
      expect(apply(50, 12)).toBe(62);
      expect(apply(30, -40)).toBe(0);
      expect(apply(90, 20)).toBe(100);
      expect(apply(10, -50)).toBe(0);
    });

    it('should fall back to sentiment-based delta when affinityDelta missing', () => {
      const json = { message: 'hey!', sentiment: 'positive' };
      const delta = (json as any).affinityDelta ?? (json.sentiment === 'positive' ? 5 : json.sentiment === 'negative' ? -5 : 0);
      expect(delta).toBe(5);
    });
  });
});
