package guild_test // module_test.go exercises the x/guild AppModule (D-054 simtest-grade). // The AppModule wraps the Keeper + exposes the MsgServer; this test // constructs an AppModule with nil shims + asserts Name, ConsensusVersion, // MsgServer, InitGenesis, ExportGenesis. Coverage target: the module.go // surface. import ( "encoding/json" "testing" "cosmossdk.io/log" "cosmossdk.io/store" storetypes "cosmossdk.io/store/types" cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" dbm "github.com/cosmos/cosmos-db" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/oy/openyield/x/guild" "github.com/oy/openyield/x/guild/types" ) func newModuleTestContext(t *testing.T) (sdk.Context, guild.AppModule, codec.Codec) { t.Helper() db := dbm.NewMemDB() cdc := newModuleTestCodec() storeKey := storetypes.NewKVStoreKey(types.StoreKey) cms := store.NewCommitMultiStore(db, log.NewNopLogger(), nil) cms.MountStoreWithDB(storeKey, storetypes.StoreTypeDB, nil) if err := cms.LoadLatestVersion(); err != nil { t.Fatalf("load latest version: %v", err) } ctx := sdk.NewContext(cms, cmtproto.Header{}, false, log.NewNopLogger()) am := guild.NewAppModule(cdc, storeKey, nil, nil) return ctx, am, cdc } func newModuleTestCodec() codec.Codec { registry := codectypes.NewInterfaceRegistry() return codec.NewProtoCodec(registry) } // TestAppModuleName asserts the module name. func TestAppModuleName(t *testing.T) { _, am, _ := newModuleTestContext(t) if am.Name() != types.ModuleName { t.Errorf("Name = %q, want %q", am.Name(), types.ModuleName) } } // TestAppModuleConsensusVersion asserts ConsensusVersion == 1. func TestAppModuleConsensusVersion(t *testing.T) { _, am, _ := newModuleTestContext(t) if am.ConsensusVersion() != guild.ConsensusVersion { t.Errorf("ConsensusVersion = %d, want %d", am.ConsensusVersion(), guild.ConsensusVersion) } if guild.ConsensusVersion != 1 { t.Errorf("ConsensusVersion const = %d, want 1", guild.ConsensusVersion) } } // TestAppModuleMsgServer asserts MsgServer returns a non-nil MsgServer. func TestAppModuleMsgServer(t *testing.T) { _, am, _ := newModuleTestContext(t) srv := am.MsgServer() if srv == nil { t.Fatal("MsgServer() returned nil") } } // TestAppModuleInitExportGenesis asserts InitGenesis + ExportGenesis round- // trip an empty genesis. func TestAppModuleInitExportGenesis(t *testing.T) { ctx, am, cdc := newModuleTestContext(t) empty := types.DefaultGenesisState() data := cdc.MustMarshalJSON(empty) am.InitGenesis(ctx, cdc, data) exported := am.ExportGenesis(ctx, cdc) if len(exported) == 0 { t.Fatal("ExportGenesis returned empty bytes") } var gs types.GenesisState if err := json.Unmarshal(exported, &gs); err != nil { t.Fatalf("ExportGenesis bytes not valid JSON: %v", err) } } // TestAppModuleRegisterServicesNoPanic asserts RegisterServices does not // panic with a nil configurator (simtest-grade — the method is a no-op stub // for the hand-rolled MsgServer wiring). func TestAppModuleRegisterServicesNoPanic(t *testing.T) { _, am, _ := newModuleTestContext(t) defer func() { if r := recover(); r != nil { t.Errorf("RegisterServices panicked: %v", r) } }() am.RegisterServices(nil) }