@critical-path/core 0.35.0 → 0.36.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/README.md +11 -0
  2. package/dist/engine/index.d.ts +2 -0
  3. package/dist/engine/index.d.ts.map +1 -1
  4. package/dist/engine/index.js +25 -2
  5. package/dist/engine/index.js.map +1 -1
  6. package/dist/types/index.d.ts +11 -0
  7. package/dist/types/index.d.ts.map +1 -1
  8. package/package.json +5 -1
  9. package/CHANGELOG.md +0 -584
  10. package/dist/deliverables.test.d.ts +0 -2
  11. package/dist/deliverables.test.d.ts.map +0 -1
  12. package/dist/deliverables.test.js +0 -232
  13. package/dist/deliverables.test.js.map +0 -1
  14. package/dist/domain/calendar.test.d.ts +0 -2
  15. package/dist/domain/calendar.test.d.ts.map +0 -1
  16. package/dist/domain/calendar.test.js +0 -181
  17. package/dist/domain/calendar.test.js.map +0 -1
  18. package/dist/domain/cpm.test.d.ts +0 -2
  19. package/dist/domain/cpm.test.d.ts.map +0 -1
  20. package/dist/domain/cpm.test.js +0 -166
  21. package/dist/domain/cpm.test.js.map +0 -1
  22. package/dist/domain/ddd.test.d.ts +0 -2
  23. package/dist/domain/ddd.test.d.ts.map +0 -1
  24. package/dist/domain/ddd.test.js +0 -349
  25. package/dist/domain/ddd.test.js.map +0 -1
  26. package/dist/domain/ladder.test.d.ts +0 -2
  27. package/dist/domain/ladder.test.d.ts.map +0 -1
  28. package/dist/domain/ladder.test.js +0 -213
  29. package/dist/domain/ladder.test.js.map +0 -1
  30. package/dist/domain/metrics.test.d.ts +0 -2
  31. package/dist/domain/metrics.test.d.ts.map +0 -1
  32. package/dist/domain/metrics.test.js +0 -252
  33. package/dist/domain/metrics.test.js.map +0 -1
  34. package/dist/domain/task-duration.test.d.ts +0 -2
  35. package/dist/domain/task-duration.test.d.ts.map +0 -1
  36. package/dist/domain/task-duration.test.js +0 -307
  37. package/dist/domain/task-duration.test.js.map +0 -1
  38. package/dist/domain/workload.test.d.ts +0 -2
  39. package/dist/domain/workload.test.d.ts.map +0 -1
  40. package/dist/domain/workload.test.js +0 -261
  41. package/dist/domain/workload.test.js.map +0 -1
  42. package/dist/engine/actor.test.d.ts +0 -2
  43. package/dist/engine/actor.test.d.ts.map +0 -1
  44. package/dist/engine/actor.test.js +0 -54
  45. package/dist/engine/actor.test.js.map +0 -1
  46. package/dist/engine/authorization.test.d.ts +0 -2
  47. package/dist/engine/authorization.test.d.ts.map +0 -1
  48. package/dist/engine/authorization.test.js +0 -154
  49. package/dist/engine/authorization.test.js.map +0 -1
  50. package/dist/engine/cascade.test.d.ts +0 -2
  51. package/dist/engine/cascade.test.d.ts.map +0 -1
  52. package/dist/engine/cascade.test.js +0 -99
  53. package/dist/engine/cascade.test.js.map +0 -1
  54. package/dist/engine/config.test.d.ts +0 -2
  55. package/dist/engine/config.test.d.ts.map +0 -1
  56. package/dist/engine/config.test.js +0 -46
  57. package/dist/engine/config.test.js.map +0 -1
  58. package/dist/engine/entity-events.test.d.ts +0 -2
  59. package/dist/engine/entity-events.test.d.ts.map +0 -1
  60. package/dist/engine/entity-events.test.js +0 -61
  61. package/dist/engine/entity-events.test.js.map +0 -1
  62. package/dist/index.test.d.ts +0 -2
  63. package/dist/index.test.d.ts.map +0 -1
  64. package/dist/index.test.js +0 -429
  65. package/dist/index.test.js.map +0 -1
  66. package/dist/plugins/plugins.test.d.ts +0 -2
  67. package/dist/plugins/plugins.test.d.ts.map +0 -1
  68. package/dist/plugins/plugins.test.js +0 -137
  69. package/dist/plugins/plugins.test.js.map +0 -1
  70. package/dist/schemas/schemas.test.d.ts +0 -2
  71. package/dist/schemas/schemas.test.d.ts.map +0 -1
  72. package/dist/schemas/schemas.test.js +0 -58
  73. package/dist/schemas/schemas.test.js.map +0 -1
  74. package/dist/status.test.d.ts +0 -2
  75. package/dist/status.test.d.ts.map +0 -1
  76. package/dist/status.test.js +0 -295
  77. package/dist/status.test.js.map +0 -1
  78. package/dist/storage/storage.test.d.ts +0 -2
  79. package/dist/storage/storage.test.d.ts.map +0 -1
  80. package/dist/storage/storage.test.js +0 -236
  81. package/dist/storage/storage.test.js.map +0 -1
  82. package/dist/store/conformance.test.d.ts +0 -2
  83. package/dist/store/conformance.test.d.ts.map +0 -1
  84. package/dist/store/conformance.test.js +0 -15
  85. package/dist/store/conformance.test.js.map +0 -1
  86. package/dist/store/firebase.test.d.ts +0 -2
  87. package/dist/store/firebase.test.d.ts.map +0 -1
  88. package/dist/store/firebase.test.js +0 -103
  89. package/dist/store/firebase.test.js.map +0 -1
  90. package/dist/store/query.test.d.ts +0 -2
  91. package/dist/store/query.test.d.ts.map +0 -1
  92. package/dist/store/query.test.js +0 -89
  93. package/dist/store/query.test.js.map +0 -1
  94. package/dist/store/sqlite.test.d.ts +0 -2
  95. package/dist/store/sqlite.test.d.ts.map +0 -1
  96. package/dist/store/sqlite.test.js +0 -177
  97. package/dist/store/sqlite.test.js.map +0 -1
  98. package/dist/task-state.test.d.ts +0 -2
  99. package/dist/task-state.test.d.ts.map +0 -1
  100. package/dist/task-state.test.js +0 -423
  101. package/dist/task-state.test.js.map +0 -1
  102. package/dist/use-case-validation.test.d.ts +0 -2
  103. package/dist/use-case-validation.test.d.ts.map +0 -1
  104. package/dist/use-case-validation.test.js +0 -281
  105. package/dist/use-case-validation.test.js.map +0 -1
  106. package/dist/utils/fractional-index.test.d.ts +0 -2
  107. package/dist/utils/fractional-index.test.d.ts.map +0 -1
  108. package/dist/utils/fractional-index.test.js +0 -182
  109. package/dist/utils/fractional-index.test.js.map +0 -1
  110. package/dist/utils/key.test.d.ts +0 -2
  111. package/dist/utils/key.test.d.ts.map +0 -1
  112. package/dist/utils/key.test.js +0 -65
  113. package/dist/utils/key.test.js.map +0 -1
  114. package/dist/utils/mentions.test.d.ts +0 -2
  115. package/dist/utils/mentions.test.d.ts.map +0 -1
  116. package/dist/utils/mentions.test.js +0 -114
  117. package/dist/utils/mentions.test.js.map +0 -1
  118. package/dist/webhooks/webhooks.test.d.ts +0 -2
  119. package/dist/webhooks/webhooks.test.d.ts.map +0 -1
  120. package/dist/webhooks/webhooks.test.js +0 -154
  121. package/dist/webhooks/webhooks.test.js.map +0 -1
  122. package/src/deliverables.test.ts +0 -284
  123. package/src/domain/authorization.ts +0 -82
  124. package/src/domain/calendar.test.ts +0 -216
  125. package/src/domain/calendar.ts +0 -502
  126. package/src/domain/cpm.test.ts +0 -189
  127. package/src/domain/cpm.ts +0 -258
  128. package/src/domain/custom-fields.ts +0 -172
  129. package/src/domain/ddd.test.ts +0 -442
  130. package/src/domain/entities.ts +0 -798
  131. package/src/domain/errors.ts +0 -38
  132. package/src/domain/events.ts +0 -369
  133. package/src/domain/graph.ts +0 -70
  134. package/src/domain/index.ts +0 -12
  135. package/src/domain/ladder.test.ts +0 -251
  136. package/src/domain/ladder.ts +0 -474
  137. package/src/domain/metrics.test.ts +0 -288
  138. package/src/domain/metrics.ts +0 -497
  139. package/src/domain/task-duration.test.ts +0 -354
  140. package/src/domain/workload.test.ts +0 -311
  141. package/src/domain/workload.ts +0 -457
  142. package/src/engine/actor.test.ts +0 -63
  143. package/src/engine/authorization.test.ts +0 -199
  144. package/src/engine/cascade.test.ts +0 -121
  145. package/src/engine/config.test.ts +0 -51
  146. package/src/engine/entity-events.test.ts +0 -67
  147. package/src/engine/index.ts +0 -2472
  148. package/src/index.test.ts +0 -514
  149. package/src/index.ts +0 -9
  150. package/src/plugins/index.ts +0 -103
  151. package/src/plugins/plugins.test.ts +0 -161
  152. package/src/schemas/index.ts +0 -402
  153. package/src/schemas/schemas.test.ts +0 -116
  154. package/src/status.test.ts +0 -354
  155. package/src/storage/file-storage.ts +0 -200
  156. package/src/storage/firebase-storage.ts +0 -209
  157. package/src/storage/index.ts +0 -3
  158. package/src/storage/s3.ts +0 -151
  159. package/src/storage/storage.test.ts +0 -274
  160. package/src/store/conformance.test.ts +0 -15
  161. package/src/store/firebase.test.ts +0 -131
  162. package/src/store/firebase.ts +0 -658
  163. package/src/store/index.ts +0 -623
  164. package/src/store/query.test.ts +0 -110
  165. package/src/store/query.ts +0 -113
  166. package/src/store/sqlite.test.ts +0 -218
  167. package/src/store/sqlite.ts +0 -1480
  168. package/src/task-state.test.ts +0 -506
  169. package/src/testing/conformance.ts +0 -319
  170. package/src/testing/index.ts +0 -1
  171. package/src/types/index.ts +0 -859
  172. package/src/use-case-validation.test.ts +0 -324
  173. package/src/utils/fractional-index.test.ts +0 -228
  174. package/src/utils/fractional-index.ts +0 -254
  175. package/src/utils/index.ts +0 -7
  176. package/src/utils/key.test.ts +0 -76
  177. package/src/utils/key.ts +0 -62
  178. package/src/utils/mentions.test.ts +0 -132
  179. package/src/utils/mentions.ts +0 -131
  180. package/src/utils/status.ts +0 -419
  181. package/src/utils/workflow.ts +0 -306
  182. package/src/webhooks/dispatcher.ts +0 -190
  183. package/src/webhooks/index.ts +0 -2
  184. package/src/webhooks/signature.ts +0 -57
  185. package/src/webhooks/webhooks.test.ts +0 -201
  186. package/tsconfig.json +0 -8
  187. package/tsconfig.tsbuildinfo +0 -1
@@ -1,442 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest';
2
- import {
3
- CriticalPathEngine,
4
- TaskEntity,
5
- ProjectEntity,
6
- DomainEventBus,
7
- CircularDependencyError,
8
- CustomFieldValidationError,
9
- DEFAULT_SOFTWARE_WORKFLOW,
10
- DEFAULT_SIMPLE_WORKFLOW,
11
- WorkflowValidationError,
12
- validateTransition,
13
- getAllowedNextStatuses,
14
- getAllowedPreviousStatuses,
15
- detectDependencyCycle,
16
- validateCustomFieldValues,
17
- type CustomFieldDefinition,
18
- type TaskStatusChangedEvent,
19
- type TimeLoggedEvent
20
- } from '../index.js';
21
-
22
- describe('Domain-Driven Design (DDD) Enhancements Suite', () => {
23
- describe('Domain Event Bus & Typed Events', () => {
24
- it('publishes and subscribes to typed domain events', async () => {
25
- const bus = new DomainEventBus();
26
- const statusChangedHandler = vi.fn();
27
- const wildcardHandler = vi.fn();
28
-
29
- const unsubscribe = bus.subscribe<TaskStatusChangedEvent>('task.status_changed', statusChangedHandler);
30
- bus.subscribe('*', wildcardHandler);
31
-
32
- const sampleEvent: TaskStatusChangedEvent = {
33
- id: 'evt_123',
34
- name: 'task.status_changed',
35
- aggregateId: 'task_1',
36
- aggregateType: 'Task',
37
- occurredAt: new Date().toISOString(),
38
- payload: {
39
- task: {
40
- id: 'task_1',
41
- projectId: 'proj_1',
42
- title: 'Sample Task',
43
- status: 'in_progress',
44
- priority: 'high',
45
- createdAt: '2026-01-01T00:00:00Z',
46
- updatedAt: '2026-01-01T00:00:00Z'
47
- },
48
- previousStatus: 'todo',
49
- newStatus: 'in_progress'
50
- }
51
- };
52
-
53
- await bus.publish(sampleEvent);
54
-
55
- expect(statusChangedHandler).toHaveBeenCalledWith(sampleEvent);
56
- expect(wildcardHandler).toHaveBeenCalledWith(sampleEvent);
57
-
58
- unsubscribe();
59
- await bus.publish(sampleEvent);
60
- expect(statusChangedHandler).toHaveBeenCalledTimes(1);
61
- expect(wildcardHandler).toHaveBeenCalledTimes(2);
62
- });
63
-
64
- it('publishes domain events from CriticalPathEngine during aggregate mutations', async () => {
65
- const engine = new CriticalPathEngine();
66
- const taskCreatedEvents: any[] = [];
67
- const statusChangedEvents: any[] = [];
68
- const timeLoggedEvents: any[] = [];
69
-
70
- engine.events.subscribe('task.created', (evt) => {
71
- taskCreatedEvents.push(evt);
72
- });
73
- engine.events.subscribe('task.status_changed', (evt) => {
74
- statusChangedEvents.push(evt);
75
- });
76
- engine.events.subscribe('time.logged', (evt) => {
77
- timeLoggedEvents.push(evt);
78
- });
79
-
80
- const project = await engine.createProject({
81
- name: 'DDD Mobile App'
82
- });
83
-
84
- const task = await engine.createTask({
85
- projectId: project.id,
86
- title: 'Design Domain Aggregates',
87
- status: 'todo'
88
- });
89
-
90
- expect(taskCreatedEvents.length).toBe(1);
91
- expect(taskCreatedEvents[0].aggregateId).toBe(task.id);
92
- expect(taskCreatedEvents[0].payload.task.title).toBe('Design Domain Aggregates');
93
-
94
- await engine.updateTask(task.id, { status: 'in_progress' });
95
- expect(statusChangedEvents.length).toBe(1);
96
- expect(statusChangedEvents[0].payload.fromStatus || statusChangedEvents[0].payload.previousStatus).toBe('todo');
97
- expect(statusChangedEvents[0].payload.toStatus || statusChangedEvents[0].payload.newStatus).toBe('in_progress');
98
-
99
- await engine.logTime({
100
- taskId: task.id,
101
- hours: 3.5,
102
- description: 'Implemented aggregate root logic'
103
- });
104
- expect(timeLoggedEvents.length).toBe(1);
105
- expect(timeLoggedEvents[0].payload.timeEntry.hours).toBe(3.5);
106
-
107
- const updatedTask = await engine.getTask(task.id);
108
- expect(updatedTask?.loggedHours).toBe(3.5);
109
- });
110
- });
111
-
112
- describe('Value Object Validation for Custom Fields', () => {
113
- const definitions: CustomFieldDefinition[] = [
114
- { id: 'cf_sp', key: 'storyPoints', label: 'Story Points', type: 'number', required: true },
115
- { id: 'cf_severity', key: 'severity', label: 'Severity', type: 'single_select', options: ['low', 'medium', 'high'] },
116
- { id: 'cf_tags', key: 'domains', label: 'Domains', type: 'multi_select', options: ['frontend', 'backend', 'devops'] },
117
- { id: 'cf_date', key: 'targetRelease', label: 'Target Release', type: 'date' }
118
- ];
119
-
120
- it('passes validation when valid custom field values are provided', () => {
121
- expect(() => {
122
- validateCustomFieldValues(definitions, {
123
- storyPoints: 5,
124
- severity: 'high',
125
- domains: ['frontend', 'backend'],
126
- targetRelease: '2026-10-01T00:00:00.000Z'
127
- });
128
- }).not.toThrow();
129
- });
130
-
131
- it('throws CustomFieldValidationError when required field is missing', () => {
132
- expect(() => {
133
- validateCustomFieldValues(definitions, {
134
- severity: 'high'
135
- });
136
- }).toThrow(CustomFieldValidationError);
137
- });
138
-
139
- it('throws CustomFieldValidationError on type mismatch or invalid option', () => {
140
- expect(() => {
141
- validateCustomFieldValues(definitions, {
142
- storyPoints: 'five' as any
143
- });
144
- }).toThrow(CustomFieldValidationError);
145
-
146
- expect(() => {
147
- validateCustomFieldValues(definitions, {
148
- storyPoints: 5,
149
- severity: 'critical' // Not in ['low', 'medium', 'high']
150
- });
151
- }).toThrow(CustomFieldValidationError);
152
-
153
- expect(() => {
154
- validateCustomFieldValues(definitions, {
155
- storyPoints: 5,
156
- domains: ['frontend', 'ai'] // 'ai' not allowed
157
- });
158
- }).toThrow(CustomFieldValidationError);
159
- });
160
-
161
- it('integrates custom field validation in CriticalPathEngine task creation and update', async () => {
162
- const engine = new CriticalPathEngine();
163
- const project = await engine.createProject({
164
- name: 'Strict Project',
165
- customFieldDefinitions: definitions
166
- });
167
-
168
- // Fails creation due to missing required storyPoints
169
- await expect(
170
- engine.createTask({
171
- projectId: project.id,
172
- title: 'Invalid Task',
173
- customFields: { severity: 'low' }
174
- })
175
- ).rejects.toThrow(CustomFieldValidationError);
176
-
177
- // Successfully creates with valid fields
178
- const validTask = await engine.createTask({
179
- projectId: project.id,
180
- title: 'Valid Task',
181
- customFields: { storyPoints: 8, severity: 'medium' }
182
- });
183
- expect(validTask.id).toBeDefined();
184
-
185
- // Fails update with invalid option
186
- await expect(
187
- engine.updateTask(validTask.id, {
188
- customFields: { severity: 'extreme' }
189
- })
190
- ).rejects.toThrow(CustomFieldValidationError);
191
- });
192
- });
193
-
194
- describe('DAG Cycle Detection & Graph Invariant Protection', () => {
195
- it('detects self-dependency cycles', () => {
196
- const result = detectDependencyCycle([], { taskId: 'task_1', dependsOnTaskId: 'task_1' });
197
- expect(result.hasCycle).toBe(true);
198
- });
199
-
200
- it('detects multi-node transitive cycles', () => {
201
- // Existing: A -> B, B -> C
202
- const existing = [
203
- { id: '1', taskId: 'task_A', dependsOnTaskId: 'task_B', type: 'blocking' as const },
204
- { id: '2', taskId: 'task_B', dependsOnTaskId: 'task_C', type: 'blocking' as const }
205
- ];
206
-
207
- // Try to add C -> A (creates C -> A -> B -> C cycle)
208
- const result = detectDependencyCycle(existing, { taskId: 'task_C', dependsOnTaskId: 'task_A' });
209
- expect(result.hasCycle).toBe(true);
210
- expect(result.cyclePath).toEqual(['task_C', 'task_A', 'task_B', 'task_C']);
211
- });
212
-
213
- it('allows valid acyclic dependencies', () => {
214
- const existing = [
215
- { id: '1', taskId: 'task_A', dependsOnTaskId: 'task_B', type: 'blocking' as const },
216
- { id: '2', taskId: 'task_B', dependsOnTaskId: 'task_C', type: 'blocking' as const }
217
- ];
218
-
219
- // Add A -> D (valid DAG)
220
- const result = detectDependencyCycle(existing, { taskId: 'task_A', dependsOnTaskId: 'task_D' });
221
- expect(result.hasCycle).toBe(false);
222
- });
223
-
224
- it('throws CircularDependencyError when engine.addDependency attempts to create a cycle', async () => {
225
- const engine = new CriticalPathEngine();
226
- const project = await engine.createProject({ name: 'Graph Test' });
227
-
228
- const taskA = await engine.createTask({ projectId: project.id, title: 'Task A' });
229
- const taskB = await engine.createTask({ projectId: project.id, title: 'Task B' });
230
- const taskC = await engine.createTask({ projectId: project.id, title: 'Task C' });
231
-
232
- await engine.addDependency({
233
- taskId: taskA.id,
234
- dependsOnTaskId: taskB.id,
235
- type: 'blocking'
236
- });
237
-
238
- await engine.addDependency({
239
- taskId: taskB.id,
240
- dependsOnTaskId: taskC.id,
241
- type: 'blocking'
242
- });
243
-
244
- // Attempt circular dependency: C depends on A
245
- await expect(
246
- engine.addDependency({
247
- taskId: taskC.id,
248
- dependsOnTaskId: taskA.id,
249
- type: 'blocking'
250
- })
251
- ).rejects.toThrow(CircularDependencyError);
252
- });
253
-
254
- it('detects cycles longer than the immediate neighbours in engine.addDependency', async () => {
255
- const engine = new CriticalPathEngine();
256
- const project = await engine.createProject({ name: 'Long Cycle' });
257
- const [a, b, c, d] = await Promise.all(
258
- ['A', 'B', 'C', 'D'].map((title) => engine.createTask({ projectId: project.id, title }))
259
- );
260
-
261
- await engine.addDependency({ taskId: a.id, dependsOnTaskId: b.id, type: 'blocking' });
262
- await engine.addDependency({ taskId: b.id, dependsOnTaskId: c.id, type: 'blocking' });
263
- await engine.addDependency({ taskId: c.id, dependsOnTaskId: d.id, type: 'blocking' });
264
-
265
- // D -> A closes A -> B -> C -> D -> A
266
- await expect(
267
- engine.addDependency({ taskId: d.id, dependsOnTaskId: a.id, type: 'blocking' })
268
- ).rejects.toThrow(CircularDependencyError);
269
-
270
- // A diamond is not a cycle: A -> D is fine because D does not reach A
271
- await expect(
272
- engine.addDependency({ taskId: a.id, dependsOnTaskId: d.id, type: 'blocking' })
273
- ).resolves.toMatchObject({ taskId: a.id, dependsOnTaskId: d.id });
274
- });
275
- });
276
-
277
- describe('Rich Domain Aggregates (TaskEntity & ProjectEntity)', () => {
278
- it('encapsulates state transitions, timestamps, and raises uncommitted domain events', () => {
279
- const task = TaskEntity.create({
280
- projectId: 'proj_1',
281
- title: 'Rich Entity Task',
282
- status: 'todo',
283
- priority: 'high'
284
- });
285
-
286
- expect(task.getUncommittedEvents().length).toBe(1);
287
- expect(task.getUncommittedEvents()[0].name).toBe('task.created');
288
- task.clearEvents();
289
-
290
- // Transition to in_progress
291
- task.transitionTo('in_progress', DEFAULT_SOFTWARE_WORKFLOW);
292
- expect(task.status).toBe('in_progress');
293
- expect(task.actualStartDate).toBeDefined();
294
- expect(task.getUncommittedEvents().length).toBe(1);
295
- expect(task.getUncommittedEvents()[0].name).toBe('task.status_changed');
296
- task.clearEvents();
297
-
298
- // Log time
299
- task.logTime({ hours: 4.5, isBillable: true });
300
- expect(task.loggedHours).toBe(4.5);
301
- expect(task.billableHours).toBe(4.5);
302
- expect(task.getUncommittedEvents().length).toBe(1);
303
- expect(task.getUncommittedEvents()[0].name).toBe('time.logged');
304
- task.clearEvents();
305
-
306
- // Complete task
307
- task.transitionTo('done', DEFAULT_SOFTWARE_WORKFLOW);
308
- expect(task.status).toBe('done');
309
- expect(task.actualEndDate).toBeDefined();
310
- expect(task.progress).toBe(100);
311
- });
312
-
313
- it('manages task todo items (add, toggle, remove) and preserves them in toPlain', () => {
314
- const task = TaskEntity.create({
315
- projectId: 'proj_1',
316
- title: 'Task with Todos',
317
- status: 'todo',
318
- priority: 'medium'
319
- });
320
-
321
- expect(task.todos).toEqual([]);
322
- task.clearEvents();
323
-
324
- // Add a todo
325
- const item1 = task.addTodo('Write unit tests');
326
- expect(item1.id).toBeDefined();
327
- expect(item1.title).toBe('Write unit tests');
328
- expect(item1.completed).toBe(false);
329
- expect(task.todos?.length).toBe(1);
330
- expect(task.getUncommittedEvents().length).toBe(1);
331
- expect(task.getUncommittedEvents()[0].name).toBe('task.updated');
332
- task.clearEvents();
333
-
334
- // Add second todo
335
- const item2 = task.addTodo('Run storybook build');
336
- expect(task.todos?.length).toBe(2);
337
-
338
- // Toggle first todo
339
- task.toggleTodo(item1.id, true);
340
- expect(task.todos?.[0].completed).toBe(true);
341
- expect(task.todos?.[0].completedAt).toBeDefined();
342
-
343
- // Toggle back
344
- task.toggleTodo(item1.id);
345
- expect(task.todos?.[0].completed).toBe(false);
346
- expect(task.todos?.[0].completedAt).toBeUndefined();
347
-
348
- // Remove second todo
349
- task.removeTodo(item2.id);
350
- expect(task.todos?.length).toBe(1);
351
- expect(task.todos?.[0].id).toBe(item1.id);
352
-
353
- // Verify toPlain serialization
354
- const plain = task.toPlain();
355
- expect(plain.todos).toEqual(task.todos);
356
- expect(plain.todos).not.toBe(task.todos); // should be cloned
357
- });
358
-
359
- it('creates ProjectEntity and handles key generation and custom field validation', () => {
360
- const project = ProjectEntity.create({
361
- name: 'Critical Path Core',
362
- customFieldDefinitions: [
363
- { id: 'cf_client', key: 'clientName', label: 'Client Name', type: 'text', required: true }
364
- ]
365
- });
366
-
367
- expect(project.key).toBe('CPC');
368
- expect(project.getUncommittedEvents().length).toBe(1);
369
- expect(project.getUncommittedEvents()[0].name).toBe('project.created');
370
-
371
- expect(() => {
372
- project.validateCustomFields({});
373
- }).toThrow(CustomFieldValidationError);
374
-
375
- expect(() => {
376
- project.validateCustomFields({ clientName: 'Acme Corp' });
377
- }).not.toThrow();
378
- });
379
- });
380
-
381
- describe('Workflow Backward Status Transitions', () => {
382
- it('allows moving task backwards to previous statuses in simple workflow', () => {
383
- // In progress -> todo should be allowed
384
- expect(validateTransition(DEFAULT_SIMPLE_WORKFLOW, 'in_progress', 'todo')).toBe(true);
385
- expect(getAllowedPreviousStatuses(DEFAULT_SIMPLE_WORKFLOW, 'in_progress')).toEqual(['todo']);
386
- // Forward should only contain downstream statuses ('done'), not backward statuses ('todo')
387
- expect(getAllowedNextStatuses(DEFAULT_SIMPLE_WORKFLOW, 'in_progress')).toEqual(['done']);
388
- expect(getAllowedNextStatuses(DEFAULT_SIMPLE_WORKFLOW, 'done')).toEqual([]);
389
- expect(getAllowedPreviousStatuses(DEFAULT_SIMPLE_WORKFLOW, 'todo')).toEqual([]);
390
- });
391
-
392
- it('disallows moving task backwards in workflows that explicitly do not define backward transitions', () => {
393
- // Legacy or custom workflow without explicit backward transitions
394
- const legacyWorkflow = {
395
- ...DEFAULT_SIMPLE_WORKFLOW,
396
- id: 'wf_simple_org_legacy',
397
- transitions: [
398
- { name: 'Start', fromStatusKey: 'todo', toStatusKey: 'in_progress' },
399
- { name: 'Complete', fromStatusKey: 'in_progress', toStatusKey: 'done' }
400
- ]
401
- };
402
-
403
- // Since transitions are explicit and do not permit it, should be false and empty
404
- expect(validateTransition(legacyWorkflow, 'in_progress', 'todo')).toBe(false);
405
- expect(getAllowedPreviousStatuses(legacyWorkflow, 'in_progress')).toEqual([]);
406
- });
407
-
408
- it('allows moving task backwards in DEFAULT_SOFTWARE_WORKFLOW', () => {
409
- expect(validateTransition(DEFAULT_SOFTWARE_WORKFLOW, 'in_progress', 'todo')).toBe(true);
410
- expect(validateTransition(DEFAULT_SOFTWARE_WORKFLOW, 'todo', 'backlog')).toBe(true);
411
- expect(getAllowedPreviousStatuses(DEFAULT_SOFTWARE_WORKFLOW, 'in_progress')).toContain('todo');
412
- expect(getAllowedPreviousStatuses(DEFAULT_SOFTWARE_WORKFLOW, 'todo')).toContain('backlog');
413
- });
414
-
415
- it('allows TaskEntity to transitionTo previous status without throwing WorkflowValidationError', () => {
416
- const task = TaskEntity.create({
417
- projectId: 'proj_1',
418
- title: 'Review implementation',
419
- status: 'in_progress'
420
- });
421
-
422
- expect(task.status).toBe('in_progress');
423
- expect(() => {
424
- task.transitionTo('todo', DEFAULT_SIMPLE_WORKFLOW);
425
- }).not.toThrow();
426
- expect(task.status).toBe('todo');
427
- });
428
-
429
- it('throws WorkflowValidationError for illegal non-backward transitions', () => {
430
- const task = TaskEntity.create({
431
- projectId: 'proj_1',
432
- title: 'Review implementation',
433
- status: 'todo'
434
- });
435
-
436
- expect(() => {
437
- task.transitionTo('canceled', DEFAULT_SIMPLE_WORKFLOW);
438
- }).toThrow(WorkflowValidationError);
439
- });
440
- });
441
- });
442
-