|
| 1 | +import { extract, generate, transform } from "./wsrv.ts"; |
| 2 | +import { assertEqualIgnoringQueryOrder } from "../test-utils.ts"; |
| 3 | +import { assertEquals } from "jsr:@std/assert"; |
| 4 | + |
| 5 | +const img = "https://example.com/image.jpg"; |
| 6 | + |
| 7 | +Deno.test("wsrv extract", async (t) => { |
| 8 | + await t.step("should parse a basic wsrv URL", () => { |
| 9 | + const { operations, src } = extract( |
| 10 | + "https://wsrv.nl/?url=example.com/image.jpg", |
| 11 | + ) ?? {}; |
| 12 | + assertEquals(src, "https://example.com/image.jpg"); |
| 13 | + assertEquals(operations, {}); |
| 14 | + }); |
| 15 | + |
| 16 | + await t.step("should parse operations from URL", () => { |
| 17 | + const { operations, src } = extract( |
| 18 | + "https://wsrv.nl/?url=example.com/image.jpg&w=300&h=200&q=85&output=webp&fit=cover", |
| 19 | + ) ?? {}; |
| 20 | + assertEquals(src, "https://example.com/image.jpg"); |
| 21 | + assertEquals(operations?.width, 300); |
| 22 | + assertEquals(operations?.height, 200); |
| 23 | + assertEquals(operations?.quality, 85); |
| 24 | + assertEquals(operations?.format, "webp"); |
| 25 | + assertEquals(operations?.fit, "cover"); |
| 26 | + }); |
| 27 | + |
| 28 | + await t.step("should return null for non-wsrv URLs", () => { |
| 29 | + const result = extract("https://example.com/image.jpg"); |
| 30 | + assertEquals(result, null); |
| 31 | + }); |
| 32 | +}); |
| 33 | + |
| 34 | +Deno.test("wsrv generate", async (t) => { |
| 35 | + await t.step("should format a URL with width and height", () => { |
| 36 | + const result = generate(img, { |
| 37 | + width: 300, |
| 38 | + height: 200, |
| 39 | + }); |
| 40 | + assertEqualIgnoringQueryOrder( |
| 41 | + result, |
| 42 | + "https://wsrv.nl/?url=example.com/image.jpg&w=300&h=200&fit=cover", |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + await t.step("should not set height if not provided", () => { |
| 47 | + const result = generate(img, { width: 300 }); |
| 48 | + assertEqualIgnoringQueryOrder( |
| 49 | + result, |
| 50 | + "https://wsrv.nl/?url=example.com/image.jpg&w=300&fit=cover", |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + await t.step("should format a URL with quality", () => { |
| 55 | + const result = generate(img, { width: 600, quality: 80 }); |
| 56 | + assertEqualIgnoringQueryOrder( |
| 57 | + result, |
| 58 | + "https://wsrv.nl/?url=example.com/image.jpg&w=600&q=80&fit=cover", |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + await t.step("should format a URL with format conversion", () => { |
| 63 | + const result = generate(img, { width: 400, format: "webp" }); |
| 64 | + assertEqualIgnoringQueryOrder( |
| 65 | + result, |
| 66 | + "https://wsrv.nl/?url=example.com/image.jpg&w=400&output=webp&fit=cover", |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + await t.step("should apply default fit=cover", () => { |
| 71 | + const result = generate(img, { width: 300 }); |
| 72 | + assertEqualIgnoringQueryOrder( |
| 73 | + result, |
| 74 | + "https://wsrv.nl/?url=example.com/image.jpg&w=300&fit=cover", |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + await t.step("should allow overriding fit parameter", () => { |
| 79 | + const result = generate(img, { width: 300, fit: "contain" }); |
| 80 | + assertEqualIgnoringQueryOrder( |
| 81 | + result, |
| 82 | + "https://wsrv.nl/?url=example.com/image.jpg&w=300&fit=contain", |
| 83 | + ); |
| 84 | + }); |
| 85 | + |
| 86 | + await t.step("should format a URL with provider-specific operations", () => { |
| 87 | + const result = generate(img, { width: 300, dpr: 2, blur: 5 }); |
| 88 | + assertEqualIgnoringQueryOrder( |
| 89 | + result, |
| 90 | + "https://wsrv.nl/?url=example.com/image.jpg&w=300&dpr=2&blur=5&fit=cover", |
| 91 | + ); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +Deno.test("wsrv transform", async (t) => { |
| 96 | + await t.step("should format a URL with width and height", () => { |
| 97 | + const result = transform(img, { |
| 98 | + width: 200, |
| 99 | + height: 100, |
| 100 | + }); |
| 101 | + assertEqualIgnoringQueryOrder( |
| 102 | + result, |
| 103 | + "https://wsrv.nl/?url=example.com/image.jpg&w=200&h=100&fit=cover", |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + await t.step("should not set height if not provided", () => { |
| 108 | + const result = transform(img, { width: 200 }); |
| 109 | + assertEqualIgnoringQueryOrder( |
| 110 | + result, |
| 111 | + "https://wsrv.nl/?url=example.com/image.jpg&w=200&fit=cover", |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + await t.step("should apply defaults to URL", () => { |
| 116 | + const result = transform( |
| 117 | + "https://wsrv.nl/?url=example.com/image.jpg&w=300", |
| 118 | + {}, |
| 119 | + ); |
| 120 | + assertEqualIgnoringQueryOrder( |
| 121 | + result, |
| 122 | + "https://wsrv.nl/?url=example.com/image.jpg&w=300&fit=cover", |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + await t.step("should override existing parameters", () => { |
| 127 | + const result = transform( |
| 128 | + "https://wsrv.nl/?url=example.com/image.jpg&w=300&fit=inside", |
| 129 | + { fit: "contain" }, |
| 130 | + ); |
| 131 | + assertEqualIgnoringQueryOrder( |
| 132 | + result, |
| 133 | + "https://wsrv.nl/?url=example.com/image.jpg&w=300&fit=contain", |
| 134 | + ); |
| 135 | + }); |
| 136 | +}); |
0 commit comments