Sub topic 14
Testing an agent before activation
This chapter assumes you know what topics, instructions, and actions are. It also assumes you know the difference between a service agent and an employee agent. You should be comfortable reading a Flow or an Apex invocable method.
Why test before activation
An agent is not deterministic code. The reasoning engine reads the user's message, picks a topic, and chooses actions. A small change in wording can send a conversation down a different path. Testing before activation catches wrong topic choices, missing actions, and instructions that the agent ignores.
Test in a sandbox or a scratch org first. Use realistic data, because an action that returns "Order not found" for every input will look broken even when the agent is fine.
Test in the builder first
The agent builder includes a conversation preview. You type as a customer or employee, and the agent answers. The preview also shows a trace of what happened:
- Which topic the engine selected.
- Which instructions it applied.
- Which actions it called, with their inputs and outputs.
- What response it produced.
Use the trace to find the cause of a bad answer. A wrong answer with the right topic usually points to unclear instructions. A wrong answer with the wrong topic usually points to a weak classification description.
What to test
Cover each of these areas for both agent types.
| Area | Example check |
|---|---|
| Topic selection | "Where is my order?" selects Order Status, not another topic. |
| Action inputs | The agent asks for the order number when you leave it out. |
| Action output | The agent reports only the status the action returned. |
| Scope limits | A refund question in Order Status is refused or redirected. |
| Error paths | "Order not found" leads to one retry, then an offer of a human. |
| Access | The action returns only records the tested user may see. |
For a service agent, also test escalation. Confirm the handoff reaches the right Omni-Channel queue with the conversation context.
For an employee agent, test with more than one user. Log in as, or run the preview as, users with different permission sets. The same question should return different data, or a refusal, depending on who asks.
Test actions separately
Test a custom action on its own before you blame the agent. For the Apex example from the earlier chapter, write a unit test that passes several requests in one call. This checks that the method is bulk-safe.
@IsTest
private class GetOrderStatusActionTest {
@IsTest
static void returnsNotFoundForUnknownOrder() {
GetOrderStatusAction.Request req = new GetOrderStatusAction.Request();
req.orderNumber = 'DOES-NOT-EXIST';
Test.startTest();
List<GetOrderStatusAction.Response> results =
GetOrderStatusAction.getStatus(new List<GetOrderStatusAction.Request>{ req });
Test.stopTest();
System.assertEquals(1, results.size());
System.assertEquals('Order not found', results[0].status);
}
}Common errors
- Testing only the happy path. Add missing inputs, off-topic questions, and rephrased requests.
- Testing as an administrator. Administrators see almost everything, so access problems stay hidden. Test as a typical user.
- Changing several things at once. Change one instruction or description, then retest, so you know which change helped.
- Treating one pass as proof. Responses can vary. Run important cases more than once.
Exercise
In the preview trace, the agent picked the correct topic but reported a refund status you did not want it to discuss. Which part of the agent do you change first, and why?
Show answer
Change the topic's instructions (and its scope). The topic selection was correct, so the classification description is not the cause. The instructions need an explicit limit, such as "Do not discuss refunds or cancellations in this topic." Then rerun the same conversation to confirm the fix.
Exercise
Why should you test an employee agent as more than one user?
Show answer
An employee agent's actions run under the user's own permissions. Different users can see different records, so the same question can produce different results. Testing with only one user, especially an administrator, hides access problems.