|
1 | 1 | import fs from "node:fs"; |
2 | 2 | import path from "node:path"; |
3 | 3 | import { jest } from "@jest/globals"; |
| 4 | +import { createMintBatch, TEST_MINTER_1 } from "../helpers"; |
4 | 5 |
|
5 | 6 | // Mock env |
6 | 7 | process.env.TWITTER_EVENTS = "sale,listing,offer,transfer,burn"; |
@@ -352,6 +353,191 @@ describe("twitter flows", () => { |
352 | 353 | ) |
353 | 354 | ).toBe(true); |
354 | 355 | }); |
| 356 | + |
| 357 | + it("retries on 429 rate limit errors from Twitter and eventually tweets", async () => { |
| 358 | + const originalEventsEnv = process.env.TWITTER_EVENTS; |
| 359 | + try { |
| 360 | + process.env.TWITTER_EVENTS = "mint"; |
| 361 | + process.env.TWITTER_QUEUE_DELAY_MS = "0"; |
| 362 | + process.env.TWITTER_BACKOFF_BASE_MS = "1"; |
| 363 | + process.env.TWITTER_BACKOFF_MAX_MS = "5"; |
| 364 | + |
| 365 | + const baseTimestamp = 3_000_000_000; |
| 366 | + const [mintEvent] = createMintBatch(1, TEST_MINTER_1, baseTimestamp); |
| 367 | + |
| 368 | + const m = require("twitter-api-v2") as { |
| 369 | + __mockReadWrite: { v2: { tweet: jest.Mock } }; |
| 370 | + }; |
| 371 | + const tweetMock = m.__mockReadWrite.v2.tweet as jest.Mock; |
| 372 | + |
| 373 | + let firstCall = true; |
| 374 | + tweetMock.mockImplementation(() => { |
| 375 | + if (firstCall) { |
| 376 | + firstCall = false; |
| 377 | + const error = { |
| 378 | + code: 429, |
| 379 | + rateLimit: { |
| 380 | + day: { |
| 381 | + remaining: 0, |
| 382 | + reset: Math.floor(Date.now() / 1000), |
| 383 | + }, |
| 384 | + }, |
| 385 | + }; |
| 386 | + throw error; |
| 387 | + } |
| 388 | + return Promise.resolve({ data: { id: "1", text: "ok" } }); |
| 389 | + }); |
| 390 | + |
| 391 | + const { tweetEvents } = await import("../../src/platforms/twitter"); |
| 392 | + tweetEvents([ |
| 393 | + mintEvent, |
| 394 | + ] as unknown as import("../../src/types").OpenSeaAssetEvent[]); |
| 395 | + |
| 396 | + await jest.runAllTimersAsync(); |
| 397 | + expect(tweetMock).toHaveBeenCalledTimes(2); |
| 398 | + } finally { |
| 399 | + process.env.TWITTER_EVENTS = originalEventsEnv; |
| 400 | + } |
| 401 | + }); |
| 402 | + |
| 403 | + it("retries transient 5xx errors from Twitter and eventually tweets", async () => { |
| 404 | + const originalEventsEnv = process.env.TWITTER_EVENTS; |
| 405 | + try { |
| 406 | + process.env.TWITTER_EVENTS = "mint"; |
| 407 | + process.env.TWITTER_QUEUE_DELAY_MS = "0"; |
| 408 | + process.env.TWITTER_BACKOFF_BASE_MS = "1"; |
| 409 | + process.env.TWITTER_BACKOFF_MAX_MS = "5"; |
| 410 | + |
| 411 | + const baseTimestamp = 3_100_000_000; |
| 412 | + const [mintEvent] = createMintBatch(1, TEST_MINTER_1, baseTimestamp); |
| 413 | + |
| 414 | + const m = require("twitter-api-v2") as { |
| 415 | + __mockReadWrite: { v2: { tweet: jest.Mock } }; |
| 416 | + }; |
| 417 | + const tweetMock = m.__mockReadWrite.v2.tweet as jest.Mock; |
| 418 | + |
| 419 | + let attempts = 0; |
| 420 | + tweetMock.mockImplementation(() => { |
| 421 | + attempts += 1; |
| 422 | + if (attempts < 3) { |
| 423 | + const error = { status: 503 }; |
| 424 | + throw error; |
| 425 | + } |
| 426 | + return Promise.resolve({ data: { id: "1", text: "ok" } }); |
| 427 | + }); |
| 428 | + |
| 429 | + const { tweetEvents } = await import("../../src/platforms/twitter"); |
| 430 | + tweetEvents([ |
| 431 | + mintEvent, |
| 432 | + ] as unknown as import("../../src/types").OpenSeaAssetEvent[]); |
| 433 | + |
| 434 | + await jest.runAllTimersAsync(); |
| 435 | + expect(tweetMock).toHaveBeenCalledTimes(3); |
| 436 | + } finally { |
| 437 | + process.env.TWITTER_EVENTS = originalEventsEnv; |
| 438 | + } |
| 439 | + }); |
| 440 | + |
| 441 | + it("drops fatal 4xx errors from Twitter without infinite retries", async () => { |
| 442 | + const originalEventsEnv = process.env.TWITTER_EVENTS; |
| 443 | + try { |
| 444 | + process.env.TWITTER_EVENTS = "mint"; |
| 445 | + process.env.TWITTER_QUEUE_DELAY_MS = "0"; |
| 446 | + process.env.TWITTER_BACKOFF_BASE_MS = "1"; |
| 447 | + process.env.TWITTER_BACKOFF_MAX_MS = "5"; |
| 448 | + |
| 449 | + const baseTimestamp = 3_200_000_000; |
| 450 | + const [mintEvent] = createMintBatch(1, TEST_MINTER_1, baseTimestamp); |
| 451 | + |
| 452 | + const m = require("twitter-api-v2") as { |
| 453 | + __mockReadWrite: { v2: { tweet: jest.Mock } }; |
| 454 | + }; |
| 455 | + const tweetMock = m.__mockReadWrite.v2.tweet as jest.Mock; |
| 456 | + |
| 457 | + tweetMock.mockImplementation(() => { |
| 458 | + const error = { status: 400 }; |
| 459 | + throw error; |
| 460 | + }); |
| 461 | + |
| 462 | + const { tweetEvents } = await import("../../src/platforms/twitter"); |
| 463 | + tweetEvents([ |
| 464 | + mintEvent, |
| 465 | + ] as unknown as import("../../src/types").OpenSeaAssetEvent[]); |
| 466 | + |
| 467 | + await jest.runAllTimersAsync(); |
| 468 | + expect(tweetMock).toHaveBeenCalledTimes(1); |
| 469 | + } finally { |
| 470 | + process.env.TWITTER_EVENTS = originalEventsEnv; |
| 471 | + } |
| 472 | + }); |
| 473 | + |
| 474 | + it("tweets single mint events when below group size threshold", async () => { |
| 475 | + const originalEventsEnv = process.env.TWITTER_EVENTS; |
| 476 | + try { |
| 477 | + process.env.TWITTER_EVENTS = "mint"; |
| 478 | + process.env.TWITTER_EVENT_GROUP_SETTLE_MS = "60000"; |
| 479 | + process.env.TWITTER_EVENT_GROUP_MIN_GROUP_SIZE = "3"; |
| 480 | + process.env.TWITTER_QUEUE_DELAY_MS = "0"; |
| 481 | + |
| 482 | + const baseTimestamp = 1_234_567_890; |
| 483 | + const mintEvents = createMintBatch(2, TEST_MINTER_1, baseTimestamp); |
| 484 | + |
| 485 | + const { tweetEvents } = await import("../../src/platforms/twitter"); |
| 486 | + tweetEvents(mintEvents); |
| 487 | + |
| 488 | + const m = require("twitter-api-v2") as { |
| 489 | + __mockReadWrite: { v2: { tweet: jest.Mock } }; |
| 490 | + }; |
| 491 | + |
| 492 | + await jest.runAllTimersAsync(); |
| 493 | + const calls = (m.__mockReadWrite.v2.tweet as jest.Mock).mock.calls; |
| 494 | + |
| 495 | + // Below minGroupSize, mints should be tweeted individually |
| 496 | + expect(calls.length).toBe(2); |
| 497 | + } finally { |
| 498 | + process.env.TWITTER_EVENTS = originalEventsEnv; |
| 499 | + } |
| 500 | + }); |
| 501 | + |
| 502 | + it("tweets multiple actor-based mint groups for the same minter over time", async () => { |
| 503 | + const originalEventsEnv = process.env.TWITTER_EVENTS; |
| 504 | + try { |
| 505 | + process.env.TWITTER_EVENTS = "mint"; |
| 506 | + process.env.TWITTER_EVENT_GROUP_SETTLE_MS = "0"; |
| 507 | + process.env.TWITTER_EVENT_GROUP_MIN_GROUP_SIZE = "2"; |
| 508 | + process.env.TWITTER_QUEUE_DELAY_MS = "0"; |
| 509 | + |
| 510 | + const baseTimestamp = 2_000_000_000; |
| 511 | + const firstBatch = createMintBatch(2, TEST_MINTER_1, baseTimestamp); |
| 512 | + const secondBatch = createMintBatch( |
| 513 | + 2, |
| 514 | + TEST_MINTER_1, |
| 515 | + baseTimestamp + 100 |
| 516 | + ); |
| 517 | + |
| 518 | + const { tweetEvents } = await import("../../src/platforms/twitter"); |
| 519 | + |
| 520 | + // First group for this minter |
| 521 | + tweetEvents(firstBatch); |
| 522 | + const m = require("twitter-api-v2") as { |
| 523 | + __mockReadWrite: { v2: { tweet: jest.Mock } }; |
| 524 | + }; |
| 525 | + await jest.runAllTimersAsync(); |
| 526 | + let calls = (m.__mockReadWrite.v2.tweet as jest.Mock).mock.calls; |
| 527 | + expect(calls.length).toBe(1); |
| 528 | + |
| 529 | + // Second independent group for the same minter should also tweet |
| 530 | + tweetEvents(secondBatch); |
| 531 | + await jest.runAllTimersAsync(); |
| 532 | + calls = (m.__mockReadWrite.v2.tweet as jest.Mock).mock.calls; |
| 533 | + |
| 534 | + // Previously this would stay at 1 due to actor-based queue key dedupe. |
| 535 | + // With the updated keying (including timestamp window), we expect 2. |
| 536 | + expect(calls.length).toBe(2); |
| 537 | + } finally { |
| 538 | + process.env.TWITTER_EVENTS = originalEventsEnv; |
| 539 | + } |
| 540 | + }); |
355 | 541 | }); |
356 | 542 |
|
357 | 543 | // Add basic tests for matchesSelection mint/burn classification |
|
0 commit comments