|
1
|
import "@medusajs/framework/modules-sdk" |
|
2
|
import { SearchTypes } from "@medusajs/framework/types" |
|
3
|
import { defineSearchIndex, search } from "@medusajs/framework/utils" |
|
4
|
|
|
5
|
export type TestProduct = { |
|
6
|
id: string |
|
7
|
title: string |
|
8
|
handle: string |
|
9
|
description: string |
|
10
|
status: string |
|
11
|
brand: string |
|
12
|
min_price: number |
|
13
|
created_at: Date |
|
14
|
updated_at: Date |
|
15
|
// Stands in for a hard-removed source row. Only used by the |
|
16
|
// catch-up-reseed tests. |
|
17
|
removed?: boolean |
|
18
|
tags: string[] |
|
19
|
variants: { sku: string; color: string }[] |
|
20
|
} |
|
21
|
|
|
22
|
/** |
|
23
|
* Stands in for the source of truth a real `seed` would read through |
|
24
|
* `query.graph`. Tests mutate it and reindex to prove the index follows. |
|
25
|
*/ |
|
26
|
export const dataset: { products: TestProduct[] } = { products: [] } |
|
27
|
|
|
28
|
export const baseProducts: TestProduct[] = [ |
|
29
|
{ |
|
30
|
id: "prod_1", |
|
31
|
title: "Red running shoe", |
|
32
|
handle: "red-running-shoe", |
|
33
|
description: "A breathable shoe for long distances", |
|
34
|
status: "published", |
|
35
|
brand: "acme", |
|
36
|
min_price: 100, |
|
37
|
created_at: new Date("2026-01-01T00:00:00.000Z"), |
|
38
|
updated_at: new Date("2026-01-01T00:00:00.000Z"), |
|
39
|
tags: ["shoe", "sport"], |
|
40
|
variants: [ |
|
41
|
{ sku: "SHOE-RED-41", color: "red" }, |
|
42
|
{ sku: "SHOE-RED-42", color: "red" }, |
|
43
|
], |
|
44
|
}, |
|
45
|
{ |
|
46
|
id: "prod_2", |
|
47
|
title: "Blue running shirt", |
|
48
|
handle: "blue-running-shirt", |
|
49
|
description: "A light shirt for warm weather", |
|
50
|
status: "published", |
|
51
|
brand: "borg", |
|
52
|
min_price: 50, |
|
53
|
created_at: new Date("2026-02-01T00:00:00.000Z"), |
|
54
|
updated_at: new Date("2026-02-01T00:00:00.000Z"), |
|
55
|
tags: ["shirt", "sport"], |
|
56
|
variants: [{ sku: "SHIRT-BLUE-M", color: "blue" }], |
|
57
|
}, |
|
58
|
{ |
|
59
|
id: "prod_3", |
|
60
|
title: "Green wool hat", |
|
61
|
handle: "green-wool-hat", |
|
62
|
description: "A warm hat for the winter", |
|
63
|
status: "draft", |
|
64
|
brand: "acme", |
|
65
|
min_price: 200, |
|
66
|
created_at: new Date("2026-03-01T00:00:00.000Z"), |
|
67
|
updated_at: new Date("2026-03-01T00:00:00.000Z"), |
|
68
|
tags: ["hat"], |
|
69
|
variants: [ |
|
70
|
{ sku: "HAT-GREEN-S", color: "green" }, |
|
71
|
{ sku: "HAT-GREEN-L", color: "olive" }, |
|
72
|
], |
|
73
|
}, |
|
74
|
] |
|
75
|
|
|
76
|
export function resetDataset(products: TestProduct[] = baseProducts): void { |
|
77
|
dataset.products = products.map((product) => ({ ...product })) |
|
78
|
consumedEvents.length = 0 |
|
79
|
consumedContainers.length = 0 |
|
80
|
onBulkSeedStart = undefined |
|
81
|
} |
|
82
|
|
|
83
|
// Simulates a live update landing on the source of truth mid-build. |
|
84
|
export function touchProduct(id: string, changes: Partial<TestProduct> = {}): void { |
|
85
|
const index = dataset.products.findIndex((product) => product.id === id) |
|
86
|
if (index === -1) { |
|
87
|
return |
|
88
|
} |
|
89
|
dataset.products[index] = { |
|
90
|
...dataset.products[index], |
|
91
|
...changes, |
|
92
|
updated_at: new Date(), |
|
93
|
} |
|
94
|
} |
|
95
|
|
|
96
|
// Simulates a live delete landing on the source of truth mid-build. |
|
97
|
export function removeProduct(id: string): void { |
|
98
|
touchProduct(id, { removed: true }) |
|
99
|
} |
|
100
|
|
|
101
|
// What `consume` was handed, so the tests can assert how the module routed. |
|
102
|
export const consumedEvents: { event: string; index: string }[] = [] |
|
103
|
|
|
104
|
export const consumedContainers: SearchTypes.SearchContainer[] = [] |
|
105
|
|
|
106
|
// Test seam: called once a bulk (unscoped) seed has taken its snapshot of |
|
107
|
// `dataset.products` but before it's done writing, so a test can simulate a |
|
108
|
// live write landing mid-build — one the bulk pass' own snapshot missed, and |
|
109
|
// only the catch-up pass (a fresh, later read) can still pick up. |
|
110
|
let onBulkSeedStart: (() => void | Promise<void>) | undefined |
|
111
|
|
|
112
|
export function setOnBulkSeedStart( |
|
113
|
hook: (() => void | Promise<void>) | undefined |
|
114
|
): void { |
|
115
|
onBulkSeedStart = hook |
|
116
|
} |
|
117
|
|
|
118
|
// Declared exactly like user code: `defineSearchIndex` compiles the DSL schema |
|
119
|
// and returns the normalized definition the test runner passes as options. |
|
120
|
export const productIndex: SearchTypes.SearchIndexDefinition = |
|
121
|
defineSearchIndex({ |
|
122
|
name: "product", |
|
123
|
entity: "product", |
|
124
|
fields: search.define({ |
|
125
|
id: search.keyword().filterable(), |
|
126
|
title: search.text().searchable({ weight: 3 }).sortable(), |
|
127
|
handle: search.keyword().filterable(), |
|
128
|
// Indexed for matching but never returned, so the split between engine |
|
129
|
// fields and `query.graph` fields is observable. |
|
130
|
description: search.text().searchable().retrievable(false), |
|
131
|
status: search.keyword().filterable().facetable(), |
|
132
|
brand: search.keyword().filterable().facetable().sortable(), |
|
133
|
min_price: search.float().filterable().sortable().facetable(), |
|
134
|
created_at: search.date().filterable().sortable(), |
|
135
|
updated_at: search.date().filterable().sortable(), |
|
136
|
tags: search.keyword().array().filterable().facetable(), |
|
137
|
variants: search |
|
138
|
.object({ |
|
139
|
sku: search.keyword().searchable().filterable(), |
|
140
|
color: search.keyword().filterable().facetable(), |
|
141
|
}) |
|
142
|
.array(), |
|
143
|
}), |
|
144
|
events: ["product.created", "product.updated", "product.deleted"], |
|
145
|
// Reads the document out of the dataset rather than off the event, the way a |
|
146
|
// real definition reads through `query.graph`: an event carries an id. |
|
147
|
async consume(event, { index, container }) { |
|
148
|
consumedEvents.push({ event: event.name, index: index.name }) |
|
149
|
consumedContainers.push(container) |
|
150
|
|
|
151
|
const id = (event.data as { id: string }).id |
|
152
|
|
|
153
|
if (event.name === "product.deleted") { |
|
154
|
return [{ action: "delete", filters: { id: [id] } }] |
|
155
|
} |
|
156
|
|
|
157
|
const product = dataset.products.find((candidate) => candidate.id === id) |
|
158
|
|
|
159
|
// Gone by the time the event was handled; the delete that follows removes it. |
|
160
|
if (!product) { |
|
161
|
return [] |
|
162
|
} |
|
163
|
|
|
164
|
return [{ action: "upsert", documents: [product] }] |
|
165
|
}, |
|
166
|
async *seed({ filters, catchup }) { |
|
167
|
const ids = (filters?.ids as string[]) ?? undefined |
|
168
|
const scoped = !!ids || !!catchup |
|
169
|
|
|
170
|
let candidates = dataset.products |
|
171
|
if (ids) { |
|
172
|
candidates = candidates.filter((product) => ids.includes(product.id)) |
|
173
|
} |
|
174
|
if (catchup) { |
|
175
|
candidates = candidates.filter( |
|
176
|
(product) => product.updated_at >= catchup.since |
|
177
|
) |
|
178
|
} |
|
179
|
if (!scoped) { |
|
180
|
// An unscoped (full) seed never re-adds what the source removed — |
|
181
|
// the target either started empty (swap) or was cleared first |
|
182
|
// (in-place). |
|
183
|
candidates = candidates.filter((product) => !product.removed) |
|
184
|
|
|
185
|
if (onBulkSeedStart) { |
|
186
|
const hook = onBulkSeedStart |
|
187
|
onBulkSeedStart = undefined |
|
188
|
await hook() |
|
189
|
} |
|
190
|
} |
|
191
|
|
|
192
|
// Two batches, so the streaming path and the sync cursor both get exercised. |
|
193
|
const half = Math.ceil(candidates.length / 2) || 1 |
|
194
|
|
|
195
|
for (let i = 0; i < candidates.length; i += half) { |
|
196
|
const batch = candidates.slice(i, i + half) |
|
197
|
const mutations: SearchTypes.SearchMutation[] = [] |
|
198
|
|
|
199
|
const upserts = batch.filter((product) => !product.removed) |
|
200
|
if (upserts.length) { |
|
201
|
mutations.push({ action: "upsert", documents: upserts }) |
|
202
|
} |
|
203
|
|
|
204
|
for (const product of batch) { |
|
205
|
if (product.removed) { |
|
206
|
mutations.push({ action: "delete", filters: { id: [product.id] } }) |
|
207
|
} |
|
208
|
} |
|
209
|
|
|
210
|
yield mutations |
|
211
|
} |
|
212
|
}, |
|
213
|
}) |