|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Steven Roussey <sroussey@gmail.com> |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, expect, it } from "vitest"; |
| 8 | +import { applyDomainOperators } from "../queryOperators"; |
| 9 | +import { trimTrailingSlashes } from "../urlText"; |
| 10 | + |
| 11 | +describe("trimTrailingSlashes", () => { |
| 12 | + it("removes one or many trailing slashes", () => { |
| 13 | + expect(trimTrailingSlashes("https://a.example/")).toBe("https://a.example"); |
| 14 | + expect(trimTrailingSlashes("https://a.example///")).toBe("https://a.example"); |
| 15 | + }); |
| 16 | + |
| 17 | + it("leaves a string with no trailing slash untouched", () => { |
| 18 | + expect(trimTrailingSlashes("https://a.example")).toBe("https://a.example"); |
| 19 | + }); |
| 20 | + |
| 21 | + it("keeps interior slashes", () => { |
| 22 | + expect(trimTrailingSlashes("https://a.example/b/c/")).toBe("https://a.example/b/c"); |
| 23 | + }); |
| 24 | + |
| 25 | + it("handles empty and all-slash input", () => { |
| 26 | + expect(trimTrailingSlashes("")).toBe(""); |
| 27 | + expect(trimTrailingSlashes("////")).toBe(""); |
| 28 | + }); |
| 29 | + |
| 30 | + /** |
| 31 | + * A long run of slashes that is NOT at the end is the shape that makes |
| 32 | + * `/\/+$/` quadratic. Under that pattern this input takes tens of seconds; |
| 33 | + * the linear scan is sub-millisecond, so the budget is generous enough that |
| 34 | + * only a genuine reintroduction of the regex can trip it. |
| 35 | + */ |
| 36 | + it("stays linear on a pathological slash run", () => { |
| 37 | + const evil = `a${"/".repeat(200_000)}b`; |
| 38 | + const started = performance.now(); |
| 39 | + expect(trimTrailingSlashes(evil)).toBe(evil); |
| 40 | + expect(performance.now() - started).toBeLessThan(1000); |
| 41 | + }); |
| 42 | + |
| 43 | + it("keeps domain normalization linear on the same input", () => { |
| 44 | + const evil = `a${"/".repeat(200_000)}b`; |
| 45 | + const started = performance.now(); |
| 46 | + applyDomainOperators("cats", [evil], undefined); |
| 47 | + expect(performance.now() - started).toBeLessThan(1000); |
| 48 | + }); |
| 49 | +}); |
0 commit comments