|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { |
|
3
|
adminHeaders, |
|
4
|
createAdminUser, |
|
5
|
} from "../../../helpers/create-admin-user" |
|
6
|
import { setTimeout } from "timers/promises" |
|
7
|
|
|
8
|
process.env.ENABLE_INDEX_MODULE = "true" |
|
9
|
|
|
10
|
jest.setTimeout(300000) |
|
11
|
|
|
12
|
const truncateIndexTables = async (dbConnection: { |
|
13
|
raw: (sql: string) => Promise<unknown> |
|
14
|
}) => { |
|
15
|
await dbConnection.raw( |
|
16
|
`TRUNCATE TABLE "index_data", "index_relation", "index_metadata", "index_sync" RESTART IDENTITY CASCADE` |
|
17
|
) |
|
18
|
} |
|
19
|
|
|
20
|
medusaIntegrationTestRunner({ |
|
21
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
22
|
let container |
|
23
|
|
|
24
|
beforeAll(async () => { |
|
25
|
container = getContainer() |
|
26
|
await createAdminUser(dbConnection, adminHeaders, container) |
|
27
|
|
|
28
|
await dbUtils.snapshot() |
|
29
|
}) |
|
30
|
|
|
31
|
afterAll(() => { |
|
32
|
delete process.env.ENABLE_INDEX_MODULE |
|
33
|
}) |
|
34
|
|
|
35
|
describe("Admin Index API", () => { |
|
36
|
describe("GET /admin/index/details", () => { |
|
37
|
it("should return index information with metadata", async () => { |
|
38
|
await setTimeout(3000) |
|
39
|
|
|
40
|
const response = await api.get(`/admin/index/details`, adminHeaders) |
|
41
|
|
|
42
|
expect(response.status).toEqual(200) |
|
43
|
expect(response.data).toHaveProperty("metadata") |
|
44
|
expect(response.data.metadata.length).toBe(7) |
|
45
|
|
|
46
|
// Verify all expected entities are present with correct structure |
|
47
|
const entities = [ |
|
48
|
"Product", |
|
49
|
"ProductVariant", |
|
50
|
"LinkProductVariantPriceSet", |
|
51
|
"Price", |
|
52
|
"SalesChannel", |
|
53
|
"LinkProductSalesChannel", |
|
54
|
"PriceSet", |
|
55
|
] |
|
56
|
|
|
57
|
entities.forEach((entityName) => { |
|
58
|
const entityMetadata = response.data.metadata.find( |
|
59
|
(m) => m.entity === entityName |
|
60
|
) |
|
61
|
expect(entityMetadata).toBeDefined() |
|
62
|
expect(entityMetadata).toMatchObject({ |
|
63
|
id: expect.any(String), |
|
64
|
entity: entityName, |
|
65
|
status: expect.stringMatching(/^(pending|processing|done)$/), |
|
66
|
fields: expect.any(Array), |
|
67
|
updated_at: expect.any(String), |
|
68
|
}) |
|
69
|
expect(entityMetadata).toHaveProperty("last_synced_key") |
|
70
|
}) |
|
71
|
|
|
72
|
// Verify specific field structures for key entities |
|
73
|
const productMetadata = response.data.metadata.find( |
|
74
|
(m) => m.entity === "Product" |
|
75
|
) |
|
76
|
expect(productMetadata.fields).toEqual( |
|
77
|
expect.arrayContaining(["id", "title", "handle", "status"]) |
|
78
|
) |
|
79
|
|
|
80
|
const variantMetadata = response.data.metadata.find( |
|
81
|
(m) => m.entity === "ProductVariant" |
|
82
|
) |
|
83
|
expect(variantMetadata.fields).toEqual( |
|
84
|
expect.arrayContaining(["id", "product_id", "sku"]) |
|
85
|
) |
|
86
|
}) |
|
87
|
|
|
88
|
describe("POST /admin/index/sync", () => { |
|
89
|
it("should trigger sync with default strategy (continue)", async () => { |
|
90
|
const response = await api.post( |
|
91
|
`/admin/index/sync`, |
|
92
|
{}, |
|
93
|
adminHeaders |
|
94
|
) |
|
95
|
|
|
96
|
expect(response.status).toEqual(200) |
|
97
|
}) |
|
98
|
|
|
99
|
it("should trigger sync with full strategy", async () => { |
|
100
|
const response = await api.post( |
|
101
|
`/admin/index/sync`, |
|
102
|
{ strategy: "full" }, |
|
103
|
adminHeaders |
|
104
|
) |
|
105
|
|
|
106
|
expect(response.status).toEqual(200) |
|
107
|
}) |
|
108
|
|
|
109
|
it("should trigger sync with reset strategy", async () => { |
|
110
|
const response = await api.post( |
|
111
|
`/admin/index/sync`, |
|
112
|
{ strategy: "reset" }, |
|
113
|
adminHeaders |
|
114
|
) |
|
115
|
|
|
116
|
expect(response.status).toEqual(200) |
|
117
|
}) |
|
118
|
|
|
119
|
it("should reject invalid strategy", async () => { |
|
120
|
const response = await api |
|
121
|
.post(`/admin/index/sync`, { strategy: "invalid" }, adminHeaders) |
|
122
|
.catch((e) => e) |
|
123
|
|
|
124
|
expect(response.response.status).toEqual(400) |
|
125
|
}) |
|
126
|
|
|
127
|
it("should sync and reflect in metadata status", async () => { |
|
128
|
await truncateIndexTables(dbConnection) |
|
129
|
|
|
130
|
const syncResponsePromise = api.post( |
|
131
|
`/admin/index/sync`, |
|
132
|
{ strategy: "full" }, |
|
133
|
adminHeaders |
|
134
|
) |
|
135
|
const updatedResponse = await api.get( |
|
136
|
`/admin/index/details`, |
|
137
|
adminHeaders |
|
138
|
) |
|
139
|
|
|
140
|
const syncResponse = await syncResponsePromise |
|
141
|
expect(syncResponse.status).toEqual(200) |
|
142
|
expect(updatedResponse.status).toEqual(200) |
|
143
|
|
|
144
|
if (updatedResponse.data.metadata.length > 0) { |
|
145
|
const hashedMetadataWithStatusPending = |
|
146
|
updatedResponse.data.metadata.some( |
|
147
|
(m) => m.status === "pending" |
|
148
|
) |
|
149
|
expect(hashedMetadataWithStatusPending).toBe(true) |
|
150
|
} |
|
151
|
}) |
|
152
|
|
|
153
|
it("should reset index and clear all data", async () => { |
|
154
|
await truncateIndexTables(dbConnection) |
|
155
|
|
|
156
|
const syncResponsePromise = api.post( |
|
157
|
`/admin/index/sync`, |
|
158
|
{ strategy: "reset" }, |
|
159
|
adminHeaders |
|
160
|
) |
|
161
|
const response = await api.get(`/admin/index/details`, adminHeaders) |
|
162
|
|
|
163
|
const syncResponse = await syncResponsePromise |
|
164
|
expect(syncResponse.status).toEqual(200) |
|
165
|
expect(response.status).toEqual(200) |
|
166
|
|
|
167
|
const metadata = response.data.metadata |
|
168
|
metadata.forEach((m) => { |
|
169
|
expect(["pending", "processing"]).toContain(m.status) |
|
170
|
}) |
|
171
|
}) |
|
172
|
}) |
|
173
|
}) |
|
174
|
}) |
|
175
|
}, |
|
176
|
}) |