package mapping import ( "strings" "testing" sdk "code.gitea.io/sdk/gitea" ) func TestIDInBodyReadsBothSpellings(t *testing.T) { cases := []struct { name string body string want string }{ {"the spelling this tool writes", "\n\n## Summary\nx", "wire-sqlc"}, // The whole reason the reader is wider than the writer. {"the spelling already in the tracker", "\n\n## Summary\nx", "wire-sqlc"}, {"indented and loosely spaced", " \n", "wire-sqlc"}, {"no marker at all", "## Summary\nx", ""}, // A mangled comment falls back to the title rather than naming a file // after garbage. {"not a slug", "\n", ""}, {"not on a line of its own", "text \n", ""}, {"the first valid marker wins", "\n\n", "first-one"}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { if got := IDInBody(c.body); got != c.want { t.Errorf("IDInBody = %q, want %q", got, c.want) } }) } } // An issue pushed under the old name is sitting in the tracker with its local // file long since deleted — the marker is the only copy of its slug there is. // It has to keep resolving, and it has to come back up in the new spelling. func TestAnIssuePushedByTheOldNameStillResolves(t *testing.T) { const inTracker = "\n\n## Summary\nПроводка sqlc.\n" id := IDInBody(inTracker) if id != "wire-sqlc-appclick" { t.Fatalf("IDInBody = %q — every issue pushed under the old name would be orphaned", id) } iss, _ := FromPayload(&sdk.Issue{Index: 42, Title: "Wire sqlc", Body: inTracker}, id, tea, PayloadOptions{}) if strings.Contains(iss.Body, "tea:id") { t.Errorf("the old marker reached the local copy: %q", iss.Body) } if iss.Body != "## Summary\nПроводка sqlc." { t.Errorf("body = %q", iss.Body) } // And the next push rewrites it into the current spelling, without ever // having two. up := ToCreate(iss, RequestOptions{}).Body if !strings.HasPrefix(up, "\n\n") { t.Errorf("the marker was not rewritten: %q", up) } if strings.Contains(up, "tea:id") { t.Errorf("both spellings went up: %q", up) } if n := strings.Count(up, ":id "); n != 1 { t.Errorf("%d markers in the body, want 1", n) } } func TestMarkersCannotAccumulate(t *testing.T) { body := "## Summary\nx" // Whatever it arrived with — one, the other, several — it goes up with one. messy := "\n\n\n\n" + body got := WithIDMarker(messy, "real-one") if want := IDMarker("real-one") + "\n\n" + body; got != want { t.Errorf("got:\n%q\nwant:\n%q", got, want) } if got := WithIDMarker(got, "real-one"); strings.Count(got, "