-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathImportSettings.test.tsx
More file actions
254 lines (218 loc) · 8.76 KB
/
Copy pathImportSettings.test.tsx
File metadata and controls
254 lines (218 loc) · 8.76 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
/** @vitest-environment jsdom */
import React from "react";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import "@testing-library/jest-dom";
import { QueryClientProvider } from "@tanstack/react-query";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createTestQueryClient, getRequestUrl } from "./test-utils";
import ImportSettings from "../src/components/ImportSettings";
import type { ImportConfig } from "@shared/schema";
const mockToast = vi.fn();
vi.mock("@/hooks/use-toast", () => ({
useToast: () => ({ toast: mockToast }),
}));
vi.mock("@/lib/queryClient", async (importOriginal) => {
const actual = await importOriginal<Record<string, unknown>>();
return {
...actual,
apiRequest: vi.fn(async () => ({ json: async () => ({}) })),
};
});
const baseConfig: ImportConfig = {
enablePostProcessing: true,
autoUnpack: true,
overwriteExisting: false,
libraryRoot: "/data/library",
transferMode: "hardlink",
autoDeleteAfterImport: false,
sortExtras: false,
importPlatformIds: [],
renamePattern: "{Title} ({Year})",
} as unknown as ImportConfig;
function createJsonResponse(data: unknown): Response {
return { ok: true, json: async () => data } as Response;
}
function mockFetch({
config = baseConfig,
platforms = [
{ id: 1, name: "PC (Microsoft Windows)" },
{ id: 2, name: "PlayStation 5" },
],
appConfig = { igdb: { configured: true } },
hardlink = {
generic: { targetRoot: "/data/library", supportedForAll: true, checkedSources: [] },
},
}: {
config?: ImportConfig | undefined;
platforms?: unknown[];
appConfig?: unknown;
hardlink?: unknown;
} = {}) {
vi.spyOn(globalThis, "fetch").mockImplementation(async (url: RequestInfo | URL) => {
const u = getRequestUrl(url);
if (u.includes("/api/imports/config")) return createJsonResponse(config);
if (u.includes("/api/igdb/platforms")) return createJsonResponse(platforms);
if (u.includes("/api/imports/hardlink/check")) return createJsonResponse(hardlink);
if (u.includes("/api/config")) return createJsonResponse(appConfig);
return createJsonResponse({});
});
}
function renderComponent() {
return render(
<QueryClientProvider client={createTestQueryClient()}>
<ImportSettings />
</QueryClientProvider>
);
}
describe("ImportSettings", () => {
beforeEach(() => {
vi.clearAllMocks();
mockFetch();
});
afterEach(() => {
vi.restoreAllMocks();
});
it("renders the general config tab with loaded settings", async () => {
renderComponent();
expect(await screen.findByText("Enable Post-Processing")).toBeInTheDocument();
expect(screen.getByDisplayValue("/data/library")).toBeInTheDocument();
expect(screen.getByText("Hardlink supported.")).toBeInTheDocument();
});
it("toggles auto-unpack and saves changes via the mutation", async () => {
const { apiRequest } = await import("@/lib/queryClient");
renderComponent();
await screen.findByText("Enable Post-Processing");
const switches = screen.getAllByRole("switch");
fireEvent.click(switches[1]);
fireEvent.click(screen.getByRole("button", { name: /save changes/i }));
await waitFor(() => {
expect(apiRequest).toHaveBeenCalledWith(
"PATCH",
"/api/imports/config",
expect.objectContaining({ autoUnpack: false })
);
});
});
it("allows category subfolders to be enabled", async () => {
const { apiRequest } = await import("@/lib/queryClient");
renderComponent();
await screen.findByText("Sort add-on files into subfolders");
fireEvent.click(screen.getAllByRole("switch")[4]);
fireEvent.click(screen.getByRole("button", { name: /save changes/i }));
await waitFor(() => {
expect(apiRequest).toHaveBeenCalledWith(
"PATCH",
"/api/imports/config",
expect.objectContaining({ sortExtras: true })
);
});
});
it("filters platforms by search text", async () => {
renderComponent();
await screen.findByText("PC (Microsoft Windows)");
fireEvent.change(screen.getByPlaceholderText("Search platforms..."), {
target: { value: "playstation" },
});
expect(screen.queryByText("PC (Microsoft Windows)")).not.toBeInTheDocument();
expect(screen.getByText("PlayStation 5")).toBeInTheDocument();
});
it("shows a no-match message when the search filters out all platforms", async () => {
renderComponent();
await screen.findByText("PC (Microsoft Windows)");
fireEvent.change(screen.getByPlaceholderText("Search platforms..."), {
target: { value: "nonexistent-platform" },
});
expect(screen.getByText("No platforms match your search.")).toBeInTheDocument();
});
it("toggles a platform filter checkbox", async () => {
renderComponent();
await screen.findByText("PC (Microsoft Windows)");
const checkbox = screen.getByLabelText("PC (Microsoft Windows)");
fireEvent.click(checkbox);
expect(checkbox).toBeChecked();
});
it("shows an error state and retry button when platforms fail to load", async () => {
vi.spyOn(globalThis, "fetch").mockImplementation(async (url: RequestInfo | URL) => {
const u = getRequestUrl(url);
if (u.includes("/api/igdb/platforms")) {
return { ok: false, json: async () => ({}) } as Response;
}
if (u.includes("/api/imports/config")) return createJsonResponse(baseConfig);
if (u.includes("/api/imports/hardlink/check")) {
return createJsonResponse({
generic: { targetRoot: "/data/library", supportedForAll: true, checkedSources: [] },
});
}
return createJsonResponse({});
});
renderComponent();
expect(await screen.findByText("Could not load platform list from IGDB.")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Retry" })).toBeInTheDocument();
});
it("shows an IGDB-not-configured message when there are no platforms and IGDB is unconfigured", async () => {
mockFetch({ platforms: [], appConfig: { igdb: { configured: false } } });
renderComponent();
expect(
await screen.findByText("IGDB is not configured yet — platform filters unavailable.")
).toBeInTheDocument();
});
it("switches to the help tab and renders guidance content", async () => {
renderComponent();
await screen.findByText("Enable Post-Processing");
fireEvent.mouseDown(screen.getByRole("tab", { name: "Help" }));
expect(await screen.findByText("How the import pipeline works")).toBeInTheDocument();
expect(screen.getByText("Transfer modes")).toBeInTheDocument();
});
it("switches to the path mappings tab", async () => {
vi.spyOn(globalThis, "fetch").mockImplementation(async (url: RequestInfo | URL) => {
const u = getRequestUrl(url);
if (u.includes("/api/imports/mappings/paths")) return createJsonResponse([]);
if (u.includes("/api/downloaders")) return createJsonResponse([]);
if (u.includes("/api/imports/config")) return createJsonResponse(baseConfig);
if (u.includes("/api/igdb/platforms")) return createJsonResponse([]);
if (u.includes("/api/imports/hardlink/check")) {
return createJsonResponse({
generic: { targetRoot: "/data/library", supportedForAll: true, checkedSources: [] },
});
}
return createJsonResponse({});
});
renderComponent();
await screen.findByText("Enable Post-Processing");
fireEvent.mouseDown(screen.getByRole("tab", { name: "Path Mappings" }));
expect(await screen.findByText("No mappings defined")).toBeInTheDocument();
});
it("shows an amber warning when hardlink is not supported for all sources", async () => {
mockFetch({
hardlink: {
generic: { targetRoot: "/data/library", supportedForAll: false, checkedSources: [] },
},
});
renderComponent();
expect(
await screen.findByText("Hardlink not available on this setup — will fall back to copy.")
).toBeInTheDocument();
});
it("shows an informational message when hardlink support is unknown", async () => {
mockFetch({
hardlink: {
generic: { targetRoot: "/data/library", supportedForAll: null, checkedSources: [] },
},
});
renderComponent();
expect(
await screen.findByText(
"Hardlink check unavailable: configure at least one downloader path first."
)
).toBeInTheDocument();
});
it("disables inner controls visually when post-processing is turned off", async () => {
mockFetch({ config: { ...baseConfig, enablePostProcessing: false } });
const { container } = renderComponent();
await screen.findByText("Enable Post-Processing");
expect(
screen.queryByText(/If your download client runs on a different machine/)
).not.toBeInTheDocument();
expect(container.querySelector(".pointer-events-none")).toBeInTheDocument();
});
});