// Wave 1 trust bug — honest renewal render. classifyRenewal must never let a
// far-future / comped `subscription_end` (the "RENEWS 6/9/2036" lie) render as
// a real monthly renewal, and must fail closed to `unknown` on bad input.

const { classifyRenewal, MONTHLY_RENEWAL_MAX_DAYS } = require('../../web/src/lib/billingDisplay');

const NOW = Date.parse('2026-07-13T00:00:00.000Z');
const DAY = 86_400_000;

describe('classifyRenewal — honest billing render', () => {
  test('MONTHLY_RENEWAL_MAX_DAYS is 60', () => {
    expect(MONTHLY_RENEWAL_MAX_DAYS).toBe(60);
  });

  test('the 2036 comped/seed row is NOT a renewal → none', () => {
    const r = classifyRenewal('2036-06-09T00:00:00.000Z', NOW);
    expect(r.kind).toBe('none');
    expect(r.iso).toBeUndefined();
  });

  test('a plausible monthly next-bill (now + 30d) → date, carries iso', () => {
    const r = classifyRenewal(new Date(NOW + 30 * DAY).toISOString(), NOW);
    expect(r.kind).toBe('date');
    expect(typeof r.iso).toBe('string');
    expect(Date.parse(r.iso)).toBe(NOW + 30 * DAY);
  });

  test('absent value (null / undefined / empty) → unknown', () => {
    expect(classifyRenewal(null, NOW).kind).toBe('unknown');
    expect(classifyRenewal(undefined, NOW).kind).toBe('unknown');
    expect(classifyRenewal('', NOW).kind).toBe('unknown');
  });

  test('a renewal well in the past → lapsed', () => {
    const r = classifyRenewal(new Date(NOW - 10 * DAY).toISOString(), NOW);
    expect(r.kind).toBe('lapsed');
  });

  test('malformed date string → unknown (never coerced to an epoch date)', () => {
    expect(classifyRenewal('not a date', NOW).kind).toBe('unknown');
    expect(classifyRenewal('N/A', NOW).kind).toBe('unknown');
  });

  test('exactly at the 60-day boundary still reads as a date; just beyond → none', () => {
    expect(classifyRenewal(new Date(NOW + 60 * DAY).toISOString(), NOW).kind).toBe('date');
    expect(classifyRenewal(new Date(NOW + 61 * DAY).toISOString(), NOW).kind).toBe('none');
  });
});
