diff --git a/go/test/site/README.md b/go/test/site/README.md index c7eceddd5..7d2ff120f 100644 --- a/go/test/site/README.md +++ b/go/test/site/README.md @@ -40,7 +40,7 @@ make siterig-judge DIR=/tmp/ftw-site-123 # judge a recorded run again | `FTW_SITE_DEVSIM_SRC` | a device-simulator checkout at the pinned commit; without it the test clones the pin into the user cache | | `FTW_SITE_FTW_BIN`, `FTW_SITE_DEVSIM_BIN` | prebuilt binaries | | `FTW_SITE_CATALOG` | the fault catalog, for `TestCatalogCoverage` | -| `FTW_SITE_JUDGE` | a results directory: `TestJudge` judges its runs again with the criteria as they are now, and rewrites their `result.json` and the summary | +| `FTW_SITE_JUDGE` | a results directory: `TestJudge` judges its runs again with the criteria as they are now, random seeds included (from each run's `plan.json`), and rewrites their `result.json` and the summary | Each run directory keeps `house/plant.jsonl` (the house once a second, with every event and mark), `house/ocpp-.jsonl` (every OCPP frame the @@ -64,7 +64,13 @@ that touch control, loadpoint, OCPP or telemetry code), `siterig/full` A criterion named in a scenario's `known` map fails on master for the finding it names. The run passes while it keeps failing, and fails once it -passes: a fix deletes its line in the same pull request. +passes: a fix deletes its line in the same pull request. The `r` +scenarios are R1-R5 of the stable plan; their known lines name the Wave 1 +item whose fix deletes them. + +A scenario with a `skip` waits for a device-simulator change the pinned +commit lacks. `TestSite` reports it as `skip` with the knob it needs; the +pull request that pins a simulator with the knob deletes the skip. ## The fault catalog @@ -76,13 +82,15 @@ its sources (`provenance` in [catalog_test.go](catalog_test.go)). "Over the limit" is the worst phase above the fuse rating, measured through the fuse. A device's response time counts as 2 s, the meter and control -tick as 2 s more. +tick as 2 s more. The random cases, `r3` and `r5a` judge a phase between +the fuse and 1.2 × the fuse as Core does: it counts only once its 60 s heat +(I² filter) is above the fuse too. | Scenario | House and steps | Criteria | Short | |---|---|---|---| | `s0-25` Idle charger | 25 A, 3 A per phase, a charger with no car, heartbeat 60 s, 300 s | Home use `measured` in 95 % of samples; no OCPP disconnect | yes | | `s0b-25` | as s0, Core's heartbeat | as s0 | | -| `s1-16`, `s1-25` Charge now | 3 A per phase, Charge now 16 A for 180 s | the room the fuse gives (25 A: 11 040 W) within 60 s; held 95 %; fuse holds; on 25 A no `fuse_limit` | yes | +| `s1-16`, `s1-25` Charge now | 3 A per phase, Charge now 16 A for 180 s | the room the fuse gives (16 A: 8 970 W; 25 A: 11 040 W) within 60 s; held 95 %; fuse holds; on 25 A no `fuse_limit` | yes | | `s2-16` Charge now during export | 16 A, 1 A house, 7 kW PV, full battery: 9.1 A export per phase | car starts within 60 s (**known**: export counted as import); fuse holds | | | `s3-16`, `s3-25` Load step | car and a 3 kW battery hold charging, then 13 A (16 A) or 21 A (25 A) per phase for 180 s | fuse holds; over the limit at most 7 s after the step | yes | | `s4-25`, `s4b-25` Saved unlock | `surplus_unlock_bat_soc` 0.9 saved, not armed; Charge now 3p, and bare (s4b) | no stops; no offer drops to 0 W; full power over the last minute | | @@ -103,5 +111,12 @@ tick as 2 s more. | `s15bat-16` One-phase charger, battery rising | 16 A, 3 A per phase, the s15 charger, battery at 50 % following its EMS 8 s late, `planner_self`; PV 2.1 kW rising every 30 s (+2.5 kW, then 1.5 kW) so Core charges the battery in steps; Charge now 6 s after the second step, while the raise is on its way | the battery order rose over 1 150 W in the 20 s before (setup check); car charging within 10 s; fuse holds; over the limit at most 3 s | | | `s15p-25` One-phase charger, planned | as s15, a plan for 600 Wh by the first quarter hour 15 min out | fuse holds; 90 % of the plan by the ready time; never called not following | | | `s14-25` One-phase car | an 11 kW charger with a one-phase car, a plan it cannot meet, 20 min | 3.4–3.8 kW over the last 10 min; never called not following | | +| `r1-25` Meter silent, then offline | 25 A, 15 A per phase, battery at 50 %, Charge now 16 A; the site meter silent (Modbus timeouts) for 120 s, then gone for 90 s. **Skipped**: the pinned simulator cannot silence the site meter | the battery relieved the fuse (setup); while silent no offer rises and the battery order only moves toward 0 (**known**: A1); a 0 W order and the battery back on its own within 64 s; the meter never shows live; Charge now back within 95 s of each return and held until the next event; fuse holds | | +| `r2-25`, `r2-60-25` Battery drops out | 25 A, 20 A per phase, the battery held at a 3 kW discharge, a car taking 10 A on Charge now, the charger at once or 60 s late; the inverter gone for 180 s. The site stays under 1.2 × the fuse, where Core cuts the car on its 60 s heat budget, not at once | the battery discharged (setup); fuse holds; over 1.2 × the fuse at most the charger's delay + 7 s; the 60 s heat under 1.13 × the fuse; no offer rises while it is gone; Home use shown as estimated throughout. The time over the fuse and when Core sends the cut are recorded | | +| `r3-25` Planner floor | 25 A, 15 A per phase, battery at 12 %, planner `soc_min` 10 %, inverter floor 5 %, charger 60 s late, Charge now 16 A for 600 s | the battery reaches the floor (setup); no discharge ordered below it (**known**: A4); the battery order swings at most 100 W over the last 5 min (**known**: A3, A4); over the fuse at most the delay + 7 s; fuse holds | | +| `r4-25` Hard kill | 25 A, 12 A per phase, Charge now 16 A; once the day's Home use is known and 10 min more, the battery held at a 3 kW discharge; 5 min later SIGKILL for 20 s, and 2 min after that for 120 s while the house steps to 16 A | Charge now back at its minimum within 60 s of each restart and held until the next kill or the end; the day's Home use kept within 1 % across the first kill; session energy counts again (**known**: E1); no state.db recovery in the log; fuse holds | | +| `r5a-25` Start beside a charging car | 25 A, 14 A per phase, battery at its 5 % floor, a car charging on its own at 16 A: 30 A per phase for 60 s before Core starts | the car charged and the site was over the fuse (setup); under the fuse within 30 s of Core answering, and over it at most 3 s after; Home use shown within 30 s; fuse holds | | +| `r5b-25` Start on a stale plan | a goal 40 → 60 % 8 h out, Core stopped for 20 min | no positive order in the first 30 s before a fresh plan; Home use shown within 30 s; fuse holds. It does not reach D6 on master: the saved plan is younger than 30 min and a fresh one lands within seconds | | +| `r5c-25` Start into a new session | Charge now, Core stopped for 10 min while the car leaves and another plugs in | Core sees the car within 60 s; Home use shown within 30 s; fuse holds. Whether Charge now carries over is recorded, not judged: ev-088 waits for the owner | | | `s16-25` Import ceiling | 25 A, house 1.3 kW, battery idle at its floor, `peak_import_ceiling_w` 4 500, Charge now 16 A: house and the car's 4 140 W minimum are above the ceiling. Starts in an hour's first minutes or waits for :56, then runs one whole clock hour and 3 min | the car starts on the hour's room before :45; every clock hour's recorded import averages at most the ceiling + 1 %; the next hour stops the car within 60 s; while held it says `import_limit` 90 % of the time and never `fuse_limit`; fuse holds | | | `s17-25` Two equal batteries | 25 A, house 3 kW, no PV, two hybrids with 10 kWh at 74 % and 60 %, Energyplan whatever `FTW_SITE_ENGINE` says (Core DP plans one battery), a 6 h horizon. Waits up to an hour for a quarter the plan discharges, then 15 min | Energyplan's plan moves the batteries in half the polls (setup); while the fleet moves 500 W or more, each battery takes at least a quarter of it 90 % of the time; while the plan moves them, the charge gap shrinks by over 0.2 points; fuse holds | | diff --git a/go/test/site/catalog_test.go b/go/test/site/catalog_test.go index 87108f722..55cfd9988 100644 --- a/go/test/site/catalog_test.go +++ b/go/test/site/catalog_test.go @@ -52,6 +52,14 @@ var provenance = map[string]struct { "s15p-25": {nil, []string{"https://github.com/srcfl/ftw/pull/1804", "release blocker 22 Oct 2026: a one-phase charger, planned charging"}}, "s16-25": {nil, []string{"https://github.com/srcfl/ftw/pull/1828", "help report 25 Oct 2026: an idle car held at 0 W for an hour under a 4.5 kW import ceiling"}}, "s17-25": {nil, []string{"https://github.com/srcfl/ftw/pull/1814", "field report 23 Oct 2026, v0.155.1-beta.1: two equal Pixii batteries take turns"}}, + "r1-25": {[]string{"tel-001", "tel-006", "tel-007", "bat-074"}, []string{"stable plan 27 Oct 2026, R1"}}, + "r2-25": {[]string{"tel-007", "tel-022", "ev-090", "ev-075"}, []string{"stable plan 27 Oct 2026, R2"}}, + "r2-60-25": {[]string{"tel-007", "tel-022", "ev-090", "ev-075"}, []string{"stable plan 27 Oct 2026, R2"}}, + "r3-25": {[]string{"ev-099", "ev-090"}, []string{"stable plan 27 Oct 2026, R3"}}, + "r4-25": {[]string{"ev-085", "ev-091", "home-061", "home-074", "ocpp-008", "upd-051"}, []string{"stable plan 27 Oct 2026, R4", "https://github.com/srcfl/ftw/pull/1787"}}, + "r5a-25": {[]string{"ev-076", "dev-100"}, []string{"stable plan 27 Oct 2026, R5"}}, + "r5b-25": {[]string{"ev-076"}, []string{"stable plan 27 Oct 2026, R5", "https://github.com/srcfl/ftw/issues/1718"}}, + "r5c-25": {[]string{"ev-088"}, []string{"stable plan 27 Oct 2026, R5"}}, } func TestEveryScenarioSaysWhereItComesFrom(t *testing.T) { diff --git a/go/test/site/evaluate_test.go b/go/test/site/evaluate_test.go index 9a2e95a13..7a2f93145 100644 --- a/go/test/site/evaluate_test.go +++ b/go/test/site/evaluate_test.go @@ -42,18 +42,19 @@ type plantDevice struct { W float64 `json:"w"` // a charger's draw BatteryW float64 `json:"battery_w"` // an inverter's battery, + charging SoCPct float64 `json:"soc_pct"` - EMSCmd float64 `json:"ems_cmd"` // 170 charge, 187 discharge, 204 stop + EMSMode float64 `json:"ems_mode"` // 2 forced, as Core drives it + EMSCmd float64 `json:"ems_cmd"` // 170 charge, 187 discharge, 204 stop EMSSetW float64 `json:"ems_setpoint_w"` Connected bool `json:"connected"` + Running *bool `json:"running"` // an inverter that answers; nil when not recorded } type ftwStatus struct { - Drivers map[string]struct { - Status string `json:"status"` - } `json:"drivers"` - LoadQuality *string `json:"load_quality"` - LoadReason *string `json:"load_reason"` - LoadW *float64 `json:"load_w"` + Drivers map[string]ftwDriver `json:"drivers"` + Dispatch []ftwDispatch `json:"dispatch"` // Core's battery orders this tick + LoadQuality *string `json:"load_quality"` + LoadReason *string `json:"load_reason"` + LoadW *float64 `json:"load_w"` Energy struct { Today struct { LoadWh *float64 `json:"load_wh"` @@ -62,6 +63,18 @@ type ftwStatus struct { } `json:"energy"` } +// ftwDispatch is one battery order Core sent: + charge, - discharge. +type ftwDispatch struct { + Driver string `json:"driver"` + TargetW float64 `json:"target_w"` +} + +type ftwDriver struct { + Status string `json:"status"` + EVSessionWh *float64 `json:"ev_session_wh"` // a charger's session energy + ConsecutiveErrors *int `json:"consecutive_errors"` // failed polls in a row; 0 once a poll reads +} + type ftwLoadpoint struct { ID string `json:"id"` Plugged bool `json:"plugged_in"` @@ -69,6 +82,7 @@ type ftwLoadpoint struct { Charger struct { Known bool `json:"known"` } `json:"charger"` + ManualActive bool `json:"manual_active"` CommandedReason string `json:"commanded_reason"` Manual struct { LimitReason string `json:"limit_reason"` // the clamp behind a limited Charge now @@ -102,6 +116,7 @@ type sample struct { Bat2 plantDevice // the second, if any Status *ftwStatus LPs []ftwLoadpoint + HeatA [3]float64 // each phase's 60 s heat, filled in by warm } type frame struct { @@ -125,6 +140,7 @@ type runData struct { events []houseEventLine frames []frame // garage's charger meta map[string]any + warmed bool // every sample's HeatA is filled in } type markLine struct { @@ -340,6 +356,24 @@ func (d *runData) firstT(t0 float64, pred func(*sample) bool) *float64 { return nil } +// holdsFrom is the seconds from t0 to the start of the unbroken run of +// samples satisfying pred that lasts until t1, or nil when the last sample +// before t1 does not. A stop that begins after t0 resets it, so a car still +// running at t0 and stopped later does not count as back. +func (d *runData) holdsFrom(t0, t1 float64, pred func(*sample) bool) *float64 { + var from *float64 + for _, r := range d.window(t0, t1) { + switch { + case !pred(r): + from = nil + case from == nil: + v := r.T - t0 + from = &v + } + } + return from +} + // runsOver is the longest contiguous stretch and the total seconds with the // worst phase above limit. func (d *runData) runsOver(t0, t1, limit float64) (longest, total float64) { @@ -367,10 +401,72 @@ func (d *runData) runsOver(t0, t1, limit float64) (longest, total float64) { return round(longest, 1), round(total, 1) } +// fuseHeatTauS is the time constant of Core's fuse heat model +// (control/fuse_heat.go). +const fuseHeatTauS = 60.0 + +// warm fills in each sample's HeatA: per phase, the 60 s I² filter of Core's +// fuse heat model, on the current through the fuse in either direction, +// from the record's first sample on. +func (d *runData) warm() { + if d.warmed { + return + } + d.warmed = true + var sq [3]float64 + for i, r := range d.samples { + for k, a := range r.PhaseA { + if i == 0 { + sq[k] = a * a + } else { + sq[k] += (1 - math.Exp(-(r.T-d.samples[i-1].T)/fuseHeatTauS)) * (a*a - sq[k]) + } + r.HeatA[k] = math.Sqrt(sq[k]) + } + } +} + +// heatOver is whether a phase is over the fuse as Core's heat model judges +// it (owner rule, 27 Oct 2026): above the fuse while its 60 s heat is above +// the fuse too, or above fuseInstantFactor x the fuse. Between the fuse and +// that factor the heat is the budget. Call warm first. +func heatOver(r *sample, fuse float64) bool { + for k, a := range r.PhaseA { + a = math.Abs(a) + if a > fuse && (r.HeatA[k] > fuse || a > fuseInstantFactor*fuse) { + return true + } + } + return false +} + +// heatRunsOver is the longest stretch in [t0, t1] that heatOver holds, +// timed like runsOver. +func (d *runData) heatRunsOver(t0, t1, fuse float64) float64 { + d.warm() + longest := 0.0 + var from *float64 + var last float64 + for _, r := range d.window(t0, t1) { + t := r.T + switch { + case heatOver(r, fuse) && from == nil: + from = &t + case !heatOver(r, fuse) && from != nil: + longest, from = math.Max(longest, t-*from), nil + } + last = t + } + if from != nil { + longest = math.Max(longest, last-*from) + } + return round(longest, 1) +} + // i2Over is the seconds the worst phase's I² filter (tau 60 s) is above // limit, and the filter's peak. func (d *runData) i2Over(t0, t1, limit float64) (over, peak float64) { - const tau = 60.0 + const tau = fuseHeatTauS var y, prev float64 started := false for _, r := range d.window(t0, t1) { @@ -686,3 +782,80 @@ func (d *runData) disconnects(t0, t1 float64) []float64 { }) return out } + +// ---- the battery, the charger's orders and the log, for R1-R5 ---- + +// batOrder is Core's battery order as the inverter holds it: + charge, - +// discharge, 0 when not forced. +func batOrder(r *sample) float64 { + if r.Bat.EMSMode != 2 { + return 0 + } + switch r.Bat.EMSCmd { + case 170: + return r.Bat.EMSSetW + case 187: + return -r.Bat.EMSSetW + } + return 0 +} + +// batForced is true while Core drives the battery: forced mode with a charge +// or discharge command. +func batForced(r *sample) bool { + return r.Bat.EMSMode == 2 && (r.Bat.EMSCmd == 170 || r.Bat.EMSCmd == 187) +} + +// offers is every SetChargingProfile on connector 1 of charger 0, timed in +// Unix seconds. +func (d *runData) offers() []profile { return connector1(profiles(d.frames, 0, math.Inf(1))) } + +// offerRises counts orders sent in [t0, t1) that offer more than the order +// before them. +func (d *runData) offerRises(t0, t1 float64) int { + n := 0 + prs := d.offers() + for i := 1; i < len(prs); i++ { + if prs[i].T >= t0 && prs[i].T < t1 && prs[i].offerW() > prs[i-1].offerW()+1 { + n++ + } + } + return n +} + +// firstOffer is the seconds from t0 to the first order from t0 on that +// satisfies pred, or nil. +func (d *runData) firstOffer(t0 float64, pred func(profile) bool) *float64 { + for _, p := range d.offers() { + if p.T >= t0 && pred(p) { + v := p.T - t0 + return &v + } + } + return nil +} + +// logLines are the lines of FTW's log that hold any of subs. +func (d *runData) logLines(subs ...string) []string { + var out []string + readLines(filepath.Join(d.dir, "ftw.log"), func(b []byte) { + for _, s := range subs { + if strings.Contains(string(b), s) { + out = append(out, string(b)) + return + } + } + }) + return out +} + +// todayLoadWh is the day's Home use FTW last showed in ws, or nil. +func todayLoadWh(ws []*sample) *float64 { + var v *float64 + for _, r := range ws { + if r.Status != nil && r.Status.Energy.Today.LoadWh != nil { + v = r.Status.Energy.Today.LoadWh + } + } + return v +} diff --git a/go/test/site/random_test.go b/go/test/site/random_test.go index a0c7f3377..3a124b728 100644 --- a/go/test/site/random_test.go +++ b/go/test/site/random_test.go @@ -9,8 +9,11 @@ import ( "math/rand" "os" "path/filepath" + "regexp" "sort" + "strconv" "strings" + "syscall" "time" ) @@ -155,7 +158,7 @@ func runPlan(c *runCtx, p randomPlan) (map[string]any, error) { case "plan": _, err = st.planTo("garage", a.SoCPct, a.GoalPct, time.Duration(a.ReadyMin*float64(time.Minute))) case "restart": - st.stopFTW() + st.stopFTW(syscall.SIGTERM) sleepS(a.DownS) err = st.startFTW() st.mark("ftw:up") @@ -211,96 +214,528 @@ func (d *runData) timing() houseTiming { return h } -// eventWindow is when the house changed on its own, and how long FTW may -// take to answer it. +// eventWindow is a stretch of time: a fault, or a Charge now hold. type eventWindow struct{ from, to float64 } +// within is whether t falls in one of ws, or up to afterS after its end. +func within(ws []eventWindow, t, afterS float64) bool { + for _, w := range ws { + if t >= w.from && t <= w.to+afterS { + return true + } + } + return false +} + +// inverterBackFrom gives, for a time t, the latest return before t (at): +// the inverter's from an outage (inverterOut) or Core's from a restart +// (down), and when the R8 count may start after it (from). Core has the inverter back at its first +// good read of it (no consecutive errors), which the Modbus reconnect +// backoff may hold back up to reconnectBackoffMaxS. After an outage, a +// default that failed while the inverter was away waits on the registry's +// retry timer, up to defaultRetryMaxS more, until #1915 (C11) sends it on +// the first good poll. A Core that starts fresh has no such wait. +func (d *runData) inverterBackFrom(inverterOut, down []eventWindow) func(t float64) (at, from float64) { + type back struct { + at, from float64 + } + var backs []back + add := func(at float64, outage bool) { + // While Core is down nothing reads the inverter: its backoff + // starts when Core is up again. + start := at + for _, w := range down { + if outage && at >= w.from && at < w.to { + start = w.to + } + } + from := start + reconnectBackoffMaxS + for _, r := range d.samples { + if r.T >= start && r.T < from && readsInverter(r) { + from = r.T + break + } + } + if outage { + from += defaultRetryMaxS + } + backs = append(backs, back{at, from}) + } + for _, w := range inverterOut { + add(w.to, true) + } + for _, w := range down { + add(w.to, false) + } + return func(t float64) (at, from float64) { + at, from = math.Inf(-1), math.Inf(-1) + for _, b := range backs { + if b.at <= t && b.at >= at { + at, from = b.at, b.from + } + } + return at, from + } +} + +// readsInverter is whether Core's last poll of the inverter succeeded. A +// status without the driver's error count says nothing, and counts as a +// read. +func readsInverter(r *sample) bool { + if r.Status == nil { + return false + } + dr, ok := r.Status.Drivers[inverterDriver] + return !ok || dr.ConsecutiveErrors == nil || *dr.ConsecutiveErrors == 0 +} + +// during is whether t falls in one of ws, up to but not at its end: at its +// end the device is back. +func during(ws []eventWindow, t float64) bool { + for _, w := range ws { + if t >= w.from && t < w.to { + return true + } + } + return false +} + +// stepWindow is a change FTW did not make, and how long FTW may take to +// answer it: until from + extraS + its lever's time + 2 s for the meter and +// the tick + 3 s. +type stepWindow struct { + plain bool // a house event that is no fault: it extends a stretch only if it raised the current + from, extraS float64 + leverS float64 // > 0: the only lever that can answer (the charger, for a car FTW did not start) + untilT float64 // > 0: the window ends here, whatever the lever (a Core restart) + noBattery bool // the step took the inverter away +} + +// What the random judge takes from the rig and from Core. +const ( + rigBatteryMaxW = 5000.0 // the simulated Sungrow's largest charge or discharge + evRestartDwellS = 35.0 // control/ev_fuse_budget.go evRestartDwell + tickS = 2.0 // the rig's site.control_interval_s + plannerFloorPct = 10.0 // Core's planner.soc_min when the config leaves it out (cmd/ftw/main.go) + + // R8: how long a battery FTW does not dispatch may hold a forced mode, + // and the Core timers that may delay it after the inverter returns. + inverterDriver = "sungrow" // the rig's inverter driver (stack_test.go) + forcedBudgetS = 10.0 + defaultRetryMaxS = 0.0 // #1915 (C11) retries the default on the first good poll after an outage +) + +// reconnectBackoffMaxS is Core's Modbus reconnect backoff cap +// (modbus/client.go reconnectBackoffMax), read from the source the rig +// builds Core from, so R8 follows Core when the cap changes (60 s, then +// 10 s with #1930). +var reconnectBackoffMaxS = coreSeconds("modbus/client.go", "reconnectBackoffMax") + +// coreSeconds reads "name = N * time.Second" from a file under go/internal. +// It panics when the line is gone: the judge has no number to fall back on. +func coreSeconds(file, name string) float64 { + b, err := os.ReadFile(filepath.Join("..", "..", "internal", file)) + if err != nil { + panic(err) + } + m := regexp.MustCompile(name + `\s*=\s*(\d+)\s*\*\s*time\.Second`).FindSubmatch(b) + if m == nil { + panic(fmt.Sprintf("no %s = N * time.Second in Core's %s", name, file)) + } + n, _ := strconv.Atoi(string(m[1])) + return float64(n) +} + +// dispatchW is Core's battery order on r, + charge; false when Core +// dispatched no battery. +func dispatchW(r *sample) (float64, bool) { + if r == nil || r.Status == nil || len(r.Status.Dispatch) == 0 { + return 0, false + } + w := 0.0 + for _, x := range r.Status.Dispatch { + w += x.TargetW + } + return w, true +} + +// worstSignedA is the phase current furthest from 0, with its direction. +func worstSignedA(r *sample) float64 { + w := 0.0 + for _, a := range r.PhaseA { + if math.Abs(a) > math.Abs(w) { + w = a + } + } + return w +} + +// chargeNowChanges says, for each Charge now in the run's plan, whether it +// asked for a power or phase mode other than the Charge now before it. +// Without a plan every Charge now counts as a change. +func (d *runData) chargeNowChanges() []bool { + b, err := os.ReadFile(filepath.Join(d.dir, "plan.json")) + if d.dir == "" || err != nil { + return nil + } + var p randomPlan + if json.Unmarshal(b, &p) != nil { + return nil + } + var out []bool + var last *ftwAction + for i, a := range p.Actions { + if a.Do != "charge_now" { + continue + } + out = append(out, last == nil || last.W != a.W || last.PhaseMode != a.PhaseMode) + last = &p.Actions[i] + } + return out +} + func judgeInvariants(d *runData) (metrics, []criterion) { fuse := d.metaF("fuse") h := d.timing() t0, t1 := d.marks["start"], d.marks["end"] run := d.window(t0, t1) + d.warm() m := metrics{"charger_command_s": h.commandS, "charger_startup_s": h.startupS, "events": len(d.events)} + charger := math.Max(2, h.commandS) // the charger answers in its own time, never under 2 s + orders := d.offers() - // Windows in which an over-limit second is the house's doing. - var windows []eventWindow - faultUntil := map[float64]float64{} // a fault's start and end + // Windows in which an over-limit second is the house's doing. Only a + // meter fault adds its length to the time FTW may take: it blinds FTW. + // An inverter outage takes the battery lever away; a charger outage + // delays the charger lever (chargerLever). + var windows []stepWindow + var faults, chargerFaults, inverterOut, chargerOut []eventWindow for _, ev := range d.events { - batteryUsable := true - if r := d.at(ev.T); r != nil && r.Bat.SoCPct <= h.floorPct+1 { - batteryUsable = false - } - // The fastest lever: the battery answers in 2 s, the charger in its - // own time, never under 2 s; then the meter and tick, 2 s, and 3 s. - lever := math.Max(2, h.commandS) - if batteryUsable { - lever = 2 - } - allow := lever + 2 + 3 + w := stepWindow{from: ev.T} + until := 0.0 switch ev.Event.Do { case "meter_late", "silence", "offline", "reconnect": - allow += ev.Event.ForS - faultUntil[ev.T] = ev.T + ev.Event.ForS + until = ev.T + ev.Event.ForS case "meter_glitch": - faultUntil[ev.T] = ev.T + 5 + until = ev.T + 5 + } + if until > 0 { + f := eventWindow{ev.T, until} + faults = append(faults, f) + switch ev.Event.Device { + case "", "meter": + chargerFaults = append(chargerFaults, f) + w.extraS = ev.Event.ForS + case "garage": + chargerFaults = append(chargerFaults, f) + chargerOut = append(chargerOut, f) + case "inverter": + inverterOut = append(inverterOut, f) + w.noBattery = true + } } - windows = append(windows, eventWindow{ev.T, ev.T + allow}) + w.plain = until == 0 + windows = append(windows, w) + } + // The battery's floor: the higher of the inverter's own floor and the + // planner's soc_min. FTW may not discharge it there (check 6), so there + // it is no lever for import either. + floorPct := h.floorPct + if p := d.metaF("planner_floor_pct"); p > 0 { + floorPct = math.Max(floorPct, p) + } else { + floorPct = math.Max(floorPct, plannerFloorPct) } for _, mk := range d.allMarks { if mk.Label == "ftw:restart" { - windows = append(windows, eventWindow{mk.T, mk.T + 60}) + windows = append(windows, stepWindow{from: mk.T, untilT: mk.T + 60}) } } - explained := func(t float64) (float64, bool) { - best, ok := 0.0, false - for _, w := range windows { + // A car that starts to draw more than FTW's last order lets it (the + // charger's own default, or a lower order still on its way) is a step + // FTW did not take, and only the charger can answer it. + orderW := func(t float64) (float64, bool) { + w, ok := 0.0, false + for _, p := range orders { + if p.T > t { + break + } + w, ok = p.offerW(), true + } + return w, ok + } + carStarts := 0 + for i := 1; i < len(d.samples); i++ { + r := d.samples[i] + if r.T < t0 || r.T > t1 || carW(r, 0)-carW(d.samples[i-1], 0) <= 1000 { + continue + } + if w, ok := orderW(r.T); !ok || carW(r, 0) > w+3*volt { + windows = append(windows, stepWindow{from: r.T, leverS: charger}) + carStarts++ + } + } + m["car_starts_ftw_did_not_order"] = carStarts + // In time order, so a new step after a stretch's allowance is told + // from a window inside it (judgeStretch). + sort.SliceStable(windows, func(i, j int) bool { return windows[i].from < windows[j].from }) + + // chargerLever is how long the charger takes to answer from t: its own + // time, after the rest of any outage it is in. + chargerLever := func(t float64) float64 { + left := 0.0 + for _, w := range chargerOut { if t >= w.from && t <= w.to { - best, ok = math.Max(best, w.to), true + left = math.Max(left, w.to-t) } } - return best, ok + return charger + left + } + // lever is how long FTW's fastest lever takes, from t, to take overA off + // the worst phase: the battery, in 2 s, while its inverter runs and its + // spare power covers the overage; otherwise the charger. At its floor + // the battery may only charge less, never discharge more. The inverter + // is back at the end of its outage. + lever := func(t, overA float64, export bool) float64 { + r := d.at(t) + if r == nil || during(inverterOut, t) || (r.Bat.Running != nil && !*r.Bat.Running) { + return chargerLever(t) + } + spare := rigBatteryMaxW + r.BatW // discharge more, or charge less + switch { + case export: + spare = rigBatteryMaxW - r.BatW + case r.Bat.SoCPct <= floorPct: + spare = math.Max(0, r.BatW) + } + if spare < overA*3*volt { + return chargerLever(t) + } + return 2 + } + until := func(w stepWindow, at, overA float64, export bool) float64 { + if w.untilT > 0 { + return w.untilT + } + lv := w.leverS + switch { + case lv > 0: + lv += chargerLever(w.from) - charger + case w.noBattery: + lv = chargerLever(w.from) + default: + lv = lever(math.Max(w.from, at), overA, export) + } + return w.from + w.extraS + lv + 2 + 3 } // 1, 2: the fuse holds; over the limit only as long as an outside step - // allows, and at most 3 s after FTW's own raise. + // allows, and at most 3 s after FTW's own raise. Between the fuse and + // 1.2 x the fuse the 60 s heat is the budget (heatOver): the clock + // starts when the heat passes the fuse, as Core's cut does. A stretch + // runs on across dips under the fuse while the heat stays over it. blownAny := anyBlown(run) + // carMovedIn is whether FTW sent the car a higher order (dir +1, toward + // import) or a lower one (dir -1, toward export) in (a, b]; with over, + // only one sent while the meter showed the fuse passed. + carMovedIn := func(a, b float64, over bool, dir float64) bool { + prev, ok := 0.0, false + for _, p := range orders { + if p.T > b { + break + } + if ok && p.T > a && dir*(p.offerW()-prev) > 1 && (!over || maxAbsI(d.at(p.T)) > fuse) { + return true + } + prev, ok = p.offerW(), true + } + return false + } + // batMovedIn is the same for the battery: FTW moved its order 0.5 A a + // phase or more toward import (dir +1) or export (dir -1) while the + // inverter ran. An order moved while the inverter is out reaches no + // battery. While Core is down or has no order yet the inverter keeps + // the last one, so a missing order keeps it too. + batMovedIn := func(a, b float64, over bool, dir float64) bool { + prev, ok := 0.0, false + for _, r := range d.samples { + if r.T < a-5 { + continue + } + if r.T > b { + break + } + w, has := dispatchW(r) + if !has && ok { + continue + } + running := !during(inverterOut, r.T) && (r.Bat.Running == nil || *r.Bat.Running) + if ok && has && r.T > a && dir*(w-prev) >= 0.5*3*volt && running && (!over || maxAbsI(r) > fuse) { + return true + } + prev, ok = w, true + } + return false + } + // ownRaise is whether FTW pushed a stretch from s to end over the fuse + // itself: it moved the car or the battery toward the stretch's + // direction in (a, s], or while the stretch was over the fuse. On + // import that is a car raise; on export a car cut (owner, 28 Oct 2026), + // as a battery move toward export already was. Raising the car on + // export is the right answer. + ownRaise := func(a, s, end float64, export bool) bool { + dir := 1.0 + if export { + dir = -1 + } + return carMovedIn(a, s, false, dir) || batMovedIn(a, s, false, dir) || + carMovedIn(s, end, true, dir) || batMovedIn(s, end, true, dir) + } + // raisedAt is whether the worst phase rose 0.5 A or more in the 3 s + // from t. + raisedAt := func(t float64) bool { + b := d.at(t - 0.5) + if b == nil { + return true + } + for _, r := range d.window(t, t+3) { + if maxAbsI(r) >= maxAbsI(b)+0.5 { + return true + } + } + return false + } unexplained, longestOwn := 0.0, 0.0 var firstBad float64 - var start *sample - for i, r := range run { - over := maxAbsI(r) > fuse - if over && start == nil { - start = r - } - if (!over || i == len(run)-1) && start != nil { - end := r.T - if to, ok := explained(start.T); ok { - if end > to { - unexplained = math.Max(unexplained, end-to) - if firstBad == 0 { - firstBad = to - } + bad := func(t float64) { + if firstBad == 0 { + firstBad = t + } + } + // judged lists each stretch the heat or 1.2 x the fuse put on the clock: + // from, end, clock start and allowed until, in seconds from the start, + // and whether it was FTW's own raise or a step. + judged := [][]any{} + judgeStretch := func(stretch []*sample, end float64) { + s := stretch[0].T + var clock *sample + overA, export := 0.0, false + for _, r := range stretch { + if clock == nil && heatOver(r, fuse) { + clock = r + } + if w := worstSignedA(r); math.Abs(w)-fuse > overA { + overA, export = math.Abs(w)-fuse, w < 0 + } + } + if clock == nil { + return // the heat budget covers it + } + c := clock.T + // A window explains the stretch unless FTW pushed it over the + // fuse after the window's meter delay (ownRaise). + to, explained, own := math.Inf(-1), false, false + for _, w := range windows { + if x := until(w, s, overA, export); w.from <= s && s <= x && !ownRaise(w.from+2, s, end, export) { + to, explained = math.Max(to, x), true + } + } + if !explained { // no change outside FTW: its own, import or export + own, to = true, c+3 + } + if !own && c > s { // Core answers once the heat says so + to = math.Max(to, c+lever(c, overA, export)+2+3) + } + // brokeAt is the first time from t on that the stretch is over the + // fuse; an over sample holds until the next one. + brokeAt := func(t float64) float64 { + for i, r := range stretch { + next := end + if i+1 < len(stretch) { + next = stretch[i+1].T + } + if maxAbsI(r) > fuse && next > t { + return math.Max(r.T, t) } - } else if end-start.T > 3 { - longestOwn = math.Max(longestOwn, end-start.T) - if firstBad == 0 { - firstBad = start.T + 3 + } + return math.Inf(1) + } + // heatCap is the most that windows opening from t on may add: once + // a phase's 60 s heat is over the fuse, Core's cut has its lever's + // time, and no further event extends the stretch (owner, 28 Oct + // 2026). Unbounded while the heat stays under the fuse. + heatCap := func(t float64) float64 { + for _, r := range stretch { + if r.T >= t && max(r.HeatA[0], r.HeatA[1], r.HeatA[2]) > fuse { + return r.T + lever(r.T, overA, export) + 2 + 3 } } - start = nil + return math.Inf(1) + } + // A window that opens inside the stretch before it broke its + // allowance extends it: a second step, an outage or a Core restart. + // A plain house event counts only if it raised the current. Inside + // FTW's own raise a window counts only once the raise is answered: + // after its 3 s, with the current under the fuse until the window. + // What windows add stops at the heat's cap, however many there are. + // A window that opens once the allowance has run out, with the + // current under the fuse since, is a new step: its cap runs from it. + capT := heatCap(s) + for grown := true; grown; { + grown = false + for _, w := range windows { + if w.from < s || w.from > math.Min(end, brokeAt(to)) || (own && w.from <= to) { + continue + } + if w.plain && !raisedAt(w.from) { + continue + } + if w.from > to { + capT = math.Max(capT, heatCap(w.from)) + } + if x := math.Min(until(w, w.from, overA, export), capT); x > to { + to, grown, own = x, true, false + } + } + } + kind := "step" + if own { + kind = "own" + } + judged = append(judged, []any{round(s-t0, 1), round(end-t0, 1), round(c-t0, 1), round(to-t0, 1), kind}) + switch { + case end <= to: + case own: + longestOwn = math.Max(longestOwn, end-c) + bad(to) + default: + unexplained = math.Max(unexplained, end-to) + bad(to) } } - m["over_limit_after_steps_s"], m["over_limit_own_s"] = round(unexplained, 1), round(longestOwn, 1) - - // 3: with every device reading and no fault, Home use is measured. - inFault := func(t float64) bool { - for from, to := range faultUntil { - if t >= from && t <= to+30 { - return true + from, last := -1, -1 + for i, r := range run { + if maxAbsI(r) > fuse { + if from < 0 { + from = i } + last = i } - return false + open := last == i || (from >= 0 && max(r.HeatA[0], r.HeatA[1], r.HeatA[2]) > fuse) + if (!open || i == len(run)-1) && from >= 0 { + judgeStretch(run[from:last+1], run[min(last+1, len(run)-1)].T) + from = -1 + } + } + heatPeak := 0.0 + for _, r := range run { + heatPeak = math.Max(heatPeak, max(r.HeatA[0], r.HeatA[1], r.HeatA[2])) } + m["over_limit_after_steps_s"], m["over_limit_own_s"] = round(unexplained, 1), round(longestOwn, 1) + m["heat_peak_a"], m["over_limit_judged"] = round(heatPeak, 2), judged + + // 3: with every device reading and no fault, Home use is measured. + inFault := func(t float64) bool { return within(faults, t, 30) } restartAt := []float64{t0} for _, mk := range d.allMarks { if mk.Label == "ftw:up" { @@ -345,12 +780,27 @@ func judgeInvariants(d *runData) (metrics, []criterion) { } m["home_use_estimated_while_reading_s"] = round(dishonest, 1) - // 4: during Charge now the car charges when there is room for it. + // 4: during Charge now the car charges when there is room for it. With + // room, Core waits evRestartDwell before it starts a stopped car, then + // the charger takes its own time, and a tick. The count of idle seconds + // with room starts again on each Charge now that changes the request. + // A cut FTW had to send needs no new start: the count runs only while + // the car is idle with room, so it starts when the cut car stops. A + // cut with room is FTW's doing and starts nothing. While Core is down + // the count stops. Only a fault of the meter or of the charger excuses + // an idle car. var holds []eventWindow + var graceFrom []float64 var holdFrom float64 + changes, k := d.chargeNowChanges(), 0 for _, mk := range d.allMarks { switch mk.Label { case "ftw:charge_now": + changed := k >= len(changes) || changes[k] + k++ + if holdFrom == 0 || changed { + graceFrom = append(graceFrom, mk.T) + } if holdFrom == 0 { holdFrom = mk.T } @@ -361,18 +811,49 @@ func judgeInvariants(d *runData) (metrics, []criterion) { } } } - grace := 60 + h.commandS + h.startupS + 10 + // roomA is the room below the fuse for the car, Core's line; 6 A is + // the car's minimum on three phases. + roomA := func(r *sample) float64 { return fuse - (maxAbsI(r) - carW(r, 0)/(3*volt)) } + // While Core is down nothing can start the car. + var down []eventWindow + for i, mk := range d.allMarks { + if mk.Label != "ftw:restart" { + continue + } + up := t1 + for _, n := range d.allMarks[i:] { + if n.Label == "ftw:up" { + up = n.T + break + } + } + down = append(down, eventWindow{mk.T, up}) + } + // The car may stand idle with room for Core's dwell, the charger's + // command and startup time and a tick, plus the 2 s for the meter and + // tick and 3 s the fuse budgets also give. + allowedIdle := evRestartDwellS + h.commandS + h.startupS + tickS + 2 + 3 + regrace := func(from, to float64) bool { + for _, g := range graceFrom { + if g > from && g <= to { + return true + } + } + return false + } idle, run4 := 0.0, 0.0 for _, hw := range holds { run4 = 0 - for i, r := range d.window(hw.from+grace, hw.to) { - // Room below fuse - 1 A, the car's line in Core. - roomA := fuse - 1 - (maxAbsI(r) - carW(r, 0)/(3*volt)) - bad := carW(r, 0) < 0.5*1380 && roomA >= 7 && !inFault(r.T) && !nearRestart(r.T) + ws := d.window(hw.from, hw.to) + for i, r := range ws { + if i > 0 && regrace(ws[i-1].T, r.T) { + run4 = 0 + } + bad := carW(r, 0) < 0.5*1380 && roomA(r) >= 6 && !within(chargerFaults, r.T, 30) && !within(down, r.T, 0) if bad && i > 0 { - run4++ + run4 += r.T - ws[i-1].T idle = math.Max(idle, run4) - if idle > 30 { + if run4 > allowedIdle { note("charge now", r.T) } } else { @@ -380,7 +861,7 @@ func judgeInvariants(d *runData) (metrics, []criterion) { } } } - m["charge_now_idle_with_room_s"] = idle + m["charge_now_idle_with_room_s"], m["charge_now_idle_allowed_s"] = round(idle, 1), allowedIdle // 5: no order above the charger's 16 A per phase. over := 0 @@ -392,17 +873,19 @@ func judgeInvariants(d *runData) (metrics, []criterion) { } m["offers_over_rating"] = over - // 6: FTW does not ask the battery to discharge at its floor. + // 6: FTW does not ask the battery to discharge at its floor (floorPct). + // FTW's orders are judged, not the inverter's register, which can hold + // an order from before an outage; check 8 judges that. floorS := 0.0 for _, r := range run { - if r.Bat.EMSCmd == 187 && r.Bat.EMSSetW > 50 && r.Bat.SoCPct <= h.floorPct+0.5 { + if w, ok := dispatchW(r); ok && w < -50 && r.Bat.SoCPct <= floorPct { floorS++ if floorS > 10 { note("floor", r.T) } } } - m["discharge_at_floor_s"] = floorS + m["discharge_at_floor_s"], m["floor_pct"] = floorS, floorPct // 7: a stale meter means no raise. raises := 0 @@ -420,6 +903,45 @@ func judgeInvariants(d *runData) (metrics, []criterion) { } } m["raises_on_a_stale_meter"] = raises + + // 8: a battery FTW does not dispatch runs in its own mode (R8, C1). The + // inverter's register may hold a forced charge or discharge FTW does + // not dispatch for forcedBudgetS. After an inverter outage or a Core + // restart the count starts once Core has the inverter back (backFrom). + // While Core is stopped with SIGTERM its driver cleanup writes the + // default, so a running inverter gets the same 10 s from the stop. One + // that comes back while Core is down has its outage's allowance. The + // triage's line is 10 s from the return itself; the seconds past that + // line that Core's timers excuse are recorded, not hidden. + backFrom := d.inverterBackFrom(inverterOut, down) + forcedS, pastS, timersS, run8, from8 := 0.0, 0.0, 0.0, 0.0, 0.0 + for i, r := range run { + w, ok := dispatchW(r) + stray := math.Abs(batOrder(r)) > 50 && !within(inverterOut, r.T, 0) + if !within(down, r.T, 0) { + stray = stray && r.Status != nil && (!ok || math.Abs(w) <= 50) + } + if !stray || i == 0 { + run8 = 0 + continue + } + if run8 == 0 { + from8 = run[i-1].T + } + run8 += r.T - run[i-1].T + forcedS = math.Max(forcedS, run8) + at, from := backFrom(r.T) + switch { + case r.T > math.Max(from8, from)+forcedBudgetS: + pastS += r.T - run[i-1].T + note("forced battery", r.T) + case r.T > math.Max(from8, at)+forcedBudgetS: + timersS += r.T - run[i-1].T + } + } + m["battery_forced_without_dispatch_s"] = round(forcedS, 1) + m["battery_forced_past_allowance_s"] = round(pastS, 1) + m["battery_forced_in_core_timers_s"] = round(timersS, 1) // The first second an invariant broke, from the start, per invariant: // a kept seed is cut a minute after the one it failed on. firsts := map[string]float64{} @@ -432,10 +954,11 @@ func judgeInvariants(d *runData) (metrics, []criterion) { crit("over the limit no longer than an outside step allows", unexplained == 0), crit("over the limit at most 3 s after FTW's own raise", longestOwn == 0), crit("Home use measured while every device reads", dishonest <= 10), - crit("Charge now charges when there is room", idle <= 30), + crit("Charge now charges when there is room", idle <= allowedIdle), crit("no offer above the charger's rating", over == 0), crit("no discharge asked at the battery's floor", floorS <= 10), crit("no raise on a stale meter", raises == 0), + crit("no forced battery mode FTW does not dispatch", pastS == 0), } } @@ -488,6 +1011,7 @@ var invariantKey = map[string]string{ "no offer above the charger's rating": "rating", "no discharge asked at the battery's floor": "floor", "no raise on a stale meter": "stale meter", + "no forced battery mode FTW does not dispatch": "forced battery", } // keepFailure writes a failing random run as a case to keep: its plan with diff --git a/go/test/site/random_unit_test.go b/go/test/site/random_unit_test.go new file mode 100644 index 000000000..19fb13209 --- /dev/null +++ b/go/test/site/random_unit_test.go @@ -0,0 +1,1263 @@ +//go:build unix + +package site + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "regexp" + "strconv" + "strings" + "testing" +) + +// Unit tests for the random judge (judgeInvariants), each on the shape of +// a nightly seed that the judge called wrong (triage of 27 Oct 2026, rig +// items R2-R7). They run in every go test, without a simulator. + +const ( + limitStep = "over the limit no longer than an outside step allows" + limitOwn = "over the limit at most 3 s after FTW's own raise" + chargeNow = "Charge now charges when there is room" + atFloor = "no discharge asked at the battery's floor" +) + +// randomRun is n one-second samples on a fuse, from start at 0 to end at +// n-1: house current base per phase, the car idle, the battery at 50 % +// with its inverter running. set changes sample i. +func randomRun(n int, fuse, base float64, set func(i int, r *sample)) *runData { + d := &runData{marks: map[string]float64{"start": 0, "end": float64(n - 1)}, meta: map[string]any{"fuse": fuse}} + running := true + for i := 0; i < n; i++ { + r := &sample{T: float64(i), PhaseA: [3]float64{base, base, base}, CarW: []float64{0}, + Bat: plantDevice{SoCPct: 50, Running: &running}} + if set != nil { + set(i, r) + } + d.samples = append(d.samples, r) + } + return d +} + +// withCharger records the charger's command and startup times as the house +// wrote them, so the judge reads them as from a real run. +func withCharger(t *testing.T, d *runData, commandS, startupS float64) { + t.Helper() + dir := t.TempDir() + spec := houseSpec{Devices: []houseDevice{{ID: "garage", CommandResponseS: &commandS, StartupDelayS: &startupS}}} + b, _ := json.Marshal(spec) + if err := os.MkdirAll(filepath.Join(dir, "house"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "house", "timeline.json"), b, 0o644); err != nil { + t.Fatal(err) + } + d.dir = dir +} + +func judged(d *runData) map[string]bool { + _, cs := judgeInvariants(d) + return verdictsOf(cs) +} + +// R2, seed 207539's shape: a step the battery answers opens the stretch, +// and a Core restart 1 s into it extends what the stretch may take. A +// restart that comes after the step's time ran out excuses nothing. +func TestInvariantsExtendAStretchByEveryWindowInsideIt(t *testing.T) { + for _, c := range []struct { + name string + restartAt float64 // 0: no restart + want bool + }{ + {"restart 1 s into the stretch", 120, true}, + {"no restart", 0, false}, + {"restart after the step's time", 130, false}, + {"restart 0.2 s after the step's time", 121.9, false}, + } { + // 20 A fuse; 26 A (1.3 x) from 119 to 143 s, after a load step + // at 114.7 s. The battery's 5 kW (7.2 A) covers the 6 A overage, + // so the step alone allows 2 + 2 + 3 s. + d := randomRun(301, 20, 10, func(i int, r *sample) { + if i >= 119 && i < 144 { + r.PhaseA = [3]float64{26, 13, 12} + } + }) + withCharger(t, d, 40, 0) + d.events = []houseEventLine{{T: 114.7, Event: houseEvent{Do: "loads"}}} + if c.restartAt > 0 { + d.allMarks = []markLine{{T: c.restartAt, Label: "ftw:restart"}, {T: c.restartAt + 9, Label: "ftw:up"}} + } + if got := judged(d)[limitStep]; got != c.want { + t.Errorf("%s: %q = %v, want %v", c.name, limitStep, got, c.want) + } + } +} + +// R3, seeds 207534, 207530 and 207539: the battery answers in 2 s only +// while its inverter runs and its spare power covers the overage; otherwise +// the charger is the lever, in its own time (5 s here). +func TestInvariantsBatteryIsTheLeverOnlyWhenItCanAnswer(t *testing.T) { + for _, c := range []struct { + name string + batW float64 + running bool + want bool + }{ + {"battery idle, 7.2 A spare for a 4 A overage", 0, true, false}, + {"battery already discharging 3266 W, 2.5 A spare", -3266, true, true}, + {"inverter offline", 0, false, true}, + } { + // 16 A fuse; 20 A (1.25 x) from 50 to 59 s after a step at 49.5 s: + // 9.5 s, past the battery's 7 s and inside the charger's 10 s. + d := randomRun(101, 16, 8, func(i int, r *sample) { + r.BatW, r.Bat.Running = c.batW, &c.running + if i >= 50 && i < 59 { + r.PhaseA = [3]float64{20, 12, 12} + } + }) + withCharger(t, d, 5, 0) + d.events = []houseEventLine{{T: 49.5, Event: houseEvent{Do: "loads"}}} + if got := judged(d)[limitStep]; got != c.want { + t.Errorf("%s: %q = %v, want %v", c.name, limitStep, got, c.want) + } + } +} + +// R4, seeds 207532 and 207537: a car that starts on the charger's own +// 16 A while FTW's order is 0 A is a step FTW did not take, answered by the +// 40 s charger. The same start right after FTW raised the order to 16 A is +// FTW's own. +func TestInvariantsCarStartingOnItsOwnIsAStep(t *testing.T) { + for _, c := range []struct { + name string + frames []frame + wantStep bool + wantOwn bool + }{ + {"FTW ordered 0 A", []frame{profileAt(0, 0), profileAt(20, 0)}, true, true}, + {"FTW raised to 16 A", []frame{profileAt(0, 0), profileAt(25, 16)}, true, false}, + } { + // 16 A fuse; the car draws 11 040 W from 30 s; the meter shows + // 20 A (1.25 x) from 31 to 39 s. + d := randomRun(121, 16, 4, func(i int, r *sample) { + if i >= 30 && i < 40 { + r.CarW[0] = 11040 + } + if i >= 31 && i < 39 { + r.PhaseA = [3]float64{20, 18, 19} + } + }) + withCharger(t, d, 40, 30) + d.frames = c.frames + v := judged(d) + if v[limitStep] != c.wantStep || v[limitOwn] != c.wantOwn { + t.Errorf("%s: step %v, own %v; want %v, %v", c.name, v[limitStep], v[limitOwn], c.wantStep, c.wantOwn) + } + } +} + +// R5, seed 207536: between the fuse and 1.2 x the fuse the 60 s heat is the +// budget (owner rule, 27 Oct 2026). 27.84 A on 25 A for 15 s from 15 A +// passes; above 1.2 x it fails once the step's time is up; so does the same +// 27.84 A held past the lever's time after the heat passes the fuse, and +// 25.5 A on a fuse already warm at its rating. +func TestInvariantsJudgeBetweenFuseAndInstantByHeat(t *testing.T) { + for _, c := range []struct { + name string + base, a float64 + from, to int + want bool + }{ + {"27.84 A for 15 s", 15, 27.84, 50, 65, true}, + {"30.1 A for 15 s", 15, 30.1, 50, 65, false}, + {"27.84 A for 200 s", 15, 27.84, 50, 250, false}, + {"25.5 A for 15 s on a warm fuse", 25, 25.5, 50, 65, false}, + // The heat passes the fuse at 127 s; the battery then has 2 + 5 s. + {"27.84 A until 7 s after the heat passes the fuse", 15, 27.84, 50, 134, true}, + {"27.84 A until 8 s after the heat passes the fuse", 15, 27.84, 50, 135, false}, + } { + d := randomRun(301, 25, c.base, func(i int, r *sample) { + if i >= c.from && i < c.to { + r.PhaseA[0] = c.a + } + }) + d.events = []houseEventLine{{T: 49.5, Event: houseEvent{Do: "loads"}}} + if got := judged(d)[limitStep]; got != c.want { + t.Errorf("%s: %q = %v, want %v", c.name, limitStep, got, c.want) + } + } +} + +// R6, seeds 207533, 207536 and 207539: during Charge now an idle car with +// room is allowed Core's 35 s dwell, the charger's command and startup time +// and a tick, plus 5 s (72 s here, with a 30 s startup). The grace starts +// again on each Charge now that changes the request and on each cut FTW +// had to send, over the fuse or with no room; a cut with room does not. +// While Core is down the count stops. A fault of the meter or the charger +// excuses the car; an inverter outage does not. +func TestInvariantsChargeNowGrace(t *testing.T) { + for _, c := range []struct { + name string + startS int // the car draws from here; 0: never + overS [2]int // the car draws 16 A, 21 A on the 16 A fuse, in [from, to) + marks []markLine + frames []frame + events []houseEventLine + want bool + }{ + {"starts after 70 s", 71, [2]int{}, nil, nil, nil, true}, + {"starts after 80 s", 81, [2]int{}, nil, nil, nil, false}, + {"a changed Charge now at 60 s", 121, [2]int{}, []markLine{{T: 60, Label: "ftw:charge_now"}}, nil, nil, true}, + {"FTW cuts the car over the fuse at 60 s", 121, [2]int{10, 61}, nil, + []frame{profileAt(0, 16), profileAt(60, 0)}, nil, true}, + {"FTW cuts an idle car with room at 60 s", 121, [2]int{}, nil, + []frame{profileAt(0, 16), profileAt(60, 0)}, nil, false}, + {"FTW hunts 6 A, 0 A, 6 A with room", 0, [2]int{}, nil, + []frame{profileAt(0, 6), profileAt(15, 0), profileAt(30, 6), profileAt(45, 0), + profileAt(60, 6), profileAt(75, 0), profileAt(90, 6), profileAt(105, 0), + profileAt(120, 6), profileAt(135, 0), profileAt(150, 6), profileAt(165, 0), + profileAt(180, 6), profileAt(195, 0)}, nil, false}, + {"Core restarts at 50 s and answers at 60 s", 121, [2]int{}, + []markLine{{T: 50, Label: "ftw:restart"}, {T: 60, Label: "ftw:up"}}, nil, nil, true}, + {"the meter late for 40 s from 10 s", 140, [2]int{}, nil, nil, + []houseEventLine{{T: 10, Event: houseEvent{Do: "meter_late", ForS: 40}}}, true}, + {"the inverter offline for 40 s from 10 s", 140, [2]int{}, nil, nil, + []houseEventLine{{T: 10, Event: houseEvent{Do: "offline", Device: "inverter", ForS: 40}}}, false}, + } { + // 16 A fuse, a 5 A house: 11 A of room. Charge now at 1 s. + d := randomRun(201, 16, 5, func(i int, r *sample) { + if c.startS > 0 && i >= c.startS { + r.CarW[0] = 6900 + r.PhaseA = [3]float64{15, 15, 15} + } + if i >= c.overS[0] && i < c.overS[1] { + r.CarW[0] = 11040 + r.PhaseA = [3]float64{21, 21, 21} + } + }) + withCharger(t, d, 0, 30) + d.allMarks = append([]markLine{{T: 1, Label: "ftw:charge_now"}}, c.marks...) + d.allMarks = append(d.allMarks, markLine{T: 200, Label: "end"}) + d.frames, d.events = c.frames, c.events + if got := judged(d)[chargeNow]; got != c.want { + t.Errorf("%s: %q = %v, want %v", c.name, chargeNow, got, c.want) + } + } +} + +// R7, seeds 207534 and 207536: the floor judges FTW's own discharge orders +// (status.dispatch) against the higher of the inverter's 5 % and the +// planner's 10 %, with no extra 0.5 %. A forced discharge the inverter +// still holds while FTW dispatches nothing is not FTW's order. +func TestInvariantsFloorJudgesFTWsOrders(t *testing.T) { + for _, c := range []struct { + name string + socPct float64 + orderW float64 // FTW's dispatch; 0: none + forcedW float64 // the inverter's register + want bool + }{ + {"FTW orders 3 kW at 5.0 %", 5, -3000, 3000, false}, + {"FTW orders 3 kW at 9 %, under the planner's 10 %", 9, -3000, 3000, false}, + {"FTW orders 3 kW at 10.5 %", 10.5, -3000, 3000, true}, + {"the register holds 3 kW at 5.0 %, FTW orders nothing", 5, 0, 3000, true}, + } { + d := randomRun(61, 16, 5, func(i int, r *sample) { + r.Bat.SoCPct = c.socPct + r.Bat.EMSMode, r.Bat.EMSCmd, r.Bat.EMSSetW = 2, 187, c.forcedW + r.Status = &ftwStatus{} + if c.orderW != 0 { + r.Status.Dispatch = []ftwDispatch{{Driver: "sungrow", TargetW: c.orderW}} + } + }) + if got := judged(d)[atFloor]; got != c.want { + t.Errorf("%s: %q = %v, want %v", c.name, atFloor, got, c.want) + } + } +} + +// An over-limit stretch no outside change explains is FTW's own, whichever +// way the current flows and however it is cut up: FTW's discharge pushing +// export over the fuse, and a warm fuse held over its rating with a 1 s dip +// every 7 s, or every 4 s. A dip does not end a stretch while the heat stays +// over the fuse. All fail after 3 s. +func TestInvariantsAStretchWithNoStepIsFTWsOwn(t *testing.T) { + export := randomRun(121, 16, -10, func(i int, r *sample) { + w := 0.0 + if i >= 47 { + w, r.BatW = -5000, -5000 + } + r.Status = &ftwStatus{Dispatch: []ftwDispatch{{Driver: "sungrow", TargetW: w}}} + if i >= 50 && i < 56 { + r.PhaseA = [3]float64{-21, -21, -21} + } + }) + dips := func(every int) *runData { + return randomRun(601, 25, 25.5, func(i int, r *sample) { + if i%every == every-1 { + r.PhaseA = [3]float64{24.9, 24.9, 24.9} + } + }) + } + for name, d := range map[string]*runData{"export 6 s at 1.31 x In": export, + "600 s on a warm fuse, a dip every 7 s": dips(7), "600 s on a warm fuse, 3 s over and 1 s under": dips(4)} { + withCharger(t, d, 5, 0) + if v := judged(d); v[limitOwn] { + t.Errorf("%s: %q passed", name, limitOwn) + } + } +} + +// A window that opens inside FTW's own raise does not turn it into a step: +// FTW raises the car at 45 s and the phases sit at 31 A (1.24 x 25 A) from +// 50 to 60 s; PV moving at 51 s changes nothing, and a load step at 51 s +// that adds 1 A comes after FTW's raise broke the line. The battery is at +// its floor, so the 40 s charger would be a step's lever. +func TestInvariantsAnEventInsideFTWsRaiseLeavesItOwn(t *testing.T) { + for _, c := range []struct { + name string + event string // at 51 s; "": none + addA float64 + }{ + {"no event", "", 0}, + {"PV moves", "pv", 0}, + {"a load step adds 1 A", "loads", 1}, + } { + d := randomRun(121, 25, 10, func(i int, r *sample) { + r.Bat.SoCPct = 6 + if i >= 50 && i < 60 { + r.PhaseA = [3]float64{31, 31, 31} + } + if i >= 51 && i < 60 { + r.PhaseA[0] += c.addA + } + }) + withCharger(t, d, 40, 0) + d.frames = []frame{profileAt(40, 0), profileAt(45, 16)} + if c.event != "" { + d.events = []houseEventLine{{T: 51, Event: houseEvent{Do: c.event}}} + } + if v := judged(d); v[limitOwn] { + t.Errorf("%s: FTW's raise held 10 s at 1.24 x In and passed", c.name) + } + } +} + +// The battery is the 2 s lever for import only above the floor the floor +// check uses (the planner's 10 % here), and never while its inverter is +// out; a charger outage delays the charger. Only a meter fault adds its +// length to a step's time. 16 A fuse, a 5 s charger, 20 A on L1 (1.25 x). +func TestInvariantsLeverAndFaults(t *testing.T) { + for _, c := range []struct { + name string + socPct float64 + from, to int // 20 A on L1 in [from, to) + events []houseEventLine + want bool + }{ + {"a step at 8 %: the charger answers", 8, 50, 59, nil, true}, + {"a step at 11 %: the battery answers", 11, 50, 59, nil, false}, + {"a step while the inverter is offline", 50, 50, 59, + []houseEventLine{{T: 30, Event: houseEvent{Do: "offline", Device: "inverter", ForS: 40}}}, true}, + {"the inverter offline 40 s, 15 s over", 50, 50, 65, + []houseEventLine{{T: 45, Event: houseEvent{Do: "offline", Device: "inverter", ForS: 40}}}, false}, + {"the meter late 40 s, 15 s over", 50, 50, 65, + []houseEventLine{{T: 45, Event: houseEvent{Do: "meter_late", ForS: 40}}}, true}, + {"the charger away 20 s, 18 s over", 8, 50, 68, + []houseEventLine{{T: 45, Event: houseEvent{Do: "reconnect", Device: "garage", ForS: 20}}}, true}, + } { + d := randomRun(121, 16, 8, func(i int, r *sample) { + r.Bat.SoCPct = c.socPct + if i >= c.from && i < c.to { + r.PhaseA = [3]float64{20, 12, 12} + } + }) + withCharger(t, d, 5, 0) + d.events = append([]houseEventLine{{T: 49.5, Event: houseEvent{Do: "loads"}}}, c.events...) + if got := judged(d)[limitStep]; got != c.want { + t.Errorf("%s: %q = %v, want %v", c.name, limitStep, got, c.want) + } + } +} + +// forcedCase is an R8 run: the inverter's register holds a forced command +// from the start until forcedTo, and FTW dispatches the battery from +// dispatchFrom (0: never). An outage of the inverter (offline: it stops; a +// silence: it runs but does not answer) or a Core restart may come first. +type forcedCase struct { + name string + outage string // "offline", "silence" or "" + outFrom float64 // the outage, from outFrom + outFor float64 // for outFor + readAt float64 // Core's polls of the inverter fail from outFrom until here + stop, up float64 // a Core restart (SIGTERM at stop), if up > 0 + cmd float64 // 187 discharge, 170 charge + setW float64 + forcedTo float64 + dispatchFrom float64 + want bool +} + +func (c forcedCase) run() *runData { + // FTW dispatched the battery until the outage or the stop. + lead := c.outFrom + if c.up > 0 && (c.outage == "" || c.stop < lead) { + lead = c.stop + } + d := randomRun(1201, 16, 5, func(i int, r *sample) { + t := float64(i) + if t < c.forcedTo { + r.Bat.EMSMode, r.Bat.EMSCmd, r.Bat.EMSSetW = 2, c.cmd, c.setW + } else { + r.Bat.EMSCmd = 204 + } + if c.outage == "offline" && t >= c.outFrom && t < c.outFrom+c.outFor { + off := false + r.Bat.Running = &off + } + if c.up > 0 && t >= c.stop && t <= c.up { + return // Core is down: no reading + } + errs := 0 + if c.outage != "" && t >= c.outFrom && t < c.readAt { + errs = 1 + int(t-c.outFrom)/2 + } + r.Status = &ftwStatus{Drivers: map[string]ftwDriver{inverterDriver: {Status: "ok", ConsecutiveErrors: &errs}}} + if t < lead || (c.dispatchFrom > 0 && t >= c.dispatchFrom) { + r.Status.Dispatch = []ftwDispatch{{Driver: inverterDriver, TargetW: -c.setW}} + } + }) + if c.outage != "" { + d.events = []houseEventLine{{T: c.outFrom, Event: houseEvent{Do: c.outage, Device: "inverter", ForS: c.outFor}}} + } + if c.up > 0 { + d.allMarks = []markLine{{T: c.stop, Label: "ftw:restart"}, {T: c.up, Label: "ftw:up"}} + } + return d +} + +// R8, C1, on the shapes of the nightly seeds on master 83c2b24c. Once Core +// has the inverter back, a battery FTW does not dispatch leaves its forced +// mode within 10 s; #1915 (C11) sends the default on Core's first good +// read. The Modbus reconnect backoff (10 s since #1930) between the +// inverter's return and that read is Core's to take. +func TestInvariantsNoForcedBatteryWithoutDispatch(t *testing.T) { + const forced = "no forced battery mode FTW does not dispatch" + read207539 := 182 + reconnectBackoffMaxS - 4 + for _, c := range []forcedCase{ + // 207533: offline 576.6-644.7 s, Core reads 9 s later, 187 / + // 2668 W until FTW dispatches again 236 s after the return (C1). + {name: "207533: held until Core's 5 min retry", outage: "offline", outFrom: 100, outFor: 68, readAt: 177, + cmd: 187, setW: 2668, forcedTo: 1201, dispatchFrom: 404, want: false}, + // 207535: the same with a forced charge, 170 / 2632 W, for 235 s. + {name: "207535: a forced charge held", outage: "offline", outFrom: 100, outFor: 69, readAt: 177, + cmd: 170, setW: 2632, forcedTo: 404, want: false}, + // 207537: the inverter runs but does not answer for 46.4 s; Core + // reads 19.8 s after it answers again, the discharge holds 264 s. + {name: "207537: held after a silence", outage: "silence", outFrom: 100, outFor: 46, readAt: 166, + cmd: 187, setW: 3854, forcedTo: 1201, dispatchFrom: 410, want: false}, + // 207538: Core reads 2 s after the return; held to the run's end. + {name: "207538: held to the end", outage: "offline", outFrom: 100, outFor: 39, readAt: 141, + cmd: 187, setW: 4674, forcedTo: 1201, want: false}, + // The same outages once C1 is fixed: the default goes out within + // 10 s of Core's first good read. + {name: "default 8 s after Core reads again", outage: "offline", outFrom: 100, outFor: 68, readAt: 177, + cmd: 187, setW: 2668, forcedTo: 185, want: true}, + // 207539: offline 508.5-590.0 s, the Modbus backoff (60 s then) + // held Core's first good read back 55.7 s, and FTW dispatched 20 s + // after it. With #1915 (C11) the default goes out on that first + // read, so 20 s fails. + {name: "207539: 20 s after Core's first read", outage: "offline", outFrom: 100, outFor: 82, readAt: read207539, + cmd: 187, setW: 814, forcedTo: 1201, dispatchFrom: read207539 + 20, want: false}, + {name: "past the registry's 30 s retry", outage: "offline", outFrom: 100, outFor: 82, readAt: read207539, + cmd: 187, setW: 814, forcedTo: 1201, dispatchFrom: read207539 + 47, want: false}, + // Core never reads the inverter again: the count starts at the + // backoff's cap after the return all the same. + {name: "Core never reads it again", outage: "offline", outFrom: 100, outFor: 82, readAt: 1201, + cmd: 187, setW: 814, forcedTo: 1201, dispatchFrom: 290, want: false}, + {name: "FTW dispatches the discharge", cmd: 187, setW: 3000, forcedTo: 1201, dispatchFrom: 1, want: true}, + {name: "forced with no dispatch and no outage", cmd: 187, setW: 3000, forcedTo: 1201, dispatchFrom: 500, want: false}, + // Item 10, 207538's restart at 625.5 s: SIGTERM, the driver's + // cleanup writes 204 within a second, Core is back 14.6 s later + // and dispatches 6 s after that. + {name: "207538: SIGTERM restart writes the default", stop: 100, up: 115, cmd: 187, setW: 868, + forcedTo: 101, dispatchFrom: 121, want: true}, + // The same stop as SIGKILL: no cleanup, the discharge runs on + // while Core is down. + {name: "restart without cleanup", stop: 100, up: 115, cmd: 187, setW: 868, + forcedTo: 121, dispatchFrom: 121, want: false}, + // 207530: Core stops while the inverter is offline (888.7-916.4 + // s), so no cleanup can land. The inverter is back 2.8 s before + // Core (here 13 s), and the default lands as Core starts. + {name: "207530: restart while the inverter is offline", outage: "offline", outFrom: 90, outFor: 12, readAt: 116, + stop: 100, up: 115, cmd: 170, setW: 2983, forcedTo: 116, dispatchFrom: 125, want: true}, + // A Core that starts fresh has no default retry to wait for: 10 s + // from its first good read, not the 40 s an outage gets. + {name: "forced 8 s after Core starts", stop: 100, up: 101, cmd: 187, setW: 868, + forcedTo: 1201, dispatchFrom: 110, want: true}, + {name: "forced 23 s after Core starts", stop: 100, up: 101, cmd: 187, setW: 868, + forcedTo: 1201, dispatchFrom: 125, want: false}, + // The inverter is back at 102 while Core, stopped at 100 during the + // outage, stays down until 150. Core's first read is at 151 and the + // default lands 7 s later. While Core is down nothing reads the + // inverter, so the count cannot start before 151. + {name: "inverter back while Core is down for 48 s", outage: "offline", outFrom: 90, outFor: 12, readAt: 151, + stop: 100, up: 150, cmd: 187, setW: 3000, forcedTo: 158, dispatchFrom: 170, want: true}, + } { + if got := judged(c.run())[forced]; got != c.want { + t.Errorf("%s: %q = %v, want %v", c.name, forced, got, c.want) + } + } +} + +// R8 with two inverter outages: the count after the second starts at its +// own return and Core's first read after it, not the first outage's. +func TestInvariantsForcedBatteryCountsFromTheLatestReturn(t *testing.T) { + const forced = "no forced battery mode FTW does not dispatch" + // Outage 1 100-120, Core reads at 125 and dispatches 130-300. Outage 2 + // 300-340, Core reads at 345 and the default lands at 353. + run := func(forcedTo float64) *runData { + d := randomRun(601, 16, 5, func(i int, r *sample) { + tt := float64(i) + if tt < forcedTo { + r.Bat.EMSMode, r.Bat.EMSCmd, r.Bat.EMSSetW = 2, 187, 3000 + } else { + r.Bat.EMSCmd = 204 + } + if (tt >= 100 && tt < 120) || (tt >= 300 && tt < 340) { + off := false + r.Bat.Running = &off + } + errs := 0 + if (tt >= 100 && tt < 125) || (tt >= 300 && tt < 345) { + errs = 3 + } + r.Status = &ftwStatus{Drivers: map[string]ftwDriver{inverterDriver: {Status: "ok", ConsecutiveErrors: &errs}}} + if tt < 100 || (tt >= 130 && tt < 300) { + r.Status.Dispatch = []ftwDispatch{{Driver: inverterDriver, TargetW: -3000}} + } + }) + d.events = []houseEventLine{ + {T: 100, Event: houseEvent{Do: "offline", Device: "inverter", ForS: 20}}, + {T: 300, Event: houseEvent{Do: "offline", Device: "inverter", ForS: 40}}, + } + return d + } + if !judged(run(353))[forced] { + t.Errorf("default 8 s after Core reads again after the second outage: got fail, want pass") + } + if judged(run(1201))[forced] { + t.Errorf("forced to the end after the second outage: got pass, want fail") + } +} + +// R8 forgives the seconds Core's reconnect backoff and default retry hold +// its first write after a return (reconnectBackoffMaxS, defaultRetryMaxS). +// The triage line is 10 s from the return, so the judge records what it +// forgave beyond that line instead of hiding it. +func TestInvariantsRecordForcedSecondsInsideCoreTimers(t *testing.T) { + // 207539's shape: back at 182, Core reads 4 s before the backoff's cap + // ends (238 with 60 s) and dispatches 7 s later, inside the backoff + // alone. Forced with no dispatch until then; past 182+10 that is from + // 193 (52 s with 60 s). + read := 182 + reconnectBackoffMaxS - 4 + c := forcedCase{outage: "offline", outFrom: 100, outFor: 82, readAt: read, + cmd: 187, setW: 814, forcedTo: 1201, dispatchFrom: read + 7} + m, _ := judgeInvariants(c.run()) + if in, past, want := m["battery_forced_in_core_timers_s"], m["battery_forced_past_allowance_s"], read+6-192; in != want || past != 0.0 { + t.Errorf("in Core's timers %v s, past the allowance %v s; want %v and 0", in, past, want) + } + // Forced to 299: from 193 to the allowance's end inside the timers, + // the rest past them. + c.dispatchFrom = 300 + m, _ = judgeInvariants(c.run()) + end := read + defaultRetryMaxS + forcedBudgetS + if in, past := m["battery_forced_in_core_timers_s"], m["battery_forced_past_allowance_s"]; in != end-192 || past != 299-end { + t.Errorf("in Core's timers %v s, past the allowance %v s; want %v and %v", in, past, end-192, 299-end) + } +} + +// R8's allowances are Core's own timers. Read them from Core's source so +// they cannot drift. Once the registry retries a failed default on the +// first good poll after an outage (#1915, C11), defaultRetryMaxS must be +// 0: from then on the 30 s the judge gives today is no longer Core's. +func TestForcedAllowancesMatchCore(t *testing.T) { + src := func(p string) string { + b, err := os.ReadFile(filepath.Join("..", "..", "internal", p)) + if err != nil { + t.Fatal(err) + } + return string(b) + } + seconds := func(code, name string) float64 { + m := regexp.MustCompile(name + `\s*=\s*(\d+)\s*\*\s*time\.Second`).FindStringSubmatch(code) + if m == nil { + t.Fatalf("no %s = N * time.Second in Core's source", name) + } + n, _ := strconv.Atoi(m[1]) + return float64(n) + } + // reconnectBackoffMaxS is read from Core's source; the 207539 shapes + // below need a backoff that can hold Core's first read back past 4 s. + if reconnectBackoffMaxS < 5 { + t.Errorf("Core's reconnectBackoffMax is %v s; rework the 207539 shapes", reconnectBackoffMaxS) + } + registry := src("drivers/registry.go") + // Today the registry sends the default on a failure and on its retry + // timer: two calls. #1915 adds a third, on the first good poll. + calls := strings.Count(registry, "attemptDefault()") + switch { + case calls == 2 && defaultRetryMaxS != seconds(registry, "defaultRetryMax"): + t.Errorf("defaultRetryMaxS = %v, Core's defaultRetryMax is %v s", defaultRetryMaxS, seconds(registry, "defaultRetryMax")) + case calls > 2 && defaultRetryMaxS != 0: + t.Errorf("Core's registry calls attemptDefault() in %d places, not 2. If it now retries "+ + "the default on the first good poll after an outage (#1915, C11), set defaultRetryMaxS "+ + "to 0 and turn the R8 cases that lean on it to fail. Otherwise update this check.", calls) + case calls < 2: + t.Errorf("Core's registry calls attemptDefault() in %d places; update this check", calls) + } +} + +// TestJudge also judges the random seeds a results directory holds, from +// the plan each run wrote. +func TestRecordedRandomFindsSeedRuns(t *testing.T) { + dir := t.TempDir() + for _, seed := range []int64{7, 9} { + b, _ := json.Marshal(drawPlan(seed)) + os.MkdirAll(filepath.Join(dir, fmt.Sprintf("random-%d", seed)), 0o755) + os.WriteFile(filepath.Join(dir, fmt.Sprintf("random-%d", seed), "plan.json"), b, 0o644) + } + var got []string + for _, sc := range recordedRandom(dir) { + got = append(got, sc.name+"@"+sc.dir) + } + if len(got) != 2 || got[0] != "r7@random-7" || got[1] != "r9@random-9" { + t.Fatalf("recordedRandom = %v", got) + } +} + +// R2 after the second review of #1920: a house event that is no fault +// extends a step's stretch only if the worst phase rose 0.5 A or more +// within 3 s of it. 16 A fuse, the battery at its floor and a 40 s charger, +// so a step allows 45 s; 20 A on L1 (1.25 x) from 12 s, after a load step +// at 10 s. +func TestInvariantsOnlyARiseExtendsAStretch(t *testing.T) { + for _, c := range []struct { + name string + to int // 20 A on L1 until here + riseAt int // L1 rises to 21 A from here; 0: never + every float64 // a load event that changes nothing, this often; 0: none + want bool + }{ + {"a load event at 50 s raises L1", 80, 50, 0, true}, + {"load events every 40 s change nothing", 290, 0, 40, false}, + } { + d := randomRun(301, 16, 8, func(i int, r *sample) { + r.Bat.SoCPct = 6 + if i >= 12 && i < c.to { + r.PhaseA = [3]float64{20, 12, 12} + if c.riseAt > 0 && i >= c.riseAt { + r.PhaseA[0] = 21 + } + } + }) + withCharger(t, d, 40, 0) + d.events = []houseEventLine{{T: 10, Event: houseEvent{Do: "loads"}}} + if c.riseAt > 0 { + d.events = append(d.events, houseEventLine{T: float64(c.riseAt), Event: houseEvent{Do: "loads"}}) + } + for t0 := 50.0; c.every > 0 && t0 < 290; t0 += c.every { + d.events = append(d.events, houseEventLine{T: t0, Event: houseEvent{Do: "loads"}}) + } + if got := judged(d)[limitStep]; got != c.want { + t.Errorf("%s: %q = %v, want %v", c.name, limitStep, got, c.want) + } + } +} + +// The inverter is back at the end of its outage: its return is no step +// unless the current rises, and then the battery answers in 2 s, not the +// charger. 16 A fuse, a 40 s charger, the inverter out from 10 to 40 s; +// the outage allows until 55 s. +func TestInvariantsTheInverterIsBackAtItsReturn(t *testing.T) { + for _, c := range []struct { + name string + from, to int // 20 A on L1 in [from, to) + }{ + {"over from 12 to 80 s, the return changes nothing", 12, 80}, + {"the current rises at the return, over until 60 s", 40, 60}, + } { + d := randomRun(121, 16, 8, func(i int, r *sample) { + run := !(i >= 10 && i < 40) + r.Bat.Running = &run + if i >= c.from && i < c.to { + r.PhaseA = [3]float64{20, 12, 12} + } + }) + withCharger(t, d, 40, 0) + d.events = []houseEventLine{ + {T: 10, Event: houseEvent{Do: "offline", Device: "inverter", ForS: 30}}, + {T: 40, Event: houseEvent{Do: "online", Device: "inverter"}}, + } + if judged(d)[limitStep] { + t.Errorf("%s: %q passed", c.name, limitStep) + } + } +} + +// R4 after the second review of #1920: a car raise FTW sends after a +// step's meter delay, or after a Core restart, is FTW's own; one sent +// before the meter shows the step is part of the step. 25 A fuse, the +// battery at its floor and a 40 s charger; the phases sit at 31 A +// (1.24 x) from 50 to 80 s. +func TestInvariantsARaiseAfterAStepIsFTWsOwn(t *testing.T) { + for _, c := range []struct { + name string + raiseAt float64 + event bool // PV moves at 40 s + restart bool // Core restarts at 30 s and is back at 35 s + want bool // both limit criteria hold + }{ + {"FTW raises 5 s after PV moved", 45, true, false, false}, + {"FTW raises 1 s after PV moved", 41, true, false, true}, + {"FTW raises 15 s after Core is back", 50, false, true, false}, + } { + d := randomRun(121, 25, 10, func(i int, r *sample) { + r.Bat.SoCPct = 6 + if i >= 50 && i < 80 { + r.PhaseA = [3]float64{31, 31, 31} + r.CarW[0] = 11040 + } + }) + withCharger(t, d, 40, 0) + d.frames = []frame{profileAt(20, 0), profileAt(c.raiseAt, 16)} + if c.event { + d.events = []houseEventLine{{T: 40, Event: houseEvent{Do: "pv", Device: "inverter"}}} + } + if c.restart { + d.allMarks = []markLine{{T: 30, Label: "ftw:restart"}, {T: 35, Label: "ftw:up"}} + } + v := judged(d) + if got := v[limitStep] && v[limitOwn]; got != c.want { + t.Errorf("%s: step %v, own %v; want both %v", c.name, v[limitStep], v[limitOwn], c.want) + } + } +} + +// A Core restart explains a stretch for 60 s from the restart. 16 A fuse, +// Core restarts at 50 s and is back at 55 s; 20 A on L1 (1.25 x). +func TestInvariantsARestartExplainsAMinute(t *testing.T) { + for _, c := range []struct { + name string + from, to int + want bool + }{ + {"over from 100 to 108 s", 100, 108, true}, + {"over from 115 to 125 s", 115, 125, false}, + } { + d := randomRun(201, 16, 8, func(i int, r *sample) { + if i >= c.from && i < c.to { + r.PhaseA = [3]float64{20, 12, 12} + } + }) + withCharger(t, d, 5, 0) + d.allMarks = []markLine{{T: 50, Label: "ftw:restart"}, {T: 55, Label: "ftw:up"}} + v := judged(d) + if got := v[limitStep] && v[limitOwn]; got != c.want { + t.Errorf("%s: step %v, own %v; want both %v", c.name, v[limitStep], v[limitOwn], c.want) + } + } +} + +// For export the battery's spare is what it can charge more: a battery +// charging 4 kW has 1 kW, too little for a 4 A overage, so the 5 s charger +// answers; one discharging 4 kW has 9 kW and answers in 2 s, even at its +// floor. 16 A fuse; -20 A on L1 from 50 to 59 s after a step at 49.5 s. +func TestInvariantsBatteryLeverForExport(t *testing.T) { + for _, c := range []struct { + name string + batW float64 + socPct float64 + want bool + }{ + {"charging 4 kW", 4000, 50, true}, + {"discharging 4 kW", -4000, 50, false}, + {"idle at its floor", 0, 6, false}, + } { + d := randomRun(101, 16, -8, func(i int, r *sample) { + r.BatW, r.Bat.SoCPct = c.batW, c.socPct + if i >= 50 && i < 59 { + r.PhaseA = [3]float64{-20, -12, -12} + } + }) + withCharger(t, d, 5, 0) + d.events = []houseEventLine{{T: 49.5, Event: houseEvent{Do: "pv", Device: "inverter"}}} + if got := judged(d)[limitStep]; got != c.want { + t.Errorf("%s: %q = %v, want %v", c.name, limitStep, got, c.want) + } + } +} + +// A dip under the fuse keeps the stretch open while the heat stays over +// it, but a new step after the dip still gets its own time (2 + 5 s for +// the battery), after a step or after FTW's own raise that was cut in +// time. A step held past its time fails as a step. 16 A fuse at 15.95 A, +// so the heat stays over the fuse for a minute after a few seconds at 20 A +// on L1. +func TestInvariantsAStepAfterADipGetsItsTime(t *testing.T) { + for _, c := range []struct { + name string + first []houseEventLine // the change before 50 s; none: FTW's own + stepTo int // the step at 80 s holds 20 A until here + wantStep, wantOwn bool + }{ + {"after a step", []houseEventLine{{T: 49.5, Event: houseEvent{Do: "loads"}}}, 84, true, true}, + {"after FTW's own raise", nil, 84, true, true}, + {"held 8 s after FTW's own raise", nil, 88, false, true}, + } { + d := randomRun(201, 16, 15.95, func(i int, r *sample) { + if (i >= 50 && i < 53) || (i >= 80 && i < c.stepTo) { + r.PhaseA[0] = 20 + } + }) + withCharger(t, d, 5, 0) + d.events = append(c.first, houseEventLine{T: 80, Event: houseEvent{Do: "loads"}}) + d.warm() + if h := d.at(79).HeatA[0]; h <= 16 { + t.Fatalf("%s: heat %.2f A at 79 s, want over the fuse", c.name, h) + } + if v := judged(d); v[limitStep] != c.wantStep || v[limitOwn] != c.wantOwn { + t.Errorf("%s: step %v, own %v; want %v, %v", c.name, v[limitStep], v[limitOwn], c.wantStep, c.wantOwn) + } + } +} + +// R4 after the third review of #1920: a car or battery raise FTW sends +// while an import stretch is over the fuse is FTW's own, after a step's +// meter delay too, and a house event that changes nothing does not take +// the rise from it. One sent in a dip under the fuse, with the heat still +// over it, is allowed: the step after it gets its time. Raising the car +// into export is the right answer. 40 s charger, the battery at its +// floor. +func TestInvariantsARaiseOverTheFuseIsFTWsOwn(t *testing.T) { + for _, c := range []struct { + name string + fuse float64 + set func(i int, r *sample) + frames []frame + events []houseEventLine + want bool // both limit criteria hold + }{ + {"0 -> 16 A at 1.24 x In after PV moved", 25, func(i int, r *sample) { + if i >= 41 && i < 84 { + r.PhaseA = [3]float64{31, 31, 31} + } + if i >= 52 && i < 84 { + r.PhaseA = [3]float64{42, 42, 42} + } + }, []frame{profileAt(20, 0), profileAt(50, 16)}, + []houseEventLine{{T: 40, Event: houseEvent{Do: "pv", Device: "inverter"}}}, false}, + {"6 -> 10 A at 1.25 x In, a load event that changes nothing as it lands", 16, func(i int, r *sample) { + if i >= 11 && i < 75 { + r.PhaseA = [3]float64{20, 12, 12} + } + if i >= 32 && i < 75 { + r.PhaseA[0] = 22 + } + }, []frame{profileAt(1, 6), profileAt(30, 10)}, + []houseEventLine{{T: 10, Event: houseEvent{Do: "loads"}}, {T: 31, Event: houseEvent{Do: "loads"}}}, false}, + {"battery charge 0 -> 3 kW at 1.25 x In after a load step", 16, func(i int, r *sample) { + r.Status = &ftwStatus{Dispatch: []ftwDispatch{{Driver: "sungrow", TargetW: 0}}} + if i >= 30 { + r.Status.Dispatch[0].TargetW, r.BatW = 3000, 3000 + } + if i >= 11 && i < 55 { + r.PhaseA = [3]float64{20, 12, 12} + } + if i >= 32 && i < 55 { + r.PhaseA = [3]float64{22, 16, 16} + } + }, nil, []houseEventLine{{T: 10, Event: houseEvent{Do: "loads"}}}, false}, + {"car and battery raised in a dip under the fuse, a step after it", 16, func(i int, r *sample) { + r.Status = &ftwStatus{Dispatch: []ftwDispatch{{Driver: "sungrow", TargetW: 0}}} + if i >= 56 { + r.Status.Dispatch[0].TargetW = 2000 + } + switch { + case i >= 11 && i < 45, i >= 71 && i < 76: + r.PhaseA = [3]float64{22, 12, 12} + case i >= 45 && i < 71: + r.PhaseA = [3]float64{15, 12, 12} + } + }, []frame{profileAt(1, 6), profileAt(55, 8)}, + []houseEventLine{{T: 10, Event: houseEvent{Do: "loads"}}, {T: 70, Event: houseEvent{Do: "loads"}}}, true}, + {"0 -> 16 A into export over the fuse after PV moved", 16, func(i int, r *sample) { + r.PhaseA, r.BatW = [3]float64{-8, -8, -8}, 4000 + if i >= 11 && i < 45 { + r.PhaseA = [3]float64{-24, -12, -12} + } + }, []frame{profileAt(1, 0), profileAt(20, 16)}, + []houseEventLine{{T: 10, Event: houseEvent{Do: "pv", Device: "inverter"}}}, true}, + } { + d := randomRun(121, c.fuse, 12, func(i int, r *sample) { + r.Bat.SoCPct = 6 + c.set(i, r) + }) + withCharger(t, d, 40, 0) + d.frames, d.events = c.frames, c.events + v := judged(d) + if got := v[limitStep] && v[limitOwn]; got != c.want { + t.Errorf("%s: step %v, own %v; want both %v", c.name, v[limitStep], v[limitOwn], c.want) + } + } +} + +// R4 for the battery: a charge raise FTW sends after a Core restart is +// its own, as a car raise is (triage C2). An order moved while the +// inverter is off reaches no battery. 16 A fuse, Core restarts at 50 s +// and is back at 55 s; the order goes 0 -> 4 kW at 60 s; 21 A on L1 +// (1.31 x) from 61 to 105 s. +func TestInvariantsABatteryRaiseAfterARestartIsFTWsOwn(t *testing.T) { + for _, c := range []struct { + name string + running bool // the inverter runs from 55 to 70 s + want bool + }{ + {"the inverter runs", true, false}, + {"the inverter is off", false, true}, + } { + d := randomRun(201, 16, 8, func(i int, r *sample) { + w := 0.0 + if i >= 60 { + w = 4000 + } + r.Status = &ftwStatus{Dispatch: []ftwDispatch{{Driver: "sungrow", TargetW: w}}} + if i >= 55 && i < 70 { + run := c.running + r.Bat.Running = &run + } + if i >= 61 && c.running { + r.BatW = w + } + if i >= 61 && i < 105 { + r.PhaseA = [3]float64{21, 13, 13} + } + }) + withCharger(t, d, 5, 0) + d.allMarks = []markLine{{T: 50, Label: "ftw:restart"}, {T: 55, Label: "ftw:up"}} + v := judged(d) + if got := v[limitStep] && v[limitOwn]; got != c.want { + t.Errorf("%s: step %v, own %v; want both %v", c.name, v[limitStep], v[limitOwn], c.want) + } + } +} + +// Fourth review of #1920: what FTW's battery order did, read correctly. +// While Core is down, or before its first tick, status has no dispatch, +// but the inverter keeps the last order: a missing order is no move. A +// move counts from 0.5 A a phase (345 W), as raisedAt does, so a one- +// sample 158 W wobble of Core's closed loop (seed 207539 at 151.7 s) is +// no raise. 16 A fuse, a load step at 40 s puts 20 A on L1. +func TestInvariantsABatteryOrderMovesOnlyWhenFTWMovesIt(t *testing.T) { + order := func(r *sample, w float64) { + r.Status = &ftwStatus{Dispatch: []ftwDispatch{{Driver: "sungrow", TargetW: w}}} + } + for _, c := range []struct { + name string + set func(i int, r *sample) + down bool // Core restarts at 44 s and is back at 49 s + want bool // both limit criteria hold + }{ + // At 6 % SoC the battery charging 1 kW cannot take 4 A a phase + // off, so the charger is the lever; Core's restart extends the + // stretch to 104 s. + {"1 kW order kept while Core is down", func(i int, r *sample) { + r.BatW, r.Bat.SoCPct = 1000, 6 + order(r, 1000) + if i >= 41 && i < 80 { + r.PhaseA = [3]float64{20, 8, 8} + } + }, true, true}, + {"1 kW order missing while Core is down, then restored", func(i int, r *sample) { + r.BatW, r.Bat.SoCPct = 1000, 6 + if i < 44 || i >= 50 { + order(r, 1000) + } + if i >= 41 && i < 80 { + r.PhaseA = [3]float64{20, 8, 8} + } + }, true, true}, + {"0 W order missing while Core is down, then 1 kW", func(i int, r *sample) { + r.Bat.SoCPct = 6 + switch { + case i < 44: + order(r, 0) + case i >= 50: + order(r, 1000) + r.BatW = 1000 + } + if i >= 41 && i < 80 { + r.PhaseA = [3]float64{20, 8, 8} + } + }, true, false}, + // The battery discharges 4 999 W and answers in 2 s: allowed + // until 47 s. + {"steady discharge order", func(i int, r *sample) { + r.BatW = -4999 + order(r, -4999) + if i >= 41 && i < 46 { + r.PhaseA = [3]float64{20, 8, 8} + } + }, false, true}, + {"one 158 W wobble toward import", func(i int, r *sample) { + r.BatW = -4999 + order(r, -4999) + if i == 43 { + order(r, -4841) + } + if i >= 41 && i < 46 { + r.PhaseA = [3]float64{20, 8, 8} + } + }, false, true}, + {"one 360 W move toward import", func(i int, r *sample) { + r.BatW = -4999 + order(r, -4999) + if i == 43 { + order(r, -4639) + } + if i >= 41 && i < 46 { + r.PhaseA = [3]float64{20, 8, 8} + } + }, false, false}, + } { + d := randomRun(121, 16, 8, c.set) + withCharger(t, d, 40, 0) + d.events = []houseEventLine{{T: 40, Event: houseEvent{Do: "loads"}}} + if c.down { + d.allMarks = []markLine{{T: 44, Label: "ftw:restart"}, {T: 49, Label: "ftw:up"}} + } + m, _ := judgeInvariants(d) + v := judged(d) + if got := v[limitStep] && v[limitOwn]; got != c.want { + t.Errorf("%s: step %v, own %v; want both %v; judged %v", c.name, v[limitStep], v[limitOwn], c.want, m["over_limit_judged"]) + } + } +} + +// Export is judged by what pushes export. A car raise before an export +// stretch soaks it up, so the stretch keeps its step's time. A battery +// pushed toward export after a Core restart is FTW's own push: the +// restart's minute does not excuse it. +func TestInvariantsExportIsFTWsOwnOnlyWhenFTWPushedExport(t *testing.T) { + t.Run("car raise before an export stretch", func(t *testing.T) { + off := false + d := randomRun(121, 16, 0, func(i int, r *sample) { + r.Bat.Running = &off + if i >= 45 && i < 80 { + r.PhaseA = [3]float64{-20, -20, -20} + } + }) + withCharger(t, d, 40, 0) + d.frames = []frame{profileAt(20, 0), profileAt(43, 16)} + d.events = []houseEventLine{{T: 40, Event: houseEvent{Do: "pv", Device: "inverter"}}} + m, _ := judgeInvariants(d) + if v := judged(d); !(v[limitStep] && v[limitOwn]) { + t.Errorf("step %v, own %v; judged %v", v[limitStep], v[limitOwn], m["over_limit_judged"]) + } + }) + for _, c := range []struct { + name string + running bool // the inverter runs from 55 s + want bool + }{ + {"battery pushed 0 -> -5 kW after a restart", true, false}, + {"the same order with the inverter off", false, true}, + } { + t.Run(c.name, func(t *testing.T) { + d := randomRun(201, 16, 0, func(i int, r *sample) { + w := 0.0 + if i >= 57 { + w = -5000 + } + r.Status = &ftwStatus{Dispatch: []ftwDispatch{{Driver: "sungrow", TargetW: w}}} + if i >= 55 && !c.running { + run := false + r.Bat.Running = &run + } + if i >= 58 && c.running { + r.BatW = w + } + if i >= 58 && i < 100 { + r.PhaseA = [3]float64{-21, -10, -10} + } + }) + withCharger(t, d, 5, 0) + d.allMarks = []markLine{{T: 50, Label: "ftw:restart"}, {T: 55, Label: "ftw:up"}} + m, _ := judgeInvariants(d) + v := judged(d) + if got := v[limitStep] && v[limitOwn]; got != c.want { + t.Errorf("step %v, own %v; want both %v; judged %v", v[limitStep], v[limitOwn], c.want, m["over_limit_judged"]) + } + }) + } + // Inside an export stretch after a PV step. The battery charges at + // its maximum, so the 40 s charger is the lever. Moving its order + // toward export while the meter is over the fuse is FTW's own; the + // same move in a dip under the fuse, with a step after it, is not. + for _, c := range []struct { + name string + moveAt int // the order goes 5 -> 4 kW; 0: no move + dip bool + want bool + }{ + {"no move", 0, false, true}, + {"battery moved toward export over the fuse", 20, false, false}, + {"the same move in a dip, a step after it", 56, true, true}, + } { + t.Run(c.name, func(t *testing.T) { + d := randomRun(121, 16, -12, func(i int, r *sample) { + r.BatW = rigBatteryMaxW + w := rigBatteryMaxW + if c.moveAt > 0 && i >= c.moveAt { + w = 4000 + } + r.Status = &ftwStatus{Dispatch: []ftwDispatch{{Driver: "sungrow", TargetW: w}}} + switch { + case !c.dip && i >= 11 && i < 45: + r.PhaseA = [3]float64{-22, -12, -12} + case c.dip && (i >= 11 && i < 45 || i >= 71 && i < 76): + r.PhaseA = [3]float64{-22, -12, -12} + case c.dip && i >= 45 && i < 71: + r.PhaseA = [3]float64{-15, -12, -12} + } + }) + withCharger(t, d, 40, 0) + d.events = []houseEventLine{{T: 10, Event: houseEvent{Do: "pv", Device: "inverter"}}} + if c.dip { + d.events = append(d.events, houseEventLine{T: 70, Event: houseEvent{Do: "pv", Device: "inverter"}}) + } + m, _ := judgeInvariants(d) + v := judged(d) + if got := v[limitStep] && v[limitOwn]; got != c.want { + t.Errorf("step %v, own %v; want both %v; judged %v", v[limitStep], v[limitOwn], c.want, m["over_limit_judged"]) + } + }) + } +} + +// The line is In, not 1.2 x In: a car raise sent while the meter shows +// 1.1 x In (under the instant cut, the heat still cool) is sent over the +// line, and the stretch is FTW's own. +func TestInvariantsARaiseBetweenFuseAndInstantIsOwn(t *testing.T) { + d := randomRun(121, 25, 12, func(i int, r *sample) { + r.Bat.SoCPct = 6 + if i >= 41 && i < 84 { + r.PhaseA = [3]float64{27.5, 27.5, 27.5} + } + if i >= 52 && i < 84 { + r.PhaseA = [3]float64{42, 42, 42} + } + }) + withCharger(t, d, 40, 0) + d.frames = []frame{profileAt(20, 0), profileAt(50, 16)} + d.events = []houseEventLine{{T: 40, Event: houseEvent{Do: "pv", Device: "inverter"}}} + m, _ := judgeInvariants(d) + if v := judged(d); v[limitStep] && v[limitOwn] { + t.Errorf("a raise sent at 1.1 x In passed; judged %v", m["over_limit_judged"]) + } +} + +// Owner, 28 Oct 2026: what events add to a stretch stops at the fuse's +// heat budget. Once L1's 60 s heat passes the fuse, Core's cut has its +// lever's time (here the 5 s charger: the battery is at its floor), and +// no further event extends the stretch, however many come. 16 A fuse, +// 8 A on each phase, a load step at 10 s puts L1 at 20 A (1.25 x In) +// from 11 s; a load event every 8 s from 18 s raises it 0.5 A from a +// 1 s dip (the third review's E3). L1's heat passes the fuse at 62 s, so +// the cap is 62 + 5 + 2 + 3 = 72 s. A Core restart at 15 s, which would +// excuse a minute, is capped too: with no dips the heat passes at 61 s. +func TestInvariantsHeatCapsWhatEventsAdd(t *testing.T) { + for _, c := range []struct { + name string + to int // L1 over the fuse until here + restartAt float64 // a Core restart instead of the load events; 0: none + want bool + }{ + {"events end before the heat passes the fuse", 55, 0, true}, + {"events end inside Core's cut time", 72, 0, true}, + {"events past the heat's cap", 76, 0, false}, + {"events chained 280 s", 290, 0, false}, + {"a Core restart past the heat's cap", 74, 15, false}, + {"a Core restart before the heat passes the fuse", 55, 15, true}, + } { + d := randomRun(301, 16, 8, func(i int, r *sample) { + r.Bat.SoCPct = 6 + if i >= 11 && i < c.to { + r.PhaseA[0] = 20 + if c.restartAt == 0 && i >= 17 && (i-17)%8 == 0 { + r.PhaseA[0] = 19.5 + } + } + }) + withCharger(t, d, 5, 0) + d.events = []houseEventLine{{T: 10, Event: houseEvent{Do: "loads"}}} + if c.restartAt > 0 { + d.allMarks = []markLine{{T: c.restartAt, Label: "ftw:restart"}, {T: c.restartAt + 9, Label: "ftw:up"}} + } else { + for at := 18.0; at < float64(c.to); at += 8 { + d.events = append(d.events, houseEventLine{T: at, Event: houseEvent{Do: "loads"}}) + } + } + d.warm() + if h := d.at(61).HeatA[0]; c.restartAt == 0 && h > 16 { + t.Fatalf("%s: heat %.2f A at 61 s, want under the fuse", c.name, h) + } + if h := d.at(62).HeatA[0]; c.to > 62 && h <= 16 { + t.Fatalf("%s: heat %.2f A at 62 s, want over the fuse", c.name, h) + } + m, _ := judgeInvariants(d) + if got := judged(d)[limitStep]; got != c.want { + t.Errorf("%s: %q = %v, want %v; judged %v", c.name, limitStep, got, c.want, m["over_limit_judged"]) + } + } +} + +// Owner, 28 Oct 2026: a car cut FTW sends that pushes export over the +// fuse is FTW's own, as a battery move toward export is: after a step's +// 2 s meter delay and before the stretch, or while the stretch is over +// the fuse. A cut the meter cannot have shown the step for yet is part +// of the step. 16 A fuse, 12 A export per phase, the battery charging at +// its maximum, so the 40 s charger is the lever; PV steps at 10 s and +// L1 exports 22 A from `from` to 45 s, inside the step's 55 s. +func TestInvariantsACarCutIntoExportIsFTWsOwn(t *testing.T) { + for _, c := range []struct { + name string + from int + frames []frame + want bool // both limit criteria hold + }{ + {"no cut", 11, []frame{profileAt(1, 16)}, true}, + {"16 -> 0 A while export is over the fuse", 11, []frame{profileAt(1, 16), profileAt(20, 0)}, false}, + {"16 -> 6 A after the meter shows the step, before the stretch", 20, []frame{profileAt(1, 16), profileAt(15, 6)}, false}, + {"16 -> 6 A before the meter shows the step", 20, []frame{profileAt(1, 16), profileAt(11, 6)}, true}, + {"6 -> 16 A while export is over the fuse", 11, []frame{profileAt(1, 6), profileAt(20, 16)}, true}, + } { + d := randomRun(121, 16, -12, func(i int, r *sample) { + r.BatW = rigBatteryMaxW + r.Status = &ftwStatus{Dispatch: []ftwDispatch{{Driver: "sungrow", TargetW: rigBatteryMaxW}}} + if i >= c.from && i < 45 { + r.PhaseA[0] = -22 + } + }) + withCharger(t, d, 40, 0) + d.frames = c.frames + d.events = []houseEventLine{{T: 10, Event: houseEvent{Do: "pv", Device: "inverter"}}} + m, _ := judgeInvariants(d) + v := judged(d) + if got := v[limitStep] && v[limitOwn]; got != c.want { + t.Errorf("%s: step %v, own %v; want both %v; judged %v", c.name, v[limitStep], v[limitOwn], c.want, m["over_limit_judged"]) + } + } +} diff --git a/go/test/site/rig_test.go b/go/test/site/rig_test.go index 35184ffb2..f507fc33d 100644 --- a/go/test/site/rig_test.go +++ b/go/test/site/rig_test.go @@ -204,12 +204,13 @@ func split(scs []scenario, n int) [][]scenario { type outcome struct { Name string `json:"name"` Dir string `json:"dir"` - Verdict string `json:"verdict"` // pass, known, fail + Verdict string `json:"verdict"` // pass, known, fail, skip DurationS float64 `json:"duration_s"` Criteria []criterion `json:"criteria"` Known []string `json:"known,omitempty"` Failures []string `json:"failures,omitempty"` Error string `json:"error,omitempty"` + Skipped string `json:"skipped,omitempty"` // what the simulator lacks } // TestSite runs the scenarios FTW_SITE names. FTW_SITE_LANES runs that many @@ -239,6 +240,16 @@ func TestSite(t *testing.T) { env := setup(t) var mu sync.Mutex var outcomes []outcome + var runnable []scenario + for _, sc := range scs { + if sc.skip == "" { + runnable = append(runnable, sc) + continue + } + t.Logf("%s skipped: %s", sc.name, sc.skip) + outcomes = append(outcomes, outcome{Name: sc.name, Dir: sc.dir, Verdict: "skip", Skipped: sc.skip}) + } + scs = runnable t.Run("lanes", func(t *testing.T) { for k, group := range split(scs, lanes) { k, group := k, group @@ -364,7 +375,7 @@ func TestJudge(t *testing.T) { t.Skip("set FTW_SITE_JUDGE to a results directory of an earlier run") } var outs []outcome - for _, sc := range selected(t, "full") { + for _, sc := range append(selected(t, "full"), recordedRandom(dir)...) { rundir := filepath.Join(dir, sc.dir) b, err := os.ReadFile(filepath.Join(rundir, "meta.json")) if err != nil { @@ -392,6 +403,25 @@ func TestJudge(t *testing.T) { writeSummary(t, dir, outs) } +// recordedRandom is each random seed recorded in dir, from the plan.json +// its run wrote, so TestJudge judges a nightly random run again. +func recordedRandom(dir string) []scenario { + files, _ := filepath.Glob(filepath.Join(dir, "random-*", "plan.json")) + sort.Strings(files) + var out []scenario + for _, f := range files { + b, err := os.ReadFile(f) + if err != nil { + continue + } + var p randomPlan + if json.Unmarshal(b, &p) == nil && p.Found == nil { + out = append(out, randomScenario(p)) + } + } + return out +} + func writeSummary(t *testing.T, out string, outs []outcome) { if len(outs) == 0 { return @@ -405,6 +435,9 @@ func writeSummary(t *testing.T, out string, outs []outcome) { if o.Error != "" { notes = append([]string{o.Error}, notes...) } + if o.Skipped != "" { + notes = append(notes, o.Skipped) + } fmt.Fprintf(&md, "| %s | %s | %s | %s |\n", o.Name, o.Verdict, (time.Duration(o.DurationS) * time.Second).String(), strings.ReplaceAll(strings.Join(notes, "; "), "|", "/")) } diff --git a/go/test/site/rig_unit_test.go b/go/test/site/rig_unit_test.go index 33db9fc8d..94f50466c 100644 --- a/go/test/site/rig_unit_test.go +++ b/go/test/site/rig_unit_test.go @@ -110,20 +110,25 @@ func TestRandomPlansAreSeeded(t *testing.T) { } // An over-limit stretch right after an outside step is the house's; the -// same stretch with no step before it is FTW's own and fails after 3 s. +// same stretch with no step before it is FTW's own and fails after 3 s, +// whether or not the record shows FTW raising anything. 31 A is over +// 1.2 x the 25 A fuse, so the heat gives no budget. func TestInvariantsTellStepsFromOwnRaises(t *testing.T) { - mk := func(withStep bool) *runData { + mk := func(withStep, withRise bool) *runData { d := &runData{marks: map[string]float64{"start": 0, "end": 100}, meta: map[string]any{"fuse": 25.0}} for i := 0; i <= 100; i++ { a := 10.0 if i >= 50 && i < 56 { - a = 26 // 6 s over the 25 A fuse + a = 31 // 6 s over the 25 A fuse } d.samples = append(d.samples, &sample{T: float64(i), PhaseA: [3]float64{a, a, a}, Bat: plantDevice{SoCPct: 50}}) } if withStep { d.events = append(d.events, houseEventLine{T: 49.5, Event: houseEvent{Do: "loads"}}) } + if withRise { + d.frames = []frame{profileAt(40, 0), profileAt(45, 16)} + } return d } failed := func(cs []criterion) []string { @@ -135,12 +140,144 @@ func TestInvariantsTellStepsFromOwnRaises(t *testing.T) { } return out } - if _, cs := judgeInvariants(mk(true)); len(failed(cs)) != 0 { + if _, cs := judgeInvariants(mk(true, false)); len(failed(cs)) != 0 { t.Fatalf("after a step: %v", failed(cs)) } - _, cs := judgeInvariants(mk(false)) - if f := failed(cs); len(f) != 1 || f[0] != "over the limit at most 3 s after FTW's own raise" { - t.Fatalf("without a step: %v", f) + for _, withRise := range []bool{false, true} { + _, cs := judgeInvariants(mk(false, withRise)) + if f := failed(cs); len(f) != 1 || f[0] != "over the limit at most 3 s after FTW's own raise" { + t.Fatalf("with no step, FTW's raise %v: %v", withRise, f) + } + } +} + +// The limit is the fuse rating: a phase may sit at exactly the fuse until +// the end of the run, and 0.1 A above it fails once the time budget is up +// while the fuse is already warm (heat at the fuse). From a cool fuse the +// same 20 s stays inside the heat budget. +func TestInvariantsJudgeAtTheFuse(t *testing.T) { + const fuse = 25.0 + const own = "over the limit at most 3 s after FTW's own raise" + const step = "over the limit no longer than an outside step allows" + for _, c := range []struct { + name string + base, a float64 // the level before 50 s, and from 50 s + to int // the level holds from 50 s until this second + withStep bool + want []string + }{ + {"at the fuse", fuse, fuse, 101, false, nil}, + {"at the fuse after a step", fuse, fuse, 101, true, nil}, + // 20 s is past the 3 s for FTW's own raise, and past the + // 2 + 2 + 3 s a step allows while the battery can answer. + {"0.1 A over", fuse, fuse + 0.1, 70, false, []string{own}}, + {"0.1 A over after a step", fuse, fuse + 0.1, 70, true, []string{step}}, + {"0.1 A over a cool fuse", 10, fuse + 0.1, 70, false, nil}, + {"0.1 A over a cool fuse after a step", 10, fuse + 0.1, 70, true, nil}, + } { + d := &runData{marks: map[string]float64{"start": 0, "end": 100}, meta: map[string]any{"fuse": fuse}} + // FTW raised the car's order at 45 s. + d.frames = []frame{profileAt(40, 0), profileAt(45, 16)} + if c.withStep { + d.frames = nil + } + for i := 0; i <= 100; i++ { + a := c.base + if i >= 50 && i < c.to { + a = c.a + } + d.samples = append(d.samples, &sample{T: float64(i), PhaseA: [3]float64{a, a, a}, Bat: plantDevice{SoCPct: 50}}) + } + if c.withStep { + d.events = append(d.events, houseEventLine{T: 49.5, Event: houseEvent{Do: "loads"}}) + } + var failed []string + _, cs := judgeInvariants(d) + for _, x := range cs { + if !x.OK { + failed = append(failed, x.Name) + } + } + if !slices.Equal(failed, c.want) { + t.Errorf("%s: failed %v, want %v", c.name, failed, c.want) + } + } +} + +// S1 expects the room the fuse rating gives: on 16 A with a 3 A house that +// is 13 A per phase, 8 970 W. The old line at fuse - 1 A (8 280 W) falls +// short of it. +func TestJudgeS1ExpectsTheRoomAtTheFuse(t *testing.T) { + const name = "target within 60 s" + for _, c := range []struct { + carW float64 + want bool + }{{8970, true}, {8280, false}} { + d := synth(181, map[string]float64{"charge_now": 0, "end": 180}, map[string]any{"fuse": 16.0, "house_a": 3}, + func(i int, r *sample) { + if i >= 10 { + r.CarW = []float64{c.carW} + } + }) + m, cs := judgeS1(d) + if m["target_w"] != 8970.0 { + t.Fatalf("target_w = %v, want 8970", m["target_w"]) + } + if got := verdictsOf(cs)[name]; got != c.want { + t.Errorf("car at %v W: %q = %v, want %v", c.carW, name, got, c.want) + } + } +} + +// S12 counts a cut as one on room alone while every phase is under the fuse +// by more than 0.1 A, and records fuse_limit while the worst phase has 6 A. +func TestJudgeS12MeasuresRoomAtTheFuse(t *testing.T) { + const fuse = 20.0 + for _, c := range []struct { + name string + a float64 // the worst phase when the order drops + cuts, limit int + }{ + {"at the fuse", fuse, 0, 0}, + {"0.1 A under", fuse - 0.1, 0, 0}, + {"0.5 A under", fuse - 0.5, 1, 0}, + {"5.5 A under", fuse - 5.5, 1, 0}, + {"6.5 A under", fuse - 6.5, 1, 1}, + } { + d := synth(121, map[string]float64{"plan": 0, "ready": 100, "end": 120}, map[string]any{"fuse": fuse, "plan_wh": 6600.0}, + func(i int, r *sample) { + r.PhaseA = [3]float64{c.a, c.a - 1, c.a - 2} + if i == 30 { + r.LPs = []ftwLoadpoint{{CommandedReason: "fuse_limit"}} + } + }) + d.frames = []frame{profileAt(10, 16), profileAt(20, 10)} + m, _ := judgeS12(d) + if m["cuts_with_room"] != c.cuts || m["fuse_limit_with_room_s"] != c.limit { + t.Errorf("%s: cuts %v, fuse_limit %v; want %d, %d", c.name, m["cuts_with_room"], m["fuse_limit_with_room_s"], c.cuts, c.limit) + } + } +} + +// During Charge now an idle car is a fault once the house leaves the car's +// 6 A minimum under the fuse; with less room it is not. +func TestInvariantsChargeNowRoomAtTheFuse(t *testing.T) { + const fuse = 25.0 + const name = "Charge now charges when there is room" + for _, c := range []struct { + houseA float64 + want bool + }{{fuse - 6, false}, {fuse - 5.5, true}} { + d := synth(201, map[string]float64{"start": 0, "end": 200}, map[string]any{"fuse": fuse}, + func(_ int, r *sample) { + r.PhaseA = [3]float64{c.houseA, c.houseA, c.houseA} + r.Bat.SoCPct = 50 + }) + d.allMarks = []markLine{{T: 1, Label: "ftw:charge_now"}, {T: 200, Label: "end"}} + _, cs := judgeInvariants(d) + if got := verdictsOf(cs)[name]; got != c.want { + t.Errorf("house %v A: %q = %v, want %v", c.houseA, name, got, c.want) + } } } @@ -208,50 +345,6 @@ func TestS8JudgesAtTheFuse(t *testing.T) { } } -// The limit is the fuse rating: a phase may sit at exactly the fuse until -// the end of the run, and 0.1 A above it fails once the time budget is up. -func TestInvariantsJudgeAtTheFuse(t *testing.T) { - const fuse = 25.0 - const own = "over the limit at most 3 s after FTW's own raise" - const step = "over the limit no longer than an outside step allows" - for _, c := range []struct { - name string - a float64 - to int // the level holds from 50 s until this second - withStep bool - want []string - }{ - {"at the fuse", fuse, 101, false, nil}, - {"at the fuse after a step", fuse, 101, true, nil}, - // 20 s is past the 3 s for FTW's own raise, and past the - // 2 + 2 + 3 s a step allows while the battery can answer. - {"0.1 A over", fuse + 0.1, 70, false, []string{own}}, - {"0.1 A over after a step", fuse + 0.1, 70, true, []string{step}}, - } { - d := &runData{marks: map[string]float64{"start": 0, "end": 100}, meta: map[string]any{"fuse": fuse}} - for i := 0; i <= 100; i++ { - a := 10.0 - if i >= 50 && i < c.to { - a = c.a - } - d.samples = append(d.samples, &sample{T: float64(i), PhaseA: [3]float64{a, a, a}, Bat: plantDevice{SoCPct: 50}}) - } - if c.withStep { - d.events = append(d.events, houseEventLine{T: 49.5, Event: houseEvent{Do: "loads"}}) - } - var failed []string - _, cs := judgeInvariants(d) - for _, x := range cs { - if !x.OK { - failed = append(failed, x.Name) - } - } - if !slices.Equal(failed, c.want) { - t.Errorf("%s: failed %v, want %v", c.name, failed, c.want) - } - } -} - // A run that selects planner_self plans in self_consumption; any other run // selects planner_passive_arbitrage and plans in passive_arbitrage. s15bat // needs Self, where PV surplus charges the battery (#1875). diff --git a/go/test/site/scenarios_p0_test.go b/go/test/site/scenarios_p0_test.go new file mode 100644 index 000000000..a2a68c4cf --- /dev/null +++ b/go/test/site/scenarios_p0_test.go @@ -0,0 +1,313 @@ +//go:build unix + +package site + +import ( + "encoding/json" + "os" + "path/filepath" + "syscall" + "time" +) + +// The P0 scenarios R1-R5 of the stable plan: a meter that stops answering, +// a battery that drops out, the planner's floor, a hard kill and a start +// into a running site. Their judges are in verdicts_test.go. A known line +// names the Wave 1 item whose fix deletes it. +func init() { + scenarios = append(scenarios, + scenario{name: "r1-25", dir: "r1-meter-silent-then-offline-25A", minutes: 10, run: func(c *runCtx) (map[string]any, error) { return r1(c, 25) }, + judge: judgeR1, skip: meterKnob, + known: map[string]string{"the battery order moves only toward 0 while the meter is silent": "A1: a failed poll keeps the held reading, and ComputeDispatch winds the battery against it (dispatch.go:1446-1449)"}}, + scenario{name: "r2-25", dir: "r2-battery-offline-25A", minutes: 8, run: func(c *runCtx) (map[string]any, error) { return r2(c, 25, 0) }, + judge: judgeR2}, + scenario{name: "r2-60-25", dir: "r2-battery-offline-slow60-25A", minutes: 10, run: func(c *runCtx) (map[string]any, error) { return r2(c, 25, 60) }, + judge: judgeR2}, + scenario{name: "r3-25", dir: "r3-planner-floor-25A", minutes: 13, run: func(c *runCtx) (map[string]any, error) { return r3(c, 25) }, + judge: judgeR3, + known: map[string]string{ + "no discharge ordered below the planner floor": "A4: fuse relief discharges to 5 %, below planner.soc_min (dispatch.go:4113-4124, 4495-4499)", + "the battery order swings at most 100 W over the last 5 min": "A3, A4: at the inverter's empty 5 % Core keeps ordering 3.9-5 kW of discharge (the slew step reopens it, dispatch.go:2148-2219)", + }}, + scenario{name: "r4-25", dir: "r4-sigkill-25A", minutes: 40, run: func(c *runCtx) (map[string]any, error) { return r4(c, 25) }, + judge: judgeR4, + known: map[string]string{"session energy counts again after each restart": "E1: a 1.6 transaction is not taken up after a restart, so session_wh stays 0 (handlers.go:615-617, #1787)"}}, + scenario{name: "r5a-25", dir: "r5a-start-car-charging-25A", minutes: 5, run: func(c *runCtx) (map[string]any, error) { return r5a(c, 25) }, + judge: judgeR5a}, + scenario{name: "r5b-25", dir: "r5b-start-stale-plan-25A", minutes: 27, run: func(c *runCtx) (map[string]any, error) { return r5b(c, 25) }, + judge: judgeR5b}, + scenario{name: "r5c-25", dir: "r5c-start-new-session-25A", minutes: 15, run: func(c *runCtx) (map[string]any, error) { return r5c(c, 25) }, + judge: judgeR5c}, + ) +} + +// ---- R1. The site meter goes silent, then offline, under Charge now ---- + +// meterKnob is the device-simulator change R1 waits for. +const meterKnob = "TODO(device-simulator): `silence` and `offline` must take the site meter " + + "(device \"meter\"); at the pinned commit they answer `no device \"meter\"`. Pin a commit " + + "that has it and delete this skip." + +// r1 puts Core's battery relief on a meter that stops answering: Modbus +// timeouts for 120 s, then, two minutes after it is back, a meter gone for +// 90 s. House 15 A per phase and Charge now 16 A put the site over the fuse +// unless the battery helps. +func r1(c *runCtx, fuse float64) (map[string]any, error) { + err := with(c, stackSpec{fuseA: fuse, loadsW: houseA(15), socPct: 50}, func(st *stack) error { + if err := st.plug(0); err != nil { + return err + } + sleepS(30) + st.mark("charge_now") + if err := st.chargeNow("garage", 16, "3p", 0); err != nil { + return err + } + sleepS(90) + st.mark("silent") + if err := st.event(houseEvent{Do: "silence", Device: "meter", ForS: 120}); err != nil { + return err + } + sleepS(120) + st.mark("back") + sleepS(120) + st.mark("offline") + if err := st.event(houseEvent{Do: "offline", Device: "meter", ForS: 90}); err != nil { + return err + } + sleepS(90) + st.mark("online") + sleepS(120) + st.mark("end") + return nil + }) + return map[string]any{"scenario": "r1", "fuse": fuse, "house_a": 15, "silent_s": 120, "offline_s": 90}, err +} + +// ---- R2. The battery drops out while the car's room rests on it ---- + +// r2 holds the battery at a 3 kW discharge under a 20 A house, so the car's +// room rests on it, then takes the inverter off the site for 180 s: each +// phase rises 4-6 A at once. The car takes at most 10 A, so the site stays +// under 1.2 x the fuse, where Core's 60 s heat budget, not an instant cut, +// decides when the car stops (control/fuse_heat.go). +func r2(c *runCtx, fuse, delayS float64) (map[string]any, error) { + err := with(c, stackSpec{fuseA: fuse, loadsW: houseA(20), socPct: 60, + chargers: []charger{{delayS: delayS, carMaxA: 10}}}, func(st *stack) error { + if err := st.plug(0); err != nil { + return err + } + st.mark("plug_wait") + sleepS(delayS + 20) + st.mark("battery_hold") + if err := st.post("/api/battery/manual_hold", map[string]any{"direction": "discharge", "power_w": 3000, "hold_s": 1800}, nil); err != nil { + return err + } + st.mark("charge_now") + if err := st.chargeNow("garage", 10, "3p", 0); err != nil { + return err + } + sleepS(delayS + 90) + st.mark("offline") + if err := st.event(houseEvent{Do: "offline", Device: "inverter"}); err != nil { + return err + } + sleepS(180) + st.mark("online") + if err := st.event(houseEvent{Do: "online", Device: "inverter"}); err != nil { + return err + } + sleepS(120) + st.mark("end") + return nil + }) + return map[string]any{"scenario": "r2", "fuse": fuse, "house_a": 20, "battery_hold_w": -3000, + "charger_delay_s": delayS, "offline_s": 180}, err +} + +// ---- R3. The battery near the planner's floor under fuse pressure ---- + +// r3 starts the battery at 12 % with the planner's floor at 10 % and the +// inverter's own at 5 %. House 15 A per phase and Charge now 16 A need the +// battery's help; the charger applies a profile 60 s late. +func r3(c *runCtx, fuse float64) (map[string]any, error) { + const delayS, floor = 60, 0.10 + err := with(c, stackSpec{fuseA: fuse, loadsW: houseA(15), socPct: 12, plannerSoCMin: floor, + chargers: []charger{{delayS: delayS}}}, func(st *stack) error { + if err := st.plug(0); err != nil { + return err + } + st.mark("plug_wait") + sleepS(delayS + 20) + st.mark("charge_now") + if err := st.chargeNow("garage", 16, "3p", 0); err != nil { + return err + } + sleepS(600) + st.mark("end") + return nil + }) + return map[string]any{"scenario": "r3", "fuse": fuse, "house_a": 15, "soc_pct": 12, + "planner_floor_pct": floor * 100, "charger_delay_s": delayS}, err +} + +// ---- R4. Core killed mid-charge and mid-discharge ---- + +// r4 kills Core with SIGKILL, as a power cut does: no driver cleanup, no +// history flush, an OCPP transaction left open. House 12 A per phase, the +// battery held at a 3 kW discharge, Charge now 16 A. The first kill lasts +// 20 s; two minutes later the second lasts 120 s, and the house steps to +// 16 A per phase while Core is gone. Like s7, the run first waits until the +// day's Home use is known, and long enough after it that one restart stays +// under the 10 % unknown-time rule. Core holds a battery for at most +// 1800 s, so the hold starts 300 s before the first kill and outlasts both. +func r4(c *runCtx, fuse float64) (map[string]any, error) { + var serial string + err := with(c, stackSpec{fuseA: fuse, loadsW: houseA(12), socPct: 60}, func(st *stack) error { + if err := st.plug(0); err != nil { + return err + } + serial = st.serial(0) + st.mark("charge_now") + if err := st.chargeNow("garage", 16, "3p", 0); err != nil { + return err + } + if waitUntil(900*time.Second, 5*time.Second, func() bool { + var s ftwStatus + return st.get("/api/status", &s) == nil && s.Energy.Today.LoadWh != nil + }) { + st.mark("today_known") + } + sleepS(600) + st.mark("battery_hold") + if err := st.post("/api/battery/manual_hold", map[string]any{"direction": "discharge", "power_w": 3000, "hold_s": 1800}, nil); err != nil { + return err + } + sleepS(300) + st.mark("kill1") + st.stopFTW(syscall.SIGKILL) + sleepS(20) + if err := st.startFTW(); err != nil { + return err + } + st.mark("up1") + sleepS(120) + st.mark("kill2") + st.stopFTW(syscall.SIGKILL) + st.mark("step") + if err := st.setLoadsA(16); err != nil { + return err + } + sleepS(120) + if err := st.startFTW(); err != nil { + return err + } + st.mark("up2") + sleepS(300) + st.mark("end") + return nil + }) + return map[string]any{"scenario": "r4", "fuse": fuse, "house_a": 12, "step_a": 16, "battery_hold_w": -3000, + "down_s": []float64{20, 120}, "charger": serial}, err +} + +// ---- R5. Core starts into a running site ---- + +// r5a starts Core while the car charges on its own: plugged when the house +// comes up, it starts a local transaction and draws all it can, 16 A on +// 14 A of house load. The battery sits at the inverter's 5 % floor, so it +// cannot carry the car: the site runs 30 A per phase until Core acts. +func r5a(c *runCtx, fuse float64) (map[string]any, error) { + err := with(c, stackSpec{fuseA: fuse, loadsW: houseA(14), socPct: 5, startAfterS: 60, + chargers: []charger{{plugged: true}}}, func(st *stack) error { + st.mark("ftw_up") + sleepS(180) + st.mark("end") + return nil + }) + return map[string]any{"scenario": "r5a", "fuse": fuse, "house_a": 14, "soc_pct": 5}, err +} + +// r5b saves a goal with hours of slack, so no fresh plan need charge now, +// then stops Core for 20 min and starts it on the plan it saved. For the +// first minute the rig asks for the plan once a second (plan.jsonl). On +// master it does not reach D6: the saved plan is younger than MaxPlanAge +// (30 min) and a fresh one lands within seconds, so the deadline reserve +// never acts. D6's proof is its unit repro; this run guards the start. +func r5b(c *runCtx, fuse float64) (map[string]any, error) { + err := with(c, stackSpec{fuseA: fuse, loadsW: houseA(3), socPct: 50}, func(st *stack) error { + if err := st.plug(0); err != nil { + return err + } + if _, err := st.planTo("garage", 40, 60, 8*time.Hour); err != nil { + return err + } + st.mark("plan") + sleepS(120) + st.mark("ftw_stop") + st.stopFTW(syscall.SIGTERM) + sleepS(1200) + st.mark("ftw_start") + if err := st.startFTW(); err != nil { + return err + } + st.mark("ftw_up") + f, err := os.Create(filepath.Join(st.rundir, "plan.jsonl")) + if err != nil { + return err + } + defer f.Close() + for end := time.Now().Add(60 * time.Second); time.Now().Before(end); sleepS(1) { + var p struct { + Plan *struct { + GeneratedAtMs int64 `json:"generated_at_ms"` + } `json:"plan"` + } + if st.get("/api/mpc/plan", &p) == nil && p.Plan != nil { + b, _ := json.Marshal(map[string]any{"t": now(), "generated_at_ms": p.Plan.GeneratedAtMs}) + f.Write(append(b, '\n')) + } + } + sleepS(60) + st.mark("end") + return nil + }) + return map[string]any{"scenario": "r5b", "fuse": fuse, "house_a": 3, "down_s": 1200}, err +} + +// r5c holds Charge now on one car, stops Core for 10 min, in which that car +// leaves and another plugs in, and starts Core into the new session. +func r5c(c *runCtx, fuse float64) (map[string]any, error) { + err := with(c, stackSpec{fuseA: fuse, loadsW: houseA(3), socPct: 50}, func(st *stack) error { + if err := st.plug(0); err != nil { + return err + } + st.mark("charge_now") + if err := st.chargeNow("garage", 16, "3p", 0); err != nil { + return err + } + sleepS(60) + st.mark("ftw_stop") + st.stopFTW(syscall.SIGTERM) + sleepS(300) + off, on := false, true + st.mark("unplug") + if err := st.event(houseEvent{Do: "plug", Device: "garage", On: &off}); err != nil { + return err + } + sleepS(30) + st.mark("replug") + if err := st.event(houseEvent{Do: "plug", Device: "garage", On: &on}); err != nil { + return err + } + sleepS(270) + st.mark("ftw_start") + if err := st.startFTW(); err != nil { + return err + } + st.mark("ftw_up") + sleepS(180) + st.mark("end") + return nil + }) + return map[string]any{"scenario": "r5c", "fuse": fuse, "house_a": 3, "down_s": 600}, err +} diff --git a/go/test/site/scenarios_p0_unit_test.go b/go/test/site/scenarios_p0_unit_test.go new file mode 100644 index 000000000..dfdb6624c --- /dev/null +++ b/go/test/site/scenarios_p0_unit_test.go @@ -0,0 +1,449 @@ +//go:build unix + +package site + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" +) + +// Unit tests for the R1-R5 judges, on records made up here. They run in +// every go test, without a simulator. + +// synth is n one-second samples from t = 0: 10 A per phase, the car idle, +// the battery not forced. set changes sample i. +func synth(n int, marks map[string]float64, meta map[string]any, set func(i int, r *sample)) *runData { + d := &runData{marks: marks, meta: meta} + for i := 0; i < n; i++ { + r := &sample{T: float64(i), PhaseA: [3]float64{10, 10, 10}, CarW: []float64{0}} + if set != nil { + set(i, r) + } + d.samples = append(d.samples, r) + } + return d +} + +// order sets the battery's EMS registers to Core's order w (+ charge). +func order(r *sample, w float64) { + switch { + case w > 0: + r.Bat.EMSMode, r.Bat.EMSCmd, r.Bat.EMSSetW = 2, 170, w + case w < 0: + r.Bat.EMSMode, r.Bat.EMSCmd, r.Bat.EMSSetW = 2, 187, -w + default: + r.Bat.EMSMode, r.Bat.EMSCmd, r.Bat.EMSSetW = 0, 204, 0 + } +} + +// profileAt is a SetChargingProfile of limitA per phase on connector 1, +// sent at t. +func profileAt(t, limitA float64) frame { + b, _ := json.Marshal([]any{2, "id", "SetChargingProfile", map[string]any{"connectorId": 1, + "csChargingProfiles": map[string]any{"chargingProfilePurpose": "TxDefaultProfile", + "chargingSchedule": map[string]any{"chargingRateUnit": "A", + "chargingSchedulePeriod": []any{map[string]any{"limit": limitA}}}}}}) + return frame{T: t, Dir: "cs->cp", Frame: b} +} + +func verdictsOf(cs []criterion) map[string]bool { + out := map[string]bool{} + for _, c := range cs { + out[c.Name] = c.OK + } + return out +} + +func TestBatOrderReadsTheForcedCommand(t *testing.T) { + for _, w := range []float64{3000, -2500, 0} { + r := &sample{} + order(r, w) + if got := batOrder(r); got != w { + t.Errorf("order %v read as %v", w, got) + } + } + r := &sample{Bat: plantDevice{EMSMode: 0, EMSCmd: 187, EMSSetW: 3000}} + if batOrder(r) != 0 || batForced(r) { + t.Fatal("a discharge command outside forced mode is no order: the inverter runs on its own") + } +} + +func TestOfferRisesCountsOnlyRaisesInTheWindow(t *testing.T) { + d := &runData{frames: []frame{profileAt(10, 8), profileAt(20, 10), profileAt(30, 6), profileAt(40, 7), profileAt(60, 16)}} + if n := d.offerRises(15, 50); n != 2 { + t.Fatalf("rises in [15, 50): %d, want 2 (8→10 A at 20 s, 6→7 A at 40 s)", n) + } + if p := d.firstOffer(25, func(p profile) bool { return p.Limit < 7 }); p == nil || *p != 5 { + t.Fatalf("first offer under 7 A from 25 s: %v, want 5 s", p) + } +} + +// r1Run is R1's record: the meter silent from 100 to 220 s and offline from +// 340 to 430 s. Core discharges 3 kW until the silence and hands the +// battery back and stands the charger down 40 s into it; the meter reads +// degraded while silent; Charge now returns 30 s after each return. orderAt +// is Core's battery order during the silence before the hand-back. +func r1Run(orderAt func(t float64) float64) *runData { + marks := map[string]float64{"charge_now": 0, "silent": 100, "back": 220, "offline": 340, "online": 430, "end": 550} + d := synth(550, marks, map[string]any{"fuse": 25.0}, func(i int, r *sample) { + t := float64(i) + switch { + case t <= 100: + order(r, -3000) + case t < 140: + order(r, orderAt(t)) + } + meter := "ok" + if (t > 102 && t < 220) || (t > 342 && t < 430) { + meter = "degraded" + } + r.Status = &ftwStatus{Drivers: map[string]ftwDriver{"meter": {Status: meter}}} + if t < 140 || (t >= 250 && t < 380) || t >= 460 { + r.CarW[0] = 11040 + } + }) + d.frames = []frame{profileAt(50, 16), profileAt(140, 0), profileAt(240, 16), profileAt(390, 0), profileAt(450, 16)} + return d +} + +func TestJudgeR1(t *testing.T) { + _, cs := judgeR1(r1Run(func(t float64) float64 { return -3000 + 50*(t-100) })) + for name, ok := range verdictsOf(cs) { + if !ok { + t.Errorf("an order easing toward 0: %q fails", name) + } + } + _, cs = judgeR1(r1Run(func(t float64) float64 { return -3000 - 100*(t-100) })) + if v := verdictsOf(cs); v["the battery order moves only toward 0 while the meter is silent"] { + t.Error("an order winding from -3 kW toward -7 kW passes") + } + _, cs = judgeR1(r1Run(func(float64) float64 { return 1000 })) + if v := verdictsOf(cs); v["the battery order moves only toward 0 while the meter is silent"] { + t.Error("an order flipping from discharge to charge passes") + } + late := r1Run(func(float64) float64 { return -3000 }) + late.frames[1] = profileAt(170, 0) + _, cs = judgeR1(late) + if v := verdictsOf(cs); v["a 0 W charger order within 64 s of the silence"] { + t.Error("a standdown 70 s into the silence passes") + } +} + +func TestJudgeR2(t *testing.T) { + marks := map[string]float64{"charge_now": 0, "offline": 100, "online": 280, "end": 400} + run := func(frames []frame) map[string]bool { + d := synth(400, marks, map[string]any{"fuse": 25.0, "charger_delay_s": 0.0}, func(i int, r *sample) { + r.BatW = -3000 + q := "measured" + if i >= 100 && i < 280 { + q = "estimated" + } + w := 5000.0 + r.Status = &ftwStatus{LoadQuality: &q, LoadW: &w} + }) + d.frames = frames + _, cs := judgeR2(d) + return verdictsOf(cs) + } + if v := run([]frame{profileAt(50, 8), profileAt(120, 6)}); !v["no EV offer rises while the battery is offline"] || + !v["Home use shown as estimated throughout the outage"] || !v["the battery discharged 2 kW or more before the drop"] { + t.Fatalf("a cut while offline: %v", v) + } + if v := run([]frame{profileAt(50, 8), profileAt(120, 10)}); v["no EV offer rises while the battery is offline"] { + t.Fatal("a raise from 8 to 10 A while the battery is offline passes") + } +} + +func TestJudgeR3(t *testing.T) { + marks := map[string]float64{"plug": 0, "charge_now": 20, "end": 400} + run := func(set func(i int, r *sample)) map[string]bool { + _, cs := judgeR3(synth(400, marks, map[string]any{"fuse": 25.0, "charger_delay_s": 60.0, "planner_floor_pct": 10.0}, set)) + return verdictsOf(cs) + } + // SoC falls from 12 % to 8 % over the first 100 s. + soc := func(i int) float64 { return 12 - 4*float64(min(i, 100))/100 } + v := run(func(i int, r *sample) { r.Bat.SoCPct = soc(i); order(r, -3000) }) + if v["no discharge ordered below the planner floor"] || !v["the battery reached the planner floor"] { + t.Fatalf("discharge down to 8 %%: %v", v) + } + v = run(func(i int, r *sample) { + r.Bat.SoCPct = soc(i) + if r.Bat.SoCPct > 10 { + order(r, -3000) + } + }) + if !v["no discharge ordered below the planner floor"] || !v["the battery order swings at most 100 W over the last 5 min"] { + t.Fatalf("stopping at the floor: %v", v) + } + v = run(func(i int, r *sample) { r.Bat.SoCPct = 10; order(r, -3000*float64(i%2)) }) + if v["the battery order swings at most 100 W over the last 5 min"] { + t.Fatal("an order hunting between 0 and -3 kW passes") + } +} + +func TestJudgeR4SessionEnergy(t *testing.T) { + marks := map[string]float64{"charge_now": 0, "kill1": 100, "up1": 130, "kill2": 250, "up2": 400, "end": 600} + run := func(afterWh float64) map[string]bool { + d := synth(600, marks, map[string]any{"fuse": 25.0, "charger": "CP1"}, func(i int, r *sample) { + wh := 2000.0 + if (i >= 130 && i < 250) || i >= 400 { + wh = afterWh + } + r.Status = &ftwStatus{Drivers: map[string]ftwDriver{"CP1": {Status: "ok", EVSessionWh: &wh}}} + r.CarW[0] = 11040 + }) + _, cs := judgeR4(d) + return verdictsOf(cs) + } + if v := run(0); v["session energy counts again after each restart"] { + t.Fatal("session energy stuck at 0 after the restarts passes") + } + if v := run(150); !v["session energy counts again after each restart"] || !v["Charge now back at its minimum within 60 s of each restart, and held"] { + t.Fatalf("session energy counting after the restarts: %v", v) + } +} + +func TestJudgeR5bAllowsChargingOnlyOnAFreshPlan(t *testing.T) { + dir := t.TempDir() + marks := map[string]float64{"ftw_start": 100, "ftw_up": 110, "end": 200} + // The rig sees the fresh plan, made at 115 s, at 116 s. + plan := `{"t": 111, "generated_at_ms": 40000}` + "\n" + `{"t": 116, "generated_at_ms": 115000}` + "\n" + if err := os.WriteFile(filepath.Join(dir, "plan.jsonl"), []byte(plan), 0o644); err != nil { + t.Fatal(err) + } + run := func(frames []frame) map[string]bool { + d := synth(200, marks, map[string]any{"fuse": 25.0}, nil) + d.dir, d.frames = dir, frames + _, cs := judgeR5b(d) + return verdictsOf(cs) + } + if v := run([]frame{profileAt(112, 16)}); v["no positive EV order in the first 30 s without a fresh plan"] { + t.Fatal("16 A before the fresh plan passes") + } + if v := run([]frame{profileAt(112, 0), profileAt(120, 16)}); !v["no positive EV order in the first 30 s without a fresh plan"] { + t.Fatal("0 A until the fresh plan, then 16 A, fails") + } +} + +func TestHoldsFromResetsOnALaterStop(t *testing.T) { + d := synth(100, nil, nil, func(i int, r *sample) { + if i < 20 || i >= 50 { + r.CarW[0] = 11040 + } + }) + charging := func(r *sample) bool { return carW(r, 0) >= 0.95*minW } + if p := d.holdsFrom(10, 100, charging); p == nil || *p != 40 { + t.Fatalf("charging at 10 s, stopped 20-49 s, back from 50 s: %v, want 40", p) + } + if p := d.holdsFrom(10, 40, charging); p != nil { + t.Fatalf("stopped at the window's end: %v, want nil", *p) + } +} + +// The car still draws at the restart, then Core stops it 20 s later for +// 50 s. A judge that takes the first charging sample calls it back at once. +func TestJudgeR4SeesAStopAfterTheRestart(t *testing.T) { + marks := map[string]float64{"charge_now": 0, "kill1": 100, "up1": 130, "kill2": 250, "up2": 400, "end": 600} + wh := 150.0 + d := synth(600, marks, map[string]any{"fuse": 25.0, "charger": "CP1"}, func(i int, r *sample) { + r.Status = &ftwStatus{Drivers: map[string]ftwDriver{"CP1": {Status: "ok", EVSessionWh: &wh}}} + if i < 150 || i >= 200 { + r.CarW[0] = 11040 + } + }) + m, cs := judgeR4(d) + if verdictsOf(cs)["Charge now back at its minimum within 60 s of each restart, and held"] { + t.Fatalf("a stop from 20 to 70 s after the restart passes: %v", m["charge_now_back_s"]) + } +} + +// r2Run is R2's record: the battery offline from 100 to 280 s, the site at +// 25.0 A (Core's own line, the fuse) before the drop and after the return, and +// overA per phase for the first overS s of the drop, then 20 A. +func r2Run(overA float64, overS int) *runData { + marks := map[string]float64{"charge_now": 0, "offline": 100, "online": 280, "end": 400} + d := synth(400, marks, map[string]any{"fuse": 25.0, "charger_delay_s": 0.0}, func(i int, r *sample) { + r.BatW = -3000 + q, w := "measured", 5000.0 + if i >= 100 && i < 280 { + q = "estimated" + } + r.Status = &ftwStatus{LoadQuality: &q, LoadW: &w} + switch { + case i >= 100 && i < 100+overS: + r.PhaseA = [3]float64{overA, overA, overA} + case i >= 100 && i < 280: + r.PhaseA = [3]float64{20, 20, 20} + default: + r.PhaseA = [3]float64{25.0, 25.0, 25.0} + } + }) + d.frames = []frame{profileAt(50, 8), profileAt(float64(100+overS-2), 0), profileAt(285, 8)} + return d +} + +func TestJudgeR2Overage(t *testing.T) { + const instant = "over 1.2 x the fuse no longer than the charger's delay + 7 s after the drop" + const heat = "the 60 s heat stays under 1.13 x the fuse" + // A short overage, then Core's own line after the return (the review's + // edge case), and the heat budget spent at 29.95 A for 18 s, as on + // master. + for _, c := range []struct { + a float64 + s int + }{{28, 4}, {29.95, 18}} { + _, cs := judgeR2(r2Run(c.a, c.s)) + if v := verdictsOf(cs); !v[instant] || !v[heat] { + t.Errorf("%.2f A for %d s, then Core's line: %v", c.a, c.s, v) + } + } + _, cs := judgeR2(r2Run(31, 12)) + if verdictsOf(cs)[instant] { + t.Error("31 A, over 1.2 x 25 A, for 12 s passes") + } + _, cs = judgeR2(r2Run(29.95, 180)) + if verdictsOf(cs)[heat] { + t.Error("a car never cut at 29.95 A for the whole outage passes") + } +} + +// r5aRun is R5a's record: Core starts at 60 s and answers at 70 s; the site +// runs preA per phase with the car at 16 A until 80 s, then afterA(i) with +// the car at 10 A. +func r5aRun(preA float64, afterA func(i int) float64) *runData { + marks := map[string]float64{"ftw_start": 60, "ftw_up": 70, "end": 250} + return synth(250, marks, map[string]any{"fuse": 25.0}, func(i int, r *sample) { + w := 3000.0 + r.Status = &ftwStatus{LoadW: &w} + if i < 80 { + r.PhaseA, r.CarW[0] = [3]float64{preA, preA, preA}, 11040 + return + } + a := afterA(i) + r.PhaseA, r.CarW[0] = [3]float64{a, a, a}, 6900 + }) +} + +// atFuse is Core's own line on a 25 A fuse: the rating itself. +func atFuse(int) float64 { return 25.0 } + +func TestJudgeR5a(t *testing.T) { + _, cs := judgeR5a(r5aRun(30, atFuse)) + for name, ok := range verdictsOf(cs) { + if !ok { + t.Errorf("30 A cut to Core's own 25.0 A line 10 s after it answers: %q fails", name) + } + } + _, cs = judgeR5a(r5aRun(22.75, atFuse)) + if verdictsOf(cs)["the site was over the fuse before Core started"] { + t.Error("a site at 22.75 A before Core starts passes the setup: the run proves nothing") + } +} + +// R5a judges at the fuse: exactly 25.0 A on a 25 A fuse passes, and 0.1 A +// over it for longer than the 3 s budget fails. +func TestJudgeR5aJudgesAtTheFuse(t *testing.T) { + const under, over = "under the fuse within 30 s of Core answering", "over the fuse at most 3 s once under it" + judge := func(afterA func(int) float64) map[string]bool { + _, cs := judgeR5a(r5aRun(30, afterA)) + return verdictsOf(cs) + } + if v := judge(atFuse); !v[under] || !v[over] { + t.Errorf("held at exactly 25.0 A: %v", v) + } + // Under the fuse at 80 s, then 25.1 A from 100 to 109 s. + v := judge(func(i int) float64 { + if i >= 100 && i < 110 { + return 25.1 + } + return 25.0 + }) + if v[over] { + t.Error("25.1 A for 9 s once under the fuse passes") + } + // The 3 s clock starts at the first sample at or under the fuse, not + // under fuse - 0.5 A: 24.8 A at 80 s, then 26 A from 85 to 94 s, fails. + v = judge(func(i int) float64 { + switch { + case i < 85: + return 24.8 + case i < 95: + return 26 + } + return 24 + }) + if !v[under] || v[over] { + t.Errorf("a dip to 24.8 A, then 26 A for 10 s: %v", v) + } + if v := judge(func(int) float64 { return 25.1 }); v[under] { + t.Error("held at 25.1 A after Core answers passes") + } +} + +// R3 allows the charger's delay + 7 s (67 s here) over the fuse: exactly +// 25.0 A throughout passes, and 25.1 A for 80 s fails. +func TestJudgeR3JudgesAtTheFuse(t *testing.T) { + const over = "over the fuse no longer than the charger's delay + 7 s" + marks := map[string]float64{"plug": 0, "charge_now": 20, "end": 400} + judge := func(a func(int) float64) bool { + d := synth(400, marks, map[string]any{"fuse": 25.0, "charger_delay_s": 60.0, "planner_floor_pct": 10.0}, func(i int, r *sample) { + v := a(i) + r.PhaseA = [3]float64{v, v, v} + }) + _, cs := judgeR3(d) + return verdictsOf(cs)[over] + } + if !judge(atFuse) { + t.Error("held at exactly 25.0 A fails") + } + if judge(func(i int) float64 { + if i >= 100 && i < 180 { + return 25.1 + } + return 25.0 + }) { + t.Error("25.1 A for 80 s passes") + } +} + +// Between the fuse and 1.2 x the fuse R3 and R5a allow what Core's 60 s +// heat allows (the #1888 review): a cool site may sit at 1.08 x the fuse +// past the budget while the heat stays under the fuse; above 1.2 x the +// budget holds. +func TestJudgeR3AndR5aAllowTheHeatBudget(t *testing.T) { + const r3over = "over the fuse no longer than the charger's delay + 7 s" + const r5over = "over the fuse at most 3 s once under it" + r3 := func(a float64) bool { + d := synth(400, map[string]float64{"plug": 0, "charge_now": 20, "end": 400}, + map[string]any{"fuse": 25.0, "charger_delay_s": 60.0, "planner_floor_pct": 10.0}, func(i int, r *sample) { + if i >= 100 && i < 180 { // 80 s, past the 67 s budget + r.PhaseA = [3]float64{a, a, a} + } + }) + _, cs := judgeR3(d) + return verdictsOf(cs)[r3over] + } + if !r3(27) { + t.Error("R3: 27 A for 80 s from a cool fuse fails; its heat stays under 25 A") + } + if r3(30.5) { + t.Error("R3: 30.5 A, over 1.2 x the fuse, for 80 s passes") + } + r5 := func(a float64) bool { + _, cs := judgeR5a(r5aRun(30, func(i int) float64 { + if i >= 200 && i < 215 { // 15 s, past the 3 s budget + return a + } + return 20 + })) + return verdictsOf(cs)[r5over] + } + if !r5(27) { + t.Error("R5a: 27 A for 15 s after 2 min at 20 A fails; its heat stays under 25 A") + } + if r5(30.5) { + t.Error("R5a: 30.5 A, over 1.2 x the fuse, for 15 s passes") + } +} diff --git a/go/test/site/scenarios_test.go b/go/test/site/scenarios_test.go index e2f396c08..2388ab65b 100644 --- a/go/test/site/scenarios_test.go +++ b/go/test/site/scenarios_test.go @@ -9,6 +9,7 @@ import ( "os" "path/filepath" "strings" + "syscall" "time" ) @@ -27,6 +28,9 @@ type scenario struct { // tolerate names criteria that may fail for a known finding but need // not: random cases, where a seed may or may not reach it. tolerate map[string]string + // skip, when set, says what the pinned device-simulator lacks for this + // scenario: TestSite reports it skipped instead of running it. + skip string } var scenarios = []scenario{ @@ -411,7 +415,7 @@ func s7(c *runCtx, fuse float64, short bool) (map[string]any, error) { sleepS(900) } st.mark("ftw_stop") - st.stopFTW() + st.stopFTW(syscall.SIGTERM) sleepS(20) st.mark("ftw_start") if err := st.startFTW(); err != nil { @@ -707,10 +711,10 @@ func judgeS12(d *runData) (metrics, []criterion) { if p.Limit > 0 && p.phasesOr3() == 1 { ones++ } - // A lower order while every phase has room is a cut on room alone, - // unless the plan itself asked for less (a pause between slots). + // A lower order while every phase is under the fuse is a cut on room + // alone, unless the plan itself asked for less (a pause between slots). if i > 0 && p.offerW() < prs[i-1].offerW()-1 { - if r := d.at(t0 + p.T); r != nil && maxAbsI(r) < fuse-1-0.5 { + if r := d.at(t0 + p.T); r != nil && maxAbsI(r) < fuse-0.1 { if after := d.at(t0 + p.T + 2); after == nil || firstLP(after).CommandedReason != "plan" { cuts++ } @@ -721,7 +725,7 @@ func judgeS12(d *runData) (metrics, []criterion) { // recorded, not judged, since a slow charger's pending order may hold it. limited := 0 for _, r := range w { - if r.LPs != nil && firstLP(r).CommandedReason == "fuse_limit" && maxAbsI(r) < fuse-1-6 { + if r.LPs != nil && firstLP(r).CommandedReason == "fuse_limit" && maxAbsI(r) < fuse-6 { limited++ } } diff --git a/go/test/site/stack_test.go b/go/test/site/stack_test.go index 4add269ee..291baaf07 100644 --- a/go/test/site/stack_test.go +++ b/go/test/site/stack_test.go @@ -208,6 +208,7 @@ type houseDevice struct { CarMaxA float64 `json:"car_max_a,omitempty"` CarPhases int `json:"car_phases,omitempty"` WiredPhases int `json:"wired_phases,omitempty"` + Plugged bool `json:"plugged,omitempty"` } // houseEvent is one change to the house (device-simulator's runEvent). @@ -247,6 +248,7 @@ type charger struct { carPhases int // 0: as many as offered wiredPhases int // 1: wired on L1 only, kw its rating there carMaxA float64 + plugged bool // a car on it when the house starts, charging on its own } type stackSpec struct { @@ -276,6 +278,11 @@ type stackSpec struct { mode string // horizonH is the planner's horizon_hours; 0 leaves Core's 48 h. horizonH int + // plannerSoCMin is the planner's soc_min (a fraction); 0 leaves Core's. + plannerSoCMin float64 + // startAfterS runs the house this long before FTW starts, as a site + // runs before Core boots; FTW's start is marked ftw_start. + startAfterS float64 } type stack struct { @@ -344,7 +351,7 @@ func (st *stack) houseSpec() houseSpec { h.Devices = append(h.Devices, houseDevice{ID: lpIDs[i], Driver: "ocpp", OCPPURL: fmt.Sprintf("ws://ftw:%s@127.0.0.1:%d/", ocppPass, st.lane.ftwOCPP()), MaxKW: kw, Vendor: "RigCo", Model: fmt.Sprintf("Rig%.0f", kw), PhaseSwitch: c.phaseSwitch, CommandResponseS: delay, - CarPhases: c.carPhases, CarMaxA: c.carMaxA, WiredPhases: c.wiredPhases, + CarPhases: c.carPhases, CarMaxA: c.carMaxA, WiredPhases: c.wiredPhases, Plugged: c.plugged, OCPPConfig: map[string]string{"HeartbeatInterval": "60", "MeterValueSampleInterval": "10"}}) } return h @@ -393,6 +400,10 @@ func (st *stack) up() error { return err } } + if st.spec.startAfterS > 0 { + sleepS(st.spec.startAfterS) + st.mark("ftw_start") + } if err := st.startFTW(); err != nil { return err } @@ -413,7 +424,9 @@ func (st *stack) startFTW() error { return waitHTTP(st.ftw+"/api/status", 90*time.Second) } -func (st *stack) stopFTW() { st.ps.stop("ftw", syscall.SIGTERM, 30*time.Second) } +// stopFTW stops FTW with sig: SIGTERM lets it clean up, SIGKILL is a power +// cut. +func (st *stack) stopFTW(sig syscall.Signal) { st.ps.stop("ftw", sig, 30*time.Second) } func (st *stack) down() { if st.rec != nil { @@ -569,7 +582,9 @@ func (st *stack) ftwConfig() (string, error) { cfg := map[string]any{ "site": map[string]any{"name": "rig", "control_interval_s": 2, "grid_target_w": 0, "grid_tolerance_w": 42, "watchdog_timeout_s": 60}, - "fuse": map[string]any{"max_amps": st.spec.fuseA, "phases": 3, "voltage": 230}, + // safety_margin_a pinned at 0 so the rig judges Core at the rating + // whatever the default. + "fuse": map[string]any{"max_amps": st.spec.fuseA, "phases": 3, "voltage": 230, "safety_margin_a": 0}, "drivers": []map[string]any{ {"name": "meter", "lua": "drivers/sdm630.lua", "is_site_meter": true, "capabilities": modbus("meter")}, {"name": "sungrow", "lua": "drivers/sungrow.lua", "battery_capacity_wh": 10000, "capabilities": modbus("inverter")}, @@ -591,6 +606,9 @@ func (st *stack) ftwConfig() (string, error) { if st.spec.horizonH > 0 { cfg["planner"].(map[string]any)["horizon_hours"] = st.spec.horizonH } + if st.spec.plannerSoCMin > 0 { + cfg["planner"].(map[string]any)["soc_min"] = st.spec.plannerSoCMin + } if st.spec.soc2Pct > 0 { cfg["drivers"] = append(cfg["drivers"].([]map[string]any), map[string]any{"name": "sungrow2", "lua": "drivers/sungrow.lua", "battery_capacity_wh": 10000, "capabilities": modbus("inverter2")}) diff --git a/go/test/site/verdicts_test.go b/go/test/site/verdicts_test.go index cfc2c678f..64d3fbb99 100644 --- a/go/test/site/verdicts_test.go +++ b/go/test/site/verdicts_test.go @@ -3,7 +3,9 @@ package site import ( + "encoding/json" "math" + "path/filepath" "sort" ) @@ -43,8 +45,11 @@ func judgeS1(d *runData) (metrics, []criterion) { fuse := d.metaF("fuse") target := maxW if fuse < 25 { - // Room on a 16 A fuse: fuse - 1 A margin - house, in 1 A steps. - target = math.Min(maxW, math.Floor(fuse-1-d.metaF("house_a"))*volt*3) + // Room on a 16 A fuse: fuse - house, in 1 A steps. Core works to + // the rating; safety_margin_a is a site's opt-in and the rig sets 0. + // The target is a floor, not the line: the battery (SoC 50) covers + // the house, so a Core that stops at fuse - 1 A also passes. + target = math.Min(maxW, math.Floor(fuse-d.metaF("house_a"))*volt*3) } m := d.carSummary(t0, t1, target) m["target_w"] = target @@ -546,3 +551,284 @@ func judgeS11(d *runData) (metrics, []criterion) { crit("fuse holds", !run["fuse_blown"].(bool)), } } + +// ---- R1-R5, the stable plan's P0 scenarios ---- + +// fuseInstantFactor is Core's: above this multiple of the fuse it cuts the +// car at once, with no heat budget (control/fuse_heat.go). mcbHourFactor is +// the current a miniature circuit breaker carries for an hour without a +// trip, the lower of the two ratings Core's heat model keeps under. +const fuseInstantFactor, mcbHourFactor = 1.2, 1.13 + +// judgeR1: while the meter is silent Core raises nothing and its battery +// order only moves toward 0; within the 60 s watchdog and two ticks it +// stands the charger down and hands the battery back; it never shows the +// meter as live; Charge now returns after the meter does and runs until the +// next event. +func judgeR1(d *runData) (metrics, []criterion) { + tCN, tS, tB, t1 := d.marks["charge_now"], d.marks["silent"], d.marks["back"], d.marks["end"] + tOff, tOn := d.marks["offline"], d.marks["online"] + var o0 float64 + if r := d.at(tS); r != nil { + o0 = batOrder(r) + } + wound := 0 + for _, r := range d.window(tS+2, tB) { + if o := batOrder(r); math.Abs(o) > math.Abs(o0)+50 || o*o0 < 0 { + wound++ + } + } + live := 0 + for _, w := range [][2]float64{{tS + 10, tB}, {tOff + 10, tOn}} { + for _, r := range d.window(w[0], w[1]) { + if dr, ok := status(r).Drivers["meter"]; ok && dr.Status == "ok" { + live++ + } + } + } + charging := func(r *sample) bool { return carW(r, 0) >= 0.95*minW } + standdown := d.firstOffer(tS, func(p profile) bool { return p.Limit == 0 }) + handBack := d.firstT(tS, func(r *sample) bool { return !batForced(r) }) + back1, back2 := d.holdsFrom(tB, tOff, charging), d.holdsFrom(tOn, t1, charging) + rises := d.offerRises(tS, tB) + m := metrics{"battery_order_at_silence_w": math.Round(o0), "battery_wound_samples": wound, + "offer_rises_while_silent": rises, "standdown_s": roundp(standdown, 1), "battery_hand_back_s": roundp(handBack, 1), + "meter_live_while_stale_samples": live, "charge_now_back_s": []*float64{roundp(back1, 1), roundp(back2, 1)}} + return m, []criterion{ + crit("the battery relieved the fuse when the meter fell silent", o0 <= -500), // the setup + crit("no EV offer rises while the meter is silent", rises == 0), + crit("the battery order moves only toward 0 while the meter is silent", wound == 0), + crit("a 0 W charger order within 64 s of the silence", le(standdown, 64)), + crit("the battery back on its own within 64 s of the silence", le(handBack, 64)), + crit("the meter never shows live while it is silent or offline", live == 0), + crit("Charge now back at its minimum within 95 s of each return, and held", le(back1, 95) && le(back2, 95)), + crit("fuse holds", !anyBlown(d.window(tCN, t1))), + } +} + +// judgeR2: when the battery that carried the car's room drops out, the fuse +// holds, no offer counts the battery's last -3 kW as room, and Home use says +// it is estimated (the stable plan's R2). Under 1.2 x the fuse Core spends a +// 60 s heat budget before it cuts the car (control/fuse_heat.go), so the +// overage is judged by that contract: above 1.2 x it lasts no longer than +// the charger takes to answer, and the 60 s heat stays under the 1.13 x a +// miniature circuit breaker carries for an hour. How long the site stays +// over the fuse and when Core sends the cut are recorded. +func judgeR2(d *runData) (metrics, []criterion) { + fuse, delay := d.metaF("fuse"), d.metaF("charger_delay_s") + tCN, tOff, tOn, t1 := d.marks["charge_now"], d.marks["offline"], d.marks["online"], d.marks["end"] + pre := meanOf(d.window(tOff-30, tOff), batSigned) + allowed := delay + 7 + instant, _ := d.runsOver(tOff, tOn+60, fuseInstantFactor*fuse) + overFuse, _ := d.runsOver(tOff, tOn+60, fuse) + heatOver, heatPeak := d.i2Over(tOff-60, tOn+60, mcbHourFactor*fuse) + during := summarize(d.window(tOff+15, tOn)) + marked := 0 + for k, v := range during.LoadQuality { + if k != "null" && k != "" && k != "measured" { + marked += v + } + } + rises := d.offerRises(tOff, tOn) + cut := d.firstOffer(tOff, func(p profile) bool { return p.Limit == 0 }) + m := metrics{"battery_w_before": math.Round(pre), "allowed_over_s": allowed, + "over_fuse_x1_2_longest_s": instant, "over_fuse_longest_s": overFuse, + "heat_peak_a": heatPeak, "heat_over_x1_13_s": heatOver, "cut_sent_s": roundp(cut, 1), + "offer_rises_while_offline": rises, "during": during, + "max_i_after_drop": round(maxOf(d.window(tOff, tOn+60), maxAbsI), 2)} + return m, []criterion{ + crit("the battery discharged 2 kW or more before the drop", pre <= -2000), // the setup + crit("fuse holds", !anyBlown(d.window(tCN, t1))), + crit("over 1.2 x the fuse no longer than the charger's delay + 7 s after the drop", instant <= allowed), + crit("the 60 s heat stays under 1.13 x the fuse", heatOver == 0), + crit("no EV offer rises while the battery is offline", rises == 0), + crit("Home use shown as estimated throughout the outage", + during.Samples > 0 && during.LoadWShown == during.Samples && marked == during.Samples), + } +} + +// judgeR3: Core orders no discharge below the planner's floor, its battery +// order settles, and the car answers when the battery stops. +func judgeR3(d *runData) (metrics, []criterion) { + fuse, delay, floor := d.metaF("fuse"), d.metaF("charger_delay_s"), d.metaF("planner_floor_pct") + t0, tCN, t1 := d.marks["plug"], d.marks["charge_now"], d.marks["end"] + run := d.window(t0, t1) + below := 0 + for _, r := range run { + if batOrder(r) < -50 && r.Bat.SoCPct < floor-0.5 { + below++ + } + } + last := d.window(t1-300, t1) + swing := 0.0 + if len(last) > 0 { + lo, hi := math.Inf(1), math.Inf(-1) + for _, r := range last { + lo, hi = math.Min(lo, batOrder(r)), math.Max(hi, batOrder(r)) + } + swing = hi - lo + } + minSoC := minOf(run, func(r *sample) float64 { return r.Bat.SoCPct }) + // Core may work up to the fuse rating; safety_margin_a is a site's + // opt-in, so the overage that counts is above the fuse. Between the + // fuse and 1.2 x the fuse Core's 60 s heat is the budget (heatOver). + overInstant, _ := d.runsOver(tCN, t1, fuse) + over := d.heatRunsOver(tCN, t1, fuse) + allowed := delay + 7 + m := metrics{"discharge_below_floor_samples": below, "battery_order_swing_last5min_w": math.Round(swing), + "soc_min_pct": round(minSoC, 2), "over_fuse_longest_s": overInstant, "over_fuse_heat_longest_s": over, + "allowed_over_s": allowed, "max_i": round(maxOf(run, maxAbsI), 2)} + return m, []criterion{ + crit("the battery reached the planner floor", len(run) > 0 && minSoC <= floor+0.5), // the setup + crit("no discharge ordered below the planner floor", below <= 10), + crit("the battery order swings at most 100 W over the last 5 min", len(last) > 0 && swing <= 100), + crit("over the fuse no longer than the charger's delay + 7 s", over <= allowed), + crit("fuse holds", !anyBlown(run)), + } +} + +// judgeR4: after each hard kill Charge now is back within a minute and runs +// until the next kill or the end (a car still drawing from before the kill +// and stopped later is not back), the session energy counts again and +// state.db opens clean; the day's Home use +// survives the first kill. The second outage's 120 s would break the 10 % +// unknown-time rule on a run this short, so its day total is recorded only. +func judgeR4(d *runData) (metrics, []criterion) { + fuse := d.metaF("fuse") + tCN, k1, u1, k2, u2, t1 := d.marks["charge_now"], d.marks["kill1"], d.marks["up1"], d.marks["kill2"], d.marks["up2"], d.marks["end"] + charging := func(r *sample) bool { return carW(r, 0) >= 0.95*minW } + back1, back2 := d.holdsFrom(u1, k2, charging), d.holdsFrom(u2, t1, charging) + before, after := todayLoadWh(d.window(k1-30, k1)), todayLoadWh(d.window(u1, k2)) + nullAfter := 0 + for _, r := range d.window(u1, k2) { + if r.Status != nil && r.Status.Energy.Today.LoadWh == nil { + nullAfter++ + } + } + kept := before != nil && after != nil && nullAfter == 0 && *after >= 0.99**before + charger, _ := d.meta["charger"].(string) + session := func(t0, t1 float64) float64 { + v := 0.0 + for _, r := range d.window(t0, t1) { + if dr, ok := status(r).Drivers[charger]; ok && dr.EVSessionWh != nil { + v = math.Max(v, *dr.EVSessionWh) + } + } + return v + } + recovery := d.logLines("database corrupt", "INTEGRITY CHECK FAILED") + overAfter, _ := d.runsOver(u2, t1, fuse) + m := metrics{"charge_now_back_s": []*float64{roundp(back1, 1), roundp(back2, 1)}, + "today_load_wh_before_kill1": before, "today_load_wh_before_kill2": after, "today_load_wh_null_after_kill1": nullAfter, + "today_load_wh_end": todayLoadWh(d.window(u2, t1)), + "session_wh_before_kills": []float64{session(k1-30, k1), session(k2-30, k2)}, + "session_wh_90s_after_ups": []float64{session(u1, u1+90), session(u2, u2+90)}, + "recovery_log_lines": len(recovery), "over_fuse_longest_after_up2_s": overAfter} + return m, []criterion{ + crit("Charge now back at its minimum within 60 s of each restart, and held", le(back1, 60) && le(back2, 60)), + crit("the day's Home use kept within 1 % across the first kill", kept), + crit("session energy counts again after each restart", session(u1, u1+90) > 0 && session(u2, u2+90) > 0), + crit("state.db opens with no recovery error", len(recovery) == 0), + crit("fuse holds", !anyBlown(d.window(tCN, t1))), + } +} + +// homeUseBack is the seconds from t0 to the first sample with Home use shown. +func (d *runData) homeUseBack(t0 float64) *float64 { + return d.firstT(t0, func(r *sample) bool { return status(r).LoadW != nil }) +} + +// judgeR5a: Core starting beside a car that already draws its maximum, with +// the site over the fuse, brings the site under the limit within 30 s and +// keeps it there. The limit is the fuse: Core may work up to the rating, +// and safety_margin_a is a site's opt-in. Once under it, a reading between +// the fuse and 1.2 x the fuse counts only while the 60 s heat is over the +// fuse too (heatOver), as Core's cut does. +func judgeR5a(d *runData) (metrics, []criterion) { + fuse := d.metaF("fuse") + tS, tUp, t1 := d.marks["ftw_start"], d.marks["ftw_up"], d.marks["end"] + pre := meanOf(d.window(tS-20, tS), func(r *sample) float64 { return carW(r, 0) }) + preI := maxOf(d.window(tS-20, tS), maxAbsI) + fit := d.firstT(tUp, func(r *sample) bool { return maxAbsI(r) <= fuse }) + overAfter := 0.0 + if fit != nil { + overAfter = d.heatRunsOver(tUp+*fit, t1, fuse) + } + shown := d.homeUseBack(tUp) + m := metrics{"car_w_before_start": math.Round(pre), "under_limit_s": roundp(fit, 1), "over_after_that_longest_s": overAfter, + "home_use_shown_s": roundp(shown, 1), "max_i_before_start": round(preI, 2)} + return m, []criterion{ + crit("the car charged before Core started", pre >= minW), // the setup + crit("the site was over the fuse before Core started", preI > fuse), // the setup + crit("under the fuse within 30 s of Core answering", le(fit, 30)), + crit("over the fuse at most 3 s once under it", fit != nil && overAfter <= 3), + crit("Home use shown within 30 s of Core answering", le(shown, 30)), + crit("fuse holds", !anyBlown(d.window(tS, t1))), + } +} + +// judgeR5b: a Core that starts on a stale plan orders no charge until a +// fresh plan asks for it, at least for its first 30 s. +func judgeR5b(d *runData) (metrics, []criterion) { + tS, tUp, t1 := d.marks["ftw_start"], d.marks["ftw_up"], d.marks["end"] + fresh, polls := math.Inf(1), 0 + readLines(filepath.Join(d.dir, "plan.jsonl"), func(b []byte) { + var l struct { + T float64 `json:"t"` + GeneratedAtMs int64 `json:"generated_at_ms"` + } + if json.Unmarshal(b, &l) != nil { + return + } + polls++ + if gen := float64(l.GeneratedAtMs) / 1000; gen >= tS { + fresh = math.Min(fresh, gen) + } + }) + early := 0 + for _, p := range d.offers() { + if p.T >= tS && p.T < tUp+30 && p.T < fresh && p.Limit > 0 { + early++ + } + } + shown := d.homeUseBack(tUp) + var freshS *float64 + if !math.IsInf(fresh, 1) { + v := round(fresh-tUp, 1) + freshS = &v + } + m := metrics{"plan_polls": polls, "fresh_plan_s": freshS, "positive_orders_before_fresh_plan": early, + "home_use_shown_s": roundp(shown, 1)} + return m, []criterion{ + crit("no positive EV order in the first 30 s without a fresh plan", early == 0), + crit("Home use shown within 30 s of Core answering", le(shown, 30)), + crit("fuse holds", !anyBlown(d.window(tS, t1))), + } +} + +// judgeR5c: Core starting into a session that began while it was away sees +// the car and shows Home use. Whether the new session keeps the old car's +// Charge now waits for the owner (ev-088): recorded, not judged. +func judgeR5c(d *runData) (metrics, []criterion) { + tS, tUp, t1 := d.marks["ftw_start"], d.marks["ftw_up"], d.marks["end"] + held, seen := 0, 0 + for _, r := range d.window(tUp+30, t1) { + if r.LPs == nil { + continue + } + seen++ + if firstLP(r).ManualActive { + held++ + } + } + plugged := d.firstT(tUp, func(r *sample) bool { return r.LPs != nil && firstLP(r).Plugged }) + shown := d.homeUseBack(tUp) + m := metrics{"charge_now_kept_samples": held, "samples": seen, "plugged_s": roundp(plugged, 1), + "home_use_shown_s": roundp(shown, 1), + "car_w_mean": math.Round(meanOf(d.window(tUp+30, t1), func(r *sample) float64 { return carW(r, 0) }))} + return m, []criterion{ + crit("Core sees the car plugged in within 60 s of answering", le(plugged, 60)), + crit("Home use shown within 30 s of Core answering", le(shown, 30)), + crit("fuse holds", !anyBlown(d.window(tS, t1))), + } +}