Skip to content

tests: add lazy-state integration tests - #1506

Open
AchrafReyani wants to merge 1 commit into
OpenFn:mainfrom
AchrafReyani:chore/lazy-state-tests
Open

tests: add lazy-state integration tests#1506
AchrafReyani wants to merge 1 commit into
OpenFn:mainfrom
AchrafReyani:chore/lazy-state-tests

Conversation

@AchrafReyani

Copy link
Copy Markdown

Short Description

Adds 21 lazy-state ($) tests to integration-tests/execute/test/lazy-state.test.ts, building up from simple reads to expressions, mapping/iteration, scoping rules and illegal usage.

Fixes #840

Implementation Details

The new tests are grouped and ordered methodically, following the examples on the Lazy State Operator docs page:

  • Basic reads – top-level value, nested path, array index, optional chaining on a missing path, and fn($.data) returning a value from state as the next state.
  • Expressions – arithmetic, && / || / !, ternary, and $ as a dynamic property key (codes[$.location.country]).
  • Objects, mapping and iteration – mapping state into an object argument (the docs' create('agent', { name: $.patient.name }) shape, via a tiny in-job upsert helper that resolves its argument the way expandReferences does), each($.data.items, …), reading the current item with $.data inside each(), and group($.data.rows, $.keyPath).
  • Scoping$ declared as a parameter or a local const is left alone; $ inside a string literal is not converted.
  • Illegal usage – every ❌ example from the docs asserts the LazyStateError message: assigning $ to a variable, using it inside a nullary arrow, inside an arrow whose parameter isn't named state, and writing to $ at the top level or inside an operation.

No source changes; only the test file is touched. No changeset added since @openfn/integration-tests-execute is private (same as previous test-only changes here).

One thing I noticed while writing these and deliberately left out: a method call on a nested state value, e.g. fnIf($.name.toUpperCase() === 'JOHN', …), currently compiles to (state => state.name.toUpperCase)() === 'JOHN' – the arrow wraps the callee rather than the call (the special case in ensureParentArrow only handles $.method() directly on state). Happy to open a separate issue for that if useful.

QA Notes

Ran in a clean node:22 Linux container (fresh pnpm install --frozen-lockfile + pnpm -r --filter=./packages/* run build):

integration-tests/execute $ pnpm ava test/lazy-state.test.ts --verbose
  ✔ … (23 tests)
  23 tests passed

integration-tests/execute $ pnpm ava
  35 tests passed
  1 test skipped

Also tsc --noEmit -p integration-tests/execute/tsconfig.json and prettier --check on the file pass.

(Side note, not addressed here: on Windows the whole execute integration suite fails before any test runs because the runtime linker builds file://C:\… instead of a file:///C:/… URL; CI on Linux is unaffected.)

AI Usage

  • I have used Claude Code
  • I have used another model
  • I have not used AI

Adds a methodical set of lazy-state ($) tests to the execute integration
suite: basic reads, expressions, object mapping / each / group, scoping
rules, and the illegal usages documented on the docs site.

Fixes OpenFn#840
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add more lazy state tests

1 participant