Files
sh c8f20bcc91 core, libs: configurable queue size, library fixes (#7542)
* core: add chat_migrate_init_queue FFI export

* bots: fix BadgeServiceErrorCode API type

* nodejs: pass required command fields

* nodejs: fix migration error types

* nodejs: install libsimplex from SIMPLEX_LIBS_DIR

* nodejs: add queue size option

* nodejs: regenerate docs

* python: add queue size option

* bots: pass incognito in APIConnect

* nodejs: accept documented success responses

* python: accept documented success responses

* nodejs: dispatch each bot message once

* nodejs: fix startChat events loop lifecycle

* nodejs, python: parse multi-line bot commands

* nodejs: fix file buffer handling in addon

* nodejs: keep events loop when chat stop fails

* python: fix send_and_wait race, load lib off loop

* python: make queue size export optional

* nodejs: receive events on a dedicated thread

* nodejs: release haskell thread after receive

* python: receive on a dedicated thread per chat

* python: test receive shutdown order

* nodejs: one receive thread per chat controller

* nodejs: harden receiver shutdown and tests

* nodejs: stop chat before closing store

* python: stop chat before closing store

* nodejs, python: harden close regression and retry

* python: free results with ucrt on windows

* nodejs: enable c++ exceptions on mac and windows
2026-09-19 12:18:46 +01:00

51 lines
2.2 KiB
TypeScript

import {ChatEvent, T} from "@simplex-chat/types"
import * as api from "../src/api"
import {subscribeChatItems} from "../src/bot"
type Handler = (evt: ChatEvent) => Promise<void>
function fakeBot(): {bot: api.ChatApi, deliver: (items: T.AChatItem[]) => Promise<void>} {
let handler: Handler | undefined
const bot = {on: (_event: string, h: Handler) => { handler = h }} as unknown as api.ChatApi
const deliver = (chatItems: T.AChatItem[]) => handler!({type: "newChatItems", chatItems} as unknown as ChatEvent)
return {bot, deliver}
}
function item(type: "rcvMsgContent" | "sndMsgContent", text: string): T.AChatItem {
return {chatItem: {content: {type, msgContent: {type: "text", text}}}} as unknown as T.AChatItem
}
describe("subscribeChatItems", () => {
it("sends a known command only to its handler", async () => {
const {bot, deliver} = fakeBot()
const calls: string[] = []
subscribeChatItems(bot, async () => { calls.push("message") }, {help: async () => { calls.push("help") }})
await deliver([item("rcvMsgContent", "/help")])
expect(calls).toEqual(["help"])
})
it("sends an unknown command to the fallback handler", async () => {
const {bot, deliver} = fakeBot()
const calls: string[] = []
subscribeChatItems(bot, async () => { calls.push("message") }, {"": async (_ci, cmd) => { calls.push(`fallback:${cmd.keyword}`) }})
await deliver([item("rcvMsgContent", "/unknown")])
expect(calls).toEqual(["fallback:unknown"])
})
it("sends unhandled commands and text to onMessage", async () => {
const {bot, deliver} = fakeBot()
const calls: string[] = []
subscribeChatItems(bot, async (_ci, content) => { calls.push(`message:${(content as T.MsgContent & {text: string}).text}`) }, {help: async () => { calls.push("help") }})
await deliver([item("rcvMsgContent", "/unknown"), item("rcvMsgContent", "hello")])
expect(calls).toEqual(["message:/unknown", "message:hello"])
})
it("ignores sent items", async () => {
const {bot, deliver} = fakeBot()
const calls: string[] = []
subscribeChatItems(bot, async () => { calls.push("message") }, {help: async () => { calls.push("help") }})
await deliver([item("sndMsgContent", "/help"), item("sndMsgContent", "hello")])
expect(calls).toEqual([])
})
})