| 1 | //go:build browser |
| 2 | |
| 3 | package headless |
| 4 | |
| 5 | import ( |
| 6 | "amadan.net/oficina/equipo/internal/browsertest" |
| 7 | |
| 8 | "context" |
| 9 | "github.com/chromedp/chromedp" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
| 17 | func TestBrowserCopySettingAndFlag(t *testing.T) { |
| 18 | for _, width := range []int64{390, 1280} { |
| 19 | t.Run(strconv.FormatInt(width, 10), func(t *testing.T) { |
| 20 | _, inst, runner := localEnv(t) |
| 21 | runner.start(t) |
| 22 | if err := inst.DB.G.Exec("INSERT INTO chat_projects(id,repo,title,state,revision) VALUES ('copy-project','amadan.net/oficina/sample','Sample','active',1)").Error; err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | cookie := inst.Cookie() |
| 26 | p := openPage(t, inst.URL, cookie.Name, cookie.Value, width, 900, width > 600) |
| 27 | defer p.stop() |
| 28 | p.run(chromedp.Navigate(inst.URL+"/conversation/settings/copy"), browsertest.Wait(`document.readyState==='complete'`)) |
| 29 | p.check(`!!document.querySelector('[name="copy_enabled"]')`, "Copy setting missing") |
| 30 | p.check(`!document.querySelector('[name="copy_enabled"]').checked`, "delegation defaulted on") |
| 31 | copyShot(t, p, "setting", width) |
| 32 | p.check(`(()=>{document.querySelector('[name="copy_enabled"]').click();document.querySelector('[data-copy-save]').click();return true})()`, "save Copy setting") |
| 33 | p.run(browsertest.Wait(`document.querySelector('[name="revision"]')?.value==='1'`)) |
| 34 | p.check(`document.querySelector('[name="copy_enabled"]').checked`, "delegation not saved") |
| 35 | if err := inst.DB.G.Exec("INSERT INTO chat_tasks(id,project_id,title,stage,creator,revision,created_at,updated_at) VALUES ('tkcopyflag','copy-project','Copy flag','noted','owner@example.com',1,datetime('now'),datetime('now'))").Error; err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if err := inst.DB.G.Exec("INSERT INTO chat_copy_reviews(id,project_id,task_id,key,text,flag) VALUES ('flag','copy-project','tkcopyflag','save','Save changes','Check the wording in context.')").Error; err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | p.run(chromedp.Navigate(inst.URL+"/tasks/tkcopyflag"), browsertest.Wait(`document.readyState==='complete'`)) |
| 42 | p.check(`document.querySelector('[data-copy-flag]')?.textContent.includes('Check the wording in context.')`, "wording flag missing") |
| 43 | p.check(`document.documentElement.scrollWidth<=innerWidth`, "Copy card overflows phone") |
| 44 | copyShot(t, p, "flag", width) |
| 45 | }) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func copyShot(t *testing.T, p *page, name string, width int64) { |
| 50 | t.Helper() |
| 51 | root := os.Getenv("EQUIPO_COPY_SHOTS") |
| 52 | if root == "" { |
| 53 | return |
| 54 | } |
| 55 | if err := os.MkdirAll(root, 0700); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 59 | raw, err := chromedp.FullScreenshot(90)(ctx, target) |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | return os.WriteFile(filepath.Join(root, name+"-"+strconv.FormatInt(width, 10)+".png"), raw, 0600) |
| 64 | })) |
| 65 | } |
| 66 | |
| 67 | func TestBrowserCopyContinue(t *testing.T) { |
| 68 | _, inst, runner := localEnv(t) |
| 69 | runner.start(t) |
| 70 | queries := []struct { |
| 71 | sql string |
| 72 | args []any |
| 73 | }{ |
| 74 | {"INSERT INTO chat_projects(id,repo,state,copy_owner,copy_revision) VALUES ('pcopy','amadan.net/oficina/sample','active','owner@example.com',1)", nil}, |
| 75 | {"INSERT INTO chat_tasks(id,project_id,title,stage,creator,branch,base_sha,revision) VALUES ('tkcopyexit','pcopy','Copy exit','in_review','owner@example.com','equipo/tkcopyexit',?,1)", []any{strings.Repeat("b", 40)}}, |
| 76 | {"INSERT INTO chat_steps(id,task_id,idx,specialist,person,state,pushed,candidate_sha) VALUES ('ce','tkcopyexit',1,'engineering','owner@example.com','done',1,?)", []any{strings.Repeat("a", 40)}}, |
| 77 | {"INSERT INTO chat_steps(id,task_id,idx,specialist,person,state,reason,terminated,attempt,candidate_sha,copy_owner,copy_revision) VALUES ('cc','tkcopyexit',2,'copy','owner@example.com','failed','copy_budget',1,1,?,'owner@example.com',1)", []any{strings.Repeat("a", 40)}}, |
| 78 | } |
| 79 | for _, q := range queries { |
| 80 | if err := inst.DB.G.Exec(q.sql, q.args...).Error; err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | } |
| 84 | cookie := inst.Cookie() |
| 85 | p := openPage(t, inst.URL, cookie.Name, cookie.Value, 390, 900, false) |
| 86 | defer p.stop() |
| 87 | p.run(chromedp.Navigate(inst.URL+"/tasks/tkcopyexit"), browsertest.Wait(`document.readyState==='complete'`)) |
| 88 | p.check(`document.querySelector('[data-copy-continue]')?.textContent==='Continue without Copy'`, "missing Copy exit") |
| 89 | p.check(`document.body.textContent.includes("Copy cannot review all these strings within this task’s token allowance.")`, "missing budget explanation") |
| 90 | p.run(chromedp.Click(`[data-copy-continue]`), browsertest.Wait(`!document.querySelector('[data-copy-continue]')`)) |
| 91 | var n int64 |
| 92 | if err := inst.DB.G.Raw("SELECT count(*) FROM chat_steps WHERE task_id='tkcopyexit' AND specialist='checks'").Scan(&n).Error; err != nil || n != 1 { |
| 93 | t.Fatalf("continue did not reach Checks: %d %v", n, err) |
| 94 | } |
| 95 | } |
| 96 | |