package com.lanyuanxiaoyao.digtal.market

import cn.hutool.json.JSONUtil
import com.lanyuanxiaoyao.digtal.market.runner.NewsRunner
import com.lanyuanxiaoyao.squirrel.core.common.Management
import jakarta.annotation.Resource
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.slf4j.LoggerFactory
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest(
    classes = [Application::class],
    webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
)
class TestManagement {
    private val logger = LoggerFactory.getLogger(javaClass)

    @Resource
    private lateinit var management: Management

    @Resource
    private lateinit var newsRunner: NewsRunner

    @Test
    fun testParse() {
        newsRunner
            .parseArticleLink(
                "9a7f1d8f-4f39-4120-adeb-7435339b97bb",
                "https://www.cantonde.com/si/common/searchInfo?json=%7B%22NAME%22%3A%22数据要素%22%2C%22IN_CATEGORY%22%3A%22%22%2C%22NOT_IN_CATEGORY%22%3A%22%22%2C%22CATEGORY%22%3A%22%22%2C%22pageNo%22%3A1%2C%22pageSize%22%3A10%7D",
            )
            .forEach { link ->
                val article = newsRunner.parseArticle(
                    "9a7f1d8f-4f39-4120-adeb-7435339b97bb",
                    link.url,
                    link.title,
                    link.datetime,
                    "",
                )
                logger.info("{} {} {} {}", article?.title, article?.createTime, article?.author, article?.text)
            }
    }

    @Test
    fun testDownload() {
        val page = runBlocking {
            management.download(
                "74ee6b33-c1a3-41f9-b947-acd0bebd0e6e",
                "https://zfsg.gd.gov.cn/gkmlpt/content/4/4514/post_4514242.html#2589",
            )
        }
        logger.info("Page: {}", page)
    }

    @Test
    fun testParseList() {
        newsRunner
            .parseArticleLink(
                "74ee6b33-c1a3-41f9-b947-acd0bebd0e6e",
                "https://search.gd.gov.cn/api/search/all?json=%7B%22page%22%3A1%2C%22keywords%22%3A%22数据要素%22%2C%22advance%22%3A%22true%22%2C%22sort%22%3A%22time%22%2C%22position%22%3A%22all%22%2C%22time_to%22%3A2524579200%2C%22time_from%22%3A189273600%2C%22site_id%22%3A%22246%22%2C%22range%22%3A%22site%22%2C%22recommand%22%3A1%7D",
                true,
            )
            .let { logger.info("{}", JSONUtil.toJsonPrettyStr(it)) }
    }

    @Test
    fun testParseArticle() {
        newsRunner
            .parseArticle(
                "74ee6b33-c1a3-41f9-b947-acd0bebd0e6e",
                "https://zfsg.gd.gov.cn/gkmlpt/content/4/4514/post_4514242.html#2589",
                "no title",
                "no datetime",
                "",
            )
            ?.let { logger.info("{}", JSONUtil.toJsonPrettyStr(it)) }
    }
}