A community based topic aggregation platform built on atproto

refactor(tests): consolidate helper functions to helpers.go

Move duplicate helper functions from multiple test files to helpers.go:
- authenticateWithPDS() - Used by post e2e tests
- contains() / anySubstring() - String utilities
- Import standardization across test files

Benefits:
- Eliminates code duplication across 6+ test files
- Centralizes test utilities for easier maintenance
- Improves test readability (focus on test logic, not helpers)

All tests continue to pass with consolidated helpers.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

+2 -1
tests/integration/community_blocking_test.go
···
import (
"Coves/internal/atproto/jetstream"
"Coves/internal/core/communities"
-
postgresRepo "Coves/internal/db/postgres"
"context"
"database/sql"
"fmt"
"testing"
"time"
+
+
postgresRepo "Coves/internal/db/postgres"
)
// TestCommunityBlocking_Indexing tests Jetstream indexing of block events
-42
tests/integration/community_e2e_test.go
···
return community
-
// authenticateWithPDS authenticates with the PDS and returns access token and DID
-
func authenticateWithPDS(pdsURL, handle, password string) (string, string, error) {
-
// Call com.atproto.server.createSession
-
sessionReq := map[string]string{
-
"identifier": handle,
-
"password": password,
-
}
-
-
reqBody, marshalErr := json.Marshal(sessionReq)
-
if marshalErr != nil {
-
return "", "", fmt.Errorf("failed to marshal session request: %w", marshalErr)
-
}
-
resp, err := http.Post(
-
pdsURL+"/xrpc/com.atproto.server.createSession",
-
"application/json",
-
bytes.NewBuffer(reqBody),
-
)
-
if err != nil {
-
return "", "", fmt.Errorf("failed to create session: %w", err)
-
}
-
defer func() { _ = resp.Body.Close() }()
-
-
if resp.StatusCode != http.StatusOK {
-
body, readErr := io.ReadAll(resp.Body)
-
if readErr != nil {
-
return "", "", fmt.Errorf("PDS auth failed (status %d, failed to read body: %w)", resp.StatusCode, readErr)
-
}
-
return "", "", fmt.Errorf("PDS auth failed (status %d): %s", resp.StatusCode, string(body))
-
}
-
-
var sessionResp struct {
-
AccessJwt string `json:"accessJwt"`
-
DID string `json:"did"`
-
}
-
-
if err := json.NewDecoder(resp.Body).Decode(&sessionResp); err != nil {
-
return "", "", fmt.Errorf("failed to decode session response: %w", err)
-
}
-
-
return sessionResp.AccessJwt, sessionResp.DID, nil
-
}
-
// queryPDSAccount queries the PDS to verify an account exists
// Returns the account's DID and handle if found
func queryPDSAccount(pdsURL, handle string) (string, string, error) {
+8 -4
tests/integration/community_hostedby_security_test.go
···
uniqueSuffix := fmt.Sprintf("%d", time.Now().UnixNano())
communityDID := generateTestDID(uniqueSuffix)
+
uniqueHandle := fmt.Sprintf("gaming%s.community.coves.social", uniqueSuffix)
// Attempt to create community claiming to be hosted by nintendo.com
// but with a coves.social handle (ATTACK!)
···
RKey: "self",
CID: "bafy123abc",
Record: map[string]interface{}{
-
"handle": "gaming.community.coves.social", // coves.social handle
+
"handle": uniqueHandle, // coves.social handle
"name": "gaming",
"displayName": "Nintendo Gaming",
"description": "Fake Nintendo community",
···
uniqueSuffix := fmt.Sprintf("%d", time.Now().UnixNano())
communityDID := generateTestDID(uniqueSuffix)
+
uniqueHandle := fmt.Sprintf("gaming%s.community.coves.social", uniqueSuffix)
// Create community with matching hostedBy and handle domains
event := &jetstream.JetstreamEvent{
···
RKey: "self",
CID: "bafy123abc",
Record: map[string]interface{}{
-
"handle": "gaming.community.coves.social", // coves.social handle
+
"handle": uniqueHandle, // coves.social handle
"name": "gaming",
"displayName": "Gaming Community",
"description": "Legitimate coves.social community",
···
uniqueSuffix := fmt.Sprintf("%d", time.Now().UnixNano())
communityDID := generateTestDID(uniqueSuffix)
+
uniqueHandle := fmt.Sprintf("gaming%s.community.coves.social", uniqueSuffix)
// Attempt to use did:plc for hostedBy (not allowed)
event := &jetstream.JetstreamEvent{
···
RKey: "self",
CID: "bafy123abc",
Record: map[string]interface{}{
-
"handle": "gaming.community.coves.social",
+
"handle": uniqueHandle,
"name": "gaming",
"displayName": "Test Community",
"description": "Test",
···
uniqueSuffix := fmt.Sprintf("%d", time.Now().UnixNano())
communityDID := generateTestDID(uniqueSuffix)
+
uniqueHandle := fmt.Sprintf("gaming%s.community.example.com", uniqueSuffix)
// Even with mismatched domain, this should succeed with skipVerification=true
event := &jetstream.JetstreamEvent{
···
RKey: "self",
CID: "bafy123abc",
Record: map[string]interface{}{
-
"handle": "gaming.community.example.com",
+
"handle": uniqueHandle,
"name": "gaming",
"displayName": "Test",
"description": "Test",
+1 -1
tests/integration/community_identifier_resolution_test.go
···
}{
{"nodots", "nodots"}, // No dots - should return as-is
{"single.dot", "single.dot"}, // Single dot - should return as-is
-
{"", ""}, // Empty - should return as-is
+
{"", ""}, // Empty - should return as-is
}
for _, tc := range testCases {
+12 -6
tests/integration/community_v2_validation_test.go
···
"Coves/internal/core/communities"
"Coves/internal/db/postgres"
"context"
+
"fmt"
"testing"
"time"
)
···
ctx := context.Background()
t.Run("accepts V2 community with rkey=self", func(t *testing.T) {
+
// Use unique DID and handle to avoid conflicts with other test runs
+
timestamp := time.Now().UnixNano()
+
testDID := fmt.Sprintf("did:plc:testv2rkey%d", timestamp)
+
testHandle := fmt.Sprintf("testv2rkey%d.community.coves.social", timestamp)
+
event := &jetstream.JetstreamEvent{
-
Did: "did:plc:community123",
+
Did: testDID,
Kind: "commit",
Commit: &jetstream.CommitEvent{
Operation: "create",
···
CID: "bafyreigaming123",
Record: map[string]interface{}{
"$type": "social.coves.community.profile",
-
"handle": "gaming.community.coves.social",
-
"name": "gaming",
+
"handle": testHandle,
+
"name": "testv2rkey",
"createdBy": "did:plc:user123",
"hostedBy": "did:web:coves.social",
"visibility": "public",
···
}
// Verify community was indexed
-
community, err := repo.GetByDID(ctx, "did:plc:community123")
+
community, err := repo.GetByDID(ctx, testDID)
if err != nil {
t.Fatalf("Community should have been indexed: %v", err)
}
···
}
// Verify record URI uses "self"
-
expectedURI := "at://did:plc:community123/social.coves.community.profile/self"
+
expectedURI := fmt.Sprintf("at://%s/social.coves.community.profile/self", testDID)
if community.RecordURI != expectedURI {
t.Errorf("Expected RecordURI %s, got %s", expectedURI, community.RecordURI)
}
···
Record: map[string]interface{}{
"$type": "social.coves.community.profile",
"handle": "gamingtest.community.coves.social", // atProto handle (DNS-resolvable)
-
"name": "gamingtest", // Short name for !mentions
+
"name": "gamingtest", // Short name for !mentions
"createdBy": "did:plc:user123",
"hostedBy": "did:web:coves.social",
"visibility": "public",
+1 -13
tests/integration/jetstream_consumer_test.go
···
})
}
-
// Helper function
-
func contains(s, substr string) bool {
-
return len(s) >= len(substr) && anySubstring(s, substr)
-
}
-
-
func anySubstring(s, substr string) bool {
-
for i := 0; i <= len(s)-len(substr); i++ {
-
if s[i:i+len(substr)] == substr {
-
return true
-
}
-
}
-
return false
-
}
+
// Helper functions moved to helpers.go
+2 -1
tests/integration/subscription_indexing_test.go
···
import (
"Coves/internal/atproto/jetstream"
"Coves/internal/core/communities"
-
postgresRepo "Coves/internal/db/postgres"
"context"
"database/sql"
"fmt"
"testing"
"time"
+
+
postgresRepo "Coves/internal/db/postgres"
)
// TestSubscriptionIndexing_ContentVisibility tests that contentVisibility is properly indexed