vpp-ai-platform/packages/services/test/ledger.test.ts

97 lines
3.2 KiB
TypeScript
Raw Normal View History

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)
})
})
M2: skill contracts, Python skill service, L2 eval harness with baseline - packages/domain: ForecastRequest, BidOptimizationRequest/Result, ReportRequest, SkillReport (+ golden and invalid fixtures, exported to contracts/ and regenerated as pydantic models). - skills-py/vpp_skills: FastAPI service with versioned registry; load/PV/ price forecasts (same-day-type EWM point forecast, conformal residual quantiles — coverage test as acceptance gate); bid-optimization MILP on HiGHS (binary block participation, hard ledger energy bounds, exact Decimal fit of the rounded curve inside the bounds, revenue distribution over quantile paths); report generator whose every figure is a {tool_call_id, path} reference, with a verifier. 48 tests incl. hypothesis property test that bids respect ledger constraints. - packages/services: LedgerService.dayAheadBounds (the P7 cascade band handed to the optimizer); Decimal resolved once for CJS/ESM interop. - packages/evals: L2 metrics (MAPE, nRMSE, coverage, direction accuracy, naive/hindsight revenue baselines), HTTP skill client, rolling-origin harness that pushes each bid through the real ledger, CLI with --check/--write-baseline; committed baseline on the SYNTHETIC dataset (no historical Hubei data yet — baselines measure the harness, not KPI). - CI: evals job boots the skill service and fails on baseline digest drift. - docs/open-questions: A6 (flexibility marginal cost = offer floor); A4/B6 wired as placeholders. README/CLAUDE.md status → M2 done, M3 next. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UoYoGYzHkFyv3ALenkRPhA
2026-09-02 06:29:08 -04:00
describe('dayAheadBounds: the cascade band handed to the bid optimizer', () => {
it('returns the same band checkCascade enforces, stamped with the ledger version', () => {
const ledger = makeLedger()
ledger.append(monthlyContract)
expect(ledger.dayAheadBounds('2026-03-15')).toEqual({
ledger_version: 1,
daily_energy_min_mwh: '1140',
daily_energy_max_mwh: '1260',
})
// A bid exactly at each bound is accepted by append — bounds are inclusive.
ledger.append(daBid('1140', 1))
ledger.append({ ...daBid('1260', 2), id: 'pos-da-max' })
})
it('throws when there is no monthly anchor', () => {
expect(() => makeLedger().dayAheadBounds('2026-03-15')).toThrow(CascadeViolation)
})
})