|
| 1 | +/** |
| 2 | + * Tests for deriveStateMachines — validates field matching, initial/final |
| 3 | + * classification, transition extraction, and current-state resolution. |
| 4 | + */ |
| 5 | +import { deriveStateMachines } from "~/hooks/useStateMachines"; |
| 6 | +import type { ObjectMeta } from "~/hooks/useObjectMeta"; |
| 7 | +import type { FieldDefinition } from "~/components/renderers"; |
| 8 | + |
| 9 | +const META: ObjectMeta = { |
| 10 | + name: "crm_opportunity", |
| 11 | + stateMachines: { |
| 12 | + lifecycle: { |
| 13 | + id: "opportunity_pipeline", |
| 14 | + initial: "prospecting", |
| 15 | + states: { |
| 16 | + prospecting: { |
| 17 | + on: { |
| 18 | + QUALIFY: { target: "qualification", description: "Qualified" }, |
| 19 | + LOSE: { target: "closed_lost", description: "Lost early" }, |
| 20 | + }, |
| 21 | + }, |
| 22 | + qualification: { on: { WIN: { target: "closed_won" } } }, |
| 23 | + closed_won: {}, |
| 24 | + closed_lost: {}, |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | +} as unknown as ObjectMeta; |
| 29 | + |
| 30 | +const FIELDS: FieldDefinition[] = [ |
| 31 | + { name: "name", type: "text" } as FieldDefinition, |
| 32 | + { |
| 33 | + name: "stage", |
| 34 | + type: "select", |
| 35 | + options: [ |
| 36 | + { value: "prospecting", label: "Prospecting" }, |
| 37 | + { value: "qualification", label: "Qualification" }, |
| 38 | + { value: "closed_won", label: "Won" }, |
| 39 | + { value: "closed_lost", label: "Lost" }, |
| 40 | + ], |
| 41 | + } as FieldDefinition, |
| 42 | +]; |
| 43 | + |
| 44 | +describe("deriveStateMachines", () => { |
| 45 | + it("derives states, transitions, and current state from a matched field", () => { |
| 46 | + const result = deriveStateMachines(META, FIELDS, { stage: "qualification" }); |
| 47 | + expect(result).toHaveLength(1); |
| 48 | + const sm = result[0]; |
| 49 | + |
| 50 | + expect(sm.key).toBe("lifecycle"); |
| 51 | + expect(sm.field).toBe("stage"); |
| 52 | + expect(sm.currentState).toBe("qualification"); |
| 53 | + |
| 54 | + // Initial / normal / final classification. |
| 55 | + const byName = Object.fromEntries(sm.states.map((s) => [s.name, s])); |
| 56 | + expect(byName.prospecting.type).toBe("initial"); |
| 57 | + expect(byName.qualification.type).toBe("normal"); |
| 58 | + expect(byName.closed_won.type).toBe("final"); |
| 59 | + expect(byName.closed_lost.type).toBe("final"); |
| 60 | + |
| 61 | + // Labels come from the matched field's option labels. |
| 62 | + expect(byName.closed_won.label).toBe("Won"); |
| 63 | + |
| 64 | + // Transitions carry event + description. |
| 65 | + expect(sm.transitions).toContainEqual({ |
| 66 | + from: "prospecting", |
| 67 | + to: "qualification", |
| 68 | + event: "QUALIFY", |
| 69 | + label: "Qualified", |
| 70 | + }); |
| 71 | + expect(sm.transitions.filter((t) => t.from === "prospecting")).toHaveLength(2); |
| 72 | + }); |
| 73 | + |
| 74 | + it("returns no current state when no field matches the state names", () => { |
| 75 | + const result = deriveStateMachines(META, [FIELDS[0]], { stage: "qualification" }); |
| 76 | + expect(result[0].field).toBeUndefined(); |
| 77 | + expect(result[0].currentState).toBeUndefined(); |
| 78 | + // Falls back to humanized labels without a matched field. |
| 79 | + const won = result[0].states.find((s) => s.name === "closed_won"); |
| 80 | + expect(won?.label).toBe("Closed Won"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("returns an empty array when the object has no state machines", () => { |
| 84 | + expect(deriveStateMachines({ name: "x" } as ObjectMeta, FIELDS, {})).toEqual([]); |
| 85 | + expect(deriveStateMachines(null, FIELDS, null)).toEqual([]); |
| 86 | + }); |
| 87 | +}); |
0 commit comments