package offsite import ( "strings" "testing" "time" ) // D71 (R-1375): the four cases of KeepAccountRecord, as a table. The web tests (TestD71_*) prove the consequence // through the real save and the real RESET; this pins the rule itself. func TestKeepAccountRecord_FourCases(t *testing.T) { now := time.Date(2026, 11, 14, 20, 0, 0, 0, time.UTC) const ( provisioned = `{"git":{"username":"x"},"offsite":{"enabled":true,"type":"shared","host":"u1-sub3.example","user":"u1-sub3","port":23}}` choiceOnly = `{"offsite":{"enabled":true,"type":"shared","quota_gb":100}}` // ticked, never provisioned (or after a RESET) withRecord = `{"offsite_account":{"type":"shared","user":"u1-sub3","host":"u1-sub3.example","unticked_at":"2026-11-01T00:00:00Z"}}` unticked = `{"git":{"username":"x"}}` ) for _, c := range []struct { name, prev, next string wantUser, wantAt string // "" = no record keepsOther string }{ {"ticked and provisioned, then unticked: a record from the old descriptor", provisioned, unticked, "u1-sub3", "2026-11-14T20:00:00Z", `"git"`}, {"a record, still unticked: carried unchanged (the form does not know it)", withRecord, unticked, "u1-sub3", "2026-11-01T00:00:00Z", `"git"`}, {"ticked again: no record", withRecord, provisioned, "", "", `"offsite"`}, {"a config that carries BOTH a ticked tier and a record: the record goes (the descriptor is the record)", withRecord, `{"offsite":{"enabled":true,"type":"shared","user":"u1-sub3"},"offsite_account":{"type":"shared","user":"u1-sub3"}}`, "", "", `"offsite"`}, {"ticked, never provisioned, then unticked: no account was made, no record", choiceOnly, unticked, "", "", `"git"`}, // review 2026-11-14: a tick with the OTHER type makes a second account; the first one still exists {"unticked shared, then ticked as DEDICATED: the shared account's record stays", withRecord, `{"offsite":{"enabled":true,"type":"dedicated","user":"u9","host":"u9.example"}}`, "u1-sub3", "2026-11-01T00:00:00Z", `"offsite"`}, {"switched from shared to dedicated in ONE save: the shared account gets a record", provisioned, `{"offsite":{"enabled":true,"type":"dedicated","user":"u9","host":"u9.example"}}`, "u1-sub3", "2026-11-14T20:00:00Z", `"offsite"`}, {"never ticked: nothing", `{}`, unticked, "", "", `"git"`}, {"an empty previous config", ``, `{}`, "", "", ``}, } { out, err := KeepAccountRecord(c.prev, c.next, now) if err != nil { t.Fatalf("%s: %v", c.name, err) } rec := ReadAccountRecord(out) switch { case c.wantUser == "" && rec != nil: t.Errorf("%s: a record %+v, want none (%s)", c.name, rec, out) case c.wantUser != "" && (rec == nil || rec.User != c.wantUser || rec.UntickedAt != c.wantAt || rec.Type != "shared"): t.Errorf("%s: record %+v, want user %s unticked_at %s (%s)", c.name, rec, c.wantUser, c.wantAt, out) } if c.keepsOther != "" && !strings.Contains(out, c.keepsOther) { t.Errorf("%s: the other keys were lost: %s", c.name, out) } } if _, err := KeepAccountRecord(provisioned, `{not json`, now); err == nil { t.Error("a config that cannot be read must be an error (the save is then refused), not a silent loss of the record") } }