Test and debug agent functions
Test an agent function with the Run button while you build it, and add console logs to see what it did, both in the editor and in live conversations.
Test with Run
Click Run in the function editor, enter a value for each input, and run it. Run uses the saved code, so save first.
Each input has its own field. Type numbers as numbers, true or false for a boolean, and JSON for an object or array, such as { "city": "Pune" } or ["red", "blue"]. Leave an optional input blank to leave it out. Click Edit as JSON to type all the inputs as one JSON object instead; a function with nested inputs, or with no declared inputs, always uses JSON.
Run is off, and its tooltip says why, while there are unsaved changes, errors or warnings in Problems, or a required input is empty.
The panel shows:
- Sent to the user: each message the function sent, as it's sent (runtime 2026.10.19 and later). Quick replies, cards, images, videos and files are shown with their kind.
- Result: what the function returned, or the error that stopped it
- Then: what happens after the function returns, when it returned an action: Halts the agent's turn, Ends the call or Transfers the call to …. Nothing is dialled during a test run.
- Console: the lines the function logged
- Variable updates: the global variables the function would change
- How long it took, and how much of that was your code running ("120 ms · 4 ms CPU")
A yielded value that isn't a message is listed as Not a message: an agent wouldn't send it. When the function is stopped by a limit, the panel says which one, for example "Stopped after 60 s". The panel keeps your last five runs.
A test run is real for everything except variables. Table changes and API calls happen exactly as they would in a conversation, so test against data you don't mind changing. Variable updates are only listed, never saved.
During a test run there's no conversation, so context.invokedBy is "dev-run" and sessionId, messageId and userMessage are empty.
Write tests
A test file sits beside the function and checks it with fixed inputs, so you can change the code and know it still works. Open it from the Tests tab. If you don't see the tab, tests aren't turned on for your agent yet.
In a test, every table and API call is answered by a mock you declare, so nothing real is written or called. A call with no mock fails the test.
These tests are for the order lookup in Handle errors → Catch failing calls, with orderId declared as a required input.
import { describe, test, expect, mock, run, beforeEach } from "@yellow/bot/test";
describe("order status", () => {
beforeEach(() => {
mock.apis.execute("getOrderStatus").returns({ status: 200, headers: {}, body: { status: "shipped" } });
});
test("returns the status", async () => {
const res = await run({ orderId: "A-1001" });
expect(res.result).toEqual({ ok: true, status: "shipped" });
expect(mock.apis.execute("getOrderStatus")).toHaveBeenCalledWith({ orderId: "A-1001" });
});
test("handles a missing order", async () => {
mock.apis.execute("getOrderStatus").throws("api 'getOrderStatus' returned HTTP 404: not found");
const res = await run({ orderId: "A-9999" });
expect(res.result).toMatchObject({ ok: false, reason: "not_found" });
});
test("refuses a call without an order number", async () => {
await expect(run({})).rejects.toThrow("orderId");
});
});
Click Run tests to run the saved test file against the saved function. Each test shows Passed, Failed or Skipped, with what was expected and what came back, the calls the function made, and its logs. Save as test on a run in the Run panel adds a test for that run.
In @yellow/bot/test | What it does |
|---|---|
run(inputs, { globals, context }) | Runs the function. Inputs are checked as an agent's call would be. globals are the values variables.global.get returns; context overrides fields such as sender or callSid. Resolves with { result, variableUpdates, logs, calls, messages }, or rejects with the function's error. |
mock.tables.search(table, filter?), mock.tables.insert(table, row?), mock.tables.update(table, filter?) | Answer table calls. The optional second argument only matches calls that contain it. |
mock.apis.execute(api, parameters?) | Answer API calls. Return the whole response: { status, headers, body }. |
.returns(value), .throws(message), .once() | What a mock answers: a value, an error, or only the next call |
describe, test (or it), .skip, .only, test.each(rows), beforeEach, afterEach | Organise tests |
expect(value) with .not, .resolves, .rejects | toBe, toEqual, toStrictEqual, toMatchObject, toContain, toContainEqual, toHaveLength, toHaveProperty, toBeTruthy, toBeFalsy, toBeNull, toBeUndefined, toBeDefined, toBeGreaterThan(OrEqual), toBeLessThan(OrEqual), toMatch, toThrow; on a mock, toHaveBeenCalled, toHaveBeenCalledTimes, toHaveBeenCalledWith |
On what run() resolved with | toHaveSent(text), toHaveHaltedTurn(result?), toEndCall(), toTransferCall(options?) |
This one is for the function in Work with voice calls:
import { test, expect, run } from "@yellow/bot/test";
test("says goodbye and hangs up on a call", async () => {
const res = await run({ wantsAgent: false }, { context: { callSid: "CA123" } });
expect(res).toHaveSent("Thanks for calling. Goodbye!");
expect(res).toEndCall();
});
Limits in a test: each test gets 3 seconds and a file 30 seconds; up to 100 tests per file; the 5-calls-per-kind limit counts per test.
Log with console
Each log line takes a short label and, optionally, one value. The label is the line's name; the value sits behind it.
console.log("Orders found", orders.length);
console.info("Customer details", { email, tier });
console.warn("Phone number missing");
console.error("Payment API failed", error);
console.debug and console.trace work too.
- Errors show their message.
console.error("Payment API failed", error)recordsError: <message>, including the status and body of a failed API call. - Objects are converted to text, and the value is shortened after 300 characters.
- One value per line. To log several values, group them into one object:
console.log("Found", { count, email }). A line with more than two arguments, or with a label that isn't text, isn't logged; aconsole-misusewarning appears in its place. - Labels can be up to 100 characters, and a run keeps up to 30 lines. Lines after the 30th are dropped, and a
logs-droppedline says so. - A
consolecall never stops the function, even a wrong one.
Logs in live conversations
When an agent calls a function, or a flow reaches a Function node, open the conversation in Analysis to see what the function did. Its rows appear under the step that ran it: Tool: <function name> for an agent, or the Function node in a flow.
- Each
consoleline, at the moment it was logged. - One row per database call, for example Database SEARCH: 1 record(s) found or Database INSERT: record inserted, with the table, the number of records and how long it took.
- One row per API call, for example Executed API getOrderStatus. Returned HTTP 200 or Failed with HTTP 401, with the status code and how long it took.
On staging and sandbox agents you can open each row to see the details: the query and response for a database call, the response for an API call, and the value you logged. Production agents show the rows and their numbers but hide the details. Header and parameter values that look like credentials, such as Authorization or cookie, are always masked.
Test runs from the Run button don't write to Analysis.