package com.lanyuanxiaoyao.digtal.market

import cn.hutool.json.JSONUtil
import com.lanyuanxiaoyao.squirrel.core.common.InMemoryDatabase
import com.lanyuanxiaoyao.squirrel.core.common.Management
import com.lanyuanxiaoyao.squirrel.core.jvm.BINARY_PATH
import com.lanyuanxiaoyao.squirrel.core.jvm.DRIVER_PATH
import com.lanyuanxiaoyao.squirrel.core.jvm.JvmManagement
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlinx.coroutines.runBlocking

class TestRule {
    private lateinit var management: Management

    private val link =
        "https://www.gov.cn/zhengce/202410/content_6979047.htm"
    private val site = sites.find { it.code == "00bfe1f5-7662-4ea5-ada3-6e9dfc19f0be" }!!

    @BeforeTest
    fun before() {
        management = JvmManagement(InMemoryDatabase())
        management.importSites(setOf(site))
        management.changeDownloader("basicCacheDownloader")
        management.changeDownloader(
            "chromeCacheDownloader",
            mapOf(
                BINARY_PATH to "/Users/lanyuanxiaoyao/Downloads/chromium/128/macOS-1289987/Chromium.app/Contents/MacOS/Chromium",
                DRIVER_PATH to "/Users/lanyuanxiaoyao/Downloads/chromium/128/macOS-1289987/chromedriver",
            )
        )
    }

    @Test
    fun test() {
        val page = runBlocking {
            management.parse(
                code = site.code,
                url = link,
            )
        }
        println(JSONUtil.toJsonPrettyStr(page))
    }

    @Test
    fun testDownload() {
        val page = runBlocking {
            management.download(
                code = site.code,
                url = link,
            )
        }
        println(page)
    }
}