ab29b28125
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint Pipeline failed
ci/woodpecker/push/test/1 Pipeline was successful
ci/woodpecker/push/test/2 Pipeline was successful
ci/woodpecker/push/test/3 Pipeline was successful
ci/woodpecker/push/test/4 Pipeline was successful
92 lines
2 KiB
TypeScript
92 lines
2 KiB
TypeScript
import { afterEach, beforeEach, expect, test } from "bun:test";
|
|
import { z } from "zod";
|
|
import NatsAdaptor from "../src/adaptors/nats";
|
|
import { RequestError } from "../src/error";
|
|
import Service from "../src/service";
|
|
|
|
let srv!: Service;
|
|
|
|
const natsServer = Bun.env.NATS_HOST || "127.0.0.1:4222";
|
|
const serviceName = "test-service";
|
|
const adaptorName = "nats";
|
|
const subject = "test";
|
|
|
|
beforeEach(() => {
|
|
srv = new Service(serviceName);
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await srv.stop();
|
|
});
|
|
|
|
test("request success", async () => {
|
|
const dataExpected = "hello success";
|
|
const statusCodeExpected = 200;
|
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
|
srv.subscribe(
|
|
adaptorName,
|
|
subject,
|
|
async (srv, msg) => {
|
|
return { data: msg.data, statusCode: statusCodeExpected };
|
|
},
|
|
z.string(),
|
|
);
|
|
|
|
await srv.listen();
|
|
|
|
const res = await srv.request(
|
|
adaptorName,
|
|
{
|
|
service: serviceName,
|
|
subject: subject,
|
|
data: dataExpected,
|
|
},
|
|
z.string(),
|
|
);
|
|
|
|
expect(res.data).toBe(dataExpected);
|
|
expect(res.statusCode).toBe(statusCodeExpected);
|
|
});
|
|
|
|
test("request error", async () => {
|
|
const dataExpected = "hello error";
|
|
const statusCodeExpected = 500;
|
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
|
srv.subscribe(adaptorName, subject, async (srv, msg) => {
|
|
throw new RequestError("request error", 500);
|
|
});
|
|
|
|
await srv.listen();
|
|
expect(() => {
|
|
return srv.request(
|
|
adaptorName,
|
|
{
|
|
service: serviceName,
|
|
subject: subject,
|
|
data: dataExpected,
|
|
},
|
|
z.string(),
|
|
);
|
|
}).toThrow();
|
|
});
|
|
|
|
test("request adaptor not found", async () => {
|
|
const dataExpected = "hello success";
|
|
const statusCodeExpected = 200;
|
|
|
|
srv.addAdaptor(adaptorName, new NatsAdaptor({ servers: [natsServer] }));
|
|
srv.subscribe(adaptorName, subject, async (srv, msg) => {
|
|
return { data: msg.data, statusCode: statusCodeExpected };
|
|
});
|
|
|
|
await srv.listen();
|
|
expect(() => {
|
|
return srv.request("bad adaptor", {
|
|
service: serviceName,
|
|
subject: subject,
|
|
data: dataExpected,
|
|
});
|
|
}).toThrow();
|
|
});
|