forked from webrpc/webrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89044fb
commit dc8ee49
Showing
1 changed file
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
}); |