@tbox.cn/app-module-shopping-guide 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/chunk-AGBLC6XI.js +1294 -0
- package/dist/chunk-U4WDS5PQ.js +173 -0
- package/dist/client/index.d.ts +25 -0
- package/dist/client/index.js +6 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +22 -0
- package/dist/server/index.d.ts +4098 -0
- package/dist/server/index.js +18 -0
- package/package.json +62 -0
- package/skills/shopping-guide/SKILL.md +36 -0
- package/skills/shopping-guide/references/comparison-format.md +5 -0
- package/skills/shopping-guide/references/faq-answers.md +5 -0
- package/skills/shopping-guide/references/floor-directory.md +4 -0
- package/skills/shopping-guide/references/intent-patterns.md +5 -0
- package/skills/shopping-guide/references/location-format.md +5 -0
- package/skills/shopping-guide/references/policy-lookup.md +5 -0
- package/skills/shopping-guide/references/product-search-tips.md +6 -0
- package/skills/shopping-guide/references/recommendation-style.md +5 -0
- package/src/client/cards/index.tsx +267 -0
- package/src/client/index.ts +39 -0
- package/src/index.ts +6 -0
- package/src/server/cards/category-list/meta.ts +19 -0
- package/src/server/cards/customer-service-faq/meta.ts +18 -0
- package/src/server/cards/human-guide-list/meta.ts +19 -0
- package/src/server/cards/index.ts +43 -0
- package/src/server/cards/location-handoff/meta.ts +20 -0
- package/src/server/cards/mall-directory/meta.ts +27 -0
- package/src/server/cards/merchant-detail/meta.ts +27 -0
- package/src/server/cards/merchant-list/meta.ts +31 -0
- package/src/server/cards/nearby-mall-list/meta.ts +17 -0
- package/src/server/cards/product-comparison/meta.ts +29 -0
- package/src/server/cards/product-detail/meta.ts +29 -0
- package/src/server/cards/product-list/meta.ts +32 -0
- package/src/server/domain/value-objects.ts +49 -0
- package/src/server/index.ts +180 -0
- package/src/server/integrations/joycity/adapter.ts +226 -0
- package/src/server/integrations/joycity/auth-strategy.ts +38 -0
- package/src/server/integrations/joycity/gateway.ts +128 -0
- package/src/server/ports/index.ts +151 -0
- package/src/server/ports/local-adapters.ts +90 -0
- package/src/server/projections/index.ts +253 -0
- package/src/server/tools/index.ts +407 -0
- package/tbox.component.json +48 -0
- package/tests/shopping-guide.test.ts +290 -0
- package/tsconfig.json +8 -0
- package/tsup.config.ts +29 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 011 S7 shopping-guide 切片验收(S2 读链端到端):
|
|
3
|
+
* fixture transport → JoyCity adapter → 投影 → 工具 execute → 卡数据(schema 校验)。
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, it } from 'vitest';
|
|
6
|
+
import { createInMemoryStateStore } from '@tbox.cn/app-sdk/server';
|
|
7
|
+
import { createServerContext } from '@tbox.cn/app-sdk/server';
|
|
8
|
+
import type { ServerContext } from '@tbox.cn/app-sdk/server';
|
|
9
|
+
import { createProviderRegistry } from '@tbox.cn/app-sdk/server';
|
|
10
|
+
import {
|
|
11
|
+
JoyCityTransportGateway,
|
|
12
|
+
JOYCITY_READ_OPERATIONS,
|
|
13
|
+
} from '../src/server/integrations/joycity/gateway';
|
|
14
|
+
import { JoyCityCatalogAdapter, JoyCityMerchantAdapter } from '../src/server/integrations/joycity/adapter';
|
|
15
|
+
import { createShoppingGuideTools } from '../src/server/tools';
|
|
16
|
+
import { resolveShoppingGuidePorts } from '../src/server/ports';
|
|
17
|
+
import {
|
|
18
|
+
productListCardDataSchema,
|
|
19
|
+
merchantListCardDataSchema,
|
|
20
|
+
productComparisonCardDataSchema,
|
|
21
|
+
} from '@tbox.cn/app-contracts-mall';
|
|
22
|
+
import type { RequestContext } from '@tbox.cn/app-contracts';
|
|
23
|
+
|
|
24
|
+
const ctx = (mallId = 'mall-1'): RequestContext => ({
|
|
25
|
+
identity: { userId: 'local_u1', source: 'local' },
|
|
26
|
+
attributes: { mall: { miniAppId: 'app1', mallId } },
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
function buildFixtureServer(): ServerContext {
|
|
30
|
+
// fixture transport:mock joycity 响应(S2 读链零网络)
|
|
31
|
+
const transport = {
|
|
32
|
+
async request(req: { path: string; body?: unknown }) {
|
|
33
|
+
if (req.path === JOYCITY_READ_OPERATIONS.searchProducts) {
|
|
34
|
+
return {
|
|
35
|
+
Code: 1,
|
|
36
|
+
Data: [
|
|
37
|
+
{
|
|
38
|
+
ProductID: 101,
|
|
39
|
+
Name: '轻量跑鞋',
|
|
40
|
+
ImageUrl: 'https://img.example.com/shoe.png',
|
|
41
|
+
BrandName: '示例品牌',
|
|
42
|
+
PriceYuan: 399,
|
|
43
|
+
OriginalPriceYuan: 499,
|
|
44
|
+
StockLabel: '有货',
|
|
45
|
+
Marketable: true,
|
|
46
|
+
MerchantID: 1,
|
|
47
|
+
MerchantName: '示例运动店',
|
|
48
|
+
BuildingName: 'A 栋',
|
|
49
|
+
FloorName: '3 楼',
|
|
50
|
+
UnitName: '301',
|
|
51
|
+
Labels: ['热卖'],
|
|
52
|
+
PromotionFacts: ['限时 8 折'],
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (req.path === JOYCITY_READ_OPERATIONS.listMerchants) {
|
|
58
|
+
return {
|
|
59
|
+
Code: 1,
|
|
60
|
+
Data: [
|
|
61
|
+
{
|
|
62
|
+
MerchantID: 1,
|
|
63
|
+
Name: '示例运动店',
|
|
64
|
+
CommercialTypeName: '运动',
|
|
65
|
+
BuildingName: 'A 栋',
|
|
66
|
+
FloorName: '3 楼',
|
|
67
|
+
UnitName: '301',
|
|
68
|
+
Status: 1,
|
|
69
|
+
Services: ['试穿'],
|
|
70
|
+
PromotionFacts: ['会员 9 折'],
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (req.path === JOYCITY_READ_OPERATIONS.getProduct) {
|
|
76
|
+
return {
|
|
77
|
+
Code: 1,
|
|
78
|
+
Data: {
|
|
79
|
+
ProductID: 101,
|
|
80
|
+
Name: '轻量跑鞋',
|
|
81
|
+
PriceYuan: 399,
|
|
82
|
+
StockLabel: '有货',
|
|
83
|
+
Marketable: true,
|
|
84
|
+
MerchantID: 1,
|
|
85
|
+
MerchantName: '示例运动店',
|
|
86
|
+
BuildingName: 'A 栋',
|
|
87
|
+
FloorName: '3 楼',
|
|
88
|
+
UnitName: '301',
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return { Code: 0, Data: null };
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// 直接注入 fixture transport(绕过 credential 链,S2 只验读链)
|
|
97
|
+
const registry = createProviderRegistry({
|
|
98
|
+
instances: [
|
|
99
|
+
{
|
|
100
|
+
providerInstanceId: 'joy-fixture',
|
|
101
|
+
providerTemplateId: 'joycity-commercial@1',
|
|
102
|
+
scope: { mallId: 'mall-1' },
|
|
103
|
+
enabled: true,
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
templates: [
|
|
107
|
+
{ providerTemplateId: 'joycity-commercial@1', credentialType: 'static-headers-v1' },
|
|
108
|
+
],
|
|
109
|
+
transportFactory: {
|
|
110
|
+
create: () => transport,
|
|
111
|
+
},
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const serverCtx = createServerContext({
|
|
115
|
+
state: createInMemoryStateStore(),
|
|
116
|
+
providers: registry,
|
|
117
|
+
});
|
|
118
|
+
serverCtx.auth.setContext(ctx());
|
|
119
|
+
return serverCtx;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
describe('S2 读链端到端(fixture transport → adapter → 投影 → 工具)', () => {
|
|
123
|
+
it('search-products:DTO → ProductRecord → product-list 卡数据(schema 通过)', async () => {
|
|
124
|
+
const serverCtx = buildFixtureServer();
|
|
125
|
+
await resolveShoppingGuidePorts(serverCtx);
|
|
126
|
+
const tools = createShoppingGuideTools({ getCtx: () => serverCtx });
|
|
127
|
+
|
|
128
|
+
const result = (await tools.searchProducts.execute!(
|
|
129
|
+
{
|
|
130
|
+
keywords: ['跑鞋'],
|
|
131
|
+
page: '1',
|
|
132
|
+
pageSize: '10',
|
|
133
|
+
} as never,
|
|
134
|
+
{} as never,
|
|
135
|
+
)) as { context: string; items: unknown[] };
|
|
136
|
+
|
|
137
|
+
expect(result.context).toBe('search');
|
|
138
|
+
expect(result.items).toHaveLength(1);
|
|
139
|
+
// 卡 schema 校验(TIER1 zod 单一来源)
|
|
140
|
+
expect(productListCardDataSchema.safeParse(result).success).toBe(true);
|
|
141
|
+
|
|
142
|
+
const item = result.items[0] as {
|
|
143
|
+
name: string;
|
|
144
|
+
price: { sale?: { display: string } };
|
|
145
|
+
merchant: { location?: { displayText: string } };
|
|
146
|
+
};
|
|
147
|
+
expect(item.name).toBe('轻量跑鞋');
|
|
148
|
+
expect(item.price.sale?.display).toBe('¥399.00');
|
|
149
|
+
expect(item.merchant.location?.displayText).toContain('A 栋');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('search-merchants:DTO → MerchantRecord → merchant-list 卡数据(schema 通过)', async () => {
|
|
153
|
+
const serverCtx = buildFixtureServer();
|
|
154
|
+
await resolveShoppingGuidePorts(serverCtx);
|
|
155
|
+
const tools = createShoppingGuideTools({ getCtx: () => serverCtx });
|
|
156
|
+
|
|
157
|
+
const result = (await tools.searchMerchants.execute!(
|
|
158
|
+
{
|
|
159
|
+
keywords: ['运动'],
|
|
160
|
+
page: '1',
|
|
161
|
+
pageSize: '10',
|
|
162
|
+
} as never,
|
|
163
|
+
{} as never,
|
|
164
|
+
)) as { context: string; items: unknown[] };
|
|
165
|
+
|
|
166
|
+
expect(result.context).toBe('search');
|
|
167
|
+
expect(merchantListCardDataSchema.safeParse(result).success).toBe(true);
|
|
168
|
+
const item = result.items[0] as { name: string; location: { displayText: string } };
|
|
169
|
+
expect(item.name).toBe('示例运动店');
|
|
170
|
+
expect(item.location.displayText).toContain('3 楼');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('compare-products:多商品对比(schema 通过)', async () => {
|
|
174
|
+
const serverCtx = buildFixtureServer();
|
|
175
|
+
await resolveShoppingGuidePorts(serverCtx);
|
|
176
|
+
const tools = createShoppingGuideTools({ getCtx: () => serverCtx });
|
|
177
|
+
|
|
178
|
+
const result = (await tools.compareProducts.execute!(
|
|
179
|
+
{
|
|
180
|
+
productIds: ['101', '101'],
|
|
181
|
+
} as never,
|
|
182
|
+
{} as never,
|
|
183
|
+
)) as unknown;
|
|
184
|
+
|
|
185
|
+
// 单商品(fixture 只有 1 个商品)也产出对比结构
|
|
186
|
+
expect(productComparisonCardDataSchema.safeParse(result).success).toBe(true);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('JoyCityCatalogAdapter 直连(adapter 层单测)', async () => {
|
|
190
|
+
const gateway = new JoyCityTransportGateway({
|
|
191
|
+
async request() {
|
|
192
|
+
return {
|
|
193
|
+
Code: 1,
|
|
194
|
+
Data: [
|
|
195
|
+
{
|
|
196
|
+
ProductID: 101,
|
|
197
|
+
Name: '轻量跑鞋',
|
|
198
|
+
PriceYuan: 199,
|
|
199
|
+
StockLabel: '有货',
|
|
200
|
+
Marketable: true,
|
|
201
|
+
MerchantID: 1,
|
|
202
|
+
MerchantName: '示例运动店',
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
};
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
const adapter = new JoyCityCatalogAdapter(gateway);
|
|
209
|
+
const page = await adapter.searchProducts({
|
|
210
|
+
ctx: { scope: { miniAppId: 'app1', mallId: 'mall-1' }, requestContext: ctx() as never },
|
|
211
|
+
page: 1,
|
|
212
|
+
pageSize: 10,
|
|
213
|
+
});
|
|
214
|
+
expect(page.items).toHaveLength(1);
|
|
215
|
+
expect(page.items[0].price.sale?.amountMinor).toBe(19900);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('JoyCityMerchantAdapter 直连', async () => {
|
|
219
|
+
const gateway = new JoyCityTransportGateway({
|
|
220
|
+
async request() {
|
|
221
|
+
return {
|
|
222
|
+
Code: 1,
|
|
223
|
+
Data: [
|
|
224
|
+
{
|
|
225
|
+
MerchantID: 1,
|
|
226
|
+
Name: '示例运动店',
|
|
227
|
+
CommercialTypeName: '运动',
|
|
228
|
+
BuildingName: 'A 栋',
|
|
229
|
+
FloorName: '3 楼',
|
|
230
|
+
Status: 1,
|
|
231
|
+
},
|
|
232
|
+
],
|
|
233
|
+
};
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
const adapter = new JoyCityMerchantAdapter(gateway);
|
|
237
|
+
const page = await adapter.searchMerchants({
|
|
238
|
+
ctx: { scope: { miniAppId: 'app1', mallId: 'mall-1' }, requestContext: ctx() as never },
|
|
239
|
+
page: 1,
|
|
240
|
+
pageSize: 10,
|
|
241
|
+
});
|
|
242
|
+
expect(page.items[0].status.code).toBe('enabled');
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('joycity C 端签名算法稳定(MD5 slice)', async () => {
|
|
246
|
+
const { computeJoyCityCSign } = await import('../src/server/integrations/joycity/auth-strategy');
|
|
247
|
+
expect(computeJoyCityCSign('pk', '1700000000000', '{"a":1}', 'sk')).toMatch(/^[0-9a-f]{16}$/);
|
|
248
|
+
// 确定性
|
|
249
|
+
expect(computeJoyCityCSign('pk', '1700000000000', '{"a":1}', 'sk')).toBe(
|
|
250
|
+
computeJoyCityCSign('pk', '1700000000000', '{"a":1}', 'sk'),
|
|
251
|
+
);
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
describe('S3 feature 门控(011 D10 / 评审 M4:execute 内运行时护栏)', () => {
|
|
256
|
+
it('customer-service feature 禁用 → FAQ 工具返回结构化 error(不抛)', async () => {
|
|
257
|
+
const serverCtx = buildFixtureServer();
|
|
258
|
+
// 注入禁用 gate(模拟部署未开启 customer-service)
|
|
259
|
+
const serverCtxDisabled = Object.assign(serverCtx, {
|
|
260
|
+
runtime: { isFeatureEnabled: () => false, config: {} },
|
|
261
|
+
});
|
|
262
|
+
const tools = createShoppingGuideTools({ getCtx: () => serverCtxDisabled });
|
|
263
|
+
serverCtx.auth.setContext(ctx());
|
|
264
|
+
const result = await (tools.customerServiceFaq as unknown as { execute(a: unknown): Promise<unknown> }).execute({
|
|
265
|
+
query: '退换货政策',
|
|
266
|
+
});
|
|
267
|
+
expect(result).toMatchObject({ error: expect.stringContaining('客服功能未启用') });
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('navigation feature 禁用 → list-nearby-malls 返回结构化 error', async () => {
|
|
271
|
+
const serverCtx = buildFixtureServer();
|
|
272
|
+
const serverCtxDisabled = Object.assign(serverCtx, {
|
|
273
|
+
runtime: { isFeatureEnabled: () => false, config: {} },
|
|
274
|
+
});
|
|
275
|
+
const tools = createShoppingGuideTools({ getCtx: () => serverCtxDisabled });
|
|
276
|
+
serverCtx.auth.setContext(ctx());
|
|
277
|
+
const result = await (tools.listNearbyMalls as unknown as { execute(a: unknown): Promise<unknown> }).execute({});
|
|
278
|
+
expect(result).toMatchObject({ error: expect.stringContaining('导航功能未启用') });
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('feature 全启用(缺省 gate)→ 工具正常执行', async () => {
|
|
282
|
+
const serverCtx = buildFixtureServer();
|
|
283
|
+
await resolveShoppingGuidePorts(serverCtx);
|
|
284
|
+
const tools = createShoppingGuideTools({ getCtx: () => serverCtx });
|
|
285
|
+
serverCtx.auth.setContext(ctx());
|
|
286
|
+
const result = await (tools.listNearbyMalls as unknown as { execute(a: unknown): Promise<unknown> }).execute({});
|
|
287
|
+
// 未禁用 → 走本地 adapter 空数据(不抛、非 error)
|
|
288
|
+
expect(result).not.toHaveProperty('error');
|
|
289
|
+
});
|
|
290
|
+
});
|
package/tsconfig.json
ADDED
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { defineConfig } from 'tsup';
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: {
|
|
5
|
+
index: 'src/index.ts',
|
|
6
|
+
'server/index': 'src/server/index.ts',
|
|
7
|
+
'client/index': 'src/client/index.ts',
|
|
8
|
+
},
|
|
9
|
+
format: ['esm'],
|
|
10
|
+
target: 'node20',
|
|
11
|
+
platform: 'node',
|
|
12
|
+
outDir: 'dist',
|
|
13
|
+
clean: true,
|
|
14
|
+
sourcemap: false,
|
|
15
|
+
dts: true,
|
|
16
|
+
external: [
|
|
17
|
+
'@tbox.cn/app-sdk',
|
|
18
|
+
'@tbox.cn/app-contracts',
|
|
19
|
+
'@tbox.cn/app-contracts-mall',
|
|
20
|
+
'react',
|
|
21
|
+
'react-dom',
|
|
22
|
+
'zod',
|
|
23
|
+
'@mastra/core',
|
|
24
|
+
'express',
|
|
25
|
+
],
|
|
26
|
+
esbuildOptions(options) {
|
|
27
|
+
options.jsx = 'automatic';
|
|
28
|
+
},
|
|
29
|
+
});
|