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

import { readTipTapHtmlSafe } from './tiptapEditorSafe';

void test('readTipTapHtmlSafe: null / destroyed → null', () => {
  assert.equal(readTipTapHtmlSafe(null), null);
  assert.equal(readTipTapHtmlSafe(undefined), null);
  assert.equal(
    readTipTapHtmlSafe({
      isDestroyed: true,
      getHTML: () => '<p>x</p>',
    }),
    null,
  );
});

void test('readTipTapHtmlSafe: getHTML throw → null', () => {
  assert.equal(
    readTipTapHtmlSafe({
      isDestroyed: false,
      getHTML: () => {
        throw new TypeError("Cannot read properties of null (reading 'cached')");
      },
    }),
    null,
  );
});

void test('readTipTapHtmlSafe: ok → html', () => {
  assert.equal(
    readTipTapHtmlSafe({
      isDestroyed: false,
      getHTML: () => '<p>ok</p>',
    }),
    '<p>ok</p>',
  );
});
