fix(): Event group id propagation and event managements (#12157)

This commit is contained in:
Adrien de Peretti
2025-04-14 20:57:52 +02:00
committed by GitHub
parent 3a481290ea
commit 2f6963a5fb
22 changed files with 777 additions and 592 deletions

View File

@@ -1,11 +1,11 @@
import { IEventBusModuleService } from "@medusajs/types"
import { CommonEvents, Modules } from "@medusajs/utils"
import FormData from "form-data"
import fs from "fs/promises"
import {
medusaIntegrationTestRunner,
TestEventUtils,
} from "@medusajs/test-utils"
import { IEventBusModuleService } from "@medusajs/types"
import { CommonEvents, Modules } from "@medusajs/utils"
import FormData from "form-data"
import fs from "fs/promises"
import path from "path"
import {
adminHeaders,

View File

@@ -74,13 +74,9 @@ async function populateData(api: any) {
},
]
await api
.post("/admin/products/batch", { create: payload }, adminHeaders)
.catch((err) => {
console.log(err)
})
await api.post("/admin/products/batch", { create: payload }, adminHeaders)
await setTimeout(10000)
await setTimeout(4000)
}
process.env.ENABLE_INDEX_MODULE = "true"
@@ -97,7 +93,7 @@ medusaIntegrationTestRunner({
process.env.ENABLE_INDEX_MODULE = "false"
})
describe.skip("Index engine - Query.index", () => {
describe("Index engine - Query.index", () => {
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
})
@@ -262,8 +258,7 @@ medusaIntegrationTestRunner({
])
})
// TODO: Investigate why this test is flacky
it.skip("should use query.index to query the index module sorting by price desc", async () => {
it("should use query.index to query the index module sorting by price desc", async () => {
await populateData(api)
const query = appContainer.resolve(

View File

@@ -12,12 +12,58 @@ jest.setTimeout(100000)
process.env.ENABLE_INDEX_MODULE = "true"
async function populateData(
api: any,
{
productCount = 50,
variantCount = 10,
priceCount = 10,
}: {
productCount?: number
variantCount?: number
priceCount?: number
} = {}
) {
const shippingProfile = (
await api.post(
`/admin/shipping-profiles`,
{ name: "Test", type: "default" },
adminHeaders
)
).data.shipping_profile
const payloads = new Array(productCount).fill(0).map((_, a) => ({
title: "Test Giftcard-" + a,
is_giftcard: true,
shipping_profile_id: shippingProfile.id,
description: "test-giftcard-description" + a,
options: [{ title: "Denominations", values: ["100"] }],
variants: new Array(variantCount).fill(0).map((_, i) => ({
title: `Test variant ${i}`,
sku: `test-variant-${i}${a}`,
prices: new Array(priceCount).fill(0).map((_, j) => ({
currency_code: Object.values(defaultCurrencies)[j].code,
amount: 10 * j,
})),
options: {
Denominations: "100",
},
})),
}))
for (const payload of payloads) {
await api.post("/admin/products", payload, adminHeaders)
}
await setTimeout(4000 * (productCount / 10))
}
medusaIntegrationTestRunner({
testSuite: ({ getContainer, dbConnection, api, dbConfig }) => {
let indexEngine: IndexTypes.IIndexService
let appContainer
beforeAll(() => {
beforeAll(async () => {
appContainer = getContainer()
indexEngine = appContainer.resolve(Modules.INDEX)
})
@@ -30,43 +76,13 @@ medusaIntegrationTestRunner({
await createAdminUser(dbConnection, adminHeaders, appContainer)
})
describe.skip("Index engine", () => {
describe("Index engine", () => {
it("should search through the indexed data and return the correct results ordered and filtered [1]", async () => {
const shippingProfile = (
await api.post(
`/admin/shipping-profiles`,
{ name: "Test", type: "default" },
adminHeaders
)
).data.shipping_profile
const payload = {
title: "Test Giftcard",
is_giftcard: true,
shipping_profile_id: shippingProfile.id,
description: "test-giftcard-description",
options: [{ title: "Denominations", values: ["100"] }],
variants: new Array(10).fill(0).map((_, i) => ({
title: `Test variant ${i}`,
sku: `test-variant-${i}`,
prices: new Array(10).fill(0).map((_, j) => ({
currency_code: Object.values(defaultCurrencies)[j].code,
amount: 10 * j,
})),
options: {
Denominations: "100",
},
})),
}
await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err)
})
// Timeout to allow indexing to finish
await setTimeout(4000)
await populateData(api, {
productCount: 1,
variantCount: 10,
priceCount: 10,
})
const { data: results } = await fetchAndRetry(
async () =>
@@ -119,41 +135,11 @@ medusaIntegrationTestRunner({
})
it("should search through the indexed data and return the correct results ordered and filtered [2]", async () => {
const shippingProfile = (
await api.post(
`/admin/shipping-profiles`,
{ name: "Test", type: "default" },
adminHeaders
)
).data.shipping_profile
const payload = {
title: "Test Giftcard",
is_giftcard: true,
description: "test-giftcard-description",
shipping_profile_id: shippingProfile.id,
options: [{ title: "Denominations", values: ["100"] }],
variants: new Array(10).fill(0).map((_, i) => ({
title: `Test variant ${i}`,
sku: `test-variant-${i}`,
prices: new Array(10).fill(0).map((_, j) => ({
currency_code: Object.values(defaultCurrencies)[j].code,
amount: 10 * j,
})),
options: {
Denominations: "100",
},
})),
}
await api
.post("/admin/products", payload, adminHeaders)
.catch((err) => {
console.log(err)
})
// Timeout to allow indexing to finish
await setTimeout(10000)
await populateData(api, {
productCount: 1,
variantCount: 10,
priceCount: 10,
})
const { data: results } = await fetchAndRetry(
async () =>
@@ -205,43 +191,8 @@ medusaIntegrationTestRunner({
}
})
it.skip("should search through the indexed data and return the correct results ordered and filtered [3]", async () => {
const shippingProfile = (
await api.post(
`/admin/shipping-profiles`,
{ name: "Test", type: "default" },
adminHeaders
)
).data.shipping_profile
const payloads = new Array(50).fill(0).map((_, a) => ({
title: "Test Giftcard-" + a,
is_giftcard: true,
shipping_profile_id: shippingProfile.id,
description: "test-giftcard-description" + a,
options: [{ title: "Denominations", values: ["100"] }],
variants: new Array(10).fill(0).map((_, i) => ({
title: `Test variant ${i}`,
sku: `test-variant-${i}${a}`,
prices: new Array(10).fill(0).map((_, j) => ({
currency_code: Object.values(defaultCurrencies)[j].code,
amount: 10 * j,
})),
options: {
Denominations: "100",
},
})),
}))
let i = 0
for (const payload of payloads) {
++i
await api.post("/admin/products", payload, adminHeaders).then(() => {
console.log(`Created ${i} products in ${payloads.length} payloads`)
})
}
await setTimeout(5000)
it("should search through the indexed data and return the correct results ordered and filtered [3]", async () => {
await populateData(api)
const queryArgs = {
fields: [

View File

@@ -66,16 +66,12 @@ medusaIntegrationTestRunner({
describe("Index engine syncing", () => {
it("should sync the data to the index based on the indexation configuration", async () => {
console.info("[Index engine] Creating products")
await populateData(api, {
productCount: 2,
variantCount: 2,
priceCount: 2,
})
console.info("[Index engine] Creating products done")
await setTimeout(1000)
await dbConnection.raw('TRUNCATE TABLE "index_data";')
await dbConnection.raw('TRUNCATE TABLE "index_relation";')
@@ -96,12 +92,9 @@ medusaIntegrationTestRunner({
// Prevent storage provider to be triggered though
;(indexEngine as any).storageProvider_.onApplicationStart = jest.fn()
console.info("[Index engine] Triggering sync")
// Trigger a sync
await (indexEngine as any).onApplicationStart_()
console.info("[Index engine] Sync done")
// 28 ms - 6511 records
const { data: results } = await indexEngine.query<"product">({
fields: [
@@ -122,12 +115,8 @@ medusaIntegrationTestRunner({
})
it("should sync the data to the index based on the updated indexation configuration", async () => {
console.info("[Index engine] Creating products")
await populateData(api)
console.info("[Index engine] Creating products done")
await setTimeout(1000)
await dbConnection.raw('TRUNCATE TABLE "index_data";')
await dbConnection.raw('TRUNCATE TABLE "index_relation";')
@@ -148,12 +137,9 @@ medusaIntegrationTestRunner({
// Prevent storage provider to be triggered though
;(indexEngine as any).storageProvider_.onApplicationStart = jest.fn()
console.info("[Index engine] Triggering sync")
// Trigger a sync
await (indexEngine as any).onApplicationStart_()
console.info("[Index engine] Sync done")
const { data: results } = await indexEngine.query<"product">({
fields: [
"product.*",