|
| 1 | +package opengraphimg |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "errors" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/s3" |
| 11 | + "github.com/gauntface/go-html-asset-manager/v4/assets/genimgs" |
| 12 | + "github.com/gauntface/go-html-asset-manager/v4/manipulations" |
| 13 | + "github.com/gauntface/go-html-asset-manager/v4/utils/config" |
| 14 | + "github.com/gauntface/go-html-asset-manager/v4/utils/html/htmlparsing" |
| 15 | + "github.com/google/go-cmp/cmp" |
| 16 | + "golang.org/x/net/html" |
| 17 | +) |
| 18 | + |
| 19 | +var errInjected = errors.New("injected error") |
| 20 | + |
| 21 | +var reset func() |
| 22 | + |
| 23 | +func TestMain(m *testing.M) { |
| 24 | + origGenimgsLookupSizes := genimgsLookupSizes |
| 25 | + |
| 26 | + reset = func() { |
| 27 | + genimgsLookupSizes = origGenimgsLookupSizes |
| 28 | + } |
| 29 | + |
| 30 | + os.Exit(m.Run()) |
| 31 | +} |
| 32 | + |
| 33 | +func TestManipulator(t *testing.T) { |
| 34 | + tests := []struct { |
| 35 | + description string |
| 36 | + doc *html.Node |
| 37 | + findNodes func(tag string, node *html.Node) []*html.Node |
| 38 | + genimgsLookupSizes func(s3 *s3.Client, conf *config.Config, imgPath string) ([]genimgs.GenImg, error) |
| 39 | + wantError error |
| 40 | + wantHTML string |
| 41 | + }{ |
| 42 | + { |
| 43 | + description: "do nothing when no property", |
| 44 | + findNodes: htmlparsing.FindNodesByTag, |
| 45 | + doc: MustGetNode(t, `<meta diff-attr="og:image" />`), |
| 46 | + wantHTML: `<html><head><meta diff-attr="og:image"/></head><body></body></html>`, |
| 47 | + }, |
| 48 | + { |
| 49 | + description: "do nothing when not og:image property", |
| 50 | + findNodes: htmlparsing.FindNodesByTag, |
| 51 | + doc: MustGetNode(t, `<meta property="og:other" />`), |
| 52 | + wantHTML: `<html><head><meta property="og:other"/></head><body></body></html>`, |
| 53 | + }, |
| 54 | + { |
| 55 | + description: "do nothing when no content", |
| 56 | + findNodes: htmlparsing.FindNodesByTag, |
| 57 | + doc: MustGetNode(t, `<meta property="og:image" />`), |
| 58 | + wantHTML: `<html><head><meta property="og:image"/></head><body></body></html>`, |
| 59 | + }, |
| 60 | + { |
| 61 | + description: "return error if getting images fails", |
| 62 | + findNodes: htmlparsing.FindNodesByTag, |
| 63 | + doc: MustGetNode(t, `<meta property="og:image" content="/images/default-social.png" />`), |
| 64 | + genimgsLookupSizes: func(s3 *s3.Client, conf *config.Config, imgPath string) ([]genimgs.GenImg, error) { |
| 65 | + return nil, errInjected |
| 66 | + }, |
| 67 | + wantError: errInjected, |
| 68 | + }, |
| 69 | + { |
| 70 | + description: "do nothing when no images", |
| 71 | + findNodes: htmlparsing.FindNodesByTag, |
| 72 | + doc: MustGetNode(t, `<meta property="og:image" content="/images/default-social.png" />`), |
| 73 | + genimgsLookupSizes: func(s3 *s3.Client, conf *config.Config, imgPath string) ([]genimgs.GenImg, error) { |
| 74 | + return []genimgs.GenImg{}, nil |
| 75 | + }, |
| 76 | + wantHTML: `<html><head><meta property="og:image" content="/images/default-social.png"/></head><body></body></html>`, |
| 77 | + }, |
| 78 | + { |
| 79 | + description: "do nothing when no basic images", |
| 80 | + findNodes: htmlparsing.FindNodesByTag, |
| 81 | + doc: MustGetNode(t, `<meta property="og:image" content="/images/default-social.png" />`), |
| 82 | + genimgsLookupSizes: func(s3 *s3.Client, conf *config.Config, imgPath string) ([]genimgs.GenImg, error) { |
| 83 | + return []genimgs.GenImg{ |
| 84 | + { |
| 85 | + Type: "image/webp", |
| 86 | + }, |
| 87 | + }, nil |
| 88 | + }, |
| 89 | + wantHTML: `<html><head><meta property="og:image" content="/images/default-social.png"/></head><body></body></html>`, |
| 90 | + }, |
| 91 | + { |
| 92 | + description: "do nothing when no images on the right size", |
| 93 | + findNodes: htmlparsing.FindNodesByTag, |
| 94 | + doc: MustGetNode(t, `<meta property="og:image" content="/images/default-social.png" />`), |
| 95 | + genimgsLookupSizes: func(s3 *s3.Client, conf *config.Config, imgPath string) ([]genimgs.GenImg, error) { |
| 96 | + return []genimgs.GenImg{ |
| 97 | + { |
| 98 | + Size: RECOMMENDED_OG_IMG_WIDTH + 1, |
| 99 | + }, |
| 100 | + }, nil |
| 101 | + }, |
| 102 | + wantHTML: `<html><head><meta property="og:image" content="/images/default-social.png"/></head><body></body></html>`, |
| 103 | + }, |
| 104 | + { |
| 105 | + description: "update the image with the correct size", |
| 106 | + findNodes: htmlparsing.FindNodesByTag, |
| 107 | + doc: MustGetNode(t, `<meta property="og:image" content="/images/default-social.png" />`), |
| 108 | + genimgsLookupSizes: func(s3 *s3.Client, conf *config.Config, imgPath string) ([]genimgs.GenImg, error) { |
| 109 | + return []genimgs.GenImg{ |
| 110 | + { |
| 111 | + Size: RECOMMENDED_OG_IMG_WIDTH, |
| 112 | + URL: "/images/default-social.1200xabc.png", |
| 113 | + }, |
| 114 | + }, nil |
| 115 | + }, |
| 116 | + wantHTML: `<html><head><meta content="http://base-url.com/images/default-social.1200xabc.png" property="og:image"/></head><body></body></html>`, |
| 117 | + }, |
| 118 | + } |
| 119 | + |
| 120 | + for _, tt := range tests { |
| 121 | + t.Run(tt.description, func(t *testing.T) { |
| 122 | + defer reset() |
| 123 | + |
| 124 | + genimgsLookupSizes = tt.genimgsLookupSizes |
| 125 | + |
| 126 | + r := manipulations.Runtime{ |
| 127 | + Config: &config.Config{ |
| 128 | + BaseURL: "http://base-url.com", |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + err := Manipulator(r, tt.doc) |
| 133 | + if !errors.Is(err, tt.wantError) { |
| 134 | + t.Fatalf("Unexpected error; got %v, want %v", err, tt.wantError) |
| 135 | + } |
| 136 | + |
| 137 | + if err != nil { |
| 138 | + return |
| 139 | + } |
| 140 | + |
| 141 | + if diff := cmp.Diff(MustRenderNode(t, tt.doc), tt.wantHTML); diff != "" { |
| 142 | + t.Fatalf("Unexpected HTML files; diff %v", diff) |
| 143 | + } |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func MustGetNode(t *testing.T, input string) *html.Node { |
| 149 | + t.Helper() |
| 150 | + |
| 151 | + doc, err := html.Parse(strings.NewReader(input)) |
| 152 | + if err != nil { |
| 153 | + t.Fatalf("Failed to parse HTML: %v", err) |
| 154 | + } |
| 155 | + return doc |
| 156 | +} |
| 157 | + |
| 158 | +func MustRenderNode(t *testing.T, n *html.Node) string { |
| 159 | + t.Helper() |
| 160 | + |
| 161 | + if n == nil { |
| 162 | + return "" |
| 163 | + } |
| 164 | + |
| 165 | + var buf bytes.Buffer |
| 166 | + err := html.Render(&buf, n) |
| 167 | + if err != nil { |
| 168 | + t.Fatalf("failed to render html node to string: %v", err) |
| 169 | + } |
| 170 | + |
| 171 | + return buf.String() |
| 172 | +} |
0 commit comments