From 271c23e5f2f42b98216af1ae2ee36d7260cd0cc7 Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar Date: Tue, 13 Oct 2026 09:23:40 +0000 Subject: [PATCH] fix(erpsetup): restore the letter head source, ignore from UI saves (#68) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What - The letter head step also compares `source` and `footer_source` (want: HTML/HTML). - `ensureFields` treats ``/`` as equal (Frappe's HTML editor adds them on a UI save). ## Why Live ERPNext letter head `BreakPilot` (last saved 2026-10-06 09:19 in the UI): `source = Image`, so documents use an image header instead of our HTML one, plus `` in content and footer. The dry run showed `letter head → would update` on every run, and an apply would not have restored `source`. ## Test plan - [x] New test: tbody-only difference → `exists`; source Image → restored to HTML. - [x] Existing erpsetup tests. - [ ] User: dry run shows `letter head → would update` once, apply, then `exists`; check one invoice PDF shows the HTML header. ## Risk **Blast radius:** Letter Head `BreakPilot` only (source fields). **Rollback:** set source back in the UI. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01DmXxNULoSNPvjF7buKipsB Co-authored-by: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Reviewed-on: https://git.breakpilot.com/platform/tenant-registry/pulls/68 --- internal/erpsetup/erpsetup.go | 12 ++++++++++-- internal/erpsetup/erpsetup_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/internal/erpsetup/erpsetup.go b/internal/erpsetup/erpsetup.go index 7f35b0c..ecadcec 100644 --- a/internal/erpsetup/erpsetup.go +++ b/internal/erpsetup/erpsetup.go @@ -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 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("", "", "", "") + 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 { diff --git a/internal/erpsetup/erpsetup_test.go b/internal/erpsetup/erpsetup_test.go index 3f56857..2fa3124 100644 --- a/internal/erpsetup/erpsetup_test.go +++ b/internal/erpsetup/erpsetup_test.go @@ -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 , nothing else changed. + lh["content"] = strings.ReplaceAll(letterHeadHTML, "", "") + lh["footer"] = strings.ReplaceAll(letterFooterHTML, "", "") + 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"]) + } +}