|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import path from "path" |
| 3 | +import { Session } from "../../src/session" |
| 4 | +import { Todo } from "../../src/session/todo" |
| 5 | +import { Bus } from "../../src/bus" |
| 6 | +import { Log } from "../../src/util/log" |
| 7 | +import { Instance } from "../../src/project/instance" |
| 8 | + |
| 9 | +const projectRoot = path.join(__dirname, "../..") |
| 10 | +Log.init({ print: false }) |
| 11 | + |
| 12 | +describe("Todo: CRUD lifecycle", () => { |
| 13 | + test("update then get returns todos in order", async () => { |
| 14 | + await Instance.provide({ |
| 15 | + directory: projectRoot, |
| 16 | + fn: async () => { |
| 17 | + const session = await Session.create({}) |
| 18 | + const todos = [ |
| 19 | + { content: "Fix SQL query", status: "pending", priority: "high" }, |
| 20 | + { content: "Add index", status: "in_progress", priority: "medium" }, |
| 21 | + { content: "Write docs", status: "completed", priority: "low" }, |
| 22 | + ] |
| 23 | + |
| 24 | + Todo.update({ sessionID: session.id, todos }) |
| 25 | + |
| 26 | + const result = Todo.get(session.id) |
| 27 | + expect(result).toHaveLength(3) |
| 28 | + expect(result[0].content).toBe("Fix SQL query") |
| 29 | + expect(result[0].status).toBe("pending") |
| 30 | + expect(result[0].priority).toBe("high") |
| 31 | + expect(result[1].content).toBe("Add index") |
| 32 | + expect(result[2].content).toBe("Write docs") |
| 33 | + |
| 34 | + await Session.remove(session.id) |
| 35 | + }, |
| 36 | + }) |
| 37 | + }) |
| 38 | + |
| 39 | + test("update with empty array clears all todos", async () => { |
| 40 | + await Instance.provide({ |
| 41 | + directory: projectRoot, |
| 42 | + fn: async () => { |
| 43 | + const session = await Session.create({}) |
| 44 | + |
| 45 | + Todo.update({ |
| 46 | + sessionID: session.id, |
| 47 | + todos: [{ content: "Task A", status: "pending", priority: "high" }], |
| 48 | + }) |
| 49 | + expect(Todo.get(session.id)).toHaveLength(1) |
| 50 | + |
| 51 | + Todo.update({ sessionID: session.id, todos: [] }) |
| 52 | + expect(Todo.get(session.id)).toHaveLength(0) |
| 53 | + |
| 54 | + await Session.remove(session.id) |
| 55 | + }, |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + test("update replaces previous todos entirely", async () => { |
| 60 | + await Instance.provide({ |
| 61 | + directory: projectRoot, |
| 62 | + fn: async () => { |
| 63 | + const session = await Session.create({}) |
| 64 | + |
| 65 | + Todo.update({ |
| 66 | + sessionID: session.id, |
| 67 | + todos: [ |
| 68 | + { content: "Old task 1", status: "pending", priority: "high" }, |
| 69 | + { content: "Old task 2", status: "pending", priority: "medium" }, |
| 70 | + ], |
| 71 | + }) |
| 72 | + expect(Todo.get(session.id)).toHaveLength(2) |
| 73 | + |
| 74 | + Todo.update({ |
| 75 | + sessionID: session.id, |
| 76 | + todos: [{ content: "New task", status: "in_progress", priority: "low" }], |
| 77 | + }) |
| 78 | + |
| 79 | + const result = Todo.get(session.id) |
| 80 | + expect(result).toHaveLength(1) |
| 81 | + expect(result[0].content).toBe("New task") |
| 82 | + expect(result[0].status).toBe("in_progress") |
| 83 | + |
| 84 | + await Session.remove(session.id) |
| 85 | + }, |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + test("get returns empty array for session with no todos", async () => { |
| 90 | + await Instance.provide({ |
| 91 | + directory: projectRoot, |
| 92 | + fn: async () => { |
| 93 | + const session = await Session.create({}) |
| 94 | + |
| 95 | + const result = Todo.get(session.id) |
| 96 | + expect(result).toEqual([]) |
| 97 | + |
| 98 | + await Session.remove(session.id) |
| 99 | + }, |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + test("publishes Todo.Event.Updated on update", async () => { |
| 104 | + await Instance.provide({ |
| 105 | + directory: projectRoot, |
| 106 | + fn: async () => { |
| 107 | + const session = await Session.create({}) |
| 108 | + let eventReceived = false |
| 109 | + let receivedTodos: Todo.Info[] = [] |
| 110 | + |
| 111 | + const unsub = Bus.subscribe(Todo.Event.Updated, (event) => { |
| 112 | + if (event.properties.sessionID === session.id) { |
| 113 | + eventReceived = true |
| 114 | + receivedTodos = event.properties.todos |
| 115 | + } |
| 116 | + }) |
| 117 | + |
| 118 | + const todos = [{ content: "Emit test", status: "pending", priority: "high" }] |
| 119 | + Todo.update({ sessionID: session.id, todos }) |
| 120 | + |
| 121 | + await new Promise((resolve) => setTimeout(resolve, 50)) |
| 122 | + |
| 123 | + unsub() |
| 124 | + |
| 125 | + expect(eventReceived).toBe(true) |
| 126 | + expect(receivedTodos).toHaveLength(1) |
| 127 | + expect(receivedTodos[0].content).toBe("Emit test") |
| 128 | + |
| 129 | + await Session.remove(session.id) |
| 130 | + }, |
| 131 | + }) |
| 132 | + }) |
| 133 | + |
| 134 | + test("todos are isolated between sessions", async () => { |
| 135 | + await Instance.provide({ |
| 136 | + directory: projectRoot, |
| 137 | + fn: async () => { |
| 138 | + const session1 = await Session.create({}) |
| 139 | + const session2 = await Session.create({}) |
| 140 | + |
| 141 | + Todo.update({ |
| 142 | + sessionID: session1.id, |
| 143 | + todos: [{ content: "Session 1 task", status: "pending", priority: "high" }], |
| 144 | + }) |
| 145 | + Todo.update({ |
| 146 | + sessionID: session2.id, |
| 147 | + todos: [ |
| 148 | + { content: "Session 2 task A", status: "pending", priority: "medium" }, |
| 149 | + { content: "Session 2 task B", status: "completed", priority: "low" }, |
| 150 | + ], |
| 151 | + }) |
| 152 | + |
| 153 | + expect(Todo.get(session1.id)).toHaveLength(1) |
| 154 | + expect(Todo.get(session1.id)[0].content).toBe("Session 1 task") |
| 155 | + expect(Todo.get(session2.id)).toHaveLength(2) |
| 156 | + |
| 157 | + await Session.remove(session1.id) |
| 158 | + await Session.remove(session2.id) |
| 159 | + }, |
| 160 | + }) |
| 161 | + }) |
| 162 | +}) |
0 commit comments