import { describe, it, expect } from 'vitest' import { useAutoGroup } from '../../../composables/useAutoGroup' function engineFor(samples: string[]) { const engine = useAutoGroup() engine.rawText.value = samples.join('\n') engine.parseInput() return engine } // Two tissue classes; fields 2-4 are factors in both. const twoClasses = ['Liver', 'Kidney'].flatMap(tissue => ['A', 'B'].flatMap(first => ['C', 'D'].flatMap(second => ['E', 'F'].map(third => `${tissue}_${first}_${second}_${third}_rep1`))), ) const designWithReplicates = ['WT', 'KO'].flatMap(genotype => ['Vehicle', 'Drug'].flatMap(treatment => ['1', '2', '3'].map(replicate => `${genotype}-${treatment}-${replicate}`)), ) describe('useAutoGroup field edits', () => { it('should name a column Field N when no name can be inferred', () => { expect(engineFor(designWithReplicates).activeSchema.value?.columns[0].name).toBe('Field 1') }) it('should rename the field in every class when renaming by field key', () => { const engine = engineFor(twoClasses) engine.renameGroupByField('Field 3', 'Dose') const names = Object.values(engine.schemas.value).map(schema => schema.columns[2].displayName) expect(names).toEqual(['Dose', 'Dose']) }) it('should keep the old name when another field already has the new name', () => { const engine = engineFor(twoClasses) engine.renameGroupByField('Field 3', 'Field 2') expect(engine.activeSchema.value?.columns[2].displayName).toBeUndefined() }) it('should keep the old name when the new name is blank', () => { const engine = engineFor(twoClasses) engine.renameGroupByField('Field 3', ' ') expect(engine.activeSchema.value?.columns[2].displayName).toBeUndefined() }) it('should use the new name in the sample metadata when a field is renamed', () => { const engine = engineFor(designWithReplicates) engine.renameGroupByField('Field 1', 'Genotype') expect(engine.result.value.metadata[0].fields.Genotype).toBe('WT') }) it('should report the number of groups that remain when a field is switched off', () => { expect(engineFor(designWithReplicates).groupCountWithout('Field 2')).toBe(2) }) it('should leave the grouping unchanged when it reports the count for a field', () => { const engine = engineFor(designWithReplicates) engine.groupCountWithout('Field 2') expect(engine.result.value.experimentalGroups).toHaveLength(4) }) }) describe('useAutoGroup field edits across classes', () => { // Two token-length classes. `Field 2` is a factor in the short names and a // constant in the long names, which have their own factor `Field 3`. const twoLengths = [ 'Liver_A_rep1', 'Liver_A_rep2', 'Liver_B_rep1', 'Liver_B_rep2', 'Liver_c_P_rep1', 'Liver_c_P_rep2', 'Liver_c_Q_rep1', 'Liver_c_Q_rep2', ] it('should keep the old name when a field of another class already has the new name', () => { const engine = engineFor(twoLengths) engine.renameGroupByField('Field 2', 'Field 3') const renamed = Object.values(engine.schemas.value).flatMap(schema => schema.columns).filter(c => c.displayName) expect(renamed).toEqual([]) }) })