|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { isValidUri } from "../../src/lib/url-utils.js"; |
| 3 | + |
| 4 | +describe("isValidUri", () => { |
| 5 | + describe("valid URIs", () => { |
| 6 | + it("should accept https URLs", () => { |
| 7 | + expect(isValidUri("https://example.com")).toBe(true); |
| 8 | + expect(isValidUri("https://example.com/path/to/resource")).toBe(true); |
| 9 | + expect(isValidUri("https://example.com/report.pdf")).toBe(true); |
| 10 | + expect(isValidUri("https://example.com:8080/path?q=1&r=2#frag")).toBe(true); |
| 11 | + }); |
| 12 | + |
| 13 | + it("should accept http URLs", () => { |
| 14 | + expect(isValidUri("http://example.com")).toBe(true); |
| 15 | + expect(isValidUri("http://localhost:3000")).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + it("should accept AT Protocol URIs", () => { |
| 19 | + expect(isValidUri("at://did:plc:abc123/org.hypercerts.claim.activity/rkey")).toBe(true); |
| 20 | + expect(isValidUri("at://did:plc:test/org.hypercerts.claim.record/def456")).toBe(true); |
| 21 | + expect(isValidUri("at://did:web:example.com/app.bsky.feed.post/3km2vj4kfqp2a")).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + it("should accept ftp URIs", () => { |
| 25 | + expect(isValidUri("ftp://files.example.com/doc.txt")).toBe(true); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should accept ipfs URIs", () => { |
| 29 | + expect(isValidUri("ipfs://QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG")).toBe(true); |
| 30 | + expect(isValidUri("ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi")).toBe(true); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should accept data URIs", () => { |
| 34 | + expect(isValidUri("data:text/plain;base64,SGVsbG8=")).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should accept mailto URIs", () => { |
| 38 | + expect(isValidUri("mailto:user@example.com")).toBe(true); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe("invalid URIs", () => { |
| 43 | + it("should reject plain text", () => { |
| 44 | + expect(isValidUri("not-a-uri")).toBe(false); |
| 45 | + expect(isValidUri("just some text")).toBe(false); |
| 46 | + expect(isValidUri("hello world")).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should reject empty strings", () => { |
| 50 | + expect(isValidUri("")).toBe(false); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should reject bare hostnames without scheme", () => { |
| 54 | + expect(isValidUri("example.com")).toBe(false); |
| 55 | + expect(isValidUri("www.example.com")).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should reject relative paths", () => { |
| 59 | + expect(isValidUri("/relative/path")).toBe(false); |
| 60 | + expect(isValidUri("./relative/path")).toBe(false); |
| 61 | + expect(isValidUri("../parent/path")).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should reject strings with only a colon", () => { |
| 65 | + expect(isValidUri(":not-valid")).toBe(false); |
| 66 | + expect(isValidUri("://missing-scheme")).toBe(false); |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments