File size: 4,588 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Guards the editing-key contract of the ChatFormInputRich:
// undo/redo is replayed from source snapshots (the token rebuilds destroy
// the native undo stack), and Tab is NOT intercepted (WCAG 2.1.2 no
// keyboard trap), matching the plain textarea.

import ChatFormInputRichHarness from './components/ChatFormInputRichHarness.svelte';
import { tick } from 'svelte';
import { describe, expect, it } from 'vitest';
import { render } from 'vitest-browser-svelte';

const SOURCE = 'see [docs](file:///a/b)';

function editableIn(container: HTMLElement): HTMLElement {
	const el = container.querySelector('[role="textbox"]');

	if (!(el instanceof HTMLElement)) throw new Error('ChatFormInputRich not rendered');

	return el;
}

function type(root: HTMLElement, text: string, inputType = 'insertText') {
	root.appendChild(document.createTextNode(text));
	root.dispatchEvent(new InputEvent('input', { bubbles: true, inputType }));
}

function keydown(root: HTMLElement, init: KeyboardEventInit) {
	const event = new KeyboardEvent('keydown', { bubbles: true, cancelable: true, ...init });

	root.dispatchEvent(event);

	return event;
}

describe('ChatFormInputRich undo/redo', () => {
	it('undoes and redoes an edit across a badge-containing buffer', async () => {
		const screen = render(ChatFormInputRichHarness, { value: SOURCE });

		await tick();

		const root = editableIn(screen.container);

		type(root, ' more');
		await tick();
		expect(screen.component.getValue()).toBe(`${SOURCE} more`);

		const undoEvent = keydown(root, { ctrlKey: true, key: 'z' });

		await tick();
		expect(undoEvent.defaultPrevented).toBe(true);
		expect(screen.component.getValue()).toBe(SOURCE);

		const redoEvent = keydown(root, { ctrlKey: true, key: 'z', shiftKey: true });

		await tick();
		expect(redoEvent.defaultPrevented).toBe(true);
		expect(screen.component.getValue()).toBe(`${SOURCE} more`);
	});

	it('redoes with Ctrl+Y as well', async () => {
		const screen = render(ChatFormInputRichHarness, { value: SOURCE });

		await tick();

		const root = editableIn(screen.container);

		type(root, ' more');
		await tick();
		keydown(root, { key: 'z', metaKey: true });
		await tick();
		expect(screen.component.getValue()).toBe(SOURCE);

		keydown(root, { ctrlKey: true, key: 'y' });
		await tick();
		expect(screen.component.getValue()).toBe(`${SOURCE} more`);
	});

	it('coalesces a typing burst into one undo step', async () => {
		const screen = render(ChatFormInputRichHarness, { value: 'abc' });

		await tick();

		const root = editableIn(screen.container);

		type(root, 'd');
		type(root, 'e');
		await tick();
		expect(screen.component.getValue()).toBe('abcde');

		keydown(root, { ctrlKey: true, key: 'z' });
		await tick();
		expect(screen.component.getValue()).toBe('abc');
	});

	it('keeps a newline as its own undo step', async () => {
		const screen = render(ChatFormInputRichHarness, { value: 'abc' });

		await tick();

		const root = editableIn(screen.container);

		type(root, 'd');
		type(root, '\n', 'insertLineBreak');
		await tick();
		expect(screen.component.getValue()).toBe('abcd\n');

		keydown(root, { ctrlKey: true, key: 'z' });
		await tick();
		expect(screen.component.getValue()).toBe('abcd');

		keydown(root, { ctrlKey: true, key: 'z' });
		await tick();
		expect(screen.component.getValue()).toBe('abc');
	});

	it('is a no-op when there is nothing to undo', async () => {
		const screen = render(ChatFormInputRichHarness, { value: 'abc' });

		await tick();

		const root = editableIn(screen.container);
		const event = keydown(root, { ctrlKey: true, key: 'z' });

		await tick();

		expect(event.defaultPrevented).toBe(true);
		expect(screen.component.getValue()).toBe('abc');
	});

	it('abandons the redo branch after a fresh edit', async () => {
		const screen = render(ChatFormInputRichHarness, { value: 'abc' });

		await tick();

		const root = editableIn(screen.container);

		type(root, 'd');
		await tick();
		keydown(root, { ctrlKey: true, key: 'z' });
		await tick();
		expect(screen.component.getValue()).toBe('abc');

		type(root, 'e');
		await tick();
		keydown(root, { ctrlKey: true, key: 'z', shiftKey: true });
		await tick();
		expect(screen.component.getValue()).toBe('abce');
	});
});

describe('ChatFormInputRich Tab key', () => {
	it('does not trap Tab (focus can leave the editable)', async () => {
		const screen = render(ChatFormInputRichHarness, { value: SOURCE });

		await tick();

		const root = editableIn(screen.container);
		const event = keydown(root, { key: 'Tab' });

		expect(event.defaultPrevented).toBe(false);
	});
});