Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Argus-Labs/world-engine into brian/…
Browse files Browse the repository at this point in the history
…world-485-make-sure-querygo-api-is-protected-with-tests
  • Loading branch information
pyrofolium committed Nov 3, 2023
2 parents 8567137 + 8add18f commit 4f605ad
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 130 deletions.
7 changes: 6 additions & 1 deletion cardinal/component_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 @@ -34,6 +36,9 @@ func TestComponentExample(t *testing.T) {
assert.NilError(t, cardinal.RegisterComponent[Age](world))

testWorldCtx := testutils.WorldToWorldContext(world)
assert.Equal(t, testWorldCtx.CurrentTick(), uint64(0))
testWorldCtx.Logger().Info().Msg("test") // Check for compile errors.
testWorldCtx.EmitEvent("test") // test for compiler errors, a check for this lives in e2e tests.
startHeight := 72
startWeight := 200
startAge := 30
Expand Down
11 changes: 11 additions & 0 deletions cardinal/ecs/component/metadata/component_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"gotest.tools/v3/assert"

"pkg.world.dev/world-engine/cardinal/ecs/entity"
"pkg.world.dev/world-engine/cardinal/ecs/filter"

Expand All @@ -27,6 +28,16 @@ type ComponentDataB struct {

func (ComponentDataB) Name() string { return "b" }

func getNameOfComponent(c metadata.Component) string {
return c.Name()
}

func TestComponentInterfaceSignature(t *testing.T) {
// The purpose of this test is to maintain api compatibility.
// It is to prevent the interface signature of metadata.Component from changing.
assert.Equal(t, getNameOfComponent(&ComponentDataA{}), "a")
}

func TestComponents(t *testing.T) {
world := ecs.NewTestWorld(t)
ecs.MustRegisterComponent[ComponentDataA](world)
Expand Down
8 changes: 8 additions & 0 deletions cardinal/ecs/ecb/ecb.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ func (m *Manager) RemoveEntity(idToRemove entity.ID) error {
m.entityIDToOriginArchID[idToRemove] = archID
}
delete(m.entityIDToArchID, idToRemove)

comps := m.GetComponentTypesForArchID(archID)
for _, comp := range comps {
key := compKey{comp.ID(), idToRemove}
delete(m.compValues, key)
m.compValuesToDelete[key] = true
}

return nil
}

Expand Down
20 changes: 20 additions & 0 deletions cardinal/ecs/ecb/recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,23 @@ func TestArchetypeCountTracksDiscardedChanges(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, 1, manager.ArchetypeCount())
}

func TestCannotFetchComponentOnRemovedEntityAfterCommit(t *testing.T) {
manager := newCmdBufferForTest(t)

id, err := manager.CreateEntity(fooComp, barComp)
assert.NilError(t, err)
_, err = manager.GetComponentForEntity(fooComp, id)
assert.NilError(t, err)
assert.NilError(t, manager.RemoveEntity(id))

// The entity has been removed. Trying to get a component for the entity should fail.
_, err = manager.GetComponentForEntity(fooComp, id)
assert.Check(t, err != nil)

assert.NilError(t, manager.CommitPending())

// Trying to get the same component after committing to the DB should also fail.
_, err = manager.GetComponentForEntity(fooComp, id)
assert.Check(t, err != nil)
}
38 changes: 38 additions & 0 deletions cardinal/entity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cardinal_test

import (
"testing"

"gotest.tools/v3/assert"
"pkg.world.dev/world-engine/cardinal"
"pkg.world.dev/world-engine/cardinal/testutils"
)

func TestCanRemoveEntity(t *testing.T) {
world, _ := testutils.MakeWorldAndTicker(t)

assert.NilError(t, cardinal.RegisterComponent[Alpha](world))

testWorldCtx := testutils.WorldToWorldContext(world)
ids, err := cardinal.CreateMany(testWorldCtx, 2, Alpha{})
assert.NilError(t, err)

removeID := ids[0]
keepID := ids[1]

assert.NilError(t, cardinal.Remove(testWorldCtx, removeID))

search, err := testWorldCtx.NewSearch(cardinal.Exact(Alpha{}))
assert.NilError(t, err)
count := 0
assert.NilError(t, search.Each(testWorldCtx, func(id cardinal.EntityID) bool {
assert.Equal(t, id, keepID)
count++
return true
}))
assert.Equal(t, 1, count)

// We should not be able to find the component for the removed ID
_, err = cardinal.GetComponent[Alpha](testWorldCtx, removeID)
assert.Check(t, err != nil)
}
55 changes: 0 additions & 55 deletions cardinal/log.go

This file was deleted.

49 changes: 0 additions & 49 deletions cardinal/log_test.go

This file was deleted.

32 changes: 32 additions & 0 deletions cardinal/option_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cardinal

import (
"context"
"errors"
"testing"

"pkg.world.dev/world-engine/chain/x/shard/types"
"pkg.world.dev/world-engine/sign"
)

type DummyAdapter struct{}

func (d *DummyAdapter) Submit(_ context.Context, _ *sign.SignedPayload, _, _ uint64) error {
return nil
}

func (d *DummyAdapter) QueryTransactions(_ context.Context, _ *types.QueryTransactionsRequest,
) (*types.QueryTransactionsResponse, error) {
return nil, errors.New("this function should never get called")
}

func TestOptionFunctionSignatures(_ *testing.T) {
// This test is designed to keep API compatibility. If a compile error happens here it means a function signature to
// public facing functions was changed.
WithAdapter(&DummyAdapter{})
WithReceiptHistorySize(1)
WithNamespace("blah")
WithPort("4040")
WithDisableSignatureVerification() //nolint:staticcheck //this test just looks for compile errors
WithPrettyLog() //nolint:staticcheck //this test just looks for compile errors
}
10 changes: 9 additions & 1 deletion cardinal/search_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 @@ -70,6 +72,12 @@ func TestSearchExample(t *testing.T) {
cardinal.Not(cardinal.Exact(Alpha{})),
60,
},
{
"alpha and beta",
cardinal.And(cardinal.Contains(Alpha{}),
cardinal.Contains(Beta{}),
), 20,
},
}
for _, tc := range testCases {
msg := "problem with " + tc.name
Expand Down
6 changes: 0 additions & 6 deletions cardinal/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@ type AnyTransaction interface {
Convert() transaction.ITransaction
}

// TransactionQueue contains the entire set of transactions that should be processed in a game tick. It is a parameter
// to a System function. Access the transactions of a particular type by using TransactionType.In.
type TransactionQueue struct {
_ *transaction.TxQueue
}

// TxData represents a single transaction.
type TxData[T any] struct {
impl ecs.TxData[T]
Expand Down
33 changes: 33 additions & 0 deletions cardinal/transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cardinal_test

import (
"errors"
"testing"

"pkg.world.dev/world-engine/cardinal/testutils"
Expand All @@ -19,11 +20,24 @@ type AddHealthToEntityResult struct{}

var addHealthToEntity = cardinal.NewTransactionType[AddHealthToEntityTx, AddHealthToEntityResult]("add_health")

func TestApis(t *testing.T) {
// this test just makes sure certain signatures remain the same.
// If they change this test will trigger a compiler error.
x := cardinal.TxData[Alpha]{}
x.Sig()
x.Hash()
assert.Equal(t, x.Value().Name(), "alpha")
type randoTx struct{}
type randoTxResult struct{}
cardinal.NewTransactionTypeWithEVMSupport[randoTx, randoTxResult]("rando_with_evm")
}

func TestTransactionExample(t *testing.T) {
world, doTick := testutils.MakeWorldAndTicker(t)
assert.NilError(t, cardinal.RegisterComponent[Health](world))
assert.NilError(t, cardinal.RegisterTransactions(world, addHealthToEntity))
cardinal.RegisterSystems(world, func(worldCtx cardinal.WorldContext) error {
// test "In" method
for _, tx := range addHealthToEntity.In(worldCtx) {
targetID := tx.Value().TargetID
err := cardinal.UpdateComponent[Health](worldCtx, targetID, func(h *Health) *Health {
Expand All @@ -32,6 +46,25 @@ func TestTransactionExample(t *testing.T) {
})
assert.Check(t, err == nil)
}
// test same as above but with forEach
addHealthToEntity.ForEach(worldCtx, func(tx cardinal.TxData[AddHealthToEntityTx]) (AddHealthToEntityResult, error) {
targetID := tx.Value().TargetID
err := cardinal.UpdateComponent[Health](worldCtx, targetID, func(h *Health) *Health {
h.Value = tx.Value().Amount
return h
})
assert.Check(t, err == nil)
addHealthToEntity.AddError(worldCtx, tx.Hash(), errors.New("test error"))
// redundant but for testing purposes
addHealthToEntity.SetResult(worldCtx, tx.Hash(), AddHealthToEntityResult{})
_, errs, ok := addHealthToEntity.GetReceipt(worldCtx, tx.Hash()) // check if receipts are working.
assert.Assert(t, ok)
assert.Equal(t, len(errs), 1)
return AddHealthToEntityResult{}, nil
})

addHealthToEntity.Convert() // Check for compilation error

return nil
})

Expand Down
4 changes: 2 additions & 2 deletions docs/pages/Cardinal/API-Reference/World.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
)

func main() {
redisAddr := os.Getenv("redis_address")
redisPassword := os.Getenv("redis_password")
redisAddr := os.Getenv("REDIS_ADDR")
redisPassword := os.Getenv("REDIS_PASSWORD")

world, err := cardinal.NewWorld(
redisAddr,
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/Nakama/client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ While `AuthenticateDevice` is the easiest way to connect to Nakama for testing,
A game client must use the `/nakama/create-persona` endpoint to claim a Persona Tag. The payload should be:

```json
{"persona_tag": "some-random-persona-tag"}
{"personaTag": "some-random-persona-tag"}
```

Verify the claim Persona Tag request was successful with the `nakama/show-persona` endpoint. Look for the field `"status":"accepted"` in the body the request was successful.
Expand Down
Loading

0 comments on commit 4f605ad

Please sign in to comment.