package provision import ( "context" "errors" "os" "path/filepath" "strings" "testing" "time" "github.com/carlosframework/platform/internal/release" ) // TestOfflineHelperRefusalsMatchTheCopyRecord pins the five transition // refusals that live in fmt.Sprintf helpers (MV-O36, MV-O38, MV-O40, MV-O41, // MV-O43) to the approved text in the copy record, byte for byte, with each // placeholder filled with the value the transition supplies. Each refusal is // driven through its real transition and still wraps ErrMoveConflict. // (census-ruling-moves-offline, condition 2.) func TestOfflineHelperRefusalsMatchTheCopyRecord(t *testing.T) { approved := offlineApprovedCopy(t) now := time.Date(2026, 10, 21, 9, 0, 0, 0, time.UTC) steps := offlineSteps(now) ctx := context.Background() for _, c := range []struct { id string walked int mut func(*Record) error fills []string // in the order the placeholders appear }{ {"MV-O36", 1, OfflineFlip(offID), []string{OfflineStopping, OfflineSealed}}, {"MV-O38", 2, OfflineSealedBy(offID, "box-b", MoveSource{}), []string{offSealer}}, {"MV-O40", 6, OfflineAbort(offID), []string{OfflineOpen}}, {"MV-O41", 1, OfflineFinish(offID, now), []string{OfflineStopping}}, {"MV-O43", 0, OfflineStart(offID, "us", "uk", "ops@example.com", now), []string{"home", "us"}}, } { t.Run(c.id, func(t *testing.T) { text, ok := approved[c.id] if !ok { t.Fatalf("%s is not an approved row of the copy record", c.id) } want := text for _, v := range c.fills { i := strings.Index(want, "<") j := strings.Index(want, ">") if i < 0 || j < i { t.Fatalf("%s: more fills than placeholders in %q", c.id, text) } want = want[:i] + v + want[j+1:] } if strings.ContainsAny(want, "<>") { t.Fatalf("%s: an unfilled placeholder in %q", c.id, want) } s := release.NewDirStore(t.TempDir()) rec := offlineRecord(t, s) offlineWalk(t, s, rec, steps[:c.walked]) _, err := SetRecordMove(ctx, s, rec.Account, rec.App, rec.Host, c.mut) if !errors.Is(err, ErrMoveConflict) { t.Fatalf("err %v, want ErrMoveConflict", err) } if got, full := err.Error(), ErrMoveConflict.Error()+": "+want; got != full { t.Fatalf("refusal\n got %q\nwant %q", got, full) } }) } } // offlineApprovedCopy reads the MV-O rows of the copy record whose status is // approved, keyed by id. func offlineApprovedCopy(t *testing.T) map[string]string { t.Helper() raw, err := os.ReadFile(filepath.Join(censusRoot(t), "docs/superpowers/plans/2026-10-06-moves-phase1-copy.md")) if err != nil { t.Fatal(err) } rows := map[string]string{} for _, line := range strings.Split(string(raw), "\n") { if !strings.HasPrefix(line, "| MV-O") { continue } cells := strings.Split(strings.Trim(line, "|"), " | ") if len(cells) != 4 || strings.TrimSpace(cells[3]) != "approved" { continue } rows[strings.TrimSpace(cells[0])] = cells[2] } return rows }