|
| 1 | +package datastore |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + "go.mongodb.org/mongo-driver/bson" |
| 12 | + "go.mongodb.org/mongo-driver/mongo" |
| 13 | +) |
| 14 | + |
| 15 | +func TestSummariesDAO_SaveAndGet(t *testing.T) { |
| 16 | + if _, ok := os.LookupEnv("ENABLE_MONGO_TESTS"); !ok { |
| 17 | + t.Skip("ENABLE_MONGO_TESTS env variable is not set") |
| 18 | + } |
| 19 | + |
| 20 | + mdb, err := New("mongodb://localhost:27017", "test_ureadability", 0) |
| 21 | + require.NoError(t, err) |
| 22 | + |
| 23 | + // create a unique collection for this test to avoid conflicts |
| 24 | + collection := mdb.client.Database(mdb.dbName).Collection("summaries_test") |
| 25 | + defer func() { |
| 26 | + _ = collection.Drop(context.Background()) |
| 27 | + }() |
| 28 | + |
| 29 | + // create an index on the expiresAt field |
| 30 | + _, err = collection.Indexes().CreateOne(context.Background(), |
| 31 | + mongo.IndexModel{ |
| 32 | + Keys: bson.D{{"expires_at", 1}}, |
| 33 | + }) |
| 34 | + require.NoError(t, err) |
| 35 | + |
| 36 | + dao := SummariesDAO{Collection: collection} |
| 37 | + |
| 38 | + content := "This is a test article content. It should generate a unique hash." |
| 39 | + summary := Summary{ |
| 40 | + Content: content, |
| 41 | + Summary: "This is a test summary of the article.", |
| 42 | + Model: "gpt-4o-mini", |
| 43 | + CreatedAt: time.Now(), |
| 44 | + } |
| 45 | + |
| 46 | + // test saving a summary |
| 47 | + err = dao.Save(context.Background(), summary) |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + // test getting the summary |
| 51 | + foundSummary, found := dao.Get(context.Background(), content) |
| 52 | + assert.True(t, found) |
| 53 | + assert.Equal(t, summary.Summary, foundSummary.Summary) |
| 54 | + assert.Equal(t, summary.Model, foundSummary.Model) |
| 55 | + assert.NotEmpty(t, foundSummary.ID) |
| 56 | + |
| 57 | + // test getting a non-existent summary |
| 58 | + _, found = dao.Get(context.Background(), "non-existent content") |
| 59 | + assert.False(t, found) |
| 60 | + |
| 61 | + // test updating an existing summary |
| 62 | + updatedSummary := Summary{ |
| 63 | + ID: foundSummary.ID, |
| 64 | + Content: content, |
| 65 | + Summary: "This is an updated summary.", |
| 66 | + Model: "gpt-4o-mini", |
| 67 | + CreatedAt: foundSummary.CreatedAt, |
| 68 | + } |
| 69 | + |
| 70 | + err = dao.Save(context.Background(), updatedSummary) |
| 71 | + require.NoError(t, err) |
| 72 | + |
| 73 | + foundSummary, found = dao.Get(context.Background(), content) |
| 74 | + assert.True(t, found) |
| 75 | + assert.Equal(t, "This is an updated summary.", foundSummary.Summary) |
| 76 | + assert.Equal(t, updatedSummary.CreatedAt, foundSummary.CreatedAt) |
| 77 | + assert.NotEqual(t, updatedSummary.UpdatedAt, foundSummary.UpdatedAt) // UpdatedAt should be set by the DAO |
| 78 | + |
| 79 | + // test deleting a summary |
| 80 | + err = dao.Delete(context.Background(), foundSummary.ID) |
| 81 | + require.NoError(t, err) |
| 82 | + |
| 83 | + _, found = dao.Get(context.Background(), content) |
| 84 | + assert.False(t, found) |
| 85 | +} |
| 86 | + |
| 87 | +func TestGenerateContentHash(t *testing.T) { |
| 88 | + content1 := "This is a test content." |
| 89 | + content2 := "This is a different test content." |
| 90 | + |
| 91 | + hash1 := GenerateContentHash(content1) |
| 92 | + hash2 := GenerateContentHash(content2) |
| 93 | + |
| 94 | + assert.NotEqual(t, hash1, hash2) |
| 95 | + assert.Equal(t, hash1, GenerateContentHash(content1)) // same content should produce same hash |
| 96 | + assert.Equal(t, 64, len(hash1)) // SHA-256 produces 64 character hex string |
| 97 | +} |
| 98 | + |
| 99 | +func TestSummariesDAO_CleanupExpired(t *testing.T) { |
| 100 | + if _, ok := os.LookupEnv("ENABLE_MONGO_TESTS"); !ok { |
| 101 | + t.Skip("ENABLE_MONGO_TESTS env variable is not set") |
| 102 | + } |
| 103 | + |
| 104 | + mdb, err := New("mongodb://localhost:27017", "test_ureadability", 0) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + // create a unique collection for this test to avoid conflicts |
| 108 | + collection := mdb.client.Database(mdb.dbName).Collection("summaries_expired_test") |
| 109 | + defer func() { |
| 110 | + _ = collection.Drop(context.Background()) |
| 111 | + }() |
| 112 | + |
| 113 | + // create an index on the expiresAt field |
| 114 | + _, err = collection.Indexes().CreateOne(context.Background(), |
| 115 | + mongo.IndexModel{ |
| 116 | + Keys: bson.D{{"expires_at", 1}}, |
| 117 | + }) |
| 118 | + require.NoError(t, err) |
| 119 | + |
| 120 | + dao := SummariesDAO{Collection: collection} |
| 121 | + ctx := context.Background() |
| 122 | + |
| 123 | + // add expired summary |
| 124 | + expiredSummary := Summary{ |
| 125 | + Content: "This is an expired summary", |
| 126 | + Summary: "Expired content", |
| 127 | + Model: "gpt-4o-mini", |
| 128 | + CreatedAt: time.Now().Add(-48 * time.Hour), |
| 129 | + UpdatedAt: time.Now().Add(-48 * time.Hour), |
| 130 | + ExpiresAt: time.Now().Add(-24 * time.Hour), // expired 24 hours ago |
| 131 | + } |
| 132 | + err = dao.Save(ctx, expiredSummary) |
| 133 | + require.NoError(t, err) |
| 134 | + |
| 135 | + // add valid summary |
| 136 | + validSummary := Summary{ |
| 137 | + Content: "This is a valid summary", |
| 138 | + Summary: "Valid content", |
| 139 | + Model: "gpt-4o-mini", |
| 140 | + CreatedAt: time.Now(), |
| 141 | + UpdatedAt: time.Now(), |
| 142 | + ExpiresAt: time.Now().Add(24 * time.Hour), // expires in 24 hours |
| 143 | + } |
| 144 | + err = dao.Save(ctx, validSummary) |
| 145 | + require.NoError(t, err) |
| 146 | + |
| 147 | + // verify both summaries exist |
| 148 | + _, foundExpired := dao.Get(ctx, expiredSummary.Content) |
| 149 | + assert.True(t, foundExpired, "Expected to find expired summary before cleanup") |
| 150 | + |
| 151 | + _, foundValid := dao.Get(ctx, validSummary.Content) |
| 152 | + assert.True(t, foundValid, "Expected to find valid summary before cleanup") |
| 153 | + |
| 154 | + // run cleanup |
| 155 | + count, err := dao.CleanupExpired(ctx) |
| 156 | + require.NoError(t, err) |
| 157 | + assert.Equal(t, int64(1), count, "Expected to clean up exactly one record") |
| 158 | + |
| 159 | + // verify expired summary is gone but valid remains |
| 160 | + _, foundExpired = dao.Get(ctx, expiredSummary.Content) |
| 161 | + assert.False(t, foundExpired, "Expected expired summary to be deleted") |
| 162 | + |
| 163 | + _, foundValid = dao.Get(ctx, validSummary.Content) |
| 164 | + assert.True(t, foundValid, "Expected valid summary to still exist") |
| 165 | +} |
0 commit comments