import { describe, expect, it } from 'vitest' import type { SampleGroup } from '../../types' import { detectSampleGroupSeparator, getSampleGroupMajorPrefix, moveSampleToGroup, removeSampleFromGroup, removeSampleGroups, reorderSampleGroup, reorderSampleMajorGroup, } from '../../components/SampleSelector.groups' const groups: SampleGroup[] = [ { name: 'Treatment/A', color: '#111111', samples: ['S1'] }, { name: 'Treatment/B', color: '#222222', samples: ['S2'] }, { name: 'Control/A', color: '#333333', samples: ['S3'] }, { name: 'Control/B', color: '#444444', samples: ['S4'] }, ] describe('SampleSelector group helpers', () => { it('detects the hierarchy separator from group names', () => { expect(detectSampleGroupSeparator(groups)).toBe('/') expect(detectSampleGroupSeparator([{ name: 'Control_A', color: '#111111', samples: [] }])).toBe('_') }) it('extracts major group prefixes with the detected separator', () => { expect(getSampleGroupMajorPrefix('Treatment/A', '/')).toBe('Treatment') expect(getSampleGroupMajorPrefix('Ungrouped', '/')).toBe('Ungrouped') }) it('normalizes whitespace around auto-group path separators', () => { expect(getSampleGroupMajorPrefix('Liver / High / 5min', '/')).toBe('Liver') }) it('moves a sample from one group to another without duplicating target samples', () => { const moved = moveSampleToGroup(groups, 'S1', 'Treatment/A', 'Control/A') expect(moved.map(group => group.samples)).toEqual([[], ['S2'], ['S3', 'S1'], ['S4']]) expect(moveSampleToGroup(groups, 'S3', null, 'Control/A')[2].samples).toEqual(['S3']) }) it('removes groups and samples without mutating the source array', () => { expect(removeSampleGroups(groups, ['Treatment/B']).map(group => group.name)).toEqual([ 'Treatment/A', 'Control/A', 'Control/B', ]) // A whole family or any intermediate level removes as a batch of leaf names. expect( removeSampleGroups(groups, ['Treatment/A', 'Treatment/B']).map(group => group.name), ).toEqual(['Control/A', 'Control/B']) expect(removeSampleFromGroup(groups, 'S1', 'Treatment/A')[0].samples).toEqual([]) expect(groups[0].samples).toEqual(['S1']) }) it('reorders flat groups without mutating the source array', () => { const reordered = reorderSampleGroup(groups, 'Control/A', 'Treatment/A', 'before') expect(reordered.map(group => group.name)).toEqual([ 'Control/A', 'Treatment/A', 'Treatment/B', 'Control/B', ]) expect(groups.map(group => group.name)).toEqual([ 'Treatment/A', 'Treatment/B', 'Control/A', 'Control/B', ]) }) it('reorders major group families as contiguous blocks', () => { const reordered = reorderSampleMajorGroup(groups, 'Control', 'Treatment', 'before') expect(reordered.map(group => group.name)).toEqual([ 'Control/A', 'Control/B', 'Treatment/A', 'Treatment/B', ]) }) it('reorders auto-group families whose separators include spaces', () => { const autoGroups: SampleGroup[] = [ { name: 'Liver / High / 5min', color: '#111111', samples: ['L1'] }, { name: 'Kidney / High / 5min', color: '#222222', samples: ['K1'] }, ] expect( reorderSampleMajorGroup(autoGroups, 'Liver', 'Kidney', 'after').map(group => group.name), ).toEqual(['Kidney / High / 5min', 'Liver / High / 5min']) }) })