| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "os" |
| 8 | "regexp" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // Opt-in fixture for tools/drive/comments.mjs: the real server-rendered |
| 13 | // comments page and its stylesheets, with a synthetic owner, sheet and |
| 14 | // comment. No session cookie is exported. |
| 15 | func TestBrowserCommentsFixture(t *testing.T) { |
| 16 | path := os.Getenv("SHEETS_COMMENTS_FIXTURE") |
| 17 | if path == "" { |
| 18 | t.Skip("set SHEETS_COMMENTS_FIXTURE to render the browser fixture") |
| 19 | } |
| 20 | app, cookies := signIn(t) |
| 21 | s, _ := newSheet(t, app, cookies, "Budget") |
| 22 | addComment(t, app, s, cookies, 0, 0, "Check this total") |
| 23 | addComment(t, app, s, cookies, 0, 0, "Checked, it matches the ledger") |
| 24 | page := do(t, app, as(httptest.NewRequest(http.MethodGet, commentsPath(s), nil), cookies)) |
| 25 | if page.Code != http.StatusOK { |
| 26 | t.Fatalf("comments page: %d", page.Code) |
| 27 | } |
| 28 | assets := map[string]string{} |
| 29 | for _, match := range regexp.MustCompile(`(?:src|href)="(/static/[^" ]+)"`).FindAllStringSubmatch(page.Body.String(), -1) { |
| 30 | asset := do(t, app, as(httptest.NewRequest(http.MethodGet, match[1], nil), cookies)) |
| 31 | if asset.Code != http.StatusOK { |
| 32 | t.Fatalf("asset %s: %d", match[1], asset.Code) |
| 33 | } |
| 34 | assets[match[1]] = asset.Body.String() |
| 35 | } |
| 36 | data, err := json.Marshal(map[string]any{"html": page.Body.String(), "assets": assets, |
| 37 | "bodies": []string{"Check this total", "Checked, it matches the ledger"}}) |
| 38 | if err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | if err := os.WriteFile(path, data, 0600); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | } |
| 45 | |