vpp-ai-platform/packages/services/test/ledger.test.ts
stewart hu f681e134cc M1: domain schemas, contracts pipeline, ledger and snapshot services
- packages/domain: zod schemas for the 07-scenario object set with a
  schemaRegistry driving schema export, fixture generation, and Python
  module naming; decimal-string/sha256/curve primitives enforce the
  docs/11 §3.3 representation rules at the type layer
- contracts/: 12 JSON Schemas (draft 2020-12), 12 golden fixtures, 3
  invalid fixtures crafted to fail on both sides (float money, bad
  digest, missing concurrency version)
- skills-py: generated pydantic models (committed, never hand-edited),
  regeneration script, mirror pytest using JSON-mode validation —
  dual-side contract tests agree on all 15 fixtures
- packages/services: content-addressed immutable snapshot store
  (memory + fs, rejects floats), LedgerService v1 with optimistic
  concurrency and the P7 constraint cascade (monthly position bounds
  day-ahead bids; no-anchor bids rejected), quality-gate stub
- cascade band shape is an OPEN-QUESTION A5 placeholder (pro-rata
  daily share ±band) — see ledger.ts checkCascade

All green: tsc typecheck, 41 TS tests, 15 Python tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019u5SLNweVio6ozJX7yfxQr

🔮 View transcript: https://logs.lojong.info/s/e8u90k3t33w590r7b5y7yzqh
2026-09-01 21:50:22 -04:00

78 lines
2.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import type { PositionUpdate } from '@vpp/domain'
import { CascadeViolation, LedgerConcurrencyError, LedgerService } from '../src/ledger.js'
const clock = () => '2026-03-14T08:00:00Z'
const monthlyContract: PositionUpdate = {
id: 'pos-monthly',
timescale: 'MONTHLY',
period: '2026-03',
kind: 'CONTRACT',
energy_mwh: '37200.0', // 1200 MWh/day over 31 days
curve: null,
source_ref: 'contract-2026-03-001',
expected_version: 0,
}
const daBid = (energy: string, expectedVersion: number): PositionUpdate => ({
id: `pos-da-${energy}`,
timescale: 'DAY_AHEAD',
period: '2026-03-15',
kind: 'BID_SUBMITTED',
energy_mwh: energy,
curve: null,
source_ref: 'b'.repeat(64),
expected_version: expectedVersion,
})
const makeLedger = () => new LedgerService({ daMonthlyDeviationBand: '0.05', clock })
describe('constraint cascade (P7): monthly position bounds day-ahead bids', () => {
it('accepts a bid within the ±5% band of the pro-rata daily share', () => {
const ledger = makeLedger()
ledger.append(monthlyContract)
const view = ledger.append(daBid('1240.0', 1)) // daily share 1200, band [1140, 1260]
expect(view.version).toBe(2)
})
it('rejects a bid above the band', () => {
const ledger = makeLedger()
ledger.append(monthlyContract)
expect(() => ledger.append(daBid('1300.0', 1))).toThrow(CascadeViolation)
})
it('rejects a bid below the band', () => {
const ledger = makeLedger()
ledger.append(monthlyContract)
expect(() => ledger.append(daBid('1000.0', 1))).toThrow(CascadeViolation)
})
it('rejects a day-ahead bid with no monthly anchor (no default-allow)', () => {
const ledger = makeLedger()
expect(() => ledger.append(daBid('1200.0', 0))).toThrow(CascadeViolation)
})
})
describe('optimistic concurrency', () => {
it('rejects an append against a stale version', () => {
const ledger = makeLedger()
ledger.append(monthlyContract)
expect(() => ledger.append({ ...monthlyContract, id: 'pos-2', expected_version: 0 })).toThrow(
LedgerConcurrencyError,
)
})
})
describe('views', () => {
it('filters by timescale and stamps recorded_at from the injected clock', () => {
const ledger = makeLedger()
ledger.append(monthlyContract)
ledger.append(daBid('1200.0', 1))
const monthly = ledger.viewByTimescale('MONTHLY')
expect(monthly).toHaveLength(1)
expect(monthly[0]!.recorded_at).toBe(clock())
expect(ledger.viewByTimescale('DAY_AHEAD')).toHaveLength(1)
})
})