|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#14311] The New Project wizard may not offer a status the state machine |
| 5 | + * refuses on create. |
| 6 | + * |
| 7 | + * The wizard's second step listed `status`, and a `select` renders its whole |
| 8 | + * option list — all five project statuses. `project_status_flow` declares |
| 9 | + * `initialStates: ['planned']`, so four of those five were dead ends: the |
| 10 | + * wizard accepted the pick, walked the author through a third step, and only |
| 11 | + * then answered `400 VALIDATION_FAILED` from the create. A demo of |
| 12 | + * "state machine + wizard" that demos a dead end teaches the wrong thing. |
| 13 | + * |
| 14 | + * These tests read the REAL page and the REAL object rather than a copy of |
| 15 | + * either, so the invariant is checked against what the app actually ships: |
| 16 | + * widening `initialStates`, re-adding the field, or adding a status option |
| 17 | + * re-opens the question here instead of rotting silently. |
| 18 | + * |
| 19 | + * The last test is the end-to-end half, on the production harness (real |
| 20 | + * `ObjectQL`, real `SqlDriver`, the app's REAL object): the create the wizard |
| 21 | + * now performs succeeds, the one it used to allow is refused, and the refusal |
| 22 | + * carries the field location and the legal initial states a form needs to act |
| 23 | + * on it. Asserting only "it throws" would pass against a rejection for any |
| 24 | + * other reason — including the `required` check, which is what a naive "just |
| 25 | + * drop the field" fix would have tripped. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 29 | +import { ObjectQL } from '@objectstack/objectql'; |
| 30 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 31 | + |
| 32 | +import { Account, Project } from '../src/data/objects/index.js'; |
| 33 | +import { NewProjectWizardPage } from '../src/ui/pages/new-project-wizard.page.js'; |
| 34 | +import { ShowcaseTranslationBundle } from '../src/system/translations/index.js'; |
| 35 | + |
| 36 | +type Rule = { |
| 37 | + type?: string; |
| 38 | + name?: string; |
| 39 | + field?: string; |
| 40 | + initialStates?: string[]; |
| 41 | + message?: string; |
| 42 | +}; |
| 43 | + |
| 44 | +const APP_ID = 'com.objectstack.showcase'; |
| 45 | +const PACKAGE_ID = `app:${APP_ID}`; |
| 46 | +const ctx = { context: { userId: 'u_showcase', isSystem: true } }; |
| 47 | + |
| 48 | +const openEngines: ObjectQL[] = []; |
| 49 | +afterEach(async () => { |
| 50 | + while (openEngines.length) { |
| 51 | + try { await openEngines.pop()?.destroy(); } catch { /* noop */ } |
| 52 | + } |
| 53 | +}); |
| 54 | + |
| 55 | +/** |
| 56 | + * The showcase's real objects on a real engine — same wiring as |
| 57 | + * `hook-body-persisted-writes.test.ts`. `showcase_project.account` is a |
| 58 | + * REQUIRED lookup, so `Account` is registered too and a real row is created: |
| 59 | + * a rejection for a dangling reference would otherwise be indistinguishable |
| 60 | + * from the state-machine refusal this test is about. |
| 61 | + */ |
| 62 | +async function bootShowcase(): Promise<ObjectQL> { |
| 63 | + const driver = new SqlDriver({ |
| 64 | + client: 'better-sqlite3', |
| 65 | + connection: { filename: ':memory:' }, |
| 66 | + useNullAsDefault: true, |
| 67 | + }); |
| 68 | + await driver.connect(); |
| 69 | + |
| 70 | + const engine = new ObjectQL(); |
| 71 | + openEngines.push(engine); |
| 72 | + engine.registerDriver(driver as never, true); |
| 73 | + await engine.init(); |
| 74 | + for (const def of [Account, Project]) { |
| 75 | + engine.registry.registerObject(def as never, PACKAGE_ID, 'showcase'); |
| 76 | + } |
| 77 | + await engine.syncSchemas(); |
| 78 | + return engine; |
| 79 | +} |
| 80 | + |
| 81 | +/** The `project_status_flow` state machine, read off the real object. */ |
| 82 | +const statusRule = ((Project as unknown as { validations?: Rule[] }).validations ?? []).find( |
| 83 | + (r) => r?.type === 'state_machine' && r?.field === 'status', |
| 84 | +)!; |
| 85 | + |
| 86 | +/** Every field the wizard's create form exposes, across all of its steps. */ |
| 87 | +function wizardFields(): string[] { |
| 88 | + const regions = (NewProjectWizardPage as unknown as { |
| 89 | + regions?: Array<{ components?: Array<{ type?: string; properties?: Record<string, unknown> }> }>; |
| 90 | + }).regions ?? []; |
| 91 | + const out: string[] = []; |
| 92 | + for (const region of regions) { |
| 93 | + for (const component of region.components ?? []) { |
| 94 | + if (component?.type !== 'object-form') continue; |
| 95 | + const sections = (component.properties?.sections ?? []) as Array<{ fields?: string[] }>; |
| 96 | + for (const section of sections) out.push(...(section.fields ?? [])); |
| 97 | + } |
| 98 | + } |
| 99 | + return out; |
| 100 | +} |
| 101 | + |
| 102 | +/** The declared option values of a select field on the real object. */ |
| 103 | +function optionValues(field: string): string[] { |
| 104 | + const def = (Project as unknown as { |
| 105 | + fields?: Record<string, { options?: Array<{ value?: string } | string> }>; |
| 106 | + }).fields?.[field]; |
| 107 | + return (def?.options ?? []).map((o) => (typeof o === 'object' && o !== null ? String(o.value) : String(o))); |
| 108 | +} |
| 109 | + |
| 110 | +describe('#14311 — the New Project wizard and the status state machine', () => { |
| 111 | + it('the premise: the object still constrains which status a project may be created in', () => { |
| 112 | + // If this ever stops holding, the rest of this file is asserting nothing. |
| 113 | + expect(statusRule?.name).toBe('project_status_flow'); |
| 114 | + expect(statusRule?.initialStates).toEqual(['planned']); |
| 115 | + expect((statusRule as { events?: string[] }).events).toContain('insert'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('the wizard does not offer a status the machine refuses on create', () => { |
| 119 | + const offered = wizardFields(); |
| 120 | + const initial = statusRule.initialStates ?? []; |
| 121 | + const refusable = optionValues('status').filter((v) => !initial.includes(v)); |
| 122 | + |
| 123 | + // More than one legal initial state would make a narrowed select the right |
| 124 | + // shape; with exactly one, the field must simply not be asked. |
| 125 | + expect(refusable.length).toBeGreaterThan(0); |
| 126 | + expect(initial).toHaveLength(1); |
| 127 | + expect(offered).not.toContain('status'); |
| 128 | + }); |
| 129 | + |
| 130 | + it('the value the wizard relies on is the machine entry point (a default, not a copy of it)', () => { |
| 131 | + // Omitting the field only works because the object DEFAULTS it, and only |
| 132 | + // stays correct because the default IS the declared initial state. |
| 133 | + const def = (Project as unknown as { |
| 134 | + fields?: Record<string, { options?: Array<{ value?: string; default?: boolean }> }>; |
| 135 | + }).fields?.status; |
| 136 | + const defaulted = (def?.options ?? []).filter((o) => o?.default).map((o) => String(o.value)); |
| 137 | + expect(defaulted).toEqual(statusRule.initialStates); |
| 138 | + }); |
| 139 | + |
| 140 | + it('EVERY rule on the object is on the translation channel in both shipped locales', () => { |
| 141 | + // An authored `validations[].message` is emitted VERBATIM unless the bundle |
| 142 | + // carries `objects.<o>._validations.<rule>.message` (#14253). Scoped to the |
| 143 | + // whole object rather than to the status rule on purpose: this one wizard |
| 144 | + // can also trip `end_after_start` and `spent_within_budget` from its |
| 145 | + // budget/schedule step, so pinning only the status rule would let the single |
| 146 | + // English sentence move one step later instead of disappearing. |
| 147 | + const rules = ((Project as unknown as { validations?: Rule[] }).validations ?? []) |
| 148 | + .filter((r) => typeof r?.name === 'string'); |
| 149 | + expect(rules.length).toBeGreaterThan(1); |
| 150 | + |
| 151 | + for (const rule of rules) { |
| 152 | + const name = rule.name!; |
| 153 | + for (const locale of ['en', 'zh-CN'] as const) { |
| 154 | + const entry = (ShowcaseTranslationBundle as any)[locale] |
| 155 | + ?.objects?.showcase_project?._validations?.[name]; |
| 156 | + expect(entry?.message, `${locale} is missing a message for ${name}`).toBeTruthy(); |
| 157 | + } |
| 158 | + // The zh-CN entry must actually BE Chinese — an English copy satisfies |
| 159 | + // "a key exists" while reproducing the defect exactly. |
| 160 | + const zh = (ShowcaseTranslationBundle as any)['zh-CN'] |
| 161 | + .objects.showcase_project._validations[name].message as string; |
| 162 | + expect(zh, `${name}'s zh-CN message is not Chinese`).toMatch(/[一-龥]/); |
| 163 | + expect(zh, `${name}'s zh-CN message is a copy of the authored one`).not.toBe(rule.message); |
| 164 | + } |
| 165 | + }); |
| 166 | + |
| 167 | + it('creates with the wizard payload and refuses the status it used to offer', async () => { |
| 168 | + const engine = await bootShowcase(); |
| 169 | + const account: any = await engine.insert( |
| 170 | + 'showcase_account', { name: 'Northwind' }, ctx as never, |
| 171 | + ); |
| 172 | + |
| 173 | + // What the wizard now sends: no `status` at all. |
| 174 | + const created: any = await engine.insert( |
| 175 | + 'showcase_project', |
| 176 | + { name: 'Wizard smoke', account: String(account.id), health: 'green' }, |
| 177 | + ctx as never, |
| 178 | + ); |
| 179 | + expect(created.status).toBe('planned'); |
| 180 | + |
| 181 | + // What it used to let an author send from step 2. |
| 182 | + let thrown: any; |
| 183 | + try { |
| 184 | + await engine.insert( |
| 185 | + 'showcase_project', |
| 186 | + { name: 'Born active', account: String(account.id), status: 'active' }, |
| 187 | + ctx as never, |
| 188 | + ); |
| 189 | + } catch (e) { thrown = e; } |
| 190 | + |
| 191 | + expect(thrown, 'expected the create to be refused').toBeDefined(); |
| 192 | + // ADR-0112 envelope — REST maps this to `400 VALIDATION_FAILED` verbatim. |
| 193 | + expect(thrown.code).toBe('VALIDATION_FAILED'); |
| 194 | + const field = thrown.fields?.find((f: any) => f.field === 'status'); |
| 195 | + // Field-located, so a multi-step form can jump to the step that owns it. |
| 196 | + expect(field, 'the refusal must name the field it is about').toBeDefined(); |
| 197 | + expect(field.code).toBe('invalid_initial_state'); |
| 198 | + // #14311 — the facts ride along with the AUTHORED message, so a form can |
| 199 | + // name the legal entry points without parsing the sentence. |
| 200 | + expect(field.constraint).toEqual({ allowed: 'planned' }); |
| 201 | + expect(field.value).toBe('active'); |
| 202 | + }, 30000); |
| 203 | +}); |
0 commit comments