From 21f552c02a1e0c2a9a075e38f8ff81976b92e2ae Mon Sep 17 00:00:00 2001 From: Tomek Marciniak Date: Mon, 9 Sep 2024 10:38:11 +0200 Subject: [PATCH] fix(klesia): decouple server from exports --- apps/klesia/package.json | 4 +- apps/klesia/src/index.spec.ts | 86 ++++++++++++++++++----------------- apps/klesia/src/index.ts | 10 +--- apps/klesia/src/server.ts | 10 ++++ apps/klesia/tsup.config.ts | 6 ++- package.json | 2 +- turbo.json | 1 + 7 files changed, 64 insertions(+), 55 deletions(-) create mode 100644 apps/klesia/src/server.ts diff --git a/apps/klesia/package.json b/apps/klesia/package.json index d988fd2..fb16cdd 100644 --- a/apps/klesia/package.json +++ b/apps/klesia/package.json @@ -12,8 +12,8 @@ }, "scripts": { "build": "tsup-node", - "dev": "tsup-node --watch --onSuccess \"node dist/index.js\"", - "start": "node dist/index.js", + "dev": "tsup-node --watch --onSuccess \"node dist/server.js\"", + "start": "node dist/server.js", "test": "bun test" }, "dependencies": { diff --git a/apps/klesia/src/index.spec.ts b/apps/klesia/src/index.spec.ts index 6ff6ad2..2821101 100644 --- a/apps/klesia/src/index.spec.ts +++ b/apps/klesia/src/index.spec.ts @@ -1,56 +1,58 @@ -import { expect, it } from "bun:test"; +import { describe, expect, it } from "bun:test"; import { testClient } from "hono/testing"; import { klesiaRpcRoute } from "./"; -const client = testClient(klesiaRpcRoute); +const request = testClient(klesiaRpcRoute).api.$post; -it("returns result for mina_getTransactionCount", async () => { - const response = await client.api.$post({ - json: { - method: "mina_getTransactionCount", - params: ["B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS"], - }, +describe("Mina Devnet RPC", () => { + it("returns result for mina_getTransactionCount", async () => { + const response = await request({ + json: { + method: "mina_getTransactionCount", + params: ["B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS"], + }, + }); + const { result } = (await response.json()) as { result: string }; + expect(BigInt(result)).toBeGreaterThan(0); }); - const { result } = (await response.json()) as { result: string }; - expect(BigInt(result)).toBeGreaterThan(0); -}); -it("returns result for mina_getBalance", async () => { - const response = await client.api.$post({ - json: { - method: "mina_getBalance", - params: ["B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS"], - }, + it("returns result for mina_getBalance", async () => { + const response = await request({ + json: { + method: "mina_getBalance", + params: ["B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS"], + }, + }); + const { result } = (await response.json()) as { result: string }; + expect(BigInt(String(result))).toBeGreaterThan(0); }); - const { result } = (await response.json()) as { result: string }; - expect(BigInt(String(result))).toBeGreaterThan(0); -}); -it("returns result for mina_blockHash", async () => { - const response = await client.api.$post({ - json: { method: "mina_blockHash" }, + it("returns result for mina_blockHash", async () => { + const response = await request({ + json: { method: "mina_blockHash" }, + }); + const { result } = (await response.json()) as { result: string }; + expect(result.length).toBeGreaterThan(0); }); - const { result } = (await response.json()) as { result: string }; - expect(result.length).toBeGreaterThan(0); -}); -it("returns result for mina_chainId", async () => { - const response = await client.api.$post({ - json: { method: "mina_chainId" }, + it("returns result for mina_chainId", async () => { + const response = await request({ + json: { method: "mina_chainId" }, + }); + const { result } = (await response.json()) as { result: string }; + expect(result.length).toBeGreaterThan(0); }); - const { result } = (await response.json()) as { result: string }; - expect(result.length).toBeGreaterThan(0); -}); -it("returns result for mina_getAccount", async () => { - const response = await client.api.$post({ - json: { - method: "mina_getAccount", - params: ["B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS"], - }, + it("returns result for mina_getAccount", async () => { + const response = await request({ + json: { + method: "mina_getAccount", + params: ["B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS"], + }, + }); + // biome-ignore lint/suspicious/noExplicitAny: TODO + const { result } = (await response.json()) as any; + expect(BigInt(result.nonce)).toBeGreaterThanOrEqual(0); + expect(BigInt(result.balance)).toBeGreaterThanOrEqual(0); }); - // biome-ignore lint/suspicious/noExplicitAny: TODO - const { result } = (await response.json()) as any; - expect(BigInt(result.nonce)).toBeGreaterThanOrEqual(0); - expect(BigInt(result.balance)).toBeGreaterThanOrEqual(0); }); diff --git a/apps/klesia/src/index.ts b/apps/klesia/src/index.ts index 91cf9ec..0d0e3e7 100644 --- a/apps/klesia/src/index.ts +++ b/apps/klesia/src/index.ts @@ -1,7 +1,6 @@ import "dotenv/config"; -import { serve } from "@hono/node-server"; import { getConnInfo } from "@hono/node-server/conninfo"; -import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi"; +import { OpenAPIHono, createRoute } from "@hono/zod-openapi"; import { PublicKeySchema } from "@mina-js/shared"; import { rateLimiter } from "hono-rate-limiter"; import { cors } from "hono/cors"; @@ -142,13 +141,6 @@ export const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => { .exhaustive(); }); -serve( - { fetch: api.fetch, port: z.coerce.number().parse(process.env.PORT ?? 3000) }, - (info) => { - console.log(`Listening on http://localhost:${info.port}`); - }, -); - export type KlesiaRpc = typeof klesiaRpcRoute; export { KlesiaNetwork, diff --git a/apps/klesia/src/server.ts b/apps/klesia/src/server.ts new file mode 100644 index 0000000..ef69f7f --- /dev/null +++ b/apps/klesia/src/server.ts @@ -0,0 +1,10 @@ +import { serve } from "@hono/node-server"; +import { z } from "@hono/zod-openapi"; +import { api } from "./"; + +serve( + { fetch: api.fetch, port: z.coerce.number().parse(process.env.PORT ?? 3000) }, + (info) => { + console.log(`Listening on http://localhost:${info.port}`); + }, +); diff --git a/apps/klesia/tsup.config.ts b/apps/klesia/tsup.config.ts index 33013ce..5da0516 100644 --- a/apps/klesia/tsup.config.ts +++ b/apps/klesia/tsup.config.ts @@ -1,3 +1,7 @@ +import { defineConfig } from "tsup"; import sharedConfig from "../../packages/shared/tsup.config"; -export default sharedConfig; +export default defineConfig({ + ...sharedConfig, + entry: ["src/index.ts", "src/server.ts"], +}); diff --git a/package.json b/package.json index 8f6eff0..ca887a8 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "private": true, "scripts": { "build": "turbo build", - "test": "bun run --filter '*' test", + "test": "turbo test", "cleanup": "bun run --filter '*' cleanup", "lint": "bunx biome check .", "format": "bunx biome check . --write --unsafe" diff --git a/turbo.json b/turbo.json index da62c81..4bca536 100644 --- a/turbo.json +++ b/turbo.json @@ -5,6 +5,7 @@ "dependsOn": ["^build"], "outputs": ["dist/**"] }, + "test": {}, "cleanup": {}, "dev": { "persistent": true,