mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-11-15 00:19:16 +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
448 lines
18 KiB
TypeScript
448 lines
18 KiB
TypeScript
import {Action, Actions, NewAction, ToolError, lostMessage, stoppingMessage} from "../../src/actions"
|
|
import {Collected} from "../../src/messages"
|
|
import {actionsWith, fakeHooks, settled, signal} from "./fixtures"
|
|
|
|
let logLines: string[]
|
|
|
|
beforeEach(() => {
|
|
logLines = []
|
|
jest.spyOn(process.stdout, "write").mockImplementation(text => (logLines.push(String(text).trimEnd()), true))
|
|
})
|
|
|
|
afterEach(() => {
|
|
jest.restoreAllMocks()
|
|
jest.useRealTimers()
|
|
})
|
|
|
|
function sendFields(text = "trail running shoes"): NewAction {
|
|
return {
|
|
tool: "send",
|
|
input: {chat: "@4", text},
|
|
subject: {kind: "bot", name: "SimpleX Directory", simplexName: "@directory.simplex", incognito: false},
|
|
chat: "@4",
|
|
mentions: [],
|
|
responses: new Collected(),
|
|
}
|
|
}
|
|
|
|
function connectFields(): NewAction {
|
|
return {
|
|
tool: "connect",
|
|
input: {target: "@directory.simplex"},
|
|
subject: {kind: "bot", name: "SimpleX Directory", simplexName: "@directory.simplex", incognito: false},
|
|
plan: {cardKind: "bot", type: "contact", takesMessage: true},
|
|
messages: new Collected(),
|
|
}
|
|
}
|
|
|
|
async function pendingAction(actions: Actions, key = "k"): Promise<Action> {
|
|
const action = actions.create(key, sendFields())
|
|
await settled()
|
|
expect(action.card).toBe(1000 + action.n)
|
|
return action
|
|
}
|
|
|
|
describe("actions in mode requests", () => {
|
|
it("writes the counter before the card and logs the action", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake, 7)
|
|
const action = await pendingAction(actions)
|
|
expect(action).toMatchObject({n: 7, state: "pending", open: 0})
|
|
expect(actions.next).toBe(8)
|
|
expect(fake.calls).toEqual(["counter 8", "card 7"])
|
|
expect(logLines).toEqual(["Action 7: send to bot SimpleX Directory (@4)"])
|
|
})
|
|
|
|
it("performs an approved action and edits its card", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
const action = await pendingAction(actions)
|
|
actions.approve(action, "Alice")
|
|
expect(action).toMatchObject({state: "performing", decidedBy: "Alice"})
|
|
fake.performs[0].resolve()
|
|
await settled()
|
|
expect(action.state).toBe("done")
|
|
expect(fake.calls).toEqual(["counter 2", "card 1", "perform 1", "outcome 1 done"])
|
|
expect(logLines.at(-1)).toBe("Action 1 approved by Alice: sent.")
|
|
})
|
|
|
|
it("fails when performing fails", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
const action = await pendingAction(actions)
|
|
actions.approve(action, "Alice")
|
|
fake.performs[0].reject(new Error("no route"))
|
|
await settled()
|
|
expect(action).toMatchObject({state: "failed", error: "no route"})
|
|
expect(logLines.at(-1)).toBe("Action 1 approved by Alice: failed: no route.")
|
|
expect(fake.calls.at(-1)).toBe("outcome 1 failed")
|
|
})
|
|
|
|
it("logs a failure on one line", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
const action = await pendingAction(actions)
|
|
actions.approve(action, "Alice")
|
|
fake.performs[0].reject(new Error("bad\u001b]52;c;eA==\u0007\nnext"))
|
|
await settled()
|
|
expect(logLines.at(-1)).toBe("Action 1 approved by Alice: failed: bad ]52;c;eA== next.")
|
|
})
|
|
|
|
it("reports connect outcomes", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
const connected = actions.create("a", connectFields())
|
|
const requested = actions.create("b", connectFields())
|
|
await settled()
|
|
actions.approve(connected, "Alice")
|
|
actions.approve(requested, "Alice")
|
|
if (connected.tool === "connect") connected.connected = true
|
|
fake.performs[0].resolve()
|
|
fake.performs[1].resolve()
|
|
await settled()
|
|
expect(logLines.slice(-2)).toEqual(["Action 1 approved by Alice: connected.", "Action 2 approved by Alice: connection requested."])
|
|
})
|
|
|
|
it("rejects", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
const action = await pendingAction(actions)
|
|
actions.reject(action, "Bob")
|
|
await settled()
|
|
expect(action).toMatchObject({state: "rejected", decidedBy: "Bob"})
|
|
expect(fake.calls.at(-1)).toBe("outcome 1 rejected")
|
|
expect(logLines.at(-1)).toBe("Action 1 rejected by Bob.")
|
|
})
|
|
|
|
it("expires after --expire hours", async () => {
|
|
jest.useFakeTimers()
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake, 1, 1)
|
|
const action = await pendingAction(actions)
|
|
jest.advanceTimersByTime(3_600_000 - 1)
|
|
expect(action.state).toBe("pending")
|
|
jest.advanceTimersByTime(1)
|
|
await settled()
|
|
expect(action.state).toBe("expired")
|
|
expect(fake.calls.at(-1)).toBe("outcome 1 expired")
|
|
expect(logLines.at(-1)).toBe("Action 1 expired.")
|
|
})
|
|
|
|
it("expires by the wall clock after the computer sleeps", async () => {
|
|
jest.useFakeTimers()
|
|
const actions = actionsWith("requests", fakeHooks(), 1, 1)
|
|
const action = await pendingAction(actions)
|
|
jest.setSystemTime(Date.now() + 3_600_000)
|
|
expect(action.state).toBe("pending")
|
|
jest.advanceTimersByTime(60_000)
|
|
expect(action.state).toBe("expired")
|
|
})
|
|
|
|
it("fails when the card can't be posted", async () => {
|
|
const fake = fakeHooks()
|
|
fake.hooks.postCard = async () => {
|
|
throw new Error("not a member")
|
|
}
|
|
const actions = actionsWith("requests", fake)
|
|
const action = actions.create("k", sendFields())
|
|
await settled()
|
|
expect(action).toMatchObject({state: "failed", error: "not a member"})
|
|
expect(action.card).toBeUndefined()
|
|
expect(logLines.at(-1)).toBe("Action 1: failed: not a member.")
|
|
})
|
|
|
|
it("fails without a card or a perform when the counter can't be written, and logs it on one line", async () => {
|
|
const fake = fakeHooks()
|
|
fake.hooks.saveCounter = async () => {
|
|
throw new Error("disk\nfull")
|
|
}
|
|
const actions = actionsWith("host", fake)
|
|
const action = actions.create("k", sendFields())
|
|
await settled()
|
|
expect(action).toMatchObject({state: "failed", error: "disk\nfull"})
|
|
expect(fake.calls).toEqual([])
|
|
expect(logLines).toEqual(["Action 1: send to bot SimpleX Directory (@4)", "Action 1: failed: disk full."])
|
|
})
|
|
|
|
it("refuses new actions without approvers or control chat", () => {
|
|
const fake = fakeHooks()
|
|
fake.approvers = 0
|
|
const actions = actionsWith("requests", fake)
|
|
expect(() => actions.create("k", sendFields())).toThrow(new ToolError("no approver"))
|
|
actions.lost = true
|
|
expect(() => actions.create("k", sendFields())).toThrow(new ToolError(lostMessage))
|
|
expect(actions.next).toBe(1)
|
|
})
|
|
|
|
it("refuses new actions once stopping", async () => {
|
|
const actions = actionsWith("requests", fakeHooks())
|
|
await actions.stop()
|
|
expect(() => actions.create("k", sendFields())).toThrow(new ToolError(stoppingMessage))
|
|
expect(actions.next).toBe(1)
|
|
})
|
|
|
|
it("asks for the refusal with the action it would create", () => {
|
|
const fake = fakeHooks()
|
|
const asked: Action[] = []
|
|
fake.hooks.refusal = action => (asked.push(action), "too long")
|
|
const actions = actionsWith("requests", fake, 4)
|
|
expect(() => actions.create("k", sendFields("long"), "Alice")).toThrow(new ToolError("too long"))
|
|
expect(asked).toEqual([expect.objectContaining({n: 4, tool: "send", input: {chat: "@4", text: "long"}, state: "performing", standing: true, decidedBy: "Alice"})])
|
|
expect(actions.next).toBe(4)
|
|
expect(actions.all()).toEqual([])
|
|
expect(fake.calls).toEqual([])
|
|
})
|
|
|
|
it("refuses a new action while 20 are pending", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
for (let i = 0; i < 20; i++) actions.create(`k${i}`, sendFields(`text ${i}`))
|
|
await settled()
|
|
expect(() => actions.create("k20", sendFields("one more"))).toThrow(new ToolError("20 actions are pending; wait for the owner's decisions."))
|
|
expect(actions.next).toBe(21)
|
|
actions.reject(actions.get(1)!, "Alice")
|
|
expect(actions.create("k20", sendFields("one more")).n).toBe(21)
|
|
})
|
|
|
|
it("fails pending actions when the control chat is lost and skips their cards", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
const pending = await pendingAction(actions, "a")
|
|
const performing = await pendingAction(actions, "b")
|
|
actions.approve(performing, "Alice")
|
|
actions.loseControl()
|
|
expect(pending).toMatchObject({state: "failed", error: lostMessage})
|
|
fake.performs[0].resolve()
|
|
await settled()
|
|
expect(performing.state).toBe("done")
|
|
expect(fake.calls.filter(call => call.startsWith("outcome"))).toEqual([])
|
|
})
|
|
|
|
it("stops: cancels pending and interrupts performing actions", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
await pendingAction(actions, "a")
|
|
const performing = await pendingAction(actions, "b")
|
|
const done = await pendingAction(actions, "c")
|
|
actions.approve(performing, "Alice")
|
|
actions.approve(done, "Alice")
|
|
fake.performs[1].resolve()
|
|
await settled()
|
|
expect((await actions.stop()).map(({n}) => n)).toEqual([1, 2])
|
|
expect(fake.calls.filter(call => call.startsWith("outcome"))).toEqual(["outcome 3 done", "outcome 1 pending interrupted", "outcome 2 performing interrupted"])
|
|
fake.performs[0].resolve()
|
|
await settled()
|
|
expect(fake.calls.filter(call => call.startsWith("outcome"))).toHaveLength(3)
|
|
})
|
|
|
|
it("aborts the signal of the performs at the stop, and logs no line when an interrupted perform ends", async () => {
|
|
const fake = fakeHooks()
|
|
const signals: AbortSignal[] = []
|
|
const perform = fake.hooks.perform
|
|
fake.hooks.perform = (action, signal) => (signals.push(signal), perform(action, signal))
|
|
const actions = actionsWith("requests", fake)
|
|
const action = await pendingAction(actions)
|
|
actions.approve(action, "Alice")
|
|
expect(signals.map(({aborted}) => aborted)).toEqual([false])
|
|
await actions.stop()
|
|
expect(signals.map(({aborted}) => aborted)).toEqual([true])
|
|
const lines = [...logLines]
|
|
fake.performs[0].resolve()
|
|
await settled()
|
|
expect(action.state).toBe("done")
|
|
expect(logLines).toEqual(lines)
|
|
expect(fake.calls.filter(call => call.startsWith("outcome"))).toEqual(["outcome 1 performing interrupted"])
|
|
})
|
|
|
|
it("stops after the outcome edit of a perform that ended before the stop", async () => {
|
|
const fake = fakeHooks()
|
|
let edited: () => void = () => undefined
|
|
fake.hooks.outcome = () => new Promise(resolve => (edited = resolve))
|
|
const actions = actionsWith("requests", fake)
|
|
const action = await pendingAction(actions)
|
|
actions.approve(action, "Alice")
|
|
fake.performs[0].resolve()
|
|
await settled()
|
|
expect(action.state).toBe("done")
|
|
let stopped = false
|
|
const stopping = actions.stop().then(() => (stopped = true))
|
|
await settled()
|
|
expect(stopped).toBe(false)
|
|
edited()
|
|
await stopping
|
|
expect(stopped).toBe(true)
|
|
})
|
|
|
|
it("stops after a card being posted, and edits it", async () => {
|
|
const fake = fakeHooks()
|
|
let postCard: (card: number) => void = () => undefined
|
|
fake.hooks.postCard = () => new Promise(resolve => (postCard = resolve))
|
|
const actions = actionsWith("requests", fake)
|
|
const action = actions.create("k", sendFields())
|
|
await settled()
|
|
const stopping = actions.stop()
|
|
await settled()
|
|
expect(fake.calls).toEqual(["counter 2"])
|
|
postCard(77)
|
|
expect(await stopping).toEqual([action])
|
|
expect(action.card).toBe(77)
|
|
expect(fake.calls).toEqual(["counter 2", "outcome 1 pending interrupted"])
|
|
})
|
|
|
|
it("forgets an action whose result was returned, and stops its collection", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
const action = await pendingAction(actions, "k")
|
|
const stopCollecting = jest.fn()
|
|
action.stopCollecting = stopCollecting
|
|
actions.reject(action, "Alice")
|
|
actions.remove(action)
|
|
expect(stopCollecting).toHaveBeenCalledTimes(1)
|
|
expect(actions.get(action.n)).toBeUndefined()
|
|
expect(actions.find("k")).toBeUndefined()
|
|
expect(actions.all()).toEqual([])
|
|
})
|
|
|
|
it("attaches calls with the same key until the result is returned", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
const action = await pendingAction(actions, "same")
|
|
expect(actions.find("same")).toBe(action)
|
|
expect(actions.find("other")).toBeUndefined()
|
|
expect(actions.all()).toEqual([action])
|
|
actions.remove(action)
|
|
expect(actions.find("same")).toBeUndefined()
|
|
expect(actions.all()).toEqual([])
|
|
})
|
|
|
|
it("forgets decided actions without an open call once they expire", async () => {
|
|
jest.useFakeTimers()
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake, 1, 1)
|
|
const rejected = await pendingAction(actions, "a")
|
|
const waited = await pendingAction(actions, "b")
|
|
const performing = await pendingAction(actions, "c")
|
|
const expiring = await pendingAction(actions, "d")
|
|
actions.reject(rejected, "Alice")
|
|
actions.reject(waited, "Alice")
|
|
actions.approve(performing, "Alice")
|
|
const stopCollecting = jest.fn()
|
|
rejected.stopCollecting = stopCollecting
|
|
waited.open = 1
|
|
jest.advanceTimersByTime(3_600_000 - 1)
|
|
expect(actions.all()).toHaveLength(4)
|
|
jest.advanceTimersByTime(1)
|
|
expect(actions.all()).toEqual([waited, performing, expiring])
|
|
expect(stopCollecting).toHaveBeenCalledTimes(1)
|
|
expect(expiring.state).toBe("expired")
|
|
jest.advanceTimersByTime(60_000)
|
|
expect(actions.all()).toEqual([waited, performing])
|
|
expect(actions.find("d")).toBeUndefined()
|
|
})
|
|
|
|
it("waits for the decision with progress, and stops on the signal", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
const action = await pendingAction(actions)
|
|
const first = signal()
|
|
const second = signal()
|
|
const waiting = actions.decided(action, first.call)
|
|
const aborted = actions.decided(action, second.call)
|
|
expect(first.progress[0]).toMatch(/^Action 1 waits for an approver until \d{4}-\d\d-\d\d \d\d:\d\d UTC\.$/)
|
|
second.controller.abort()
|
|
expect(await aborted).toBe(false)
|
|
actions.reject(action, "Alice")
|
|
expect(await waiting).toBe(true)
|
|
expect(await actions.decided(action, signal().call)).toBe(true)
|
|
})
|
|
|
|
it("logs names on one line", () => {
|
|
const actions = actionsWith("requests", fakeHooks())
|
|
const fields = sendFields()
|
|
if (fields.tool === "send") Object.assign(fields, {subject: {kind: "group", name: "Hikers\u001b]52;c;Y3VybA==\u0007\n/'approve 7'", incognito: false}})
|
|
actions.create("k", fields)
|
|
expect(logLines).toEqual(["Action 1: send to group Hikers ]52;c;Y3VybA== /'approve 7' (@4)"])
|
|
})
|
|
|
|
it("performs an action approved in advance at once, without a card, and posts the outcome", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
const action = actions.create("k", sendFields(), "Alice")
|
|
expect(action).toMatchObject({state: "performing", standing: true, decidedBy: "Alice"})
|
|
await settled()
|
|
fake.performs[0].resolve()
|
|
await settled()
|
|
expect(action.state).toBe("done")
|
|
expect(action.card).toBeUndefined()
|
|
expect(fake.calls).toEqual(["counter 2", "perform 1", "outcome 1 done"])
|
|
expect(logLines).toEqual(["Action 1: send to bot SimpleX Directory (@4)", "Action 1 approved by Alice: sent."])
|
|
})
|
|
|
|
it("logs leave actions", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("requests", fake)
|
|
const subject = {kind: "bot", name: "SimpleX Directory", simplexName: "@directory.simplex", incognito: false}
|
|
const contact = actions.create("a", {tool: "leave", input: {chat: "@4"}, subject, chat: "@4"})
|
|
const group = actions.create("b", {tool: "leave", input: {chat: "#2"}, subject: {kind: "business", name: "ep", incognito: false}, chat: "#2"})
|
|
await settled()
|
|
expect(logLines).toEqual(["Action 1: delete bot SimpleX Directory (@4)", "Action 2: leave business ep (#2)"])
|
|
actions.approve(contact, "Alice")
|
|
actions.approve(group, "Alice")
|
|
fake.performs[0].resolve()
|
|
fake.performs[1].resolve()
|
|
await settled()
|
|
expect(logLines.slice(-2)).toEqual(["Action 1 approved by Alice: deleted.", "Action 2 approved by Alice: left."])
|
|
})
|
|
})
|
|
|
|
describe("actions in mode host", () => {
|
|
it("performs at once and posts the outcome", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("host", fake)
|
|
const action = actions.create("k", connectFields())
|
|
expect(action.state).toBe("performing")
|
|
expect(logLines).toEqual(["Action 1: connect to bot SimpleX Directory, as itself"])
|
|
await settled()
|
|
fake.performs[0].resolve()
|
|
await settled()
|
|
expect(action.state).toBe("done")
|
|
expect(fake.calls).toEqual(["counter 2", "perform 1", "outcome 1 done"])
|
|
expect(logLines.at(-1)).toBe("Action 1: performed without approval (mode host).")
|
|
})
|
|
|
|
it("cancels an action whose counter is being written at the stop, without performing it", async () => {
|
|
const fake = fakeHooks()
|
|
let written: () => void = () => undefined
|
|
fake.hooks.saveCounter = () => new Promise(resolve => (written = resolve))
|
|
const actions = actionsWith("host", fake)
|
|
const action = actions.create("k", sendFields())
|
|
const stopping = actions.stop()
|
|
written()
|
|
expect(await stopping).toEqual([action])
|
|
expect(action).toMatchObject({state: "pending", interrupted: true})
|
|
expect(fake.calls).toEqual(["outcome 1 pending interrupted"])
|
|
})
|
|
|
|
it("refuses a new action while 20 are being performed", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("host", fake)
|
|
for (let i = 0; i < 20; i++) actions.create(`k${i}`, sendFields(`text ${i}`))
|
|
await settled()
|
|
expect(() => actions.create("k20", sendFields("one more"))).toThrow(new ToolError("20 actions are being performed; wait for their results."))
|
|
expect(actions.next).toBe(21)
|
|
fake.performs[0].resolve()
|
|
await settled()
|
|
expect(actions.create("k20", sendFields("one more")).n).toBe(21)
|
|
})
|
|
|
|
it("logs a failure", async () => {
|
|
const fake = fakeHooks()
|
|
const actions = actionsWith("host", fake)
|
|
actions.create("k", sendFields())
|
|
await settled()
|
|
fake.performs[0].reject(new Error("contact not ready"))
|
|
await settled()
|
|
expect(logLines.at(-1)).toBe("Action 1: performed without approval (mode host); failed: contact not ready.")
|
|
})
|
|
})
|