Skip to content

Commit 92968b2

Browse files
committed
improved test cases w/ previously stored embeddings
1 parent 13fea52 commit 92968b2

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/tests/PostTest.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,10 +1024,12 @@ describe('post tests', () => {
10241024
post1.user = seller;
10251025
post1.title = 'Electronics Device';
10261026
post1.description = 'A great electronic gadget';
1027+
post1.embedding = new Array(512).fill(0.1); // Mock embedding
10271028

10281029
post2.user = seller;
10291030
post2.title = 'Similar Electronic Item';
10301031
post2.description = 'Another electronic device';
1032+
post2.embedding = new Array(512).fill(0.12); // Similar mock embedding
10311033

10321034
// Create a completed transaction (purchase history)
10331035
const transaction = TransactionFactory.fakeTemplate();
@@ -1046,6 +1048,9 @@ describe('post tests', () => {
10461048

10471049
expect(response).toHaveProperty('postIds');
10481050
expect(Array.isArray(response.postIds)).toBe(true);
1051+
expect(response.postIds.length).toBeGreaterThan(0);
1052+
// Should include post2 since it's similar to purchased post1
1053+
expect(response.postIds).toContain(post2.id);
10491054
});
10501055

10511056
test('get purchase suggestions - custom limit parameter', async () => {
@@ -1054,6 +1059,7 @@ describe('post tests', () => {
10541059
const post = PostFactory.fake();
10551060

10561061
post.user = seller;
1062+
post.embedding = new Array(512).fill(0.1); // Mock embedding
10571063

10581064
// Create a completed transaction
10591065
const transaction = TransactionFactory.fakeTemplate();
@@ -1080,6 +1086,7 @@ describe('post tests', () => {
10801086
const post = PostFactory.fake();
10811087

10821088
post.user = seller;
1089+
post.embedding = new Array(512).fill(0.1); // Mock embedding
10831090

10841091
// Create an incomplete transaction (should not affect suggestions)
10851092
const transaction = TransactionFactory.fakeTemplate();
@@ -1105,35 +1112,54 @@ describe('post tests', () => {
11051112
const seller2 = UserFactory.fake();
11061113
const seller3 = UserFactory.fake();
11071114

1115+
// Create embeddings that represent semantic similarity
1116+
// Gaming/electronics posts will have similar embeddings (high values in first half)
1117+
const gamingEmbedding = new Array(512).fill(0);
1118+
for (let i = 0; i < 256; i++) gamingEmbedding[i] = 0.8; // Electronics features
1119+
1120+
const verySimilarEmbedding = new Array(512).fill(0);
1121+
for (let i = 0; i < 256; i++) verySimilarEmbedding[i] = 0.75; // Very similar to gaming
1122+
1123+
const somewhatSimilarEmbedding = new Array(512).fill(0);
1124+
for (let i = 0; i < 256; i++) somewhatSimilarEmbedding[i] = 0.5; // Somewhat similar
1125+
1126+
const differentEmbedding = new Array(512).fill(0);
1127+
for (let i = 256; i < 512; i++) differentEmbedding[i] = 0.8; // Different features (food/organic)
1128+
11081129
// User's purchased post (electronics)
11091130
const purchasedPost = PostFactory.fake();
11101131
purchasedPost.user = seller1;
11111132
purchasedPost.title = 'Gaming Laptop';
11121133
purchasedPost.description = 'High performance gaming laptop with RTX graphics card';
1134+
purchasedPost.embedding = gamingEmbedding;
11131135

11141136
// User's own post (should be excluded)
11151137
const ownPost = PostFactory.fake();
11161138
ownPost.user = buyer;
11171139
ownPost.title = 'My Gaming Setup';
11181140
ownPost.description = 'My personal gaming computer setup';
1141+
ownPost.embedding = gamingEmbedding.slice(); // Copy of gaming embedding
11191142

11201143
// Very similar post (should rank highest)
11211144
const verySimilarPost = PostFactory.fake();
11221145
verySimilarPost.user = seller2;
11231146
verySimilarPost.title = 'Gaming Computer';
11241147
verySimilarPost.description = 'Powerful gaming computer with high-end graphics card for gaming';
1148+
verySimilarPost.embedding = verySimilarEmbedding;
11251149

11261150
// Somewhat similar post (should rank lower)
11271151
const somewhatSimilarPost = PostFactory.fake();
11281152
somewhatSimilarPost.user = seller3;
11291153
somewhatSimilarPost.title = 'Electronics Device';
11301154
somewhatSimilarPost.description = 'Electronic gadget for tech enthusiasts';
1155+
somewhatSimilarPost.embedding = somewhatSimilarEmbedding;
11311156

11321157
// Completely different post (should rank lowest)
11331158
const differentPost = PostFactory.fake();
11341159
differentPost.user = seller3;
11351160
differentPost.title = 'Organic Vegetables';
11361161
differentPost.description = 'Fresh organic vegetables from local farm';
1162+
differentPost.embedding = differentEmbedding;
11371163

11381164
// Create a completed transaction (purchase history)
11391165
const transaction = TransactionFactory.fakeTemplate();
@@ -1174,32 +1200,57 @@ describe('post tests', () => {
11741200
const seller2 = UserFactory.fake();
11751201
const seller3 = UserFactory.fake();
11761202

1203+
// Create embeddings for electronics (high in first third)
1204+
const electronicsEmbedding = new Array(512).fill(0);
1205+
for (let i = 0; i < 170; i++) electronicsEmbedding[i] = 0.8;
1206+
1207+
// Create embeddings for books (high in middle third)
1208+
const bookEmbedding = new Array(512).fill(0);
1209+
for (let i = 170; i < 340; i++) bookEmbedding[i] = 0.8;
1210+
1211+
// Electronics recommendation (similar to electronics)
1212+
const electronicsRecEmbedding = new Array(512).fill(0);
1213+
for (let i = 0; i < 170; i++) electronicsRecEmbedding[i] = 0.75;
1214+
1215+
// Book recommendation (similar to books)
1216+
const bookRecEmbedding = new Array(512).fill(0);
1217+
for (let i = 170; i < 340; i++) bookRecEmbedding[i] = 0.75;
1218+
1219+
// Unrelated (high in last third)
1220+
const unrelatedEmbedding = new Array(512).fill(0);
1221+
for (let i = 340; i < 512; i++) unrelatedEmbedding[i] = 0.8;
1222+
11771223
// User's purchase history: electronics and books
11781224
const electronicsPost = PostFactory.fake();
11791225
electronicsPost.user = seller1;
11801226
electronicsPost.title = 'Smartphone Device';
11811227
electronicsPost.description = 'Latest smartphone with advanced features';
1228+
electronicsPost.embedding = electronicsEmbedding;
11821229

11831230
const bookPost = PostFactory.fake();
11841231
bookPost.user = seller1;
11851232
bookPost.title = 'Programming Book';
11861233
bookPost.description = 'Computer science textbook for learning programming';
1234+
bookPost.embedding = bookEmbedding;
11871235

11881236
// Available posts for recommendation
11891237
const electronicsRecommendation = PostFactory.fake();
11901238
electronicsRecommendation.user = seller2;
11911239
electronicsRecommendation.title = 'Tablet Computer';
11921240
electronicsRecommendation.description = 'Portable tablet device with touchscreen';
1241+
electronicsRecommendation.embedding = electronicsRecEmbedding;
11931242

11941243
const bookRecommendation = PostFactory.fake();
11951244
bookRecommendation.user = seller2;
11961245
bookRecommendation.title = 'Software Engineering';
11971246
bookRecommendation.description = 'Textbook about software development and programming';
1247+
bookRecommendation.embedding = bookRecEmbedding;
11981248

11991249
const unrelatedPost = PostFactory.fake();
12001250
unrelatedPost.user = seller3;
12011251
unrelatedPost.title = 'Kitchen Utensils';
12021252
unrelatedPost.description = 'Set of cooking tools and kitchen equipment';
1253+
unrelatedPost.embedding = unrelatedEmbedding;
12031254

12041255
// Create completed transactions (purchase history)
12051256
const transaction1 = TransactionFactory.fake();

src/tests/data/DatabaseConnection.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class DatabaseConnection {
1818
type: 'postgres',
1919
username: 'postgres',
2020
});
21+
// Ensure pgvector extension is installed for tests
22+
await DatabaseConnection.conn.query('CREATE EXTENSION IF NOT EXISTS vector');
2123
}
2224
return DatabaseConnection.conn;
2325
}

0 commit comments

Comments
 (0)