|
5 | 5 | "fmt" |
6 | 6 | "os" |
7 | 7 | "path/filepath" |
| 8 | + "strconv" |
8 | 9 | "testing" |
9 | 10 |
|
10 | 11 | "github.com/stretchr/testify/require" |
@@ -71,9 +72,9 @@ func TestImportCheckpointsCacheBatches(t *testing.T) { |
71 | 72 | for i := range checkpointEveryFiles + 1 { |
72 | 73 | channelID := "111111111111111121" |
73 | 74 | messageID := 333333333333333346 + i |
74 | | - body := []byte(fmt.Sprintf(`https://discord.com/channels/999999999999999996/%s |
| 75 | + body := bytesf(`https://discord.com/channels/999999999999999996/%s |
75 | 76 | {"id":"%d","channel_id":"%s","content":"checkpoint cache %d","timestamp":"2026-04-23T18:20:43Z","author":{"id":"222222222222222232","username":"alice"}} |
76 | | -`, channelID, messageID, channelID, i)) |
| 77 | +`, channelID, messageID, channelID, i) |
77 | 78 | require.NoError(t, os.WriteFile(filepath.Join(cachePath, fmt.Sprintf("entry_%03d", i)), body, 0o600)) |
78 | 79 | } |
79 | 80 |
|
@@ -101,18 +102,18 @@ func TestImportUsesLaterCacheMetadataBeforeCheckpointingEarlierBatch(t *testing. |
101 | 102 |
|
102 | 103 | channelID := "111111111111111121" |
103 | 104 | guildID := "999999999999999996" |
104 | | - require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_000"), []byte(fmt.Sprintf(`https://discord.com/api/v9/channels/%s/messages?limit=50 |
| 105 | + require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_000"), bytesf(`https://discord.com/api/v9/channels/%s/messages?limit=50 |
105 | 106 | {"id":"333333333333333346","channel_id":"%s","content":"needs later channel metadata","timestamp":"2026-04-23T18:20:43Z","author":{"id":"222222222222222232","username":"alice"}} |
106 | | -`, channelID, channelID)), 0o600)) |
| 107 | +`, channelID, channelID), 0o600)) |
107 | 108 | for i := 1; i < checkpointEveryFiles; i++ { |
108 | | - require.NoError(t, os.WriteFile(filepath.Join(cachePath, fmt.Sprintf("entry_%03d", i)), []byte(fmt.Sprintf( |
| 109 | + require.NoError(t, os.WriteFile(filepath.Join(cachePath, fmt.Sprintf("entry_%03d", i)), bytesf( |
109 | 110 | "https://discord.com/api/v9/channels/%s/messages?limit=50\n", |
110 | 111 | channelID, |
111 | | - )), 0o600)) |
| 112 | + ), 0o600)) |
112 | 113 | } |
113 | | - require.NoError(t, os.WriteFile(filepath.Join(cachePath, fmt.Sprintf("entry_%03d", checkpointEveryFiles)), []byte(fmt.Sprintf(`https://discord.com/api/v9/channels/%s/messages?limit=50 |
| 114 | + require.NoError(t, os.WriteFile(filepath.Join(cachePath, fmt.Sprintf("entry_%03d", checkpointEveryFiles)), bytesf(`https://discord.com/api/v9/channels/%s/messages?limit=50 |
114 | 115 | {"id":"%s","guild_id":"%s","type":0,"name":"later-metadata"} |
115 | | -`, channelID, channelID, guildID)), 0o600)) |
| 116 | +`, channelID, channelID, guildID), 0o600)) |
116 | 117 |
|
117 | 118 | st, err := store.Open(ctx, filepath.Join(dir, "discrawl.db")) |
118 | 119 | require.NoError(t, err) |
@@ -147,20 +148,20 @@ func TestImportCheckpointsPartiallyResolvedRetryBatch(t *testing.T) { |
147 | 148 | resolvedChannelID := "111111111111111121" |
148 | 149 | unresolvedChannelID := "111111111111111122" |
149 | 150 | guildID := "999999999999999996" |
150 | | - require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_000"), []byte(fmt.Sprintf(`https://discord.com/api/v9/channels/%s/messages?limit=50 |
| 151 | + require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_000"), bytesf(`https://discord.com/api/v10/channels/%s/messages?limit=50 |
151 | 152 | https://discord.com/api/v9/channels/%s/messages?limit=50 |
152 | 153 | {"id":"333333333333333346","channel_id":"%s","content":"partially resolved retry message","timestamp":"2026-04-23T18:20:43Z","author":{"id":"222222222222222232","username":"alice"}} |
153 | 154 | {"id":"333333333333333347","channel_id":"%s","content":"still unresolved retry message","timestamp":"2026-04-23T18:20:44Z","author":{"id":"222222222222222232","username":"alice"}} |
154 | | -`, resolvedChannelID, unresolvedChannelID, resolvedChannelID, unresolvedChannelID)), 0o600)) |
| 155 | +`, resolvedChannelID, unresolvedChannelID, resolvedChannelID, unresolvedChannelID), 0o600)) |
155 | 156 | for i := 1; i < checkpointEveryFiles; i++ { |
156 | | - require.NoError(t, os.WriteFile(filepath.Join(cachePath, fmt.Sprintf("entry_%03d", i)), []byte(fmt.Sprintf( |
| 157 | + require.NoError(t, os.WriteFile(filepath.Join(cachePath, fmt.Sprintf("entry_%03d", i)), bytesf( |
157 | 158 | "https://discord.com/api/v9/channels/%s/messages?limit=50\n", |
158 | 159 | resolvedChannelID, |
159 | | - )), 0o600)) |
| 160 | + ), 0o600)) |
160 | 161 | } |
161 | | - require.NoError(t, os.WriteFile(filepath.Join(cachePath, fmt.Sprintf("entry_%03d", checkpointEveryFiles)), []byte(fmt.Sprintf(`https://discord.com/api/v9/channels/%s/messages?limit=50 |
| 162 | + require.NoError(t, os.WriteFile(filepath.Join(cachePath, fmt.Sprintf("entry_%03d", checkpointEveryFiles)), bytesf(`https://discord.com/api/v9/channels/%s/messages?limit=50 |
162 | 163 | {"id":"%s","guild_id":"%s","type":0,"name":"partially-resolved"} |
163 | | -`, resolvedChannelID, resolvedChannelID, guildID)), 0o600)) |
| 164 | +`, resolvedChannelID, resolvedChannelID, guildID), 0o600)) |
164 | 165 |
|
165 | 166 | st, err := store.Open(ctx, filepath.Join(dir, "discrawl.db")) |
166 | 167 | require.NoError(t, err) |
@@ -196,9 +197,9 @@ func TestImportCheckpointsUnresolvableRouteBearingCacheMisses(t *testing.T) { |
196 | 197 | require.NoError(t, os.MkdirAll(cachePath, 0o755)) |
197 | 198 |
|
198 | 199 | channelID := "111111111111111121" |
199 | | - require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_000"), []byte(fmt.Sprintf(`https://discord.com/api/v9/channels/%s/messages?limit=50 |
| 200 | + require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_000"), bytesf(`https://discord.com/api/v9/channels/%s/messages?limit=50 |
200 | 201 | {"id":"333333333333333346","channel_id":"%s","content":"permanent unresolved cache miss","timestamp":"2026-04-23T18:20:43Z","author":{"id":"222222222222222232","username":"alice"}} |
201 | | -`, channelID, channelID)), 0o600)) |
| 202 | +`, channelID, channelID), 0o600)) |
202 | 203 |
|
203 | 204 | st, err := store.Open(ctx, filepath.Join(dir, "discrawl.db")) |
204 | 205 | require.NoError(t, err) |
@@ -229,11 +230,11 @@ func TestImportDoesNotAppendEventsForSkippedMixedBatch(t *testing.T) { |
229 | 230 | guildID := "999999999999999996" |
230 | 231 | resolvedChannelID := "111111111111111121" |
231 | 232 | unresolvedChannelID := "111111111111111122" |
232 | | - require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_000"), []byte(fmt.Sprintf(`https://discord.com/channels/%s/%s |
| 233 | + require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_000"), bytesf(`https://discord.com/channels/%s/%s |
233 | 234 | https://discord.com/api/v9/channels/%s/messages?limit=50 |
234 | 235 | {"id":"333333333333333346","channel_id":"%s","content":"mixed resolved message","timestamp":"2026-04-23T18:20:43Z","author":{"id":"222222222222222232","username":"alice"}} |
235 | 236 | {"id":"333333333333333347","channel_id":"%s","content":"mixed unresolved message","timestamp":"2026-04-23T18:20:44Z","author":{"id":"222222222222222232","username":"alice"}} |
236 | | -`, guildID, resolvedChannelID, unresolvedChannelID, resolvedChannelID, unresolvedChannelID)), 0o600)) |
| 237 | +`, guildID, resolvedChannelID, unresolvedChannelID, resolvedChannelID, unresolvedChannelID), 0o600)) |
237 | 238 |
|
238 | 239 | st, err := store.Open(ctx, filepath.Join(dir, "discrawl.db")) |
239 | 240 | require.NoError(t, err) |
@@ -267,10 +268,10 @@ func TestImportDoesNotDuplicateEventsWhenSwitchingFullCacheModes(t *testing.T) { |
267 | 268 |
|
268 | 269 | channelID := "111111111111111121" |
269 | 270 | guildID := "999999999999999996" |
270 | | - require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_000"), []byte(fmt.Sprintf(`https://discord.com/channels/%s/%s |
| 271 | + require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_000"), bytesf(`https://discord.com/channels/%s/%s |
271 | 272 | {"id":"%s","guild_id":"%s","type":0,"name":"mode-switch"} |
272 | 273 | {"id":"333333333333333346","channel_id":"%s","content":"mode switch event once","timestamp":"2026-04-23T18:20:43Z","author":{"id":"222222222222222232","username":"alice"}} |
273 | | -`, guildID, channelID, channelID, guildID, channelID)), 0o600)) |
| 274 | +`, guildID, channelID, channelID, guildID, channelID), 0o600)) |
274 | 275 |
|
275 | 276 | t.Run("full then default", func(t *testing.T) { |
276 | 277 | st, err := store.Open(ctx, filepath.Join(dir, "full-first.db")) |
@@ -319,14 +320,14 @@ func TestImportFastCachePreservesKnownChannelMetadataAcrossBatches(t *testing.T) |
319 | 320 |
|
320 | 321 | channelID := "111111111111111121" |
321 | 322 | guildID := "999999999999999996" |
322 | | - require.NoError(t, os.WriteFile(filepath.Join(leveldbPath, "000001.log"), []byte(fmt.Sprintf( |
| 323 | + require.NoError(t, os.WriteFile(filepath.Join(leveldbPath, "000001.log"), bytesf( |
323 | 324 | `{"id":"%s","guild_id":"%s","type":11,"name":"known-thread","thread_metadata":{"archived":false}}`, |
324 | 325 | channelID, |
325 | 326 | guildID, |
326 | | - )), 0o600)) |
327 | | - require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_0"), []byte(fmt.Sprintf(`https://discord.com/channels/%s/%s |
| 327 | + ), 0o600)) |
| 328 | + require.NoError(t, os.WriteFile(filepath.Join(cachePath, "entry_0"), bytesf(`https://discord.com/channels/%s/%s |
328 | 329 | {"id":"333333333333333346","channel_id":"%s","content":"thread metadata cache","timestamp":"2026-04-23T18:20:43Z","author":{"id":"222222222222222232","username":"alice"}} |
329 | | -`, guildID, channelID, channelID)), 0o600)) |
| 330 | +`, guildID, channelID, channelID), 0o600)) |
330 | 331 |
|
331 | 332 | st, err := store.Open(ctx, filepath.Join(dir, "discrawl.db")) |
332 | 333 | require.NoError(t, err) |
@@ -374,9 +375,13 @@ func TestImportFastCacheRouteFiltersServiceWorkerCacheStorage(t *testing.T) { |
374 | 375 |
|
375 | 376 | func requireMessageCount(t *testing.T, ctx context.Context, st *store.Store, table string, expected int) { |
376 | 377 | t.Helper() |
377 | | - _, rows, err := st.ReadOnlyQuery(ctx, fmt.Sprintf("select count(*) from %s", table)) |
| 378 | + _, rows, err := st.ReadOnlyQuery(ctx, "select count(*) from "+table) |
378 | 379 | require.NoError(t, err) |
379 | 380 | require.Len(t, rows, 1) |
380 | 381 | require.Len(t, rows[0], 1) |
381 | | - require.Equal(t, fmt.Sprint(expected), rows[0][0]) |
| 382 | + require.Equal(t, strconv.Itoa(expected), rows[0][0]) |
| 383 | +} |
| 384 | + |
| 385 | +func bytesf(format string, args ...any) []byte { |
| 386 | + return fmt.Appendf(nil, format, args...) |
382 | 387 | } |
0 commit comments