/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package com.runicgateway.app.core.push

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test

/**
 * Tests for the app's own ntfy topic + endpoint URL building (PLAN.md §11, work
 * item 1) — the heart of the embedded-distributor design.
 */
class NtfyTopicTest {

    @Test fun generatesUnguessableTopicsInTheAllowedCharset() {
        val a = NtfyTopic.generate()
        val b = NtfyTopic.generate()
        assertNotEquals(a, b)
        assertTrue("prefixed", a.startsWith("up"))
        assertTrue("length", a.length >= 24)
        assertTrue("charset", a.all { it.isLetterOrDigit() })
    }

    @Test fun buildsEndpointAndSseUrls() {
        assertEquals("https://ntfy.tld/up7", NtfyTopic.endpointUrl("https://ntfy.tld", "up7"))
        assertEquals("https://ntfy.tld/up7/sse", NtfyTopic.sseUrl("https://ntfy.tld", "up7"))
    }

    @Test fun toleratesTrailingSlashOnBase() {
        assertEquals("https://ntfy.tld/up7", NtfyTopic.endpointUrl("https://ntfy.tld/", "up7"))
    }

    @Test fun nullOrBlankInputsYieldNull() {
        assertNull(NtfyTopic.endpointUrl(null, "up7"))
        assertNull(NtfyTopic.endpointUrl("", "up7"))
        assertNull(NtfyTopic.endpointUrl("https://ntfy.tld", " "))
        assertNull(NtfyTopic.sseUrl(null, "up7"))
    }
}
