package release // The layout intent's WIRE SHAPE (isolated-exec design ยง8.2): what lands in // control/layout.json, what a box makes of it for its own label, and that a // schema this binary does not understand is refused rather than guessed at. import ( "context" "testing" ) func TestLayoutIntentRoundTripAndScope(t *testing.T) { s := NewDirStore(t.TempDir()) ctx := context.Background() if _, found, err := LoadLayoutIntent(ctx, s, "kass-1"); err != nil || found { t.Fatalf("absent object: found=%v err=%v", found, err) } stamped, err := AppendLayoutIntent(ctx, s, LayoutIntent{Mode: LayoutModeNested, Scope: LayoutScope{Box: "kass-1"}}) if err != nil { t.Fatalf("append: %v", err) } if stamped.ID == "" || stamped.RequestedAt == "" { t.Fatalf("append did not return the stamped id and timestamp: %+v", stamped) } got, found, err := LoadLayoutIntent(ctx, s, "kass-1") if err != nil || !found || got.Mode != LayoutModeNested { t.Fatalf("round trip: %+v %v %v", got, found, err) } if got.ID != stamped.ID || got.RequestedAt != stamped.RequestedAt { t.Fatalf("stored request = %+v, want the returned stamped values %+v", got, stamped) } if !got.Applies("kass-1") || got.Applies("kass-2") { t.Error("box scope must match only the named box") } if _, found, err := LoadLayoutIntent(ctx, s, "kass-2"); err != nil || found { t.Fatalf("a box-scoped intent must not apply to another box: found=%v err=%v", found, err) } // A later fleet-wide request (empty scope) applies everywhere, including // kass-1, and it is newer, so it is the one that wins there too. second, err := AppendLayoutIntent(ctx, s, LayoutIntent{Mode: LayoutModeFlat}) if err != nil { t.Fatalf("second append: %v", err) } if second.ID == stamped.ID { t.Fatalf("second append reused the first request's id %q", second.ID) } got, found, err = LoadLayoutIntent(ctx, s, "kass-1") if err != nil || !found || got.Mode != LayoutModeFlat { t.Fatalf("the newest applying request must win: %+v %v %v", got, found, err) } if !got.Applies("anything") { t.Error("an empty scope is the fleet") } // A newer schema is an error, not a guess. The store has no raw put, so // the fixture is written by deleting the key and putting the body fresh // (create-only). if err := s.Delete(ctx, LayoutIntentKey()); err != nil { t.Fatalf("delete: %v", err) } if _, err := s.Put(ctx, LayoutIntentKey(), []byte(`{"schema":2,"requests":[{"mode":"nested"}]}`), ""); err != nil { t.Fatalf("put newer-schema fixture: %v", err) } if _, _, err := LoadLayoutIntent(ctx, s, "kass-1"); err == nil { t.Fatal("a newer schema must be an error, not a guess") } } func TestValidLayoutMode(t *testing.T) { for _, m := range []string{LayoutModeFlat, LayoutModeNested} { if !ValidLayoutMode(m) { t.Errorf("ValidLayoutMode(%q) = false, want true", m) } } for _, m := range []string{LayoutModeNone, "bogus"} { if ValidLayoutMode(m) { t.Errorf("ValidLayoutMode(%q) = true, want false", m) } } } func TestAppendLayoutIntentRefusesAnInvalidMode(t *testing.T) { s := NewDirStore(t.TempDir()) stamped, err := AppendLayoutIntent(context.Background(), s, LayoutIntent{Mode: "bogus"}) if err == nil { t.Fatal("an invalid mode must be refused, not written") } if stamped != (LayoutIntent{}) { t.Fatalf("a refused append must return the zero value, got %+v", stamped) } if _, found, err := LoadLayoutIntent(context.Background(), s, "any"); err != nil || found { t.Fatalf("a refused append must leave the object absent: found=%v err=%v", found, err) } } // TestLayoutFloorIsTheFirstReleaseWhereNestedRowsWake pins the console's // downgrade floor to the release history. v0.20.0 is the first tag whose // unit set carries data-instances.mount (git ls-tree v0.19.0 and v0.20.0 // internal/host/units/, checked 2026-10-06), but a nested row cannot wake // on v0.20.0 to v0.24.2: their carlos-edge.service still carries // RestrictSUIDSGID=, which makes the agent's openat2 fail with ENOSYS, and // every nested open goes through it. Plan 4 (7b7d3026) dropped the line; // no tag contains it yet, so the floor is the next release, v0.25.0, which // must be cut from a main that contains 7b7d3026. Moving either constant // means re-checking the tags, not editing this test to match. func TestLayoutFloorIsTheFirstReleaseWhereNestedRowsWake(t *testing.T) { if LayoutFloor != (SemVer{Major: 0, Minor: 25, Patch: 0}) { t.Fatalf("LayoutFloor = %+v, want v0.25.0 (the first release carrying 7b7d3026's edge openat2 fix)", LayoutFloor) } for v, pre := range map[string]bool{ "v0.19.0": true, "v0.20.0": true, "v0.24.2": true, "0.24.9": true, "v0.25.0": false, "v0.26.0": false, "v1.0.0": false, // Not a version: nobody can place it against the floor, so it is refused. "dev": true, "": true, "v0.25": true, "abc123": true, } { if got := PreLayout(v); got != pre { t.Errorf("PreLayout(%q) = %v, want %v", v, got, pre) } } // BelowFloor is the placeable half of PreLayout: an unparseable tag is // false, and the caller decides what that means for its own rule. for v, below := range map[string]bool{"v0.19.0": true, "v0.24.2": true, "v0.25.0": false, "dev": false, "": false} { if got := BelowFloor(v, LayoutFloor); got != below { t.Errorf("BelowFloor(%q, LayoutFloor) = %v, want %v", v, got, below) } } } func TestRecordLayoutIntentAppendsWithForceAndScope(t *testing.T) { s := NewDirStore(t.TempDir()) ctx := context.Background() first, err := RecordLayoutIntent(ctx, s, LayoutModeNested, "kass-1", true) if err != nil || first.ID == "" || first.Mode != LayoutModeNested || first.Scope.Box != "kass-1" || !first.Force { t.Fatalf("first = %+v %v", first, err) } second, err := RecordLayoutIntent(ctx, s, LayoutModeFlat, "", false) if err != nil || second.ID == first.ID || second.Mode != LayoutModeFlat || second.Scope.Box != "" || second.Force { t.Fatalf("second = %+v %v", second, err) } got, found, err := LoadLayoutIntent(ctx, s, "kass-1") if err != nil || !found || got.ID != second.ID { t.Fatalf("kass-1 sees %+v (found %v, err %v), want the newest request %s", got, found, err, second.ID) } if _, err := RecordLayoutIntent(ctx, s, "sideways", "", false); err == nil { t.Fatal("bad mode accepted") } }