diff --git a/test/appRouter.ts b/test/appRouter.ts index f3a3bb2..cb8c42d 100644 --- a/test/appRouter.ts +++ b/test/appRouter.ts @@ -7,11 +7,22 @@ const t = initTRPC.create(); const publicProcedure = t.procedure; const router = t.router; +const state = { count: 0 }; + export const appRouter = router({ greet: publicProcedure .input((val: unknown) => { if (typeof val === 'string') return val; throw new Error(`Invalid input: ${typeof val}`); }) - .query(({ input }) => ({ greeting: `hello, ${input}!` })) + .query(({ input }) => ({ greeting: `hello, ${input}!` })), + countUp: publicProcedure + .input((val: unknown) => { + if (typeof val === 'number') return val; + throw new Error(`Invalid input: ${typeof val}`); + }) + .mutation(({ input }) => { + state.count += input; + return state.count; + }) }); diff --git a/test/broker.ts b/test/broker.ts new file mode 100644 index 0000000..3d39041 --- /dev/null +++ b/test/broker.ts @@ -0,0 +1,9 @@ +import Aedes from 'aedes'; +import { createServer } from 'net'; + +const aedes = new Aedes(); +const server = createServer(aedes.handle); + +server.listen(1883, () => { + console.log('server started and listening on port 1883'); +}); diff --git a/test/client.ts b/test/client.ts deleted file mode 100644 index 04aa429..0000000 --- a/test/client.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { createTRPCProxyClient } from '@trpc/client'; -import mqtt from 'mqtt'; - -import { mqttLink } from '../src/link'; -import type { AppRouter } from './appRouter'; - -const client = mqtt.connect('mqtt://localhost'); - -export const trpc = createTRPCProxyClient({ - links: [ - mqttLink({ - client, - requestTopic: 'rpc/request' - }) - ] -}); - -const main = async () => { - const greeting = await trpc.greet.query('world'); - console.log(greeting); - console.log('Closing MQTT client...'); - client.end(); - console.log('MQTT client closed.'); -}; - -main(); diff --git a/test/index.test.ts b/test/index.test.ts new file mode 100644 index 0000000..a05586e --- /dev/null +++ b/test/index.test.ts @@ -0,0 +1,64 @@ +import { createTRPCProxyClient } from '@trpc/client'; +import Aedes from 'aedes'; +import { once } from 'events'; +import mqtt from 'mqtt'; +import { createServer } from 'net'; + +import { createMQTTHandler } from '../src/adapter'; +import { mqttLink } from '../src/link'; +import { AppRouter, appRouter } from './appRouter'; + +const requestTopic = 'rpc/request'; + +const aedes = new Aedes(); +// aedes.on('publish', (packet, client) => console.log(packet.topic, packet.payload.toString())); +const broker = createServer(aedes.handle); +broker.listen(1883); +const mqttClient = mqtt.connect('mqtt://localhost'); + +createMQTTHandler({ + client: mqttClient, + requestTopic, + router: appRouter +}); + +const client = createTRPCProxyClient({ + links: [ + mqttLink({ + client: mqttClient, + requestTopic + }) + ] +}); + +beforeAll(async () => { + await once(broker, 'listening'); + await once(mqttClient, 'connect'); +}); + +test('broker is listening', () => { + expect(broker.listening).toBe(true); +}); + +test('mqtt client is connected', () => { + expect(mqttClient.connected).toBe(true); +}); + +test('greet query', async () => { + const greeting = await client.greet.query('world'); + expect(greeting).toEqual({ greeting: 'hello, world!' }); +}); + +test('countUp mutation', async () => { + const addOne = await client.countUp.mutate(1); + expect(addOne).toBe(1); + + const addTwo = await client.countUp.mutate(2); + expect(addTwo).toBe(3); +}); + +afterAll(async () => { + mqttClient.end(); + broker.close(); + aedes.close(); +}); diff --git a/test/server.ts b/test/server.ts deleted file mode 100644 index 5a827d5..0000000 --- a/test/server.ts +++ /dev/null @@ -1,18 +0,0 @@ -import mqtt from 'mqtt'; - -import { createMQTTHandler } from '../src/adapter'; -import { appRouter } from './appRouter'; - -const client = mqtt.connect('mqtt://localhost'); - -createMQTTHandler({ - client, - requestTopic: 'rpc/request', - router: appRouter -}); - -process.on('SIGINT', () => { - console.log('Closing MQTT client...'); - client.end(); - console.log('MQTT client closed.'); -});