Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 227 additions & 0 deletions src/test/categoryUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
import { describe, expect, it } from 'vitest';
import {
getLevel,
getSubGroup,
getSubGroupDescription,
getSubGroupLabel,
LEVEL_CONFIG,
LEVELS_ORDERED,
type Level,
} from '../components/shared/categoryUtils';
import { STATE_NAMES } from '../data/programStatus';
import { pathsByLevel } from './fixtures/usParameterPaths.json';

type PathCase<Expected extends string = string> = [path: string | null, expected: Expected];

describe('getLevel', () => {
const levelCases: PathCase<Level>[] = [
[null, 'household'],
['', 'household'],
['gov.irs.income', 'federal'],
['gov.states.ca.tax', 'state'],
['gov.states.tax.income', 'state'],
['gov.local.ca.la.x', 'local'],
['gov.territories.pr.x', 'territory'],
['gov.contrib.ubi.x', 'reform'],
['contrib.x', 'reform'],
['household.income.x', 'household'],
['input.x', 'household'],
['government.x', 'household'],
['households.x', 'household'],
];
it.each(levelCases)('classifies %j as %s', (path, expected) => {
expect(getLevel(path)).toBe(expected);
});

// Federal is the general government level when no specific jurisdiction is named.
const governmentCases: PathCase<Level>[] = [
['gov', 'federal'],
['gov.statesx.y', 'federal'],
['gov.localities.x', 'federal'],
['gov.contribution.x', 'federal'],
];
it.fails.each(governmentCases)('classifies %j as %s', (path, expected) => {
expect(getLevel(path)).toBe(expected);
});
});

describe('getSubGroup', () => {
const groupCases: PathCase[] = [
[null, 'Other'],
['', 'Other'],
['gov', 'Other'],
['gov.irs.income', 'irs'],
['gov.states.ca.tax', 'CA'],
['gov.states.tax.income', 'Cross-state'],
['gov.states.general_assistance', 'Cross-state'],
['gov.states.unemployment_compensation', 'Cross-state'],
['gov.states.workers_compensation', 'Cross-state'],
['gov.local.ca.la.x', 'CA-la'],
['gov.territories.pr.x', 'PR'],
['gov.contrib.ubi.x', 'ubi'],
['contrib.x', 'x'],
['household.income.x', 'income'],
['input.x', 'Other'],
['government.x', 'Other'],
['gov.statesx.y', 'statesx'],
['gov.localities.x', 'localities'],
['gov.contribution.x', 'contribution'],
['households.x', 'Other'],
['gov.states.ca', 'CA'],
['contrib', 'Other'],
];
it.each(groupCases)('groups %j under %j', (path, expected) => {
expect(getSubGroup(path)).toBe(expected);
});

const jurisdictionCases: PathCase[] = [
['gov.states', 'Other'],
['gov.local', 'Other'],
['gov.local.ca', 'CA'],
['gov.territories', 'Other'],
['gov.states.taxation.x', 'Other'],
['gov.states.generality.x', 'Other'],
['gov.states.unemploymentx.x', 'Other'],
['gov.states.workersx.x', 'Other'],
];
it.fails.each(jurisdictionCases)('groups %j under %j', (path, expected) => {
expect(getSubGroup(path)).toBe(expected);
});
});

const displayCases: [level: Level, key: string, label: string, description: string | null][] = [
['federal', 'irs', 'Internal Revenue Service (IRS)', 'Income tax, credits, deductions, and filing rules'],
['federal', 'hhs', 'Health & Human Services (HHS)', 'Medicaid, CHIP, ACA subsidies, and poverty guidelines'],
['federal', 'usda', 'USDA', 'SNAP, school meals, WIC, and CSFP'],
['federal', 'ssa', 'Social Security Administration (SSA)', 'Social Security benefits and payroll taxes'],
['federal', 'hud', 'Housing & Urban Development (HUD)', 'Housing assistance and rental subsidies'],
['federal', 'ed', 'Dept. of Education', 'Pell grants and student aid'],
['federal', 'aca', 'Affordable Care Act (ACA)', 'Marketplace eligibility and premium subsidies'],
['federal', 'doe', 'Dept. of Energy', 'Energy efficiency rebates and credits'],
['federal', 'fcc', 'Federal Communications Commission (FCC)', 'Affordable Connectivity Program'],
['federal', 'puf', 'IRS Public Use File', null],
['federal', 'simulation', 'Simulation', 'Behavioral responses, labor supply elasticities, and simulation settings'],
['federal', 'gao', 'GAO', null],
['state', 'Cross-state', 'Cross-state', null],
['state', 'ZZ', 'ZZ', null],
['local', 'CA-la', 'Los Angeles (CA)', null],
['local', 'CA-riv', 'Riverside (CA)', null],
['local', 'NY-nyc', 'New York City (NY)', null],
['local', 'CA-ala', 'Alameda (CA)', null],
['local', 'CA-sf', 'San Francisco (CA)', null],
['local', 'CO-denver', 'Denver (CO)', null],
['local', 'TX-harris', 'Harris County (TX)', null],
['local', 'MD-montgomery', 'Montgomery County (MD)', null],
['local', 'OR-multnomah_county', 'Multnomah County (OR)', null],
['local', 'CA-tax', 'Local taxes (CA)', null],
['local', 'CA-example', 'example (CA)', null],
['territory', 'PR', 'Puerto Rico', null],
['territory', 'GU', 'Guam', null],
['territory', 'VI', 'US Virgin Islands', null],
['territory', 'ZZ', 'ZZ', null],
['reform', 'child_tax_credit_2025', 'Child Tax Credit 2025', null],
['reform', '2025_tax_credit_v2', '2025 Tax Credit V2', null],
['household', 'demographic', 'Demographics', 'Age, marital status, disability, and household composition'],
['household', 'income', 'Income', 'Employment, self-employment, investment, and other income sources'],
['household', 'expense', 'Expenses', 'Childcare, medical, housing, and other deductible expenses'],
['household', 'assets', 'Assets', 'Savings, property, and other asset holdings'],
['household', 'marginal_tax_rate', 'Marginal tax rates', 'Effective marginal rates on additional income'],
['household', 'cliff', 'Cliff analysis', 'Benefit cliffs from income changes'],
['household', 'other_inputs', 'other_inputs', null],
];

const localLabelCases: [key: string, label: string][] = [
['CA', 'CA'],
['CA-', 'CA'],
['CA-san-diego', 'san-diego (CA)'],
['tax', 'Local taxes'],
['MD-montgomery_county', 'Montgomery County (MD)'],
];

describe('getSubGroupLabel', () => {
it.each(displayCases)('labels %s key %j as %j', (level, key, label) => {
expect(getSubGroupLabel(key, level)).toBe(label);
});

it.fails.each(localLabelCases)('labels local key %j as %j', (key, label) => {
expect(getSubGroupLabel(key, 'local')).toBe(label);
});

it('uses the same 51 state codes and names as the program registry', () => {
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const namedStates: Record<string, string> = {};
// A code with no name is returned unchanged. Check all two-letter codes
// so this catches extra state entries as well as missing or renamed ones.
for (const first of letters) {
for (const second of letters) {
const code = first + second;
const label = getSubGroupLabel(code, 'state');
if (label !== code) namedStates[code] = label;
}
}
expect(Object.keys(namedStates)).toHaveLength(51);
expect(namedStates).toEqual(STATE_NAMES);
});
});

describe('getSubGroupDescription', () => {
it.each(displayCases)('describes %s key %j', (level, key, _label, description) => {
expect(getSubGroupDescription(key, level)).toBe(description);
});

it.each(localLabelCases)('has no description for local key %j', (key) => {
expect(getSubGroupDescription(key, 'local')).toBeNull();
});
});

it('lists each US level once, in the order specified by its config', () => {
expect(LEVELS_ORDERED).toEqual(['federal', 'state', 'local', 'territory', 'reform', 'household']);
LEVELS_ORDERED.forEach((level, index) => {
expect(LEVEL_CONFIG[level].order, level).toBe(index);
});
});

describe('PolicyEngine US parameter paths', () => {
const sampleCases = Object.entries(pathsByLevel).flatMap(([level, groups]) =>
Object.entries(groups).flatMap(([subgroup, paths]) =>
paths.map((path) => ({ path, level, subgroup })),
),
);
const paths = sampleCases.map(({ path }) => path);
// These shared parameters fall through to the wrong subgroup today.
const subgroupFailures = new Set([
'gov.states.household.state_eitcs',
'gov.local.md.flat_rate',
'gov.local.tax.assessment_rate',
]);

it('contains at least 200 distinct sampled paths', () => {
expect(paths.length).toBeGreaterThanOrEqual(200);
expect(new Set(paths).size).toBe(paths.length);
expect(paths).toEqual(expect.arrayContaining([...subgroupFailures]));
});

it.each(sampleCases)('classifies $path as $level', ({ path, level }) => {
expect(getLevel(path)).toBe(level);
});

it.each(sampleCases.filter(({ path }) => !subgroupFailures.has(path)))(
'groups $path under $subgroup',
({ path, subgroup }) => {
expect(getSubGroup(path)).toBe(subgroup);
},
);

it.fails.each(sampleCases.filter(({ path }) => subgroupFailures.has(path)))(
'groups $path under $subgroup',
({ path, subgroup }) => {
expect(getSubGroup(path)).toBe(subgroup);
},
);

it('gives each sampled path a non-empty subgroup label', () => {
for (const path of paths) {
expect(getSubGroupLabel(getSubGroup(path), getLevel(path)), path).toMatch(/\S/);
}
});
});
Loading