// @vitest-environment jsdom
import { describe, expect, it, afterEach } from 'vitest';
import { Editor } from '@tiptap/core';
import { editorExtensions } from './editor-image.js';

describe('ImageWithDimensions', () => {
  let editor: Editor | undefined;

  afterEach(() => {
    editor?.destroy();
    editor = undefined;
  });

  it('round-trips width, height, and alt on img tags', () => {
    editor = new Editor({
      extensions: editorExtensions,
      content: '<img src="https://cdn/a.png" alt="a cat" width="640" height="480" />',
    });

    const html = editor.getHTML();
    expect(html).toContain('alt="a cat"');
    expect(html).toContain('width="640"');
    expect(html).toContain('height="480"');
  });

  it('setImage command forwards width/height into the inserted node (insert-path liveness)', () => {
    // The feature inserts via the command path (chain().setImage(...)), not the parse path above.
    // setImage forwards all options as attrs; width/height are declared by addAttributes, so they
    // survive ProseMirror and renderHTML emits them. This guards the dead-end-to-end regression the
    // Option-A rework fixed (stock Image drops width/height; an undeclared attr would vanish here).
    editor = new Editor({ extensions: editorExtensions });
    // Stock setImage's command type declares only {src,alt,title}; the runtime extension accepts the
    // extra declared attrs. The cast mirrors EditorScreen's call site (same necessary widening).
    editor
      .chain()
      .setImage({ src: 'https://cdn/a.png', alt: 'a cat', width: 640, height: 480 } as { src: string })
      .run();

    const html = editor.getHTML();
    expect(html).toContain('src="https://cdn/a.png"');
    expect(html).toContain('alt="a cat"');
    expect(html).toContain('width="640"');
    expect(html).toContain('height="480"');
  });
});
