Sub topic 5
Asserting the expected topic
This chapter covers the expectedTopic field of a test case. It assumes you know what an Agentforce agent, a topic, and an action are. It also assumes you can read YAML and have access to an org with the Testing Center enabled. The chapter is about 1,250 words and focuses on how topic assertions work, how to write them, and where they commonly go wrong.
What a topic assertion checks
When a customer sends an utterance, the agent's planner first chooses a topic. A topic is a category of work. It has a description, a scope, and a set of instructions and actions. Only after that choice does the agent pick actions and write a reply.
The expected topic is your claim about that first decision. The Testing Center sends the utterance to the agent, records which topic the agent selected, and compares it with the value you wrote. If the two match, the topic check passes. If they differ, it fails.
This check is useful because it fails early and points to a specific cause. A wrong topic usually means one of three things:
- The utterance is ambiguous.
- Two topic descriptions overlap.
- A topic's description or scope does not describe the work it should handle.
Wrong actions or a wrong reply are often a downstream effect of a wrong topic. Checking the topic first narrows the search.
Writing the value
In a YAML spec, expectedTopic is a string on the test case. Use the topic's developer name, not its label. The developer name is the API-style name, such as Order_Status, with underscores and no spaces. The label is the friendly name shown in the builder, such as "Order Status". A test that uses the label can fail even though the agent behaved correctly.
name: Order_Support_Agent_Tests
description: Topic routing checks for the order support agent
subjectType: AGENT
subjectName: Order_Support_Agent
testCases:
- utterance: Where is my order 1001?
expectedTopic: Order_Status
expectedActions:
- Get_Order_Status
expectedOutcome: The agent tells the customer the current status of the order.
- utterance: I want to send back the shoes I bought last week
expectedTopic: Returns
expectedActions: []
expectedOutcome: The agent explains how to start a return.The topic names here are examples. They must match the developer names in your own agent. Copy them from the agent's topic list instead of typing them from memory. A one-character difference, such as Order_status for Order_Status, makes the test fail for a reason that has nothing to do with the agent.
When you build the test in the Testing Center UI, you pick the topic from the agent's own list, which avoids spelling mistakes. The YAML route gives you version control and review, but you have to keep the names in sync by hand.
Choosing utterances that test routing
A topic assertion is only as good as its utterance. A few guidelines help.
- Write the way customers write. Real customers use short, informal, incomplete messages. "where's my stuff" tests routing better than "Please provide the current delivery status of my order."
- Avoid copying the topic description. If your utterance repeats words from the topic's description, the test passes for the wrong reason. It shows the agent can match keywords, not that it understands intent.
- Vary the phrasing. Write several utterances for each topic. One may name the order number, one may not, and one may use slang or a typo.
- Test the boundary. Write utterances that sit near the edge between two topics, such as "I got the wrong item, where is my refund?" These show where your topic descriptions overlap.
- Keep each utterance to one intent. A message that asks two things can reasonably go to either topic. Then the assertion tells you little.
Aim for at least three utterances per topic, plus several for the boundaries between the topics that are easiest to confuse.
Asserting that the request is out of scope
Some requests should not be handled by any of your business topics. Examples are a question about pizza recipes or a request outside the agent's purpose. These need a topic assertion too.
Agentforce agents normally have a topic for this case. Depending on how your agent is configured, it may be a custom topic such as Off_Topic or a built-in fallback topic. Whichever you use, assert its developer name.
- utterance: What is the best pizza recipe?
expectedTopic: Off_Topic
expectedActions: []
expectedOutcome: The agent politely declines and explains what it can help with.Out-of-scope tests protect you from a common failure. An agent with a broad topic description may start answering questions it should refuse. The test fails when that happens. Check the exact fallback topic name in your agent before you write these cases, because it can differ between agents.
Topic checks and the rest of the case
The three expectations in a test case are evaluated separately. A case can pass the topic check and fail the action check, or the reverse. The results show each one, so you can see which part failed.
| Topic result | Actions result | Likely cause |
|---|---|---|
| Pass | Pass | The agent routed and acted as expected. |
| Fail | Fail | The wrong topic was chosen, so the wrong actions were available. Fix routing first. |
| Pass | Fail | The topic is right, but its instructions or action descriptions do not lead to the expected action. |
| Fail | Pass | Rare. The agent reached the right action through another topic, or the test names the wrong topic. Check both. |
Read topic failures before action failures. If the topic is wrong, fixing it often fixes the other results without any further change.
Diagnosing a topic failure
When the topic check fails, the results show the topic the agent actually chose. Compare it with your expectation and ask which side is wrong.
- Is the test wrong? Check the spelling and the developer name. Check whether the utterance is ambiguous enough that either topic is a fair answer. If so, rewrite the utterance.
- Is the topic description unclear? The planner relies on topic descriptions and scope to choose. Vague text such as "Handles customer questions" gives it little to use. Say what the topic covers and what it does not.
- Do two topics overlap? If the agent chose a neighboring topic, compare the two descriptions. Add a scope line to each that excludes the other's work.
- Was the topic changed recently? If a topic was renamed, split, or merged, old tests may point to a name that no longer exists.
Change one thing at a time and rerun, so you can tie the change in result to the change you made.
Edge cases
- Renamed topics. If you rename a topic's developer name, every test that mentions the old name fails. Search your spec files for the old name when you rename.
- Multi-turn conversations. A topic assertion applies to the utterance you send. If the agent's choice depends on earlier turns, a single-utterance test may route differently than a real conversation. Keep topic tests self-contained, and write each utterance so its intent is clear without history.
- Nondeterminism. Language models can answer the same utterance differently across runs. A test that passes once and fails once on the same utterance usually means the utterance is borderline. Treat it as a signal to sharpen the topic descriptions or the utterance, not as a reason to rerun until it passes.
- Test data. Order numbers and similar values in utterances do not need to exist for a routing test, but they may matter for the actions check. Keep them realistic.
Summary of practices
- Use the developer name of the topic, copied from the agent.
- Write customer-style utterances with one intent each.
- Include several phrasings per topic and some boundary cases.
- Assert the fallback topic for out-of-scope requests.
- Read topic results before action results.
- Update tests whenever a topic is renamed, split, or merged.
Exercise
A test case has the utterance "I need to change my delivery address" and expects the topic Order Status (with a space). The agent routes it correctly to its order topic, but the test fails on the topic check. Give two possible causes and the fix for each. Then write the corrected expectedTopic line, assuming the developer name is Order_Status.
Show answer
Cause 1: the test uses the label ("Order Status") instead of the developer name, so the strings do not match even though the agent chose the right topic. Fix: use the developer name. Cause 2: the agent actually chose a different topic, such as an address-change or account topic, because the utterance is about changing data, not checking status. Fix: check the topic shown in the results. If the other topic is the right place for this request, change the expectation to that topic. If not, clarify the topic descriptions. Corrected line: expectedTopic: Order_Status.
Exercise
You have two topics, Returns and Refunds. Describe how you would write test cases that show whether their descriptions overlap, and what you would change if the tests fail.
Show answer
Write several utterances for each topic, plus boundary utterances that mention both ideas, such as "I sent the item back, when do I get my money?". Assert the topic you intend for each. If boundary cases route to the wrong topic or inconsistently, the descriptions overlap. Fix this by adding explicit scope to each description: for example, Returns covers starting and shipping a return, and Refunds covers payment status after a return. Then rerun the same tests and confirm they pass.