package wang.yaojia.webterm.terminalview

import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

/**
 * The link allowlist (plan §6.5): only http/https URLs from attacker-influenced terminal output may
 * become an `ACTION_VIEW` intent. Everything else — `file:`, `content:`, `intent:`, `javascript:`,
 * custom app schemes, scheme-less text — is rejected so a printed payload can't exfiltrate a local file
 * or fire a deep link.
 */
class LinkPolicyTest {

    @Test
    fun `http and https are allowed, case-insensitively`() {
        assertTrue(LinkPolicy.isAllowedUrl("http://example.com"))
        assertTrue(LinkPolicy.isAllowedUrl("https://example.com/path?q=1"))
        assertTrue(LinkPolicy.isAllowedUrl("HTTPS://EXAMPLE.COM"))
    }

    @Test
    fun `non-web and scheme-less URLs are rejected`() {
        assertFalse(LinkPolicy.isAllowedUrl("file:///etc/passwd"))
        assertFalse(LinkPolicy.isAllowedUrl("content://media/external"))
        assertFalse(LinkPolicy.isAllowedUrl("intent://scan#Intent;scheme=x;end"))
        assertFalse(LinkPolicy.isAllowedUrl("javascript:alert(1)"))
        assertFalse(LinkPolicy.isAllowedUrl("myapp://open"))
        assertFalse(LinkPolicy.isAllowedUrl("example.com"))
        assertFalse(LinkPolicy.isAllowedUrl(""))
        assertFalse(LinkPolicy.isAllowedUrl(":nohost"))
    }
}
