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
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dev": "vite",
"build": "tsc -b && vite build",
"lint": "oxlint",
"test": "vitest run",
"test": "node --test src/mini-algo.test.ts && vitest run src/App.test.tsx",
"preview": "vite preview"
},
"dependencies": {
Expand Down
144 changes: 144 additions & 0 deletions frontend/src/mini-algo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/// <reference types="node" />

import assert from 'node:assert/strict';
import test from 'node:test';
import {
fitTest,
STAGING_AREA_CAPACITY,
type Sticker,
type StickerSelection,
} from './mini-algo.ts';

const baseSticker: Sticker = {
name: '',
height: 1,
width: 1,
description: '',
tags: [],
id: '',
categorySection: '',
hpglFilePath: '',
svgLocation: '',
};

function selection(id: string, height: number, width = 1, quantity = 1): StickerSelection {
return {
id,
sticker: { ...baseSticker, id, height, width },
quantity,
};
}


// tests moved into their own file using github copilot (and turned into actual tests)
test('accepts a sticker that fits without mutating the staging area', () => {
const existing = selection('existing', 6, 7);
const stagingArea = { stickers: [existing] };

assert.deepEqual(fitTest(stagingArea, selection('new', 1, 3)), {
fits: true,
stickersToRemove: [],
});
assert.deepEqual(stagingArea.stickers, [existing]);
});

test('suggests removing existing stickers when the area is full', () => {
const existing = selection('existing', 6, 7);
const stagingArea = { stickers: [existing] };

assert.deepEqual(fitTest(stagingArea, selection('new', 2, 2)), {
fits: false,
stickersToRemove: [[existing]],
});
assert.deepEqual(stagingArea.stickers, [existing]);
});

test('rejects a missing sticker', () => {
assert.deepEqual(fitTest({ stickers: [] }, null), {
fits: false,
stickersToRemove: [],
});
assert.deepEqual(fitTest({ stickers: [] }, undefined), {
fits: false,
stickersToRemove: [],
});
});

test('rejects a sticker quantity that exceeds capacity', () => {
assert.deepEqual(fitTest({ stickers: [] }, selection('too-large', 5, 5, 2)), {
fits: false,
stickersToRemove: [],
});
});

test('suggests combinations of existing stickers when needed', () => {
const first = selection('first', 2, 5);
const second = selection('second', 4, 5);
const stagingArea = { stickers: [first, second] };

assert.deepEqual(fitTest(stagingArea, selection('new', 6, 6)), {
fits: false,
stickersToRemove: [[first, second]],
});
});

test('prefers the smallest removal option and preserves staging order', () => {
const small = selection('small', 1, 2);
const large = selection('large', 4, 5);

assert.deepEqual(fitTest({ stickers: [small, large] }, selection('new', 5, 6)), {
fits: false,
stickersToRemove: [[large]],
});
});

test('returns alternate removal combinations in staging order', () => {
const first = selection('first', 4, 5);
const second = selection('second', 5, 4);
const third = selection('third', 2, 5);

assert.deepEqual(
fitTest({ stickers: [first, second, third] }, selection('new', 4, 5)),
{
fits: false,
stickersToRemove: [
[first, second],
[first, third],
[second, third],
],
},
);
});

test('only suggests removing up to MAX_STICKERS_PER_REMOVAL (3) stickers', () => {
const stickers = Array.from({ length: 5 }, (_, index) =>
selection(`sticker-${index}`, 4, 5),
);

const result = fitTest({ stickers }, selection('new', 1, 5));

assert.equal(result.fits, false);
assert(result.stickersToRemove.length > 0);
assert(result.stickersToRemove.some((option) => option.length === 3));
assert(result.stickersToRemove.every((option) => option.length <= 3));
});

test('caps removal suggestions at five options', () => {
const stickers = Array.from({ length: 5 }, (_, index) =>
selection(`option-${index}`, 4, 5),
);
const result = fitTest({ stickers }, selection('new', 1, 5));

assert.equal(result.stickersToRemove.length, 5);
assert(result.stickersToRemove.every((option) => option.length <= 3));
});

test('rejects a sticker larger than the staging area capacity', () => {
assert.deepEqual(
fitTest(
{ stickers: [selection('existing', 1, 3)] },
selection('too-large', STAGING_AREA_CAPACITY + 1, 1),
),
{ fits: false, stickersToRemove: [] },
);
});
185 changes: 185 additions & 0 deletions frontend/src/mini-algo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
export const STAGING_AREA_CAPACITY = 45;
const MAX_REMOVAL_OPTIONS = 5;
const MAX_STICKERS_PER_REMOVAL_OPTION = 3;

export interface Sticker {
name: string;
height: number;
width: number;
description: string;
tags: string[];
id: string;
categorySection: string;
hpglFilePath: string;
svgLocation: string;
}

export interface StickerSelection {
id: string;
sticker: Sticker;
quantity: number;
}

export interface StagingArea {
accountId: string;
stickers: StickerSelection[];
}

export interface FitTestResult {
fits: boolean;
stickersToRemove: StickerSelection[][];
}

export const sticker: Sticker = {
name: '',
height: 3,
width: 3,
description: '',
tags: [],
id: '',
categorySection: '',
hpglFilePath: '',
svgLocation: '',
};

export const sticker_selection: StickerSelection = {
id: '',
sticker,
quantity: 1,
};

type RemovalCandidate = {
selection: StickerSelection;
index: number;
area: number;
};


function collectOptions(
removalCandidates: RemovalCandidate[],
stickersToRemove: number,
overflowAmount: number,
): StickerSelection[][] {
// Array for selected stickers to remove and the list of options to remove from the staging area
const options: StickerSelection[][] = [];
const selected: RemovalCandidate[] = [];

// Recursive function to find a list of options to remove from the stickers in the staging area.
function search(startIndex: number, freedArea: number): void {
// Stop the recursion once we have gathered enough removal options.
if (options.length >= MAX_REMOVAL_OPTIONS) {
return;
}

// If the current selected combination of stickers is equal to stickersToRemove,
// we examine if removing those stickers frees enough space
// to accommodate the overflowAmount. If so, we push that combination on to
// the list of options, sorted by their original staging area index.
if (selected.length === stickersToRemove) {
if (freedArea >= overflowAmount) {
options.push(
[...selected]
.sort((left, right) => left.index - right.index)
.map(({ selection }) => selection),
);
}
return;
}

// Are there enough candidates left to finish this combination?
const remainingCount = stickersToRemove - selected.length;
if (removalCandidates.length - startIndex < remainingCount) {
return;
}

// Make sure that the amount of area that could be freed by the remaining stickers is enough to accommodate the overflowAmount
let maximumRemainingArea = 0;
for (let index = startIndex; index < startIndex + remainingCount; index++) {
maximumRemainingArea += removalCandidates[index].area;
}
if (freedArea + maximumRemainingArea < overflowAmount) { // if not end
return;
}

// Recursively call this function with a new starting index.
for (
let index = startIndex;
index <= removalCandidates.length - remainingCount &&
options.length < MAX_REMOVAL_OPTIONS;
index++
) {
selected.push(removalCandidates[index]);
search(index + 1, freedArea + removalCandidates[index].area);
selected.pop();
}
}

// Start the recursive function to find options of stickers to remove and return results.
search(0, 0);
return options;
}

/**
* Checks whether a sticker selection can be added to the staging area.
*
* Calculates the total area used by the stickers in the staging area.
* Determines if adding the new sticker selection would exceed the staging area's capacity.
* If the new selection would exceed the capacity, it identifies combinations of existing selections that could be removed to make room.
* Returns a `FitTestResult` object containing whether the new selection fits and the suggested combinations to remove if it doesn't.
* STAGING_AREA_CAPACITY, MAX_REMOVAL_OPTIONS, MAX_STICKERS_PER_REMOVAL_OPTION define limits for the function's behavior.
*
* A result with `fits: false` and non-empty `stickersToRemove` means the new
* selection could fit if one of the suggested combinations were removed.
*/
export function fitTest(
stagingArea: Pick<StagingArea, 'stickers'>,
stickerToAdd: StickerSelection | null | undefined,
): FitTestResult {
if (!stickerToAdd) {
return { fits: false, stickersToRemove: [] };
}

// calculate the area of stickers in the staging area
const existingArea = stagingArea.stickers.reduce((totalArea, currentSticker) => {
return totalArea + currentSticker.sticker.height * currentSticker.sticker.width * currentSticker.quantity;
}, 0);

// calculate area of sticker(s) to add
const stickerArea = stickerToAdd.sticker.height * stickerToAdd.sticker.width * stickerToAdd.quantity;

// sticker cannot fit in area capacity?
if (stickerArea > STAGING_AREA_CAPACITY) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an issue for this ticket, but this is a good reminder that we need to think about how to handle sellers wanting to post stickers that can't fit alone on a half sheet

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

return { fits: false, stickersToRemove: [] };
}

const overflowAmount = existingArea + stickerArea - STAGING_AREA_CAPACITY;

if (overflowAmount > 0) {
// Create a list of stickers, their indexes, and areas in the staging area with their sizes
// sorted from greatest area to least area.
const candidates = stagingArea.stickers
.map((selection, index) => ({
selection,
index,
area: selection.sticker.height * selection.sticker.width * selection.quantity,
}))
.sort((left, right) => right.area - left.area);

// For each stickersToRemove up to either the number of canidates or the max stickers per removal option
for (
let stickersToRemove = 1;
stickersToRemove <= Math.min(candidates.length, MAX_STICKERS_PER_REMOVAL_OPTION);
stickersToRemove++
) {
const options = collectOptions(candidates, stickersToRemove, overflowAmount);
if (options.length > 0) {
return { fits: false, stickersToRemove: options };
}
}

// No combination of stickers found.
return { fits: false, stickersToRemove: [] };
}

return { fits: true, stickersToRemove: [] };
}
Loading