|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | | -import { isFrozenReading, observedAtOf } from "../freeze"; |
| 2 | +import { isFrozenReading, observedAtOf, composeObservedAt } from "../freeze"; |
3 | 3 |
|
4 | 4 | const NOW = 1_790_000_000, STALE = 720; |
5 | 5 | describe("isFrozenReading — freshness of the value source, independent of monotonicity", () => { |
@@ -27,3 +27,30 @@ describe("observedAtOf — oldest contributing source bounds freshness", () => { |
27 | 27 | }); |
28 | 28 | it("nothing contributed → null", () => expect(observedAtOf([{ used: false, ts: S }])).toBeNull()); |
29 | 29 | }); |
| 30 | + |
| 31 | +describe("composeObservedAt — actual Latest composition cases", () => { |
| 32 | + const S = 1_790_000_000, OLD = 1_789_650_000; // fresh vs 4 days old |
| 33 | + const full = (ts: number, extra: Partial<import("../freeze").InvFlow> = {}) => ({ genPower: 4000, usePower: 900, gridPower: -3000, battPower: 100, soc: 75, observedAt: ts, ...extra }); |
| 34 | + it("old station + fresh inverter with ALL 4 powers + SOC, NO lifetime point → fresh (lifetime is not instantaneous)", () => { |
| 35 | + expect(composeObservedAt(full(S), OLD, true)).toBe(S); |
| 36 | + }); |
| 37 | + it("old station + fresh inverter powers, SOC absent on BOTH → fresh (absent SOC contributes nothing)", () => { |
| 38 | + expect(composeObservedAt(full(S, { soc: undefined }), OLD, false)).toBe(S); |
| 39 | + }); |
| 40 | + it("old station + fresh inverter powers, SOC absent on inverter but known on station → station contributes → old", () => { |
| 41 | + expect(composeObservedAt(full(S, { soc: undefined }), OLD, true)).toBe(OLD); |
| 42 | + }); |
| 43 | + it("fresh station + 4-day-old device that only has SOC → device contributes → old", () => { |
| 44 | + expect(composeObservedAt({ soc: 75, observedAt: OLD }, S, true)).toBe(OLD); |
| 45 | + }); |
| 46 | + it("fresh station + device with no mapped field (e.g. only BatteryRatedCapacity) → station only → fresh", () => { |
| 47 | + expect(composeObservedAt({ observedAt: null }, S, true)).toBe(S); |
| 48 | + expect(composeObservedAt(null, S, true)).toBe(S); |
| 49 | + }); |
| 50 | + it("zero values are real contributions (gen 0 at night from the inverter counts as inverter-sourced)", () => { |
| 51 | + expect(composeObservedAt(full(S, { genPower: 0, gridPower: 0, battPower: 0 }), OLD, true)).toBe(S); |
| 52 | + }); |
| 53 | + it("partial override with a timestamp-less device → unprovable", () => { |
| 54 | + expect(composeObservedAt({ genPower: 4000, observedAt: null }, S, true)).toBeNull(); |
| 55 | + }); |
| 56 | +}); |
0 commit comments