snapshot of https://github.com/ueberdosis/hocuspocus.git @ cf3cfa355b4d3bb61c6b6c80071f71783e32470e (2026-09-15)
Some checks failed
build / lint (22) (push) Failing after 17s
build / test (22) (push) Failing after 8s
build / lint (23) (push) Failing after 2s
build / build (22) (push) Has been skipped
build / build (23) (push) Has been skipped
build / test (23) (push) Failing after 4s
docsearch / docsearch (push) Failing after 1s
Some checks failed
build / lint (22) (push) Failing after 17s
build / test (22) (push) Failing after 8s
build / lint (23) (push) Failing after 2s
build / build (22) (push) Has been skipped
build / build (23) (push) Has been skipped
build / test (23) (push) Failing after 4s
docsearch / docsearch (push) Failing after 1s
This commit is contained in:
101
tests/server/onStateless.ts
Normal file
101
tests/server/onStateless.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import test from 'ava'
|
||||
import type { onStatelessPayload } from '@hocuspocus/server'
|
||||
import { newHocuspocus, newHocuspocusProvider } from '../utils/index.ts'
|
||||
|
||||
test('broadcast stateless message to all connections', async t => {
|
||||
await new Promise(async resolve => {
|
||||
const payloadToSend = 'STATELESS-MESSAGE'
|
||||
const server = await newHocuspocus(t, {
|
||||
onStateless: async ({ document }) => {
|
||||
await document.broadcastStateless(payloadToSend)
|
||||
},
|
||||
})
|
||||
|
||||
let count = 2
|
||||
const onStatelessCallback = (payload: string) => {
|
||||
t.is(payload, payloadToSend)
|
||||
count -= 1
|
||||
if (count === 0) {
|
||||
t.pass()
|
||||
resolve('done')
|
||||
}
|
||||
}
|
||||
|
||||
newHocuspocusProvider(t, server, { onStateless: ({ payload }) => onStatelessCallback(payload) })
|
||||
const provider = newHocuspocusProvider(t, server, {
|
||||
onStateless: ({ payload }) => onStatelessCallback(payload),
|
||||
onSynced: () => {
|
||||
provider.sendStateless(payloadToSend)
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('send a stateless message to a specific connection', async t => {
|
||||
await new Promise(async resolve => {
|
||||
const server = await newHocuspocus(t, {
|
||||
onStateless: async ({ connection }: onStatelessPayload) => {
|
||||
connection.sendStateless('This is a specific message.')
|
||||
},
|
||||
})
|
||||
|
||||
await newHocuspocusProvider(t, server, {
|
||||
onStateless: async () => {
|
||||
throw Error('The provider1 should not receive messages')
|
||||
},
|
||||
})
|
||||
|
||||
const provider = await newHocuspocusProvider(t, server, {
|
||||
onSynced: () => {
|
||||
provider.sendStateless('Send stateless message, and then a stateless message is will be received')
|
||||
},
|
||||
onStateless: () => {
|
||||
t.pass()
|
||||
resolve('done')
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('calls the onStateless hook', async t => {
|
||||
await new Promise(async resolve => {
|
||||
const payloadToSend = 'STATELESS-MESSAGE'
|
||||
class CustomExtension {
|
||||
async onStateless({ payload }: onStatelessPayload) {
|
||||
t.is(payload, payloadToSend)
|
||||
t.pass()
|
||||
resolve('done')
|
||||
}
|
||||
}
|
||||
|
||||
const server = await newHocuspocus(t, {
|
||||
extensions: [
|
||||
new CustomExtension(),
|
||||
],
|
||||
})
|
||||
|
||||
const provider = await newHocuspocusProvider(t, server, {
|
||||
onSynced: async () => {
|
||||
provider.sendStateless(payloadToSend)
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('the server actively sends a stateless message', async t => {
|
||||
const payloadToSend = 'STATELESS-MESSAGE'
|
||||
const server = await newHocuspocus(t)
|
||||
|
||||
await new Promise(resolve => {
|
||||
newHocuspocusProvider(t, server, {
|
||||
onSynced: async () => {
|
||||
server.documents.get('hocuspocus-test')?.broadcastStateless(payloadToSend)
|
||||
},
|
||||
onStateless: async ({ payload }) => {
|
||||
t.is(payload, payloadToSend)
|
||||
t.pass()
|
||||
resolve('done')
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user