|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/google/uuid" |
| 9 | + "github.com/riverqueue/river" |
| 10 | + "github.com/riverqueue/river/rivertype" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "github.com/formbricks/hub/internal/config" |
| 15 | + "github.com/formbricks/hub/internal/models" |
| 16 | + "github.com/formbricks/hub/internal/repository" |
| 17 | + "github.com/formbricks/hub/internal/service" |
| 18 | + "github.com/formbricks/hub/pkg/database" |
| 19 | +) |
| 20 | + |
| 21 | +// countingEmbeddingInserter records the FeedbackEmbeddingArgs jobs enqueued. |
| 22 | +type countingEmbeddingInserter struct { |
| 23 | + ids []uuid.UUID |
| 24 | +} |
| 25 | + |
| 26 | +func (c *countingEmbeddingInserter) Insert( |
| 27 | + _ context.Context, args river.JobArgs, _ *river.InsertOpts, |
| 28 | +) (*rivertype.JobInsertResult, error) { |
| 29 | + if a, ok := args.(service.FeedbackEmbeddingArgs); ok { |
| 30 | + c.ids = append(c.ids, a.FeedbackRecordID) |
| 31 | + } |
| 32 | + |
| 33 | + return &rivertype.JobInsertResult{}, nil |
| 34 | +} |
| 35 | + |
| 36 | +func embeddingBackfillRepos(t *testing.T) (*repository.FeedbackRecordsRepository, *repository.EmbeddingsRepository) { |
| 37 | + t.Helper() |
| 38 | + |
| 39 | + cfg, err := config.Load() |
| 40 | + require.NoError(t, err) |
| 41 | + |
| 42 | + db, err := database.NewPostgresPool( |
| 43 | + context.Background(), cfg.Database.URL, database.WithPoolConfig(cfg.Database.PoolConfig())) |
| 44 | + require.NoError(t, err) |
| 45 | + |
| 46 | + t.Cleanup(db.Close) |
| 47 | + |
| 48 | + return repository.NewFeedbackRecordsRepository(db), repository.NewEmbeddingsRepository(db) |
| 49 | +} |
| 50 | + |
| 51 | +// TestListFeedbackRecordIDsForBackfill_KeysetPagination locks the new keyset behavior of the |
| 52 | +// embedding backfill query: each page is bounded by the limit, ids are returned strictly |
| 53 | +// ascending with no id on two pages, and paging from uuid.Nil eventually returns every record |
| 54 | +// that needs an embedding. A fresh model is used so the created records are all eligible. |
| 55 | +func TestListFeedbackRecordIDsForBackfill_KeysetPagination(t *testing.T) { |
| 56 | + ctx := context.Background() |
| 57 | + feedbackRepo, embeddingsRepo := embeddingBackfillRepos(t) |
| 58 | + |
| 59 | + model := "backfill-keyset-" + uuid.NewString() |
| 60 | + tenant := uuid.NewString() |
| 61 | + |
| 62 | + makeText := func(value string) uuid.UUID { |
| 63 | + rec, err := feedbackRepo.Create(ctx, &models.CreateFeedbackRecordRequest{ |
| 64 | + SourceType: "formbricks", |
| 65 | + SubmissionID: uuid.NewString(), |
| 66 | + TenantID: tenant, |
| 67 | + FieldID: "q1", |
| 68 | + FieldType: models.FieldTypeText, |
| 69 | + ValueText: &value, |
| 70 | + }) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + return rec.ID |
| 74 | + } |
| 75 | + |
| 76 | + mine := map[uuid.UUID]bool{} |
| 77 | + for _, value := range []string{"one", "two", "three", "four", "five"} { |
| 78 | + mine[makeText(value)] = true |
| 79 | + } |
| 80 | + |
| 81 | + seen := map[uuid.UUID]bool{} |
| 82 | + afterID := uuid.Nil |
| 83 | + |
| 84 | + var prev uuid.UUID |
| 85 | + |
| 86 | + hasPrev := false |
| 87 | + |
| 88 | + for { |
| 89 | + page, err := embeddingsRepo.ListFeedbackRecordIDsForBackfill(ctx, model, afterID, 2) |
| 90 | + require.NoError(t, err) |
| 91 | + require.LessOrEqual(t, len(page), 2, "LIMIT bounds the page size") |
| 92 | + |
| 93 | + if len(page) == 0 { |
| 94 | + break |
| 95 | + } |
| 96 | + |
| 97 | + for _, id := range page { |
| 98 | + require.False(t, seen[id], "an id must not appear on two pages") |
| 99 | + seen[id] = true |
| 100 | + |
| 101 | + if hasPrev { |
| 102 | + require.Negativef(t, bytes.Compare(prev[:], id[:]), "ids are returned strictly ascending") |
| 103 | + } |
| 104 | + |
| 105 | + prev = id |
| 106 | + hasPrev = true |
| 107 | + } |
| 108 | + |
| 109 | + afterID = page[len(page)-1] |
| 110 | + |
| 111 | + if len(page) < 2 { |
| 112 | + break |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + for id := range mine { |
| 117 | + assert.Truef(t, seen[id], "record %s needing an embedding must be returned across pages", id) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// TestBackfillEmbeddings_StreamsAllEligible drives the service backfill against the real DB |
| 122 | +// with a recording inserter and asserts it enqueues an embedding job for every record that |
| 123 | +// needs one (a fresh model makes the created records eligible). |
| 124 | +func TestBackfillEmbeddings_StreamsAllEligible(t *testing.T) { |
| 125 | + ctx := context.Background() |
| 126 | + feedbackRepo, embeddingsRepo := embeddingBackfillRepos(t) |
| 127 | + |
| 128 | + model := "backfill-stream-" + uuid.NewString() |
| 129 | + tenant := uuid.NewString() |
| 130 | + |
| 131 | + makeText := func(value string) uuid.UUID { |
| 132 | + rec, err := feedbackRepo.Create(ctx, &models.CreateFeedbackRecordRequest{ |
| 133 | + SourceType: "formbricks", |
| 134 | + SubmissionID: uuid.NewString(), |
| 135 | + TenantID: tenant, |
| 136 | + FieldID: "q1", |
| 137 | + FieldType: models.FieldTypeText, |
| 138 | + ValueText: &value, |
| 139 | + }) |
| 140 | + require.NoError(t, err) |
| 141 | + |
| 142 | + return rec.ID |
| 143 | + } |
| 144 | + |
| 145 | + mine := []uuid.UUID{makeText("one"), makeText("two"), makeText("three")} |
| 146 | + |
| 147 | + inserter := &countingEmbeddingInserter{} |
| 148 | + svc := service.NewFeedbackRecordsService(feedbackRepo, embeddingsRepo, model, nil, inserter, "embeddings", 3) |
| 149 | + |
| 150 | + enqueued, err := svc.BackfillEmbeddings(ctx, model) |
| 151 | + require.NoError(t, err) |
| 152 | + require.GreaterOrEqual(t, enqueued, len(mine)) |
| 153 | + |
| 154 | + got := map[uuid.UUID]bool{} |
| 155 | + for _, id := range inserter.ids { |
| 156 | + got[id] = true |
| 157 | + } |
| 158 | + |
| 159 | + for _, id := range mine { |
| 160 | + assert.Truef(t, got[id], "record %s needing an embedding must be enqueued", id) |
| 161 | + } |
| 162 | +} |
0 commit comments