import { TxEnvelopeLegacy } from 'ox'
import { expectTypeOf, test } from 'vp/test'

test('default', () => {
  {
    const envelope = TxEnvelopeLegacy.from({
      to: '0x0000000000000000000000000000000000000000',
      value: 69n,
    })
    expectTypeOf(envelope).toEqualTypeOf<{
      readonly to: '0x0000000000000000000000000000000000000000'
      readonly value: 69n
      readonly type: 'legacy'
    }>()
    expectTypeOf(envelope).toMatchTypeOf<TxEnvelopeLegacy.TxEnvelopeLegacy>()
  }

  {
    const envelope = TxEnvelopeLegacy.from(
      '0x123' as TxEnvelopeLegacy.Serialized,
    )
    expectTypeOf(envelope).toMatchTypeOf<TxEnvelopeLegacy.TxEnvelopeLegacy>()
  }

  {
    const envelope = TxEnvelopeLegacy.from({
      to: '0x0000000000000000000000000000000000000000',
      value: 69n,
      r: '0x0000000000000000000000000000000000000000000000000000000000000000',
      s: '0x0000000000000000000000000000000000000000000000000000000000000001',
      v: 37,
    })
    expectTypeOf(envelope).toEqualTypeOf<{
      readonly to: '0x0000000000000000000000000000000000000000'
      readonly value: 69n
      readonly r: '0x0000000000000000000000000000000000000000000000000000000000000000'
      readonly s: '0x0000000000000000000000000000000000000000000000000000000000000001'
      readonly v: 37
      readonly type: 'legacy'
    }>()
    expectTypeOf(envelope).toMatchTypeOf<TxEnvelopeLegacy.TxEnvelopeLegacy>()
  }
})

test('options: signature', () => {
  const envelope = TxEnvelopeLegacy.from(
    {
      to: '0x0000000000000000000000000000000000000000',
      value: 69n,
    },
    {
      signature: {
        r: '0x0000000000000000000000000000000000000000000000000000000000000000',
        s: '0x0000000000000000000000000000000000000000000000000000000000000001',
        yParity: 0,
      },
    },
  )
  expectTypeOf(envelope).toEqualTypeOf<{
    readonly to: '0x0000000000000000000000000000000000000000'
    readonly value: 69n
    readonly r: '0x0000000000000000000000000000000000000000000000000000000000000000'
    readonly s: '0x0000000000000000000000000000000000000000000000000000000000000001'
    readonly v: 27
    readonly yParity: 0
    readonly type: 'legacy'
  }>()
  expectTypeOf(envelope).toMatchTypeOf<TxEnvelopeLegacy.TxEnvelopeLegacy>()
})
