|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import assert from 'assert'; |
| 7 | +import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js'; |
| 8 | +import { ChatTodoListWidget } from '../../browser/chatContentParts/chatTodoListWidget.js'; |
| 9 | +import { IChatTodo, IChatTodoListService } from '../../common/chatTodoListService.js'; |
| 10 | +import { mainWindow } from '../../../../../base/browser/window.js'; |
| 11 | + |
| 12 | +suite('ChatTodoListWidget Accessibility', () => { |
| 13 | + const store = ensureNoDisposablesAreLeakedInTestSuite(); |
| 14 | + |
| 15 | + let widget: ChatTodoListWidget; |
| 16 | + let mockTodoListService: IChatTodoListService; |
| 17 | + |
| 18 | + const sampleTodos: IChatTodo[] = [ |
| 19 | + { id: 1, title: 'First task', status: 'not-started' }, |
| 20 | + { id: 2, title: 'Second task', status: 'in-progress', description: 'This is a task description' }, |
| 21 | + { id: 3, title: 'Third task', status: 'completed' } |
| 22 | + ]; |
| 23 | + |
| 24 | + setup(() => { |
| 25 | + // Mock the todo list service |
| 26 | + mockTodoListService = { |
| 27 | + _serviceBrand: undefined, |
| 28 | + getTodos: (sessionId: string) => sampleTodos, |
| 29 | + setTodos: (sessionId: string, todos: IChatTodo[]) => { } |
| 30 | + }; |
| 31 | + |
| 32 | + widget = store.add(new ChatTodoListWidget(mockTodoListService)); |
| 33 | + mainWindow.document.body.appendChild(widget.domNode); |
| 34 | + }); |
| 35 | + |
| 36 | + teardown(() => { |
| 37 | + if (widget.domNode.parentNode) { |
| 38 | + widget.domNode.parentNode.removeChild(widget.domNode); |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + test('creates proper semantic list structure', () => { |
| 43 | + widget.render('test-session'); |
| 44 | + |
| 45 | + const todoListContainer = widget.domNode.querySelector('.todo-list-container'); |
| 46 | + assert.ok(todoListContainer, 'Should have todo list container'); |
| 47 | + assert.strictEqual(todoListContainer?.getAttribute('aria-labelledby'), 'todo-list-title'); |
| 48 | + assert.strictEqual(todoListContainer?.getAttribute('role'), 'list'); |
| 49 | + |
| 50 | + const titleElement = widget.domNode.querySelector('#todo-list-title'); |
| 51 | + assert.ok(titleElement, 'Should have title element with ID todo-list-title'); |
| 52 | + assert.ok(titleElement?.textContent?.includes('Todos')); |
| 53 | + |
| 54 | + // The todo list container itself acts as the list (no nested ul element) |
| 55 | + const todoItems = todoListContainer?.querySelectorAll('li.todo-item'); |
| 56 | + assert.ok(todoItems && todoItems.length > 0, 'Should have todo items in the list container'); |
| 57 | + }); |
| 58 | + |
| 59 | + test('todo items have proper accessibility attributes', () => { |
| 60 | + widget.render('test-session'); |
| 61 | + |
| 62 | + const todoItems = widget.domNode.querySelectorAll('.todo-item'); |
| 63 | + assert.strictEqual(todoItems.length, 3, 'Should have 3 todo items'); |
| 64 | + |
| 65 | + // Check first item (not-started) |
| 66 | + const firstItem = todoItems[0] as HTMLElement; |
| 67 | + assert.strictEqual(firstItem.getAttribute('role'), 'listitem'); |
| 68 | + assert.strictEqual(firstItem.getAttribute('tabindex'), '0'); |
| 69 | + assert.ok(firstItem.getAttribute('aria-label')?.includes('First task')); |
| 70 | + assert.ok(firstItem.getAttribute('aria-label')?.includes('not started')); |
| 71 | + |
| 72 | + // Check second item (in-progress with description) |
| 73 | + const secondItem = todoItems[1] as HTMLElement; |
| 74 | + assert.ok(secondItem.getAttribute('aria-label')?.includes('Second task')); |
| 75 | + assert.ok(secondItem.getAttribute('aria-label')?.includes('in progress')); |
| 76 | + assert.ok(secondItem.getAttribute('aria-label')?.includes('This is a task description')); |
| 77 | + |
| 78 | + // Check third item (completed) |
| 79 | + const thirdItem = todoItems[2] as HTMLElement; |
| 80 | + assert.ok(thirdItem.getAttribute('aria-label')?.includes('Third task')); |
| 81 | + assert.ok(thirdItem.getAttribute('aria-label')?.includes('completed')); |
| 82 | + }); |
| 83 | + |
| 84 | + test('status icons are hidden from screen readers', () => { |
| 85 | + widget.render('test-session'); |
| 86 | + |
| 87 | + const statusIcons = widget.domNode.querySelectorAll('.todo-status-icon'); |
| 88 | + statusIcons.forEach(icon => { |
| 89 | + assert.strictEqual(icon.getAttribute('aria-hidden'), 'true', 'Status icons should be hidden from screen readers'); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + test('expand button has proper accessibility attributes', () => { |
| 94 | + widget.render('test-session'); |
| 95 | + |
| 96 | + // The expandoElement has the accessibility attributes |
| 97 | + const expandoElement = widget.domNode.querySelector('.todo-list-expand'); |
| 98 | + assert.ok(expandoElement, 'Should have expando element'); |
| 99 | + assert.strictEqual(expandoElement?.getAttribute('role'), 'button'); |
| 100 | + assert.strictEqual(expandoElement?.getAttribute('tabindex'), '0'); |
| 101 | + assert.strictEqual(expandoElement?.getAttribute('aria-expanded'), 'false'); // Should be collapsed due to in-progress task |
| 102 | + assert.strictEqual(expandoElement?.getAttribute('aria-controls'), 'todo-list-container'); |
| 103 | + |
| 104 | + // The title element should have aria-label with progress information |
| 105 | + const titleElement = expandoElement?.querySelector('.todo-list-title'); |
| 106 | + assert.ok(titleElement, 'Should have title element'); |
| 107 | + const titleText = titleElement?.textContent; |
| 108 | + assert.ok(titleText?.includes('Todos (1/3)'), `Title should show progress format, but got: "${titleText}"`); |
| 109 | + }); test('hidden status text elements exist for screen readers', () => { |
| 110 | + widget.render('test-session'); |
| 111 | + |
| 112 | + const statusElements = widget.domNode.querySelectorAll('.todo-status-text'); |
| 113 | + assert.strictEqual(statusElements.length, 3, 'Should have 3 status text elements'); |
| 114 | + |
| 115 | + statusElements.forEach((element, index) => { |
| 116 | + assert.strictEqual(element.id, `todo-status-${index}`, 'Should have proper ID'); |
| 117 | + // Check that it's visually hidden but accessible to screen readers |
| 118 | + const style = (element as HTMLElement).style; |
| 119 | + assert.strictEqual(style.position, 'absolute'); |
| 120 | + assert.strictEqual(style.left, '-10000px'); |
| 121 | + assert.strictEqual(style.width, '1px'); |
| 122 | + assert.strictEqual(style.height, '1px'); |
| 123 | + assert.strictEqual(style.overflow, 'hidden'); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + test('widget displays properly when no todos exist', () => { |
| 128 | + // Create a new mock service with empty todos |
| 129 | + const emptyTodoListService: IChatTodoListService = { |
| 130 | + _serviceBrand: undefined, |
| 131 | + getTodos: (sessionId: string) => [], |
| 132 | + setTodos: (sessionId: string, todos: IChatTodo[]) => { } |
| 133 | + }; |
| 134 | + |
| 135 | + const emptyWidget = store.add(new ChatTodoListWidget(emptyTodoListService)); |
| 136 | + mainWindow.document.body.appendChild(emptyWidget.domNode); |
| 137 | + |
| 138 | + emptyWidget.render('test-session'); |
| 139 | + |
| 140 | + // Widget should be hidden when no todos |
| 141 | + assert.strictEqual(emptyWidget.domNode.style.display, 'none', 'Widget should be hidden when no todos'); |
| 142 | + }); |
| 143 | + |
| 144 | + test('clear button has proper accessibility', () => { |
| 145 | + widget.render('test-session'); |
| 146 | + |
| 147 | + const clearButton = widget.domNode.querySelector('.todo-clear-button-container .monaco-button'); |
| 148 | + assert.ok(clearButton, 'Should have clear button'); |
| 149 | + assert.strictEqual(clearButton?.getAttribute('tabindex'), '0', 'Clear button should be focusable'); |
| 150 | + }); |
| 151 | + |
| 152 | + test('title element displays progress correctly and is accessible', () => { |
| 153 | + widget.render('test-session'); |
| 154 | + |
| 155 | + const titleElement = widget.domNode.querySelector('#todo-list-title'); |
| 156 | + assert.ok(titleElement, 'Should have title element with ID'); |
| 157 | + |
| 158 | + // Title should show progress format: "Todos (1/3)" since one todo is in-progress |
| 159 | + // When collapsed, it also shows the current task: "Todos (1/3) - Second task" |
| 160 | + const titleText = titleElement?.textContent; |
| 161 | + assert.ok(titleText?.includes('Todos (1/3)'), `Title should show progress format, but got: "${titleText}"`); |
| 162 | + |
| 163 | + // Verify aria-labelledby connection works |
| 164 | + const todoListContainer = widget.domNode.querySelector('.todo-list-container'); |
| 165 | + assert.strictEqual(todoListContainer?.getAttribute('aria-labelledby'), 'todo-list-title'); |
| 166 | + }); |
| 167 | +}); |
0 commit comments