From 4f0d289fe48b5210a8d764cd84e6b2d1970c971e Mon Sep 17 00:00:00 2001 From: Tirth Patel Date: Mon, 19 Jan 2026 16:54:28 -0600 Subject: [PATCH] fix: wait for child containers before creating carousel CreateCarouselPost now polls each child container until it reaches FINISHED status before creating the carousel container. This fixes flaky integration tests where the API would reject carousel creation with "Invalid Carousel Children" errors due to children not being ready yet. Removed unnecessary time.Sleep(1 * time.Second) workarounds from integration tests since the library now handles this properly. Co-Authored-By: Claude Opus 4.5 --- posts_create.go | 8 ++++++++ tests/integration/integration_test.go | 5 ++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/posts_create.go b/posts_create.go index 7c9001b..fc389f1 100644 --- a/posts_create.go +++ b/posts_create.go @@ -138,6 +138,14 @@ func (c *Client) CreateCarouselPost(ctx context.Context, content *CarouselPostCo return nil, err } + // Wait for all child containers to be ready before creating the carousel container + // The Threads API requires child containers to be in FINISHED status + for i, childID := range content.Children { + if err := c.waitForContainerReady(ctx, ContainerID(childID), DefaultContainerPollMaxAttempts, DefaultContainerPollInterval); err != nil { + return nil, fmt.Errorf("child container %d (%s) not ready: %w", i+1, childID, err) + } + } + // Create container first containerID, err := c.createCarouselContainer(ctx, content) if err != nil { diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 872d008..a079671 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -341,8 +341,7 @@ func TestIntegration_PostOperations(t *testing.T) { } t.Logf("Created second media container: %s", container2) - // Wait a moment for containers to be ready - time.Sleep(1 * time.Second) + // Note: No sleep needed - CreateCarouselPost waits for child containers to be ready // Create carousel post content := &threads.CarouselPostContent{ @@ -681,7 +680,7 @@ func TestIntegration_SpoilersAndTextAttachments(t *testing.T) { return } - time.Sleep(1 * time.Second) + // Note: No sleep needed - CreateCarouselPost waits for child containers to be ready // Create carousel with all media marked as spoilers content := &threads.CarouselPostContent{