pr-hw1/test/plan.test.ts
peppa-bot b4f907d1d5 PH-1: образ продукта, форматирование
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 10:02:48 +00:00

91 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { buildPlan } from "../src/plan.ts";
import type { BusyEvent, Task } from "../src/store.ts";
import { zonedParts, zonedToUtc } from "../src/time.ts";
const tz = "Asia/Almaty";
const L = (d: number, h: number, mi = 0) => zonedToUtc(2026, 12, d, h, mi, tz);
const hm = (d: Date) => {
const p = zonedParts(d, tz);
return `${p.day} ${String(p.hour).padStart(2, "0")}:${String(p.minute).padStart(2, "0")}`;
};
function task(id: string, due?: Date, extra: Partial<Task> = {}): Task {
return {
id,
kind: "task",
title: id,
due: due?.toISOString(),
dueHasTime: false,
source: "gmail",
sourceId: `g:${id}`,
sourceTitle: "Отчёт",
excerpt: "",
createdAt: "2026-12-01T00:00:00Z",
status: "open",
...extra,
};
}
function first<T>(xs: T[]): T {
const x = xs[0];
assert.ok(x);
return x;
}
const ev = (start: Date, end: Date): BusyEvent => ({
id: "e",
title: "Встреча",
start: start.toISOString(),
end: end.toISOString(),
});
describe("Требование: План дня с предложенным временем", () => {
it("Scenario: Предложение в свободное окно", () => {
const plan = buildPlan([task("подготовить отчёт", L(1, 23, 59))], [ev(L(1, 9), L(1, 11))], {
now: L(1, 8),
tz,
});
assert.equal(plan.length, 1);
assert.equal(hm(first(plan).start), "1 11:00");
assert.equal(hm(first(plan).end), "1 11:30");
assert.equal(first(plan).movedFromToday, false);
});
it("Scenario: Свободного времени нет", () => {
const plan = buildPlan([task("a", L(1, 23, 59))], [ev(L(1, 9), L(1, 19))], { now: L(1, 8), tz });
assert.equal(hm(first(plan).start), "2 09:00");
assert.equal(first(plan).movedFromToday, true);
});
it("задачи раскладываются по сроку и не пересекаются", () => {
const plan = buildPlan([task("late", L(5, 12)), task("soon", L(2, 12)), task("none")], [], {
now: L(1, 10, 5),
tz,
});
assert.deepEqual(
plan.map((p) => [p.taskId, hm(p.start)]),
[
["soon", "1 10:15"],
["late", "1 10:45"],
["none", "1 11:15"],
],
);
});
it("принятое время сохраняется и занимает окно", () => {
const accepted = { start: L(1, 9).toISOString(), end: L(1, 9, 30).toISOString() };
const plan = buildPlan([task("a", undefined, { accepted }), task("b")], [], { now: L(1, 8), tz });
assert.deepEqual(
plan.map((p) => [p.taskId, hm(p.start), p.accepted]),
[
["a", "1 09:00", true],
["b", "1 09:30", false],
],
);
});
it("выполненные задачи в план не попадают", () => {
assert.equal(buildPlan([task("a", undefined, { status: "done" })], [], { now: L(1, 8), tz }).length, 0);
});
});