/**
 * Marketplace reputation score (Phase 5, issue 290).
 * Score in expected range 0–100; offline or stale lastSeen reduces score.
 */

import { describe, it, expect } from "vitest";
import {
  computeReputationScore,
  STALE_MS,
} from "@/lib/marketplace-reputation";

describe("marketplace reputation score", () => {
  const now = Date.now();
  const recent = new Date(now - 60 * 1000);
  const stale = new Date(now - (STALE_MS + 60 * 1000));

  it("returns score in range 0–100", () => {
    expect(computeReputationScore("online", 50, recent)).toBeGreaterThanOrEqual(0);
    expect(computeReputationScore("online", 50, recent)).toBeLessThanOrEqual(100);
    expect(computeReputationScore("offline", null, stale)).toBeGreaterThanOrEqual(0);
    expect(computeReputationScore("offline", null, stale)).toBeLessThanOrEqual(100);
  });

  it("online + recent + low latency gives 100", () => {
    expect(computeReputationScore("online", 50, recent)).toBe(100);
    expect(computeReputationScore("online", 99, recent)).toBe(100);
  });

  it("online + recent + medium latency gives 85", () => {
    expect(computeReputationScore("online", 100, recent)).toBe(85);
    expect(computeReputationScore("online", 499, recent)).toBe(85);
  });

  it("online + recent + high latency gives 75", () => {
    expect(computeReputationScore("online", 500, recent)).toBe(75);
    expect(computeReputationScore("online", 2000, recent)).toBe(75);
  });

  it("offline removes online bonus (max 75 from latency only)", () => {
    expect(computeReputationScore("offline", 50, recent)).toBe(75);
    expect(computeReputationScore("degraded", 50, recent)).toBe(75);
  });

  it("stale lastSeen removes online bonus", () => {
    expect(computeReputationScore("online", 50, stale)).toBe(75);
    expect(computeReputationScore("online", 200, stale)).toBe(60);
  });

  it("offline and no latency gives base 50", () => {
    expect(computeReputationScore("offline", null, stale)).toBe(50);
    expect(computeReputationScore("offline", undefined, recent)).toBe(50);
  });

  it("accepts lastSeen as ISO string", () => {
    expect(computeReputationScore("online", 50, recent.toISOString())).toBe(100);
    expect(computeReputationScore("online", 50, stale.toISOString())).toBe(75);
  });

  it("empty or weird status treated as offline", () => {
    expect(computeReputationScore("", 50, recent)).toBe(75);
    expect(computeReputationScore("unknown", 50, recent)).toBe(75);
  });
});
