|
| 1 | +import { describe, test, expect, beforeEach } from "bun:test" |
| 2 | +import { TaskManager } from "../src/task-manager" |
| 3 | + |
| 4 | +describe("TaskManager", () => { |
| 5 | + let tm: TaskManager |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + tm = new TaskManager() |
| 9 | + }) |
| 10 | + |
| 11 | + // ── remove ──────────────────────────────────────────────────────────────── |
| 12 | + |
| 13 | + describe("remove", () => { |
| 14 | + test("returns true and removes an existing task", () => { |
| 15 | + const task = tm.add("Task A") |
| 16 | + expect(tm.remove(task.id)).toBe(true) |
| 17 | + expect(tm.get(task.id)).toBeUndefined() |
| 18 | + }) |
| 19 | + |
| 20 | + test("returns false for a non-existent id", () => { |
| 21 | + expect(tm.remove("999")).toBe(false) |
| 22 | + }) |
| 23 | + |
| 24 | + test("task is no longer in list after removal", () => { |
| 25 | + const task = tm.add("Task B") |
| 26 | + tm.remove(task.id) |
| 27 | + expect(tm.list().find((t) => t.id === task.id)).toBeUndefined() |
| 28 | + }) |
| 29 | + |
| 30 | + test("removing one task does not affect others", () => { |
| 31 | + const a = tm.add("A") |
| 32 | + const b = tm.add("B") |
| 33 | + tm.remove(a.id) |
| 34 | + expect(tm.get(b.id)).toBeDefined() |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + // ── update ──────────────────────────────────────────────────────────────── |
| 39 | + |
| 40 | + describe("update", () => { |
| 41 | + test("updates title", () => { |
| 42 | + const task = tm.add("Original") |
| 43 | + const updated = tm.update(task.id, { title: "Updated" }) |
| 44 | + expect(updated?.title).toBe("Updated") |
| 45 | + }) |
| 46 | + |
| 47 | + test("updates description", () => { |
| 48 | + const task = tm.add("Task", "medium", "old desc") |
| 49 | + const updated = tm.update(task.id, { description: "new desc" }) |
| 50 | + expect(updated?.description).toBe("new desc") |
| 51 | + }) |
| 52 | + |
| 53 | + test("updates priority", () => { |
| 54 | + const task = tm.add("Task", "low") |
| 55 | + const updated = tm.update(task.id, { priority: "high" }) |
| 56 | + expect(updated?.priority).toBe("high") |
| 57 | + }) |
| 58 | + |
| 59 | + test("updates multiple fields at once", () => { |
| 60 | + const task = tm.add("Old title", "low", "old desc") |
| 61 | + const updated = tm.update(task.id, { title: "New title", priority: "high", description: "new desc" }) |
| 62 | + expect(updated?.title).toBe("New title") |
| 63 | + expect(updated?.priority).toBe("high") |
| 64 | + expect(updated?.description).toBe("new desc") |
| 65 | + }) |
| 66 | + |
| 67 | + test("returns undefined for non-existent id", () => { |
| 68 | + expect(tm.update("999", { title: "X" })).toBeUndefined() |
| 69 | + }) |
| 70 | + |
| 71 | + test("does not change id", () => { |
| 72 | + const task = tm.add("Task") |
| 73 | + const originalId = task.id |
| 74 | + tm.update(task.id, { title: "New" }) |
| 75 | + expect(tm.get(originalId)?.id).toBe(originalId) |
| 76 | + }) |
| 77 | + |
| 78 | + test("does not change status", () => { |
| 79 | + const task = tm.add("Task") |
| 80 | + tm.complete(task.id) |
| 81 | + tm.update(task.id, { title: "New" }) |
| 82 | + expect(tm.get(task.id)?.status).toBe("completed") |
| 83 | + }) |
| 84 | + |
| 85 | + test("does not change createdAt", () => { |
| 86 | + const task = tm.add("Task") |
| 87 | + const originalCreatedAt = task.createdAt |
| 88 | + tm.update(task.id, { title: "New" }) |
| 89 | + expect(tm.get(task.id)?.createdAt).toEqual(originalCreatedAt) |
| 90 | + }) |
| 91 | + |
| 92 | + test("does not change completedAt", () => { |
| 93 | + const task = tm.add("Task") |
| 94 | + tm.complete(task.id) |
| 95 | + const completedAt = tm.get(task.id)!.completedAt |
| 96 | + tm.update(task.id, { title: "New" }) |
| 97 | + expect(tm.get(task.id)?.completedAt).toEqual(completedAt) |
| 98 | + }) |
| 99 | + |
| 100 | + test("returns the updated task object", () => { |
| 101 | + const task = tm.add("Task") |
| 102 | + const result = tm.update(task.id, { title: "Updated" }) |
| 103 | + expect(result).toBeDefined() |
| 104 | + expect(result?.title).toBe("Updated") |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + // ── sortBy ──────────────────────────────────────────────────────────────── |
| 109 | + |
| 110 | + describe("sortBy", () => { |
| 111 | + test("sorts by priority: high first, then medium, then low", () => { |
| 112 | + tm.add("Low task", "low") |
| 113 | + tm.add("High task", "high") |
| 114 | + tm.add("Medium task", "medium") |
| 115 | + |
| 116 | + const sorted = tm.sortBy("priority") |
| 117 | + expect(sorted[0].priority).toBe("high") |
| 118 | + expect(sorted[1].priority).toBe("medium") |
| 119 | + expect(sorted[2].priority).toBe("low") |
| 120 | + }) |
| 121 | + |
| 122 | + test("sorts by status: in_progress first, then pending, then completed", () => { |
| 123 | + const t1 = tm.add("Pending task") |
| 124 | + const t2 = tm.add("Completed task") |
| 125 | + const t3 = tm.add("In-progress task") |
| 126 | + tm.complete(t2.id) |
| 127 | + // manually set in_progress |
| 128 | + const task3 = tm.get(t3.id)! |
| 129 | + task3.status = "in_progress" |
| 130 | + |
| 131 | + const sorted = tm.sortBy("status") |
| 132 | + expect(sorted[0].status).toBe("in_progress") |
| 133 | + expect(sorted[1].status).toBe("pending") |
| 134 | + expect(sorted[2].status).toBe("completed") |
| 135 | + }) |
| 136 | + |
| 137 | + test("sorts by createdAt ascending (earliest first)", async () => { |
| 138 | + const t1 = tm.add("First") |
| 139 | + await new Promise((r) => setTimeout(r, 5)) |
| 140 | + const t2 = tm.add("Second") |
| 141 | + await new Promise((r) => setTimeout(r, 5)) |
| 142 | + const t3 = tm.add("Third") |
| 143 | + |
| 144 | + const sorted = tm.sortBy("createdAt") |
| 145 | + expect(sorted[0].id).toBe(t1.id) |
| 146 | + expect(sorted[1].id).toBe(t2.id) |
| 147 | + expect(sorted[2].id).toBe(t3.id) |
| 148 | + }) |
| 149 | + |
| 150 | + test("returns a new array (does not mutate internal state)", () => { |
| 151 | + tm.add("Low", "low") |
| 152 | + tm.add("High", "high") |
| 153 | + |
| 154 | + const before = tm.list() |
| 155 | + const sorted = tm.sortBy("priority") |
| 156 | + |
| 157 | + // sorted is a different array reference |
| 158 | + expect(sorted).not.toBe(before) |
| 159 | + // internal list order is unchanged |
| 160 | + expect(tm.list()[0].priority).toBe("low") |
| 161 | + }) |
| 162 | + |
| 163 | + test("returns all tasks when sorting", () => { |
| 164 | + tm.add("A", "high") |
| 165 | + tm.add("B", "medium") |
| 166 | + tm.add("C", "low") |
| 167 | + expect(tm.sortBy("priority")).toHaveLength(3) |
| 168 | + }) |
| 169 | + }) |
| 170 | +}) |
0 commit comments