-
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
b8cacce
commit a9025da
Showing
6 changed files
with
122 additions
and
24 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,28 @@ | ||
import { beforeAll, expect, mock, test } from "bun:test" | ||
import { faker } from "@faker-js/faker" | ||
import { Type } from "@sinclair/typebox" | ||
import { Client } from "@wsx/client" | ||
import { Wsx } from "@wsx/server" | ||
|
||
const onEmit = mock((message: string) => void message) | ||
|
||
async function initialize() { | ||
const wsx = new Wsx() | ||
.route("/some", ({ body }) => onEmit(body), { body: Type.String() }) | ||
.listen(0) | ||
const { port } = wsx.server! | ||
return await Client<typeof wsx>(`ws://localhost:${port}`) | ||
} | ||
|
||
let routes: Awaited<ReturnType<typeof initialize>>["routes"] | ||
beforeAll(async () => { | ||
const client = await initialize() | ||
routes = client.routes | ||
}) | ||
|
||
test("valid", async () => { | ||
const message = faker.lorem.sentence() | ||
routes.some.emit(message) | ||
await Bun.sleep(10) | ||
expect(onEmit).toBeCalledWith(message) | ||
}) |
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,29 @@ | ||
import { beforeAll, expect, mock, test } from "bun:test" | ||
import { faker } from "@faker-js/faker" | ||
import { Type } from "@sinclair/typebox" | ||
import { Client } from "@wsx/client" | ||
import { Wsx } from "@wsx/server" | ||
|
||
const onRpc = mock((message: string) => message) | ||
|
||
async function initialize() { | ||
const wsx = new Wsx() | ||
.route("/some", ({ body }) => onRpc(body), { body: Type.String() }) | ||
.listen(0) | ||
const { port } = wsx.server! | ||
return await Client<typeof wsx>(`ws://localhost:${port}`) | ||
} | ||
|
||
let routes: Awaited<ReturnType<typeof initialize>>["routes"] | ||
beforeAll(async () => { | ||
const client = await initialize() | ||
routes = client.routes | ||
}) | ||
|
||
test("valid", async () => { | ||
const message = faker.lorem.sentence() | ||
const response = await routes.some.call(message) | ||
expect(response.status).toBe("success") | ||
expect(response.body).toBe(message) | ||
expect(onRpc).toBeCalledWith(message) | ||
}) |
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 |
---|---|---|
@@ -1,26 +1,44 @@ | ||
import { expect, test } from "bun:test" | ||
import { beforeAll, expect, test } from "bun:test" | ||
import { faker } from "@faker-js/faker" | ||
import { Type } from "@sinclair/typebox" | ||
import { type Static, Type } from "@sinclair/typebox" | ||
import { Client } from "@wsx/client" | ||
import { Wsx } from "@wsx/server" | ||
|
||
const wsx = new Wsx() | ||
.route("/valid", ({ body }) => body, { | ||
body: Type.Object({ message: Type.String() }), | ||
response: Type.Object({ message: Type.String() }), | ||
}) | ||
.listen(0) | ||
type ValidBody = Static<typeof ValidBody> | ||
const ValidBody = Type.Object({ message: Type.String() }) | ||
|
||
async function initialize() { | ||
const wsx = new Wsx() | ||
.route("/validate", ({ body }) => body, { | ||
body: ValidBody, | ||
response: Type.Object({ message: Type.String() }), | ||
}) | ||
.listen(0) | ||
|
||
const { port } = wsx.server! | ||
const { port } = wsx.server! | ||
|
||
const { routes } = await Client<typeof wsx>(`ws://localhost:${port}`) | ||
return await Client<typeof wsx>(`ws://localhost:${port}`) | ||
} | ||
|
||
let routes: Awaited<ReturnType<typeof initialize>>["routes"] | ||
beforeAll(async () => { | ||
const client = await initialize() | ||
routes = client.routes | ||
}) | ||
|
||
test("valid", async () => { | ||
const message = faker.lorem.sentence() | ||
const response = await routes.valid.call({ | ||
const response = await routes.validate.call({ | ||
message, | ||
}) | ||
console.log(response) | ||
expect(response.data).toBeObject() | ||
expect(response.data!.message).toBe(message) | ||
expect(response.status).toBe("success") | ||
expect((response.body as ValidBody).message).toBe(message) | ||
}) | ||
|
||
test("invalid", async () => { | ||
const response = await routes.validate.call({ | ||
message: 5 as any, | ||
}) | ||
expect(response.status).toBe("fail") | ||
expect((response as { message: string }).message).toBe("Validation failed") | ||
}) |
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
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
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