summaryrefslogtreecommitdiff
path: root/web/test/task-actions.test.mjs
blob: a1790faab2cecee04ec082af2370bdd91e0f5af5 (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
// task-actions.test.mjs — button visibility logic for Cancel/Restart/Resume actions
//
// Run with: node --test web/test/task-actions.test.mjs

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

// ── Logic under test ──────────────────────────────────────────────────────────
//
// Interrupted states (CANCELLED, FAILED, BUDGET_EXCEEDED) get BOTH a Resume
// button and a Restart button. TIMED_OUT gets Resume only.

const RESUME_STATES   = new Set(['TIMED_OUT', 'CANCELLED', 'FAILED', 'BUDGET_EXCEEDED']);
const RESTART_STATES  = new Set(['CANCELLED', 'FAILED', 'BUDGET_EXCEEDED']);

function getCardActions(state) {
  const actions = [];
  if (state === 'PENDING')  return ['run'];
  if (state === 'RUNNING')  return ['cancel'];
  if (state === 'READY')    return ['approve'];
  if (RESUME_STATES.has(state))  actions.push('resume');
  if (RESTART_STATES.has(state)) actions.push('restart');
  return actions.length ? actions : [null];
}

function getResumeEndpoint(state) {
  return RESUME_STATES.has(state) ? '/resume' : null;
}

function getRestartEndpoint(state) {
  return RESTART_STATES.has(state) ? '/run' : null;
}

// ── Tests ─────────────────────────────────────────────────────────────────────

describe('task card action buttons', () => {
  it('shows Run button for PENDING', () => {
    assert.deepEqual(getCardActions('PENDING'), ['run']);
  });

  it('shows Cancel button for RUNNING', () => {
    assert.deepEqual(getCardActions('RUNNING'), ['cancel']);
  });

  it('shows Resume AND Restart buttons for FAILED', () => {
    assert.deepEqual(getCardActions('FAILED'), ['resume', 'restart']);
  });

  it('shows Resume AND Restart buttons for CANCELLED', () => {
    assert.deepEqual(getCardActions('CANCELLED'), ['resume', 'restart']);
  });

  it('shows Resume AND Restart buttons for BUDGET_EXCEEDED', () => {
    assert.deepEqual(getCardActions('BUDGET_EXCEEDED'), ['resume', 'restart']);
  });

  it('shows Resume button only for TIMED_OUT (no restart)', () => {
    assert.deepEqual(getCardActions('TIMED_OUT'), ['resume']);
  });

  it('shows approve buttons for READY', () => {
    assert.deepEqual(getCardActions('READY'), ['approve']);
  });

  it('shows no button for COMPLETED', () => {
    assert.deepEqual(getCardActions('COMPLETED'), [null]);
  });

  it('shows no button for QUEUED', () => {
    assert.deepEqual(getCardActions('QUEUED'), [null]);
  });
});

describe('task action API endpoints', () => {
  it('TIMED_OUT uses /resume endpoint', () => {
    assert.equal(getResumeEndpoint('TIMED_OUT'), '/resume');
  });

  it('CANCELLED uses /resume endpoint for resume', () => {
    assert.equal(getResumeEndpoint('CANCELLED'), '/resume');
  });

  it('FAILED uses /resume endpoint for resume', () => {
    assert.equal(getResumeEndpoint('FAILED'), '/resume');
  });

  it('BUDGET_EXCEEDED uses /resume endpoint for resume', () => {
    assert.equal(getResumeEndpoint('BUDGET_EXCEEDED'), '/resume');
  });

  it('CANCELLED uses /run endpoint for restart', () => {
    assert.equal(getRestartEndpoint('CANCELLED'), '/run');
  });

  it('FAILED uses /run endpoint for restart', () => {
    assert.equal(getRestartEndpoint('FAILED'), '/run');
  });

  it('BUDGET_EXCEEDED uses /run endpoint for restart', () => {
    assert.equal(getRestartEndpoint('BUDGET_EXCEEDED'), '/run');
  });

  it('TIMED_OUT has no /run restart endpoint', () => {
    assert.equal(getRestartEndpoint('TIMED_OUT'), null);
  });
});