import test from 'node:test';
import assert from 'node:assert/strict';
import { evaluateSecurityAlertPolicy, isWithinQuietHours } from '../lib/security/security-alert-policy.mjs';

const profile = {
  timezone: 'Europe/Berlin',
  quietHours: '22:00-07:00',
  alertPreference: 'important',
};

test('quiet hours use the configured profile timezone and wrap midnight', () => {
  assert.equal(isWithinQuietHours('22:00-07:00', 'Europe/Berlin', new Date('2026-07-05T21:00:00Z')), true);
  assert.equal(isWithinQuietHours('22:00-07:00', 'Europe/Berlin', new Date('2026-07-05T10:00:00Z')), false);
  assert.equal(isWithinQuietHours('invalid', 'Europe/Berlin', new Date()), false);
});

test('flash alerts override quiet hours while lower priorities respect them', () => {
  const night = new Date('2026-07-05T21:00:00Z');
  assert.deepEqual(evaluateSecurityAlertPolicy({ notify: true, priority: 'priority' }, profile, night), { send: false, reason: 'quiet_hours' });
  assert.deepEqual(evaluateSecurityAlertPolicy({ notify: true, priority: 'flash' }, profile, night), { send: true, reason: 'allowed' });
});

test('alert preference suppresses routine and non-critical notifications', () => {
  const day = new Date('2026-07-05T10:00:00Z');
  assert.equal(evaluateSecurityAlertPolicy({ notify: true, priority: 'routine' }, profile, day).send, false);
  assert.equal(evaluateSecurityAlertPolicy({ notify: true, priority: 'priority' }, { ...profile, alertPreference: 'critical_only' }, day).send, false);
  assert.equal(evaluateSecurityAlertPolicy({ notify: true, priority: 'flash' }, { ...profile, alertPreference: 'critical_only' }, day).send, true);
});
