1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
package processor
import (
"context"
"image"
"image/png"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"codeberg.org/snonux/snonux/internal/config"
"codeberg.org/snonux/snonux/internal/post"
)
var ctx = context.Background() //nolint:gochecknoglobals // test-only top-level helper used by every test in the file
func TestRun_processesTxt(t *testing.T) {
t.Parallel()
in := t.TempDir()
out := t.TempDir()
if err := os.WriteFile(filepath.Join(in, "note.txt"), []byte("Hello world"), 0o644); err != nil {
t.Fatal(err)
}
cfg := &config.Config{InputDir: in, OutputDir: out, BaseURL: "https://x.test"}
n, err := Run(ctx, cfg)
if err != nil {
t.Fatalf("Run: %v", err)
}
if n != 1 {
t.Fatalf("processed count = %d; want 1", n)
}
entries, err := os.ReadDir(filepath.Join(out, "posts"))
if err != nil || len(entries) != 1 {
t.Fatalf("posts dir: %v entries=%v", err, entries)
}
postDir := filepath.Join(out, "posts", entries[0].Name())
p, err := post.Load(postDir)
if err != nil {
t.Fatalf("Load: %v", err)
}
if p.PostType != post.TypeText {
t.Fatalf("type %v", p.PostType)
}
if _, err := os.ReadFile(filepath.Join(in, "note.txt")); !os.IsNotExist(err) {
t.Fatal("source should be removed")
}
}
func TestRun_unsupportedExt(t *testing.T) {
t.Parallel()
in := t.TempDir()
out := t.TempDir()
if err := os.WriteFile(filepath.Join(in, "x.bin"), []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
_, err := Run(ctx, &config.Config{InputDir: in, OutputDir: out, BaseURL: "https://x"})
if err == nil {
t.Fatal("expected error")
}
}
func TestRun_readInputDirFails(t *testing.T) {
t.Parallel()
_, err := Run(ctx, &config.Config{InputDir: "/nonexistent/inbox/xyz", OutputDir: t.TempDir(), BaseURL: "https://x"})
if err == nil {
t.Fatal("expected error")
}
}
func TestRun_png(t *testing.T) {
t.Parallel()
in := t.TempDir()
out := t.TempDir()
pngPath := filepath.Join(in, "shot.png")
f, err := os.Create(pngPath)
if err != nil {
t.Fatal(err)
}
img := image.NewRGBA(image.Rect(0, 0, 4, 4))
if err := png.Encode(f, img); err != nil {
f.Close()
t.Fatal(err)
}
f.Close()
n, err := Run(ctx, &config.Config{InputDir: in, OutputDir: out, BaseURL: "https://x"})
if err != nil {
t.Fatalf("Run: %v", err)
}
if n != 1 {
t.Fatalf("n=%d", n)
}
}
func TestRun_mp3(t *testing.T) {
t.Parallel()
in := t.TempDir()
out := t.TempDir()
if err := os.WriteFile(filepath.Join(in, "clip.mp3"), []byte("fake-mp3-bytes"), 0o644); err != nil {
t.Fatal(err)
}
n, err := Run(ctx, &config.Config{InputDir: in, OutputDir: out, BaseURL: "https://x"})
if err != nil {
t.Fatalf("Run: %v", err)
}
if n != 1 {
t.Fatalf("n=%d", n)
}
}
func TestRun_markdown(t *testing.T) {
t.Parallel()
in := t.TempDir()
out := t.TempDir()
if err := os.WriteFile(filepath.Join(in, "x.md"), []byte("# Hi\n\n**bold**"), 0o644); err != nil {
t.Fatal(err)
}
n, err := Run(ctx, &config.Config{InputDir: in, OutputDir: out, BaseURL: "https://x"})
if err != nil {
t.Fatalf("Run: %v", err)
}
if n != 1 {
t.Fatalf("n=%d", n)
}
entries, _ := os.ReadDir(filepath.Join(out, "posts"))
p, err := post.Load(filepath.Join(out, "posts", entries[0].Name()))
if err != nil {
t.Fatal(err)
}
if p.PostType != post.TypeMarkdown {
t.Fatalf("got %v", p.PostType)
}
}
func TestUniqueID_new(t *testing.T) {
t.Parallel()
postsDir := t.TempDir()
id, err := uniqueID(postsDir, time.Now().UTC())
if err != nil {
t.Fatalf("uniqueID: %v", err)
}
if id == "" {
t.Fatal("expected non-empty id")
}
}
func TestUniqueID_collision(t *testing.T) {
t.Parallel()
postsDir := t.TempDir()
now := time.Now().UTC()
// Pre-create the first expected directory so uniqueID must pick the next suffix.
firstID := post.NewID(now, 0)
if err := os.MkdirAll(filepath.Join(postsDir, firstID), 0o755); err != nil {
t.Fatal(err)
}
id, err := uniqueID(postsDir, now)
if err != nil {
t.Fatalf("uniqueID: %v", err)
}
if id == firstID {
t.Fatalf("expected different id, got %q", id)
}
}
func TestUniqueID_statError(t *testing.T) {
t.Parallel()
// Create a postsDir and remove read permission so Stat fails with
// a permission error rather than IsNotExist.
postsDir := t.TempDir()
if err := os.Chmod(postsDir, 0o000); err != nil {
t.Fatal(err)
}
defer os.Chmod(postsDir, 0o755) // restore for cleanup
_, err := uniqueID(postsDir, time.Now().UTC())
if err == nil {
t.Fatal("expected error when stat fails")
}
}
func TestRun_markdownWithLocalImage(t *testing.T) {
t.Parallel()
in := t.TempDir()
out := t.TempDir()
pngPath := filepath.Join(in, "embed.png")
f, err := os.Create(pngPath)
if err != nil {
t.Fatal(err)
}
if err := png.Encode(f, image.NewRGBA(image.Rect(0, 0, 2, 2))); err != nil {
f.Close()
t.Fatal(err)
}
f.Close()
md := `
text`
if err := os.WriteFile(filepath.Join(in, "post.md"), []byte(md), 0o644); err != nil {
t.Fatal(err)
}
n, err := Run(ctx, &config.Config{InputDir: in, OutputDir: out, BaseURL: "https://x"})
if err != nil {
t.Fatalf("Run: %v", err)
}
if n != 1 {
t.Fatalf("n=%d", n)
}
entries, _ := os.ReadDir(filepath.Join(out, "posts"))
pdir := filepath.Join(out, "posts", entries[0].Name())
p, err := post.Load(pdir)
if err != nil {
t.Fatal(err)
}
if len(p.Assets) < 1 {
t.Fatalf("want assets, got %+v", p.Assets)
}
if _, err := os.Stat(filepath.Join(pdir, "embed.png")); err != nil {
t.Fatal(err)
}
}
func TestRun_twoMarkdownsClaimingSameImageFails(t *testing.T) {
t.Parallel()
in := t.TempDir()
out := t.TempDir()
// Shared image in the inbox.
pngPath := filepath.Join(in, "pic.png")
f, err := os.Create(pngPath)
if err != nil {
t.Fatal(err)
}
if err := png.Encode(f, image.NewRGBA(image.Rect(0, 0, 2, 2))); err != nil {
f.Close()
t.Fatal(err)
}
f.Close()
// Two markdown files both reference the same image.
if err := os.WriteFile(filepath.Join(in, "a.md"), []byte("\n"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(in, "b.md"), []byte("\n"), 0o644); err != nil {
t.Fatal(err)
}
_, err = Run(ctx, &config.Config{InputDir: in, OutputDir: out, BaseURL: "https://x"})
if err == nil {
t.Fatal("expected error when two markdowns claim the same image")
}
if !strings.Contains(err.Error(), "pic.png") {
t.Fatalf("error should mention the conflicting image, got: %v", err)
}
// Verify that no post directories were created and no source files deleted.
entries, _ := os.ReadDir(filepath.Join(out, "posts"))
if len(entries) != 0 {
t.Fatalf("expected no posts created, got %d", len(entries))
}
for _, name := range []string{"pic.png", "a.md", "b.md"} {
if _, err := os.Stat(filepath.Join(in, name)); err != nil {
t.Fatalf("source %s should still exist: %v", name, err)
}
}
}
func TestRun_duplicateImageClaimsInSameMarkdownAllowed(t *testing.T) {
t.Parallel()
in := t.TempDir()
out := t.TempDir()
pngPath := filepath.Join(in, "pic.png")
f, err := os.Create(pngPath)
if err != nil {
t.Fatal(err)
}
if err := png.Encode(f, image.NewRGBA(image.Rect(0, 0, 2, 2))); err != nil {
f.Close()
t.Fatal(err)
}
f.Close()
// Same markdown references the same image twice — should not be treated as a conflict.
md := "\n\n"
if err := os.WriteFile(filepath.Join(in, "post.md"), []byte(md), 0o644); err != nil {
t.Fatal(err)
}
n, err := Run(ctx, &config.Config{InputDir: in, OutputDir: out, BaseURL: "https://x"})
if err != nil {
t.Fatalf("Run: %v", err)
}
if n != 1 {
t.Fatalf("n=%d; want 1", n)
}
}
func TestRun_markdownWithLocalImage_removeFails(t *testing.T) {
t.Parallel()
if runtime.GOOS == "windows" {
t.Skip("chmod does not reliably deny removal on Windows")
}
in := t.TempDir()
out := t.TempDir()
pngPath := filepath.Join(in, "embed.png")
f, err := os.Create(pngPath)
if err != nil {
t.Fatal(err)
}
if err := png.Encode(f, image.NewRGBA(image.Rect(0, 0, 2, 2))); err != nil {
f.Close()
t.Fatal(err)
}
f.Close()
md := `
text`
if err := os.WriteFile(filepath.Join(in, "post.md"), []byte(md), 0o644); err != nil {
t.Fatal(err)
}
// Remove write permission from the inbox so os.Remove on the extra fails.
if err := os.Chmod(in, 0o555); err != nil {
t.Fatal(err)
}
defer os.Chmod(in, 0o755) // restore for cleanup
_, err = Run(ctx, &config.Config{InputDir: in, OutputDir: out, BaseURL: "https://x"})
if err == nil {
t.Fatal("expected error when inbox extra removal fails")
}
if !strings.Contains(err.Error(), "embed.png") {
t.Fatalf("error should mention embed.png, got: %v", err)
}
// Ensure nothing was persisted: post directory should have been rolled back.
entries, _ := os.ReadDir(filepath.Join(out, "posts"))
if len(entries) != 0 {
t.Fatalf("expected no posts after rollback, got %d", len(entries))
}
// Source files should still be in the inbox.
for _, name := range []string{"post.md", "embed.png"} {
if _, err := os.Stat(filepath.Join(in, name)); err != nil {
t.Fatalf("source %s should still exist: %v", name, err)
}
}
}
|