This commit is contained in:
Andras Schmelczer 2026-09-11 17:57:03 +01:00
commit 7959fe85cc
29 changed files with 1679 additions and 498 deletions

View file

@ -4,7 +4,7 @@ import { skirtingCuts } from '../src/lib/cut-list';
import { cutLength } from '../src/lib/format';
import type { Room, SkirtingSettings } from '../src/lib/types';
const settings: SkirtingSettings = { stockLengths: [2400], thickness: 18, minLength: 300, kerf: 3 };
const settings: SkirtingSettings = { stockLength: 2400, thickness: 18, minLength: 300, kerf: 3 };
const rectangle = (width = 4000, height = 3000, id = 'r1'): Room => ({
id,
name: id,
@ -18,23 +18,20 @@ const rectangle = (width = 4000, height = 3000, id = 'r1'): Room => ({
});
describe('skirting cutting plan', () => {
it('subtracts internal butt thickness and preserves every installed wall run', () => {
it('retains coping overlap in the wall-length blanks and preserves every visible run', () => {
const plan = planSkirting([rectangle()], settings, 5);
expect(plan.issues.filter((issue) => issue.severity === 'error')).toEqual([]);
expect(plan.pieces.reduce((sum, piece) => sum + piece.backLength, 0)).toBeCloseTo(
14_000 - 4 * 18,
);
expect(plan.pieces.reduce((sum, piece) => sum + piece.backLength, 0)).toBeCloseTo(14_000);
expect(plan.pieces.every((piece) => piece.length >= settings.minLength)).toBe(true);
expect(skirtingCuts(plan)[0].id).toBe('A1');
for (let wall = 0; wall < 4; wall++) {
const pieces = plan.pieces.filter((piece) => piece.wallIndex === wall);
expect(pieces[0].leftType).toBe('internal');
expect(pieces.at(-1)?.rightType).toBe('internal');
expect(
pieces.every(
(piece) => Math.abs(piece.leftMitre) < 1e-6 && Math.abs(piece.rightMitre) < 1e-6,
),
).toBe(true);
expect(pieces[0].leftType).toBe(wall === 0 ? 'internal' : 'cope');
expect(pieces.at(-1)?.rightType).toBe(wall === 3 ? 'cope' : 'internal');
expect(pieces.every((piece) => piece.visibleLength! >= settings.minLength - 1e-6)).toBe(true);
expect(pieces.reduce((sum, piece) => sum + piece.visibleLength!, 0)).toBeCloseTo(
[4000, 3000, 4000, 3000][wall] - 36,
);
}
});
@ -43,7 +40,7 @@ describe('skirting cutting plan', () => {
room.walls[0].doors = [{ id: 'd1', offset: 1000, width: 900 }];
const plan = planSkirting([room], settings, 5);
const pieces = plan.pieces.filter((piece) => piece.wallIndex === 0);
expect(pieces.reduce((sum, piece) => sum + piece.backLength, 0)).toBeCloseTo(4000 - 900 - 18);
expect(pieces.reduce((sum, piece) => sum + piece.backLength, 0)).toBeCloseTo(4000 - 900);
expect(
pieces.some((piece) => Math.abs(piece.end.x - 1000) < 1e-6 && piece.rightType === 'square'),
).toBe(true);
@ -96,7 +93,7 @@ describe('skirting cutting plan', () => {
doors: [],
})),
};
const plan = planSkirting([room], { ...settings, stockLengths: [6000] }, 5);
const plan = planSkirting([room], { ...settings, stockLength: 6000 }, 5);
expect(plan.issues.filter((issue) => issue.severity === 'error')).toEqual([]);
const front = plan.pieces.find((piece) => piece.wallIndex === 4)!;
expect(front.backLength).toBeCloseTo(1000);
@ -108,7 +105,7 @@ describe('skirting cutting plan', () => {
expect(front.rightBevel).toBeCloseTo(45);
expect(front.template?.map((point) => Math.round(point.x))).toEqual([18, 1018, 1036, 0]);
const reversed = { ...room, walls: room.walls.map((wall) => ({ ...wall, turn: -wall.turn })) };
const reversePlan = planSkirting([reversed], { ...settings, stockLengths: [6000] }, 5);
const reversePlan = planSkirting([reversed], { ...settings, stockLength: 6000 }, 5);
const reverseFront = reversePlan.pieces.find((piece) => piece.wallIndex === 4)!;
expect(reverseFront.length).toBeCloseTo(1036);
expect(reverseFront.leftBevel).toBeCloseTo(-45);
@ -117,7 +114,7 @@ describe('skirting cutting plan', () => {
it('packs across rooms with deterministic unique IDs and accounts for kerf', () => {
const rooms = [rectangle(1800, 1800, 'r1'), rectangle(1800, 1800, 'r2')];
const options = { ...settings, stockLengths: [1800, 3600], thickness: 0, kerf: 3 };
const options = { ...settings, stockLength: 1800, thickness: 0, kerf: 3 };
const plan = planSkirting(rooms, options, 5);
expect(plan).toEqual(planSkirting(rooms, options, 5));
expect(new Set(plan.pieces.map((piece) => piece.id)).size).toBe(plan.pieces.length);
@ -127,7 +124,7 @@ describe('skirting cutting plan', () => {
expect(plan.waste).toBeCloseTo(0);
const trimmed = planSkirting(
[rectangle(1199, 1199)],
{ ...settings, stockLengths: [2400], thickness: 0, minLength: 1199 },
{ ...settings, stockLength: 2400, thickness: 0, minLength: 1199 },
5,
);
expect(trimmed.stockCount).toBe(4); // 1199 + 3 + 1199 cannot fit into 2400.
@ -138,7 +135,7 @@ describe('skirting cutting plan', () => {
it('allows the final saw pass to extend beyond a short stock tail', () => {
const plan = planSkirting(
[rectangle(1199, 1199)],
{ ...settings, stockLengths: [1200], thickness: 0 },
{ ...settings, stockLength: 1200, thickness: 0 },
5,
);
expect(plan.pieces).toHaveLength(4);
@ -163,10 +160,10 @@ describe('skirting cutting plan', () => {
expect(plan.pieces.reduce((sum, piece) => sum + piece.length, 0)).toBeCloseTo(4794);
});
it('looks ahead when choosing from different stock lengths', () => {
it('packs multiple cuts into the single purchased stock length', () => {
const plan = planSkirting(
[rectangle(1200, 1200)],
{ ...settings, stockLengths: [1800, 2400], thickness: 0, kerf: 0 },
{ ...settings, stockLength: 2400, thickness: 0, kerf: 0 },
5,
);
expect(plan.stockCount).toBe(2);
@ -174,17 +171,66 @@ describe('skirting cutting plan', () => {
expect(plan.waste).toBeCloseTo(0);
});
it('lets reused tails set wall joints to save a stock length without extra saw cuts', () => {
it('discards rounded splits that exceed fractional stock while retaining a fitting candidate', () => {
const plan = planSkirting([rectangle(2400.1, 2400.1)], { ...settings, stockLength: 1200.2 }, 5);
expect(plan.issues.filter((issue) => issue.severity === 'error')).toEqual([]);
expect(plan.pieces).toHaveLength(8);
for (let wall = 0; wall < 4; wall++) {
expect(
plan.pieces
.filter((piece) => piece.wallIndex === wall)
.reduce((sum, piece) => sum + piece.backLength, 0),
).toBeCloseTo(2400.1);
}
for (const stock of plan.stocks) {
let available = stock.length;
for (const id of stock.pieceIds) {
const piece = plan.pieces.find((piece) => piece.id === id)!;
const blank =
Math.abs(piece.length - available) < 1e-6 ? available : cutLength(piece.length);
expect(blank).toBeLessThanOrEqual(available + 1e-6);
expect(piece.blankLength).toBeCloseTo(blank);
expect(piece.visibleLength).toBeGreaterThanOrEqual(settings.minLength - 1e-6);
available = Math.max(0, available - blank - settings.kerf);
}
}
});
it('adds a joint when fractional stock cannot supply the rounded two-piece split', () => {
const plan = planSkirting([rectangle(2400.3, 2400.3)], { ...settings, stockLength: 1200.2 }, 5);
expect(plan.issues.filter((issue) => issue.severity === 'error')).toEqual([]);
expect(plan.pieces.length).toBeGreaterThan(8);
for (let wall = 0; wall < 4; wall++) {
expect(
plan.pieces
.filter((piece) => piece.wallIndex === wall)
.reduce((sum, piece) => sum + piece.backLength, 0),
).toBeCloseTo(2400.3);
}
});
it('keeps another room when rounding makes a minimum-sized split impossible', () => {
const valid = rectangle(2400.4, 2400.4, 'valid');
const invalid = rectangle(2400.1, 2400.1, 'invalid');
const options = { ...settings, stockLength: 1200.2, thickness: 0, minLength: 1200.05 };
const plan = planSkirting([valid, invalid], options, 5);
expect(plan.pieces).toEqual(planSkirting([valid], options, 5).pieces);
expect(plan.pieces).toHaveLength(8);
const failures = plan.issues.filter((issue) => issue.severity === 'error');
expect(failures.length).toBeGreaterThan(0);
expect(failures.every((issue) => issue.roomId === invalid.id)).toBe(true);
});
it('lets reused tails set wall joints while counting preparation and coping operations', () => {
const room = rectangle(4000, 4000);
const plan = planSkirting([room], settings, 5);
expect(plan).toEqual(planSkirting([room], settings, 5));
expect(plan.stockCount).toBe(7); // Fixed joints used eight lengths for this simple square.
expect(plan.cuts).toBe(4);
expect(plan.waste).toBeCloseTo(7 * 2400 - (16000 - 4 * 18));
expect(plan.pieces.some((piece) => Math.abs(piece.length - 815) < 1e-6)).toBe(true);
expect(plan.cuts).toBe(13);
expect(plan.waste).toBeCloseTo(7 * 2400 - 16000);
for (let wallIndex = 0; wallIndex < 4; wallIndex++) {
const pieces = plan.pieces.filter((piece) => piece.wallIndex === wallIndex);
expect(pieces.reduce((sum, piece) => sum + piece.backLength, 0)).toBeCloseTo(3982);
expect(pieces.reduce((sum, piece) => sum + piece.backLength, 0)).toBeCloseTo(4000);
pieces.slice(1).forEach((piece, index) => {
expect(piece.start.x).toBeCloseTo(pieces[index].end.x);
expect(piece.start.y).toBeCloseTo(pieces[index].end.y);
@ -213,24 +259,25 @@ describe('skirting cutting plan', () => {
doors: [],
})),
};
const plan = planSkirting([room], { ...settings, stockLengths: [311] }, 5);
const plan = planSkirting([room], { ...settings, stockLength: 340 }, 5);
expect(plan.issues.filter((issue) => issue.severity === 'error')).toEqual([]);
expect(plan.pieces).toHaveLength(30);
for (const piece of plan.pieces) {
expect(piece.length).toBeLessThanOrEqual(311 + 1e-6);
expect(piece.length).toBeLessThanOrEqual(340 + 1e-6);
expect(piece.backLength).toBeGreaterThanOrEqual(300 - 1e-6);
expect(piece.template![2].x - piece.template![3].x).toBeGreaterThanOrEqual(300 - 1e-6);
expect(piece.visibleLength).toBeGreaterThanOrEqual(300 - 1e-6);
}
for (let wallIndex = 0; wallIndex < 3; wallIndex++) {
expect(
plan.pieces
.filter((piece) => piece.wallIndex === wallIndex)
.reduce((sum, piece) => sum + piece.backLength, 0),
).toBeCloseTo(3100 - 18 / Math.sin((120 * Math.PI) / 180));
).toBeCloseTo(3100);
}
});
it('calculates angled butt cuts and back setbacks for non-square internal corners', () => {
it('prepares oblique copes at half-turn angles and wall-fits their receiving ends', () => {
const room: Room = {
id: 'triangle',
name: 'Triangle',
@ -242,18 +289,19 @@ describe('skirting cutting plan', () => {
doors: [],
})),
};
const plan = planSkirting([room], { ...settings, stockLengths: [4000] }, 5);
const plan = planSkirting([room], { ...settings, stockLength: 4000 }, 5);
expect(plan.pieces).toHaveLength(3);
expect(plan.pieces[0].backLength).toBeCloseTo(3000 - 18 / Math.sin((120 * Math.PI) / 180));
expect(plan.pieces.every((piece) => Math.abs(piece.backLength - 3000) < 1e-6)).toBe(true);
for (const piece of plan.pieces) {
expect(piece.leftMitre).toBeCloseTo(30);
expect(piece.rightMitre).toBeCloseTo(30);
expect(piece.leftType).toBe('internal');
expect(piece.leftBevel).toBeCloseTo(30);
expect(piece.rightBevel).toBeCloseTo(-30);
expect(piece.template![2].x - piece.template![3].x).toBeGreaterThan(settings.minLength);
expect(piece.leftMitre).toBeCloseTo(piece.wallIndex === 0 ? 30 : 60);
expect(piece.rightMitre).toBeCloseTo(piece.wallIndex === 2 ? 60 : 30);
expect(piece.leftType).toBe(piece.wallIndex === 0 ? 'internal' : 'cope');
expect(piece.visibleLength).toBeCloseTo(3000 - 36 * Math.tan(Math.PI / 3));
expect(piece.installedTemplate![1].x - piece.installedTemplate![0].x).toBeGreaterThan(
settings.minLength,
);
}
expect(plan.cuts).toBe(9); // Three blank crosscuts and six end bevels.
expect(plan.cuts).toBe(12); // Three blank crosscuts, six preparation bevels, three copes.
});
it('can leave a minimum-sized final piece without making a tiny join', () => {
@ -272,9 +320,9 @@ describe('skirting cutting plan', () => {
const room = rectangle();
room.walls[3].length += 100;
expect(planSkirting([room], settings, 5).pieces).toEqual([]);
expect(
planSkirting([rectangle()], { ...settings, stockLengths: [] }, 5).issues[0].severity,
).toBe('error');
expect(planSkirting([rectangle()], { ...settings, stockLength: 0 }, 5).issues[0].severity).toBe(
'error',
);
expect(
planSkirting([rectangle()], { ...settings, minLength: 1e-10 }, 5).issues[0].severity,
).toBe('error');
@ -310,12 +358,15 @@ describe('skirting cutting plan', () => {
).toEqual(['small', 'open']);
expect(plan.stockCount).toBe(planSkirting([valid], settings, 5).stockCount);
expect(
planSkirting([valid], { ...settings, stockLengths: [] }, 5).issues[0].roomId,
planSkirting([valid], { ...settings, stockLength: 0 }, 5).issues[0].roomId,
).toBeUndefined();
});
it('attributes corner stock-fit failures to the affected room', () => {
const valid = rectangle(4000, 3000, 'valid');
const valid = rectangle(4100, 4100, 'valid');
valid.walls.forEach((wall) => {
wall.doors = [{ id: `${wall.id}-door`, offset: 0, width: 100 }];
});
const triangle: Room = {
id: 'triangle',
name: 'Triangle',
@ -324,7 +375,7 @@ describe('skirting cutting plan', () => {
};
const plan = planSkirting(
[valid, triangle],
{ ...settings, stockLengths: [500], minLength: 490 },
{ ...settings, stockLength: 500, minLength: 490 },
5,
);
expect(plan.pieces.length).toBeGreaterThan(0);
@ -337,4 +388,101 @@ describe('skirting cutting plan', () => {
),
).toBe(true);
});
it('counts full-stock copes as cuts and uses a receiving first wall to close the installation order', () => {
const plan = planSkirting([rectangle(2400, 2400)], settings, 1);
expect(plan.issues.filter((issue) => issue.severity === 'error')).toEqual([]);
expect(plan.stockCount).toBe(4);
expect(plan.cuts).toBe(8); // Four preparation mitres and four profile-coping operations.
for (const piece of plan.pieces) expect(piece.length).toBeCloseTo(2400);
expect(plan.pieces.map((piece) => [piece.leftType, piece.rightType])).toEqual([
['internal', 'internal'],
['cope', 'internal'],
['cope', 'internal'],
['cope', 'cope'],
]);
expect(skirtingCuts(plan).map((piece) => piece.id)).toEqual(['A1', 'A2', 'A3']);
expect(plan.pieces[0].id).toMatch(/^SU/);
for (const piece of plan.pieces) {
expect(piece.visibleLength).toBeCloseTo(2364);
expect(piece.leftOverlap).toBeCloseTo(18);
expect(piece.rightOverlap).toBeCloseTo(18);
}
const two = planSkirting(
[rectangle(2400, 2400, 'one'), rectangle(2400, 2400, 'two')],
settings,
1,
);
const ids = two.pieces.map((piece) => piece.id);
expect(new Set(ids).size).toBe(8);
expect(two.stocks.flatMap((stock) => stock.pieceIds).sort()).toEqual([...ids].sort());
});
it('does not cope across a door opening that removes the receiving corner', () => {
const r = rectangle(2400, 2400);
r.walls[0].doors = [{ id: 'corner-door', offset: 0, width: 900 }];
const plan = planSkirting([r], settings, 1);
expect(plan.issues.filter((issue) => issue.severity === 'error')).toEqual([]);
expect(plan.pieces.filter((piece) => piece.wallIndex === 0)[0].leftType).toBe('square');
expect(plan.pieces.filter((piece) => piece.wallIndex === 3).at(-1)!.rightType).toBe('square');
expect(
plan.pieces.reduce(
(sum, piece) =>
sum + Number(piece.leftType === 'cope') + Number(piece.rightType === 'cope'),
0,
),
).toBe(3);
});
it('rejects an exposed run shorter than the minimum even when the preparation blank is long enough', () => {
const plan = planSkirting([rectangle(330, 2400)], settings, 1);
expect(plan.pieces).toEqual([]); // 330 mm blank, but only 330 - 2*18 = 294 mm is visible.
expect(
plan.issues.some((issue) => issue.severity === 'error' && issue.message.includes('minimum')),
).toBe(true);
});
it.each([1, -1])(
'reserves oblique overlap at obtuse internal corners (orientation %i)',
(orientation) => {
const r: Room = {
id: 'hexagon',
name: 'Hexagon',
direction: 0,
walls: Array.from({ length: 6 }, (_, index) => ({
id: `w${index}`,
length: 1000.2,
turn: 60 * orientation,
doors: [],
})),
};
const plan = planSkirting([r], { ...settings, stockLength: 2400 }, 1);
expect(plan.issues.filter((issue) => issue.severity === 'error')).toEqual([]);
const allowance = 18 / Math.tan(Math.PI / 3);
expect(plan.pieces[0].length).toBeCloseTo(1000.2 + 2 * allowance);
expect(plan.pieces.at(-1)!.length).toBeCloseTo(1000.2);
expect(plan.pieces.at(-1)!.leftMitre).toBeCloseTo(30);
expect(plan.pieces.at(-1)!.rightMitre).toBeCloseTo(30);
for (const piece of plan.pieces)
expect(piece.visibleLength).toBeCloseTo(1000.2 - 36 * Math.tan(Math.PI / 6));
for (const stock of plan.stocks) {
const pieces = stock.pieceIds.map((id) => plan.pieces.find((piece) => piece.id === id)!);
expect(
pieces.reduce((sum, piece) => sum + cutLength(piece.length), 0) +
(pieces.length - 1) * settings.kerf,
).toBeLessThanOrEqual(stock.length);
}
expect(plan).toEqual(planSkirting([r], { ...settings, stockLength: 2400 }, 1));
},
);
it('counts a finishing crosscut when the rounded blank uses the complete available stock', () => {
const plan = planSkirting(
[rectangle(1199.6, 1199.6)],
{ ...settings, stockLength: 1200, thickness: 0 },
1,
);
expect(plan.stockCount).toBe(4);
expect(plan.cuts).toBe(4);
expect(skirtingCuts(plan)).toHaveLength(4);
expect(plan.pieces.every((piece) => cutLength(piece.length) === 1200)).toBe(true);
});
});