import test from 'node:test';
import assert from 'node:assert/strict';
import { parseMindspaceCoverMeta, upsertMindspaceCoverMeta } from './mindspace-cover-meta.mjs';

test('upsertMindspaceCoverMeta replaces existing meta tag', () => {
  const html = `<!doctype html><html><head>
<meta name="mindspace-cover" content='{"tag":"旧","emoji":"🌿"}'>
<title>Demo</title></head><body></body></html>`;
  const next = upsertMindspaceCoverMeta(html, { tag: '旅行', emoji: '✈️', accent: '#eeb04e' });
  const parsed = parseMindspaceCoverMeta(next);
  assert.equal(parsed.tag, '旅行');
  assert.equal(parsed.emoji, '✈️');
  assert.equal(parsed.accent, '#eeb04e');
});

test('upsertMindspaceCoverMeta inserts meta into head when missing', () => {
  const html = '<!doctype html><html><head><title>Demo</title></head><body></body></html>';
  const next = upsertMindspaceCoverMeta(html, { tag: '美食', emoji: '🍜' });
  assert.match(next, /name="mindspace-cover"/);
  assert.equal(parseMindspaceCoverMeta(next).tag, '美食');
});
