task-list.test.ts
TypeScript · 128 рядків
1import test from 'node:test'
2import assert from 'node:assert/strict'
3import { queryExternalTasks, queryTasks } from '../shared/task-list.ts'
4
5const tasks = [
6 {
7 id: 'task-10',
8 entityId: 'PART-10',
9 orderId: 'ORDER-2',
10 type: 'PICK',
11 title: 'Pick part 10',
12 status: 'open',
13 state: 'created',
14 context: { large: 'workflow data' },
15 payload: { large: 'task payload' },
16 createdAt: '2026-01-01T10:00:00.000Z',
17 updatedAt: '2026-01-01T12:00:00.000Z'
18 },
19 {
20 id: 'task-2',
21 entityId: 'PART-2',
22 orderId: 'ORDER-1',
23 type: 'DELIVERY',
24 title: 'Deliver part 2',
25 status: 'in_progress',
26 state: 'moving',
27 createdAt: '2026-01-01T09:00:00.000Z',
28 updatedAt: '2026-01-01T11:00:00.000Z'
29 },
30 {
31 id: 'task-1',
32 entityId: 'PART-1',
33 orderId: 'ORDER-1',
34 type: 'PICK',
35 title: 'Pick part 1',
36 status: 'completed',
37 state: 'done',
38 createdAt: '2026-01-01T08:00:00.000Z',
39 updatedAt: '2026-01-01T13:00:00.000Z'
40 },
41 {
42 id: 'task-cancelled',
43 entityId: 'PART-99',
44 orderId: 'ORDER-9',
45 type: 'PICK',
46 title: 'Cancelled task',
47 status: 'cancelled',
48 state: 'cancelled',
49 createdAt: '2026-01-01T07:00:00.000Z',
50 updatedAt: '2026-01-01T14:00:00.000Z'
51 }
52]
53
54test('task list separates active and completed tasks with stable counts', () => {
55 const active = queryTasks(tasks)
56 const completed = queryTasks(tasks, { scope: 'completed' })
57
58 assert.deepEqual(active.tasks.map(task => task.id), ['task-10', 'task-2'])
59 assert.deepEqual(completed.tasks.map(task => task.id), ['task-1'])
60 assert.deepEqual(active.counts, {
61 active: 2,
62 completed: 1
63 })
64 assert.deepEqual(active.types, ['DELIVERY', 'PICK'])
65 assert.deepEqual(completed.types, ['PICK'])
66 assert.equal('context' in active.tasks[0], false)
67 assert.equal('payload' in active.tasks[0], false)
68})
69
70test('task list searches codes, filters types, and sorts table columns', () => {
71 const byOrder = queryTasks(tasks, {
72 query: 'order-1',
73 sort: 'entityId',
74 direction: 'asc'
75 })
76 assert.deepEqual(byOrder.tasks.map(task => task.entityId), ['PART-2'])
77
78 const pickTasks = queryTasks(tasks, {
79 type: 'PICK',
80 sort: 'entityId',
81 direction: 'asc'
82 })
83 assert.deepEqual(pickTasks.tasks.map(task => task.entityId), ['PART-10'])
84
85 const byTaskId = queryTasks(tasks, {
86 scope: 'completed',
87 query: 'task-1'
88 })
89 assert.equal(byTaskId.total, 1)
90 assert.equal(byTaskId.tasks[0].entityId, 'PART-1')
91})
92
93test('external task list filters by exact entity and person user id', () => {
94 const result = queryExternalTasks([
95 ...tasks,
96 {
97 id: 'task-user',
98 entityType: 'warehouse_item',
99 entityId: 'PART-2',
100 type: 'PICK',
101 status: 'open',
102 assignee: { type: 'person', id: 'user-7' },
103 updatedAt: '2026-01-02T10:00:00.000Z'
104 }
105 ], {
106 entityType: 'warehouse_item',
107 entityId: 'PART-2',
108 userId: 'user-7'
109 })
110
111 assert.equal(result.total, 1)
112 assert.equal(result.tasks[0].id, 'task-user')
113 assert.equal('context' in result.tasks[0], false)
114})
115
116test('external task list supports status filters and bounded pagination', () => {
117 const result = queryExternalTasks(tasks, {
118 status: 'open,in_progress',
119 limit: 1,
120 offset: 1
121 })
122
123 assert.equal(result.total, 2)
124 assert.equal(result.tasks.length, 1)
125 assert.equal(result.limit, 1)
126 assert.equal(result.offset, 1)
127})
128