import { readFile } from 'node:fs/promises';
import { describe, expect, it } from 'vitest';
import { isCad2DFile, parseCad } from '@hmwebviewer/viewer2d';

describe('viewer2d package interface', () => {
  it('parses the sample DXF through the public package interface', async () => {
    const file = await readFile(
      new URL('../apps/viewer-2d-sample/public/samples/simple.dxf', import.meta.url),
    );
    const result = await parseCad({
      name: 'simple.dxf',
      data: file.buffer.slice(file.byteOffset, file.byteOffset + file.byteLength),
    });
    const entities = result.entities as Array<{ layer?: string; type?: string }>;

    expect(isCad2DFile('drawing.DWG')).toBe(true);
    expect(isCad2DFile('drawing.dxf')).toBe(true);
    expect(isCad2DFile('drawing.pdf')).toBe(false);
    expect(entities.map(({ type, layer }) => [type, layer])).toEqual([
      ['LINE', 'OUTLINE'],
      ['LINE', 'OUTLINE'],
      ['LINE', 'OUTLINE'],
      ['LINE', 'OUTLINE'],
      ['CIRCLE', '0'],
      ['TEXT', '0'],
    ]);
  });
});
