import test from 'node:test'; import assert from 'node:assert/strict'; import { createOrgChart } from './index.js'; import { engineTree } from './render.js'; import type { RenderContext } from './render.js'; import type { OrganizationTreeNode } from '../data/index.js'; const node = (id: string, parentId: string | null, children: OrganizationTreeNode[] = []): OrganizationTreeNode => ({ organization: { id, parentId, name: id, type: parentId ? 'office' : 'root', leaderId: null, displayOrder: 0 }, children, }); test('Auto engine projection preserves root, nested ancestry, sibling order and depths without mutation', () => { const roots = [node('root', null, [node('first', 'root', [node('cell', 'first', [node('team', 'cell')])]), node('second', 'root')])]; const before = structuredClone(roots); const context = { orderedOrganizations: [], readOnly: true } as unknown as RenderContext; const result = engineTree(roots, 'test', context); assert.equal(result[0]?.depth, 0); assert.equal(result[0]?.children?.[0]?.children?.[0]?.children?.[0]?.depth, 3); assert.deepEqual(context.orderedOrganizations.map(item => item.id), ['root', 'first', 'cell', 'team', 'second']); assert.deepEqual(result[0]?.children?.map(item => item.organizationIndex), [1, 4]); assert.deepEqual(roots, before); }); test('independent roots remain independent and empty hierarchy stays empty', () => { const context = { orderedOrganizations: [], readOnly: true } as unknown as RenderContext; const roots = [node('first', null), node('second', null)]; assert.equal(engineTree(roots, 'test', context).length, 2); assert.deepEqual(engineTree([], 'empty', context), []); }); const options = { dataSource: { load: async () => ({}) }, stylesheetUrls: { engine: '/engine.css', core: '/style.css' } }; test('layout defaults remain compatible and displayMode is an independent public option', () => { for (const displayMode of ['interactive', 'readOnly'] as const) { for (const layoutMode of [undefined, 'fixed', 'auto'] as const) { const handle = createOrgChart({ ...options, displayMode, layoutMode }); assert.equal(handle.getState().status, 'idle'); handle.destroy(); } } }); test('invalid layout and unknown Auto company fail explicitly, while Fixed ignores company configuration', () => { assert.throws(() => createOrgChart({ ...options, layoutMode: 'invalid' as 'auto' }), /layoutMode/); assert.throws(() => createOrgChart({ ...options, layoutMode: 'auto', companyId: 'unknown' }), /companyId/); const fixed = createOrgChart({ ...options, layoutMode: 'fixed', companyId: 'unknown' }); assert.equal(fixed.getState().status, 'idle'); fixed.destroy(); });