import test from 'node:test';
import assert from 'node:assert/strict';

import { DateRangeCalculator } from '../js/utils/date-range-calculator.js';
import { PaginationModel } from '../js/models/pagination-model.js';

test('calculates date presets using an injected current day', () => {
    const today = new Date('2026-06-04T12:00:00+07:00');

    const last7 = DateRangeCalculator.calculatePreset('last7', today);
    assert.equal(DateRangeCalculator.toDateKey(last7.start), '2026-05-29');
    assert.equal(DateRangeCalculator.toDateKey(last7.end), '2026-06-04');

    const thisMonth = DateRangeCalculator.calculatePreset('thisMonth', today);
    assert.equal(DateRangeCalculator.toDateKey(thisMonth.start), '2026-06-01');
    assert.equal(DateRangeCalculator.toDateKey(thisMonth.end), '2026-06-04');

    const all = DateRangeCalculator.calculatePreset('all', today);
    assert.equal(all.start, null);
    assert.equal(all.end, null);
});

test('formats and parses dd/mm/yyyy date input safely', () => {
    const date = new Date(2026, 5, 4);
    assert.equal(DateRangeCalculator.formatDisplay(date), '04/06/2026');
    assert.equal(DateRangeCalculator.toDateKey(date), '2026-06-04');
    assert.equal(DateRangeCalculator.parseDisplayDate('04/06/2026')?.getFullYear(), 2026);
    assert.equal(DateRangeCalculator.parseDisplayDate('31/02/2026'), null);
    assert.equal(DateRangeCalculator.parseDisplayDate('04/06/10000'), null);
});

test('creates stable compact pagination ranges', () => {
    assert.deepEqual(PaginationModel.range(1, 4), [1, 2, 3, 4]);
    assert.deepEqual(PaginationModel.range(1, 10), [1, 2, '...', 10]);
    assert.deepEqual(PaginationModel.range(5, 10), [1, '...', 4, 5, 6, '...', 10]);
    assert.deepEqual(PaginationModel.range(10, 10), [1, '...', 9, 10]);
});
