요청 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..db6a493 100644
--- a/src/stock.mjs
+++ b/src/stock.mjs
@@ -19,3 +19,27 @@ export function orderList(items) {
.filter((it) => it.qty < it.minQty)
.map((it) => ({ name: it.name, need: it.minQty * 2 - it.qty, unit: it.unit }));
}
+
+// 아침 '오늘 할 일': 유통기한 지남 / 3일 내 만료 / minQty 미만을 한 목록으로.
+// 한 재료가 여러 조건에 걸리면 한 줄에 reasons로 모은다.
+// 급한 순서: 기한 지남 → 기한 임박(남은 날 적은 순) → 재고 부족만(부족 비율 큰 순).
+// 유통기한 당일(daysLeft 0)은 아직 쓸 수 있으므로 '임박'으로 본다.
+export function todayTasks(items, today) {
+ const tasks = [];
+ for (const it of items) {
+ const daysLeft = daysBetween(today, it.expires);
+ const reasons = [];
+ if (daysLeft < 0) reasons.push('expired');
+ else if (daysLeft <= 3) reasons.push('expiring');
+ if (it.qty < it.minQty) reasons.push('low');
+ if (reasons.length === 0) continue;
+ tasks.push({ name: it.name, reasons, daysLeft, qty: it.qty, minQty: it.minQty, unit: it.unit });
+ }
+ const rank = (t) =>
+ t.reasons.includes('expired') ? 0 : t.reasons.includes('expiring') ? 1 : 2;
+ return tasks.sort(
+ (a, b) =>
+ rank(a) - rank(b) ||
+ (rank(a) < 2 ? a.daysLeft - b.daysLeft : a.qty / a.minQty - b.qty / b.minQty),
+ );
+}
diff --git a/test/stock.test.mjs b/test/stock.test.mjs
index 71de632..3131c65 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,32 @@ 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'], 1],
+ ['우유', ['expiring', 'low'], 2],
+ ['바닐라 시럽', ['expiring'], 3], // 3일 뒤까지 포함
+ ],
+);
+assert.ok(!tasks.some((t) => t.name === '원두'));
+
+// 재고 부족만 걸린 것은 기한 문제 뒤에, 부족 비율 큰 순
+const lowOnly = todayTasks(
+ [
+ { name: 'A', qty: 4, unit: '개', expires: '2027-01-01', minQty: 5 },
+ { name: 'B', qty: 0, unit: '개', expires: '2027-01-01', minQty: 5 },
+ { name: 'C', qty: 9, unit: '개', expires: '2026-10-22', minQty: 1 },
+ { name: 'D', qty: 9, unit: '개', expires: '2026-10-22', minQty: 1 },
+ ],
+ '2026-10-18',
+);
+assert.deepEqual(lowOnly.map((t) => t.name), ['B', 'A']); // C·D는 4일 뒤라 제외
+assert.deepEqual(todayTasks([], '2026-10-18'), []);
+console.log('todayTasks 테스트 통과');