Spaces:
Paused
Paused
File size: 12,820 Bytes
4e7644b | 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 | // @vitest-environment jsdom
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
// The vite.config.ts aliases @tauri-apps/api/core and @tauri-apps/plugin-dialog
// to src/test/tauri-mock.ts, which exports vi.fn() instances we can control directly.
import { invoke, isTauri, save } from "../test/tauri-mock";
import {
DownloadCancelledError,
downloadFileFromUrl,
} from "./downloadFileFromUrl";
import { openExternalLink } from "./openExternalLink";
describe("openExternalLink", () => {
const windowOpen = vi.fn();
const fetchMock = vi.fn();
beforeEach(() => {
invoke.mockReset();
isTauri.mockReturnValue(false);
invoke.mockResolvedValue(undefined);
save.mockReset();
fetchMock.mockReset();
vi.stubGlobal("fetch", fetchMock);
Object.defineProperty(URL, "createObjectURL", {
configurable: true,
value: vi.fn(() => "blob:download"),
});
Object.defineProperty(URL, "revokeObjectURL", {
configurable: true,
value: vi.fn(),
});
windowOpen.mockReset();
vi.spyOn(window, "open").mockImplementation(windowOpen);
delete (window as any).pywebview;
delete (window as any).__TAURI_INTERNALS__;
localStorage.clear();
(globalThis as any).VITE_API_BASE_URL = "";
(globalThis as any).TOKEN = "";
window.history.replaceState(null, "", "/");
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
it("uses the pywebview bridge for the legacy desktop app", () => {
const openExternal = vi.fn();
(window as any).pywebview = {
api: {
open_external_link: openExternal,
},
};
isTauri.mockReturnValue(true);
openExternalLink("https://github.com/agentscope-ai/QwenPaw");
expect(openExternal).toHaveBeenCalledWith(
"https://github.com/agentscope-ai/QwenPaw",
);
expect(invoke).not.toHaveBeenCalled();
expect(windowOpen).not.toHaveBeenCalled();
});
it("does not send non-HTTP links to the legacy pywebview bridge", () => {
const openExternal = vi.fn();
(window as any).pywebview = {
api: {
open_external_link: openExternal,
},
};
openExternalLink("mailto:support@example.com");
expect(openExternal).not.toHaveBeenCalled();
expect(windowOpen).toHaveBeenCalledWith(
"mailto:support@example.com",
"_blank",
"noopener,noreferrer",
);
});
it("ignores unsafe or fragment-only links", () => {
openExternalLink("javascript:alert(1)");
openExternalLink("#");
expect(invoke).not.toHaveBeenCalled();
expect(windowOpen).not.toHaveBeenCalled();
});
it("rejects ambiguous HTTP links without slashes before opening", () => {
isTauri.mockReturnValue(true);
openExternalLink("http:example.com");
expect(invoke).not.toHaveBeenCalled();
expect(windowOpen).not.toHaveBeenCalled();
});
it("uses the Tauri external link command for supported non-HTTP schemes", () => {
isTauri.mockReturnValue(true);
openExternalLink("mailto:support@example.com");
expect(invoke).toHaveBeenCalledWith("open_external_link", {
url: "mailto:support@example.com",
});
expect(windowOpen).not.toHaveBeenCalled();
});
it("uses the Tauri external link command in the Tauri desktop app", () => {
isTauri.mockReturnValue(true);
openExternalLink("https://qwenpaw.agentscope.io/docs/intro?lang=zh");
expect(invoke).toHaveBeenCalledWith("open_external_link", {
url: "https://qwenpaw.agentscope.io/docs/intro?lang=zh",
});
expect(windowOpen).not.toHaveBeenCalled();
});
it("uses injected Tauri internals when isTauri is false", () => {
(window as any).__TAURI_INTERNALS__ = {
invoke: vi.fn(),
};
openExternalLink("https://github.com/agentscope-ai/QwenPaw");
expect(invoke).toHaveBeenCalledWith("open_external_link", {
url: "https://github.com/agentscope-ai/QwenPaw",
});
expect(windowOpen).not.toHaveBeenCalled();
});
it("logs Tauri external link failures without falling back to window.open", async () => {
isTauri.mockReturnValue(true);
invoke.mockRejectedValue(new Error("permission denied"));
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
openExternalLink("https://github.com/agentscope-ai/QwenPaw");
await Promise.resolve();
await Promise.resolve();
expect(windowOpen).not.toHaveBeenCalled();
expect(warnSpy).toHaveBeenCalled();
});
it("falls back to window.open in the web console", () => {
openExternalLink("https://qwenpaw.agentscope.io/docs/intro?lang=en");
expect(windowOpen).toHaveBeenCalledWith(
"https://qwenpaw.agentscope.io/docs/intro?lang=en",
"_blank",
"noopener,noreferrer",
);
});
it("uses the Tauri command from backend-hosted Tauri consoles", () => {
(window as any).__TAURI_INTERNALS__ = {
invoke: vi.fn(),
};
window.history.replaceState(null, "", "/console/inbox");
openExternalLink("https://github.com/agentscope-ai/QwenPaw");
expect(invoke).toHaveBeenCalledWith("open_external_link", {
url: "https://github.com/agentscope-ai/QwenPaw",
});
expect(fetchMock).not.toHaveBeenCalled();
expect(windowOpen).not.toHaveBeenCalled();
});
it("uses window.open for backend-hosted browser consoles", () => {
window.history.replaceState(null, "", "/console/inbox");
openExternalLink("https://github.com/agentscope-ai/QwenPaw");
expect(fetchMock).not.toHaveBeenCalled();
expect(windowOpen).toHaveBeenCalledWith(
"https://github.com/agentscope-ai/QwenPaw",
"_blank",
"noopener,noreferrer",
);
});
it("does not add auth query parameters to generic external links", () => {
localStorage.setItem("qwenpaw_auth_token", "tok");
openExternalLink("https://evil.example/api/foo");
expect(windowOpen).toHaveBeenCalledWith(
"https://evil.example/api/foo",
"_blank",
"noopener,noreferrer",
);
});
it("resolves relative links before passing them to desktop bridges", () => {
isTauri.mockReturnValue(true);
openExternalLink("/docs/faq");
expect(invoke).toHaveBeenCalledWith("open_external_link", {
url: "http://localhost:3000/docs/faq",
});
});
it("downloads Tauri files with headers through the native backend command", async () => {
isTauri.mockReturnValue(true);
save.mockResolvedValue("C:\\Downloads\\server.zip");
localStorage.setItem("qwenpaw_auth_token", "tok");
await expect(
downloadFileFromUrl("/api/workspace/download", "workspace.zip", {
headers: { "X-Agent-Id": "agent-a" },
preferResponseFilename: true,
}),
).resolves.toBeUndefined();
expect(save).toHaveBeenCalledWith({
defaultPath: "workspace.zip",
});
expect(invoke).toHaveBeenCalledWith("download_backend_file", {
request: {
url: "http://localhost:3000/api/workspace/download",
filePath: "C:\\Downloads\\server.zip",
headers: { "X-Agent-Id": "agent-a" },
},
});
expect(save.mock.invocationCallOrder[0]).toBeLessThan(
invoke.mock.invocationCallOrder[0],
);
expect(fetchMock).not.toHaveBeenCalled();
expect(
invoke.mock.calls.some(([command]) => command === "open_external_link"),
).toBe(false);
});
it("uses the pywebview save bridge for legacy desktop downloads", async () => {
const saveFile = vi.fn().mockResolvedValue(true);
(window as any).pywebview = {
api: {
save_file: saveFile,
},
};
await expect(
downloadFileFromUrl(
"/api/backups/abc/export",
"Backup 2026-05-22 14:13.zip",
{
headers: { Authorization: "Bearer tok" },
errorMessage: "Export failed",
},
),
).resolves.toBeUndefined();
expect(saveFile).toHaveBeenCalledWith(
"http://localhost:3000/api/backups/abc/export",
"Backup 2026-05-22 14_13.zip",
{ Authorization: "Bearer tok" },
);
expect(fetchMock).not.toHaveBeenCalled();
expect(save).not.toHaveBeenCalled();
});
it("keeps legacy pywebview downloads backward-compatible without headers", async () => {
const saveFile = vi.fn().mockResolvedValue(true);
(window as any).pywebview = {
api: {
save_file: saveFile,
},
};
await expect(
downloadFileFromUrl("/api/backups/abc/export", "backup.zip"),
).resolves.toBeUndefined();
expect(saveFile).toHaveBeenCalledWith(
"http://localhost:3000/api/backups/abc/export",
"backup.zip",
);
});
it("sanitizes Tauri save dialog filenames for Windows", async () => {
isTauri.mockReturnValue(true);
save.mockResolvedValue("C:\\Downloads\\backup.zip");
await expect(
downloadFileFromUrl(
"/api/backups/abc/export",
"Backup 2026-05-22 14:13.zip",
),
).resolves.toBeUndefined();
expect(save).toHaveBeenCalledWith({
defaultPath: "Backup 2026-05-22 14_13.zip",
});
expect(invoke).toHaveBeenCalledWith(
"download_backend_file",
expect.objectContaining({
request: expect.objectContaining({
filePath: "C:\\Downloads\\backup.zip",
}),
}),
);
expect(fetchMock).not.toHaveBeenCalled();
});
it("reports Tauri download cancellation before starting the native download", async () => {
isTauri.mockReturnValue(true);
save.mockResolvedValue(null);
fetchMock.mockResolvedValue(new Response("zip"));
await expect(
downloadFileFromUrl("/api/workspace/download", "workspace.zip", {
headers: { "X-Agent-Id": "agent-a" },
}),
).rejects.toBeInstanceOf(DownloadCancelledError);
expect(fetchMock).not.toHaveBeenCalled();
expect(invoke).not.toHaveBeenCalledWith(
"download_backend_file",
expect.anything(),
);
});
it("surfaces Tauri native download failures with the caller's message", async () => {
isTauri.mockReturnValue(true);
save.mockResolvedValue("C:\\Downloads\\server.zip");
invoke.mockRejectedValue(new Error("HTTP 500"));
await expect(
downloadFileFromUrl("/api/workspace/download", "workspace.zip", {
errorMessage: "Export failed",
}),
).rejects.toThrow("Export failed");
expect(fetchMock).not.toHaveBeenCalled();
});
it("does not add auth query parameters to external API-shaped downloads", async () => {
localStorage.setItem("qwenpaw_auth_token", "tok");
fetchMock.mockResolvedValue(new Response("zip"));
await expect(
downloadFileFromUrl("https://evil.example/api/export", "backup.zip", {
headers: { "X-Agent-Id": "agent-a" },
}),
).resolves.toBeUndefined();
expect(fetchMock).toHaveBeenCalledWith("https://evil.example/api/export", {
headers: { "X-Agent-Id": "agent-a" },
});
});
it("rejects non-HTTP URLs before selecting a download runtime", async () => {
isTauri.mockReturnValue(true);
save.mockResolvedValue("C:\\Downloads\\mail.zip");
await expect(
downloadFileFromUrl("mailto:support@example.com", "mail.zip"),
).rejects.toThrow("Download URL is invalid");
expect(save).not.toHaveBeenCalled();
expect(invoke).not.toHaveBeenCalledWith(
"download_backend_file",
expect.anything(),
);
expect(fetchMock).not.toHaveBeenCalled();
});
it("rejects ambiguous HTTP downloads without slashes", async () => {
await expect(
downloadFileFromUrl("http:example.com/export.zip", "backup.zip"),
).rejects.toThrow("Download URL is invalid");
expect(fetchMock).not.toHaveBeenCalled();
expect(invoke).not.toHaveBeenCalledWith(
"download_backend_file",
expect.anything(),
);
});
it("uses browser downloads outside Tauri", async () => {
fetchMock.mockResolvedValue(
new Response("zip", {
headers: {
"Content-Disposition": "attachment; filename*=UTF-8''server.zip",
},
}),
);
const click = vi.fn();
const createElement = vi.spyOn(document, "createElement");
createElement.mockImplementation((tagName: string) => {
const element = document.createElementNS(
"http://www.w3.org/1999/xhtml",
tagName,
) as HTMLElement;
if (tagName === "a") {
element.click = click;
}
return element;
});
await expect(
downloadFileFromUrl("/api/backups/abc/export", "backup.zip", {
preferResponseFilename: true,
}),
).resolves.toBeUndefined();
expect(click).toHaveBeenCalled();
expect(URL.revokeObjectURL).not.toHaveBeenCalledWith("blob:download");
await new Promise((resolve) => setTimeout(resolve, 0));
expect(URL.revokeObjectURL).toHaveBeenCalledWith("blob:download");
});
});
|