import assert from 'node:assert/strict';
import test from 'node:test';
import { algorithmInternals, computeHotScore } from './plaza-algorithm.mjs';

const { DEFAULT_CONFIG } = algorithmInternals;

test('computeHotScore matches documented 3-hour example', () => {
  const publishedAt = Date.now() - 3 * 3_600_000;
  const score = computeHotScore(
    {
      view_count: 500,
      like_count: 20,
      comment_count: 3,
      collect_count: 5,
      published_at: publishedAt,
    },
    DEFAULT_CONFIG,
  );
  assert.ok(Math.abs(score - 12.97) < 0.05);
});

test('computeHotScore decays for older posts with same stats', () => {
  const stats = {
    view_count: 500,
    like_count: 20,
    comment_count: 3,
    collect_count: 5,
  };
  const recent = computeHotScore(
    { ...stats, published_at: Date.now() - 3 * 3_600_000 },
    DEFAULT_CONFIG,
  );
  const older = computeHotScore(
    { ...stats, published_at: Date.now() - 24 * 3_600_000 },
    DEFAULT_CONFIG,
  );
  assert.ok(recent > older);
  assert.ok(Math.abs(older - 1.09) < 0.05);
});

test('computeHotScore never returns negative values', () => {
  assert.equal(
    computeHotScore(
      { view_count: 0, like_count: 0, comment_count: 0, collect_count: 0, published_at: Date.now() },
      DEFAULT_CONFIG,
    ),
    0,
  );
});
