import { it, expect } from 'vitest' import { subtreeIDs, selectDepartmentBranch, departmentTree } from './qaRegionSelection' const nodes = [ { id: 'root', parent_id: '', name: 'Root' }, { id: 'child', parent_id: 'root', name: 'Child' }, { id: 'leaf', parent_id: 'child', name: 'Leaf' }, { id: 'other', parent_id: 'root', name: 'Other' }, ] it('selects a full branch and removes partial ancestors when a descendant is unchecked', () => { expect(new Set(subtreeIDs(nodes, ['child']))).toEqual(new Set(['child', 'leaf'])) const selected = selectDepartmentBranch(nodes, [], 'root', true) expect(new Set(selected)).toEqual(new Set(nodes.map((d) => d.id))) expect(new Set(selectDepartmentBranch(nodes, selected, 'leaf', false))).toEqual(new Set(['other'])) expect(selected).toHaveLength(4) }) it('collapses hierarchy without dropping source rows or changing statistics', () => { const before = JSON.stringify(nodes) expect(departmentTree(nodes, new Set(['child'])).map((r) => r.department.id)).toEqual(['root', 'child', 'other']) expect(departmentTree(nodes, new Set(['root']), 'Leaf').map((r) => r.department.id)).toEqual([ 'root', 'child', 'leaf', ]) expect(JSON.stringify(nodes)).toBe(before) expect(() => departmentTree([{ id: 'bad', parent_id: 'bad', name: 'Cycle' }], new Set())).toThrow() })