fix: hide cursor natively, keep OSD on top, pin codec extension contract (#3)
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package osd
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.piksar.eu/piotr/groundstation/internal/protocol"
|
||||
)
|
||||
|
||||
func TestTableSelectionRequiresAnIdentifier(t *testing.T) {
|
||||
@@ -129,3 +134,142 @@ func TestVTXLifecycleStatesAreDistinct(t *testing.T) {
|
||||
t.Fatal("pending, applied and error must be distinct lifecycle states")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSnapshotDomainsAreEncodedInDisjointByteRanges(t *testing.T) {
|
||||
// The four contract domains — operator configuration (table identity
|
||||
// and ceiling), the requested policy level, the applied acknowledgment
|
||||
// level and the observed link — must occupy disjoint byte ranges: a
|
||||
// change to one domain may never alias another domain's bytes, so
|
||||
// future extensions land behind a version bump instead of colliding.
|
||||
base := Snapshot{
|
||||
Sequence: 1,
|
||||
TimestampMS: 2,
|
||||
Channels: [protocol.Channels]int16{},
|
||||
Armed: false,
|
||||
Video: false,
|
||||
OpConfig: OperatorConfig{
|
||||
Table: Table{Gen: 1, Hash: 1},
|
||||
MaxPower: 1,
|
||||
},
|
||||
Policy: GSPolicy{Level: 1},
|
||||
Ack: VTXAck{
|
||||
Table: Table{Gen: 1},
|
||||
Level: 1,
|
||||
Lifecycle: VTXLifecycleApplied,
|
||||
},
|
||||
Link: GSObservedLink{Known: false},
|
||||
}
|
||||
|
||||
deltas := map[string]struct {
|
||||
rangeStart, rangeEnd int
|
||||
mutate func(*Snapshot)
|
||||
}{
|
||||
"operator-config": {opMaxPowerOffset, policyLevelOffset, func(s *Snapshot) { s.OpConfig.MaxPower = 2 }},
|
||||
"policy-level": {policyLevelOffset, ackTableGenOffset, func(s *Snapshot) { s.Policy.Level = 2 }},
|
||||
"ack-level": {ackTableGenOffset, linkAgeOffset, func(s *Snapshot) { s.Ack.Level = 2 }},
|
||||
"observed-link": {linkAgeOffset, crcOffset, func(s *Snapshot) { s.Link.AgeMS = 2 }},
|
||||
}
|
||||
reference := Encode(base)
|
||||
for name, delta := range deltas {
|
||||
modified := base
|
||||
delta.mutate(&modified)
|
||||
encoded := Encode(modified)
|
||||
if len(encoded) != len(reference) {
|
||||
t.Fatalf("%s: encoded length changed", name)
|
||||
}
|
||||
for i := range encoded {
|
||||
if encoded[i] == reference[i] {
|
||||
continue
|
||||
}
|
||||
// The CRC covers the whole record and necessarily changes with
|
||||
// any domain change; every payload byte must stay inside the
|
||||
// domain's own range.
|
||||
if i >= crcOffset {
|
||||
continue
|
||||
}
|
||||
if i < delta.rangeStart || i >= delta.rangeEnd {
|
||||
t.Fatalf("%s: byte %d changed outside the domain range [%d,%d)",
|
||||
name, i, delta.rangeStart, delta.rangeEnd)
|
||||
}
|
||||
}
|
||||
if _, err := Decode(encoded); err != nil {
|
||||
t.Fatalf("%s: modified snapshot must still decode: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeRejectsCorruptionAtomically(t *testing.T) {
|
||||
// Malformed input must never yield a partially decoded snapshot that
|
||||
// could clear or corrupt one domain while keeping others: any flipped
|
||||
// byte anywhere in the record rejects the whole snapshot, and the
|
||||
// publisher keeps its last valid state until a new valid one arrives.
|
||||
encoded := Encode(Snapshot{
|
||||
OpConfig: OperatorConfig{Table: Table{Gen: 5, Hash: 5}, MaxPower: 5},
|
||||
Policy: GSPolicy{Level: 5},
|
||||
Ack: VTXAck{Level: 5, Lifecycle: VTXLifecycleApplied},
|
||||
Link: GSObservedLink{Known: true, AgeMS: 5},
|
||||
})
|
||||
// Sample corruption positions across the whole record — magic, body
|
||||
// regions and CRC — derived from the actual encoded length, so the
|
||||
// atomicity contract holds across future version bumps and layout
|
||||
// extensions without coupling to today's offsets.
|
||||
positions := []int{
|
||||
0,
|
||||
len(encoded) / 4,
|
||||
len(encoded) / 2,
|
||||
3 * len(encoded) / 4,
|
||||
len(encoded) - 1,
|
||||
}
|
||||
for _, offset := range positions {
|
||||
corrupt := append([]byte(nil), encoded...)
|
||||
corrupt[offset] ^= 0xff
|
||||
got, err := Decode(corrupt)
|
||||
if err == nil {
|
||||
t.Fatalf("flipped byte at %d must reject the record", offset)
|
||||
}
|
||||
if got != (Snapshot{}) {
|
||||
t.Fatalf("flipped byte at %d must not emit a partial snapshot", offset)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileCorruptionDoesNotClearLastValidSnapshot(t *testing.T) {
|
||||
// A corrupted state file reports no snapshot for the moment, but the
|
||||
// publisher retains its last valid snapshot and the next heartbeat
|
||||
// rewrite restores exactly those values: corruption never clears
|
||||
// last valid data, and nothing is fabricated in between.
|
||||
path := filepath.Join(t.TempDir(), "osd-state.bin")
|
||||
now := time.UnixMilli(10_000_000)
|
||||
source := NewFileSource(path)
|
||||
source.now = func() time.Time { return now }
|
||||
|
||||
want := testSnapshot()
|
||||
want.PublishedMS = now.UnixMilli()
|
||||
if err := os.WriteFile(path, Encode(want), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, state := source.Latest(); state != SnapshotFresh {
|
||||
t.Fatalf("state=%v want fresh before corruption", state)
|
||||
}
|
||||
|
||||
corrupt := append([]byte(nil), Encode(want)...)
|
||||
corrupt[len(corrupt)/2] ^= 0xff
|
||||
if err := os.WriteFile(path, corrupt, 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, state := source.Latest(); state != SnapshotNone {
|
||||
t.Fatalf("state=%v want none while the file is corrupt", state)
|
||||
}
|
||||
|
||||
// The publisher's heartbeat rewrites its retained last valid snapshot.
|
||||
if err := os.WriteFile(path, Encode(want), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, state := source.Latest()
|
||||
if state != SnapshotFresh {
|
||||
t.Fatalf("state=%v want fresh after the heartbeat restore", state)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("restored snapshot=%+v want the untouched last valid %+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,24 @@
|
||||
// intent only: it constrains what may be requested and is never proof of
|
||||
// applied VTX power, and static configuration is never rendered or serialized
|
||||
// as applied VTX state.
|
||||
//
|
||||
// Extension contract for future domains: the snapshot is an explicit,
|
||||
// extensible foundation of independent domains — operator table identity,
|
||||
// requested level, applied level, observed link, and flight telemetry
|
||||
// (versioned WFB-derived battery/GPS/arm/flight-mode/attitude with source
|
||||
// and freshness, future work). Telemetry domains land as new typed nested
|
||||
// structs with their own explicit pending/unknown states, appended at fixed
|
||||
// offsets before the CRC behind a version bump; the typed fields plus the
|
||||
// versioned all-or-nothing codec are the extension seam, and no runtime
|
||||
// placeholder fields exist before a domain is actually modeled. Domains are
|
||||
// independent by construction: each occupies a disjoint byte range (pinned
|
||||
// by contract tests), decoding is atomic — malformed or damaged records are
|
||||
// rejected whole and can never clear one domain while keeping others — and
|
||||
// the publisher retains its last valid snapshot, republishing it until a
|
||||
// new valid snapshot replaces it. Flight telemetry stays independent of
|
||||
// video and policy; battery, GPS and similar aircraft state must never be
|
||||
// inferred from RC channels, and the only RC-derived flight state remains
|
||||
// the already-defined CH5/AUX1 arm indication.
|
||||
package osd
|
||||
|
||||
import (
|
||||
|
||||
Reference in New Issue
Block a user