|
| 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 '../validators/state-machine-next-values'; |
| 11 | +import { stateMachineNextValues as fromPackageRoot } from '../../index'; |
| 12 | + |
| 13 | +// The showcase task's machine (examples/app-showcase task.object.ts). |
| 14 | +const transitions = { |
| 15 | + backlog: ['todo'], |
| 16 | + todo: ['in_progress', 'backlog'], |
| 17 | + in_progress: ['in_review', 'todo'], |
| 18 | + in_review: ['done', 'in_progress'], |
| 19 | + done: ['in_progress'], |
| 20 | +}; |
| 21 | + |
| 22 | +const withRule = (rule: Record<string, unknown>) => ({ |
| 23 | + validations: [{ type: 'state_machine', field: 'status', transitions, ...rule }], |
| 24 | +}); |
| 25 | + |
| 26 | +const sorted = (s: Set<string> | null) => (s ? [...s].sort() : s); |
| 27 | + |
| 28 | +describe('stateMachineNextValues', () => { |
| 29 | + it('is published from the package root', () => { |
| 30 | + expect(fromPackageRoot).toBe(stateMachineNextValues); |
| 31 | + }); |
| 32 | + |
| 33 | + it('offers the persisted value plus its declared next states', () => { |
| 34 | + expect(sorted(stateMachineNextValues(withRule({}), 'status', 'backlog'))).toEqual([ |
| 35 | + 'backlog', |
| 36 | + 'todo', |
| 37 | + ]); |
| 38 | + }); |
| 39 | + |
| 40 | + it('narrows on an explicit error-severity, active, update-event rule', () => { |
| 41 | + const rule = withRule({ severity: 'error', active: true, events: ['update'] }); |
| 42 | + expect(sorted(stateMachineNextValues(rule, 'status', 'in_review'))).toEqual([ |
| 43 | + 'done', |
| 44 | + 'in_progress', |
| 45 | + 'in_review', |
| 46 | + ]); |
| 47 | + }); |
| 48 | + |
| 49 | + it('does not narrow on a warning or info machine — the server never blocks it', () => { |
| 50 | + expect(stateMachineNextValues(withRule({ severity: 'warning' }), 'status', 'backlog')).toBeNull(); |
| 51 | + expect(stateMachineNextValues(withRule({ severity: 'info' }), 'status', 'backlog')).toBeNull(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('does not narrow on an inactive rule', () => { |
| 55 | + expect(stateMachineNextValues(withRule({ active: false }), 'status', 'backlog')).toBeNull(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('does not narrow on a rule whose events leave out update', () => { |
| 59 | + expect(stateMachineNextValues(withRule({ events: ['insert'] }), 'status', 'backlog')).toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('reads an empty events list as the spec default, which covers update', () => { |
| 63 | + expect(sorted(stateMachineNextValues(withRule({ events: [] }), 'status', 'done'))).toEqual([ |
| 64 | + 'done', |
| 65 | + 'in_progress', |
| 66 | + ]); |
| 67 | + }); |
| 68 | + |
| 69 | + it('leaves an undeclared from-state unconstrained', () => { |
| 70 | + expect(stateMachineNextValues(withRule({}), 'status', 'archived')).toBeNull(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('leaves an empty persisted value unconstrained', () => { |
| 74 | + expect(stateMachineNextValues(withRule({}), 'status', null)).toBeNull(); |
| 75 | + expect(stateMachineNextValues(withRule({}), 'status', undefined)).toBeNull(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('intersects several blocking rules on the same field', () => { |
| 79 | + const schema = { |
| 80 | + validations: [ |
| 81 | + { type: 'state_machine', field: 'status', transitions }, |
| 82 | + { type: 'state_machine', field: 'status', transitions: { in_review: ['done'] } }, |
| 83 | + ], |
| 84 | + }; |
| 85 | + expect(sorted(stateMachineNextValues(schema, 'status', 'in_review'))).toEqual([ |
| 86 | + 'done', |
| 87 | + 'in_review', |
| 88 | + ]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('ignores a warning rule beside a blocking one', () => { |
| 92 | + const schema = { |
| 93 | + validations: [ |
| 94 | + { type: 'state_machine', field: 'status', transitions }, |
| 95 | + { |
| 96 | + type: 'state_machine', |
| 97 | + field: 'status', |
| 98 | + severity: 'warning', |
| 99 | + transitions: { in_review: [] }, |
| 100 | + }, |
| 101 | + ], |
| 102 | + }; |
| 103 | + expect(sorted(stateMachineNextValues(schema, 'status', 'in_review'))).toEqual([ |
| 104 | + 'done', |
| 105 | + 'in_progress', |
| 106 | + 'in_review', |
| 107 | + ]); |
| 108 | + }); |
| 109 | + |
| 110 | + it('ignores rules on other fields and rules of other types', () => { |
| 111 | + const schema = { |
| 112 | + validations: [ |
| 113 | + { type: 'state_machine', field: 'phase', transitions }, |
| 114 | + { type: 'script', field: 'status', condition: 'true' }, |
| 115 | + ], |
| 116 | + }; |
| 117 | + expect(stateMachineNextValues(schema, 'status', 'backlog')).toBeNull(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments