The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
import { ContainerRegistrationKeys, Modules } from "@medusajs/framework/utils"
import { MedusaContainer } from "@medusajs/framework/types"
import {
createProductAttributesWorkflow,
createProductsWorkflow,
} from "@mercurjs/core/workflows"
import {
adminHeaders,
createAdminUser,
} from "../../../helpers/create-admin-user"
import { createSellerUser } from "../../../helpers/create-seller-user"
import { waitForIndexedProducts } from "../../../helpers/wait-for-indexed-products"
import {
AttributeType,
MercurModules,
ProductChangeActionType,
ProductChangeStatus,
} from "@mercurjs/types"
jest.setTimeout(60000)
/**
* SPEC-014 §G — vendor attribute batch endpoint
* `POST /vendor/products/:id/attributes/batch`. Unlike the admin surface
* (direct-apply), the vendor surface routes the batch through the ProductChange
* "request" pipeline (`productEditUpdateAttributesWorkflow`): it stages one
* `ATTRIBUTE_*` action per batch entry and returns `202 { product_change }`.
* The test env runs with `MEDUSA_FF_PRODUCT_REQUEST=false`, so the staged
* change auto-confirms inline — the batch engine still applies before the GET,
* which is how these tests assert the applied state. Scoped to a product the
* seller owns.
*/
medusaIntegrationTestRunner({
testSuite: ({ getContainer, api, dbConnection }) => {
describe("Vendor - product attributes batch", () => {
let appContainer: MedusaContainer
let seller: any
let sellerHeaders: any
beforeAll(() => {
appContainer = getContainer()
})
beforeEach(async () => {
await createAdminUser(dbConnection, adminHeaders, appContainer)
const res = await createSellerUser(appContainer, {
email: "[email protected]",
name: "Vendor Attrs Store",
})
seller = res.seller
sellerHeaders = res.headers
})
const createAttr = async (opts: {
name: string
type: AttributeType
is_variant_axis?: boolean
values?: string[]
}) => {
const { result } = await createProductAttributesWorkflow(
appContainer,
).run({
input: {
attributes: [
{
name: opts.name,
type: opts.type,
is_variant_axis: opts.is_variant_axis ?? false,
values: (opts.values ?? []).map((name, rank) => ({
name,
rank,
})),
},
],
},
})
const id = result[0].id
const query = appContainer.resolve(ContainerRegistrationKeys.QUERY)
const { data } = await query.graph({
entity: "product_attribute",
fields: ["id", "values.id", "values.name"],
filters: { id },
})
const byName = new Map<string, string>(
(data[0].values ?? []).map((v: { id: string; name: string }) => [
v.name,
v.id,
]),
)
return { id, byName }
}
const createOwnedProduct = async () => {
const { result } = await createProductsWorkflow(appContainer).run({
input: {
products: [
{
title: "Vendor Product",
status: "published",
seller_ids: [seller.id],
},
],
created_by: seller.id,
},
})
return (result as { id: string }[])[0].id
}
const batch = (productId: string, body: Record<string, unknown>) =>
api.post(
`/vendor/products/${productId}/attributes/batch`,
body,
sellerHeaders,
)
// Axis attachment via the ProductOption side (product.options populate is
// broken on the 2.16 preview build).
const optionAttached = async (productId: string, title: string) => {
const query = appContainer.resolve(ContainerRegistrationKeys.QUERY)
const { data } = await query.graph({
entity: "product_option",
fields: ["id", "title", "products.id"],
filters: { title },
})
return (data ?? []).some(
(o: any) =>
o.title === title &&
(o.products ?? []).some((p: any) => p.id === productId),
)
}
// Re-read the applied attribute state after a staged batch
// auto-confirms (PRODUCT_REQUEST off in the test env).
const getProduct = async (productId: string) =>
(await api.get(`/vendor/products/${productId}`, sellerHeaders)).data
.product
const scopedAttr = (product: any, name: string) =>
(product.scoped_attributes ?? []).find((a: any) => a.name === name)
it("add: inline axis → exclusive option + scoped attribute", async () => {
const productId = await createOwnedProduct()
const res = await batch(productId, {
add: [{ title: "Size", values: ["S", "M"], is_variant_axis: true }],
})
expect(res.status).toEqual(202)
expect(await optionAttached(productId, "Size")).toBe(true)
const product = await getProduct(productId)
const scoped = (product.scoped_attributes ?? []).find(
(a: any) => a.name === "Size",
)
expect(scoped).toBeTruthy()
})
it("update: rename a scoped axis attribute via the staged batch (202 + auto-confirm)", async () => {
const productId = await createOwnedProduct()
const added = await batch(productId, {
add: [{ title: "Size", values: ["S", "M"], is_variant_axis: true }],
})
expect(added.status).toEqual(202)
const sizeId = scopedAttr(await getProduct(productId), "Size").id
// The vendor edit form sends rename + value add in one update entry; the
// `title` rides verbatim through the ProductChange action to the engine.
const res = await batch(productId, {
update: [{ id: sizeId, title: "Sizing", add: [{ value: "L" }] }],
})
expect(res.status).toEqual(202)
const product = await getProduct(productId)
const renamed = scopedAttr(product, "Sizing")
expect(renamed).toBeTruthy()
expect(scopedAttr(product, "Size")).toBeFalsy()
expect((renamed.values ?? []).map((v: any) => v.name).sort()).toEqual([
"L",
"M",
"S",
])
})
it("allows any seller to request changes on a master product it did not create", async () => {
// master product created by a different actor — every seller can
// still request a change through the product-edit pipeline.
const { result } = await createProductsWorkflow(appContainer).run({
input: {
products: [{ title: "Foreign", status: "published" }],
created_by: "foreign-actor",
},
})
const foreignId = (result as { id: string }[])[0].id
const attr = await createAttr({ name: "Material", type: "text" })
const res = await batch(foreignId, {
add: [{ id: attr.id, value: "x" }],
})
expect(res.status).toEqual(202)
})
// --- request flow: ProductChange staging via the batch endpoint ---
it("stages one ATTRIBUTE_ADD / ATTRIBUTE_UPDATE / ATTRIBUTE_REMOVE action per batch entry", async () => {
const color = await createAttr({
name: "ReqColor",
type: "multi_select",
is_variant_axis: true,
values: ["Red", "Blue"],
})
const flag = await createAttr({ name: "ReqFlag", type: "toggle" })
const text = await createAttr({ name: "ReqText", type: "text" })
const productId = await createOwnedProduct()
// Seed links to update / remove against (auto-confirmed inline).
await batch(productId, {
add: [
{ id: flag.id, value: true },
{ id: text.id, value: "seed" },
],
})
const res = await batch(productId, {
add: [{ id: color.id, value_ids: [color.byName.get("Red")!] }],
update: [{ id: flag.id, value: false }],
remove: [text.id],
})
expect(res.status).toEqual(202)
const actions = res.data.product_change.actions as Array<{
action: string
details: Record<string, any>
}>
const byType = (t: ProductChangeActionType) =>
actions.filter((a) => a.action === t)
expect(byType(ProductChangeActionType.ATTRIBUTE_ADD)).toHaveLength(1)
expect(byType(ProductChangeActionType.ATTRIBUTE_UPDATE)).toHaveLength(1)
expect(byType(ProductChangeActionType.ATTRIBUTE_REMOVE)).toHaveLength(1)
// Each action carries the raw batch op verbatim in `details`.
expect(
byType(ProductChangeActionType.ATTRIBUTE_ADD)[0].details.attribute.id,
).toBe(color.id)
expect(
byType(ProductChangeActionType.ATTRIBUTE_UPDATE)[0].details.update.id,
).toBe(flag.id)
expect(
byType(ProductChangeActionType.ATTRIBUTE_REMOVE)[0].details
.attribute_id,
).toBe(text.id)
})
it("rejects a second batch while a pending change is open", async () => {
const attr = await createAttr({ name: "PendingAttr", type: "text" })
const productId = await createOwnedProduct()
// Seed a PENDING change directly (the test env auto-confirms HTTP
// batches, so simulate the approval-queue state explicitly).
const service: any = appContainer.resolve(MercurModules.PRODUCT_EDIT)
const [change] = await service.createProductChanges([
{
product_id: productId,
created_by: seller.id,
status: ProductChangeStatus.PENDING,
},
])
await service.createProductChangeActions([
{
product_change_id: change.id,
product_id: productId,
action: ProductChangeActionType.ATTRIBUTE_ADD,
details: { attribute: { id: attr.id, value: "pending" } },
},
])
const err = await batch(productId, {
add: [{ id: attr.id, value: "another" }],
}).catch((e) => e)
expect(err.response.status).toBeGreaterThanOrEqual(400)
})
it("update: value_ids replaces the selection of an attached non-axis attribute", async () => {
const material = await createAttr({
name: "Material",
type: AttributeType.MULTI_SELECT,
values: ["Cotton", "Wool", "Silk"],
})
const cotton = material.byName.get("Cotton")!
const wool = material.byName.get("Wool")!
const silk = material.byName.get("Silk")!
const productId = await createOwnedProduct()
await batch(productId, {
add: [{ id: material.id, value_ids: [cotton, wool] }],
})
const res = await batch(productId, {
update: [{ id: material.id, value_ids: [wool, silk] }],
})
expect(res.status).toEqual(202)
const product = await getProduct(productId)
const applied = (product.attributes ?? []).find(
(a: any) => a.id === material.id,
)
expect((applied.values ?? []).map((v: any) => v.name).sort()).toEqual([
"Silk",
"Wool",
])
})
it("update: value_ids on an axis attribute keeps the shared option attached", async () => {
const color = await createAttr({
name: "AxisColor",
type: AttributeType.MULTI_SELECT,
is_variant_axis: true,
values: ["Red", "Blue"],
})
const red = color.byName.get("Red")!
const blue = color.byName.get("Blue")!
const productId = await createOwnedProduct()
await batch(productId, {
add: [{ id: color.id, value_ids: [red] }],
})
expect(await optionAttached(productId, "AxisColor")).toBe(true)
const res = await batch(productId, {
update: [{ id: color.id, value_ids: [blue] }],
})
expect(res.status).toEqual(202)
const product = await getProduct(productId)
const applied = (product.attributes ?? []).find(
(a: any) => a.id === color.id,
)
expect((applied.values ?? []).map((v: any) => v.name)).toEqual(["Blue"])
// The mirror option is never unlinked/relinked to change the subset.
expect(await optionAttached(productId, "AxisColor")).toBe(true)
const query = appContainer.resolve(ContainerRegistrationKeys.QUERY)
const { data } = await query.graph({
entity: "product_option",
fields: ["id", "title", "is_exclusive"],
filters: { title: "AxisColor" },
})
expect(data[0].is_exclusive).toBe(false)
})
it("update: rejects value_ids combined with add", async () => {
const material = await createAttr({
name: "ConflictMaterial",
type: AttributeType.MULTI_SELECT,
values: ["Cotton", "Wool"],
})
const cotton = material.byName.get("Cotton")!
const wool = material.byName.get("Wool")!
const productId = await createOwnedProduct()
await batch(productId, {
add: [{ id: material.id, value_ids: [cotton] }],
})
const err = await batch(productId, {
update: [{ id: material.id, value_ids: [wool], add: [wool] }],
}).catch((e) => e)
expect(err.response.status).toEqual(400)
expect(err.response.data.message).toContain("value_ids")
})
})
describe("Vendor - product list scoping", () => {
let appContainer: MedusaContainer
let sellerA: any
let sellerB: any
let headersA: any
let headersB: any
beforeAll(() => {
appContainer = getContainer()
})
beforeEach(async () => {
const a = await createSellerUser(appContainer, {
email: "[email protected]",
name: "Scope A Store",
})
sellerA = a.seller
headersA = a.headers
const b = await createSellerUser(appContainer, {
email: "[email protected]",
name: "Scope B Store",
})
headersB = b.headers
sellerB = b.seller
})
const createProduct = async (
title: string,
status: string,
createdBy: string,
sellerIds?: string[]
) => {
const { result } = await createProductsWorkflow(appContainer).run({
input: {
products: [{ title, status, seller_ids: sellerIds } as any],
created_by: createdBy,
},
})
const id = (result as { id: string }[])[0].id
await waitForIndexedProducts(appContainer, [
{ id, status, restricted: !!sellerIds?.length },
])
return id
}
it("scopes the vendor list to the seller's own proposed products plus published", async () => {
const proposedByA = await createProduct("A Proposed", "proposed", sellerA.id)
const published = await createProduct("Global Published", "published", "other-actor")
const listAsA = await api.get("/vendor/products?limit=100", headersA)
const idsA = listAsA.data.products.map((p: { id: string }) => p.id)
expect(idsA).toEqual(expect.arrayContaining([proposedByA, published]))
const listAsB = await api.get("/vendor/products?limit=100", headersB)
const idsB = listAsB.data.products.map((p: { id: string }) => p.id)
expect(idsB).toContain(published)
expect(idsB).not.toContain(proposedByA)
})
it("hides a restricted published product from sellers it is not assigned to", async () => {
// Published, but restricted (product_seller) to seller A only.
const restrictedToA = await createProduct(
"Restricted To A",
"published",
"other-actor",
[sellerA.id]
)
const listAsA = await api.get("/vendor/products?limit=100", headersA)
const idsA = listAsA.data.products.map((p: { id: string }) => p.id)
expect(idsA).toContain(restrictedToA)
const listAsB = await api.get("/vendor/products?limit=100", headersB)
const idsB = listAsB.data.products.map((p: { id: string }) => p.id)
expect(idsB).not.toContain(restrictedToA)
})
it("does not retrieve another seller's unpublished product by id", async () => {
const proposedByA = await createProduct("A Proposed Detail", "proposed", sellerA.id)
const asA = await api.get(`/vendor/products/${proposedByA}`, headersA)
expect(asA.status).toEqual(200)
const asB = await api
.get(`/vendor/products/${proposedByA}`, headersB)
.catch((e) => e.response)
expect(asB.status).toEqual(404)
const preview = await api
.get(`/vendor/products/${proposedByA}/preview`, headersB)
.catch((e) => e.response)
expect(preview.status).toEqual(404)
})
it("does not retrieve a published product restricted to other sellers by id", async () => {
const restrictedToA = await createProduct(
"Restricted Detail",
"published",
"other-actor",
[sellerA.id]
)
const asA = await api.get(`/vendor/products/${restrictedToA}`, headersA)
expect(asA.status).toEqual(200)
const asB = await api
.get(`/vendor/products/${restrictedToA}`, headersB)
.catch((e) => e.response)
expect(asB.status).toEqual(404)
})
it("does not delete or edit another seller's draft product", async () => {
const draftByA = await createProduct("A Draft", "draft", sellerA.id)
const remove = await api
.delete(`/vendor/products/${draftByA}`, headersB)
.catch((e) => e.response)
expect(remove.status).toEqual(404)
const update = await api
.post(`/vendor/products/${draftByA}`, { title: "Hijacked" }, headersB)
.catch((e) => e.response)
expect(update.status).toEqual(404)
const stillThere = await api.get(`/vendor/products/${draftByA}`, headersA)
expect(stillThere.status).toEqual(200)
expect(stillThere.data.product.title).toEqual("A Draft")
})
it("applies the same scoping when a category filter forces the graph fallback", async () => {
const productModule = appContainer.resolve(Modules.PRODUCT)
const [category] = await productModule.createProductCategories([
{ name: "Fallback Category", is_active: true },
])
const create = async (
title: string,
status: string,
createdBy: string,
sellerIds?: string[]
) => {
const { result } = await createProductsWorkflow(appContainer).run({
input: {
products: [
{
title,
status,
seller_ids: sellerIds,
category_ids: [category.id],
} as any,
],
created_by: createdBy,
},
})
return (result as { id: string }[])[0].id
}
const open = await create("Fallback Open", "published", "other-actor")
const restrictedToA = await create(
"Fallback Restricted",
"published",
"other-actor",
[sellerA.id]
)
const proposedByA = await create("Fallback Proposed", "proposed", sellerA.id)
const asA = await api.get(
`/vendor/products?category_id=${category.id}&limit=100`,
headersA
)
expect(asA.data.products.map((p: { id: string }) => p.id).sort()).toEqual(
[open, restrictedToA, proposedByA].sort()
)
const asB = await api.get(
`/vendor/products?category_id=${category.id}&limit=100`,
headersB
)
expect(asB.data.products.map((p: { id: string }) => p.id)).toEqual([open])
expect(asB.data.count).toEqual(1)
})
it("paginates the index-backed list with a count that is exact on the last page", async () => {
const ids = [
await createProduct("CountProbe One", "published", "other-actor"),
await createProduct("CountProbe Two", "published", "other-actor"),
await createProduct("CountProbe Three", "published", "other-actor"),
]
const idFilter = ids.map((id) => `id[]=${id}`).join("&")
const first = await api.get(`/vendor/products?${idFilter}&limit=2`, headersA)
expect(first.data.products).toHaveLength(2)
expect(first.data.count).toEqual(3)
const last = await api.get(
`/vendor/products?${idFilter}&limit=2&offset=2`,
headersA
)
expect(last.data.products).toHaveLength(1)
expect(last.data.count).toEqual(3)
const seen = [...first.data.products, ...last.data.products].map(
(p: { id: string }) => p.id
)
expect(seen.sort()).toEqual([...ids].sort())
})
it("matches a partial word in q, as before the index engine", async () => {
const shirt = await createProduct("Linen Shirt", "published", "other-actor")
await createProduct("Wool Scarf", "published", "other-actor")
const response = await api.get("/vendor/products?q=Shi", headersA)
expect(response.data.products.map((p: { id: string }) => p.id)).toEqual([
shirt,
])
})
describe("cached list invalidation", () => {
const listIds = async (headers: any) =>
(await api.get("/vendor/products?limit=100", headers)).data.products.map(
(p: { id: string }) => p.id
)
it("drops a product from a cached list once it is restricted to another seller", async () => {
const productId = await createProduct("Cache Restrict", "published", "other-actor")
expect(await listIds(headersB)).toContain(productId)
expect(await listIds(headersB)).toContain(productId)
await appContainer.resolve(ContainerRegistrationKeys.LINK).create({
[Modules.PRODUCT]: { product_id: productId },
[MercurModules.SELLER]: { seller_id: sellerA.id },
})
await waitForIndexedProducts(appContainer, [
{ id: productId, status: "published", restricted: true },
])
expect(await listIds(headersB)).not.toContain(productId)
expect(await listIds(headersA)).toContain(productId)
})
it("serves a product update instead of the cached list entry", async () => {
const productId = await createProduct("Cache Before", "published", "other-actor")
const titleOf = async () =>
(await api.get("/vendor/products?limit=100", headersA)).data.products.find(
(p: { id: string }) => p.id === productId
)?.title
expect(await titleOf()).toEqual("Cache Before")
await appContainer
.resolve(Modules.PRODUCT)
.updateProducts(productId, { title: "Cache After" })
await waitForIndexedProducts(appContainer, [
{ id: productId, title: "Cache After", restricted: false },
])
expect(await titleOf()).toEqual("Cache After")
})
})
it("serves the seller-scoped list through the index engine", async () => {
await createProduct("Index Path", "published", "other-actor")
const query = appContainer.resolve(ContainerRegistrationKeys.QUERY)
const indexSpy = jest.spyOn(query, "index")
await api.get("/vendor/products?limit=5", headersA)
expect(indexSpy).toHaveBeenCalled()
indexSpy.mockRestore()
})
it("returns the exact visible count on a full page, not the planner estimate", async () => {
const visible = [
await createProduct("Exact Open 1", "published", "other-actor"),
await createProduct("Exact Open 2", "published", "other-actor"),
await createProduct("Exact Allowlisted", "published", "other-actor", [
sellerA.id,
]),
await createProduct("Exact Own Draft", "draft", sellerA.id),
await createProduct("Exact Own Proposed", "proposed", sellerA.id),
]
await createProduct("Exact Hidden Restricted", "published", "other-actor", [
sellerB.id,
])
await createProduct("Exact Hidden Draft", "draft", "other-actor")
const page = await api.get("/vendor/products?limit=2", headersA)
expect(page.data.products).toHaveLength(2)
expect(page.data.count).toEqual(visible.length)
})
it("hides a published product from its creator once restricted to other sellers", async () => {
const b = await createSellerUser(appContainer, {
email: "[email protected]",
name: "Scope C Store",
})
const createdByAForC = await createProduct(
"Created By A, Assigned To C",
"published",
sellerA.id,
[b.seller.id]
)
const listAsA = await api.get("/vendor/products?limit=100", headersA)
const idsA = listAsA.data.products.map((p: { id: string }) => p.id)
expect(idsA).not.toContain(createdByAForC)
const listAsC = await api.get("/vendor/products?limit=100", b.headers)
const idsC = listAsC.data.products.map((p: { id: string }) => p.id)
expect(idsC).toContain(createdByAForC)
})
})
// MER-246: creating a product with several images persisted only one in the
// UI. The images survive the create workflow, but the vendor detail query
// omits the relation from its server defaults, so the bare `*images` the
// panel requested resolved against nothing and returned no images — leaving
// only the scalar `thumbnail`. The panel now spells the relation out
// (`images.id,images.url,images.rank`); this guards that contract.
describe("Vendor - product images", () => {
let appContainer: MedusaContainer
let sellerHeaders: any
beforeAll(() => {
appContainer = getContainer()
})
beforeEach(async () => {
const res = await createSellerUser(appContainer, {
email: "[email protected]",
name: "Vendor Images Store",
})
sellerHeaders = res.headers
})
it("returns every image on the detail query, not just the thumbnail", async () => {
const images = [
{ url: "https://example.com/mer-246-1.png" },
{ url: "https://example.com/mer-246-2.png" },
{ url: "https://example.com/mer-246-3.png" },
]
const created = await api.post(
"/vendor/products",
{
status: "published",
title: "Multi Image Product",
images,
thumbnail: images[0].url,
variants: [{ title: "Default" }],
},
sellerHeaders
)
const productId = created.data.product.id
const res = await api.get(
`/vendor/products/${productId}?fields=images.id,images.url,images.rank`,
sellerHeaders
)
const returned: { url: string }[] = res.data.product.images ?? []
expect(returned).toHaveLength(images.length)
expect(returned.map((i) => i.url).sort()).toEqual(
images.map((i) => i.url).sort()
)
})
})
},
})