From dc8ee49354da21290245a2fd94fa5ede0773df58 Mon Sep 17 00:00:00 2001 From: Alexander Kolberg Date: Thu, 7 Dec 2023 16:08:15 +0100 Subject: [PATCH] basic unit test setup --- _examples/golang-sse/webapp/tests/sse.test.ts | 134 ++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 _examples/golang-sse/webapp/tests/sse.test.ts diff --git a/_examples/golang-sse/webapp/tests/sse.test.ts b/_examples/golang-sse/webapp/tests/sse.test.ts new file mode 100644 index 00000000..7717ce11 --- /dev/null +++ b/_examples/golang-sse/webapp/tests/sse.test.ts @@ -0,0 +1,134 @@ +import { beforeEach, test, expect, vi } from "vitest"; + +import { + WebrpcError, + Chat, + SubscribeMessagesReturn, + Message, + WebrpcServerPanicError, +} from "../src/rpc"; + +const data = [ + { + id: 1, + text: "Hello", + authorName: "John", + createdAt: new Date().toDateString(), + }, + { + id: 2, + text: "Hi", + authorName: "Joe", + createdAt: new Date().toDateString(), + }, + { + id: 3, + text: "How are you?", + authorName: "John", + createdAt: new Date().toDateString(), + }, +] satisfies Message[]; + + +function createMockFetch( + { status, body }: { status?: number; body?: any } = { + status: 200, + body: undefined, + } +) { + return function mockFetch(input: RequestInfo | URL, init?: RequestInit) { + const stream = new ReadableStream({ + start(controller) { + data.forEach((item) => { + const buffer = new TextEncoder().encode( + JSON.stringify({ message: item }) + "\n" + ); + controller.enqueue(buffer); + }); + + controller.close(); + }, + }); + + body = body || stream; + + return Promise.resolve( + new Response(body, { + status, + headers: { + "Content-Type": "application/x-ndjson", + }, + }) + ); + }; +} + + + +let onMessage = (msg: any) => {}; +let onError = (err: WebrpcError) => {}; + +beforeEach(() => { + onMessage = (msg: any) => {}; + onError = (err: WebrpcError) => {}; +}); + + + +test("call onOpen right before opening stream", async () => { + const mockFetch = createMockFetch(); + const api = new Chat("", mockFetch); + + const onOpen = vi.fn(); + await api.subscribeMessages( + { serverTimeoutSec: 10 }, + { onOpen, onMessage, onError } + ); + + expect(onOpen).toHaveBeenCalled(); +}); + +test("call onMessage with correct data", async () => { + const mockFetch = createMockFetch(); + const api = new Chat("", mockFetch); + + let messages: Message[] = []; + const onMessage = (msg: SubscribeMessagesReturn) => { + messages.push(msg.message); + }; + await api.subscribeMessages({ serverTimeoutSec: 10 }, { onMessage, onError }); + + expect(messages).toEqual(data); +}); + +test("call onClose when stream is done", async () => { + const mockFetch = createMockFetch(); + const api = new Chat("", mockFetch); + + const onClose = vi.fn(); + + await api.subscribeMessages( + { serverTimeoutSec: 10 }, + { onMessage, onError, onClose } + ); + + expect(onClose).toHaveBeenCalled(); +}); + +test("call onError with WebrpcServerPanicError on server panic", async () => { + const mockFetch = createMockFetch({ + status: 500, + body: JSON.stringify({ code: -6 }), + }); + + const api = new Chat("", mockFetch); + let error: WebrpcError | undefined; + + const onError = (err: WebrpcError) => { + error = err; + }; + + await api.subscribeMessages({ serverTimeoutSec: 10 }, { onMessage, onError }); + + expect(error).toEqual(new WebrpcServerPanicError()); +});