summaryrefslogtreecommitdiff
path: root/internal/cleaner/emoji_test.go
blob: 30642a8180b93b47640e2f86b9b0f7ea5b06cb1e (plain)
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package cleaner_test

import (
	"context"
	"errors"
	"time"

	"github.com/superseriousbusiness/gotosocial/internal/config"
	"github.com/superseriousbusiness/gotosocial/internal/db"
	"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/util"
)

func copyMap(in map[string]*gtsmodel.Emoji) map[string]*gtsmodel.Emoji {
	out := make(map[string]*gtsmodel.Emoji, len(in))

	for k, v1 := range in {
		v2 := new(gtsmodel.Emoji)
		*v2 = *v1
		out[k] = v2
	}

	return out
}

func (suite *CleanerTestSuite) TestEmojiUncacheRemote() {
	suite.testEmojiUncacheRemote(
		context.Background(),
		mapvals(suite.emojis),
	)
}

func (suite *CleanerTestSuite) TestEmojiUncacheRemoteDryRun() {
	suite.testEmojiUncacheRemote(
		gtscontext.SetDryRun(context.Background()),
		mapvals(suite.emojis),
	)
}

func (suite *CleanerTestSuite) TestEmojiFixBroken() {
	suite.testEmojiFixBroken(
		context.Background(),
		mapvals(suite.emojis),
	)
}

func (suite *CleanerTestSuite) TestEmojiFixBrokenDryRun() {
	suite.testEmojiFixBroken(
		gtscontext.SetDryRun(context.Background()),
		mapvals(suite.emojis),
	)
}

func (suite *CleanerTestSuite) TestEmojiPruneUnused() {
	suite.testEmojiPruneUnused(
		context.Background(),
		mapvals(suite.emojis),
	)
}

func (suite *CleanerTestSuite) TestEmojiPruneUnusedDryRun() {
	suite.testEmojiPruneUnused(
		gtscontext.SetDryRun(context.Background()),
		mapvals(suite.emojis),
	)
}

func (suite *CleanerTestSuite) TestEmojiFixCacheStates() {
	// Copy testrig emojis + mark
	// rainbow emoji as uncached
	// so there's something to fix.
	emojis := copyMap(suite.emojis)
	emojis["rainbow"].Cached = util.Ptr(false)

	suite.testEmojiFixCacheStates(
		context.Background(),
		mapvals(emojis),
	)
}

func (suite *CleanerTestSuite) TestEmojiFixCacheStatesDryRun() {
	// Copy testrig emojis + mark
	// rainbow emoji as uncached
	// so there's something to fix.
	emojis := copyMap(suite.emojis)
	emojis["rainbow"].Cached = util.Ptr(false)

	suite.testEmojiFixCacheStates(
		gtscontext.SetDryRun(context.Background()),
		mapvals(emojis),
	)
}

func (suite *CleanerTestSuite) testEmojiUncacheRemote(ctx context.Context, emojis []*gtsmodel.Emoji) {
	var uncacheIDs []string

	// Test state.
	t := suite.T()

	// Get max remote cache days to keep.
	days := config.GetMediaRemoteCacheDays()
	olderThan := time.Now().Add(-24 * time.Hour * time.Duration(days))

	for _, emoji := range emojis {
		// Check whether this emoji should be uncached.
		ok, err := suite.shouldUncacheEmoji(ctx, emoji, olderThan)
		if err != nil {
			t.Fatalf("error checking whether emoji should be uncached: %v", err)
		}

		if ok {
			// Mark this emoji ID as to be uncached.
			uncacheIDs = append(uncacheIDs, emoji.ID)
		}
	}

	// Attempt to uncache remote emojis.
	found, err := suite.cleaner.Emoji().UncacheRemote(ctx, olderThan)
	if err != nil {
		t.Errorf("error uncaching remote emojis: %v", err)
		return
	}

	// Check expected were uncached.
	if found != len(uncacheIDs) {
		t.Errorf("expected %d emojis to be uncached, %d were", len(uncacheIDs), found)
		return
	}

	if gtscontext.DryRun(ctx) {
		// nothing else to test.
		return
	}

	for _, id := range uncacheIDs {
		// Fetch the emoji by ID that should now be uncached.
		emoji, err := suite.state.DB.GetEmojiByID(ctx, id)
		if err != nil {
			t.Fatalf("error fetching emoji from database: %v", err)
		}

		// Check cache state.
		if *emoji.Cached {
			t.Errorf("emoji %s@%s should have been uncached", emoji.Shortcode, emoji.Domain)
		}

		// Check that the emoji files in storage have been deleted.
		if ok, err := suite.state.Storage.Has(ctx, emoji.ImagePath); err != nil {
			t.Fatalf("error checking storage for emoji: %v", err)
		} else if ok {
			t.Errorf("emoji %s@%s image path should not exist", emoji.Shortcode, emoji.Domain)
		} else if ok, err := suite.state.Storage.Has(ctx, emoji.ImageStaticPath); err != nil {
			t.Fatalf("error checking storage for emoji: %v", err)
		} else if ok {
			t.Errorf("emoji %s@%s image static path should not exist", emoji.Shortcode, emoji.Domain)
		}
	}
}

func (suite *CleanerTestSuite) shouldUncacheEmoji(ctx context.Context, emoji *gtsmodel.Emoji, after time.Time) (bool, error) {
	if emoji.ImageRemoteURL == "" {
		// Local emojis are never uncached.
		return false, nil
	}

	if emoji.Cached == nil || !*emoji.Cached {
		// Emoji is already uncached.
		return false, nil
	}

	// Get related accounts using this emoji (if any).
	accounts, err := suite.state.DB.GetAccountsUsingEmoji(ctx, emoji.ID)
	if err != nil {
		return false, err
	}

	// Check if accounts are recently updated.
	for _, account := range accounts {
		if account.FetchedAt.After(after) {
			return false, nil
		}
	}

	// Get related statuses using this emoji (if any).
	statuses, err := suite.state.DB.GetStatusesUsingEmoji(ctx, emoji.ID)
	if err != nil {
		return false, err
	}

	// Check if statuses are recently updated.
	for _, status := range statuses {
		if status.FetchedAt.After(after) {
			return false, nil
		}
	}

	return true, nil
}

func (suite *CleanerTestSuite) testEmojiFixBroken(ctx context.Context, emojis []*gtsmodel.Emoji) {
	var fixIDs []string

	// Test state.
	t := suite.T()

	for _, emoji := range emojis {
		// Check whether this emoji should be fixed.
		ok, err := suite.shouldFixBrokenEmoji(ctx, emoji)
		if err != nil {
			t.Fatalf("error checking whether emoji should be fixed: %v", err)
		}

		if ok {
			// Mark this emoji ID as to be fixed.
			fixIDs = append(fixIDs, emoji.ID)
		}
	}

	// Attempt to fix broken emojis.
	found, err := suite.cleaner.Emoji().FixBroken(ctx)
	if err != nil {
		t.Errorf("error fixing broken emojis: %v", err)
		return
	}

	// Check expected were fixed.
	if found != len(fixIDs) {
		t.Errorf("expected %d emojis to be fixed, %d were", len(fixIDs), found)
		return
	}

	if gtscontext.DryRun(ctx) {
		// nothing else to test.
		return
	}

	for _, id := range fixIDs {
		// Fetch the emoji by ID that should now be fixed.
		emoji, err := suite.state.DB.GetEmojiByID(ctx, id)
		if err != nil {
			t.Fatalf("error fetching emoji from database: %v", err)
		}

		// Ensure category was cleared.
		if emoji.CategoryID != "" {
			t.Errorf("emoji %s@%s should have empty category", emoji.Shortcode, emoji.Domain)
		}
	}
}

func (suite *CleanerTestSuite) shouldFixBrokenEmoji(ctx context.Context, emoji *gtsmodel.Emoji) (bool, error) {
	if emoji.CategoryID == "" {
		// no category issue.
		return false, nil
	}

	// Get the related category for this emoji.
	category, err := suite.state.DB.GetEmojiCategory(ctx, emoji.CategoryID)
	if err != nil && !errors.Is(err, db.ErrNoEntries) {
		return false, nil
	}

	return (category == nil), nil
}

func (suite *CleanerTestSuite) testEmojiPruneUnused(ctx context.Context, emojis []*gtsmodel.Emoji) {
	var pruneIDs []string

	// Test state.
	t := suite.T()

	for _, emoji := range emojis {
		// Check whether this emoji should be pruned.
		ok, err := suite.shouldPruneEmoji(ctx, emoji)
		if err != nil {
			t.Fatalf("error checking whether emoji should be pruned: %v", err)
		}

		if ok {
			// Mark this emoji ID as to be pruned.
			pruneIDs = append(pruneIDs, emoji.ID)
		}
	}

	// Attempt to prune emojis.
	found, err := suite.cleaner.Emoji().PruneUnused(ctx)
	if err != nil {
		t.Errorf("error fixing broken emojis: %v", err)
		return
	}

	// Check expected were pruned.
	if found != len(pruneIDs) {
		t.Errorf("expected %d emojis to be pruned, %d were", len(pruneIDs), found)
		return
	}

	if gtscontext.DryRun(ctx) {
		// nothing else to test.
		return
	}

	for _, id := range pruneIDs {
		// Fetch the emoji by ID that should now be pruned.
		emoji, err := suite.state.DB.GetEmojiByID(ctx, id)
		if err != nil && !errors.Is(err, db.ErrNoEntries) {
			t.Fatalf("error fetching emoji from database: %v", err)
		}

		// Ensure gone.
		if emoji != nil {
			t.Errorf("emoji %s@%s should have been pruned", emoji.Shortcode, emoji.Domain)
		}
	}
}

func (suite *CleanerTestSuite) shouldPruneEmoji(ctx context.Context, emoji *gtsmodel.Emoji) (bool, error) {
	if emoji.ImageRemoteURL == "" {
		// Local emojis are never pruned.
		return false, nil
	}

	// Get related accounts using this emoji (if any).
	accounts, err := suite.state.DB.GetAccountsUsingEmoji(ctx, emoji.ID)
	if err != nil {
		return false, err
	} else if len(accounts) > 0 {
		return false, nil
	}

	// Get related statuses using this emoji (if any).
	statuses, err := suite.state.DB.GetStatusesUsingEmoji(ctx, emoji.ID)
	if err != nil {
		return false, err
	} else if len(statuses) > 0 {
		return false, nil
	}

	return true, nil
}

func (suite *CleanerTestSuite) testEmojiFixCacheStates(ctx context.Context, emojis []*gtsmodel.Emoji) {
	var fixIDs []string

	// Test state.
	t := suite.T()

	for _, emoji := range emojis {
		// Check whether this emoji should be fixed.
		ok, err := suite.shouldFixEmojiCacheState(ctx, emoji)
		if err != nil {
			t.Fatalf("error checking whether emoji should be fixed: %v", err)
		}

		if ok {
			// Mark this emoji ID as to be fixed.
			fixIDs = append(fixIDs, emoji.ID)
		}
	}

	// Attempt to fix broken emoji cache states.
	found, err := suite.cleaner.Emoji().FixCacheStates(ctx)
	if err != nil {
		t.Errorf("error fixing broken emojis: %v", err)
		return
	}

	// Check expected were fixed.
	if found != len(fixIDs) {
		t.Errorf("expected %d emojis to be fixed, %d were", len(fixIDs), found)
		return
	}

	if gtscontext.DryRun(ctx) {
		// nothing else to test.
		return
	}

	for _, id := range fixIDs {
		// Fetch the emoji by ID that should now be fixed.
		emoji, err := suite.state.DB.GetEmojiByID(ctx, id)
		if err != nil {
			t.Fatalf("error fetching emoji from database: %v", err)
		}

		// Ensure emoji cache state has been fixed.
		ok, err := suite.shouldFixEmojiCacheState(ctx, emoji)
		if err != nil {
			t.Fatalf("error checking whether emoji should be fixed: %v", err)
		} else if ok {
			t.Errorf("emoji %s@%s cache state should have been fixed", emoji.Shortcode, emoji.Domain)
		}
	}
}

func (suite *CleanerTestSuite) shouldFixEmojiCacheState(ctx context.Context, emoji *gtsmodel.Emoji) (bool, error) {
	// Check whether emoji image path exists.
	haveImage, err := suite.state.Storage.Has(ctx, emoji.ImagePath)
	if err != nil {
		return false, err
	}

	// Check whether emoji static path exists.
	haveStatic, err := suite.state.Storage.Has(ctx, emoji.ImageStaticPath)
	if err != nil {
		return false, err
	}

	switch exists := (haveImage && haveStatic); {
	case emoji.Cached != nil &&
		*emoji.Cached && !exists:
		// (cached can be nil in tests)
		// Cached but missing files.
		return true, nil

	case emoji.Cached != nil &&
		!*emoji.Cached && exists:
		// (cached can be nil in tests)
		// Uncached but unexpected files.
		return true, nil

	default:
		// No cache state issue.
		return false, nil
	}
}