@critical-path/core 0.16.0 → 0.18.0
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.
- package/CHANGELOG.md +19 -1
- package/README.md +50 -1
- package/dist/domain/entities.d.ts.map +1 -1
- package/dist/domain/entities.js +5 -0
- package/dist/domain/entities.js.map +1 -1
- package/dist/domain/index.d.ts +2 -0
- package/dist/domain/index.d.ts.map +1 -1
- package/dist/domain/index.js +2 -0
- package/dist/domain/index.js.map +1 -1
- package/dist/domain/ladder.d.ts.map +1 -1
- package/dist/domain/ladder.js +14 -0
- package/dist/domain/ladder.js.map +1 -1
- package/dist/domain/ladder.test.js +3 -3
- package/dist/domain/ladder.test.js.map +1 -1
- package/dist/domain/metrics.d.ts +29 -0
- package/dist/domain/metrics.d.ts.map +1 -0
- package/dist/domain/metrics.js +403 -0
- package/dist/domain/metrics.js.map +1 -0
- package/dist/domain/metrics.test.d.ts +2 -0
- package/dist/domain/metrics.test.d.ts.map +1 -0
- package/dist/domain/metrics.test.js +251 -0
- package/dist/domain/metrics.test.js.map +1 -0
- package/dist/domain/workload.d.ts +10 -0
- package/dist/domain/workload.d.ts.map +1 -0
- package/dist/domain/workload.js +349 -0
- package/dist/domain/workload.js.map +1 -0
- package/dist/domain/workload.test.d.ts +2 -0
- package/dist/domain/workload.test.d.ts.map +1 -0
- package/dist/domain/workload.test.js +201 -0
- package/dist/domain/workload.test.js.map +1 -0
- package/dist/engine/index.d.ts +5 -1
- package/dist/engine/index.d.ts.map +1 -1
- package/dist/engine/index.js +55 -2
- package/dist/engine/index.js.map +1 -1
- package/dist/types/index.d.ts +91 -0
- package/dist/types/index.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/domain/entities.ts +6 -0
- package/src/domain/index.ts +2 -0
- package/src/domain/ladder.test.ts +3 -3
- package/src/domain/ladder.ts +16 -0
- package/src/domain/metrics.test.ts +286 -0
- package/src/domain/metrics.ts +484 -0
- package/src/domain/workload.test.ts +238 -0
- package/src/domain/workload.ts +403 -0
- package/src/engine/index.ts +86 -3
- package/src/types/index.ts +110 -1
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import type { Task, Activity, TimeEntry } from '../types/index.js';
|
|
3
|
+
import {
|
|
4
|
+
inferTaskProgress,
|
|
5
|
+
calculateTaskEVM,
|
|
6
|
+
reconstructTaskProgressHistory,
|
|
7
|
+
calculateTaskMetrics
|
|
8
|
+
} from './metrics.js';
|
|
9
|
+
import { TaskEntity } from './entities.js';
|
|
10
|
+
import { CriticalPathEngine } from '../engine/index.js';
|
|
11
|
+
import { InMemoryStore } from '../store/index.js';
|
|
12
|
+
|
|
13
|
+
describe('Task Metrics Domain', () => {
|
|
14
|
+
const baseTask: Task = {
|
|
15
|
+
id: 'task-1',
|
|
16
|
+
projectId: 'proj-1',
|
|
17
|
+
title: 'Design API Architecture',
|
|
18
|
+
status: 'in_progress',
|
|
19
|
+
semanticStatus: 'in_progress',
|
|
20
|
+
priority: 'high',
|
|
21
|
+
estimatedHours: 20,
|
|
22
|
+
loggedHours: 10,
|
|
23
|
+
actualStartDate: '2026-09-01T09:00:00Z',
|
|
24
|
+
dueDate: '2026-09-10T17:00:00Z',
|
|
25
|
+
createdAt: '2026-09-01T08:00:00Z',
|
|
26
|
+
updatedAt: '2026-09-05T12:00:00Z'
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
describe('inferTaskProgress', () => {
|
|
30
|
+
it('returns 100% progress for completed tasks', () => {
|
|
31
|
+
const completedTask: Task = { ...baseTask, status: 'done', semanticStatus: 'completed' };
|
|
32
|
+
const res = inferTaskProgress(completedTask);
|
|
33
|
+
expect(res.progressPercentage).toBe(100);
|
|
34
|
+
expect(res.breakdown.effortProgress).toBe(100);
|
|
35
|
+
expect(res.breakdown.scheduleProgress).toBe(100);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('uses explicit progress when provided', () => {
|
|
39
|
+
const taskWithExplicit: Task = {
|
|
40
|
+
...baseTask,
|
|
41
|
+
progress: 45,
|
|
42
|
+
todos: [
|
|
43
|
+
{ id: 't1', title: 'Step 1', completed: true },
|
|
44
|
+
{ id: 't2', title: 'Step 2', completed: false }
|
|
45
|
+
]
|
|
46
|
+
};
|
|
47
|
+
const res = inferTaskProgress(taskWithExplicit);
|
|
48
|
+
expect(res.isExplicit).toBe(true);
|
|
49
|
+
expect(res.source).toBe('explicit');
|
|
50
|
+
expect(res.progressPercentage).toBe(45);
|
|
51
|
+
expect(res.breakdown.explicitProgress).toBe(45);
|
|
52
|
+
expect(res.breakdown.todoProgress).toBe(50);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('infers progress from todos when explicit progress is absent', () => {
|
|
56
|
+
const taskWithTodos: Task = {
|
|
57
|
+
...baseTask,
|
|
58
|
+
progress: undefined,
|
|
59
|
+
todos: [
|
|
60
|
+
{ id: 't1', title: 'Step 1', completed: true },
|
|
61
|
+
{ id: 't2', title: 'Step 2', completed: true },
|
|
62
|
+
{ id: 't3', title: 'Step 3', completed: false },
|
|
63
|
+
{ id: 't4', title: 'Step 4', completed: false }
|
|
64
|
+
]
|
|
65
|
+
};
|
|
66
|
+
const res = inferTaskProgress(taskWithTodos);
|
|
67
|
+
expect(res.isExplicit).toBe(false);
|
|
68
|
+
expect(res.source).toBe('todos');
|
|
69
|
+
expect(res.progressPercentage).toBe(50);
|
|
70
|
+
expect(res.breakdown.todoProgress).toBe(50);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('infers progress from effort (logged vs estimated) when todos and explicit are absent', () => {
|
|
74
|
+
const taskWithEffort: Task = {
|
|
75
|
+
...baseTask,
|
|
76
|
+
progress: undefined,
|
|
77
|
+
todos: undefined,
|
|
78
|
+
estimatedHours: 40,
|
|
79
|
+
loggedHours: 10
|
|
80
|
+
};
|
|
81
|
+
const res = inferTaskProgress(taskWithEffort);
|
|
82
|
+
expect(res.isExplicit).toBe(false);
|
|
83
|
+
expect(res.source).toBe('time_effort');
|
|
84
|
+
expect(res.progressPercentage).toBe(25);
|
|
85
|
+
expect(res.breakdown.effortProgress).toBe(25);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('infers progress from schedule elapsed time when no other metrics are present', () => {
|
|
89
|
+
const taskWithSchedule: Task = {
|
|
90
|
+
...baseTask,
|
|
91
|
+
progress: undefined,
|
|
92
|
+
todos: undefined,
|
|
93
|
+
estimatedHours: 0,
|
|
94
|
+
loggedHours: 0,
|
|
95
|
+
actualStartDate: '2026-09-01T00:00:00Z',
|
|
96
|
+
dueDate: '2026-09-11T00:00:00Z'
|
|
97
|
+
};
|
|
98
|
+
const midDate = new Date('2026-09-06T00:00:00Z'); // 50% elapsed
|
|
99
|
+
const res = inferTaskProgress(taskWithSchedule, { referenceDate: midDate });
|
|
100
|
+
expect(res.source).toBe('schedule_elapsed');
|
|
101
|
+
expect(res.progressPercentage).toBe(50);
|
|
102
|
+
expect(res.breakdown.scheduleProgress).toBe(50);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe('calculateTaskEVM', () => {
|
|
107
|
+
it('computes standard EVM values for on-track tasks', () => {
|
|
108
|
+
const task: Task = {
|
|
109
|
+
...baseTask,
|
|
110
|
+
estimatedHours: 20,
|
|
111
|
+
loggedHours: 10,
|
|
112
|
+
actualStartDate: '2026-09-01T00:00:00Z',
|
|
113
|
+
dueDate: '2026-09-11T00:00:00Z',
|
|
114
|
+
progress: 50
|
|
115
|
+
};
|
|
116
|
+
const midDate = new Date('2026-09-06T00:00:00Z'); // 50% expected
|
|
117
|
+
const evm = calculateTaskEVM(task, { referenceDate: midDate });
|
|
118
|
+
|
|
119
|
+
expect(evm.plannedValue).toBe(10); // 50% of 20h
|
|
120
|
+
expect(evm.earnedValue).toBe(10); // 50% of 20h
|
|
121
|
+
expect(evm.actualCost).toBe(10);
|
|
122
|
+
expect(evm.costVariance).toBe(0);
|
|
123
|
+
expect(evm.scheduleVariance).toBe(0);
|
|
124
|
+
expect(evm.costPerformanceIndex).toBe(1.0);
|
|
125
|
+
expect(evm.schedulePerformanceIndex).toBe(1.0);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('detects cost overrun and schedule lag (CPI < 1, SPI < 1)', () => {
|
|
129
|
+
const task: Task = {
|
|
130
|
+
...baseTask,
|
|
131
|
+
estimatedHours: 20,
|
|
132
|
+
loggedHours: 15, // Spent 15h
|
|
133
|
+
progress: 25, // Only 25% complete (5h EV)
|
|
134
|
+
actualStartDate: '2026-09-01T00:00:00Z',
|
|
135
|
+
dueDate: '2026-09-11T00:00:00Z'
|
|
136
|
+
};
|
|
137
|
+
const midDate = new Date('2026-09-06T00:00:00Z'); // 50% expected (10h PV)
|
|
138
|
+
const evm = calculateTaskEVM(task, { referenceDate: midDate });
|
|
139
|
+
|
|
140
|
+
expect(evm.plannedValue).toBe(10);
|
|
141
|
+
expect(evm.earnedValue).toBe(5);
|
|
142
|
+
expect(evm.actualCost).toBe(15);
|
|
143
|
+
expect(evm.costVariance).toBe(-10); // 5 - 15 = -10
|
|
144
|
+
expect(evm.scheduleVariance).toBe(-5); // 5 - 10 = -5
|
|
145
|
+
expect(evm.costPerformanceIndex).toBe(0.33); // 5 / 15
|
|
146
|
+
expect(evm.schedulePerformanceIndex).toBe(0.5); // 5 / 10
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
describe('reconstructTaskProgressHistory & Curve Profile', () => {
|
|
151
|
+
it('detects linear progress', () => {
|
|
152
|
+
const activities: Activity[] = [
|
|
153
|
+
{ id: 'a1', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 25 }, createdAt: '2026-09-02T10:00:00Z' },
|
|
154
|
+
{ id: 'a2', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 50 }, createdAt: '2026-09-04T10:00:00Z' },
|
|
155
|
+
{ id: 'a3', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 75 }, createdAt: '2026-09-06T10:00:00Z' },
|
|
156
|
+
{ id: 'a4', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 100 }, createdAt: '2026-09-08T10:00:00Z' }
|
|
157
|
+
];
|
|
158
|
+
const task: Task = { ...baseTask, progress: 100, updatedAt: '2026-09-08T10:00:00Z' };
|
|
159
|
+
const history = reconstructTaskProgressHistory(task, activities);
|
|
160
|
+
|
|
161
|
+
expect(history.points.length).toBeGreaterThanOrEqual(4);
|
|
162
|
+
expect(history.curveProfile).toBe('linear');
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('detects S-Curve progress', () => {
|
|
166
|
+
const activities: Activity[] = [
|
|
167
|
+
{ id: 'a1', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 15 }, createdAt: '2026-09-02T10:00:00Z' },
|
|
168
|
+
{ id: 'a2', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 45 }, createdAt: '2026-09-05T10:00:00Z' },
|
|
169
|
+
{ id: 'a3', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 85 }, createdAt: '2026-09-07T10:00:00Z' },
|
|
170
|
+
{ id: 'a4', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 95 }, createdAt: '2026-09-09T10:00:00Z' }
|
|
171
|
+
];
|
|
172
|
+
const task: Task = { ...baseTask, progress: 95, updatedAt: '2026-09-09T10:00:00Z' };
|
|
173
|
+
const history = reconstructTaskProgressHistory(task, activities);
|
|
174
|
+
|
|
175
|
+
expect(history.curveProfile).toBe('s_curve');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('detects early surge progress', () => {
|
|
179
|
+
const activities: Activity[] = [
|
|
180
|
+
{ id: 'a1', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 75 }, createdAt: '2026-09-02T10:00:00Z' },
|
|
181
|
+
{ id: 'a2', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 75 }, createdAt: '2026-09-05T10:00:00Z' },
|
|
182
|
+
{ id: 'a3', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 78 }, createdAt: '2026-09-08T10:00:00Z' }
|
|
183
|
+
];
|
|
184
|
+
const task: Task = { ...baseTask, progress: 78, updatedAt: '2026-09-08T10:00:00Z' };
|
|
185
|
+
const history = reconstructTaskProgressHistory(task, activities);
|
|
186
|
+
|
|
187
|
+
expect(history.curveProfile).toBe('early_surge');
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('detects late rush progress', () => {
|
|
191
|
+
const activities: Activity[] = [
|
|
192
|
+
{ id: 'a1', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 10 }, createdAt: '2026-09-03T10:00:00Z' },
|
|
193
|
+
{ id: 'a2', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 15 }, createdAt: '2026-09-06T10:00:00Z' },
|
|
194
|
+
{ id: 'a3', taskId: 'task-1', actorId: 'u1', action: 'task.updated', details: { progress: 90 }, createdAt: '2026-09-08T10:00:00Z' }
|
|
195
|
+
];
|
|
196
|
+
const task: Task = { ...baseTask, progress: 90, updatedAt: '2026-09-08T10:00:00Z' };
|
|
197
|
+
const history = reconstructTaskProgressHistory(task, activities);
|
|
198
|
+
|
|
199
|
+
expect(history.curveProfile).toBe('late_rush');
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
describe('calculateTaskMetrics & Multi-dimensional Variance', () => {
|
|
204
|
+
it('computes complete metrics with effort and schedule variances', () => {
|
|
205
|
+
const task: Task = {
|
|
206
|
+
...baseTask,
|
|
207
|
+
plannedStartDate: '2026-09-01T09:00:00Z',
|
|
208
|
+
actualStartDate: '2026-09-01T09:00:00Z',
|
|
209
|
+
dueDate: '2026-09-05T17:00:00Z',
|
|
210
|
+
actualEndDate: '2026-09-07T17:00:00Z',
|
|
211
|
+
status: 'done',
|
|
212
|
+
semanticStatus: 'completed',
|
|
213
|
+
estimatedHours: 20,
|
|
214
|
+
loggedHours: 25
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
const metrics = calculateTaskMetrics(task);
|
|
218
|
+
expect(metrics.taskId).toBe('task-1');
|
|
219
|
+
expect(metrics.realityDelta.varianceHours).toBe(5);
|
|
220
|
+
expect(metrics.realityDelta.effortVarianceHours).toBe(5);
|
|
221
|
+
expect(metrics.realityDelta.accuracyRatio).toBe(1.25); // 25 / 20
|
|
222
|
+
expect(metrics.realityDelta.scheduleVarianceDays).toBe(2); // Completed 2 days late
|
|
223
|
+
expect(metrics.realityDelta.durationVarianceHours).toBe(48); // 2 days extra duration
|
|
224
|
+
expect(metrics.inferredActuals.activeWorkingHours).toBeGreaterThan(0);
|
|
225
|
+
expect(metrics.inferredActuals.isEndDateInferred).toBe(true);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
describe('Task Re-opening Timestamp Behavior', () => {
|
|
230
|
+
it('resets actualEndDate when TaskEntity transitions from completed back to in_progress', () => {
|
|
231
|
+
const entity = TaskEntity.create({
|
|
232
|
+
projectId: 'proj-1',
|
|
233
|
+
title: 'Bugfix',
|
|
234
|
+
status: 'todo'
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Move to in_progress -> stamps actualStartDate
|
|
238
|
+
entity.transitionTo('in_progress');
|
|
239
|
+
expect(entity.actualStartDate).toBeDefined();
|
|
240
|
+
expect(entity.actualEndDate).toBeUndefined();
|
|
241
|
+
|
|
242
|
+
// Move to done -> stamps actualEndDate
|
|
243
|
+
entity.transitionTo('done');
|
|
244
|
+
expect(entity.actualEndDate).toBeDefined();
|
|
245
|
+
|
|
246
|
+
// Re-open back to in_progress -> actualEndDate must be cleared
|
|
247
|
+
entity.transitionTo('in_progress');
|
|
248
|
+
expect(entity.actualEndDate).toBeUndefined();
|
|
249
|
+
expect(entity.actualStartDate).toBeDefined();
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('integrates with CriticalPathEngine getTaskMetrics and getTaskProgressHistory', async () => {
|
|
253
|
+
const store = new InMemoryStore();
|
|
254
|
+
const engine = new CriticalPathEngine({ store });
|
|
255
|
+
|
|
256
|
+
const project = await engine.createProject({ name: 'Engine Test' });
|
|
257
|
+
const task = await engine.createTask({
|
|
258
|
+
projectId: project.id,
|
|
259
|
+
title: 'Implement Auth',
|
|
260
|
+
estimatedHours: 16
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// Move to in_progress
|
|
264
|
+
await engine.updateTask(task.id, { status: 'in_progress' });
|
|
265
|
+
await engine.logTime({ taskId: task.id, hours: 8, isBillable: true });
|
|
266
|
+
|
|
267
|
+
const metrics = await engine.getTaskMetrics(task.id);
|
|
268
|
+
expect(metrics).not.toBeNull();
|
|
269
|
+
expect(metrics?.taskId).toBe(task.id);
|
|
270
|
+
expect(metrics?.realityDelta.loggedHours).toBe(8);
|
|
271
|
+
expect(metrics?.realityDelta.estimatedHours).toBe(16);
|
|
272
|
+
expect(metrics?.progress.source).toBe('time_effort');
|
|
273
|
+
expect(metrics?.progress.progressPercentage).toBe(50);
|
|
274
|
+
expect(metrics?.evm.earnedValue).toBe(8);
|
|
275
|
+
|
|
276
|
+
const history = await engine.getTaskProgressHistory(task.id);
|
|
277
|
+
expect(history).not.toBeNull();
|
|
278
|
+
expect(history?.points.length).toBeGreaterThanOrEqual(1);
|
|
279
|
+
|
|
280
|
+
// Verify ladder view includes metrics
|
|
281
|
+
const ladder = await engine.getTaskLadder(task.id);
|
|
282
|
+
expect(ladder?.metrics).toBeDefined();
|
|
283
|
+
expect(ladder?.metrics?.taskId).toBe(task.id);
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
});
|