package main import ( "encoding/json" "fmt" "testing" "github.com/carlosframework/platform/internal/rolloutwords" "github.com/carlosframework/platform/internal/updatewords" ) // Astra I1 #12: the reason is the EFFECTIVE one the box reports, whether the // record set it or the app's default did; an inherited major hold names its // number from the report. func TestUpdatesReason(t *testing.T) { two := 2 tx := updatewords.Text for _, c := range []struct { u *apiUpdatesJoin settingsDefault bool want string }{ {nil, true, ""}, {&apiUpdatesJoin{Reason: "adopt", Enforced: true}, true, ""}, {&apiUpdatesJoin{Reason: "manual", Detail: "v1", Enforced: true}, false, fmt.Sprintf(tx("settings.box.reason.manual"), "v1")}, {&apiUpdatesJoin{Reason: "pin", Detail: "v3", Enforced: true}, false, fmt.Sprintf(tx("settings.box.reason.pin"), "v3")}, {&apiUpdatesJoin{Reason: "request", Detail: "v5", Enforced: true}, false, fmt.Sprintf(tx("settings.box.reason.request"), "v5")}, {&apiUpdatesJoin{Reason: "failed", Detail: "v6", Enforced: true}, false, fmt.Sprintf(tx("settings.box.reason.failed"), "v6")}, {&apiUpdatesJoin{Reason: "major", Detail: "v2", Major: &two, Enforced: true}, false, fmt.Sprintf(tx("settings.box.reason.major"), 2, "v2")}, {&apiUpdatesJoin{Reason: "major", Detail: "v2", Major: &two, Baking: "v4", Enforced: true}, false, fmt.Sprintf(tx("settings.box.reason.major"), 2, "v2") + "; " + fmt.Sprintf(tx("settings.box.reason.baking"), "v4")}, {&apiUpdatesJoin{Baking: "v4", Enforced: true}, false, fmt.Sprintf(tx("settings.box.reason.baking"), "v4")}, {&apiUpdatesJoin{Enforced: false}, false, tx("settings.cli.not-enforced-reason")}, {&apiUpdatesJoin{Enforced: false}, true, ""}, {&apiUpdatesJoin{Reason: "unknown", Enforced: true}, false, tx("settings.box.reason.unknown")}, {&apiUpdatesJoin{Reason: "new", Enforced: true}, false, tx("settings.box.reason.new")}, // A reason word from a newer box renders nothing, and never panics. {&apiUpdatesJoin{Reason: "from-the-future", Detail: "v9", Enforced: true}, false, ""}, } { if got := updatesReason(c.u, c.settingsDefault); got != c.want { t.Errorf("updatesReason(%+v, %v) = %q, want %q", c.u, c.settingsDefault, got, c.want) } } } // Astra I1 #11: the list record carries the report under update_report and // the record's own setting under updates; both decode. func TestTheListRecordDecodesBothUpdatesFields(t *testing.T) { var r apiInstanceRecord body := `{"host":"a.shop.example.com","updates":"manual","settings_default":false, "update_report":{"reason":"manual","detail":"v1","enforced":true}}` if err := json.Unmarshal([]byte(body), &r); err != nil { t.Fatalf("decode: %v", err) } if r.UpdatesSetting != "manual" || r.Updates == nil || r.Updates.Reason != "manual" || r.SettingsDefault { t.Fatalf("decoded %+v / %+v / %v", r.UpdatesSetting, r.Updates, r.SettingsDefault) } } // The REASON cell: a rollout reason (a failure hold above all) outranks a // preference; with none, the settings reason shows. func TestReasonCellOrder(t *testing.T) { manual := &apiUpdatesJoin{Reason: "manual", Detail: "v1", Enforced: true} manualText := fmt.Sprintf(updatewords.Text("settings.box.reason.manual"), "v1") held := &apiRollout{Installed: "v2", Serving: "v1", Awake: true, HeldBy: "v2"} for _, c := range []struct { name string r apiInstanceRecord want string }{ {"held outranks manual", apiInstanceRecord{Rollout: held, Updates: manual}, fmt.Sprintf(rolloutwords.StateHeld, "v2")}, {"no rollout reason shows manual", apiInstanceRecord{Rollout: &apiRollout{Installed: "v1", Serving: "v1", Awake: true}, Updates: manual}, manualText}, {"no rollout at all shows manual", apiInstanceRecord{Updates: manual}, manualText}, {"nothing to say", apiInstanceRecord{}, ""}, {"an old box on a default record", apiInstanceRecord{Updates: &apiUpdatesJoin{}, SettingsDefault: true}, ""}, {"an old box on a held record", apiInstanceRecord{Updates: &apiUpdatesJoin{}}, updatewords.Text("settings.cli.not-enforced-reason")}, } { if got := reasonCell(c.r); got != c.want { t.Errorf("%s: reasonCell = %q, want %q", c.name, got, c.want) } } } // Astra r1 (R18): a report the console cannot attribute to this instance's // tenant reaches the list as whether the box enforces settings and nothing // else. Enforced, it says nothing; not enforced, a non-default record still // reads "settings not enforced". func TestAnEnforcedOnlyReportReadsCorrectly(t *testing.T) { for _, c := range []struct { body string want string }{ {`{"host":"h","settings_default":false,"update_report":{"enforced":true}}`, ""}, {`{"host":"h","settings_default":true,"update_report":{"enforced":true}}`, ""}, {`{"host":"h","settings_default":false,"update_report":{"enforced":false}}`, updatewords.Text("settings.cli.not-enforced-reason")}, {`{"host":"h","settings_default":true,"update_report":{"enforced":false}}`, ""}, } { var r apiInstanceRecord if err := json.Unmarshal([]byte(c.body), &r); err != nil { t.Fatalf("decode %s: %v", c.body, err) } if got := reasonCell(r); got != c.want { t.Errorf("%s: reasonCell = %q, want %q", c.body, got, c.want) } } }