Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
926bcf48dThis file is pinned to the selected release. Download the original to open it in your design tool.
import { describe, it, expect, beforeAll } from 'vitest';
import { readFile } from 'node:fs/promises';
import { parseBoard } from '../src/kicad/pcb.js';
import { computeLayoutMetrics, type LayoutMetrics } from '../src/kicad/layout-metrics.js';
import { REFERENCE_BOARD, REFERENCE_CONSTRAINTS } from './helpers.js';
/**
* Mutation testing for the layout scorer (design D10). Take a good board,
* degrade it four ways, require the score to fall every time. A scorer that
* cannot separate the mutants from the original is not measuring layout
* quality, and this is also the regression test that catches a scorer (or a
* future placer) change that quietly makes layouts worse.
*
* Mutations are anchored exact-match text replaces on the fixture source, the
* same way copperhead itself edits KiCad files: nothing here serializes a board.
*/
interface Mutation {
name: string;
/** What the degradation is supposed to show up as, beyond a lower score. */
expect: (mutated: LayoutMetrics, original: LayoutMetrics) => void;
apply: (text: string) => string;
}
const replaceOnce = (text: string, from: string, to: string): string => {
const at = text.indexOf(from);
if (at === -1) throw new Error(`mutation anchor not found: ${from}`);
if (text.indexOf(from, at + 1) !== -1) throw new Error(`mutation anchor is not unique: ${from}`);
return text.slice(0, at) + to + text.slice(at + from.length);
};
/** Remove a whole `(footprint …)` block by its refdes, balancing parentheses.
* Still a text edit on the source: nothing here serializes a board. */
const deleteFootprint = (text: string, ref: string): string => {
const at = text.indexOf(`(property "Reference" "${ref}"`);
if (at === -1) throw new Error(`refdes not found: ${ref}`);
const start = text.lastIndexOf('(footprint', at);
let depth = 0;
for (let i = start; i < text.length; i++) {
if (text[i] === '(') depth++;
else if (text[i] === ')' && --depth === 0) return text.slice(0, start) + text.slice(i + 1);
}
throw new Error(`unbalanced footprint block for ${ref}`);
};
const MUTATIONS: Mutation[] = [
{
name: 'every decoupling cap moves 15 mm from the part it bypasses',
apply: (t) => replaceOnce(replaceOnce(t, '(at 117.5 110.5)', '(at 117.5 125.5)'), '(at 117.5 118.5)', '(at 117.5 133.5)'),
expect: (m) => {
const row = m.hard.find((r) => r.key === 'power.decoupling_distance_mm')!;
expect(row.status).toBe('fail');
expect(m.soft.unroutedNetNames).toContain('+5V');
},
},
{
name: 'the power net is narrowed to 0.25 mm',
apply: (t) => t.replace(/\(width 1\.5\)/g, '(width 0.25)'),
expect: (m, original) => {
const row = m.hard.find((r) => r.key === 'mech.load_trace_width_mm')!;
expect(row.status).toBe('fail');
expect(row.actual).toBe('0.25 mm');
// Nothing moved, so this mutation is visible only through the hard table.
expect(m.soft.routedNetFraction).toBe(original.soft.routedNetFraction);
},
},
{
name: 'placements are shuffled within the outline',
apply: (t) =>
replaceOnce(
replaceOnce(replaceOnce(t, '(at 120 115)', '(at 145 128)'), '(at 135 113.73)', '(at 110 132)'),
'(at 135 118.27)',
'(at 128 133)',
),
expect: (m) => {
expect(m.soft.unroutedNetNames).toEqual(expect.arrayContaining(['+5V', 'SIG1', 'SIG2']));
expect(m.soft.offBoardFootprints).toBe(0);
},
},
{
name: 'the ground zone is deleted',
apply: (t) => t.slice(0, t.indexOf(' (zone')) + ')\n',
expect: (m) => {
expect(m.soft.zones).toBe(0);
expect(m.soft.unroutedNetNames).toContain('GND');
expect(m.hard.find((r) => r.key === 'thermal.copper_area_mm2')!.status).toBe('fail');
},
},
];
describe('layout scorer mutation suite', () => {
let source = '';
let original: LayoutMetrics;
beforeAll(async () => {
source = await readFile(REFERENCE_BOARD, 'utf8');
original = computeLayoutMetrics(parseBoard(source, 'reference-board.kicad_pcb'), REFERENCE_CONSTRAINTS);
});
it('scores the undegraded reference board with every hard constraint passing', () => {
expect(original.hard.every((r) => r.status === 'pass')).toBe(true);
expect(original.score).toBe(93.4);
});
for (const mutation of MUTATIONS) {
it(`scores strictly lower when ${mutation.name}`, () => {
const mutated = computeLayoutMetrics(
parseBoard(mutation.apply(source), 'mutant.kicad_pcb'),
REFERENCE_CONSTRAINTS,
);
mutation.expect(mutated, original);
expect(mutated.score).toBeLessThan(original.score);
});
}
it('scores deleting the bypass caps no higher than moving them 15 mm', () => {
// Regression: an n/a row used to shrink the hard term's denominator, so a
// board with its decoupling DELETED (row n/a, term back to full marks)
// outscored the same board with its decoupling misplaced (row fail). On a
// populated board an unanswerable constraint now forfeits the hard term.
const moved = computeLayoutMetrics(parseBoard(MUTATIONS[0]!.apply(source), 'moved.kicad_pcb'), REFERENCE_CONSTRAINTS);
const deleted = computeLayoutMetrics(
parseBoard(deleteFootprint(deleteFootprint(source, 'C1'), 'C2'), 'deleted.kicad_pcb'),
REFERENCE_CONSTRAINTS,
);
expect(deleted.soft.footprints).toBe(6);
expect(deleted.hard.find((r) => r.key === 'power.decoupling_distance_mm')!.status).toBe('n/a');
expect(deleted.terms.find((t) => t.name === 'Hard constraints')!.points).toBe(0);
expect(deleted.score).toBeLessThanOrEqual(moved.score);
expect(deleted.score).toBeLessThan(original.score);
});
it('leaves the fixture on disk untouched', async () => {
expect(await readFile(REFERENCE_BOARD, 'utf8')).toBe(source);
});
});