OGP の画像の試験を追加

このコミットが含まれているのは:
routersys committed 2026-11-15 17:13:46 +09:00
1 親 42019c798e
コミット 2a86a2d6ee
1ファイル変更
+120
+120
ファイルの表示
@@ -2,6 +2,7 @@ package site
import (
"encoding/json"
"encoding/xml"
"io/fs"
"net/url"
"os"
@@ -311,3 +312,122 @@ func TestTheBrokersCsvIsNeverPublished(t *testing.T) {
}
}
}
func cards(t *testing.T, public string) map[string]string {
t.Helper()
found := map[string]string{}
err := filepath.WalkDir(public, func(path string, entry fs.DirEntry, err error) error {
if err == nil && !entry.IsDir() && entry.Name() == "ogp.svg" {
rel, _ := filepath.Rel(public, path)
found[filepath.ToSlash(rel)] = read(t, public, filepath.ToSlash(rel))
}
return err
})
if err != nil {
t.Fatal(err)
}
return found
}
func TestEveryCardIsWellFormedSvgOfTheSizeOfAnOgpImage(t *testing.T) {
found := cards(t, build(t, true))
if len(found) < 3+len(pnlDays(t)) {
t.Fatalf("%d cards", len(found))
}
for name, svg := range found {
var doc struct {
Width string `xml:"width,attr"`
Height string `xml:"height,attr"`
}
if err := xml.Unmarshal([]byte(svg), &doc); err != nil {
t.Errorf("%s: %v", name, err)
continue
}
if doc.Width != "1200" || doc.Height != "630" {
t.Errorf("%s: %sx%s", name, doc.Width, doc.Height)
}
if strings.Contains(svg, "<script") || strings.Contains(svg, "href=") {
t.Errorf("%s: a script or a link", name)
}
}
}
func TestEveryPageNamesACardThatIsBuilt(t *testing.T) {
public := build(t, true)
found := cards(t, public)
meta := regexp.MustCompile(`property="og:image" content="https://fx\.routersys\.com/([^"]*)ogp\.png"`)
err := filepath.WalkDir(public, func(path string, entry fs.DirEntry, err error) error {
if err != nil || entry.IsDir() || !strings.HasSuffix(path, ".html") {
return err
}
rel, _ := filepath.Rel(public, path)
page := read(t, public, filepath.ToSlash(rel))
if strings.Contains(page, `http-equiv="refresh"`) || strings.Contains(page, "http-equiv=refresh") || rel == "404.html" {
return nil
}
m := meta.FindStringSubmatch(page)
if m == nil {
t.Errorf("%s: no og:image", rel)
return nil
}
if _, ok := found[m[1]+"ogp.svg"]; !ok {
t.Errorf("%s: %sogp.svg is not built", rel, m[1])
}
for _, want := range []string{`property="og:title"`, `property="og:description"`, `property="og:url"`, `name="twitter:card" content="summary_large_image"`} {
if !strings.Contains(page, want) {
t.Errorf("%s lacks %s", rel, want)
}
}
return nil
})
if err != nil {
t.Fatal(err)
}
}
func TestCardsShowTheFiguresOfTheirPage(t *testing.T) {
found := cards(t, build(t, false))
var report struct {
Monthly []struct{ Label string }
}
data, _ := os.ReadFile(filepath.Join(root(t), "data", "pnl", "report.json"))
if err := json.Unmarshal(data, &report); err != nil {
t.Fatal(err)
}
latest := report.Monthly[len(report.Monthly)-1].Label
for name, wants := range map[string][]string{
"pnl/ogp.svg": {latest + "の損益", "通算", "<polyline"},
"pnl/" + pnlDays(t)[0] + "/ogp.svg": {"この日の損益", "決済", "<polyline"},
"ogp.svg": {"通算の損益", "FX の収支と、取引の振り返り"},
} {
for _, want := range wants {
if !strings.Contains(found[name], want) {
t.Errorf("%s lacks %q", name, want)
}
}
}
}
func TestPostCardEscapesAndTruncatesTheTitle(t *testing.T) {
card := read(t, build(t, true), "posts/2026-11-09-escape/ogp.svg")
for _, want := range []string{"Tom &amp; Jerry &lt;試験", "&gt; 長い題名", "…</text>", "2026-11-09"} {
if !strings.Contains(card, want) {
t.Errorf("the card lacks %q", want)
}
}
if strings.Count(card, `font-size="68"`) > 3 {
t.Error("more than three title lines")
}
}
func TestTheDockerfileConvertsEveryCardAndFailsIfOneIsLeft(t *testing.T) {
data, err := os.ReadFile(filepath.Join(root(t), "Dockerfile"))
if err != nil {
t.Fatal(err)
}
for _, want := range []string{"rsvg-convert", "font-noto-cjk", `-name ogp.svg`, `test -z "$(find /public -name ogp.svg)"`} {
if !strings.Contains(string(data), want) {
t.Errorf("the Dockerfile lacks %q", want)
}
}
}