summaryrefslogtreecommitdiff
path: root/web/test/stories.test.mjs
blob: 263f202ed72afa9c3653678649383fe2c02bd33c (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
// stories.test.mjs — TDD tests for stories UI functions.
//
// Run with: node --test web/test/stories.test.mjs

import { describe, it, beforeEach } from 'node:test';
import assert from 'node:assert/strict';
import { renderStoryCard, storyStatusLabel } from '../app.js';

// ── Minimal DOM mock ──────────────────────────────────────────────────────────

function makeMockDoc() {
  function createElement(tag) {
    return {
      tag,
      className: '',
      textContent: '',
      innerHTML: '',
      hidden: false,
      dataset: {},
      children: [],
      _listeners: {},
      style: {},
      appendChild(child) { this.children.push(child); return child; },
      prepend(...nodes) { this.children.unshift(...nodes); },
      append(...nodes) { nodes.forEach(n => this.children.push(n)); },
      addEventListener(ev, fn) { this._listeners[ev] = fn; },
      querySelector(sel) {
        const cls = sel.startsWith('.') ? sel.slice(1) : null;
        function search(el) {
          if (cls && el.className && el.className.split(' ').includes(cls)) return el;
          for (const c of el.children || []) {
            const found = search(c);
            if (found) return found;
          }
          return null;
        }
        return search(this);
      },
      querySelectorAll(sel) {
        const cls = sel.startsWith('.') ? sel.slice(1) : null;
        const results = [];
        function search(el) {
          if (cls && el.className && el.className.split(' ').includes(cls)) results.push(el);
          for (const c of el.children || []) search(c);
        }
        search(this);
        return results;
      },
    };
  }
  return { createElement };
}

function makeStory(overrides = {}) {
  return {
    id: 'story-1',
    name: 'Add login page',
    project_id: 'claudomator',
    branch_name: 'story/add-login-page',
    status: 'PENDING',
    created_at: '2026-03-25T10:00:00Z',
    updated_at: '2026-03-25T10:00:00Z',
    ...overrides,
  };
}

// ── storyStatusLabel ──────────────────────────────────────────────────────────

describe('storyStatusLabel', () => {
  it('returns human-readable label for PENDING', () => {
    assert.equal(storyStatusLabel('PENDING'), 'Pending');
  });

  it('returns human-readable label for IN_PROGRESS', () => {
    assert.equal(storyStatusLabel('IN_PROGRESS'), 'In Progress');
  });

  it('returns human-readable label for SHIPPABLE', () => {
    assert.equal(storyStatusLabel('SHIPPABLE'), 'Shippable');
  });

  it('returns human-readable label for DEPLOYED', () => {
    assert.equal(storyStatusLabel('DEPLOYED'), 'Deployed');
  });

  it('returns human-readable label for VALIDATING', () => {
    assert.equal(storyStatusLabel('VALIDATING'), 'Validating');
  });

  it('returns human-readable label for REVIEW_READY', () => {
    assert.equal(storyStatusLabel('REVIEW_READY'), 'Review Ready');
  });

  it('returns human-readable label for NEEDS_FIX', () => {
    assert.equal(storyStatusLabel('NEEDS_FIX'), 'Needs Fix');
  });

  it('falls back to the raw status for unknown values', () => {
    assert.equal(storyStatusLabel('UNKNOWN_STATE'), 'UNKNOWN_STATE');
  });
});

// ── renderStoryCard ───────────────────────────────────────────────────────────

describe('renderStoryCard', () => {
  let doc;
  beforeEach(() => { doc = makeMockDoc(); });

  it('renders the story name', () => {
    const card = renderStoryCard(makeStory(), doc);
    function findText(el, text) {
      if (el.textContent === text) return true;
      return (el.children || []).some(c => findText(c, text));
    }
    assert.ok(findText(card, 'Add login page'), 'card should contain story name');
  });

  it('has story-card class', () => {
    const card = renderStoryCard(makeStory(), doc);
    assert.ok(card.className.split(' ').includes('story-card'), 'root element should have story-card class');
  });

  it('status badge has data-status matching story status', () => {
    const card = renderStoryCard(makeStory({ status: 'IN_PROGRESS' }), doc);
    const badge = card.querySelector('.story-status-badge');
    assert.ok(badge, 'badge element should exist');
    assert.equal(badge.dataset.status, 'IN_PROGRESS');
  });

  it('status badge shows human-readable label', () => {
    const card = renderStoryCard(makeStory({ status: 'REVIEW_READY' }), doc);
    const badge = card.querySelector('.story-status-badge');
    assert.equal(badge.textContent, 'Review Ready');
  });

  it('shows project_id', () => {
    const card = renderStoryCard(makeStory({ project_id: 'nav' }), doc);
    function findText(el, text) {
      if (el.textContent === text) return true;
      return (el.children || []).some(c => findText(c, text));
    }
    assert.ok(findText(card, 'nav'), 'card should show project_id');
  });

  it('shows branch_name when present', () => {
    const card = renderStoryCard(makeStory({ branch_name: 'story/my-feature' }), doc);
    function findText(el, text) {
      if (el.textContent && el.textContent.includes(text)) return true;
      return (el.children || []).some(c => findText(c, text));
    }
    assert.ok(findText(card, 'story/my-feature'), 'card should show branch_name');
  });

  it('does not show branch section when branch_name is empty', () => {
    const card = renderStoryCard(makeStory({ branch_name: '' }), doc);
    const branchEl = card.querySelector('.story-branch');
    assert.ok(!branchEl, 'no .story-branch element when branch is empty');
  });

  it('card dataset.storyId is set to story id', () => {
    const card = renderStoryCard(makeStory({ id: 'abc-123' }), doc);
    assert.equal(card.dataset.storyId, 'abc-123');
  });
});