package types

import (
	"encoding/json"
	"testing"

	"github.com/Conflux-Chain/go-conflux-sdk/utils"
	"github.com/stretchr/testify/assert"
)

func TestEpochTraceJson(t *testing.T) {
	v := `{"cfxTraces":[{"action":{"from":"cfxtest:aaph8hphbv84fkn3bunm051aek68aua6wy0tg08xnd","fromPocket":"balance","fromSpace":"native","to":"cfxtest:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa6f0vrcsw","toPocket":"gas_payment","toSpace":"none","value":"0x29b25a8b5c00"},"blockHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochNumber":"0x7994e22","transactionHash":"0xec4e68bbe8250ff060bfba7494ba0df88ed065b44708a2548f132b14af158c46","transactionPosition":"0x0","type":"internal_transfer_action","valid":true},{"action":{"callType":"call","from":"cfxtest:aaph8hphbv84fkn3bunm051aek68aua6wy0tg08xnd","gas":"0x5ffe","input":"0xd0e30db0","space":"native","to":"cfxtest:achs3nehae0j6ksvy1bhrffsh1rtfrw1f6w1kzv46t","value":"0x1"},"blockHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochNumber":"0x7994e22","transactionHash":"0xec4e68bbe8250ff060bfba7494ba0df88ed065b44708a2548f132b14af158c46","transactionPosition":"0x0","type":"call","valid":true},{"action":{"callType":"staticcall","from":"cfxtest:achs3nehae0j6ksvy1bhrffsh1rtfrw1f6w1kzv46t","gas":"0x2dcd","input":"0xaabbb8ca000000000000000000000000187f1d870c7da2a5790c16ab6ee02279e0401c95b281fc8c12954d22544db45de3159a39272895b169a852b314f9cc762e44c53b","space":"native","to":"cfxtest:aceju902vhx1rtf8f6h3wp1p9m8r9xj2ea400xyd8y","value":"0x0"},"blockHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochNumber":"0x7994e22","transactionHash":"0xec4e68bbe8250ff060bfba7494ba0df88ed065b44708a2548f132b14af158c46","transactionPosition":"0x0","type":"call","valid":true},{"action":{"gasLeft":"0x2aa4","outcome":"success","returnData":"0x0000000000000000000000000000000000000000000000000000000000000000"},"blockHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochNumber":"0x7994e22","transactionHash":"0xec4e68bbe8250ff060bfba7494ba0df88ed065b44708a2548f132b14af158c46","transactionPosition":"0x0","type":"call_result","valid":true},{"action":{"gasLeft":"0xdc3","outcome":"success","returnData":"0x"},"blockHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochNumber":"0x7994e22","transactionHash":"0xec4e68bbe8250ff060bfba7494ba0df88ed065b44708a2548f132b14af158c46","transactionPosition":"0x0","type":"call_result","valid":true},{"action":{"from":"cfxtest:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa6f0vrcsw","fromPocket":"gas_payment","fromSpace":"none","to":"cfxtest:aaph8hphbv84fkn3bunm051aek68aua6wy0tg08xnd","toPocket":"balance","toSpace":"native","value":"0x3344329de00"},"blockHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochHash":"0x188a4d96d95b3d43b38615a4073358b0904c2ef54e80f856ed833b10817d3a40","epochNumber":"0x7994e22","transactionHash":"0xec4e68bbe8250ff060bfba7494ba0df88ed065b44708a2548f132b14af158c46","transactionPosition":"0x0","type":"internal_transfer_action","valid":true}],"ethTraces":[{"action":{"callType":"call","from":"0xb5c6245c83cf89f2b9c56fa0482af190f3424cdf","gas":"0xfc12","input":"0x095ea7b3000000000000000000000000873789aaf553fd0b4252d0d2b72c6331c47aff2effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","to":"0x54593e02c39aeff52b166bd036797d2b1478de8d","value":"0x0"},"blockHash":"0x990cb3bbdd416c0683ac500dc13140da866159c64be9cf049c89d7e30d2decbe","blockNumber":72826080,"result":{"gasUsed":"0x55e3","output":"0x0000000000000000000000000000000000000000000000000000000000000001"},"subtraces":0,"traceAddress":[],"transactionHash":"0xfe75de43736f24a6eeea0f4aee16047e0d3fad9253ca9c761db94295c31384a8","transactionPosition":0,"type":"call","valid":true},{"action":{"callType":"call","from":"0x120793061e044761bdf125e1925253ba9f86d734","gas":"0xc146","input":"0x095ea7b300000000000000000000000013319d155e2d8f26f421214111b5592a86088878ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","to":"0x21ea4b3239912470b9200357dda507f7c24053e2","value":"0x0"},"blockHash":"0x990cb3bbdd416c0683ac500dc13140da866159c64be9cf049c89d7e30d2decbe","blockNumber":72826080,"result":{"gasUsed":"0x1b9c","output":"0x0000000000000000000000000000000000000000000000000000000000000001"},"subtraces":0,"traceAddress":[],"transactionHash":"0x4e25634696640b544c54dd99b48f47862a66022bd703ab2f9500cca578fa224e","transactionPosition":1,"type":"call","valid":true}],"mirrorAddressMap":{}}`
	var e EpochTrace
	err := json.Unmarshal([]byte(v), &e)
	assert.NoError(t, err, v)

	output, err := json.Marshal(e)
	assert.NoError(t, err, v)

	assert.Equal(t, utils.FormatJson(v), utils.FormatJson(string(output)))
}
