-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNewChatForm.test.ts
More file actions
209 lines (175 loc) · 6.26 KB
/
Copy pathNewChatForm.test.ts
File metadata and controls
209 lines (175 loc) · 6.26 KB
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
import { fireEvent, render, screen, waitFor } from '@testing-library/svelte';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import NewChatFormTestHarness from './NewChatFormTestHarness.svelte';
import * as chatsApi from '$lib/api/chats';
import * as settingsApi from '$lib/api/settings';
import * as gitApi from '$lib/api/git';
import type { RemoteSettingsSnapshot } from '$shared/settings';
vi.mock('$lib/api/chats', () => ({
validateStart: vi.fn()
}));
vi.mock('$lib/api/git', () => ({
getGitWorktrees: vi.fn()
}));
vi.mock('$lib/api/settings', () => ({
getRemoteSettings: vi.fn(),
updateRemoteSettings: vi.fn(),
}));
function installMatchMedia(matches: boolean): void {
Object.defineProperty(window, 'matchMedia', {
configurable: true,
value: vi.fn().mockImplementation((query: string) => ({
matches,
media: query,
onchange: null,
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
addListener: vi.fn(),
removeListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
}
function deferred<T>() {
let resolve!: (value: T) => void;
const promise = new Promise<T>((res) => {
resolve = res;
});
return { promise, resolve };
}
function makeSnapshot(overrides: Partial<RemoteSettingsSnapshot> = {}): RemoteSettingsSnapshot {
return {
version: 1,
ui: {},
uiEffective: {},
paths: { pinnedProjectPaths: [], browseStartPath: '' },
pinnedChatIds: [],
lastProvider: 'claude',
lastProjectPath: '',
lastModel: 'opus',
lastApiProviderId: null,
lastModelEndpointId: null,
lastModelProtocol: null,
lastPermissionMode: 'default',
lastThinkingMode: 'none',
lastClaudeThinkingMode: 'auto',
lastAmpAgentMode: 'smart',
projectBasePath: '/workspace',
telegramBotTokenAvailable: false,
...overrides,
};
}
async function renderReadyNewChatForm(options: {
sendByShiftEnter?: boolean;
isMobile?: boolean;
} = {}) {
const onStartChat = vi.fn();
vi.mocked(settingsApi.getRemoteSettings).mockResolvedValueOnce(makeSnapshot({
lastProjectPath: '/workspace/project',
}));
vi.mocked(chatsApi.validateStart).mockResolvedValue({
valid: true,
isGitRepo: false
});
render(NewChatFormTestHarness, {
props: {
...options,
onStartChat,
}
});
const messageInput = await screen.findByPlaceholderText('How can I help you today?') as HTMLTextAreaElement;
await waitFor(() => {
expect(screen.queryByRole('status', { name: 'Loading chat defaults...' })).toBeNull();
});
await fireEvent.input(messageInput, { target: { value: 'Start this session' } });
const sendButton = screen.getByTitle('Start session') as HTMLButtonElement;
await waitFor(() => {
expect(sendButton.disabled).toBe(false);
});
return { messageInput, onStartChat };
}
describe('NewChatForm', () => {
beforeEach(() => {
vi.clearAllMocks();
installMatchMedia(false);
});
it('shows a centered spinner and hides the composer until settings load', async () => {
const pending = deferred<Awaited<ReturnType<typeof settingsApi.getRemoteSettings>>>();
vi.mocked(settingsApi.getRemoteSettings).mockReturnValueOnce(pending.promise);
const { container } = render(NewChatFormTestHarness);
const projectPathInput = screen.getByLabelText('Project Path');
const messageInput = screen.getByPlaceholderText('How can I help you today?');
const hiddenFormContainer = container.querySelector('.space-y-6[aria-hidden="true"]');
expect(screen.getByRole('status', { name: 'Loading chat defaults...' })).toBeTruthy();
expect(hiddenFormContainer).toBeTruthy();
expect(hiddenFormContainer?.contains(projectPathInput)).toBe(true);
expect(hiddenFormContainer?.contains(messageInput)).toBe(true);
pending.resolve(makeSnapshot({
lastProjectPath: '/workspace/project',
}));
await waitFor(() => {
expect(screen.queryByRole('status', { name: 'Loading chat defaults...' })).toBeNull();
});
expect(container.querySelector('.space-y-6[aria-hidden="true"]')).toBeNull();
expect(container.querySelector('.space-y-6[aria-hidden="false"]')?.contains(projectPathInput)).toBe(true);
expect(container.querySelector('.space-y-6[aria-hidden="false"]')?.contains(messageInput)).toBe(true);
});
it('opens the worktree picker as a separate dialog when the project is a git repo', async () => {
const chatsApi = await import('$lib/api/chats');
vi.mocked(settingsApi.getRemoteSettings).mockResolvedValueOnce(makeSnapshot({
lastProjectPath: '/workspace/project',
}));
vi.mocked(chatsApi.validateStart).mockResolvedValue({
valid: true,
isGitRepo: true
});
vi.mocked(gitApi.getGitWorktrees).mockResolvedValue({
worktrees: [
{
name: 'main',
path: '/workspace/project',
branch: 'main',
isCurrent: true,
isMain: true,
isPathMissing: false
}
]
});
render(NewChatFormTestHarness);
const openButton = await screen.findByRole('button', { name: 'Select a different worktree' });
await fireEvent.click(openButton);
const worktreeDialog = await screen.findByRole('dialog', { name: 'Select worktree' });
expect(worktreeDialog).toBeTruthy();
expect(worktreeDialog.textContent).toContain('New worktree');
});
it('starts on Enter when send by Shift+Enter is disabled', async () => {
const { messageInput, onStartChat } = await renderReadyNewChatForm({
sendByShiftEnter: false,
});
await fireEvent.keyDown(messageInput, { key: 'Enter' });
expect(onStartChat).toHaveBeenCalledTimes(1);
});
it('keeps Enter as a newline when send by Shift+Enter is enabled', async () => {
const { messageInput, onStartChat } = await renderReadyNewChatForm({
sendByShiftEnter: true,
});
await fireEvent.keyDown(messageInput, { key: 'Enter' });
expect(onStartChat).not.toHaveBeenCalled();
});
it('starts on Shift+Enter when send by Shift+Enter is enabled', async () => {
const { messageInput, onStartChat } = await renderReadyNewChatForm({
sendByShiftEnter: true,
});
await fireEvent.keyDown(messageInput, { key: 'Enter', shiftKey: true });
expect(onStartChat).toHaveBeenCalledTimes(1);
});
it('does not let the mobile viewport override the Enter setting', async () => {
installMatchMedia(true);
const { messageInput, onStartChat } = await renderReadyNewChatForm({
sendByShiftEnter: false,
isMobile: true,
});
await fireEvent.keyDown(messageInput, { key: 'Enter' });
expect(onStartChat).toHaveBeenCalledTimes(1);
});
});