fix(erpsetup): restore the letter head source, ignore <tbody> from UI saves (#68)
ci / shared (push) Successful in 14s
ci / test (push) Successful in 28m11s
ci / image (push) Successful in 35s

## What
- The letter head step also compares `source` and `footer_source` (want: HTML/HTML).
- `ensureFields` treats `<tbody>`/`</tbody>` 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 `<tbody>` 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: #68
This commit was merged in pull request #68.
This commit is contained in:
sharangandSharang Parnerkar committed 2026-10-13 09:23:40 +00:00
1 parent 8e2d8db4bf
commit 271c23e5f2
2 files changed
+40 -2

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"])
}
}