import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import test from 'node:test';

import { normalizePreferences } from '../src/js/preferences.js';
import { createTimedCache } from '../src/js/perf-cache.js';

test('normalizePreferences trims persisted user input and falls back to safe options', () => {
  assert.deepEqual(
    normalizePreferences({
      email: '  USER@EXAMPLE.COM ',
      feishuName: '  阿树  ',
      chargerType: 'rocket',
      vendor: 'gongniu',
    }),
    {
      email: 'user@example.com',
      feishuName: '阿树',
      chargerType: 'all',
      vendor: 'gongniu',
    },
  );
});

test('createTimedCache returns fresh values and drops stale ones', () => {
  const cache = createTimedCache(1000);

  cache.set('analytics:7', { ok: true }, 1000);

  assert.deepEqual(cache.get('analytics:7', 1500), { ok: true });
  assert.equal(cache.get('analytics:7', 2501), null);
});

test('monitor surface does not render recommendation card', () => {
  const html = readFileSync(new URL('../index.html', import.meta.url), 'utf8');

  assert.equal(html.includes('id="recommendation-card"'), false);
  assert.equal(html.includes('recommendation-title'), false);
  assert.equal(html.includes('recommendation-basis'), false);
  assert.equal(html.includes('recommendation-subscribe-btn'), false);
  assert.equal(html.includes('id="preference-panel"'), false);
  assert.equal(html.includes('togglePreferencePanel()'), false);
  assert.equal(html.includes('id="preference-feishu-name"'), false);
  assert.equal(html.includes('id="preference-email"'), false);
});
