mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-22 22:08:21 +00:00
* bots, nodejs: document chat, connection, member contact and network commands, typed commands in library * bots, nodejs: restore apiConnect, wrap domain once, test all connect responses * nodejs: JSDoc for new methods * docs: plan for bot API commands and typed Node library methods * tests: channel prepared with a domain has a verified SimpleX name * docs: apply review fixes to the plan for bot API commands * docs: apply second review pass to the plan for bot API commands * docs: refer the bot API plan to the rewritten test commit * docs: record decisions in the plan for bot API commands * docs: plan tests of the generated Python command strings * python: test the generated strings of the documented commands * docs: plan for files, /leave and library methods in the MCP server * docs: wait at stop in the MCP plan * docs: library methods and /leave mechanics in the MCP plan * mcp: typed library methods for chat commands * docs: plan the message information command for bots * mcp: /leave CHAT owner command to leave a chat at once * docs: file servers, GIF and video choices decided in the MCP plan * docs: file mechanics in the MCP plan * mcp: files of received messages * bots, nodejs: message information with the file servers for bots * docs: plan the relay approval option of receiving files in Node * mcp: download of received files with owner approval * docs: name the xftp-server test helper in the files plan * nodejs: receive files without approving unknown relays * docs: plan the user servers command for bots * bots, nodejs: user servers and relays by operator for bots * nodejs: document the message information method * docs: plan the file upload progress event for bots * mcp: send files with owner approval, refusing private keys * bots: file upload progress event for bots * mcp: /leave template in the agent control chat menu * docs: sent copies are deleted for contacts too, at sndFileCompleteXFTP * bots: drop the file upload progress event * docs: drop the upload progress event plan * nodejs: document user servers and relay approval of received files * mcp: download cards name unknown file servers after the upload * docs: record the wait for the file description in the files plan * mcp: fixed wait at stop for cancelled cards, without /sql * docs: record decision C4, a fixed wait at stop, in the files plan * ci: test the MCP server package on pull requests * ci: state when the MCP workflow can be dispatched * ci: run the MCP server tests when its libraries change * mcp: log the errors of image previews and resizing * mcp: keep files in transfer in the data folder * mcp: restrict sent files by the real paths of refused folders * mcp: validate the file at the send by its inode and change time * mcp: limit ffmpeg and ffprobe to local files and no input commands * mcp: ask the owner for unknown file servers; read approvals at creation * mcp: name downloaded files by their ID; return paths of downloads only * mcp: end each download with a result for the agent and the card * mcp: show files above their sender's size limit as failed * mcp: refuse empty files, as without file and captions too long to send * mcp: tell the owner about failed uploads and unposted received files * mcp: delete resized copies left by the last run at start * mcp: name downloads and leaving in the help and greeting, align docs * mcp: compute the download path for received files only * mcp: send a /receive file to the owner under its name * mcp: keep files received without a download in the chat's folder * mcp: keep a left chat's files and delete them with /delete CHAT files * mcp: test that sent images are resized by the core * mcp: wait 30 minutes for a download's progress before cancelling it * mcp: end a download as deleted only when its message is not found * mcp: delete the copy of a /receive file whose post fails * mcp: count the quoted content in a message's length, as the core sends it * mcp: name received bytes, unknown-server downloads and left chat files plainly * mcp: test refused folders, file changes and download approvals * docs: align MCP server docs with refused folders, leaves and download waits * mcp: move received files only in full, delete partials only of downloads * mcp: report a received file missing from its folder as failed * mcp: move received files of a group's member chats into its folder * mcp: read a video's preview and duration as an MP4 or QuickTime file * mcp: 0.2.0 on simplex-chat 7.1.0-beta.10.1 * mcp: file folders by chat type, /delete cancels downloads, files reply * docs: MCP server specs and plan for 0.2.0 * mcp: /receive gives the agent a file, /forward sends it to the owner * docs: MCP server specs for /receive and /forward of download cards * docs: MCP plan records the temp folder command as a library exception * mcp: /delete cancels resumed downloads; /forward quote fits the limit * docs: MCP specs and plans for /forward quotes and resumed downloads * docs: MCP specs name the source commit they are checked against * docs: MCP PLAN states the /forward quote and move rules * mcp: a download moves its completed file whatever its size * mcp: /receive N all covers its card and later files, not pending ones * docs: MCP files approval scope, reply text and README server wording * mcp: a download approves relays only for a card naming unknown servers * mcp: files in member chats of a group are tracked as in the main chat * docs: MCP relay approval of downloads and files in member chats * mcp: a rejected file gets a card again under the files approval * docs: MCP rejected files under the files approval, README scope * mcp: an offered file without a description gets no card or action * docs: MCP a file without a description is not downloaded * mcp: 0.2.0 on simplex-chat 7.1.0-beta.10.2 * ci: test the MCP server as a scope of build-linux
985 lines
60 KiB
TypeScript
985 lines
60 KiB
TypeScript
import {existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, writeFileSync} from "fs"
|
|
import {tmpdir} from "os"
|
|
import {basename, dirname, join} from "path"
|
|
import {CallToolResult} from "@modelcontextprotocol/server"
|
|
import {ChatEvent, T} from "@simplex-chat/types"
|
|
import {core} from "simplex-chat"
|
|
import {Action, Actions, Mode, NewAction, ToolError} from "../../src/actions"
|
|
import {Control, cardText, parseCommand} from "../../src/control"
|
|
import {ChatItemsWatch, Collected, utcMinutes} from "../../src/messages"
|
|
import {OwnerEntry, waitForCancelledCards} from "../../src/serve"
|
|
import {AgentTools} from "../../src/tools"
|
|
import {FakeChat, actionsWith, contact, data, direct, event, fakeHooks, fileItem, group, groupChat, item, member, settled, signal, user} from "./fixtures"
|
|
|
|
beforeEach(() => {
|
|
jest.spyOn(process.stdout, "write").mockImplementation(() => true)
|
|
})
|
|
|
|
afterEach(() => jest.restoreAllMocks())
|
|
|
|
afterAll(() => rmSync(data, {recursive: true, force: true}))
|
|
|
|
const commandsReply = "Commands: /approve, /reject, /receive, /forward, /revoke, /leave, /delete, /pending, /list. Each card shows its commands."
|
|
|
|
const alice = member(3, "Alice", T.GroupMemberRole.Owner)
|
|
const bob = member(7, "Bob", T.GroupMemberRole.Member)
|
|
const self = member(1, "agent", T.GroupMemberRole.Owner, T.GroupMemberStatus.Creator)
|
|
|
|
function fields(chat = "@4", text = "hi"): NewAction {
|
|
return {
|
|
tool: "send",
|
|
input: {chat, text},
|
|
subject: {kind: "bot", name: "SimpleX Directory", incognito: false},
|
|
chat,
|
|
mentions: [],
|
|
responses: new Collected(),
|
|
}
|
|
}
|
|
|
|
function agentChats(): FakeChat {
|
|
const chat = new FakeChat()
|
|
chat.members.set(10, [self, alice, bob])
|
|
chat.chats.set("@4", {chatInfo: direct(contact(4, "SimpleX Directory", {peerType: "bot"})), items: []})
|
|
chat.chats.set("#2", {chatInfo: groupChat(group(2, "ep", member(1, "agent"), {businessChat: {}})), items: []})
|
|
chat.chats.set("#10", {chatInfo: groupChat(group(10, "agent")), items: []})
|
|
return chat
|
|
}
|
|
|
|
async function controlWith(chat: FakeChat, actions: Actions, owners?: OwnerEntry[], mode: Mode = "requests"): Promise<{control: Control; tools: AgentTools}> {
|
|
const tools = new AgentTools({chat: chat.api(), user, actions, watch: new ChatItemsWatch(), controlChat: () => "#10", standingApprover: (chatInfo, standing) => control.standingApprover(chatInfo, standing), refusedFile: (path, chatInfo, chat) => control.refusedFile(path, chatInfo, chat), data}, mode)
|
|
const control: Control = new Control(chat.api(), group(10, "agent"), {displayName: "agent", owners, registerHint: "hint", qrCode: "small"}, actions, tools)
|
|
await control.load()
|
|
return {control, tools}
|
|
}
|
|
|
|
async function setup(owners?: OwnerEntry[]) {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake, 5)
|
|
const chat = agentChats()
|
|
const {control} = await controlWith(chat, actions, owners)
|
|
const action = actions.create("k", fields())
|
|
await settled()
|
|
return {fake, actions, chat, control, action}
|
|
}
|
|
|
|
function customDataOf(chat: FakeChat, ref: string): object | undefined {
|
|
const {chatInfo} = chat.chats.get(ref)!
|
|
return chatInfo.type === "direct" ? chatInfo.contact.customData : (chatInfo as T.ChatInfo.Group).groupInfo.customData
|
|
}
|
|
|
|
function chatInfoOf(chat: FakeChat, ref: string): T.ChatInfo {
|
|
return chat.chats.get(ref)!.chatInfo
|
|
}
|
|
|
|
describe("parseCommand", () => {
|
|
it("reads one-line commands", () => {
|
|
expect(parseCommand("/approve 5")).toEqual({keyword: "approve", params: "5"})
|
|
expect(parseCommand(" /reject 5 ")).toEqual({keyword: "reject", params: "5"})
|
|
expect(parseCommand("/pending\n")).toEqual({keyword: "pending", params: ""})
|
|
expect(parseCommand("/approve #2 all")).toEqual({keyword: "approve", params: "#2 all"})
|
|
expect(parseCommand("/approve 5\nnow")).toBeUndefined()
|
|
expect(parseCommand("approve 5")).toBeUndefined()
|
|
expect(parseCommand("ok /approve 5")).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe("owner commands", () => {
|
|
it("approves and rejects pending actions", async () => {
|
|
const {control, action, fake} = await setup()
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "5"})).toBeUndefined()
|
|
expect(action).toMatchObject({state: "performing", decidedBy: "Alice"})
|
|
expect(fake.calls).toContain("perform 5")
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "5"})).toBe("Action 5 is not pending.")
|
|
})
|
|
|
|
it("rejects", async () => {
|
|
const {control, action} = await setup()
|
|
expect(await control.ownerCommand(alice, {keyword: "reject", params: "5"})).toBeUndefined()
|
|
expect(action).toMatchObject({state: "rejected", decidedBy: "Alice"})
|
|
})
|
|
|
|
it("approves and rejects an action named with its chat", async () => {
|
|
const {control, actions, action} = await setup()
|
|
const connect = actions.create("c", {tool: "connect", input: {target: "@directory.simplex"}, subject: {kind: "bot", name: "SimpleX Directory", incognito: false}, plan: {cardKind: "bot", type: "contact", takesMessage: true}, messages: new Collected()})
|
|
await settled()
|
|
expect(await control.ownerCommand(alice, {keyword: "reject", params: "#2 5"})).toBe("Action 5 is not for #2.")
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "@4 6"})).toBe("Action 6 is not for @4.")
|
|
expect(action.state).toBe("pending")
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "@4 5"})).toBeUndefined()
|
|
expect(action).toMatchObject({state: "performing", decidedBy: "Alice"})
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "6"})).toBeUndefined()
|
|
expect(connect.state).toBe("performing")
|
|
})
|
|
|
|
it("expires due actions before a command", async () => {
|
|
const {control, action} = await setup()
|
|
jest.spyOn(Date, "now").mockReturnValue(action.expires)
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "5"})).toBe("Action 5 is not pending.")
|
|
expect(action.state).toBe("expired")
|
|
})
|
|
|
|
it("refuses unknown numbers and numbers from an earlier run", async () => {
|
|
const {control} = await setup()
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "4"})).toBe("Action 4 is not pending.")
|
|
expect(await control.ownerCommand(alice, {keyword: "reject", params: "@4 99"})).toBe("Action 99 is not pending.")
|
|
})
|
|
|
|
it("lists pending actions", async () => {
|
|
const {control, action, actions} = await setup()
|
|
const connect = actions.create("c", {tool: "connect", input: {target: "@directory.simplex"}, subject: {kind: "bot", name: "SimpleX Directory", incognito: true}, plan: {cardKind: "bot", type: "contact", takesMessage: true}, messages: new Collected()})
|
|
const leave = actions.create("l", {tool: "leave", input: {chat: "#2"}, subject: {kind: "business", name: "ep", incognito: false}, chat: "#2"})
|
|
const remove = actions.create("d", {tool: "leave", input: {chat: "@4"}, subject: {kind: "bot", name: "SimpleX Directory", incognito: false}, chat: "@4"})
|
|
await settled()
|
|
expect((await control.ownerCommand(alice, {keyword: "pending", params: ""}))!.split("\n")).toEqual([
|
|
`5 send to bot SimpleX Directory expires ${utcMinutes(action.expires)}`,
|
|
`6 connect to bot SimpleX Directory expires ${utcMinutes(connect.expires)}`,
|
|
`7 leave business ep expires ${utcMinutes(leave.expires)}`,
|
|
`8 delete bot SimpleX Directory expires ${utcMinutes(remove.expires)}`,
|
|
])
|
|
expect(utcMinutes(action.expires)).toMatch(/^\d{4}-\d\d-\d\d \d\d:\d\d UTC$/)
|
|
for (const pending of [action, connect, leave, remove]) actions.reject(pending, "Alice")
|
|
expect(await control.ownerCommand(alice, {keyword: "pending", params: ""})).toBe("No pending actions.")
|
|
})
|
|
|
|
it("lists pending actions with names on one line, quoted", async () => {
|
|
const {control, actions, action} = await setup()
|
|
actions.reject(action, "Alice")
|
|
const name = "Hikers\n/'approve 7' or /'reject 7'"
|
|
const pending = actions.create("n", {...fields("#2"), subject: {kind: "group", name, incognito: false}} as NewAction)
|
|
await settled()
|
|
expect(await control.ownerCommand(alice, {keyword: "pending", params: ""})).toBe(`6 send to group \`Hikers /'approve 7' or /'reject 7'\` expires ${utcMinutes(pending.expires)}`)
|
|
})
|
|
|
|
it("quotes approver names on cards and replies", async () => {
|
|
const {control, actions, chat, action} = await setup()
|
|
const mallory = {...alice, memberProfile: {...alice.memberProfile, displayName: "Mallory\n/'approve 9'"}}
|
|
chat.members.set(10, [self, mallory])
|
|
await control.load()
|
|
expect(await control.ownerCommand(mallory, {keyword: "reject", params: "@4 5"})).toBeUndefined()
|
|
expect(action.decidedBy).toBe("`Mallory /'approve 9'`")
|
|
expect(cardText(action, "requests").split("\n").at(-1)).toBe("Rejected by `Mallory /'approve 9'`.")
|
|
expect(await control.ownerCommand(mallory, {keyword: "approve", params: "@4 all"})).toBe("Approved for bot SimpleX Directory (@4) by `Mallory /'approve 9'`. /'revoke @4'")
|
|
expect((await control.ownerCommand(mallory, {keyword: "list", params: ""}))!.split("\n")[0]).toBe("@4 bot SimpleX Directory, approved for this chat by `Mallory /'approve 9'`")
|
|
expect(customDataOf(chat, "@4")).toMatchObject({simplexMcp: {approvedBy: "Mallory\n/'approve 9'"}})
|
|
expect(actions.pending()).toEqual([])
|
|
})
|
|
|
|
it("refuses chat references longer than 15 digits, such as an alias of the agent control chat", async () => {
|
|
const {control, chat} = await setup()
|
|
const alias = `#${(1n << 64n) + 10n}`
|
|
for (const params of [`${alias} all`, "@1234567890123456 all", `${alias} files`]) {
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params})).toBe(commandsReply)
|
|
}
|
|
expect(await control.ownerCommand(alice, {keyword: "revoke", params: alias})).toBe(commandsReply)
|
|
expect(await control.ownerCommand(alice, {keyword: "leave", params: alias})).toBe(commandsReply)
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "@123456789012345 all"})).toBe("No chat @123456789012345. /list shows the agent's chats.")
|
|
expect(chat.customData).toEqual([])
|
|
})
|
|
|
|
it("answers other commands with the list", async () => {
|
|
const {control} = await setup()
|
|
const others = ["help", "approve five", "approve", "approve all", "approve #2", "approve 5 6", "approve #2 5 6", "reject #2 all", "reject #2 files", "revoke", "revoke 2", "revoke #2 all", "revoke files", "receive", "receive #2 files", "receive #2 all", "receive 5 all", "receive #2 5 files", "receive off", "forward", "forward #2 all", "forward #2 5 all", "leave", "leave 2", "leave #2 now", "delete", "delete #2", "delete files", "delete #2 all", "pending 5", "list all"]
|
|
for (const text of others) {
|
|
expect(await control.ownerCommand(bob, parseCommand(`/${text}`)!)).toBe(commandsReply)
|
|
}
|
|
})
|
|
|
|
it("accepts only approvers", async () => {
|
|
const unlisted = await setup()
|
|
for (const params of ["5", "@4 all"]) expect(await unlisted.control.ownerCommand(bob, {keyword: "approve", params})).toBe("Only owners approve actions.")
|
|
expect(await unlisted.control.ownerCommand(bob, {keyword: "revoke", params: "@4"})).toBe("Only owners approve actions.")
|
|
expect(await unlisted.control.ownerCommand(bob, {keyword: "list", params: ""})).toBe("Only owners approve actions.")
|
|
expect(unlisted.chat.customData).toEqual([])
|
|
const listed = await setup([{memberId: 3, name: "Alice"}])
|
|
expect(await listed.control.ownerCommand(member(8, "Carol", T.GroupMemberRole.Owner), {keyword: "approve", params: "5"})).toBe("Only the owners listed in --owners approve actions.")
|
|
expect(await listed.control.ownerCommand(member(8, "Carol", T.GroupMemberRole.Owner), {keyword: "approve", params: "@4 all"})).toBe("Only the owners listed in --owners approve actions.")
|
|
expect(await listed.control.ownerCommand(alice, {keyword: "pending", params: ""})).toMatch(/^5 {2}send/)
|
|
const none = await setup([])
|
|
expect(await none.control.ownerCommand(alice, {keyword: "approve", params: "5"})).toBe("Only the owners listed in --owners approve actions.")
|
|
})
|
|
|
|
it("accepts a listed owner by the name on one line, as the restart line prints it", async () => {
|
|
const chat = agentChats()
|
|
const ana = member(3, "Ana\nLu, Jr", T.GroupMemberRole.Owner)
|
|
chat.members.set(10, [self, ana])
|
|
const {control} = await controlWith(chat, actionsWith("requests", fakeHooks()), [{memberId: 3, name: "Ana Lu, Jr"}])
|
|
await control.start()
|
|
expect(await control.ownerCommand(ana, {keyword: "pending", params: ""})).toBe("No pending actions.")
|
|
})
|
|
|
|
it("replies in the agent control chat to commands only", async () => {
|
|
const {control, chat} = await setup()
|
|
const chatInfo = groupChat(group(10, "agent"))
|
|
control.onMessage(event(chatInfo, item(1, "/approve 4", {type: "groupRcv", groupMember: alice})))
|
|
control.onMessage(event(chatInfo, item(2, "thanks", {type: "groupRcv", groupMember: alice})))
|
|
control.onMessage(event(chatInfo, item(3, "/approve 5\nplease", {type: "groupRcv", groupMember: alice})))
|
|
control.onMessage(event(chatInfo, item(4, "/approve @4 all", {type: "groupRcv", groupMember: alice})))
|
|
control.onMessage(event(chatInfo, item(5, "/revoke @4", {type: "groupRcv", groupMember: alice})))
|
|
await new Promise(resolve => setImmediate(resolve))
|
|
expect(chat.replies).toEqual(["Action 4 is not pending.", "Approved for bot SimpleX Directory (@4) by Alice. /'revoke @4'", "Messages to bot SimpleX Directory (@4) need approval again."])
|
|
})
|
|
})
|
|
|
|
describe("approval of all messages to a chat", () => {
|
|
it("records the approver, keeps other custom data, and approves the chat's pending sends", async () => {
|
|
const {control, actions, chat, action} = await setup()
|
|
const {contact: directory} = chatInfoOf(chat, "@4") as T.ChatInfo.Direct
|
|
directory.customData = {other: 1}
|
|
const second = actions.create("k2", fields("@4", "second"))
|
|
const other = actions.create("k3", fields("#2"))
|
|
const leave = actions.create("k4", {tool: "leave", input: {chat: "@4"}, subject: {kind: "bot", name: "SimpleX Directory", incognito: false}, chat: "@4"})
|
|
await settled()
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "@4 all"})).toBe("Approved for bot SimpleX Directory (@4) by Alice. /'revoke @4'")
|
|
expect(customDataOf(chat, "@4")).toEqual({other: 1, simplexMcp: {approvedBy: "Alice", approvedById: 3, approvedAt: expect.stringMatching(/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$/)}})
|
|
expect([action, second, other, leave].map(({state, decidedBy}) => [state, decidedBy])).toEqual([["performing", "Alice"], ["performing", "Alice"], ["pending", undefined], ["pending", undefined]])
|
|
expect(control.standingApprover(chatInfoOf(chat, "@4"), "messages")).toBe("Alice")
|
|
expect(control.standingApprover(chatInfoOf(chat, "#2"), "messages")).toBeUndefined()
|
|
})
|
|
|
|
it("approves a group chat and refuses the agent control chat and unknown chats", async () => {
|
|
const {control, chat} = await setup()
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "#2 all"})).toBe("Approved for business ep (#2) by Alice. /'revoke #2'")
|
|
expect(customDataOf(chat, "#2")).toMatchObject({simplexMcp: {approvedBy: "Alice", approvedById: 3}})
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "#10 all"})).toBe("No chat #10. /list shows the agent's chats.")
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "@99 all"})).toBe("No chat @99. /list shows the agent's chats.")
|
|
expect(chat.customData.map(({chat: ref}) => ref)).toEqual(["#2"])
|
|
})
|
|
|
|
it("revokes the approval and keeps other custom data", async () => {
|
|
const {control, chat} = await setup()
|
|
const {contact: directory} = chatInfoOf(chat, "@4") as T.ChatInfo.Direct
|
|
directory.customData = {other: 1}
|
|
await control.ownerCommand(alice, {keyword: "approve", params: "@4 all"})
|
|
expect(await control.ownerCommand(alice, {keyword: "revoke", params: "@4"})).toBe("Messages to bot SimpleX Directory (@4) need approval again.")
|
|
expect(customDataOf(chat, "@4")).toEqual({other: 1})
|
|
expect(control.standingApprover(chatInfoOf(chat, "@4"), "messages")).toBeUndefined()
|
|
expect(await control.ownerCommand(alice, {keyword: "revoke", params: "@4"})).toBe("Messages to @4 have no standing approval.")
|
|
expect(await control.ownerCommand(alice, {keyword: "revoke", params: "#10"})).toBe("Messages to #10 have no standing approval.")
|
|
expect(await control.ownerCommand(alice, {keyword: "revoke", params: "#99"})).toBe("Messages to #99 have no standing approval.")
|
|
expect(chat.customData.map(({chat: ref}) => ref)).toEqual(["@4", "@4"])
|
|
})
|
|
|
|
it("applies only while the approving member is an approver", async () => {
|
|
const chat = agentChats()
|
|
const actions = actionsWith("requests", fakeHooks())
|
|
const {contact: directory} = chatInfoOf(chat, "@4") as T.ChatInfo.Direct
|
|
directory.customData = {simplexMcp: {approvedBy: "Alice", approvedById: 3, approvedAt: "2026-10-18T10:00:00.000Z"}}
|
|
const {control} = await controlWith(chat, actions)
|
|
expect(control.standingApprover(chatInfoOf(chat, "@4"), "messages")).toBe("Alice")
|
|
chat.members.set(10, [self, {...alice, memberRole: T.GroupMemberRole.Admin}, bob])
|
|
await control.load()
|
|
expect(control.standingApprover(chatInfoOf(chat, "@4"), "messages")).toBeUndefined()
|
|
chat.members.set(10, [self, bob])
|
|
await control.load()
|
|
expect(control.standingApprover(chatInfoOf(chat, "@4"), "messages")).toBeUndefined()
|
|
chat.members.set(10, [self, alice, {...bob, memberRole: T.GroupMemberRole.Owner}])
|
|
expect((await controlWith(chat, actions, [{memberId: 3, name: "Alice"}])).control.standingApprover(chatInfoOf(chat, "@4"), "messages")).toBe("Alice")
|
|
expect((await controlWith(chat, actions, [{memberId: 7, name: "Bob"}])).control.standingApprover(chatInfoOf(chat, "@4"), "messages")).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
function downloadFields(chat = "#2", file: object = {}): NewAction {
|
|
return {
|
|
tool: "download",
|
|
input: {chat, item: 30},
|
|
subject: {kind: "business", name: "ep", incognito: false},
|
|
chat,
|
|
item: 30,
|
|
from: "Bob",
|
|
file: {fileId: 7, name: "report.pdf", size: 2400000, type: "file", ...file},
|
|
}
|
|
}
|
|
|
|
describe("receiving all files of a chat", () => {
|
|
it("receives a download and the later ones of its chat, apart from its messages and other pending downloads, until /receive CHAT off", async () => {
|
|
const {control, actions, chat} = await setup()
|
|
const {contact: directory} = chatInfoOf(chat, "@4") as T.ChatInfo.Direct
|
|
directory.customData = {other: 1}
|
|
const download = actions.create("d1", downloadFields("@4"))
|
|
const second = actions.create("d2", {...downloadFields("@4", {fileId: 8}), item: 31} as NewAction)
|
|
await settled()
|
|
expect(await control.ownerCommand(alice, {keyword: "receive", params: `@4 ${download.n} all`})).toBe("Files approved for bot SimpleX Directory (@4) by Alice. /'receive @4 off'")
|
|
expect([download, second].map(({state, decidedBy}) => [state, decidedBy])).toEqual([["performing", "Alice"], ["pending", undefined]])
|
|
expect(actions.get(5)).toMatchObject({state: "pending"})
|
|
expect(customDataOf(chat, "@4")).toEqual({other: 1, simplexMcpFiles: {approvedBy: "Alice", approvedById: 3, approvedAt: expect.any(String)}})
|
|
expect([control.standingApprover(chatInfoOf(chat, "@4"), "files"), control.standingApprover(chatInfoOf(chat, "@4"), "messages")]).toEqual(["Alice", undefined])
|
|
await control.ownerCommand(alice, {keyword: "approve", params: "@4 all"})
|
|
expect((await control.ownerCommand(alice, {keyword: "list", params: ""}))!.split("\n")[0]).toBe("@4 bot SimpleX Directory, approved for this chat by Alice, files approved by Alice")
|
|
expect(await control.ownerCommand(alice, {keyword: "receive", params: "@4 off"})).toBe("Files from bot SimpleX Directory (@4) need approval again.")
|
|
expect(customDataOf(chat, "@4")).toEqual({other: 1, simplexMcp: {approvedBy: "Alice", approvedById: 3, approvedAt: expect.any(String)}})
|
|
expect(await control.ownerCommand(alice, {keyword: "receive", params: "@4 off"})).toBe("Files from @4 have no standing approval.")
|
|
expect(await control.ownerCommand(alice, {keyword: "revoke", params: "@4"})).toBe("Messages to bot SimpleX Directory (@4) need approval again.")
|
|
expect(customDataOf(chat, "@4")).toEqual({other: 1})
|
|
})
|
|
|
|
it("receives the named download also from unknown servers, and each named download only", async () => {
|
|
const {control, actions} = await setup()
|
|
const known = actions.create("d1", downloadFields("@4"))
|
|
const unknown = actions.create("d2", {...downloadFields("@4", {fileId: 8}), item: 31, unknownServers: ["xftp://hash2@xftp.example.com"]} as NewAction)
|
|
await settled()
|
|
const reply = "Files approved for bot SimpleX Directory (@4) by Alice. /'receive @4 off'"
|
|
expect(await control.ownerCommand(alice, {keyword: "receive", params: `@4 ${known.n} all`})).toBe(reply)
|
|
expect([known.state, unknown.state]).toEqual(["performing", "pending"])
|
|
expect(await control.ownerCommand(alice, {keyword: "receive", params: `@4 ${unknown.n} all`})).toBe(reply)
|
|
expect(unknown).toMatchObject({state: "performing", decidedBy: "Alice"})
|
|
})
|
|
|
|
it("leaves a download that stops being pending while the approval is stored", async () => {
|
|
const {control, actions, chat} = await setup()
|
|
const download = actions.create("d1", downloadFields("@4"))
|
|
await settled()
|
|
const store = chat.apiSetContactCustomData.bind(chat)
|
|
chat.apiSetContactCustomData = async (contactId, customData) => {
|
|
actions.reject(download, "Bob")
|
|
return store(contactId, customData)
|
|
}
|
|
await control.ownerCommand(alice, {keyword: "receive", params: `@4 ${download.n} all`})
|
|
expect(download).toMatchObject({state: "rejected", decidedBy: "Bob"})
|
|
})
|
|
|
|
it("takes /receive and /forward for downloads only, and /approve for the other actions only", async () => {
|
|
const {control, actions, action, chat} = await setup()
|
|
const download = actions.create("d1", downloadFields("@4"))
|
|
await settled()
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: `@4 ${download.n}`})).toBe(`Action ${download.n} is a download: /'receive @4 ${download.n}', /'receive @4 ${download.n} all', /'forward @4 ${download.n}' or /'reject @4 ${download.n}'.`)
|
|
for (const params of [`${action.n}`, `@4 ${action.n}`]) {
|
|
expect(await control.ownerCommand(alice, {keyword: "receive", params})).toBe(`Action ${action.n} is not a download.`)
|
|
expect(await control.ownerCommand(alice, {keyword: "forward", params})).toBe(`Action ${action.n} is not a download.`)
|
|
}
|
|
expect(await control.ownerCommand(alice, {keyword: "receive", params: `@4 ${action.n} all`})).toBe(`Action ${action.n} is not a download.`)
|
|
expect([action.state, download.state]).toEqual(["pending", "pending"])
|
|
expect(await control.ownerCommand(alice, {keyword: "receive", params: `@4 ${download.n}`})).toBeUndefined()
|
|
expect(download).toMatchObject({state: "performing", decidedBy: "Alice"})
|
|
expect(chat.customData).toEqual([])
|
|
})
|
|
|
|
it("answers /approve CHAT files and /revoke CHAT files with the command list", async () => {
|
|
const {control, chat} = await setup()
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "@4 files"})).toBe(commandsReply)
|
|
expect(await control.ownerCommand(alice, {keyword: "revoke", params: "@4 files"})).toBe(commandsReply)
|
|
expect(chat.customData).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe("downloads in the agent control chat", () => {
|
|
const preview = `data:image/jpg;base64,${"A".repeat(14000)}`
|
|
const flushed = (): Promise<void> => new Promise(resolve => setImmediate(resolve))
|
|
|
|
afterEach(() => {
|
|
rmSync(join(data, "forwarded"), {recursive: true, force: true})
|
|
rmSync(join(data, "files", "group-2"), {recursive: true, force: true})
|
|
})
|
|
|
|
async function downloadSetup(file: object = {}) {
|
|
const fake = fakeHooks()
|
|
const actions: Actions = new Actions("requests", 23, 1, {...fake.hooks, refusal: action => control.refusal(action), postCard: action => control.postCard(action), outcome: action => control.outcome(action)})
|
|
const chat = agentChats()
|
|
const {control, tools} = await controlWith(chat, actions)
|
|
const action = actions.create("d", downloadFields("#2", file))
|
|
await flushed()
|
|
const cards = chat.sent.filter(({chat: [, id]}) => id === 10).map(({messages: [message]}) => message)
|
|
return {fake, actions, chat, control, tools, action, cards}
|
|
}
|
|
|
|
it("posts a file card with the sender, the name and the size, and edits it at the outcome", async () => {
|
|
const {actions, chat, action, cards} = await downloadSetup()
|
|
expect(cards.map(({msgContent}) => msgContent)).toEqual([
|
|
{
|
|
type: "text",
|
|
text: "I want to download a file from Bob in business ep (#2):\n`report.pdf`, 2.4 MB\n/'receive #2 1', /'receive #2 1 all', /'forward #2 1' or /'reject #2 1'",
|
|
},
|
|
])
|
|
actions.reject(action, "Alice")
|
|
await flushed()
|
|
expect(chat.updates.at(-1)).toEqual({chatId: 10, itemId: action.card, text: "I want to download a file from Bob in business ep (#2):\n`report.pdf`, 2.4 MB\nRejected by Alice."})
|
|
})
|
|
|
|
it("shows the sender's preview on an image card, resized to fit the card, or a text card when it can't be resized", async () => {
|
|
const small = "data:image/jpg;base64,/9j/small"
|
|
const fits = await downloadSetup({type: "image", preview: small})
|
|
expect(fits.cards[0].msgContent).toMatchObject({type: "image", image: small, text: expect.stringMatching(/^I want to download a file from Bob/)})
|
|
const resize = jest.spyOn(core, "chatResizeImageUri").mockResolvedValue(small)
|
|
const resized = await downloadSetup({type: "video", preview, duration: 42})
|
|
expect(resized.cards[0].msgContent).toMatchObject({type: "video", image: small, duration: 42})
|
|
const [[bytes, budget]] = resize.mock.calls
|
|
expect(Buffer.from(bytes as Uint8Array).toString("base64")).toBe("A".repeat(14000))
|
|
expect(budget).toBeLessThan(14000)
|
|
resize.mockRejectedValue(new Error("image cannot be decoded or does not fit maxSize"))
|
|
const errors = jest.spyOn(process.stderr, "write").mockImplementation(() => true)
|
|
const text = await downloadSetup({type: "image", preview})
|
|
expect(text.cards[0].msgContent.type).toBe("text")
|
|
expect(errors).toHaveBeenCalledWith("action 1 card image: image cannot be decoded or does not fit maxSize\n")
|
|
})
|
|
|
|
it("posts no card for a download in mode host or under the files approval", async () => {
|
|
for (const mode of ["requests", "host"] as const) {
|
|
const chat = agentChats()
|
|
const {contact: directory} = chatInfoOf(chat, "@4") as T.ChatInfo.Direct
|
|
directory.customData = {simplexMcpFiles: {approvedBy: "Alice", approvedById: 3, approvedAt: "2026-10-30T10:00:00.000Z"}}
|
|
const fake = fakeHooks()
|
|
const actions: Actions = new Actions(mode, 23, 1, {...fake.hooks, refusal: action => control.refusal(action), postCard: action => control.postCard(action), outcome: action => control.outcome(action)})
|
|
const {control} = await controlWith(chat, actions, undefined, mode)
|
|
const action = actions.create("d", downloadFields("@4"), control.standingApprover(chatInfoOf(chat, "@4"), "files"))
|
|
await flushed()
|
|
fake.performs[0].resolve()
|
|
await flushed()
|
|
expect(action).toMatchObject({state: "done", standing: mode === "requests"})
|
|
expect(chat.sent).toEqual([])
|
|
}
|
|
})
|
|
|
|
it("forwards the file into the agent control chat as a reply to its card, which stays pending, and keeps it outside the chat's folder", async () => {
|
|
const {chat, tools, action, cards, control} = await downloadSetup()
|
|
const forwarded = join(data, "forwarded", "group-2", "7.pdf")
|
|
const offered = fileItem(30, {type: "file", text: "report"}, {type: "rcvInvitation"}, {chatDir: {type: "groupRcv", groupMember: bob}})
|
|
chat.chats.get("#2")!.items.push(offered)
|
|
chat.responses.set(`/freceive 7 ${forwarded}`, {type: "rcvFileAccepted", user, chatItem: event(chatInfoOf(chat, "#2"), offered)})
|
|
expect(await control.ownerCommand(alice, {keyword: "forward", params: `#2 ${action.n}`})).toBeUndefined()
|
|
await flushed()
|
|
writeFileSync(forwarded, "report")
|
|
tools.onFile(event(chatInfoOf(chat, "#2"), fileItem(30, {type: "file", text: "report"}, {type: "rcvComplete"}, {fileSource: {filePath: forwarded}})))
|
|
while (chat.sent.length < 2) await flushed()
|
|
const copy = chat.sent[1].messages[0].fileSource!.filePath
|
|
expect(chat.sent[1]).toEqual({chat: [T.ChatType.Group, 10], messages: [{fileSource: {filePath: copy}, msgContent: {type: "file", text: ""}, quotedItemId: action.card, mentions: {}}]})
|
|
expect([dirname(dirname(copy)), basename(copy), readFileSync(copy, "utf8")]).toEqual([join(data, "files", "sent"), "report.pdf", "report"])
|
|
expect([existsSync(forwarded), existsSync(join(data, "files", "group-2", "7.pdf"))]).toEqual([true, false])
|
|
expect(action.state).toBe("pending")
|
|
expect(cards).toHaveLength(1)
|
|
})
|
|
|
|
it("forwards an image with the sender's preview, as a reply to its card only when the message fits", async () => {
|
|
jest.spyOn(core, "chatResizeImageUri").mockResolvedValue("data:image/jpg;base64,/9j/small")
|
|
for (const [length, quoted] of [[14000, true], [14800, false]] as const) {
|
|
const image = `data:image/jpg;base64,${"A".repeat(length)}`
|
|
const {chat, action, control} = await downloadSetup({type: "image", preview: image})
|
|
const path = join(data, "files", "group-2", "7.pdf")
|
|
mkdirSync(dirname(path), {recursive: true})
|
|
writeFileSync(path, "image")
|
|
chat.chats.get("#2")!.items.push(fileItem(30, {type: "image", text: "", image}, {type: "rcvComplete"}, {chatDir: {type: "groupRcv", groupMember: bob}, fileSource: {filePath: path}}))
|
|
await control.ownerCommand(alice, {keyword: "forward", params: `${action.n}`})
|
|
while (chat.sent.length < 2) await flushed()
|
|
expect(chat.sent[1].messages[0]).toEqual({fileSource: {filePath: expect.any(String)}, msgContent: {type: "image", text: "", image}, quotedItemId: quoted ? action.card : undefined, mentions: {}})
|
|
}
|
|
})
|
|
|
|
it("replies when the file is not forwarded", async () => {
|
|
const {chat, tools, action, control} = await downloadSetup()
|
|
chat.chats.get("#2")!.items.push(fileItem(30, {type: "file", text: "report"}, {type: "rcvTransfer", rcvProgress: 1, rcvTotal: 2}, {chatDir: {type: "groupRcv", groupMember: bob}}))
|
|
await control.ownerCommand(alice, {keyword: "forward", params: `${action.n}`})
|
|
await flushed()
|
|
tools.onFile(event(chatInfoOf(chat, "#2"), fileItem(30, {type: "file", text: "report"}, {type: "rcvError", rcvFileError: {type: "auth"}})))
|
|
await flushed()
|
|
expect(chat.sent.at(-1)!.messages[0].msgContent.text).toBe("Action 1: the file was not received: `Wrong key or unknown file chunk address - most likely file is deleted.`.")
|
|
})
|
|
|
|
it("replies when the forwarded file can't be posted, and deletes its copy", async () => {
|
|
const {chat, action, control} = await downloadSetup()
|
|
const path = join(data, "files", "group-2", "7.pdf")
|
|
mkdirSync(dirname(path), {recursive: true})
|
|
writeFileSync(path, "report")
|
|
rmSync(join(data, "files", "sent"), {recursive: true, force: true})
|
|
chat.chats.get("#2")!.items.push(fileItem(30, {type: "file", text: "report"}, {type: "rcvComplete"}, {chatDir: {type: "groupRcv", groupMember: bob}, fileSource: {filePath: path}}))
|
|
const send = chat.apiSendMessages.bind(chat)
|
|
chat.apiSendMessages = async (target, messages) => {
|
|
if (messages[0].fileSource !== undefined) throw new Error("file too large")
|
|
return send(target, messages)
|
|
}
|
|
await control.ownerCommand(alice, {keyword: "forward", params: `${action.n}`})
|
|
while (chat.sent.length < 2) await flushed()
|
|
expect(chat.sent[1].messages[0].msgContent.text).toBe("Action 1: the file was not received: `file too large`.")
|
|
expect(readdirSync(join(data, "files", "sent"))).toEqual([])
|
|
})
|
|
|
|
it("fails and cancels the running downloads of a chat when /delete CHAT files deletes its files, forwarded ones included", async () => {
|
|
const {chat, action, control} = await downloadSetup()
|
|
const folder = join(data, "files", "group-2")
|
|
const forwarded = join(data, "forwarded", "group-2")
|
|
const offered = fileItem(30, {type: "file", text: "report"}, {type: "rcvInvitation"}, {chatDir: {type: "groupRcv", groupMember: bob}})
|
|
chat.chats.get("#2")!.items.push(offered)
|
|
chat.responses.set(`/freceive 7 ${join(forwarded, "7.pdf")}`, {type: "rcvFileAccepted", user, chatItem: event(chatInfoOf(chat, "#2"), offered)})
|
|
chat.responses.set("/fcancel 7", {type: "rcvFileCancelled", user, rcvFileTransfer: {fileId: 7}})
|
|
await control.ownerCommand(alice, {keyword: "forward", params: `${action.n}`})
|
|
await flushed()
|
|
mkdirSync(folder, {recursive: true})
|
|
writeFileSync(join(folder, "3.pdf"), "x".repeat(120))
|
|
writeFileSync(join(forwarded, "4.pdf"), "x".repeat(80))
|
|
expect(await control.ownerCommand(alice, {keyword: "delete", params: "#2 files"})).toBe("Deleted 2 files from #2, 200 bytes.")
|
|
await flushed()
|
|
expect(chat.commands).toContain("/fcancel 7")
|
|
expect(chat.sent.at(-1)!.messages[0].msgContent.text).toBe("Action 1: the file was not received: `Your owner deleted the files of this chat.`.")
|
|
expect([existsSync(folder), existsSync(forwarded)]).toEqual([false, false])
|
|
})
|
|
|
|
it("cancels the downloads of a chat that the core continues after a restart when /delete CHAT files deletes its files", async () => {
|
|
const {chat, control} = await downloadSetup()
|
|
const items = chat.chats.get("#2")!.items
|
|
items.push(fileItem(30, {type: "file", text: ""}, {type: "rcvTransfer", rcvProgress: 1, rcvTotal: 2}, {fileId: 8}))
|
|
items.push(...Array.from({length: 120}, (_, i) => item(40 + i, "hi")))
|
|
items.push(fileItem(200, {type: "voice", text: "", duration: 3}, {type: "rcvAccepted"}, {fileId: 9}))
|
|
items.push(fileItem(201, {type: "file", text: ""}, {type: "rcvComplete"}, {fileId: 10}))
|
|
for (const fileId of [8, 9]) chat.responses.set(`/fcancel ${fileId}`, {type: "rcvFileCancelled", user, rcvFileTransfer: {fileId}})
|
|
expect(await control.ownerCommand(alice, {keyword: "delete", params: "#2 files"})).toBe("No files from #2.")
|
|
expect(chat.commands.filter(cmd => cmd.startsWith("/fcancel"))).toEqual(["/fcancel 9", "/fcancel 8"])
|
|
})
|
|
})
|
|
|
|
describe("files sent by the agent in the agent control chat", () => {
|
|
const refusal = "private keys and the server's own files are not sent."
|
|
const flushed = (): Promise<void> => new Promise(resolve => setImmediate(resolve))
|
|
let files: string
|
|
|
|
beforeEach(() => {
|
|
files = realpathSync(mkdtempSync(join(tmpdir(), "simplex-mcp-files-")))
|
|
})
|
|
|
|
afterEach(() => rmSync(files, {recursive: true, force: true}))
|
|
|
|
it("posts the refusal of a private key with the path as code, and logs it on one line", async () => {
|
|
const lines: string[] = []
|
|
jest.spyOn(process.stdout, "write").mockImplementation(text => (lines.push(String(text).trimEnd()), true))
|
|
const chat = agentChats()
|
|
const {tools} = await controlWith(chat, actionsWith("requests", fakeHooks()))
|
|
const key = join(files, "id_rsa")
|
|
writeFileSync(key, "key")
|
|
const result = await tools.call("send", {chat: "@4", file: key}, signal().call)
|
|
expect((result!.content[0] as {text: string}).text).toBe(`file: ${key} is refused: ${refusal}`)
|
|
await flushed()
|
|
expect(lines.at(-1)).toBe(`Refused to send ${key} to bot SimpleX Directory (@4): ${refusal}`)
|
|
expect(chat.sent.map(({chat: [, id], messages: [{msgContent}]}) => [id, msgContent.text])).toEqual([[10, `Refused to send \`${key}\` to bot SimpleX Directory (@4): ${refusal}`]])
|
|
})
|
|
|
|
it("posts and logs the failed upload of a sent file", async () => {
|
|
const lines: string[] = []
|
|
jest.spyOn(process.stdout, "write").mockImplementation(text => (lines.push(String(text).trimEnd()), true))
|
|
const chat = agentChats()
|
|
const {control} = await controlWith(chat, actionsWith("requests", fakeHooks()))
|
|
const sentFile = fileItem(40, {type: "file", text: ""}, {type: "sndError", sndFileError: {type: "auth"}}, {chatDir: {type: "directSnd"}})
|
|
control.fileNotSent({type: "sndFileError", user, chatItem_: event(chatInfoOf(chat, "@4"), sentFile), fileTransferMeta: {fileId: 7}, errorMessage: "XFTP AUTH"} as unknown as Parameters<Control["fileNotSent"]>[0])
|
|
await flushed()
|
|
const reason = "Wrong key or unknown file chunk address - most likely file is deleted."
|
|
expect(lines.at(-1)).toBe(`The file report.pdf was not sent to bot SimpleX Directory (@4): ${reason}.`)
|
|
expect(chat.sent.map(({chat: [, id], messages: [{msgContent}]}) => [id, msgContent.text])).toEqual([[10, `The file \`report.pdf\` was not sent to bot SimpleX Directory (@4): \`${reason}\`.`]])
|
|
})
|
|
|
|
it("shows the image's preview on the send card", async () => {
|
|
const preview = "data:image/jpg;base64,/9j/preview"
|
|
jest.spyOn(core, "resizeImage").mockResolvedValue(preview)
|
|
const fake = fakeHooks()
|
|
const actions: Actions = new Actions("requests", 23, 1, {...fake.hooks, refusal: action => control.refusal(action), postCard: action => control.postCard(action), outcome: action => control.outcome(action)})
|
|
const chat = agentChats()
|
|
const {control, tools} = await controlWith(chat, actions)
|
|
const photo = join(files, "trail.jpg")
|
|
writeFileSync(photo, "x".repeat(3_100_000))
|
|
const {controller, call} = signal()
|
|
const sending = tools.call("send", {chat: "@4", text: "trail map", file: photo}, call)
|
|
while (chat.sent.length === 0) await flushed()
|
|
expect(chat.sent[0]).toEqual({
|
|
chat: [T.ChatType.Group, 10],
|
|
messages: [
|
|
{
|
|
msgContent: {type: "image", text: `I want to send a message to bot SimpleX Directory:\n\`trail map\`\nattaching image \`${photo}\`, 3.1 MB\n/'approve @4 1', /'approve @4 all' or /'reject @4 1'`, image: preview},
|
|
mentions: {},
|
|
},
|
|
],
|
|
})
|
|
controller.abort()
|
|
expect(await sending).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe("leaving a chat at an owner's command", () => {
|
|
it("leaves a group at once, rejects its pending actions, and replies and logs who asked", async () => {
|
|
const lines: string[] = []
|
|
jest.spyOn(process.stdout, "write").mockImplementation(text => (lines.push(String(text).trimEnd()), true))
|
|
const {control, actions, chat, action, fake} = await setup()
|
|
const send = actions.create("k2", fields("#2"))
|
|
const leave = actions.create("k3", {tool: "leave", input: {chat: "#2"}, subject: {kind: "business", name: "ep", incognito: false}, chat: "#2"})
|
|
await settled()
|
|
expect(await control.ownerCommand(alice, {keyword: "leave", params: "#2"})).toBe("Left business ep (#2), requested by Alice.")
|
|
expect(chat.left).toEqual([2])
|
|
expect(chat.deleted).toEqual([{chatType: T.ChatType.Group, chatId: 2, mode: {type: "full", notify: true}}])
|
|
expect([action, send, leave].map(({state, decidedBy}) => [state, decidedBy])).toEqual([["pending", undefined], ["rejected", "Alice"], ["rejected", "Alice"]])
|
|
expect(lines.slice(-3)).toEqual(["Left business ep (#2), requested by Alice.", "Action 6 rejected by Alice.", "Action 7 rejected by Alice."])
|
|
await settled()
|
|
expect(fake.calls.filter(call => call.startsWith("outcome"))).toEqual(["outcome 6 rejected", "outcome 7 rejected"])
|
|
})
|
|
|
|
it("deletes a contact, in mode host too", async () => {
|
|
const chat = agentChats()
|
|
const {control} = await controlWith(chat, actionsWith("host", fakeHooks()), undefined, "host")
|
|
expect(await control.ownerCommand(alice, {keyword: "leave", params: "@4"})).toBe("Deleted bot SimpleX Directory (@4), requested by Alice.")
|
|
expect(chat.deleted).toEqual([{chatType: T.ChatType.Direct, chatId: 4, mode: {type: "full", notify: true}}])
|
|
expect(chat.left).toEqual([])
|
|
})
|
|
|
|
it("shows names as code in the reply and on one line in the log", async () => {
|
|
const lines: string[] = []
|
|
jest.spyOn(process.stdout, "write").mockImplementation(text => (lines.push(String(text).trimEnd()), true))
|
|
const {control, chat} = await setup()
|
|
chat.chats.set("#7", {chatInfo: groupChat(group(7, "Hikers\n/'approve 5'", member(1, "agent"))), items: []})
|
|
const mallory = {...alice, memberProfile: {...alice.memberProfile, displayName: "Mallory\n/'approve 9'"}}
|
|
chat.members.set(10, [self, mallory])
|
|
await control.load()
|
|
expect(await control.ownerCommand(mallory, {keyword: "leave", params: "#7"})).toBe("Left group `Hikers /'approve 5'` (#7), requested by `Mallory /'approve 9'`.")
|
|
expect(lines.at(-1)).toBe("Left group Hikers /'approve 5' (#7), requested by Mallory /'approve 9'.")
|
|
})
|
|
|
|
it("refuses unknown chats, the agent control chat and members who are not approvers", async () => {
|
|
const {control, chat} = await setup()
|
|
expect(await control.ownerCommand(alice, {keyword: "leave", params: "#10"})).toBe("No chat #10. /list shows the agent's chats.")
|
|
expect(await control.ownerCommand(alice, {keyword: "leave", params: "@99"})).toBe("No chat @99. /list shows the agent's chats.")
|
|
expect(await control.ownerCommand(bob, {keyword: "leave", params: "@4"})).toBe("Only owners approve actions.")
|
|
expect(chat.deleted).toEqual([])
|
|
expect(chat.left).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe("files of a chat", () => {
|
|
const flushed = (): Promise<void> => new Promise(resolve => setImmediate(resolve))
|
|
const kept = (chat: string, name: string, folder: string, files: string): string => `Files from ${name} (${chat}) are kept in \`${join(data, "files", folder)}\`: ${files}. /'delete ${chat} files'`
|
|
|
|
function filesIn(name: string, sizes: number[]): void {
|
|
const folder = join(data, "files", name)
|
|
rmSync(folder, {recursive: true, force: true})
|
|
mkdirSync(folder, {recursive: true})
|
|
sizes.forEach((size, i) => writeFileSync(join(folder, `${i + 1}.pdf`), "x".repeat(size)))
|
|
}
|
|
|
|
it("are kept when an owner's /leave leaves the chat, with a message after the reply; forwarded files are deleted", async () => {
|
|
const {control, chat} = await setup()
|
|
filesIn("group-2", [1500, 600])
|
|
const forwarded = join(data, "forwarded", "group-2")
|
|
mkdirSync(forwarded, {recursive: true})
|
|
writeFileSync(join(forwarded, "3.pdf"), "x")
|
|
expect(await control.ownerCommand(alice, {keyword: "leave", params: "#2"})).toBe("Left business ep (#2), requested by Alice.")
|
|
await flushed()
|
|
expect(chat.sent.at(-1)!.messages[0].msgContent.text).toBe(kept("#2", "business ep", "group-2", "2 files, 2.1 KB"))
|
|
expect(existsSync(forwarded)).toBe(false)
|
|
filesIn("contact-4", [])
|
|
const before = chat.sent.length
|
|
await control.ownerCommand(alice, {keyword: "leave", params: "@4"})
|
|
await flushed()
|
|
expect(chat.sent).toHaveLength(before)
|
|
})
|
|
|
|
it("are kept when an approved leave leaves the chat, with a message after its card", async () => {
|
|
const fake = fakeHooks()
|
|
const actions: Actions = new Actions("requests", 23, 1, {...fake.hooks, refusal: action => control.refusal(action), postCard: action => control.postCard(action), outcome: action => control.outcome(action), perform: (action, signal) => tools.perform(action, signal)})
|
|
const chat = agentChats()
|
|
const {control, tools} = await controlWith(chat, actions)
|
|
filesIn("group-2", [120])
|
|
const leave = actions.create("l", {tool: "leave", input: {chat: "#2"}, subject: {kind: "business", name: "ep", incognito: false}, chat: "#2"})
|
|
await flushed()
|
|
actions.approve(leave, "Alice")
|
|
while (chat.sent.length < 2) await flushed()
|
|
expect(chat.updates.at(-1)!.text).toBe("I left business ep (#2).\nApproved by Alice.")
|
|
expect(chat.sent[1].messages[0].msgContent.text).toBe(kept("#2", "business ep", "group-2", "1 file, 120 bytes"))
|
|
})
|
|
|
|
it("are deleted by /delete CHAT files, for a current or a left chat", async () => {
|
|
const {control} = await setup()
|
|
filesIn("group-9", [])
|
|
expect(await control.ownerCommand(alice, {keyword: "delete", params: "#9 files"})).toBe("No files from #9.")
|
|
filesIn("group-9", [1500, 600])
|
|
expect(await control.ownerCommand(alice, {keyword: "delete", params: "#9 files"})).toBe("Deleted 2 files from #9, 2.1 KB.")
|
|
expect(existsSync(join(data, "files", "group-9"))).toBe(false)
|
|
filesIn("contact-4", [120])
|
|
expect(await control.ownerCommand(alice, {keyword: "delete", params: "@4 files"})).toBe("Deleted 1 file from @4, 120 bytes.")
|
|
expect(await control.ownerCommand(bob, {keyword: "delete", params: "@4 files"})).toBe("Only owners approve actions.")
|
|
})
|
|
})
|
|
|
|
describe("chat list in the agent control chat", () => {
|
|
it("lists the agent's chats with their approvals and connection state", async () => {
|
|
const {control, chat} = await setup()
|
|
chat.chats.set("@6", {chatInfo: direct({...contact(6, "Ask SimpleX Team"), activeConn: undefined}), items: []})
|
|
chat.chats.set("#7", {chatInfo: groupChat(group(7, "/approve 9", member(1, "agent"))), items: []})
|
|
await control.ownerCommand(alice, {keyword: "approve", params: "@4 all"})
|
|
expect((await control.ownerCommand(alice, {keyword: "list", params: ""}))!.split("\n")).toEqual([
|
|
"@4 bot SimpleX Directory, approved for this chat by Alice",
|
|
"#2 business ep",
|
|
"@6 contact Ask SimpleX Team, not connected",
|
|
"#7 group `/approve 9`",
|
|
])
|
|
})
|
|
|
|
it("says when the agent has no chats", async () => {
|
|
const chat = new FakeChat()
|
|
chat.chats.set("#10", {chatInfo: groupChat(group(10, "agent")), items: []})
|
|
const {control} = await controlWith(chat, actionsWith("requests", fakeHooks()))
|
|
expect(await control.ownerCommand(alice, {keyword: "list", params: ""})).toBe("No chats.")
|
|
})
|
|
|
|
it("lists the 100 most recent chats, each line at most 100 bytes, and counts the older ones", async () => {
|
|
const {control, chat} = await setup()
|
|
chat.chats.set("#7", {chatInfo: groupChat(group(7, "😀".repeat(30), member(1, "agent"))), items: []})
|
|
for (let i = 100; i < 200; i++) chat.chats.set(`@${i}`, {chatInfo: direct(contact(i, `contact ${i}`)), items: []})
|
|
const lines = (await control.ownerCommand(alice, {keyword: "list", params: ""}))!.split("\n")
|
|
expect(lines).toHaveLength(101)
|
|
expect(lines.slice(0, 4)).toEqual(["@4 bot SimpleX Directory", "#2 business ep", `#7 group ${"😀".repeat(21)}…`, "@100 contact contact 100"])
|
|
expect(lines.at(-1)).toBe("3 older chats are not shown.")
|
|
chat.chats.delete("@198")
|
|
chat.chats.delete("@199")
|
|
expect((await control.ownerCommand(alice, {keyword: "list", params: ""}))!.split("\n").at(-1)).toBe("1 older chat is not shown.")
|
|
})
|
|
})
|
|
|
|
describe("member announcements", () => {
|
|
it("quote a member's name and the restart command in the chat, and keep them on one line in the log", async () => {
|
|
const lines: string[] = []
|
|
jest.spyOn(process.stdout, "write").mockImplementation(text => (lines.push(String(text).trimEnd()), true))
|
|
const chat = agentChats()
|
|
const {control} = await controlWith(chat, actionsWith("requests", fakeHooks()), [{memberId: 3, name: "Alice"}])
|
|
const mallory = member(7, "Bob\u001b]52;c;Y3VybA==\u0007\n/'approve 9'", T.GroupMemberRole.Owner)
|
|
chat.members.set(10, [self, alice, mallory])
|
|
await control.onEvent({type: "memberRole", user, groupInfo: group(10, "agent"), byMember: alice, member: mallory, fromRole: T.GroupMemberRole.Member, toRole: T.GroupMemberRole.Owner} as unknown as ChatEvent)
|
|
const restart = "is now an owner and can't approve actions yet. Restart the server with"
|
|
const owners = "--owners 3:Alice,'7:Bob ]52;c;Y3VybA== /'\\''approve 9'\\'''"
|
|
expect(lines).toEqual([`Bob ]52;c;Y3VybA== /'approve 9' (member 7) ${restart} ${owners}`])
|
|
expect(chat.sent.map(({messages: [{msgContent}]}) => msgContent.text)).toEqual([`\`Bob ]52;c;Y3VybA== /'approve 9'\` (member 7) ${restart} \`${owners}\``])
|
|
})
|
|
})
|
|
|
|
describe("command menu", () => {
|
|
const menu = [
|
|
{type: "command", keyword: "pending", label: "Pending actions"},
|
|
{type: "command", keyword: "list", label: "Chats and approvals"},
|
|
{type: "command", keyword: "leave", label: "Leave a chat", params: "<chat ID>"},
|
|
]
|
|
|
|
it("is updated at load when it differs", async () => {
|
|
const chat = agentChats()
|
|
const actions = actionsWith("requests", fakeHooks())
|
|
const old = group(10, "agent", self, {groupProfile: {displayName: "agent", fullName: "", groupPreferences: {history: {enable: "on"}, commands: [{type: "command", keyword: "approve", label: "Approve", params: "N"}]}}})
|
|
const tools = new AgentTools({chat: chat.api(), user, actions, watch: new ChatItemsWatch(), controlChat: () => "#10", standingApprover: () => undefined, refusedFile: () => undefined, data}, "requests")
|
|
await new Control(chat.api(), old, {displayName: "agent", registerHint: "hint", qrCode: "small"}, actions, tools).load()
|
|
expect(chat.profiles).toEqual([{groupId: 10, groupProfile: {displayName: "agent", fullName: "", groupPreferences: {history: {enable: "on"}, commands: menu}}}])
|
|
const current = group(10, "agent", self, {groupProfile: {displayName: "agent", fullName: "", groupPreferences: {history: {enable: "on"}, commands: menu}}})
|
|
await new Control(chat.api(), current, {displayName: "agent", registerHint: "hint", qrCode: "small"}, actions, tools).load()
|
|
expect(chat.profiles).toHaveLength(1)
|
|
})
|
|
|
|
it("answers the /leave template sent unedited with the list of commands", async () => {
|
|
const {control, chat} = await setup()
|
|
expect(await control.ownerCommand(alice, parseCommand("/leave <chat ID>")!)).toBe(commandsReply)
|
|
expect(chat.deleted).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe("sends to a chat approved for all messages", () => {
|
|
async function standing(approvers: T.GroupMember[], mode: Mode = "requests") {
|
|
const chat = agentChats()
|
|
chat.members.set(10, [self, ...approvers])
|
|
const {contact: directory} = chatInfoOf(chat, "@4") as T.ChatInfo.Direct
|
|
directory.customData = {simplexMcp: {approvedBy: "Alice", approvedById: 3, approvedAt: "2026-10-18T10:00:00.000Z"}}
|
|
const actions: Actions = new Actions(mode, 23, 1, {
|
|
refusal: action => control.refusal(action),
|
|
saveCounter: async () => undefined,
|
|
postCard: action => control.postCard(action),
|
|
outcome: action => control.outcome(action),
|
|
perform: (action, signal) => tools.perform(action, signal),
|
|
})
|
|
const {control, tools} = await controlWith(chat, actions, undefined, mode)
|
|
return {chat, actions, control, tools}
|
|
}
|
|
|
|
const cards = (chat: FakeChat): string[] => chat.sent.filter(({chat: [, id]}) => id === 10).map(({messages: [{msgContent}]}) => msgContent.text)
|
|
|
|
it("performs the send at once, without a card, and posts the outcome card", async () => {
|
|
const {chat, tools} = await standing([alice])
|
|
const result = await tools.call("send", {chat: "@4", text: "trail shoes", wait: 0}, signal().call)
|
|
expect(result!.structuredContent).toMatchObject({action: 1, status: "done", chat: "@4", responses: [], more: 0})
|
|
expect(chat.sent.filter(({chat: [, id]}) => id === 4).map(({messages: [{msgContent}]}) => msgContent.text)).toEqual(["trail shoes"])
|
|
await settled()
|
|
expect(cards(chat)).toEqual(["I sent a message to bot SimpleX Directory:\n`trail shoes`\nApproved for this chat by Alice. /'revoke @4'"])
|
|
})
|
|
|
|
it("posts a card when the approving member is no longer an approver", async () => {
|
|
const {chat, actions, tools} = await standing([{...alice, memberRole: T.GroupMemberRole.Member}, member(8, "Carol", T.GroupMemberRole.Owner)])
|
|
const {controller, call} = signal()
|
|
const sending = tools.call("send", {chat: "@4", text: "trail shoes", wait: 0}, call)
|
|
await settled()
|
|
expect(actions.get(1)).toMatchObject({state: "pending", standing: false})
|
|
expect(cards(chat)).toEqual(["I want to send a message to bot SimpleX Directory:\n`trail shoes`\n/'approve @4 1', /'approve @4 all' or /'reject @4 1'"])
|
|
expect(chat.sent.filter(({chat: [, id]}) => id === 4)).toEqual([])
|
|
controller.abort()
|
|
expect(await sending).toBeUndefined()
|
|
})
|
|
|
|
it("does not cover leave", async () => {
|
|
const {chat, actions, tools} = await standing([alice])
|
|
const {controller, call} = signal()
|
|
const leaving = tools.call("leave", {chat: "@4"}, call)
|
|
await settled()
|
|
expect(actions.get(1)).toMatchObject({tool: "leave", state: "pending", standing: false})
|
|
expect(cards(chat)).toEqual(["I want to delete bot SimpleX Directory (@4).\n/'approve @4 1' or /'reject @4 1'"])
|
|
controller.abort()
|
|
expect(await leaving).toBeUndefined()
|
|
})
|
|
|
|
it("applies in no chat in mode host", async () => {
|
|
const {chat, control, tools} = await standing([alice], "host")
|
|
expect(control.standingApprover(chatInfoOf(chat, "@4"), "messages")).toBeUndefined()
|
|
const {chats} = (await tools.call("list", {}, signal().call))!.structuredContent as {chats: {chat: string}[]}
|
|
expect(chats.find(({chat: ref}) => ref === "@4")).not.toHaveProperty("approved")
|
|
expect((await control.ownerCommand(alice, {keyword: "list", params: ""}))!.split("\n")[0]).toBe("@4 bot SimpleX Directory")
|
|
expect(await control.ownerCommand(alice, {keyword: "approve", params: "#2 all"})).toBe("Approvals apply in mode requests; this server runs in mode host.")
|
|
expect(chat.customData).toEqual([])
|
|
expect((await tools.call("send", {chat: "@4", text: "trail shoes", wait: 0}, signal().call))!.structuredContent).toMatchObject({action: 1, status: "done"})
|
|
await settled()
|
|
expect(cards(chat)).toEqual(["I sent a message to bot SimpleX Directory:\n`trail shoes`\nPerformed without approval (mode host)."])
|
|
})
|
|
})
|
|
|
|
describe("approval card size", () => {
|
|
const tooLong = "text: too long for its approval card; shorten it."
|
|
|
|
async function sendTools(approvers: T.GroupMember[] = [alice]) {
|
|
const chat = agentChats()
|
|
chat.members.set(10, [self, ...approvers, bob])
|
|
const {contact: directory} = chatInfoOf(chat, "@4") as T.ChatInfo.Direct
|
|
directory.customData = {simplexMcp: {approvedBy: "Alice", approvedById: 3, approvedAt: "2026-10-18T10:00:00.000Z"}}
|
|
const actions: Actions = new Actions("requests", 23, 1, {
|
|
refusal: action => control.refusal(action),
|
|
saveCounter: async () => undefined,
|
|
postCard: action => control.postCard(action),
|
|
outcome: action => control.outcome(action),
|
|
perform: (action, signal) => tools.perform(action, signal),
|
|
})
|
|
const {control, tools} = await controlWith(chat, actions)
|
|
const textOf = (result: CallToolResult | undefined): string => (result!.content[0] as {text: string}).text
|
|
return {chat, actions, tools, textOf}
|
|
}
|
|
|
|
function connectFields(name: string, extra: {message?: string; description?: string; welcome?: string}): NewAction {
|
|
return {
|
|
tool: "connect",
|
|
input: {target: "https://simplex.chat/a#ridgeline", ...(extra.message === undefined ? {} : {message: extra.message})},
|
|
subject: {kind: "contact", name, incognito: false},
|
|
plan: {cardKind: "contact", type: "contact", takesMessage: true, description: extra.description, welcome: extra.welcome},
|
|
messages: new Collected(),
|
|
}
|
|
}
|
|
|
|
const encodedLength = (text: string): number => Buffer.byteLength(JSON.stringify(text)) - 2
|
|
|
|
const flushed = (): Promise<void> => new Promise(resolve => setImmediate(resolve))
|
|
|
|
it("accepts a text whose largest card has 15000 bytes and refuses one byte more, counting multi-byte characters", async () => {
|
|
const {chat, actions, tools, textOf} = await sendTools()
|
|
const atLimit = "€".repeat(4374) + "aa"
|
|
expect(textOf(await tools.call("send", {chat: "#2", text: `${atLimit}a`}, signal().call))).toBe(tooLong)
|
|
expect(actions.next).toBe(1)
|
|
const {controller, call} = signal()
|
|
const sending = tools.call("send", {chat: "#2", text: atLimit}, call)
|
|
await flushed()
|
|
expect(actions.get(1)).toMatchObject({tool: "send", state: "pending", card: expect.any(Number)})
|
|
expect(chat.sent.map(({chat: [, id]}) => id)).toEqual([10])
|
|
controller.abort()
|
|
expect(await sending).toBeUndefined()
|
|
})
|
|
|
|
it("counts escaped characters and the standing approval line of a chat approved for all messages", async () => {
|
|
const {chat, actions, tools, textOf} = await sendTools()
|
|
const atLimit = "\b".repeat(2183) + "aa"
|
|
expect(textOf(await tools.call("send", {chat: "@4", text: `${atLimit}a`, wait: 0}, signal().call))).toBe(tooLong)
|
|
expect(actions.next).toBe(1)
|
|
expect((await tools.call("send", {chat: "@4", text: atLimit, wait: 0}, signal().call))!.structuredContent).toMatchObject({action: 1, status: "done"})
|
|
await flushed()
|
|
expect(chat.sent.map(({chat: [, id]}) => id)).toEqual([4, 10])
|
|
})
|
|
|
|
it("measures the card with the longest name of the current approvers", async () => {
|
|
const {actions, tools, textOf} = await sendTools([alice, member(8, "😀".repeat(50), T.GroupMemberRole.Owner)])
|
|
expect(textOf(await tools.call("send", {chat: "#2", text: "€".repeat(4374) + "aa"}, signal().call))).toBe(tooLong)
|
|
const {controller, call} = signal()
|
|
const sending = tools.call("send", {chat: "#2", text: "€".repeat(4309) + "aa"}, call)
|
|
await flushed()
|
|
expect(actions.get(1)).toMatchObject({state: "pending"})
|
|
controller.abort()
|
|
expect(await sending).toBeUndefined()
|
|
})
|
|
|
|
it("counts the lines of the text, which the card quotes one by one", async () => {
|
|
const {actions, tools, textOf} = await sendTools()
|
|
expect(textOf(await tools.call("send", {chat: "#2", text: "a\n".repeat(3000)}, signal().call))).toBe(tooLong)
|
|
const {controller, call} = signal()
|
|
const sending = tools.call("send", {chat: "#2", text: "a\n".repeat(2000)}, call)
|
|
await flushed()
|
|
expect(actions.get(1)).toMatchObject({state: "pending"})
|
|
controller.abort()
|
|
expect(await sending).toBeUndefined()
|
|
})
|
|
|
|
it("refuses a connect whose message makes its largest card longer than 15000 bytes", async () => {
|
|
const {chat, actions} = await sendTools()
|
|
const name = "😀".repeat(1000)
|
|
expect(() => actions.create("c", connectFields(name, {message: "\b".repeat(2000), welcome: "Welcome!"}))).toThrow(new ToolError("message: too long for its approval card; shorten it."))
|
|
expect(actions.next).toBe(1)
|
|
actions.create("c", connectFields(name, {message: "\b".repeat(1400), welcome: "Welcome!"}))
|
|
await flushed()
|
|
expect(chat.sent.map(({chat: [, id]}) => id)).toEqual([10])
|
|
})
|
|
|
|
it("cuts the link texts on a connect card to fit 15000 bytes in its largest form, and keeps them whole in the action", async () => {
|
|
const {chat, actions} = await sendTools()
|
|
const description = "Trail shoes. ".repeat(1500)
|
|
const action = actions.create("c", connectFields("Ridgeline", {description, welcome: "Welcome!"}))
|
|
await flushed()
|
|
const [card] = chat.sent.filter(({chat: [, id]}) => id === 10).map(({messages: [{msgContent}]}) => msgContent.text.split("\n"))
|
|
expect(card).toHaveLength(3)
|
|
expect(card[1]).toMatch(/^`Trail shoes\. .*…`$/)
|
|
const failed = encodedLength(cardText({...action, state: "failed", decidedBy: "Alice", error: "€".repeat(600)}, "requests"))
|
|
expect(failed).toBeLessThanOrEqual(15000)
|
|
expect(failed).toBeGreaterThan(15000 - 8)
|
|
expect(action.tool === "connect" && action.plan.description).toBe(description)
|
|
})
|
|
})
|
|
|
|
describe("wait at the stop", () => {
|
|
const send = {tool: "send", card: 1001} as Action
|
|
const connect = {tool: "connect"} as Action
|
|
const download = {tool: "download"} as Action
|
|
|
|
afterEach(() => jest.useRealTimers())
|
|
|
|
function logged(): string[] {
|
|
const lines: string[] = []
|
|
jest.spyOn(process.stdout, "write").mockImplementation(text => (lines.push(String(text).trimEnd()), true))
|
|
return lines
|
|
}
|
|
|
|
it("waits the stop wait when cards were cancelled, and stops at once without them", async () => {
|
|
jest.useFakeTimers()
|
|
const lines = logged()
|
|
let ended = false
|
|
const waiting = waitForCancelledCards([send, connect, download], 10, new AbortController().signal).then(() => (ended = true))
|
|
expect(lines).toEqual(["Stopping: sending 2 cancelled cards, up to 10 s. Ctrl-C to stop now."])
|
|
await jest.advanceTimersByTimeAsync(9_999)
|
|
expect(ended).toBe(false)
|
|
await jest.advanceTimersByTimeAsync(1)
|
|
await waiting
|
|
await waitForCancelledCards([download], 10, new AbortController().signal)
|
|
await waitForCancelledCards([send], 0, new AbortController().signal)
|
|
await waitForCancelledCards([], 10, new AbortController().signal)
|
|
expect(lines).toHaveLength(1)
|
|
expect(jest.getTimerCount()).toBe(0)
|
|
})
|
|
|
|
it("ends the wait at a second Ctrl-C", async () => {
|
|
jest.useFakeTimers()
|
|
const lines = logged()
|
|
const endWait = new AbortController()
|
|
let ended = false
|
|
const waiting = waitForCancelledCards([send], 10, endWait.signal).then(() => (ended = true))
|
|
expect(lines).toEqual(["Stopping: sending 1 cancelled card, up to 10 s. Ctrl-C to stop now."])
|
|
await jest.advanceTimersByTimeAsync(1_000)
|
|
expect(ended).toBe(false)
|
|
endWait.abort()
|
|
await waiting
|
|
expect(jest.getTimerCount()).toBe(0)
|
|
await waitForCancelledCards([send], 10, endWait.signal)
|
|
expect(jest.getTimerCount()).toBe(0)
|
|
})
|
|
})
|