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