-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathRepository.test.jsx
More file actions
524 lines (421 loc) · 16.5 KB
/
Repository.test.jsx
File metadata and controls
524 lines (421 loc) · 16.5 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import React from "react";
import { Repository } from "../../src/pages/Repository";
import { setupAPIMock } from "../testutils/api-mocks";
import { AppContext } from "../../src/contexts/AppContext";
import { vi } from "vitest";
import "@testing-library/jest-dom";
let axiosMock;
// Mock child components to keep tests focused on Repository logic
vi.mock("../../src/components/SetupRepository", () => ({
SetupRepository: () => <div>Setup Repository Component</div>,
}));
vi.mock("../../src/components/Logs", () => ({
// eslint-disable-next-line react/prop-types
Logs: ({ taskID }) => <div>Logs for task {taskID}</div>,
}));
vi.mock("../../src/utils/taskutil", async () => {
const actual = await vi.importActual("../../src/utils/taskutil");
return {
...actual,
cancelTask: vi.fn(),
};
});
// Mock context value
const mockContextValue = {
repositoryUpdated: vi.fn(),
repositoryDescriptionUpdated: vi.fn(),
repoDescription: "Test Repository",
};
// Common test data
const connectedStatus = {
connected: true,
description: "My Test Repository",
readonly: false,
configFile: "/path/to/config",
storage: "filesystem",
encryption: "AES256-GCM-HMAC-SHA256",
hash: "BLAKE2B-256",
splitter: "DYNAMIC-4M-BUZHASH",
formatVersion: "1",
eccOverheadPercent: 10,
ecc: "REED-SOLOMON",
supportsContentCompression: true,
username: "testuser",
hostname: "testhost",
};
// Helper function to render Repository with context
const renderWithContext = (contextValue = mockContextValue) => {
return render(
<AppContext.Provider value={contextValue}>
<Repository />
</AppContext.Provider>,
);
};
/**
* Setup API mocks before each test
*/
beforeEach(() => {
axiosMock = setupAPIMock();
// Clear all mocks
vi.clearAllMocks();
// Mock throttle API to return default empty settings
axiosMock.onGet("/api/v1/repo/throttle").reply(200, {
maxUploadSpeedBytesPerSecond: 0,
maxDownloadSpeedBytesPerSecond: 0,
});
});
/**
* Clean up after each test
*/
afterEach(() => {
axiosMock.reset();
});
describe("Repository component - loading state", () => {
test("shows loading spinner initially", () => {
// Mock a delayed response
axiosMock.onGet("/api/v1/repo/status").reply(() => {
return new Promise(() => {
// Never resolve to keep loading state
});
});
renderWithContext();
// React Bootstrap spinner has this specific class
const spinner = document.querySelector(".spinner-border");
expect(spinner).toBeInTheDocument();
});
test("handles API error gracefully", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(500, { message: "Server error" });
renderWithContext();
await waitFor(() => {
expect(screen.getByText("Request failed with status code 500")).toBeInTheDocument();
});
});
});
describe("Repository component - connected state", () => {
test("displays connected repository information", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, connectedStatus);
renderWithContext();
await waitFor(() => {
expect(screen.getByText("Connected To Repository")).toBeInTheDocument();
expect(screen.getByDisplayValue("My Test Repository")).toBeInTheDocument();
expect(screen.getByDisplayValue("filesystem")).toBeInTheDocument();
expect(screen.getByDisplayValue("AES256-GCM-HMAC-SHA256")).toBeInTheDocument();
expect(screen.getByDisplayValue("BLAKE2B-256")).toBeInTheDocument();
expect(screen.getByDisplayValue("DYNAMIC-4M-BUZHASH")).toBeInTheDocument();
expect(screen.getByDisplayValue("10%")).toBeInTheDocument();
expect(screen.getByDisplayValue("REED-SOLOMON")).toBeInTheDocument();
expect(screen.getByDisplayValue("yes")).toBeInTheDocument();
expect(screen.getByDisplayValue("testuser@testhost")).toBeInTheDocument();
});
expect(mockContextValue.repositoryDescriptionUpdated).toHaveBeenCalledWith("My Test Repository");
});
test("displays readonly badge when repository is readonly", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, {
...connectedStatus,
readonly: true,
});
renderWithContext();
await waitFor(() => {
expect(screen.getByText("Repository is read-only")).toBeInTheDocument();
});
});
test("displays server URL when connected via API server", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, {
...connectedStatus,
apiServerURL: "https://api.example.com",
});
renderWithContext();
await waitFor(() => {
expect(screen.getByDisplayValue("https://api.example.com")).toBeInTheDocument();
// Should not display other config details when using API server
expect(screen.queryByDisplayValue("filesystem")).not.toBeInTheDocument();
});
});
test("updates repository description", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, connectedStatus);
axiosMock.onPost("/api/v1/repo/description").reply(200, {
description: "Updated Description",
});
renderWithContext();
await waitFor(() => {
expect(screen.getByDisplayValue("My Test Repository")).toBeInTheDocument();
});
// Change description
const descriptionInput = screen.getByDisplayValue("My Test Repository");
await userEvent.clear(descriptionInput);
await userEvent.type(descriptionInput, "Updated Description");
// Click update button
const updateButton = screen.getByTestId("update-description");
await userEvent.click(updateButton);
await waitFor(() => {
expect(mockContextValue.repositoryDescriptionUpdated).toHaveBeenCalledWith("Updated Description");
});
});
test("shows validation error when description is empty", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, {
...connectedStatus,
description: "",
});
renderWithContext();
await waitFor(() => {
// Get the description input specifically by its name attribute
const descriptionInput = document.querySelector('input[name="status.description"]');
expect(descriptionInput).toHaveClass("is-invalid");
expect(screen.getByText("Description Is Required")).toBeInTheDocument();
});
});
test("disconnects from repository", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, connectedStatus);
axiosMock.onPost("/api/v1/repo/disconnect").reply(200, {});
renderWithContext();
await waitFor(() => {
expect(screen.getByTestId("disconnect")).toBeInTheDocument();
});
const disconnectButton = screen.getByTestId("disconnect");
await userEvent.click(disconnectButton);
await waitFor(() => {
expect(mockContextValue.repositoryUpdated).toHaveBeenCalledWith(false);
});
});
test("displays disabled ECC when eccOverheadPercent is 0", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, {
...connectedStatus,
eccOverheadPercent: 0,
ecc: null,
});
renderWithContext();
await waitFor(() => {
expect(screen.getByDisplayValue("Disabled")).toBeInTheDocument();
expect(screen.getByDisplayValue("-")).toBeInTheDocument();
});
});
});
describe("Repository component - initializing state", () => {
test("shows initializing state with task ID", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, {
connected: false,
initTaskID: "task-123",
});
renderWithContext();
await waitFor(() => {
expect(screen.getByText("Initializing Repository...")).toBeInTheDocument();
expect(screen.getByText("Show Log")).toBeInTheDocument();
});
});
test("toggles log display during initialization", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, {
connected: false,
initTaskID: "task-123",
});
renderWithContext();
await waitFor(() => {
expect(screen.getByText("Show Log")).toBeInTheDocument();
});
// Click to show log
const showLogButton = screen.getByText("Show Log");
await userEvent.click(showLogButton);
expect(screen.getByText("Hide Log")).toBeInTheDocument();
// Click to hide log
const hideLogButton = screen.getByText("Hide Log");
await userEvent.click(hideLogButton);
expect(screen.getByText("Show Log")).toBeInTheDocument();
});
test("cancels connection during initialization", async () => {
// Import the mocked cancelTask
const { cancelTask } = await import("../../src/utils/taskutil");
axiosMock.onGet("/api/v1/repo/status").reply(200, {
connected: false,
initTaskID: "task-123",
});
renderWithContext();
await waitFor(() => {
expect(screen.getByText("Cancel Connection")).toBeInTheDocument();
});
const cancelButton = screen.getByText("Cancel Connection");
await userEvent.click(cancelButton);
// Verify cancelTask was called with correct task ID
expect(cancelTask).toHaveBeenCalledWith("task-123");
});
test("polls for status during initialization", async () => {
let callCount = 0;
axiosMock.onGet("/api/v1/repo/status").reply(() => {
callCount++;
if (callCount === 1) {
return [200, { connected: false, initTaskID: "task-123" }];
} else {
return [200, { connected: true, description: "Connected!", ...connectedStatus }];
}
});
renderWithContext();
await waitFor(() => {
expect(screen.getByText("Initializing Repository...")).toBeInTheDocument();
});
await waitFor(
() => {
expect(screen.getByText("Connected To Repository")).toBeInTheDocument();
},
{ timeout: 2000 },
);
});
});
describe("Repository component - disconnected state", () => {
test("shows SetupRepository component when not connected", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, {
connected: false,
initTaskID: null,
});
renderWithContext();
await waitFor(() => {
// SetupRepository component should be rendered
// We're not testing its internals, just that it's rendered
expect(screen.queryByText("Connected To Repository")).not.toBeInTheDocument();
expect(screen.queryByText("Initializing Repository...")).not.toBeInTheDocument();
});
});
});
describe("Repository component - CLI equivalent", () => {
test("displays CLI equivalent command", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, connectedStatus);
renderWithContext();
await waitFor(() => {
// Look for the terminal button that CLIEquivalent renders
expect(screen.getByTestId("show-cli-button")).toBeInTheDocument();
});
// Click the terminal button to show the CLI command
const terminalButton = screen.getByTestId("show-cli-button");
await userEvent.click(terminalButton);
// Should show the actual CLI command with kopia executable
await waitFor(() => {
const input = screen.getByDisplayValue("kopia repository status");
expect(input).toBeInTheDocument();
});
});
});
describe("Repository component - throttle settings", () => {
test("displays throttle settings when connected directly", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, connectedStatus);
axiosMock.onGet("/api/v1/repo/throttle").reply(200, {
maxUploadSpeedBytesPerSecond: 0,
maxDownloadSpeedBytesPerSecond: 0,
});
renderWithContext();
await waitFor(() => {
expect(screen.getByText("Upload/Download Speed Limits")).toBeInTheDocument();
const uploadInput = document.querySelector('input[name="throttle.maxUploadSpeedBytesPerSecond"]');
const downloadInput = document.querySelector('input[name="throttle.maxDownloadSpeedBytesPerSecond"]');
expect(uploadInput).toBeInTheDocument();
expect(downloadInput).toBeInTheDocument();
});
});
test("hides throttle settings when connected via API server", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, {
...connectedStatus,
apiServerURL: "http://localhost:51515",
});
renderWithContext();
await waitFor(() => {
expect(screen.queryByText("Upload/Download Speed Limits")).not.toBeInTheDocument();
});
});
test("loads existing throttle settings", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, connectedStatus);
axiosMock.onGet("/api/v1/repo/throttle").reply(200, {
maxUploadSpeedBytesPerSecond: 1048576, // 1M
maxDownloadSpeedBytesPerSecond: 2097152, // 2M
});
renderWithContext();
await waitFor(() => {
const uploadInput = document.querySelector('input[name="throttle.maxUploadSpeedBytesPerSecond"]');
const downloadInput = document.querySelector('input[name="throttle.maxDownloadSpeedBytesPerSecond"]');
expect(uploadInput).toHaveValue("1M");
expect(downloadInput).toHaveValue("2M");
});
});
test("updates throttle settings with various formats", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, connectedStatus);
axiosMock.onGet("/api/v1/repo/throttle").reply(200, {
maxUploadSpeedBytesPerSecond: 0,
maxDownloadSpeedBytesPerSecond: 0,
});
axiosMock.onPut("/api/v1/repo/throttle").reply(200, {});
renderWithContext();
await waitFor(() => {
expect(screen.getByText("Upload/Download Speed Limits")).toBeInTheDocument();
});
const uploadInput = document.querySelector('input[name="throttle.maxUploadSpeedBytesPerSecond"]');
const downloadInput = document.querySelector('input[name="throttle.maxDownloadSpeedBytesPerSecond"]');
const saveButton = screen.getByText("Save Settings");
await userEvent.clear(uploadInput);
await userEvent.type(uploadInput, "100K");
await userEvent.clear(downloadInput);
await userEvent.type(downloadInput, "2G");
await userEvent.click(saveButton);
await waitFor(() => {
expect(axiosMock.history.put[0].data).toBe(
JSON.stringify({
maxUploadSpeedBytesPerSecond: 102400,
maxDownloadSpeedBytesPerSecond: 2147483648,
}),
);
});
});
test("allows empty values for unlimited speed", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, connectedStatus);
axiosMock.onGet("/api/v1/repo/throttle").reply(200, {
maxUploadSpeedBytesPerSecond: 1048576,
maxDownloadSpeedBytesPerSecond: 2097152,
});
axiosMock.onPut("/api/v1/repo/throttle").reply(200, {});
renderWithContext();
await waitFor(() => {
const uploadInput = document.querySelector('input[name="throttle.maxUploadSpeedBytesPerSecond"]');
expect(uploadInput).toHaveValue("1M");
});
const uploadInput = document.querySelector('input[name="throttle.maxUploadSpeedBytesPerSecond"]');
const saveButton = screen.getByText("Save Settings");
await userEvent.clear(uploadInput);
await userEvent.click(saveButton);
await waitFor(() => {
expect(axiosMock.history.put[0].data).toBe(
JSON.stringify({
maxUploadSpeedBytesPerSecond: 0,
maxDownloadSpeedBytesPerSecond: 2097152,
}),
);
});
});
test("handles update errors", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, connectedStatus);
axiosMock.onGet("/api/v1/repo/throttle").reply(200, {
maxUploadSpeedBytesPerSecond: 0,
maxDownloadSpeedBytesPerSecond: 0,
});
axiosMock.onPut("/api/v1/repo/throttle").reply(500, { error: "Failed" });
const alertMock = vi.spyOn(window, "alert").mockImplementation(() => {});
renderWithContext();
await waitFor(() => {
expect(screen.getByText("Upload/Download Speed Limits")).toBeInTheDocument();
});
const uploadInput = document.querySelector('input[name="throttle.maxUploadSpeedBytesPerSecond"]');
const saveButton = screen.getByText("Save Settings");
await userEvent.type(uploadInput, "5M");
await userEvent.click(saveButton);
await waitFor(() => {
expect(alertMock).toHaveBeenCalledWith(expect.stringContaining("Error updating throttle settings"));
});
alertMock.mockRestore();
});
test("handles throttle fetch errors gracefully", async () => {
axiosMock.onGet("/api/v1/repo/status").reply(200, connectedStatus);
axiosMock.onGet("/api/v1/repo/throttle").reply(500, { error: "Failed" });
const consoleLogMock = vi.spyOn(console, "log").mockImplementation(() => {});
renderWithContext();
await waitFor(() => {
expect(screen.getByText("Upload/Download Speed Limits")).toBeInTheDocument();
expect(consoleLogMock).toHaveBeenCalledWith("Unable to fetch throttle settings:", expect.any(Error));
});
consoleLogMock.mockRestore();
});
});