Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cardinal): allow user to generate and keep track of custom sigs for testing. #378

Merged
merged 11 commits into from
Nov 2, 2023
36 changes: 19 additions & 17 deletions cardinal/testutils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ var (
privateKey *ecdsa.PrivateKey
)

func uniqueSignature() *sign.SignedPayload {
func UniqueSignatureWithName(name string) *sign.SignedPayload {
if privateKey == nil {
var err error
privateKey, err = crypto.GenerateKey()
Expand All @@ -136,14 +136,22 @@ func uniqueSignature() *sign.SignedPayload {
nonce++
// We only verify signatures when hitting the HTTP server, and in tests we're likely just adding transactions
// directly to the World queue. It's OK if the signature does not match the payload.
sig, err := sign.NewSignedPayload(privateKey, "some-persona-tag", "namespace", nonce, `{"some":"data"}`)
sig, err := sign.NewSignedPayload(privateKey, name, "namespace", nonce, `{"some":"data"}`)
if err != nil {
panic(err)
}
return sig
}

func AddTransactionToWorldByAnyTransaction(world *cardinal.World, cardinalTx cardinal.AnyTransaction, value any) {
func UniqueSignature() *sign.SignedPayload {
return UniqueSignatureWithName("some-persona-tag")
}

func AddTransactionToWorldByAnyTransaction(
world *cardinal.World,
cardinalTx cardinal.AnyTransaction,
value any,
signedPayload *sign.SignedPayload) {
worldCtx := WorldToWorldContext(world)
ecsWorld := cardinal.TestingWorldContextToECSWorld(worldCtx)

Expand All @@ -163,15 +171,17 @@ func AddTransactionToWorldByAnyTransaction(world *cardinal.World, cardinalTx car
panic(fmt.Sprintf("cannot find transaction %q in registered transactions. Did you register it?",
cardinalTx.Convert().Name()))
}
// uniqueSignature is copied from
sig := uniqueSignature()
_, _ = ecsWorld.AddTransaction(txID, value, sig)

_, _ = ecsWorld.AddTransaction(txID, value, signedPayload)
}

// MakeWorldAndTicker sets up a cardinal.World as well as a function that can execute one game tick. The *cardinal.World
// will be automatically started when doTick is called for the first time. The cardinal.World will be shut down at the
// end of the test. If doTick takes longer than 5 seconds to run, t.Fatal will be called.
func MakeWorldAndTicker(t *testing.T, opts ...cardinal.WorldOption) (world *cardinal.World, doTick func()) {
func MakeWorldAndTicker(t *testing.T,
opts ...cardinal.WorldOption) (
world *cardinal.World,
doTick func()) {
startTickCh, doneTickCh := make(chan time.Time), make(chan uint64)
opts = append(opts, cardinal.WithTickChannel(startTickCh), cardinal.WithTickDoneChannel(doneTickCh))
world, err := cardinal.NewMockWorld(opts...)
Expand Down Expand Up @@ -211,16 +221,8 @@ func MakeWorldAndTicker(t *testing.T, opts ...cardinal.WorldOption) (world *card
}
})

select {
case startTickCh <- time.Now():
case <-timeout:
t.Fatal("timeout while waiting for tick start")
}
select {
case <-doneTickCh:
case <-timeout:
t.Fatal("timeout while waiting for tick end")
}
startTickCh <- time.Now()
<-doneTickCh
}

return world, doTick
Expand Down
8 changes: 5 additions & 3 deletions cardinal/transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cardinal_test

import (
"pkg.world.dev/world-engine/cardinal/testutils"
"testing"

"pkg.world.dev/world-engine/cardinal/testutils"

"gotest.tools/v3/assert"

"pkg.world.dev/world-engine/cardinal"
)

Expand Down Expand Up @@ -40,10 +42,10 @@ func TestTransactionExample(t *testing.T) {
// Queue up the transaction.
idToModify := ids[3]
amountToModify := 20

payload := testutils.UniqueSignature()
testutils.AddTransactionToWorldByAnyTransaction(
world, addHealthToEntity,
AddHealthToEntityTx{idToModify, amountToModify},
AddHealthToEntityTx{idToModify, amountToModify}, payload,
)

// The health change should be applied during this tick
Expand Down