Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions test/machine/MermaidVisualization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,15 @@ const inspection: InspectionApi<TestMachine, { readonly active: boolean }> = {
branches: [{ type: "direct", target: "Root.Route" }]
}
],
activityDefinitions: () => [{ source: "Root.Route", id: "worker %%\nend note", type: "process" }],
activityDefinitions: () => [
{
source: "Root",
id: "search",
type: "machine",
child: { id: "search", machineId: null }
},
{ source: "Root.Route", id: "worker %%\nend note", type: "process" }
],
configuration: () => states.slice(0, 2),
enabled: () => ["Continue %%\nnow"]
}
Expand All @@ -74,7 +82,11 @@ describe("Mermaid visualization", () => {

assert.notInclude(rendered, "%%")
assert.include(rendered, "accTitle: Unsafe #37;#37; Machine")
assert.include(rendered, "state \"● Quoted #quot;root#quot; #37;#37; (Root)\" as state_0")
assert.include(
rendered,
"state \"● Quoted #quot;root#quot; #37;#37; (Root) · machine / search → search\" as state_0"
)
assert.notInclude(rendered, "state_0: machine / search → search")
assert.include(rendered, "state \"● Choose route (Route)\" as state_1")
assert.include(rendered, "state state_1 <<choice>>")
assert.include(rendered, "state_1 --> state_2: choice [approved #37;#37; now]")
Expand Down
13 changes: 9 additions & 4 deletions test/machine/visualization/mermaid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,24 @@ export const makeMermaidRenderer = <Machine extends MachineValue, Snapshot>(
const id = ids.get(node.path)
if (id === undefined) return

lines.push(`${indent(depth)}state "${stateLabel(node, active)}" as ${id}`)
const ownedActivities = activities.get(node.path) ?? []
if (ownedActivities.length > 0) {
const descendants = children.get(node.path) ?? []
const renderedActivities = ownedActivities.map((activity) => escapeText(activityLabel(activity))).join(" · ")
const label = descendants.length > 0 && renderedActivities.length > 0
? `${stateLabel(node, active)} · ${renderedActivities}`
: stateLabel(node, active)

lines.push(`${indent(depth)}state "${label}" as ${id}`)
if (descendants.length === 0 && renderedActivities.length > 0) {
lines.push(
`${indent(depth)}${id}: ${ownedActivities.map((activity) => escapeText(activityLabel(activity))).join(" · ")}`
`${indent(depth)}${id}: ${renderedActivities}`
)
}
if (node.type === "choice") {
lines.push(`${indent(depth)}state ${id} <<choice>>`)
return
}

const descendants = children.get(node.path) ?? []
if (descendants.length > 0) {
lines.push(`${indent(depth)}state ${id} {`)
if (node.type === "parallel") {
Expand Down