2024-01-30 21:50:19 +01:00
|
|
|
import { afterEach, beforeEach, expect, test } from "bun:test";
|
2024-01-04 21:20:47 +01:00
|
|
|
import { z } from "zod";
|
2024-01-30 21:50:19 +01:00
|
|
|
import NatsAdaptor from "../src/adaptors/nats";
|
|
|
|
import { RequestError } from "../src/error";
|
|
|
|
import Service from "../src/service";
|
2023-10-19 21:48:44 +02:00
|
|
|
|
|
|
|
let srv!: Service;
|
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
const natsServer = Bun.env.NATS_HOST || "127.0.0.1:4222";
|
2023-10-19 21:48:44 +02:00
|
|
|
const serviceName = "test-service";
|
|
|
|
const adaptorName = "nats";
|
|
|
|
const subject = "test";
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2024-01-30 21:50:19 +01:00
|
|
|
srv = new Service(serviceName);
|
2023-10-19 21:48:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
2024-01-30 21:50:19 +01:00
|
|
|
await srv.stop();
|
2023-10-19 21:48:44 +02:00
|
|
|
});
|
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
test("request success", async () => {
|
|
|
|
const dataExpected = "hello success";
|
|
|
|
const statusCodeExpected = 200;
|
2023-10-19 22:53:31 +02:00
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
|
|
|
srv.subscribe(
|
|
|
|
adaptorName,
|
|
|
|
subject,
|
2024-04-10 19:46:07 +02:00
|
|
|
async (srv, msg) => {
|
2024-01-30 21:50:19 +01:00
|
|
|
return { data: msg.data, statusCode: statusCodeExpected };
|
|
|
|
},
|
|
|
|
z.string(),
|
|
|
|
);
|
2023-10-19 21:48:44 +02:00
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
await srv.listen();
|
2023-10-19 21:48:44 +02:00
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
const res = await srv.request(
|
|
|
|
adaptorName,
|
|
|
|
{
|
|
|
|
service: serviceName,
|
|
|
|
subject: subject,
|
|
|
|
data: dataExpected,
|
|
|
|
},
|
|
|
|
z.string(),
|
|
|
|
);
|
2023-10-19 21:48:44 +02:00
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
expect(res.data).toBe(dataExpected);
|
|
|
|
expect(res.statusCode).toBe(statusCodeExpected);
|
2024-01-04 21:20:47 +01:00
|
|
|
});
|
2023-10-19 22:53:31 +02:00
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
test("request error", async () => {
|
|
|
|
const dataExpected = "hello error";
|
|
|
|
const statusCodeExpected = 500;
|
2024-01-04 21:20:47 +01:00
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
2024-04-10 19:46:07 +02:00
|
|
|
srv.subscribe(adaptorName, subject, async (srv, msg) => {
|
|
|
|
throw new RequestError("request error", 500);
|
|
|
|
});
|
2024-01-04 21:20:47 +01:00
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
await srv.listen();
|
|
|
|
expect(() => {
|
|
|
|
return srv.request(
|
|
|
|
adaptorName,
|
|
|
|
{
|
|
|
|
service: serviceName,
|
|
|
|
subject: subject,
|
|
|
|
data: dataExpected,
|
|
|
|
},
|
|
|
|
z.string(),
|
|
|
|
);
|
|
|
|
}).toThrow();
|
2023-10-19 22:53:31 +02:00
|
|
|
});
|
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
test("request adaptor not found", async () => {
|
|
|
|
const dataExpected = "hello success";
|
|
|
|
const statusCodeExpected = 200;
|
2024-01-04 21:20:47 +01:00
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
2024-04-10 19:46:07 +02:00
|
|
|
srv.subscribe(adaptorName, subject, async (srv, msg) => {
|
2024-01-30 21:50:19 +01:00
|
|
|
return { data: msg.data, statusCode: statusCodeExpected };
|
|
|
|
});
|
2024-01-04 21:20:47 +01:00
|
|
|
|
2024-01-30 21:50:19 +01:00
|
|
|
await srv.listen();
|
|
|
|
expect(() => {
|
|
|
|
return srv.request("bad adaptor", {
|
|
|
|
service: serviceName,
|
|
|
|
subject: subject,
|
|
|
|
data: dataExpected,
|
|
|
|
});
|
|
|
|
}).toThrow();
|
2023-10-19 23:27:51 +02:00
|
|
|
});
|