import "reflect-metadata"; import { MethodIdResolver, Runtime, RuntimeModule } from "@proto-kit/module"; import { ChildContainerProvider } from "@proto-kit/common"; import { Protocol, ProtocolModule } from "@proto-kit/protocol"; import { VanillaProtocolModules, VanillaRuntimeModules, } from "@proto-kit/library"; import { Sequencer, SequencerModule, AppChain } from "@proto-kit/sequencer"; import { PrivateKey } from "o1js"; class TestRuntimeModule extends RuntimeModule { public initialized = false; public create(childContainerProvider: ChildContainerProvider) { super.create(childContainerProvider); // Just to test if it doesn't throw childContainerProvider(); this.initialized = true; } } class TestProtocolModule extends ProtocolModule { public initialized = false; public create() { this.initialized = true; } } class TestSequencerModule extends SequencerModule { public initialized = false; public started = false; public create() { this.initialized = true; } public async start(): Promise { this.started = true; } } function createAppChain() { const appChain = AppChain.from({ Runtime: Runtime.from( VanillaRuntimeModules.with({ TestRuntimeModule, }) ), Protocol: Protocol.from( VanillaProtocolModules.with({ TestProtocolModule, }) ), Sequencer: Sequencer.from({ TestSequencerModule, }), }); appChain.configurePartial({ Runtime: { Balances: {}, TestRuntimeModule: {}, }, Protocol: { ...Protocol.defaultConfig(), TransactionFee: { tokenId: 0n, feeRecipient: PrivateKey.random().toPublicKey().toBase58(), baseFee: 0n, perWeightUnitFee: 0n, methods: {}, }, TestProtocolModule: {}, }, Sequencer: { TestSequencerModule: {}, }, }); return appChain; } describe("modularization", () => { let appChain: ReturnType; it("should initialize all modules correctly", async () => { appChain = createAppChain(); await appChain.start(); // eslint-disable-next-line @typescript-eslint/no-unused-vars const m = appChain.protocol.resolve("TestProtocolModule"); expect(appChain.runtime.resolve("TestRuntimeModule").initialized).toBe( true ); expect(appChain.protocol.resolve("TestProtocolModule").initialized).toBe( true ); expect(appChain.sequencer.resolve("TestSequencerModule").initialized).toBe( true ); expect(appChain.sequencer.resolve("TestSequencerModule").started).toBe( true ); expect(appChain.runtime.dependencyContainer.isRegistered("Runtime")).toBe( false ); // Tests that the DependencyFactory got executed expect( appChain.runtime.resolveOrFail("MethodIdResolver", MethodIdResolver) ).toBeDefined(); }); afterAll(async () => { await appChain.close(); }); });