fix(erpsetup): restore the letter head source, ignore <tbody> from UI saves #68

Merged
sharang merged 1 commits from fix/erpsetup-letterhead-source into main 2026-10-13 09:23:41 +00:00
2 changed files with 40 additions and 2 deletions

No files matched your search

+10 -2
View File
@@ -156,7 +156,7 @@ func Run(ctx context.Context, cfg Config) ([]Step, error) {
[][]any{{"file_url", "=", logoFileURL}}, logoFileDoc()); err != nil {
return r.steps, err
}
if err := r.ensureFields(ctx, "Letter Head", letterHeadName, "letter head "+letterHeadName, letterHeadDoc(), "content", "footer"); err != nil {
if err := r.ensureFields(ctx, "Letter Head", letterHeadName, "letter head "+letterHeadName, letterHeadDoc(), "source", "footer_source", "content", "footer"); err != nil {
return r.steps, err
}
for _, pf := range printFormats() {
@@ -271,7 +271,7 @@ func (r *runner) ensureFields(ctx context.Context, doctype, name, what string, d
_ = json.Unmarshal(body, &cur)
patch := map[string]any{}
for _, k := range keys {
if fmt.Sprint(cur.Data[k]) != fmt.Sprint(doc[k]) {
if !sameValue(cur.Data[k], doc[k]) {
patch[k] = doc[k]
}
}
@@ -430,6 +430,14 @@ func (r *runner) call(ctx context.Context, method, path string, body any) (int,
return resp.StatusCode, raw, nil
}
// sameValue compares a stored field with the wanted one. Frappe's HTML
// editor wraps table rows in <tbody> when a document is saved in the UI;
// that is not a change of ours and must not show up as one on every run.
func sameValue(got, want any) bool {
norm := strings.NewReplacer("<tbody>", "", "</tbody>", "")
return norm.Replace(fmt.Sprint(got)) == norm.Replace(fmt.Sprint(want))
}
func truncate(b []byte) string {
s := strings.TrimSpace(string(b))
if len(s) > 300 {
+30
View File
@@ -487,3 +487,33 @@ func TestProductWordingIsNoLegalCheck(t *testing.T) {
}
}
}
func TestLetterHeadSourceIsRestoredAndTbodyIgnored(t *testing.T) {
f := newFake()
srv := httptest.NewServer(f)
defer srv.Close()
if _, err := Run(context.Background(), cfg(srv.URL)); err != nil {
t.Fatal(err)
}
lh := f.docs["Letter Head"][letterHeadName]
// Saved once in the UI: the editor added <tbody>, nothing else changed.
lh["content"] = strings.ReplaceAll(letterHeadHTML, "<tr>", "<tbody><tr>")
lh["footer"] = strings.ReplaceAll(letterFooterHTML, "</table>", "</tbody></table>")
steps, err := Run(context.Background(), cfg(srv.URL))
if err != nil {
t.Fatal(err)
}
for _, s := range steps {
if s.What == "letter head "+letterHeadName && s.Result != "exists" {
t.Fatalf("tbody alone must not count as a change: %s", s.Result)
}
}
// The source was switched to Image: that is a change and is put back.
lh["source"] = "Image"
if _, err := Run(context.Background(), cfg(srv.URL)); err != nil {
t.Fatal(err)
}
if lh["source"] != "HTML" {
t.Fatalf("source not restored: %v", lh["source"])
}
}