|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#27485 — a formula that does not parse keeps the field's stored |
| 5 | + * `returnType`. |
| 6 | + * |
| 7 | + * The inspector stamps `returnType` from the formula's inferred CEL type once |
| 8 | + * the author edits it. A source with a parse fault infers no type (`unknown`), |
| 9 | + * so every keystroke that broke the formula cleared the stored type, and the |
| 10 | + * broken body reached the server without it. The stamp is now settled from the |
| 11 | + * editor's two verdicts on one source: a source with a lint error proves no |
| 12 | + * type and the stored one stays; a source that lints clean stamps the proven |
| 13 | + * type, or clears it when none is proven, as before. |
| 14 | + * |
| 15 | + * The engine is stubbed so both verdicts are deterministic; the live lint and |
| 16 | + * inference are CelPredicateField.test.tsx's job. |
| 17 | + */ |
| 18 | + |
| 19 | +import '@testing-library/jest-dom/vitest'; |
| 20 | +import * as React from 'react'; |
| 21 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 22 | +import { render, screen, fireEvent, cleanup, waitFor } from '@testing-library/react'; |
| 23 | + |
| 24 | +vi.mock('../useMetadata', () => ({ |
| 25 | + useMetadataClient: () => ({ |
| 26 | + list: vi.fn().mockResolvedValue([]), |
| 27 | + listDrafts: vi.fn().mockResolvedValue([]), |
| 28 | + }), |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock('../previews/useObjectFields', () => ({ |
| 32 | + useObjectFields: () => ({ fields: [], loading: false, error: null }), |
| 33 | +})); |
| 34 | + |
| 35 | +import { ObjectFieldInspector } from './ObjectFieldInspector'; |
| 36 | +import { __setCelFormulaLoader } from '../celAuthoring'; |
| 37 | + |
| 38 | +afterEach(() => { |
| 39 | + cleanup(); |
| 40 | + __setCelFormulaLoader(undefined); |
| 41 | +}); |
| 42 | + |
| 43 | +/** The card's repro shape: a source left open on a bracket or an operator. */ |
| 44 | +const BROKEN = /[*+\-/&|=<>(]\s*$/; |
| 45 | + |
| 46 | +function stubEngine() { |
| 47 | + __setCelFormulaLoader(() => |
| 48 | + Promise.resolve({ |
| 49 | + validateExpression: (_role: string, input: unknown) => { |
| 50 | + const src = typeof input === 'string' ? input : String((input as { source?: string })?.source ?? ''); |
| 51 | + return BROKEN.test(src) |
| 52 | + ? { ok: false, errors: [{ message: 'Parse error: unexpected end of input' }], warnings: [] } |
| 53 | + : { ok: true, errors: [], warnings: [] }; |
| 54 | + }, |
| 55 | + introspectScope: () => ({ fields: ['est_hours'], roots: ['record'], functions: ['has'] }), |
| 56 | + // A broken source proves no type, as the engine answers it. |
| 57 | + inferExpressionType: (input: unknown) => { |
| 58 | + const src = typeof input === 'string' ? input : ''; |
| 59 | + if (BROKEN.test(src)) return 'unknown' as const; |
| 60 | + if (src.includes("'")) return 'text' as const; |
| 61 | + if (src.includes('+')) return 'unknown' as const; |
| 62 | + return 'number' as const; |
| 63 | + }, |
| 64 | + }), |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +function Harness({ onDef }: { onDef: (def: Record<string, unknown>) => void }) { |
| 69 | + const [fields, setFields] = React.useState<Record<string, Record<string, unknown>>>({ |
| 70 | + est_hours: { type: 'number' }, |
| 71 | + f_total: { type: 'formula', label: 'F Total', expression: 'record.est_hours * 2', returnType: 'number' }, |
| 72 | + }); |
| 73 | + React.useEffect(() => { |
| 74 | + onDef(fields.f_total); |
| 75 | + }); |
| 76 | + return ( |
| 77 | + <ObjectFieldInspector |
| 78 | + type="object" |
| 79 | + name="work_order" |
| 80 | + draft={{ name: 'work_order', fields }} |
| 81 | + selection={{ kind: 'field', id: 'f_total' }} |
| 82 | + onPatch={(patch: Record<string, unknown>) => setFields(patch.fields as typeof fields)} |
| 83 | + onClearSelection={() => {}} |
| 84 | + onSelectionChange={() => {}} |
| 85 | + readOnly={false} |
| 86 | + locale={'en-US'} |
| 87 | + onBlockingIssuesChange={() => {}} |
| 88 | + /> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +function renderFormula() { |
| 93 | + stubEngine(); |
| 94 | + const defs: Array<Record<string, unknown>> = []; |
| 95 | + render(<Harness onDef={(def) => defs.push(def)} />); |
| 96 | + const box = screen.getByText('Formula (CEL)').parentElement!.querySelector('[role="combobox"]') as HTMLTextAreaElement; |
| 97 | + const latest = () => defs.at(-1)!; |
| 98 | + return { box, latest }; |
| 99 | +} |
| 100 | + |
| 101 | +const settle = () => new Promise((r) => setTimeout(r, 600)); |
| 102 | + |
| 103 | +describe('ObjectFieldInspector — a broken formula keeps the stored returnType (objectstack#27485)', () => { |
| 104 | + it("the card's repro: `record.est_hours > (` leaves returnType as stored", async () => { |
| 105 | + const { box, latest } = renderFormula(); |
| 106 | + fireEvent.change(box, { target: { value: 'record.est_hours > (' } }); |
| 107 | + await waitFor(() => expect(screen.getByText(/Parse error/)).toBeInTheDocument(), { timeout: 3000 }); |
| 108 | + await settle(); |
| 109 | + expect(latest().expression).toBe('record.est_hours > ('); |
| 110 | + expect(latest().returnType).toBe('number'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('a source that parses again stamps its proven type', async () => { |
| 114 | + const { box, latest } = renderFormula(); |
| 115 | + fireEvent.change(box, { target: { value: 'record.est_hours > (' } }); |
| 116 | + await waitFor(() => expect(screen.getByText(/Parse error/)).toBeInTheDocument(), { timeout: 3000 }); |
| 117 | + fireEvent.change(box, { target: { value: "'hours: ' + string(record.est_hours)" } }); |
| 118 | + await waitFor(() => expect(latest().returnType).toBe('text'), { timeout: 3000 }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('a source that parses and proves no type still clears it, as before', async () => { |
| 122 | + const { box, latest } = renderFormula(); |
| 123 | + fireEvent.change(box, { target: { value: 'record.est_hours + record.est_hours' } }); |
| 124 | + await waitFor(() => expect(latest().returnType).toBeUndefined(), { timeout: 3000 }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments