pdfUrl.test.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import { buildPdfUrl, buildPdfDownloadUrl } from "./pdfUrl.js";
  4. describe("lib/frontend/explorer/pdfUrl", () => {
  5. it("builds the expected PDF stream URL", () => {
  6. const url = buildPdfUrl({
  7. branch: "NL01",
  8. year: "2024",
  9. month: "10",
  10. day: "23",
  11. filename: "test.pdf",
  12. });
  13. expect(url).toBe("/api/files/NL01/2024/10/23/test.pdf");
  14. });
  15. it("encodes filenames with spaces and special characters (#, &, +, %)", () => {
  16. const filename = "Lieferschein #1 & Co +100%.pdf";
  17. const url = buildPdfUrl({
  18. branch: "NL01",
  19. year: "2024",
  20. month: "10",
  21. day: "23",
  22. filename,
  23. });
  24. expect(url).toBe(
  25. "/api/files/NL01/2024/10/23/Lieferschein%20%231%20%26%20Co%20%2B100%25.pdf"
  26. );
  27. });
  28. it("encodes unicode filenames safely", () => {
  29. const filename = "Müller Übergabe.pdf";
  30. const url = buildPdfUrl({
  31. branch: "NL01",
  32. year: "2024",
  33. month: "10",
  34. day: "23",
  35. filename,
  36. });
  37. // We intentionally compare against encodeURIComponent behavior
  38. // to avoid hand-maintaining unicode encodings.
  39. const expected = `/api/files/NL01/2024/10/23/${encodeURIComponent(
  40. filename
  41. )}`;
  42. expect(url).toBe(expected);
  43. });
  44. it("builds the download URL with ?download=1", () => {
  45. const url = buildPdfDownloadUrl({
  46. branch: "NL01",
  47. year: "2024",
  48. month: "10",
  49. day: "23",
  50. filename: "test.pdf",
  51. });
  52. expect(url).toBe("/api/files/NL01/2024/10/23/test.pdf?download=1");
  53. });
  54. it("throws for missing or empty segments", () => {
  55. expect(() =>
  56. buildPdfUrl({
  57. branch: "NL01",
  58. year: "2024",
  59. month: "10",
  60. day: "23",
  61. filename: "",
  62. })
  63. ).toThrow(/must not be empty/i);
  64. expect(() =>
  65. buildPdfUrl({
  66. branch: "",
  67. year: "2024",
  68. month: "10",
  69. day: "23",
  70. filename: "test.pdf",
  71. })
  72. ).toThrow(/branch/i);
  73. });
  74. });