-
Notifications
You must be signed in to change notification settings - Fork 23
/
connect.test.ts
64 lines (59 loc) · 2.81 KB
/
connect.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { createClient, createRouterTransport, ServiceImpl } from "@connectrpc/connect";
import { createConnectTransport } from "@connectrpc/connect-node";
import { describe, it } from "node:test";
import assert from "node:assert";
import { ElizaService, SayRequestSchema } from "./gen/connectrpc/eliza/v1/eliza_pb.js";
import { ConverseRequest, IntroduceRequest, SayRequest } from "./gen/connectrpc/eliza/v1/eliza_pb.js";
import routes from "./connect.js";
import { setupTestServer } from "./setup-test-server.js";
import { build } from "./server.js";
import { create } from "@bufbuild/protobuf";
describe("testing the eliza service with an in-memory server", () => {
it("say should repeat what we said", async () => {
// Create an in-memory transport with the routes from connect.ts
const transport = createRouterTransport(routes);
const client = createClient(ElizaService, transport);
const { sentence } = await client.say({ sentence: "hello" });
assert.strictEqual(sentence, "You said hello");
});
});
describe("testing the eliza service with a full HTTP server", () => {
// For each test in this describe-block, set up the full HTTP server from server.ts
const serverInfo = setupTestServer(build);
// Alternatively, serve just the routes from connect.ts:
// const serverInfo = setupTestServer(() => http.createServer(connectNodeAdapter({ routes })));
it("say should repeat what we said", async () => {
// create a transport for the test server
const transport = createConnectTransport(serverInfo());
const client = createClient(ElizaService, transport);
const { sentence } = await client.say({ sentence: "hello" });
assert.strictEqual(sentence, "You said hello");
});
});
describe("unit testing the eliza service", () => {
// We are not using this style in connect.ts, but services can also be
// implemented as classes. This approach is useful for unit testing, making
// it trivial to inject test-only dependencies via the constructor.
class Eliza implements ServiceImpl<typeof ElizaService> {
say(req: SayRequest) {
return {
sentence: `You said ${req.sentence}`,
}
}
async * introduce(req: IntroduceRequest) {
yield { sentence: `Hi ${req.name}, I'm Eliza` }
}
async * converse(reqs: AsyncIterable<ConverseRequest>) {
for await (const req of reqs) {
yield {
sentence: `You said ${req.sentence}`,
}
}
}
}
it("say should repeat what we said", async () => {
const eliza = new Eliza();
const { sentence } = await eliza.say(create(SayRequestSchema, { sentence: "hello" }));
assert.strictEqual(sentence, "You said hello");
});
});