Files
simplex-chat/packages/simplex-chat-nodejs/tests/api.test.ts
Evgeny 43aa3e7e8a nodejs: simplex-chat npm package (#5556)
* nodejs: addon

* rename

* changes

* change lib name

* package

* lib path

* simplex-chat-nodejs: fix library paths

* simplex-chat-nodejs: change addon name

* simplex-chat-nodejs: install libs, adjust package and installation

* simplex-chat-nodejs: add npmignore

* gitignore: add additional nodejs path

* simplex-chat-nodejs: fix shim name

* gitignore: ignore nodejs package lock

* simplex-chat-nodejs: rename shim to underscore

* simplex-chat-nodejs: fix library loading on Mac

* simplex-chat-nodejs: expose low-level functions, move tests

* simplex-chat-nodejs: expose shim fucntions

* simplex-chat-nodejs: fixed libs version

* simplex-chat-nodejs: switch to official repository

* simpelx-chat-nodejs: adjust release tag

* async addon, tests

* refactor, fixes

* high level chat api

* simplify cpp add-on - move logic to JS, fix API

* api for events, api test

* update @simplex-chat/types

* Revert "update @simplex-chat/types"

This reverts commit da3f89866f.

* change @simplex-chat/types version

* receiver for any events, wait with timeout

* low-level bot example

* typedoc

* network connection events

* declarative bot api

* readme, docs

* update docs

* update readme

* add liveMessage support

* allow passing welcome message as string

* @simplex-chat/webrtc-client 6.5.0-beta.3

* bot test

* concurrent connection in tests

* nodejs/download-libs: cleanup on version mismatch

* nodejs/download-libs: bump libs version

* do not handle signals in Haskell

* update bot examples

* flatten docs and use local links to code

* update readme

* 6.5.0-beta.4

* include more files in npm package, 6.5.0-beta.4.2

* .gitignore

---------

Co-authored-by: Avently <7953703+avently@users.noreply.github.com>
Co-authored-by: shum <github.shum@liber.li>
2026-01-14 21:42:21 +00:00

68 lines
3.3 KiB
TypeScript

import * as path from "path"
import * as fs from "fs"
import {CEvt, T} from "@simplex-chat/types"
import {api} from ".."
const CT = T.ChatType
describe("API tests (use preset servers)", () => {
const tmpDir = "./tests/tmp"
const alicePath = path.join(tmpDir, "alice")
const bobPath = path.join(tmpDir, "bob")
beforeEach(() => fs.mkdirSync(tmpDir, {recursive: true}))
afterEach(() => fs.rmSync(tmpDir, {recursive: true, force: true}))
it("should send/receive message", async () => {
// create users and start chat controllers
const alice = await api.ChatApi.init(alicePath)
const bob = await api.ChatApi.init(bobPath)
const servers: string[] = []
let eventCount = 0
alice.on("hostConnected" as CEvt.Tag, async ({transportHost}: any) => { servers.push(transportHost) })
alice.onAny(async () => { eventCount++ })
await expect(alice.apiGetActiveUser()).resolves.toBeUndefined()
const aliceUser = await alice.apiCreateActiveUser({displayName: "alice", fullName: ""})
await expect(alice.apiGetActiveUser()).resolves.toMatchObject(aliceUser)
await bob.apiCreateActiveUser({displayName: "bob", fullName: ""})
await alice.startChat()
await bob.startChat()
// connect via link
const link = await alice.apiCreateLink(aliceUser.userId)
await expect(bob.apiConnectActiveUser(link)).resolves.toBe(api.ConnReqType.Invitation)
const [bobContact, aliceContact] = await Promise.all([
(await alice.wait("contactConnected")).contact,
(await bob.wait("contactConnected")).contact
])
expect(bobContact).toMatchObject({profile: {displayName: "bob"}})
expect(aliceContact).toMatchObject({profile: {displayName: "alice"}})
// exchange messages
const isMessage = ({contactId}: T.Contact, msg: string) => (evt: CEvt.NewChatItems) =>
evt.chatItems.some(ci => ci.chatInfo.type === CT.Direct && ci.chatInfo.contact.contactId === contactId && ci.chatItem.meta.itemText === msg)
await alice.apiSendTextMessage([CT.Direct, bobContact.contactId], "hello")
await bob.wait("newChatItems", isMessage(aliceContact, "hello"))
await bob.apiSendTextMessage([CT.Direct, aliceContact.contactId], "hello too")
await alice.wait("newChatItems", isMessage(bobContact, "hello too"), 10000)
await alice.apiSendTextMessage([CT.Direct, bobContact.contactId], "how are you?")
await bob.wait("newChatItems", isMessage(aliceContact, "how are you?"))
await bob.apiSendTextMessage([CT.Direct, aliceContact.contactId], "ok, and you?")
await alice.wait("newChatItems", isMessage(bobContact, "ok, and you?"), 10000)
// no more messages
await expect(alice.wait("newChatItems", 500)).resolves.toBeUndefined()
await expect(bob.wait("newChatItems", 500)).resolves.toBeUndefined()
// delete contacts, stop chat controllers and close databases
await alice.apiDeleteChat(CT.Direct, bobContact.contactId)
await bob.wait("contactDeletedByContact")
await bob.apiDeleteChat(CT.Direct, aliceContact.contactId)
await alice.stopChat()
await bob.stopChat()
await alice.close()
await bob.close()
await expect(alice.startChat).rejects.toThrow()
await expect(bob.startChat).rejects.toThrow()
expect(servers.length).toBe(2)
expect(servers[0] !== servers[1]).toBe(true)
expect(eventCount > 0).toBe(true)
}, 30000)
})