@@ -117,6 +117,19 @@ func TestExecNonZeroExit(t *testing.T) {
117117 }
118118}
119119
120+ // minimalPNG is a valid 1x1 white PNG used in tests.
121+ var minimalPNG = []byte {
122+ 0x89 , 0x50 , 0x4e , 0x47 , 0x0d , 0x0a , 0x1a , 0x0a , // PNG signature
123+ 0x00 , 0x00 , 0x00 , 0x0d , 0x49 , 0x48 , 0x44 , 0x52 , // IHDR chunk
124+ 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x01 ,
125+ 0x08 , 0x02 , 0x00 , 0x00 , 0x00 , 0x90 , 0x77 , 0x53 ,
126+ 0xde , 0x00 , 0x00 , 0x00 , 0x0c , 0x49 , 0x44 , 0x41 ,
127+ 0x54 , 0x08 , 0xd7 , 0x63 , 0xf8 , 0xcf , 0xc0 , 0x00 ,
128+ 0x00 , 0x00 , 0x02 , 0x00 , 0x01 , 0xe2 , 0x21 , 0xbc ,
129+ 0x33 , 0x00 , 0x00 , 0x00 , 0x00 , 0x49 , 0x45 , 0x4e ,
130+ 0x44 , 0xae , 0x42 , 0x60 , 0x82 ,
131+ }
132+
120133func TestImage (t * testing.T ) {
121134 dir := t .TempDir ()
122135 file := filepath .Join (dir , "demo.md" )
@@ -125,27 +138,12 @@ func TestImage(t *testing.T) {
125138 t .Fatal (err )
126139 }
127140
128- // Create a tiny valid PNG file and a script that outputs its path
129141 pngPath := filepath .Join (dir , "test.png" )
130- // Minimal 1x1 white PNG
131- pngData := []byte {
132- 0x89 , 0x50 , 0x4e , 0x47 , 0x0d , 0x0a , 0x1a , 0x0a , // PNG signature
133- 0x00 , 0x00 , 0x00 , 0x0d , 0x49 , 0x48 , 0x44 , 0x52 , // IHDR chunk
134- 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 , 0x01 ,
135- 0x08 , 0x02 , 0x00 , 0x00 , 0x00 , 0x90 , 0x77 , 0x53 ,
136- 0xde , 0x00 , 0x00 , 0x00 , 0x0c , 0x49 , 0x44 , 0x41 ,
137- 0x54 , 0x08 , 0xd7 , 0x63 , 0xf8 , 0xcf , 0xc0 , 0x00 ,
138- 0x00 , 0x00 , 0x02 , 0x00 , 0x01 , 0xe2 , 0x21 , 0xbc ,
139- 0x33 , 0x00 , 0x00 , 0x00 , 0x00 , 0x49 , 0x45 , 0x4e ,
140- 0x44 , 0xae , 0x42 , 0x60 , 0x82 ,
141- }
142- if err := os .WriteFile (pngPath , pngData , 0644 ); err != nil {
142+ if err := os .WriteFile (pngPath , minimalPNG , 0644 ); err != nil {
143143 t .Fatal (err )
144144 }
145145
146- script := "echo " + pngPath
147-
148- if err := Image (file , script , "" ); err != nil {
146+ if err := Image (file , pngPath , "" ); err != nil {
149147 t .Fatal (err )
150148 }
151149
@@ -162,3 +160,75 @@ func TestImage(t *testing.T) {
162160 t .Errorf ("expected image output in file, got: %s" , s )
163161 }
164162}
163+
164+ func TestImageMarkdownRef (t * testing.T ) {
165+ dir := t .TempDir ()
166+ file := filepath .Join (dir , "demo.md" )
167+
168+ if err := Init (file , "Test" , "dev" ); err != nil {
169+ t .Fatal (err )
170+ }
171+
172+ pngPath := filepath .Join (dir , "test.png" )
173+ if err := os .WriteFile (pngPath , minimalPNG , 0644 ); err != nil {
174+ t .Fatal (err )
175+ }
176+
177+ input := ""
178+
179+ if err := Image (file , input , "" ); err != nil {
180+ t .Fatal (err )
181+ }
182+
183+ content , err := os .ReadFile (file )
184+ if err != nil {
185+ t .Fatal (err )
186+ }
187+
188+ s := string (content )
189+ if ! strings .Contains (s , " {
190+ t .Errorf ("expected alt text 'My screenshot' in image output, got: %s" , s )
191+ }
192+ if ! strings .Contains (s , "```bash {image}" ) {
193+ t .Errorf ("expected image code block in file, got: %s" , s )
194+ }
195+ }
196+
197+ func TestParseImageInput (t * testing.T ) {
198+ tests := []struct {
199+ input string
200+ path string
201+ altText string
202+ }{
203+ {"/path/to/img.png" , "/path/to/img.png" , "" },
204+ {"" , "/path/to/img.png" , "alt text" },
205+ {"" , "file.jpg" , "" },
206+ {"" , "shot.png" , "Screenshot of homepage" },
207+ {"  " , "file.png" , "padded" },
208+ {"not-markdown.png" , "not-markdown.png" , "" },
209+ }
210+ for _ , tt := range tests {
211+ path , alt := parseImageInput (tt .input )
212+ if path != tt .path {
213+ t .Errorf ("parseImageInput(%q): path = %q, want %q" , tt .input , path , tt .path )
214+ }
215+ if alt != tt .altText {
216+ t .Errorf ("parseImageInput(%q): altText = %q, want %q" , tt .input , alt , tt .altText )
217+ }
218+ }
219+ }
220+
221+ func TestImageMarkdownRefBadPath (t * testing.T ) {
222+ dir := t .TempDir ()
223+ file := filepath .Join (dir , "demo.md" )
224+
225+ if err := Init (file , "Test" , "dev" ); err != nil {
226+ t .Fatal (err )
227+ }
228+
229+ input := ""
230+ err := Image (file , input , "" )
231+ if err == nil {
232+ t .Error ("expected error for nonexistent image path in markdown ref" )
233+ }
234+ }
0 commit comments