import assert from 'node:assert/strict';
import test from 'node:test';
import { formatHistory, parseTimeWindow } from './mention-history.js';
import type { StoredMessage } from '../db/messages.js';

function message(id: number, text: string): StoredMessage {
  return {
    id,
    message_id: id,
    chat_id: 1,
    user_id: id,
    username: `user${id}`,
    first_name: null,
    text,
    date: id,
  };
}

test('bounded time windows always cap the database query', () => {
  const window = parseTimeWindow('summarize the last 999999 weeks');

  assert.equal(window.limit, 250);
  assert.ok(window.fromDate !== undefined && window.fromDate >= 0);
});

test('history keeps newest messages within the UTF-8 byte budget', () => {
  const history = formatHistory([
    message(1, `old-${'א'.repeat(80)}`),
    message(2, `middle-${'ב'.repeat(80)}`),
    message(3, `new-${'ג'.repeat(80)}`),
  ], 300);

  assert.ok(Buffer.byteLength(history, 'utf8') <= 300);
  assert.doesNotMatch(history, /old-/);
  assert.match(history, /new-/);
});
