import { describe, expect, test } from "vitest"; import { replaceCloakedLinksInHtml } from "../reminders/utils"; describe("replaceCloakedLinksInHtml", () => { describe("basic cloaked links", () => { test("should replace cloaked link text with the actual URL", () => { const html = 'Click here'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); test("should replace cloaked link with different text", () => { const html = 'Visit our safe website'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://malicious-site.com'); }); test("should handle links with additional attributes", () => { const html = 'Click here'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); }); describe("nested HTML tags", () => { test("should handle bold text inside link", () => { const html = 'Click here'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); test("should handle strong text inside link", () => { const html = 'Click here'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); test("should handle span inside link", () => { const html = 'Click here'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); test("should handle multiple nested tags", () => { const html = 'Click here'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); test("should handle nested tags with visible URL - should remain unchanged", () => { const html = 'https://example.com'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); }); describe("already-visible URLs", () => { test("should not modify link when text matches URL exactly", () => { const html = 'https://example.com'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); test("should not modify link when text matches URL without protocol", () => { const html = 'example.com'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('example.com'); }); test("should not modify link when text matches URL with trailing slash difference", () => { const html = 'https://example.com'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); test("should handle case-insensitive URL comparison", () => { const html = 'https://example.com'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); }); describe("empty link text", () => { test("should replace empty link text with URL", () => { const html = ''; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); test("should replace whitespace-only link text with URL", () => { const html = ' '; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); }); describe("multiple links in content", () => { test("should replace all cloaked links in content", () => { const html = '
'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe( 'Visit https://site1.com and https://site2.com
' ); }); test("should handle mix of cloaked and visible links", () => { const html = 'https://visible.com and Click me
'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe( 'https://visible.com and https://hidden.com
' ); }); test("should handle multiple nested HTML links", () => { const html = ''; const result = replaceCloakedLinksInHtml(html); expect(result).toBe( '' ); }); }); describe("edge cases", () => { test("should handle links with single quotes in href", () => { const html = "Click here"; const result = replaceCloakedLinksInHtml(html); expect(result).toBe("https://example.com"); }); test("should handle multiline link content", () => { const html = ` Click here `; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com'); }); test("should preserve surrounding content", () => { const html = 'Before
LinkAfter
'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('Before
https://example.comAfter
'); }); test("should handle content with no links", () => { const html = "No links here
"; const result = replaceCloakedLinksInHtml(html); expect(result).toBe("No links here
"); }); test("should handle empty string", () => { const html = ""; const result = replaceCloakedLinksInHtml(html); expect(result).toBe(""); }); test("should escape HTML special characters in href to prevent XSS", () => { // Test case for potential XSS vector where href contains HTML special characters const html = 'Click here'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe('https://example.com?foo=1&bar=2'); }); test("should escape angle brackets in href", () => { const html = 'Click here'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe( 'https://example.com/<script>alert(1)</script>' ); }); test("should escape quotes in href", () => { const html = 'Click here'; const result = replaceCloakedLinksInHtml(html); expect(result).toBe( 'https://example.com?q="test"' ); }); }); });