import { describe, expect, it } from 'vitest';

import type { OneNotePageSummaryDto } from '../onenote/model/dto.js';
import { buildPageTree } from './page-tree.js';

function page(id: string, level?: number): OneNotePageSummaryDto {
  return { id, level, title: id, text: '', textPreview: '' };
}

describe('section navigation trees', () => {
  it('turns authored page levels into parent/subpage relationships', () => {
    expect(
      buildPageTree([
        page('root', 1),
        page('child', 2),
        page('grandchild', 3),
        page('next-root', 1),
        page('skipped-level', 3),
      ])
    ).toEqual([
      {
        page: page('root', 1),
        children: [
          {
            page: page('child', 2),
            children: [{ page: page('grandchild', 3), children: [] }],
          },
        ],
      },
      {
        page: page('next-root', 1),
        children: [{ page: page('skipped-level', 3), children: [] }],
      },
    ]);
  });

  it('treats absent and invalid levels as top-level pages', () => {
    expect(
      buildPageTree([page('unset'), page('zero', 0), page('nan', Number.NaN)])
    ).toMatchObject([
      { page: { id: 'unset' }, children: [] },
      { page: { id: 'zero' }, children: [] },
      { page: { id: 'nan' }, children: [] },
    ]);
  });
});
