@jsb188/schedule 1.1.2
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/package.json +62 -0
- package/src/constants/schedule.ts +549 -0
- package/src/css.d.ts +7 -0
- package/src/libs/gantt-geometry.ts +771 -0
- package/src/libs/gantt-histogram.ts +124 -0
- package/src/libs/gantt-keyboard.ts +113 -0
- package/src/libs/gantt-state.ts +57 -0
- package/src/libs/gantt-url.ts +63 -0
- package/src/modules/Gantt.helpers.ts +119 -0
- package/src/modules/Gantt.tsx +584 -0
- package/src/types/schedule.d.ts +1149 -0
- package/src/ui/ActivityFormUI.tsx +133 -0
- package/src/ui/GanttEditorsUI.tsx +102 -0
- package/src/ui/GanttHistogramUI.tsx +236 -0
- package/src/ui/GanttUI.tsx +792 -0
- package/src/ui/ProjectFormUI.tsx +250 -0
- package/src/ui/ProjectMobileListUI.tsx +80 -0
- package/src/ui/ProjectToolbarUI.tsx +54 -0
- package/src/ui/RelationshipListUI.tsx +176 -0
- package/src/ui/ScheduleRunUI.tsx +173 -0
- package/src/ui/ScheduleSettingsUI.tsx +420 -0
- package/src/utils/coverage.helpers.ts +679 -0
- package/src/utils/coverage.ts +630 -0
- package/src/utils/cpm.helpers.ts +674 -0
- package/src/utils/cpm.ts +907 -0
- package/src/utils/cpmLeveling.helpers.ts +564 -0
- package/src/utils/cpmLeveling.ts +367 -0
- package/src/utils/defaults.ts +8 -0
- package/src/utils/eligibility.ts +457 -0
- package/src/utils/planCsv.ts +344 -0
- package/src/utils/predecessorText.ts +96 -0
- package/src/utils/recurrence.ts +616 -0
- package/src/utils/request.ts +153 -0
- package/src/utils/resource.ts +182 -0
- package/src/utils/rule.ts +216 -0
- package/src/utils/run.ts +43 -0
- package/src/utils/series.ts +161 -0
- package/src/utils/slot.ts +208 -0
- package/src/utils/workTime.ts +1382 -0
- package/tests/coverage.fixtures.ts +111 -0
- package/tests/coverage.test.ts +503 -0
- package/tests/cpm.bench.test.ts +154 -0
- package/tests/cpm.test.ts +354 -0
- package/tests/cpmCorrectness.test.ts +327 -0
- package/tests/cpmLeveling.test.ts +471 -0
- package/tests/eligibility.test.ts +320 -0
- package/tests/gantt-geometry.test.ts +367 -0
- package/tests/gantt-histogram.test.ts +78 -0
- package/tests/gantt-keyboard.test.ts +69 -0
- package/tests/helpers/cpmFixtures.ts +101 -0
- package/tests/projectInputs.test.ts +165 -0
- package/tests/recurrence.test.ts +68 -0
- package/tests/request.test.ts +99 -0
- package/tests/resource.test.ts +95 -0
- package/tests/rule.test.ts +98 -0
- package/tests/series.test.ts +120 -0
- package/tests/workTime.test.ts +1007 -0
- package/tests/workTimeContracts.test.ts +111 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
buildGanttHistogram,
|
|
4
|
+
buildGanttHistogramScale,
|
|
5
|
+
clampGanttHistogramRange,
|
|
6
|
+
} from '../src/libs/gantt-histogram.ts';
|
|
7
|
+
|
|
8
|
+
describe('resource histogram', () => {
|
|
9
|
+
it('stacks external usage and flags only finite capacity overloads', () => {
|
|
10
|
+
const scale = buildGanttHistogramScale('2026-09-01', '2026-09-10', 'UTC');
|
|
11
|
+
const bars = buildGanttHistogram(
|
|
12
|
+
[
|
|
13
|
+
{
|
|
14
|
+
resourceId: 'a',
|
|
15
|
+
date: '2026-09-02',
|
|
16
|
+
demandUnits: 2,
|
|
17
|
+
externalUnits: 1,
|
|
18
|
+
availableUnits: 2,
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
resourceId: 'a',
|
|
22
|
+
date: '2026-09-03',
|
|
23
|
+
demandUnits: 10,
|
|
24
|
+
externalUnits: 2,
|
|
25
|
+
availableUnits: null,
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
resourceId: 'a',
|
|
29
|
+
date: '2026-09-10',
|
|
30
|
+
demandUnits: 1,
|
|
31
|
+
externalUnits: 0,
|
|
32
|
+
availableUnits: 1,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
resourceId: 'b',
|
|
36
|
+
date: '2026-09-02',
|
|
37
|
+
demandUnits: 1,
|
|
38
|
+
externalUnits: 0,
|
|
39
|
+
availableUnits: 1,
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
scale,
|
|
43
|
+
'a',
|
|
44
|
+
);
|
|
45
|
+
expect(bars).toHaveLength(2);
|
|
46
|
+
expect(bars[0]).toMatchObject({
|
|
47
|
+
x: 14,
|
|
48
|
+
demandHeight: 16,
|
|
49
|
+
externalHeight: 8,
|
|
50
|
+
overallocated: true,
|
|
51
|
+
});
|
|
52
|
+
expect(bars[1].overallocated).toBe(false);
|
|
53
|
+
expect(bars[1].availableUnits).toBeNull();
|
|
54
|
+
expect(buildGanttHistogram([], scale, 'a')).toEqual([]);
|
|
55
|
+
});
|
|
56
|
+
it('clamps multi-year axes to 1,100 local days across daylight saving changes', () => {
|
|
57
|
+
const scale = buildGanttHistogramScale(
|
|
58
|
+
'2024-01-01',
|
|
59
|
+
'2029-01-01',
|
|
60
|
+
'America/Toronto',
|
|
61
|
+
);
|
|
62
|
+
const result = clampGanttHistogramRange(scale, '2026-03-08T16:00:00Z');
|
|
63
|
+
expect(result).toEqual({
|
|
64
|
+
from: '2026-02-06',
|
|
65
|
+
to: '2029-01-01',
|
|
66
|
+
clamped: true,
|
|
67
|
+
});
|
|
68
|
+
const wider = clampGanttHistogramRange(
|
|
69
|
+
{ ...scale, endAt: new Date('2030-01-01T05:00:00Z') },
|
|
70
|
+
'2026-03-08T16:00:00Z',
|
|
71
|
+
);
|
|
72
|
+
expect(wider).toEqual({
|
|
73
|
+
from: '2026-02-06',
|
|
74
|
+
to: '2029-02-10',
|
|
75
|
+
clamped: true,
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
handleGanttKeyboardEvent,
|
|
4
|
+
type GanttKeyboardContext,
|
|
5
|
+
} from '../src/libs/gantt-keyboard.ts';
|
|
6
|
+
|
|
7
|
+
const context: GanttKeyboardContext = {
|
|
8
|
+
rowIndex: 20,
|
|
9
|
+
rowCount: 5000,
|
|
10
|
+
columnIndex: 3,
|
|
11
|
+
columnCount: 4,
|
|
12
|
+
pageSize: 30,
|
|
13
|
+
isWbs: false,
|
|
14
|
+
editable: true,
|
|
15
|
+
zoom: 'WEEK',
|
|
16
|
+
};
|
|
17
|
+
/* Build an ordinary key event with explicit modifier defaults. */
|
|
18
|
+
function key(key: string, modifiers: Partial<KeyboardEvent> = {}) {
|
|
19
|
+
return {
|
|
20
|
+
key,
|
|
21
|
+
ctrlKey: false,
|
|
22
|
+
metaKey: false,
|
|
23
|
+
shiftKey: false,
|
|
24
|
+
altKey: false,
|
|
25
|
+
...modifiers,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
it('navigates virtual row boundaries, extended selection, page steps, and wrapped cells', () => {
|
|
30
|
+
expect(
|
|
31
|
+
handleGanttKeyboardEvent(key('ArrowDown', { shiftKey: true }), context),
|
|
32
|
+
).toEqual({ kind: 'MOVE', rowIndex: 21, columnIndex: 3, extend: true });
|
|
33
|
+
expect(handleGanttKeyboardEvent(key('End'), context)).toMatchObject({
|
|
34
|
+
rowIndex: 4999,
|
|
35
|
+
});
|
|
36
|
+
expect(handleGanttKeyboardEvent(key('Home'), context)).toMatchObject({
|
|
37
|
+
rowIndex: 0,
|
|
38
|
+
});
|
|
39
|
+
expect(handleGanttKeyboardEvent(key('PageDown'), context)).toMatchObject({
|
|
40
|
+
rowIndex: 50,
|
|
41
|
+
});
|
|
42
|
+
expect(handleGanttKeyboardEvent(key('Tab'), context)).toMatchObject({
|
|
43
|
+
rowIndex: 21,
|
|
44
|
+
columnIndex: 0,
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
it('separates editable actions from navigation and WBS collapse', () => {
|
|
48
|
+
expect(handleGanttKeyboardEvent(key(' '), context)).toBeNull();
|
|
49
|
+
expect(
|
|
50
|
+
handleGanttKeyboardEvent(key(' '), { ...context, isWbs: true }),
|
|
51
|
+
).toEqual({ kind: 'COLLAPSE' });
|
|
52
|
+
expect(
|
|
53
|
+
handleGanttKeyboardEvent(key('Delete'), { ...context, editable: false }),
|
|
54
|
+
).toBeNull();
|
|
55
|
+
expect(handleGanttKeyboardEvent(key('x'), context)).toEqual({
|
|
56
|
+
kind: 'EDIT',
|
|
57
|
+
initialText: 'x',
|
|
58
|
+
});
|
|
59
|
+
expect(handleGanttKeyboardEvent(key('+'), context)).toEqual({
|
|
60
|
+
kind: 'ZOOM',
|
|
61
|
+
zoom: 'DAY',
|
|
62
|
+
});
|
|
63
|
+
expect(
|
|
64
|
+
handleGanttKeyboardEvent(
|
|
65
|
+
key('Insert', { ctrlKey: true, shiftKey: true }),
|
|
66
|
+
context,
|
|
67
|
+
),
|
|
68
|
+
).toEqual({ kind: 'INSERT', wbs: true });
|
|
69
|
+
});
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CpmActivityInput,
|
|
3
|
+
CpmAssignmentInput,
|
|
4
|
+
CpmRelationshipInput,
|
|
5
|
+
CpmResourceInput,
|
|
6
|
+
CpmScheduleInput,
|
|
7
|
+
} from '../../src/types/schedule.d.ts';
|
|
8
|
+
import { runCriticalPathSchedule } from '../../src/utils/cpm.ts';
|
|
9
|
+
import { getStandardWorkCalendarDef } from '../../src/utils/workTime.ts';
|
|
10
|
+
|
|
11
|
+
export const DAY = 480;
|
|
12
|
+
/* Interpret a winter local date/time in the fixture's Los Angeles zone. */
|
|
13
|
+
export function la(date: string, time = '08:00'): Date {
|
|
14
|
+
return new Date(`${date}T${time}:00-08:00`);
|
|
15
|
+
}
|
|
16
|
+
/* Build an unstarted task with duration in working days. */
|
|
17
|
+
export function task(
|
|
18
|
+
id: string,
|
|
19
|
+
days = 1,
|
|
20
|
+
extra: Partial<CpmActivityInput> = {},
|
|
21
|
+
): CpmActivityInput {
|
|
22
|
+
return { id, type: 'TASK', plannedDurationMinutes: days * DAY, ...extra };
|
|
23
|
+
}
|
|
24
|
+
/* Build a zero-duration activity of either milestone type. */
|
|
25
|
+
export function milestone(
|
|
26
|
+
id: string,
|
|
27
|
+
type: 'START_MILESTONE' | 'FINISH_MILESTONE' = 'FINISH_MILESTONE',
|
|
28
|
+
): CpmActivityInput {
|
|
29
|
+
return task(id, 0, { type });
|
|
30
|
+
}
|
|
31
|
+
/* Connect two activities with one of the four supported relationship types. */
|
|
32
|
+
export function fs(
|
|
33
|
+
predecessorId: string,
|
|
34
|
+
successorId: string,
|
|
35
|
+
lagMinutes = 0,
|
|
36
|
+
type: CpmRelationshipInput['type'] = 'FS',
|
|
37
|
+
): CpmRelationshipInput {
|
|
38
|
+
return { predecessorId, successorId, lagMinutes, type };
|
|
39
|
+
}
|
|
40
|
+
/* Allocate a fixed daily rate or evenly distributed remaining effort to a resource. */
|
|
41
|
+
export function assignment(
|
|
42
|
+
activityId: string,
|
|
43
|
+
resourceId = 'crane',
|
|
44
|
+
unitsPerDay: number | null = DAY,
|
|
45
|
+
remainingUnits = 0,
|
|
46
|
+
): CpmAssignmentInput {
|
|
47
|
+
return { activityId, resourceId, unitsPerDay, remainingUnits };
|
|
48
|
+
}
|
|
49
|
+
/* Build a levelable full-day equipment resource. */
|
|
50
|
+
export function resource(
|
|
51
|
+
id = 'crane',
|
|
52
|
+
extra: Partial<CpmResourceInput> = {},
|
|
53
|
+
): CpmResourceInput {
|
|
54
|
+
return {
|
|
55
|
+
id,
|
|
56
|
+
kind: 'EQUIPMENT',
|
|
57
|
+
maxUnitsPerDay: DAY,
|
|
58
|
+
canLevel: true,
|
|
59
|
+
availability: [],
|
|
60
|
+
timeOff: [],
|
|
61
|
+
...extra,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/* Run a project from Monday January 5 with a fresh standard calendar. */
|
|
65
|
+
export function schedule(
|
|
66
|
+
activities: CpmActivityInput[],
|
|
67
|
+
relationships: CpmRelationshipInput[] = [],
|
|
68
|
+
extra: Partial<CpmScheduleInput> = {},
|
|
69
|
+
) {
|
|
70
|
+
return runCriticalPathSchedule({
|
|
71
|
+
activities,
|
|
72
|
+
relationships,
|
|
73
|
+
calendars: { STANDARD: getStandardWorkCalendarDef('America/Los_Angeles') },
|
|
74
|
+
defaultCalendarId: 'STANDARD',
|
|
75
|
+
projectStartAt: la('2026-01-05'),
|
|
76
|
+
dataDateAt: la('2026-01-05'),
|
|
77
|
+
...extra,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
/* Enable leveling while allowing a fixture to replace any engine input. */
|
|
81
|
+
export function level(
|
|
82
|
+
activities: CpmActivityInput[],
|
|
83
|
+
extra: Partial<CpmScheduleInput> = {},
|
|
84
|
+
) {
|
|
85
|
+
return schedule(activities, [], {
|
|
86
|
+
resources: [resource()],
|
|
87
|
+
assignments: activities.map((a) => assignment(a.id)),
|
|
88
|
+
...extra,
|
|
89
|
+
options: {
|
|
90
|
+
...extra.options,
|
|
91
|
+
leveling: { enabled: true, ...extra.options?.leveling },
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/* Find one activity's schedule result by its stable identifier. */
|
|
96
|
+
export function byId(
|
|
97
|
+
result: ReturnType<typeof runCriticalPathSchedule>,
|
|
98
|
+
id: string,
|
|
99
|
+
) {
|
|
100
|
+
return result.activities.find((a) => a.id === id)!;
|
|
101
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
readGanttUrlState,
|
|
4
|
+
writeGanttUrlState,
|
|
5
|
+
} from '../src/libs/gantt-url.ts';
|
|
6
|
+
import {
|
|
7
|
+
daysTextToMinutes,
|
|
8
|
+
formatPredecessorText,
|
|
9
|
+
parsePredecessorText,
|
|
10
|
+
} from '../src/utils/predecessorText.ts';
|
|
11
|
+
import {
|
|
12
|
+
parseSchedulePlanCsv,
|
|
13
|
+
readScheduleCsvRows,
|
|
14
|
+
} from '../src/utils/planCsv.ts';
|
|
15
|
+
import { getLatestScheduleRun } from '../src/utils/run.ts';
|
|
16
|
+
|
|
17
|
+
describe('project inputs', () => {
|
|
18
|
+
it('uses strict working-day durations', () => {
|
|
19
|
+
expect(daysTextToMinutes('2.5', 420)).toBe(1050);
|
|
20
|
+
for (const value of ['2x', '-1', 'Infinity', 'NaN', ''])
|
|
21
|
+
expect(daysTextToMinutes(value, 480)).toBeNull();
|
|
22
|
+
});
|
|
23
|
+
it('preserves unrelated URL values and omits defaults', () => {
|
|
24
|
+
const params = writeGanttUrlState(new URLSearchParams('foo=bar&z=day'), {
|
|
25
|
+
zoom: 'WEEK',
|
|
26
|
+
criticalOnly: true,
|
|
27
|
+
histogramResourceIds: ['r1', 'r2'],
|
|
28
|
+
});
|
|
29
|
+
expect(params.toString()).toBe('foo=bar&crit=1&hist=r1%2Cr2');
|
|
30
|
+
expect(readGanttUrlState(new URLSearchParams('z=bogus&rv=bad')).zoom).toBe(
|
|
31
|
+
'WEEK',
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
it('parses codes before ambiguous relationship suffixes and converts hours', () => {
|
|
35
|
+
const ids = new Map([
|
|
36
|
+
['A', '1'],
|
|
37
|
+
['ASS', '2'],
|
|
38
|
+
['A10', '3'],
|
|
39
|
+
]);
|
|
40
|
+
expect(parsePredecessorText('ASS, A10 FF -4h, A SS +1', ids, 420)).toEqual({
|
|
41
|
+
items: [
|
|
42
|
+
{ predecessorId: '2', type: 'FS', lagMinutes: 0 },
|
|
43
|
+
{ predecessorId: '3', type: 'FF', lagMinutes: -240 },
|
|
44
|
+
{ predecessorId: '1', type: 'SS', lagMinutes: 420 },
|
|
45
|
+
],
|
|
46
|
+
unknownCodes: [],
|
|
47
|
+
duplicateCodes: [],
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
it('rejects unknown and duplicate predecessor codes', () => {
|
|
51
|
+
const parsed = parsePredecessorText(
|
|
52
|
+
'A, AFS, missing',
|
|
53
|
+
new Map([['A', '1']]),
|
|
54
|
+
480,
|
|
55
|
+
);
|
|
56
|
+
expect(parsed.duplicateCodes).toEqual(['A']);
|
|
57
|
+
expect(parsed.unknownCodes).toEqual(['missing']);
|
|
58
|
+
});
|
|
59
|
+
it('formats canonical predecessor codes', () => {
|
|
60
|
+
const current = [
|
|
61
|
+
{
|
|
62
|
+
id: 'r1',
|
|
63
|
+
predecessorId: 'a',
|
|
64
|
+
successorId: 'b',
|
|
65
|
+
type: 'FS' as const,
|
|
66
|
+
lagMinutes: 0,
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
expect(formatPredecessorText(current, new Map([['a', 'A']]), 480)).toBe(
|
|
70
|
+
'A',
|
|
71
|
+
);
|
|
72
|
+
});
|
|
73
|
+
it('reads escaped quotes, commas and embedded line breaks', () => {
|
|
74
|
+
expect(
|
|
75
|
+
readScheduleCsvRows('code,name\r\nA,"Pour, then ""finish""\ncarefully"'),
|
|
76
|
+
).toEqual([
|
|
77
|
+
['code', 'name'],
|
|
78
|
+
['A', 'Pour, then "finish"\ncarefully'],
|
|
79
|
+
]);
|
|
80
|
+
expect(() => readScheduleCsvRows('A,"broken')).toThrow();
|
|
81
|
+
});
|
|
82
|
+
it('builds hierarchy, typed codes, resources and predecessor links', () => {
|
|
83
|
+
const result = parseSchedulePlanCsv(
|
|
84
|
+
'ID,Activity,Days,WBS,Preds,Resources,code:phase\nA,Start,1,1.2,,CREW:2,rough\nB,Finish,0.5,1.2,AFF+2h,CRANE,finish',
|
|
85
|
+
{ minutesPerDay: 420, timeZone: 'UTC' },
|
|
86
|
+
);
|
|
87
|
+
expect(result.errors).toEqual([]);
|
|
88
|
+
expect(result.plan.activities[1].plannedDurationMinutes).toBe(210);
|
|
89
|
+
expect(result.plan.wbs).toEqual([
|
|
90
|
+
{ code: '1', name: '1', parentCode: null },
|
|
91
|
+
{ code: '1.2', name: '1.2', parentCode: '1' },
|
|
92
|
+
]);
|
|
93
|
+
expect(result.plan.relationships[0]).toEqual({
|
|
94
|
+
predecessorCode: 'A',
|
|
95
|
+
successorCode: 'B',
|
|
96
|
+
type: 'FF',
|
|
97
|
+
lagMinutes: 120,
|
|
98
|
+
});
|
|
99
|
+
expect(result.plan.assignments[0].unitsPerDay).toBe(2);
|
|
100
|
+
expect(result.plan.activities[0].codes).toEqual([
|
|
101
|
+
{ typeKey: 'phase', valueKey: 'rough' },
|
|
102
|
+
]);
|
|
103
|
+
});
|
|
104
|
+
it('snaps import dates to referenced calendar hours and holidays', () => {
|
|
105
|
+
const result = parseSchedulePlanCsv(
|
|
106
|
+
'code,name,start,finish\nA,Work,2026-09-07,2026-09-09',
|
|
107
|
+
{
|
|
108
|
+
minutesPerDay: 240,
|
|
109
|
+
timeZone: 'UTC',
|
|
110
|
+
calendar: {
|
|
111
|
+
id: 'c',
|
|
112
|
+
timeZone: 'UTC',
|
|
113
|
+
weekHours: {
|
|
114
|
+
'1': [['10:00', '14:00']],
|
|
115
|
+
'2': [['10:00', '14:00']],
|
|
116
|
+
'3': [['10:00', '14:00']],
|
|
117
|
+
},
|
|
118
|
+
exceptions: { '2026-09-07': [] },
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
expect(result.errors).toEqual([]);
|
|
123
|
+
expect(result.plan.activities[0].primaryConstraintAt).toBe(
|
|
124
|
+
'2026-09-08T10:00:00.000Z',
|
|
125
|
+
);
|
|
126
|
+
expect(result.plan.activities[0].secondaryConstraintAt).toBe(
|
|
127
|
+
'2026-09-09T14:00:00.000Z',
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
it('retains row-specific import errors and blocks malformed fields', () => {
|
|
131
|
+
const result = parseSchedulePlanCsv(
|
|
132
|
+
'code,name,duration,priority\nA,One,2x,11\nA,Two,1,2',
|
|
133
|
+
{ minutesPerDay: 480, timeZone: 'UTC' },
|
|
134
|
+
);
|
|
135
|
+
expect(result.errors.map((row) => row.code)).toEqual([
|
|
136
|
+
'INVALID_FIELD',
|
|
137
|
+
'INVALID_FIELD',
|
|
138
|
+
'DUPLICATE_CODE',
|
|
139
|
+
]);
|
|
140
|
+
});
|
|
141
|
+
it('orders run sequences exactly beyond Number precision and excludes superseded work', () => {
|
|
142
|
+
expect(
|
|
143
|
+
getLatestScheduleRun([
|
|
144
|
+
{ sequence: '9007199254740993', status: 'COMPLETE' },
|
|
145
|
+
{ sequence: '9007199254740994', status: 'SUPERSEDED' },
|
|
146
|
+
{ sequence: '9007199254740992', status: 'RUNNING' },
|
|
147
|
+
])?.sequence,
|
|
148
|
+
).toBe('9007199254740993');
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('newer status fragments replace an earlier state of the same run', () => {
|
|
153
|
+
const queued = { sequence: '5', version: '1', status: 'QUEUED' };
|
|
154
|
+
const complete = { sequence: '5', version: '3', status: 'COMPLETE' };
|
|
155
|
+
expect(getLatestScheduleRun([queued, complete])).toBe(complete);
|
|
156
|
+
expect(getLatestScheduleRun([complete, queued])).toBe(complete);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('superseded fragments invalidate an older queued copy of the same run', () => {
|
|
160
|
+
const complete = { sequence: '4', version: '8', status: 'COMPLETE' };
|
|
161
|
+
const queued = { sequence: '5', version: '1', status: 'QUEUED' };
|
|
162
|
+
const superseded = { sequence: '5', version: '3', status: 'SUPERSEDED' };
|
|
163
|
+
expect(getLatestScheduleRun([complete, superseded, queued])).toBe(complete);
|
|
164
|
+
expect(getLatestScheduleRun([queued, superseded])).toBe(null);
|
|
165
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
expandScheduleSeriesInstances,
|
|
4
|
+
validateScheduleRecurrence,
|
|
5
|
+
} from '../src/utils/recurrence.ts';
|
|
6
|
+
import { validateScheduleSlotTemplate } from '../src/utils/slot.ts';
|
|
7
|
+
|
|
8
|
+
describe('recurrence', () => {
|
|
9
|
+
it('expands a weekly rule in local time across daylight saving', () => {
|
|
10
|
+
const instances = expandScheduleSeriesInstances(
|
|
11
|
+
{
|
|
12
|
+
recurrence: { freq: 'WEEKLY', byWeekday: [1, 3] },
|
|
13
|
+
timeZone: 'America/Los_Angeles',
|
|
14
|
+
startsAt: '2026-03-02T15:00:00Z', // Monday 07:00 PST
|
|
15
|
+
durationMinutes: 240,
|
|
16
|
+
},
|
|
17
|
+
{ to: new Date('2026-03-13T00:00:00Z') },
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
expect(instances.map((instance) => instance.startAt.toISOString())).toEqual(
|
|
21
|
+
[
|
|
22
|
+
'2026-03-02T15:00:00.000Z',
|
|
23
|
+
'2026-03-04T15:00:00.000Z',
|
|
24
|
+
'2026-03-09T14:00:00.000Z', // still 07:00 local after spring forward
|
|
25
|
+
'2026-03-11T14:00:00.000Z',
|
|
26
|
+
],
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('validates rules', () => {
|
|
31
|
+
expect(
|
|
32
|
+
validateScheduleRecurrence({ freq: 'WEEKLY', byWeekday: [5, 1] })
|
|
33
|
+
.recurrence?.byWeekday,
|
|
34
|
+
).toEqual([1, 5]);
|
|
35
|
+
expect(
|
|
36
|
+
validateScheduleRecurrence({ freq: 'DAILY', byWeekday: [1] }).error,
|
|
37
|
+
).toBeTruthy();
|
|
38
|
+
expect(
|
|
39
|
+
validateScheduleRecurrence({
|
|
40
|
+
freq: 'YEARLY',
|
|
41
|
+
byMonth: [2],
|
|
42
|
+
byMonthDay: [30],
|
|
43
|
+
}).error,
|
|
44
|
+
).toBeTruthy();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('validates slot templates', () => {
|
|
48
|
+
const result = validateScheduleSlotTemplate([
|
|
49
|
+
{
|
|
50
|
+
key: 'teacher',
|
|
51
|
+
requiredCount: 2,
|
|
52
|
+
predicate: { qualifications: ['INFANT'] },
|
|
53
|
+
},
|
|
54
|
+
]);
|
|
55
|
+
expect(result.slotTemplate).toEqual([
|
|
56
|
+
{
|
|
57
|
+
key: 'teacher',
|
|
58
|
+
label: null,
|
|
59
|
+
requiredCount: 2,
|
|
60
|
+
predicate: { qualifications: ['INFANT'] },
|
|
61
|
+
ordinal: 0,
|
|
62
|
+
},
|
|
63
|
+
]);
|
|
64
|
+
expect(
|
|
65
|
+
validateScheduleSlotTemplate([{ key: 'a' }, { key: 'a' }]).error,
|
|
66
|
+
).toBeTruthy();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
canActOnScheduleRequest,
|
|
4
|
+
getScheduleRequestExpiry,
|
|
5
|
+
getScheduleRequestInitialStatus,
|
|
6
|
+
getScheduleRequestPolicy,
|
|
7
|
+
getScheduleRequestTransitionSources,
|
|
8
|
+
} from '../src/utils/request.ts';
|
|
9
|
+
|
|
10
|
+
describe('request lifecycle', () => {
|
|
11
|
+
const policy = getScheduleRequestPolicy({});
|
|
12
|
+
it('requires swap consent before manager approval even if an old policy disables it', () => {
|
|
13
|
+
expect(
|
|
14
|
+
getScheduleRequestTransitionSources('SWAP', 'APPROVE', 'MANAGER', policy),
|
|
15
|
+
).toEqual(['ACCEPTED_BY_COUNTERPARTY']);
|
|
16
|
+
expect(
|
|
17
|
+
getScheduleRequestTransitionSources('SWAP', 'APPROVE', 'MANAGER', {
|
|
18
|
+
...policy,
|
|
19
|
+
requireCounterpartyConsent: false,
|
|
20
|
+
}),
|
|
21
|
+
).toEqual(['ACCEPTED_BY_COUNTERPARTY']);
|
|
22
|
+
});
|
|
23
|
+
it('never allows a terminal request to act again', () => {
|
|
24
|
+
for (const status of [
|
|
25
|
+
'APPROVED',
|
|
26
|
+
'DECLINED',
|
|
27
|
+
'CANCELLED',
|
|
28
|
+
'EXPIRED',
|
|
29
|
+
] as const)
|
|
30
|
+
for (const verb of [
|
|
31
|
+
'approve',
|
|
32
|
+
'decline',
|
|
33
|
+
'consent',
|
|
34
|
+
'refuse',
|
|
35
|
+
'withdraw',
|
|
36
|
+
] as const)
|
|
37
|
+
for (const actor of ['MANAGER', 'COUNTERPARTY', 'REQUESTER'] as const)
|
|
38
|
+
expect(
|
|
39
|
+
canActOnScheduleRequest(
|
|
40
|
+
verb,
|
|
41
|
+
{ kind: 'SWAP', status },
|
|
42
|
+
actor,
|
|
43
|
+
policy,
|
|
44
|
+
),
|
|
45
|
+
).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
it('only auto-approves a clean worker claim or drop when explicitly enabled', () => {
|
|
48
|
+
const enabled = {
|
|
49
|
+
...policy,
|
|
50
|
+
autoApproveClaims: true,
|
|
51
|
+
autoApproveDrops: true,
|
|
52
|
+
};
|
|
53
|
+
expect(getScheduleRequestInitialStatus('CLAIM', policy, true, false)).toBe(
|
|
54
|
+
'PENDING',
|
|
55
|
+
);
|
|
56
|
+
expect(getScheduleRequestInitialStatus('CLAIM', enabled, true, false)).toBe(
|
|
57
|
+
'APPROVED',
|
|
58
|
+
);
|
|
59
|
+
expect(getScheduleRequestInitialStatus('DROP', enabled, false, false)).toBe(
|
|
60
|
+
'PENDING',
|
|
61
|
+
);
|
|
62
|
+
expect(getScheduleRequestInitialStatus('DROP', enabled, true, true)).toBe(
|
|
63
|
+
'PENDING',
|
|
64
|
+
);
|
|
65
|
+
expect(getScheduleRequestInitialStatus('SWAP', enabled, true, false)).toBe(
|
|
66
|
+
'PENDING',
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
it('expires at the earlier of the policy limit and shift start', () => {
|
|
70
|
+
const now = new Date('2026-09-06T00:00Z');
|
|
71
|
+
const start = new Date('2026-09-10T00:00Z');
|
|
72
|
+
expect(
|
|
73
|
+
getScheduleRequestExpiry(
|
|
74
|
+
{ ...policy, requestExpiryHours: 1 },
|
|
75
|
+
now,
|
|
76
|
+
start,
|
|
77
|
+
).toISOString(),
|
|
78
|
+
).toBe('2026-09-06T01:00:00.000Z');
|
|
79
|
+
expect(
|
|
80
|
+
getScheduleRequestExpiry(
|
|
81
|
+
{ ...policy, requestExpiryHours: 0 },
|
|
82
|
+
now,
|
|
83
|
+
start,
|
|
84
|
+
),
|
|
85
|
+
).toEqual(start);
|
|
86
|
+
});
|
|
87
|
+
it('rejects invalid policy values rather than treating strings as enabled switches', () => {
|
|
88
|
+
expect(() =>
|
|
89
|
+
getScheduleRequestPolicy({
|
|
90
|
+
settings: { requests: { autoApproveClaims: 'false' } },
|
|
91
|
+
}),
|
|
92
|
+
).toThrow('20150');
|
|
93
|
+
expect(() =>
|
|
94
|
+
getScheduleRequestPolicy({
|
|
95
|
+
settings: { requests: { minNoticeHours: -1 } },
|
|
96
|
+
}),
|
|
97
|
+
).toThrow('20150');
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
getQualificationExpiryMs,
|
|
4
|
+
validateScheduleDemandRow,
|
|
5
|
+
validateScheduleResourceConfig,
|
|
6
|
+
} from '../src/utils/resource.ts';
|
|
7
|
+
import { validateScheduleSlotPredicate } from '../src/utils/slot.ts';
|
|
8
|
+
import { at } from './coverage.fixtures.ts';
|
|
9
|
+
|
|
10
|
+
describe('validated resource config and demand', () => {
|
|
11
|
+
it('normalizes the closed empty shape and qualification dates', () => {
|
|
12
|
+
expect(validateScheduleResourceConfig('LABOR', null).config).toEqual({
|
|
13
|
+
qualifications: [],
|
|
14
|
+
roles: [],
|
|
15
|
+
tags: [],
|
|
16
|
+
ageGroup: null,
|
|
17
|
+
notify: null,
|
|
18
|
+
});
|
|
19
|
+
expect(
|
|
20
|
+
validateScheduleResourceConfig('LABOR', {
|
|
21
|
+
qualifications: [{ key: ' CPR ', expiresAt: '2026-01-01' }],
|
|
22
|
+
roles: ['B', 'A', 'A'],
|
|
23
|
+
}).config,
|
|
24
|
+
).toMatchObject({
|
|
25
|
+
qualifications: [{ key: 'CPR', expiresAt: '2026-01-01T00:00:00.000Z' }],
|
|
26
|
+
roles: ['A', 'B'],
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
it.each([
|
|
30
|
+
{ ratioDivisor: 4 },
|
|
31
|
+
{ qualifications: [{ key: 'CPR', expiresAt: 'bad' }] },
|
|
32
|
+
{ qualifications: [{ key: 'CPR' }, { key: 'CPR' }] },
|
|
33
|
+
{ roles: [5] },
|
|
34
|
+
{ notify: { channels: ['PUSH'] } },
|
|
35
|
+
{ notify: { channels: [], typo: true } },
|
|
36
|
+
])('rejects invalid labor config %j', (config) => {
|
|
37
|
+
expect(validateScheduleResourceConfig('LABOR', config).error).toBeTruthy();
|
|
38
|
+
});
|
|
39
|
+
it('enforces kind-specific fields and treats expiry invalid as epoch zero', () => {
|
|
40
|
+
expect(
|
|
41
|
+
validateScheduleResourceConfig('SPACE', { roles: ['TEACHER'] }).error,
|
|
42
|
+
).toBeTruthy();
|
|
43
|
+
expect(
|
|
44
|
+
validateScheduleResourceConfig('LABOR', { ageGroup: 'INFANT' }).error,
|
|
45
|
+
).toBeTruthy();
|
|
46
|
+
expect(getQualificationExpiryMs({ expiresAt: 'bad' })).toBe(0);
|
|
47
|
+
expect(getQualificationExpiryMs({ expiresAt: null })).toBeNull();
|
|
48
|
+
});
|
|
49
|
+
it('rejects unknown predicate fields and non-string keys', () => {
|
|
50
|
+
expect(validateScheduleSlotPredicate({ typo: [] }).error).toBeTruthy();
|
|
51
|
+
expect(validateScheduleSlotPredicate({ roles: [123] }).error).toBeTruthy();
|
|
52
|
+
});
|
|
53
|
+
it.each([-1, 1.111, NaN, Infinity])(
|
|
54
|
+
'rejects demand quantity %s',
|
|
55
|
+
(quantity) => {
|
|
56
|
+
expect(
|
|
57
|
+
validateScheduleDemandRow({
|
|
58
|
+
resourceId: 'room',
|
|
59
|
+
key: 'INFANT',
|
|
60
|
+
quantity,
|
|
61
|
+
startAt: at(8),
|
|
62
|
+
endAt: at(12),
|
|
63
|
+
}).error,
|
|
64
|
+
).toBeTruthy();
|
|
65
|
+
},
|
|
66
|
+
);
|
|
67
|
+
it('accepts fractional child forecasts and rejects reversed intervals or missing rooms', () => {
|
|
68
|
+
expect(
|
|
69
|
+
validateScheduleDemandRow({
|
|
70
|
+
resourceId: 'room',
|
|
71
|
+
key: ' INFANT ',
|
|
72
|
+
quantity: 4.25,
|
|
73
|
+
startAt: at(8),
|
|
74
|
+
endAt: at(12),
|
|
75
|
+
}).demand,
|
|
76
|
+
).toMatchObject({ key: 'INFANT', quantity: 4.25 });
|
|
77
|
+
expect(
|
|
78
|
+
validateScheduleDemandRow({
|
|
79
|
+
resourceId: 'room',
|
|
80
|
+
key: 'INFANT',
|
|
81
|
+
quantity: 4,
|
|
82
|
+
startAt: at(12),
|
|
83
|
+
endAt: at(8),
|
|
84
|
+
}).error,
|
|
85
|
+
).toBeTruthy();
|
|
86
|
+
expect(
|
|
87
|
+
validateScheduleDemandRow({
|
|
88
|
+
key: 'INFANT',
|
|
89
|
+
quantity: 4,
|
|
90
|
+
startAt: at(8),
|
|
91
|
+
endAt: at(12),
|
|
92
|
+
}).error,
|
|
93
|
+
).toBeTruthy();
|
|
94
|
+
});
|
|
95
|
+
});
|