import { describe, it, expect } from "vitest"; import { KcSanitizer } from "keycloakify/tools/kcSanitize/KcSanitizer"; // Adjust the import path as needed // Implementation of Keycloak Java method KeycloakSanitizerTest with bunch of more test for p tag styling // https://github.com/keycloak/keycloak/blob/8ce8a4ba089eef25a0e01f58e09890399477b9ef/services/src/test/java/org/keycloak/theme/KeycloakSanitizerTest.java#L32 describe("KeycloakSanitizerMethod", () => { it("should handle escapes correctly", () => { let html: string | null = ""; let expectedResult: string | null; html = "
Keycloak
"; expectedResult = '
Keycloak
'; assertResult(expectedResult, html); html = "

Foo

"; expectedResult = "

Foo

"; assertResult(expectedResult, html); html = '
Keycloak
'; expectedResult = '
Keycloak
'; assertResult(expectedResult, html); html = null; // Type assertion to handle null expectedResult = null; expect(() => assertResult(expectedResult, html)).toThrow( "Cannot escape null value." ); html = ""; expectedResult = ""; assertResult(expectedResult, html); }); it("should handle URLs correctly", () => { let html: string = ""; html = "

link

"; assertResult('

link

', html); html = '

link

'; assertResult("

link

", html); html = "

link

"; assertResult("

link

", html); html = '

link

'; assertResult("

link

", html); html = '

link

'; assertResult("

link

", html); html = '

link

'; assertResult("

link

", html); html = '

link

'; assertResult("

link

", html); html = '

link

'; assertResult("

link

", html); html = '

link

'; assertResult( '

link

', html ); html = "

link1link2

"; assertResult( '

link1link2

', html ); }); it("should handle text styles correctly", () => { let html: string = ""; html = "

text

"; assertResult("

text

", html); html = "

text

"; assertResult("

text

", html); html = `

red text

`; assertResult(`

red text

`, html); html = `

red text

`; assertResult(`

red text

`, html); html = `

This is a paragraph with larger text.

`; assertResult( `

This is a paragraph with larger text.

`, html ); html = `

או נושא שתבחר

`; assertResult(`

או נושא שתבחר

`, html); }); function assertResult(expectedResult: string | null, html: string | null): void { if (expectedResult === null) { expect(KcSanitizer.sanitize(html)).toThrow("Cannot escape null value."); } else { const result = KcSanitizer.sanitize(html); console.log("expectedResult is ", expectedResult); console.log("Result is ", result); expect(result).toBe(expectedResult); } } });