summaryrefslogtreecommitdiff
path: root/web/test/running-view.test.mjs
blob: 88419bc7da76075809a0fcf27b77ca1aa980e0e0 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// running-view.test.mjs — pure function tests for the Running tab
//
// Tests:
//   filterRunningTasks(tasks)       — returns only tasks where state === RUNNING
//   formatElapsed(startISO)         — returns elapsed string like "2m 30s", "1h 5m"
//   fetchRecentExecutions(basePath, fetchFn) — calls /api/executions?since=24h
//   formatDuration(startISO, endISO) — returns duration string for history table
//
// Run with: node --test web/test/running-view.test.mjs

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';

// ── Inline implementations ─────────────────────────────────────────────────────

function extractLogLines(lines, max = 500) {
  if (lines.length <= max) return lines;
  return lines.slice(lines.length - max);
}

function filterRunningTasks(tasks) {
  return tasks.filter(t => t.state === 'RUNNING');
}

function formatElapsed(startISO) {
  if (startISO == null) return '';
  const elapsed = Math.floor((Date.now() - new Date(startISO).getTime()) / 1000);
  if (elapsed < 0) return '0s';
  const h = Math.floor(elapsed / 3600);
  const m = Math.floor((elapsed % 3600) / 60);
  const s = elapsed % 60;
  if (h > 0) return `${h}h ${m}m`;
  if (m > 0) return `${m}m ${s}s`;
  return `${s}s`;
}

async function fetchRecentExecutions(basePath, fetchFn) {
  const res = await fetchFn(`${basePath}/api/executions?since=24h`);
  if (!res.ok) throw new Error(`HTTP ${res.status}`);
  return res.json();
}

// formatDuration: returns human-readable duration between two ISO timestamps.
// If endISO is null/undefined, uses now (for in-progress tasks).
// If startISO is null/undefined, returns '--'.
function formatDuration(startISO, endISO) {
  if (startISO == null) return '--';
  const start = new Date(startISO).getTime();
  const end = endISO != null ? new Date(endISO).getTime() : Date.now();
  const elapsed = Math.max(0, Math.floor((end - start) / 1000));
  const h = Math.floor(elapsed / 3600);
  const m = Math.floor((elapsed % 3600) / 60);
  const s = elapsed % 60;
  if (h > 0) return `${h}h ${m}m`;
  if (m > 0) return `${m}m ${s}s`;
  return `${s}s`;
}

// ── Tests: filterRunningTasks ─────────────────────────────────────────────────

describe('filterRunningTasks', () => {
  it('returns only RUNNING tasks from mixed list', () => {
    const tasks = [
      { id: '1', state: 'RUNNING' },
      { id: '2', state: 'COMPLETED' },
      { id: '3', state: 'RUNNING' },
      { id: '4', state: 'QUEUED' },
    ];
    const result = filterRunningTasks(tasks);
    assert.equal(result.length, 2);
    assert.ok(result.every(t => t.state === 'RUNNING'));
  });

  it('returns empty array when no tasks are RUNNING', () => {
    const tasks = [
      { id: '1', state: 'COMPLETED' },
      { id: '2', state: 'QUEUED' },
    ];
    assert.deepEqual(filterRunningTasks(tasks), []);
  });

  it('handles empty task list', () => {
    assert.deepEqual(filterRunningTasks([]), []);
  });

  it('does not include QUEUED tasks', () => {
    const tasks = [{ id: '1', state: 'QUEUED' }];
    assert.deepEqual(filterRunningTasks(tasks), []);
  });

  it('does not include READY tasks', () => {
    const tasks = [{ id: '1', state: 'READY' }];
    assert.deepEqual(filterRunningTasks(tasks), []);
  });
});

// ── Tests: formatElapsed ──────────────────────────────────────────────────────

describe('formatElapsed', () => {
  it('returns empty string for null', () => {
    assert.equal(formatElapsed(null), '');
  });

  it('returns empty string for undefined', () => {
    assert.equal(formatElapsed(undefined), '');
  });

  it('returns Xs format for elapsed under a minute', () => {
    const start = new Date(Date.now() - 45 * 1000).toISOString();
    assert.equal(formatElapsed(start), '45s');
  });

  it('returns Xm Ys format for 2 minutes 30 seconds ago', () => {
    const start = new Date(Date.now() - (2 * 60 + 30) * 1000).toISOString();
    assert.equal(formatElapsed(start), '2m 30s');
  });

  it('returns Xh Ym format for over an hour', () => {
    const start = new Date(Date.now() - (1 * 3600 + 5 * 60) * 1000).toISOString();
    assert.equal(formatElapsed(start), '1h 5m');
  });

  it('returns 0s for future timestamp', () => {
    const start = new Date(Date.now() + 60 * 1000).toISOString();
    assert.equal(formatElapsed(start), '0s');
  });
});

// ── Tests: fetchRecentExecutions ──────────────────────────────────────────────

describe('fetchRecentExecutions', () => {
  it('calls /api/executions?since=24h with basePath prefix', async () => {
    let calledUrl;
    const mockFetch = async (url) => {
      calledUrl = url;
      return { ok: true, json: async () => [] };
    };
    await fetchRecentExecutions('/claudomator', mockFetch);
    assert.equal(calledUrl, '/claudomator/api/executions?since=24h');
  });

  it('calls with empty basePath', async () => {
    let calledUrl;
    const mockFetch = async (url) => {
      calledUrl = url;
      return { ok: true, json: async () => [] };
    };
    await fetchRecentExecutions('', mockFetch);
    assert.equal(calledUrl, '/api/executions?since=24h');
  });

  it('returns parsed JSON response', async () => {
    const data = [{ id: 'exec-1', task_id: 't-1', status: 'COMPLETED' }];
    const mockFetch = async () => ({ ok: true, json: async () => data });
    const result = await fetchRecentExecutions('', mockFetch);
    assert.deepEqual(result, data);
  });

  it('throws on non-OK HTTP status', async () => {
    const mockFetch = async () => ({ ok: false, status: 500 });
    await assert.rejects(
      () => fetchRecentExecutions('', mockFetch),
      /HTTP 500/,
    );
  });
});

// ── Tests: formatDuration ─────────────────────────────────────────────────────

describe('formatDuration', () => {
  it('returns -- for null startISO', () => {
    assert.equal(formatDuration(null, null), '--');
  });

  it('returns -- for undefined startISO', () => {
    assert.equal(formatDuration(undefined, null), '--');
  });

  it('returns Xs for duration under a minute', () => {
    const start = new Date(Date.now() - 45 * 1000).toISOString();
    const end = new Date().toISOString();
    assert.equal(formatDuration(start, end), '45s');
  });

  it('returns Xm Ys for duration between 1 and 60 minutes', () => {
    const start = new Date(Date.now() - (3 * 60 + 15) * 1000).toISOString();
    const end = new Date().toISOString();
    assert.equal(formatDuration(start, end), '3m 15s');
  });

  it('returns Xh Ym for duration over an hour', () => {
    const start = new Date(Date.now() - (2 * 3600 + 30 * 60) * 1000).toISOString();
    const end = new Date().toISOString();
    assert.equal(formatDuration(start, end), '2h 30m');
  });

  it('uses current time when endISO is null', () => {
    // Start was 10s ago, no end → should return ~10s
    const start = new Date(Date.now() - 10 * 1000).toISOString();
    const result = formatDuration(start, null);
    assert.match(result, /^\d+s$/);
  });
});

// ── sortExecutionsDesc: inline implementation ─────────────────────────────────

function sortExecutionsDesc(executions) {
  return [...executions].sort((a, b) =>
    new Date(b.started_at).getTime() - new Date(a.started_at).getTime(),
  );
}

// ── Tests: sortExecutionsDesc ─────────────────────────────────────────────────

describe('sortExecutionsDesc', () => {
  it('sorts executions newest first', () => {
    const execs = [
      { id: 'a', started_at: '2024-01-01T00:00:00Z' },
      { id: 'b', started_at: '2024-01-03T00:00:00Z' },
      { id: 'c', started_at: '2024-01-02T00:00:00Z' },
    ];
    const result = sortExecutionsDesc(execs);
    assert.equal(result[0].id, 'b');
    assert.equal(result[1].id, 'c');
    assert.equal(result[2].id, 'a');
  });

  it('returns empty array for empty input', () => {
    assert.deepEqual(sortExecutionsDesc([]), []);
  });

  it('does not mutate the original array', () => {
    const execs = [
      { id: 'a', started_at: '2024-01-01T00:00:00Z' },
      { id: 'b', started_at: '2024-01-03T00:00:00Z' },
    ];
    const copy = [execs[0], execs[1]];
    sortExecutionsDesc(execs);
    assert.deepEqual(execs, copy);
  });

  it('returns single-element array unchanged', () => {
    const execs = [{ id: 'a', started_at: '2024-01-01T00:00:00Z' }];
    assert.equal(sortExecutionsDesc(execs)[0].id, 'a');
  });
});

// ── Tests: filterRunningTasks (explicit empty input check) ─────────────────────

describe('filterRunningTasks (empty input)', () => {
  it('returns [] for empty input', () => {
    assert.deepEqual(filterRunningTasks([]), []);
  });
});

// ── Tests: extractLogLines ────────────────────────────────────────────────────

describe('extractLogLines', () => {
  it('returns lines unchanged when count is below max', () => {
    const lines = ['a', 'b', 'c'];
    assert.deepEqual(extractLogLines(lines, 500), lines);
  });

  it('returns lines unchanged when count equals max', () => {
    const lines = Array.from({ length: 500 }, (_, i) => `line${i}`);
    assert.equal(extractLogLines(lines, 500).length, 500);
    assert.equal(extractLogLines(lines, 500)[0], 'line0');
  });

  it('truncates to last max lines when count exceeds max', () => {
    const lines = Array.from({ length: 600 }, (_, i) => `line${i}`);
    const result = extractLogLines(lines, 500);
    assert.equal(result.length, 500);
    assert.equal(result[0], 'line100');
    assert.equal(result[499], 'line599');
  });

  it('uses default max of 500', () => {
    const lines = Array.from({ length: 501 }, (_, i) => `line${i}`);
    const result = extractLogLines(lines);
    assert.equal(result.length, 500);
    assert.equal(result[0], 'line1');
  });

  it('returns empty array for empty input', () => {
    assert.deepEqual(extractLogLines([]), []);
  });

  it('does not mutate the original array', () => {
    const lines = Array.from({ length: 600 }, (_, i) => `line${i}`);
    const copy = [...lines];
    extractLogLines(lines, 500);
    assert.deepEqual(lines, copy);
  });
});