import { describe, it, expect } from 'vitest';
import { rankMessagesByImportance, scoreMessageImportance, selectImportantMessages } from './weighting.js';
import type { Message } from '../models/types.js';

describe('weighting', () => {
  it('scores low-value chatter lower than preference and tool outcomes', () => {
    const chatter: Message = { role: 'user', content: 'hello there' };
    const preference: Message = { role: 'user', content: 'I prefer concise bullet points in responses.' };
    const outcome: Message = { role: 'assistant', content: 'Command succeeded with exit code 0. Output saved.' };

    const chatterScore = scoreMessageImportance(chatter);
    const preferenceScore = scoreMessageImportance(preference);
    const outcomeScore = scoreMessageImportance(outcome);

    expect(preferenceScore).toBeGreaterThan(chatterScore);
    expect(outcomeScore).toBeGreaterThan(chatterScore);
  });

  it('applies recency as a small tie-break boost', () => {
    const messages: Message[] = [
      { role: 'user', content: 'simple note' },
      { role: 'user', content: 'simple note' },
    ];

    const ranked = rankMessagesByImportance(messages);
    expect(ranked[1].score).toBeGreaterThan(ranked[0].score);
  });

  it('selects important messages above threshold and keeps original order', () => {
    const messages: Message[] = [
      { role: 'user', content: 'hello' },
      { role: 'user', content: 'I prefer markdown tables for reports.' },
      { role: 'assistant', content: 'Tool result: command failed with exit code 1' },
      { role: 'assistant', content: 'ok' },
    ];

    const selected = selectImportantMessages(messages, {
      threshold: 0.45,
      maxMessages: 4,
    });

    expect(selected).toHaveLength(2);
    expect(selected[0].index).toBe(1);
    expect(selected[1].index).toBe(2);
  });
});
