[pine-worker testing]

Bun unit tests, shared parity fixtures, and the path to cross-implementation strategy event validation.

pine-worker testing

Abstract

Testing for pine-worker has two layers:

  1. Local Bun unit tests — AST/evaluator/series behavior in pure TS
  2. Cross-language parity — Python oracle generates JSON fixtures; TS must match

Until the parity harness is fully wired (bun run parity is a placeholder), treat Python fixtures as the contract and unit tests as constructive proofs of the port’s foundations.

Conceptual model

Interface surface

Commands

cd pine-worker
bun install
bun test
bun test --watch
bun run typecheck
bun run parity   # prints note until harness lands

Unit test files

FileFocus
test/ast.test.tsAST schema / types
test/evaluator.test.tsVisitor eval, assign, literals
test/series.test.tsHistorical series indexing

Tests use bun:test (describe / test / expect).

Parity fixtures (shared with Python)

PathContent
tests/fixtures/parity/pine/*.pineMinimal strategy scripts
tests/fixtures/parity/json/*.jsonExpected events / series snapshots
tests/fixtures/parity/generate_fixtures.pyOracle generator
tests/fixtures/parity/ohlcv.pyShared bars

Example scenarios: entry long/short, close, close_all, exit stop/limit, cancel, no-action, var counters.

Python-side consumers

Parity is also exercised from the main suite (e.g. strategy event tests). Regenerating fixtures:

python tests/fixtures/parity/generate_fixtures.py

Internals

Evaluator test style

Tests hand-build AST node objects (plain dictionaries with type discriminators) rather than parsing Pine text — because the TS parser target is incomplete. That decouples evaluator progress from lexer work.

NA and control-flow signals

evaluator/types.ts defines NA, BreakSignal, ContinueSignal, ReturnSignal. Unit tests should cover:

  • arithmetic with NA → NA
  • comparisons with NA → false (Pine rule)
  • truthiness where NA is falsy (unlike some JS values)

Future parity harness

Intended location: pine-worker/test/parity/ with bun test test/parity --timeout 30000. Design goal: load each JSON fixture, evaluate corresponding AST/script path, assert event lists match oracle.

Invariants & edge cases

  1. Never edit JSON fixtures by hand to make TS pass — regenerate from Python or fix Python if oracle is wrong.
  2. OHLCV must match between generators and consumers (ohlcv.py).
  3. Timeouts: strategy loops can be heavy; parity script reserves 30s.
  4. Typecheck is mandatory before claiming port milestones (tsc --noEmit).

Worked examples

Run a single unit file

cd pine-worker
bun test test/series.test.ts

Add a unit case for assignment

Follow patterns in evaluator.test.ts: build ScriptAssignIdentifier load → expect.

Oracle refresh after Python strategy change

python tests/fixtures/parity/generate_fixtures.py
git diff tests/fixtures/parity/json/
pytest tests/test_strategy_events.py -q

Failure modes

SymptomCauseFix
Fixture mismatch after Python fixStale JSONRegenerate
TS passes units, fails parityMissing strategy builtinsPort + converter
Flaky floatsCompare with tolerancesMirror Python assert style
parity script always “not wired”Expected until harness landsImplement under test/parity/

See also