|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect } from 'vitest'; |
| 10 | +import { stateMachineNextValues } from './inline-edit-options'; |
| 11 | + |
| 12 | +// Mirrors examples/app-showcase task.object.ts — the state machine that rejected |
| 13 | +// done → in_review live (done only transitions to in_progress). |
| 14 | +const taskSchema = { |
| 15 | + validations: [ |
| 16 | + { |
| 17 | + type: 'state_machine', |
| 18 | + field: 'status', |
| 19 | + transitions: { |
| 20 | + backlog: ['todo'], |
| 21 | + todo: ['in_progress', 'backlog'], |
| 22 | + in_progress: ['in_review', 'todo'], |
| 23 | + in_review: ['done', 'in_progress'], |
| 24 | + done: ['in_progress'], |
| 25 | + }, |
| 26 | + }, |
| 27 | + ], |
| 28 | +}; |
| 29 | + |
| 30 | +describe('stateMachineNextValues', () => { |
| 31 | + it('returns the current value plus its allowed transitions', () => { |
| 32 | + const r = stateMachineNextValues(taskSchema, 'status', 'in_review'); |
| 33 | + expect(r).not.toBeNull(); |
| 34 | + expect([...(r as Set<string>)].sort()).toEqual(['done', 'in_progress', 'in_review']); |
| 35 | + }); |
| 36 | + |
| 37 | + it('constrains a near-terminal state to itself + its one valid move', () => { |
| 38 | + // The exact live bug: from `done` the only valid move is `in_progress`, |
| 39 | + // so `in_review` must NOT be offered. |
| 40 | + const r = stateMachineNextValues(taskSchema, 'status', 'done'); |
| 41 | + expect([...(r as Set<string>)].sort()).toEqual(['done', 'in_progress']); |
| 42 | + expect((r as Set<string>).has('in_review')).toBe(false); |
| 43 | + }); |
| 44 | + |
| 45 | + it('always includes the current value so it stays selectable', () => { |
| 46 | + const r = stateMachineNextValues(taskSchema, 'status', 'backlog'); |
| 47 | + expect((r as Set<string>).has('backlog')).toBe(true); |
| 48 | + expect((r as Set<string>).has('todo')).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns null (unconstrained) for a field with no state machine', () => { |
| 52 | + expect(stateMachineNextValues(taskSchema, 'priority', 'medium')).toBeNull(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns null when the current state is undeclared (lenient, mirrors the engine)', () => { |
| 56 | + expect(stateMachineNextValues(taskSchema, 'status', 'archived')).toBeNull(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns only the current value for a terminal state (no outgoing edges)', () => { |
| 60 | + const terminal = { |
| 61 | + validations: [{ type: 'state_machine', field: 'status', transitions: { done: [] } }], |
| 62 | + }; |
| 63 | + const r = stateMachineNextValues(terminal, 'status', 'done'); |
| 64 | + expect([...(r as Set<string>)]).toEqual(['done']); |
| 65 | + }); |
| 66 | + |
| 67 | + it('returns null for missing/empty schema or validations', () => { |
| 68 | + expect(stateMachineNextValues(null, 'status', 'done')).toBeNull(); |
| 69 | + expect(stateMachineNextValues({}, 'status', 'done')).toBeNull(); |
| 70 | + expect(stateMachineNextValues({ validations: [] }, 'status', 'done')).toBeNull(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('coerces non-string transition values to strings', () => { |
| 74 | + const numeric = { |
| 75 | + validations: [{ type: 'state_machine', field: 'level', transitions: { 1: [2, 3] } }], |
| 76 | + }; |
| 77 | + const r = stateMachineNextValues(numeric, 'level', 1); |
| 78 | + expect([...(r as Set<string>)].sort()).toEqual(['1', '2', '3']); |
| 79 | + }); |
| 80 | +}); |
0 commit comments