|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// The CROSS-PACKAGE DRIFT PIN for `flow-template-grammar.ts` (#16096). |
| 4 | +// |
| 5 | +// That module MIRRORS the automation template evaluator's whole-token dispatch, |
| 6 | +// because `@objectstack/lint` depends on `@objectstack/spec` and never on a |
| 7 | +// runtime, so the dialect cannot be imported from the package that owns it. A |
| 8 | +// mirror nobody checks is the "N copies, the next author fixes one of N" shape |
| 9 | +// `filter-walk.ts` was written against — so this file reads the ORIGINAL from |
| 10 | +// disk and fails when any mirrored piece stops matching it. |
| 11 | +// |
| 12 | +// The read escapes this package, spelled so `check:cross-package-test-inputs` |
| 13 | +// can see it, and `$TURBO_ROOT$/packages/services/service-automation/src/**` is |
| 14 | +// already a declared input of `@objectstack/lint#test` in turbo.json. |
| 15 | + |
| 16 | +import { existsSync, readFileSync } from 'node:fs'; |
| 17 | +import { dirname, join } from 'node:path'; |
| 18 | +import { fileURLToPath } from 'node:url'; |
| 19 | +import { describe, expect, it } from 'vitest'; |
| 20 | + |
| 21 | +import { |
| 22 | + classifyFlowTemplateToken, |
| 23 | + DATE_FUNCTION_RE, |
| 24 | + VARIABLE_PATH_RE, |
| 25 | + SAFE_EXPRESSION_RE, |
| 26 | + IDENTIFIER_SCAN_RE, |
| 27 | + CALL_POSITION_RE, |
| 28 | + FLOW_TEMPLATE_DATE_FUNCTIONS, |
| 29 | + FLOW_TEMPLATE_VALUE_FUNCTIONS, |
| 30 | +} from './flow-template-grammar.js'; |
| 31 | + |
| 32 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 33 | + |
| 34 | +/** Walk up to the workspace root — the directory holding pnpm-workspace.yaml. */ |
| 35 | +function findUp(predicate: (dir: string) => boolean): string { |
| 36 | + let dir = HERE; |
| 37 | + for (;;) { |
| 38 | + if (predicate(dir)) return dir; |
| 39 | + const parent = dirname(dir); |
| 40 | + if (parent === dir) throw new Error('workspace root not found from ' + HERE); |
| 41 | + dir = parent; |
| 42 | + } |
| 43 | +} |
| 44 | +const REPO = findUp((dir) => existsSync(join(dir, 'pnpm-workspace.yaml'))); |
| 45 | + |
| 46 | +const ORIGINAL = join(REPO, 'packages/services/service-automation/src/builtin/template.ts'); |
| 47 | +// Loud absence: if this file moves, the mirror is unpinned, and an unpinned |
| 48 | +// mirror is the defect this test exists to prevent. Failing to read IS the |
| 49 | +// regression — never a skip. |
| 50 | +const source = readFileSync(ORIGINAL, 'utf8'); |
| 51 | + |
| 52 | +describe('the mirrored grammar still matches the evaluator that owns it', () => { |
| 53 | + const mirrored: Array<[string, RegExp]> = [ |
| 54 | + ['the NOW()/TODAY() ± N day form', DATE_FUNCTION_RE], |
| 55 | + ['the variable / dotted-path form', VARIABLE_PATH_RE], |
| 56 | + ['the arithmetic character set', SAFE_EXPRESSION_RE], |
| 57 | + ['the identifier scan', IDENTIFIER_SCAN_RE], |
| 58 | + ['the call-position lookahead', CALL_POSITION_RE], |
| 59 | + ]; |
| 60 | + for (const [label, re] of mirrored) { |
| 61 | + it(`${label} appears verbatim in template.ts`, () => { |
| 62 | + expect(source).toContain(re.source); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + it('mirrors the value-function table exactly — no name added, none dropped', () => { |
| 67 | + const block = /const EXPRESSION_FUNCTION_ARITY[^{]*\{([\s\S]*?)\n\};/.exec(source); |
| 68 | + expect(block, 'EXPRESSION_FUNCTION_ARITY not found in template.ts').toBeTruthy(); |
| 69 | + const names = [...block![1].matchAll(/^\s*([A-Za-z_$][\w$]*)\s*:/gm)].map((m) => m[1]); |
| 70 | + expect(names.sort()).toEqual([...FLOW_TEMPLATE_VALUE_FUNCTIONS].sort()); |
| 71 | + }); |
| 72 | + |
| 73 | + it('mirrors the two whole-token date function names', () => { |
| 74 | + for (const name of FLOW_TEMPLATE_DATE_FUNCTIONS) expect(DATE_FUNCTION_RE.source).toContain(name); |
| 75 | + // And the evaluator still keeps them OUT of the value table — the reason |
| 76 | + // `{TODAY() - 45 - 10}` is refused while `{TODAY() - 45}` is not. |
| 77 | + const block = /const EXPRESSION_FUNCTION_ARITY[^{]*\{([\s\S]*?)\n\};/.exec(source); |
| 78 | + for (const name of FLOW_TEMPLATE_DATE_FUNCTIONS) expect(block![1]).not.toContain(name); |
| 79 | + }); |
| 80 | + |
| 81 | + it('the evaluator still REFUSES an unknown call rather than resolving it to null', () => { |
| 82 | + // The mirror only means something while the runtime still throws here. |
| 83 | + expect(source).toContain('throw unknownFunctionError(match, trimmed)'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('the filter position still hands an unresolved KNOWN filter token to the engine', () => { |
| 87 | + // The layer-one/layer-two split the rule is built on. |
| 88 | + expect(source).toContain('isKnownFilterToken'); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe('dispatch ORDER — the property the negative control depends on', () => { |
| 93 | + it('classifies {TODAY() - 45} as a date function, never as a call', () => { |
| 94 | + expect(classifyFlowTemplateToken('TODAY() - 45')).toEqual({ kind: 'date-function', name: 'TODAY' }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('classifies TOMORROW() as an unknown function', () => { |
| 98 | + expect(classifyFlowTemplateToken('TOMORROW()')).toEqual({ kind: 'unknown-function', name: 'TOMORROW' }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('classifies the open arm as variable-path, never as a finding', () => { |
| 102 | + expect(classifyFlowTemplateToken('recordId')).toEqual({ kind: 'variable-path', head: 'recordId' }); |
| 103 | + expect(classifyFlowTemplateToken('record.id')).toEqual({ kind: 'variable-path', head: 'record' }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('classifies $User.* as user context', () => { |
| 107 | + expect(classifyFlowTemplateToken('$User.Id')).toEqual({ kind: 'user-context' }); |
| 108 | + }); |
| 109 | + |
| 110 | + it('classifies a junk shape as unresolvable rather than as a call', () => { |
| 111 | + expect(classifyFlowTemplateToken('30 days ago')).toEqual({ kind: 'unresolvable-shape' }); |
| 112 | + expect(classifyFlowTemplateToken('')).toEqual({ kind: 'unresolvable-shape' }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('never reports a reserved literal in call position', () => { |
| 116 | + expect(classifyFlowTemplateToken('null(1)').kind).not.toBe('unknown-function'); |
| 117 | + }); |
| 118 | +}); |
0 commit comments