Skip to content

Commit

Permalink
broadcast test
Browse files Browse the repository at this point in the history
  • Loading branch information
MeowningMaster committed May 22, 2024
1 parent bd6bde3 commit 177e367
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 26 deletions.
1 change: 1 addition & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ jobs:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1
- run: bun install
- run: bun nx run-many -t test
- run: bun scripts/version.ts
- run: bun nx run-many -t publish
6 changes: 6 additions & 0 deletions examples/tests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
"options": {
"cwd": "{projectRoot}"
}
},
"test": {
"command": "bun test",
"options": {
"cwd": "{projectRoot}"
}
}
}
}
64 changes: 64 additions & 0 deletions examples/tests/src/broadcast.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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 { type Broadcast, LocalBroadcastsManager, Wsx } from "@wsx/server"

let clientA: TestClient
let clientB: TestClient
let clientC: TestClient
let room: Broadcast

type Server = ReturnType<typeof Server>
const Server = () =>
new Wsx()
.event("/rec", { body: Type.String() })
.route("/sub", ({ ws }) => {
room.subscribe(ws)
})
.route(
"/pub",
({ body: message, events }) => {
events.rec.emit(message, { broadcast: room })
},
{ body: Type.String() },
)
.listen(0)

type TestClient = Awaited<ReturnType<typeof TestClient>>
const TestClient = (url: string) => Client<Server>(url)

beforeAll(async () => {
const wsx = Server()
const { port } = wsx.server!
const url = `ws://localhost:${port}`

clientA = await TestClient(url)
clientB = await TestClient(url)
clientC = await TestClient(url)

const broadcastManager = new LocalBroadcastsManager()
room = broadcastManager.topic("test")
})

test("valid", async () => {
const recMockA = mock((message: string) => message)
const recMockB = mock((message: string) => message)
const recMockC = mock((message: string) => message)

await clientA.routes.sub.call()
clientA.routes.rec.listen(({ body }) => recMockA(body))

await clientB.routes.sub.call()
clientB.routes.rec.listen(({ body }) => recMockB(body))

await clientC.routes.sub.call()
clientC.routes.rec.listen(({ body }) => recMockC(body))

const message = faker.lorem.sentence()
await clientB.routes.pub.call(message)

expect(recMockA).toBeCalledWith(message)
expect(recMockB).toBeCalledTimes(0)
expect(recMockC).toBeCalledWith(message)
})
16 changes: 8 additions & 8 deletions examples/tests/src/emit.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
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 { Client, type ClientType } from "@wsx/client"
import { Wsx } from "@wsx/server"

const onEmit = mock((message: string) => void message)

async function initialize() {
const wsx = new Wsx()
type Server = ReturnType<typeof Server>
const Server = () =>
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"]
let routes: ClientType<Server>
beforeAll(async () => {
const client = await initialize()
const wsx = Server()
const { port } = wsx.server!
const client = await Client<typeof wsx>(`ws://localhost:${port}`)
routes = client.routes
})

Expand Down
16 changes: 8 additions & 8 deletions examples/tests/src/rpc.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
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 { Client, type ClientType } from "@wsx/client"
import { Wsx } from "@wsx/server"

const onRpc = mock((message: string) => message)

async function initialize() {
const wsx = new Wsx()
type Server = ReturnType<typeof Server>
const Server = () =>
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"]
let routes: ClientType<Server>
beforeAll(async () => {
const client = await initialize()
const wsx = Server()
const { port } = wsx.server!
const client = await Client<typeof wsx>(`ws://localhost:${port}`)
routes = client.routes
})

Expand Down
18 changes: 8 additions & 10 deletions examples/tests/src/validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { beforeAll, expect, test } from "bun:test"
import { faker } from "@faker-js/faker"
import { type Static, Type } from "@sinclair/typebox"
import { Client } from "@wsx/client"
import { Client, type ClientType } from "@wsx/client"
import { Wsx } from "@wsx/server"

type ValidBody = Static<typeof ValidBody>
const ValidBody = Type.Object({ message: Type.String() })

async function initialize() {
const wsx = new Wsx()
type Server = ReturnType<typeof Server>
const Server = () =>
new Wsx()
.route("/validate", ({ body }) => body, {
body: ValidBody,
response: Type.Object({ message: Type.String() }),
})
.listen(0)

const { port } = wsx.server!

return await Client<typeof wsx>(`ws://localhost:${port}`)
}

let routes: Awaited<ReturnType<typeof initialize>>["routes"]
let routes: ClientType<Server>
beforeAll(async () => {
const client = await initialize()
const wsx = Server()
const { port } = wsx.server!
const client = await Client<typeof wsx>(`ws://localhost:${port}`)
routes = client.routes
})

Expand Down

0 comments on commit 177e367

Please sign in to comment.