Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
172 lines
5.9 KiB
Go
172 lines
5.9 KiB
Go
package seheweit
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"math"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"runtime"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func fixtureSources(t *testing.T) sources {
|
|
t.Helper()
|
|
return sources{
|
|
now: func() time.Time { return time.UnixMilli(123456789) },
|
|
readFile: func(path string) ([]byte, error) {
|
|
switch path {
|
|
case "/proc/loadavg":
|
|
return []byte("0.01 0.20 3.00 1/99 1\n"), nil
|
|
case "/proc/self/io":
|
|
return []byte("rchar: 999\nread_bytes: 10\nwrite_bytes: 20\n"), nil
|
|
default:
|
|
t.Fatalf("unexpected file read: %s", path)
|
|
return nil, nil
|
|
}
|
|
},
|
|
statfs: func(path string, fs *syscall.Statfs_t) error {
|
|
if !filepath.IsAbs(path) {
|
|
t.Fatalf("relative root %q", path)
|
|
}
|
|
fs.Blocks, fs.Bavail, fs.Bsize = 100, 30, 4096
|
|
return nil
|
|
},
|
|
memory: func(m *runtime.MemStats) { m.HeapAlloc, m.Sys, m.NumGC = 1024, 4096, 9 },
|
|
cpus: func() int { return 8 }, goroutines: func() int { return 17 },
|
|
}
|
|
}
|
|
|
|
func TestSnapshotWireCompatibility(t *testing.T) {
|
|
queue := Queue{Stats: QueueStats{Queued: 2, Running: 1}, ActiveWorkers: 1, Paused: true}
|
|
snapshot := collect(t.TempDir(), queue, fixtureSources(t))
|
|
data, err := json.Marshal(snapshot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Exact pre-extraction wire shape, including zeros/false (no omitempty).
|
|
want := map[string]any{
|
|
"timestamp_ms": int64(123456789),
|
|
"io": map[string]any{"scope": "process", "available": true, "process_id": processID, "read_bytes": 10, "write_bytes": 20},
|
|
"cpu": map[string]any{"logical_cpus": 8, "load_average": "0.01 0.20 3.00"},
|
|
"memory": map[string]any{"heap_bytes": 1024, "go_system_bytes": 4096, "gc_cycles": 9, "goroutines": 17},
|
|
"storage": map[string]any{"path": snapshot.Storage.Path, "total_bytes": 409600, "used_bytes": 286720, "available_bytes": 122880},
|
|
"queue": map[string]any{"stats": map[string]int{"queued": 2, "running": 1}, "active_workers": 1, "paused": true},
|
|
}
|
|
wantData, _ := json.Marshal(want)
|
|
var gotJSON, wantJSON any
|
|
if err := json.Unmarshal(data, &gotJSON); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := json.Unmarshal(wantData, &wantJSON); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(gotJSON, wantJSON) {
|
|
t.Fatalf("wire changed: %s != %s", data, wantData)
|
|
}
|
|
zero, _ := json.Marshal(Snapshot{})
|
|
if strings.Contains(string(zero), "null") || !strings.Contains(string(zero), `"paused":false`) {
|
|
t.Fatalf("zero shape %s", zero)
|
|
}
|
|
}
|
|
|
|
func TestCollectionFailureAndIsolation(t *testing.T) {
|
|
src := fixtureSources(t)
|
|
src.readFile = func(string) ([]byte, error) {
|
|
return []byte("read_bytes: 5\nwrite_bytes: 6\n"), errors.New("unavailable")
|
|
}
|
|
src.statfs = func(_ string, fs *syscall.Statfs_t) error {
|
|
fs.Blocks, fs.Bavail, fs.Bsize = 100, 20, 4096
|
|
return errors.New("unavailable")
|
|
}
|
|
got := collect("", Queue{}, src)
|
|
if !filepath.IsAbs(got.Storage.Path) || got.Storage.TotalBytes != 0 || got.IO.Available || got.IO.ReadBytes != 0 || got.CPU.LoadAverage != "" {
|
|
t.Fatalf("failed source leaked partial values: %+v", got)
|
|
}
|
|
if got.Memory.HeapBytes != 1024 || got.CPU.LogicalCPUs != 8 || got.IO.ProcessID == "" {
|
|
t.Fatalf("independent sources lost: %+v", got)
|
|
}
|
|
// Each call has independent values; the process identity alone is shared.
|
|
for i := 0; i < 16; i++ {
|
|
t.Run("parallel", func(t *testing.T) {
|
|
t.Parallel()
|
|
other := collect("", Queue{}, fixtureSources(t))
|
|
if other.IO.ProcessID != got.IO.ProcessID || other.Storage.TotalBytes != 409600 {
|
|
t.Fatalf("identity/isolation %+v", other)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestStorageInvalidCounters(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
blocks, available uint64
|
|
size int64
|
|
}{
|
|
{10, 11, 4096}, {10, 1, -1}, {10, 1, 0}, {math.MaxUint64, 1, 4096},
|
|
} {
|
|
if got := storageCounters("/root", tc.blocks, tc.available, tc.size); got != (Storage{Path: "/root"}) {
|
|
t.Fatalf("invalid storage wrapped: %+v", got)
|
|
}
|
|
}
|
|
if got := storageCounters("/root", math.MaxUint64, 0, 1); got.UsedBytes != math.MaxUint64 {
|
|
t.Fatalf("valid boundary %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestProcessIOParser(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name, text string
|
|
available bool
|
|
read, write uint64
|
|
}{
|
|
{"valid", "read_bytes: 12\nwrite_bytes: 34\nrchar: 999\ncancelled_write_bytes: 99", true, 12, 34},
|
|
{"zero", "read_bytes: 0\nwrite_bytes: 0", true, 0, 0},
|
|
{"empty", "", false, 0, 0},
|
|
{"partial", "read_bytes: 12", false, 12, 0},
|
|
{"negative", "read_bytes: -1\nwrite_bytes: 34", false, 0, 0},
|
|
{"overflow", "read_bytes: 18446744073709551616\nwrite_bytes: 0", false, 0, 0},
|
|
{"invalid", "read_bytes: 12\nwrite_bytes: bad", false, 12, 0},
|
|
{"max", "read_bytes: 18446744073709551615\nwrite_bytes: 0", true, math.MaxUint64, 0},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := ParseProcessIO(tc.text)
|
|
if got.Available != tc.available || got.ReadBytes != tc.read || got.WriteBytes != tc.write || got.Scope != "process" || got.ProcessID != processID {
|
|
t.Fatalf("%+v", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProcFileBound(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "counters")
|
|
for _, size := range []int{maxProcBytes, maxProcBytes + 1} {
|
|
if err := os.WriteFile(path, []byte(strings.Repeat("x", size)), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, err := readProcFile(path)
|
|
if size == maxProcBytes && (err != nil || len(data) != size) {
|
|
t.Fatalf("limit rejected: %v", err)
|
|
}
|
|
if size > maxProcBytes && (err == nil || data != nil) {
|
|
t.Fatal("oversize file accepted")
|
|
}
|
|
}
|
|
if _, err := readProcFile(path + "-missing"); err == nil {
|
|
t.Fatal("missing file accepted")
|
|
}
|
|
}
|
|
|
|
func TestCollectLocalRuntime(t *testing.T) {
|
|
root := t.TempDir()
|
|
before := time.Now().UnixMilli()
|
|
got := Collect(root, Queue{Paused: true})
|
|
if got.TimestampMS < before || got.TimestampMS > time.Now().UnixMilli() || got.Storage.Path != root || got.CPU.LogicalCPUs < 1 || got.Memory.Goroutines < 1 || !got.Queue.Paused || got.IO.ProcessID != processID {
|
|
t.Fatalf("runtime snapshot %+v", got)
|
|
}
|
|
}
|