요청 2: 급한 순서를 정해야 하는 요청
요청문: src/stock.mjs에 todayTasks(items, today) 함수를 만들어 줘. 아침에 카페 사장님이 보는 '오늘 할 일' 목록이야. 유통기한이 지난 것, 3일 안에 끝나는 것, 수량이 minQty보다 적은 것을 한 목록으로 합쳐서 급한 것부터 보여 주고 싶어. today는 'YYYY-MM-DD' 문자열이야. test/stock.test.mjs에 테스트도 추가하고 돌려 봐.
diff --git a/src/stock.mjs b/src/stock.mjs
index f81b897..b766fdb 100644
--- a/src/stock.mjs
+++ b/src/stock.mjs
@@ -19,3 +19,33 @@ export function orderList(items) {
.filter((it) => it.qty < it.minQty)
.map((it) => ({ name: it.name, need: it.minQty * 2 - it.qty, unit: it.unit }));
}
+
+const SOON_DAYS = 3;
+const RANK = { expired: 0, expiring: 1, low: 2 };
+
+// 아침 '오늘 할 일': 유통기한 지남 / 3일 안에 만료 / minQty 미만을 품목당 한 줄로 합쳐 급한 순으로
+// 반환: [{ name, reasons: ['expired'|'expiring'|'low', ...], daysLeft, qty, minQty, unit }]
+export function todayTasks(items, today) {
+ const tasks = [];
+ for (const it of items) {
+ const daysLeft = daysBetween(today, it.expires);
+ const reasons = [];
+ // 남은 수량이 없으면 버릴 것도 없으니 유통기한은 따지지 않음
+ if (it.qty > 0) {
+ if (daysLeft < 0) reasons.push('expired');
+ else if (daysLeft <= SOON_DAYS) reasons.push('expiring');
+ }
+ if (it.qty < it.minQty) reasons.push('low');
+ if (reasons.length) {
+ tasks.push({ name: it.name, reasons, daysLeft, qty: it.qty, minQty: it.minQty, unit: it.unit });
+ }
+ }
+ return tasks.sort((a, b) => {
+ const ra = RANK[a.reasons[0]];
+ const rb = RANK[b.reasons[0]];
+ if (ra !== rb) return ra - rb;
+ // 유통기한 문제끼리는 남은 날짜가 적은 순, 재고 부족끼리는 모자란 비율이 큰 순
+ if (ra < RANK.low) return a.daysLeft - b.daysLeft;
+ return a.qty / a.minQty - b.qty / b.minQty;
+ });
+}
diff --git a/test/stock.test.mjs b/test/stock.test.mjs
index 71de632..1be585c 100644
--- a/test/stock.test.mjs
+++ b/test/stock.test.mjs
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';
-import { lowStock, orderList, daysBetween } from '../src/stock.mjs';
+import { readFileSync } from 'node:fs';
+import { lowStock, orderList, daysBetween, todayTasks } from '../src/stock.mjs';
const items = [
{ name: '우유', qty: 3, unit: 'L', expires: '2026-10-20', minQty: 5 },
{ name: '원두', qty: 4, unit: 'kg', expires: '2027-01-10', minQty: 2 },
@@ -8,3 +9,35 @@ assert.deepEqual(lowStock(items), ['우유']);
assert.deepEqual(orderList(items), [{ name: '우유', need: 7, unit: 'L' }]);
assert.equal(daysBetween('2026-10-18', '2026-10-20'), 2);
console.log('기본 테스트 통과');
+
+// todayTasks: data.json 기준, 오늘 = 2026-10-18
+const data = JSON.parse(readFileSync(new URL('../data.json', import.meta.url), 'utf8'));
+const tasks = todayTasks(data, '2026-10-18');
+assert.deepEqual(
+ tasks.map((t) => [t.name, t.reasons, t.daysLeft]),
+ [
+ ['딸기', ['expired'], -1], // 어제 지남 → 가장 급함
+ ['생크림', ['expiring'], 0], // 오늘까지
+ ['우유', ['expiring', 'low'], 2], // 두 사유를 한 줄로 합침
+ ['바닐라 시럽', ['expiring'], 3], // 3일째 경계 포함
+ ['치즈케이크', ['low'], 1], // 수량 0 → 유통기한은 무시, 재고 부족만
+ ],
+);
+assert.ok(!tasks.some((t) => t.name === '원두')); // 문제없는 품목은 빠짐
+
+// 재고 부족끼리는 모자란 비율이 큰 순
+const lows = todayTasks(
+ [
+ { name: 'A', qty: 4, unit: '개', expires: '2027-01-01', minQty: 5 },
+ { name: 'B', qty: 1, unit: '개', expires: '2027-01-01', minQty: 5 },
+ ],
+ '2026-10-18',
+);
+assert.deepEqual(lows.map((t) => t.name), ['B', 'A']);
+
+// 4일 남은 것은 아직 할 일 아님
+assert.deepEqual(
+ todayTasks([{ name: 'C', qty: 3, unit: '개', expires: '2026-10-22', minQty: 1 }], '2026-10-18'),
+ [],
+);
+console.log('todayTasks 테스트 통과');