package bolone.test

import alraune.*
import alraune.entity.*
import bolone.BoFrontFuns
import bolone.TextField
import bolone.rp.FileField
import bolone.rp.new_FileField_Value
import bolone.rp.new_OrderFileFields
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import pieces100.*
import vgrechka.*

class Test_jsonizeForCallingFront {
    @Before fun setUp() {
        initStandaloneToolMode__localMemoryDBConfig()
    }

    @Test fun test1() {
        fart(shouldContainSecrets = false)
        fart(shouldContainSecrets = true) {
            AlTestPile.pretendAdmin(it)}
    }

    private fun fart(shouldContainSecrets: Boolean, fuckContext: (Context1ProjectStuff) -> Unit = {}) {
        withNewContext1 {
            rctx0.al.useConnection {
                val shit = BoFrontFuns.InitOrderFilesPage(
                    editable = true,
                    orderHandle = OrderHandle(id = 123L, lockId = 234L, expectedOptimisticVersion = "adfadsf").toJsonizable(),
                    bucket = "fsadfasdf",
                    tabsTopRightContainerDomid = "sadfadsf",
                    reloadURL = "dfasdfasdf",
                    reloadToFirstPageURL = "dfsdkfjskdfjk",
                    storikanURL = "zxcvczxv",
                    userKind = AlUserKind.Customer,
                    fileItems = listOf(
                        BoFrontFuns.InitOrderFilesPage.FileItem(
                            id = 123L,
                            file = new_Order_File(
                                id = 234L, createdAt = now(), updatedAt = now(), uuid = "27ec5886-cf26-4f26-bb98-f950979958e4",
                                state = Order.File.State.Unknown, title = "qweqweqwe", details = "asdfasdfasdfasdf",

                                // Should not be included
                                adminNotes = "Надо наебать этого заказчика на деньги",

                                toBeFixed = new_Order_ToBeFixed(
                                    what = "Не были бы вы трижды любезны исправить вот это...",

                                    // Should not be included
                                    detailsForAdmin = "Дебил, бля..."),

                                resource = new_Order_Resource(
                                    name = "qweeidfgd", size = 42433, downloadUrl = "sdfhhhhvv", secretKeyBase64 = "sdfjsfdjkgsdnfgs")),
                            editTriggerDomid = "qweqwe",
                            rightIconsContainerDomid = "dsfafasdf",
                            fields = new_OrderFileFields(
                                file = FileField.noError(new_FileField_Value(
                                    name = "aaaaaaa",
                                    size = 123123,
                                    downloadURL = "faskdfasdfsaf",
                                    secretKeyBase64 = "sakioisdjfaosdf")),
                                title = TextField.noError("asdfadsfasf"),
                                details = TextField.noError("kjaksdfsadfaskdfjasdf")),
                            copiedTo = listOf())))

                val json = dyna {
                    fuckContext(it)
                    BoFrontFuns.jsonizeForCallingFront(shit, pretty = true)
                }
                clog(json)

                val file = shit.fileItems.first().file
                val secret1 = file.adminNotes!!
                val secret2 = file.toBeFixed!!.detailsForAdmin

                val containsSecrets = json.contains(secret1) || json.contains(secret2)
                Assert.assertTrue(containsSecrets == shouldContainSecrets)
            }
        }
    }
}