@tbox.cn/app-module-shopping-guide 0.2.1 → 0.8.2

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.
Files changed (89) hide show
  1. package/AGENTS.md +16 -0
  2. package/README.md +96 -0
  3. package/dist/chunk-AYQZO2FV.js +1496 -0
  4. package/dist/chunk-QDTU2FRN.js +1682 -0
  5. package/dist/client/index.d.ts +3 -3
  6. package/dist/client/index.js +1 -1
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.js +2 -8
  9. package/dist/server/index.d.ts +2512 -608
  10. package/dist/server/index.js +1 -7
  11. package/dist/shop-guide-header-art-pea-v2-OME4MKBW.jpg +0 -0
  12. package/dist/shopping-guide-OQ2SSHXL.css +2593 -0
  13. package/package.json +25 -16
  14. package/skills/shopping-guide/SKILL.md +127 -24
  15. package/src/client/assets/shop-guide-header-art-pea-v2.jpg +0 -0
  16. package/src/client/assets/shop-guide-header-art.png +0 -0
  17. package/src/client/assets.d.ts +10 -0
  18. package/src/client/cards/category-list/index.tsx +113 -0
  19. package/src/client/cards/human-guide-list/index.tsx +72 -0
  20. package/src/client/cards/index.ts +14 -0
  21. package/src/client/cards/location-handoff/index.tsx +52 -0
  22. package/src/client/cards/mall-directory/index.tsx +77 -0
  23. package/src/client/cards/nearby-mall-list/index.tsx +40 -0
  24. package/src/client/cards/product-comparison/index.tsx +100 -0
  25. package/src/client/cards/product-detail/index.tsx +97 -0
  26. package/src/client/cards/product-list/index.tsx +223 -0
  27. package/src/client/cards/recommend-merchants/index.tsx +147 -0
  28. package/src/client/cards/shop-detail/index.tsx +133 -0
  29. package/src/client/cards/shop-list/index.tsx +216 -0
  30. package/src/client/cards/view.ts +147 -0
  31. package/src/client/categories.ts +38 -0
  32. package/src/client/components/ImageWithFallback.tsx +34 -0
  33. package/src/client/components/MerchantRow.tsx +78 -0
  34. package/src/client/components/ShopDetailSheet.tsx +383 -0
  35. package/src/client/components/ShopGuideHeader.tsx +24 -0
  36. package/src/client/components/ShopGuideSectionLabel.tsx +10 -0
  37. package/src/client/components/ShopListSheet.tsx +293 -0
  38. package/src/client/index.ts +8 -6
  39. package/src/client/styles/shopping-guide.css +2593 -0
  40. package/src/client/types/css.d.ts +2 -0
  41. package/src/server/cards/category-list/meta.ts +0 -2
  42. package/src/server/cards/human-guide-list/meta.ts +0 -2
  43. package/src/server/cards/index.ts +10 -10
  44. package/src/server/cards/location-handoff/meta.ts +0 -2
  45. package/src/server/cards/mall-directory/meta.ts +0 -2
  46. package/src/server/cards/nearby-mall-list/meta.ts +1 -3
  47. package/src/server/cards/product-comparison/meta.ts +0 -2
  48. package/src/server/cards/product-detail/meta.ts +0 -2
  49. package/src/server/cards/product-list/meta.ts +0 -2
  50. package/src/server/cards/recommend-merchants/meta.ts +37 -0
  51. package/src/server/cards/recommend-merchants/samples.ts +189 -0
  52. package/src/server/cards/{merchant-detail → shop-detail}/meta.ts +6 -6
  53. package/src/server/cards/shop-detail/samples.ts +72 -0
  54. package/src/server/cards/{merchant-list → shop-list}/meta.ts +9 -9
  55. package/src/server/cards/shop-list/samples.ts +154 -0
  56. package/src/server/domain/value-objects.ts +1 -2
  57. package/src/server/handler.ts +63 -0
  58. package/src/server/index.ts +161 -101
  59. package/src/server/ports/index.ts +120 -91
  60. package/src/server/ports/local-adapters.ts +5 -55
  61. package/src/server/projections/index.ts +159 -41
  62. package/src/server/tools/index.ts +590 -315
  63. package/tbox.module.json +137 -0
  64. package/tests/cards-render.test.tsx +830 -0
  65. package/tests/floor-pretool.test.ts +275 -0
  66. package/tests/handler.test.ts +259 -0
  67. package/tests/merchant-list-limit.test.ts +53 -0
  68. package/tests/ports-resolve.test.ts +112 -0
  69. package/tests/samples.test.ts +71 -0
  70. package/tests/shopping-guide.test.ts +402 -234
  71. package/tests/skill.test.ts +86 -0
  72. package/tests/view.test.ts +42 -0
  73. package/tsup.config.ts +9 -1
  74. package/dist/chunk-AGBLC6XI.js +0 -1294
  75. package/dist/chunk-U4WDS5PQ.js +0 -173
  76. package/skills/shopping-guide/references/comparison-format.md +0 -5
  77. package/skills/shopping-guide/references/faq-answers.md +0 -5
  78. package/skills/shopping-guide/references/floor-directory.md +0 -4
  79. package/skills/shopping-guide/references/intent-patterns.md +0 -5
  80. package/skills/shopping-guide/references/location-format.md +0 -5
  81. package/skills/shopping-guide/references/policy-lookup.md +0 -5
  82. package/skills/shopping-guide/references/product-search-tips.md +0 -6
  83. package/skills/shopping-guide/references/recommendation-style.md +0 -5
  84. package/src/client/cards/index.tsx +0 -267
  85. package/src/server/cards/customer-service-faq/meta.ts +0 -18
  86. package/src/server/integrations/joycity/adapter.ts +0 -226
  87. package/src/server/integrations/joycity/auth-strategy.ts +0 -38
  88. package/src/server/integrations/joycity/gateway.ts +0 -128
  89. package/tbox.component.json +0 -48
@@ -0,0 +1,275 @@
1
+ /**
2
+ * 洋葱 L2 楼层预执行切片验收:
3
+ * - normalizeFloorKey / extractFloorMentions 纯函数(跨 provider 楼层格式归一);
4
+ * - floor-shops 工具 execute:目录归一解析 → searchShops(floorCode) → shop-list 出卡(v2 cards 契约);
5
+ * - 失败语义:楼层不存在 / 目录不可用 / 引用不可解析 → 无卡 summary(模型降级自答)。
6
+ */
7
+ import { describe, expect, it } from 'vitest';
8
+ import { createInMemoryStateStore, createToolExecContext } from '@tbox.cn/app-sdk/server';
9
+ import { createServerContext } from '@tbox.cn/app-sdk/server';
10
+ import type { ServerContext } from '@tbox.cn/app-sdk/server';
11
+ import {
12
+ createIntegrationsRegistry,
13
+ createAuthStrategyRegistry,
14
+ createEnvironmentSecretProvider,
15
+ } from '@tbox.cn/app-sdk/server';
16
+ import { createShoppingGuideTools, extractFloorMentions, normalizeFloorKey } from '../src/server/tools';
17
+ import { serverModule } from '../src/server/index';
18
+ import { shopListCardDataSchema } from '@tbox.cn/app-contracts-mall';
19
+ import type { RequestContext } from '@tbox.cn/app-contracts';
20
+
21
+ const ctx = (mallId = 'mall-1'): RequestContext => ({
22
+ identity: { userId: 'local_u1', source: 'local' },
23
+ attributes: { mall: { miniAppId: 'app1', mallId } },
24
+ });
25
+
26
+ /** mock provider 形态:floorCode = floorName('B1' / '1 楼' / '2 楼' …);searchShops 按楼层过滤 */
27
+ function stubDirectory() {
28
+ const floors = ['B1', '1 楼', '2 楼', '3 楼', '4 楼'].map((f) => ({
29
+ floorCode: f,
30
+ floorName: f,
31
+ businessCategories: [],
32
+ merchantCount: 1,
33
+ }));
34
+ return { buildings: [{ buildingCode: 'A', buildingName: 'A 栋', floors }], totalMerchants: 5 };
35
+ }
36
+
37
+ function shopOf(floor: string, name: string) {
38
+ return {
39
+ merchantId: `shop:${floor}:${name}`,
40
+ name,
41
+ logoUrl: undefined,
42
+ coverUrls: [],
43
+ brand: undefined,
44
+ businessCategories: ['餐饮'],
45
+ location: { displayText: `A 栋 ${floor} 301`, buildingName: 'A 栋', floorName: floor, unitName: '301' },
46
+ status: { code: 'enabled' as const, label: '营业中', observedAt: '2026-08-14T00:00:00Z' },
47
+ description: `${name}(${floor})`,
48
+ contactInfo: [],
49
+ services: [],
50
+ promotionFacts: [],
51
+ };
52
+ }
53
+
54
+ function stubFloorPorts(opts: { directory: ReturnType<typeof stubDirectory> | null }) {
55
+ const allShops = [
56
+ shopOf('2 楼', '二楼美妆店'),
57
+ shopOf('3 楼', '三楼烤鱼'),
58
+ shopOf('4 楼', '四楼火锅'),
59
+ ];
60
+ return {
61
+ catalog: {},
62
+ shop: {
63
+ async searchShops(criteria: { floorCode?: string; page: number; pageSize: number }) {
64
+ const items = allShops.filter((s) => !criteria.floorCode || s.location.floorName === criteria.floorCode);
65
+ return { items, page: criteria.page, pageSize: criteria.pageSize, total: items.length, hasMore: false };
66
+ },
67
+ },
68
+ category: { async listCategories() { return []; } },
69
+ directory: {
70
+ async getDirectory() { return opts.directory; },
71
+ },
72
+ } as never;
73
+ }
74
+
75
+
76
+ /** 槽 id → 四键 bag 的单 Port 分发(017 槽位原子化测试基建) */
77
+ const SLOT_KEY_OF: Record<string, string> = {
78
+ 'shopping-guide.catalog': 'catalog',
79
+ 'shopping-guide.shops': 'shop',
80
+ 'shopping-guide.categories': 'category',
81
+ 'shopping-guide.directory': 'directory',
82
+ };
83
+ const portOf = (bag: Record<string, unknown>, service: string): unknown =>
84
+ bag[SLOT_KEY_OF[service] ?? ''] ?? {};
85
+
86
+ const FIXTURE_PROVIDER = 'fixture';
87
+ const FIXTURE_PROVIDER_ID = 'fixture-floor@1';
88
+
89
+ function buildFixtureServer(opts: { directory: ReturnType<typeof stubDirectory> | null }): ServerContext {
90
+ const registry = createIntegrationsRegistry({
91
+ integrations: {
92
+ services: {
93
+ 'shopping-guide.catalog': { provider: FIXTURE_PROVIDER, implementation: FIXTURE_PROVIDER_ID, instances: { 'mall-1': { enabled: true } } },
94
+ 'shopping-guide.shops': { provider: FIXTURE_PROVIDER, implementation: FIXTURE_PROVIDER_ID, instances: { 'mall-1': { enabled: true } } },
95
+ 'shopping-guide.categories': { provider: FIXTURE_PROVIDER, implementation: FIXTURE_PROVIDER_ID, instances: { 'mall-1': { enabled: true } } },
96
+ 'shopping-guide.directory': { provider: FIXTURE_PROVIDER, implementation: FIXTURE_PROVIDER_ID, instances: { 'mall-1': { enabled: true } } },
97
+ },
98
+ },
99
+ catalog: {
100
+ providers: {
101
+ [FIXTURE_PROVIDER]: {
102
+ [FIXTURE_PROVIDER_ID]: { provider: FIXTURE_PROVIDER, implementation: FIXTURE_PROVIDER_ID, credentialType: '-', owner: 'fixture', services: ['shopping-guide.catalog', 'shopping-guide.shops', 'shopping-guide.categories', 'shopping-guide.directory'], local: true },
103
+ },
104
+ },
105
+ services: {},
106
+ credentialTypes: {},
107
+ },
108
+ authStrategies: createAuthStrategyRegistry(),
109
+ secretProvider: createEnvironmentSecretProvider({ source: {}, production: false }),
110
+ instanceIdOf: (scope) => scope.mallId ?? '',
111
+ });
112
+ registry.registerAdapter(FIXTURE_PROVIDER, FIXTURE_PROVIDER_ID, (input) =>
113
+ portOf(stubFloorPorts(opts) as Record<string, unknown>, input.service));
114
+ return createServerContext({
115
+ state: createInMemoryStateStore(),
116
+ integrations: registry,
117
+ });
118
+ }
119
+
120
+ describe('楼层归一纯函数', () => {
121
+ it('normalizeFloorKey:跨格式归一(正楼/负楼/中文数字/F 后缀)', () => {
122
+ expect(normalizeFloorKey('2 楼')).toBe('2');
123
+ expect(normalizeFloorKey('2F')).toBe('2');
124
+ expect(normalizeFloorKey('2f')).toBe('2');
125
+ expect(normalizeFloorKey('第2层')).toBe('2');
126
+ expect(normalizeFloorKey('二楼')).toBe('2');
127
+ expect(normalizeFloorKey('3楼')).toBe('3');
128
+ expect(normalizeFloorKey('B1')).toBe('B1');
129
+ expect(normalizeFloorKey('b1 楼')).toBe('B1');
130
+ expect(normalizeFloorKey('负一楼')).toBe('B1');
131
+ expect(normalizeFloorKey('负1楼')).toBe('B1');
132
+ expect(normalizeFloorKey('随便逛逛')).toBeUndefined();
133
+ expect(normalizeFloorKey('')).toBeUndefined();
134
+ });
135
+
136
+ it('extractFloorMentions:整句抽取 + 归一键去重 + 上限 3', () => {
137
+ expect(extractFloorMentions('2楼有啥美食可逛吗')).toEqual(['2楼']);
138
+ expect(extractFloorMentions('3楼/4楼有啥')).toEqual(['3楼', '4楼']);
139
+ expect(extractFloorMentions('B1有什么好吃的')).toEqual(['B1']);
140
+ expect(extractFloorMentions('二楼和三楼哪层人多')).toEqual(['二楼', '三楼']);
141
+ expect(extractFloorMentions('我爱逛美妆')).toEqual([]);
142
+ expect(extractFloorMentions('几楼有超市')).toEqual([]);
143
+ expect(extractFloorMentions('帮我订201房间')).toEqual([]);
144
+ expect(extractFloorMentions('')).toEqual([]);
145
+ expect(extractFloorMentions('2楼 2楼 3楼 4楼 5楼')).toHaveLength(3);
146
+ });
147
+ });
148
+
149
+ describe('floor-shops 工具 execute(洋葱 L2 确定性预执行)', () => {
150
+ it('单楼层:目录归一解析 → shop-list 出卡(schema 通过;floorCode 传目录真源值)', async () => {
151
+ const serverCtx = buildFixtureServer({ directory: stubDirectory() });
152
+ const tools = createShoppingGuideTools({ getCtx: () => serverCtx });
153
+ const result = (await tools.floorShops.execute!(
154
+ { floorQuery: '2楼' } as never,
155
+ createToolExecContext(ctx()) as never,
156
+ )) as { summary: string; cards: { cardType: string; data: unknown }[] };
157
+
158
+ expect(result.cards).toHaveLength(1);
159
+ expect(result.cards[0].cardType).toBe('shop-list');
160
+ expect(shopListCardDataSchema.safeParse(result.cards[0].data).success).toBe(true);
161
+ const data = result.cards[0].data as { items: { name: string }[] };
162
+ expect(data.items.map((i) => i.name)).toEqual(['二楼美妆店']);
163
+ expect(result.summary).toContain('二楼美妆店');
164
+ });
165
+
166
+ it('多楼层合并:3楼,4楼 → 单张 shop-list 合并两楼门店', async () => {
167
+ const serverCtx = buildFixtureServer({ directory: stubDirectory() });
168
+ const tools = createShoppingGuideTools({ getCtx: () => serverCtx });
169
+ const result = (await tools.floorShops.execute!(
170
+ { floorQuery: '3楼,4楼' } as never,
171
+ createToolExecContext(ctx()) as never,
172
+ )) as { cards: { data: { items: { name: string }[] } }[] };
173
+
174
+ expect(result.cards).toHaveLength(1);
175
+ expect(result.cards[0].data.items.map((i) => i.name)).toEqual(['三楼烤鱼', '四楼火锅']);
176
+ });
177
+
178
+ it('楼层不存在:无卡 + floor_not_found summary(模型降级自答)', async () => {
179
+ const serverCtx = buildFixtureServer({ directory: stubDirectory() });
180
+ const tools = createShoppingGuideTools({ getCtx: () => serverCtx });
181
+ const result = (await tools.floorShops.execute!(
182
+ { floorQuery: '9楼' } as never,
183
+ createToolExecContext(ctx()) as never,
184
+ )) as { summary: string; cards?: unknown[] };
185
+
186
+ expect(result.cards).toBeUndefined();
187
+ expect(result.summary).toContain('floor_not_found');
188
+ });
189
+
190
+ it('目录不可用:无卡 + directory_unavailable summary', async () => {
191
+ const serverCtx = buildFixtureServer({ directory: null });
192
+ const tools = createShoppingGuideTools({ getCtx: () => serverCtx });
193
+ const result = (await tools.floorShops.execute!(
194
+ { floorQuery: '2楼' } as never,
195
+ createToolExecContext(ctx()) as never,
196
+ )) as { summary: string; cards?: unknown[] };
197
+
198
+ expect(result.cards).toBeUndefined();
199
+ expect(result.summary).toContain('directory_unavailable');
200
+ });
201
+
202
+ it('空引用:无卡 + floor_query_empty summary', async () => {
203
+ const serverCtx = buildFixtureServer({ directory: stubDirectory() });
204
+ const tools = createShoppingGuideTools({ getCtx: () => serverCtx });
205
+ const result = (await tools.floorShops.execute!(
206
+ { floorQuery: '' } as never,
207
+ createToolExecContext(ctx()) as never,
208
+ )) as { summary: string; cards?: unknown[] };
209
+
210
+ expect(result.cards).toBeUndefined();
211
+ expect(result.summary).toContain('floor_query_empty');
212
+ });
213
+ });
214
+
215
+ describe('preTool decide 互斥链(2026-09-03:category-shops / mall-overview 门控)', () => {
216
+ // server/index.ts preTools 声明(serverModule 顶层加载 skills——exports 含 ./package.json,解析失败优雅降级)
217
+ const preTools = serverModule.preTools as unknown as Record<
218
+ string,
219
+ { decide: (input: { text?: string; attributes?: Record<string, unknown> }) => { decision: string; reason?: string; args?: Record<string, unknown> } }
220
+ >;
221
+
222
+ // B3:mall-context/mall-overview decide 自 attributes 解析 mall scope(mallSlot.read)
223
+ const decideOf = (id: string, text: string) =>
224
+ preTools[id]!.decide({ text, attributes: { mall: { miniAppId: 'app1', mallId: 'mall-1' } } });
225
+
226
+ it('「购物有哪些」→ category-shops run(categoryQuery=购物)', () => {
227
+ const d = decideOf('category-shops', '购物有哪些');
228
+ expect(d.decision).toBe('run');
229
+ expect(d.args).toEqual({ categoryQuery: '购物' });
230
+ });
231
+
232
+ it('「餐饮有啥」「儿童亲子有哪些」→ run;多词截 3 合并', () => {
233
+ expect(decideOf('category-shops', '餐饮有啥').decision).toBe('run');
234
+ const d = decideOf('category-shops', '购物餐饮娱乐生活儿童');
235
+ expect(d.args).toEqual({ categoryQuery: '购物,餐饮,生活' }); // 按词表序截前 3
236
+ });
237
+
238
+ it('楼层互斥:「3楼有什么餐饮」→ category-shops skip;「3楼有什么好逛的」→ mall-overview skip', () => {
239
+ expect(decideOf('category-shops', '3楼有什么餐饮')).toMatchObject({ decision: 'skip', reason: expect.stringContaining('floor-shops') });
240
+ expect(decideOf('mall-overview', '3楼有什么好逛的')).toMatchObject({ decision: 'skip', reason: expect.stringContaining('floor-shops') });
241
+ });
242
+
243
+ it('floor-shops 纯楼层:「3楼有什么」「3楼呢」「3楼逛什么」→ run', () => {
244
+ expect(decideOf('floor-shops', '3楼有什么')).toMatchObject({ decision: 'run', args: { floorQuery: '3楼' } });
245
+ expect(decideOf('floor-shops', '3楼呢')).toMatchObject({ decision: 'run', args: { floorQuery: '3楼' } });
246
+ expect(decideOf('floor-shops', '3楼逛什么')).toMatchObject({ decision: 'run', args: { floorQuery: '3楼' } });
247
+ });
248
+
249
+ it('floor-shops 楼层+额外意图 → skip:让模型调 search-shops 做精准过滤', () => {
250
+ expect(decideOf('floor-shops', '3楼有餐饮店吗')).toMatchObject({ decision: 'skip', reason: expect.stringContaining('search-shops') });
251
+ expect(decideOf('floor-shops', '2楼有什么好吃的吗?我有点饿了')).toMatchObject({ decision: 'skip', reason: expect.stringContaining('search-shops') });
252
+ expect(decideOf('floor-shops', '3楼有什么运动品牌')).toMatchObject({ decision: 'skip', reason: expect.stringContaining('search-shops') });
253
+ });
254
+
255
+ it('结构词互斥:「餐饮类目有哪些」→ category-shops skip(归 mall-overview 概览口径)', () => {
256
+ const d = decideOf('floor-shops', '3楼呢');
257
+ expect(d.decision).toBe('run');
258
+ expect(d.args).toEqual({ floorQuery: '3楼' });
259
+ });
260
+
261
+ it('结构词互斥:「餐饮类目有哪些」→ category-shops skip(归 mall-overview 概览口径)', () => {
262
+ expect(decideOf('category-shops', '餐饮类目有哪些')).toMatchObject({ decision: 'skip', reason: expect.stringContaining('mall-overview') });
263
+ });
264
+
265
+ it('「你好」→ category-shops skip(无类目提及);空文本 → skip', () => {
266
+ expect(decideOf('category-shops', '你好')).toMatchObject({ decision: 'skip' });
267
+ expect(decideOf('category-shops', '')).toMatchObject({ decision: 'skip' });
268
+ expect(decideOf('category-shops', ' ')).toMatchObject({ decision: 'skip' });
269
+ });
270
+
271
+ it('逛族词命中(2026-09-03):「商场有哪些可以逛的」「随便逛逛」→ mall-overview run', () => {
272
+ expect(decideOf('mall-overview', '商场有哪些可以逛的')).toMatchObject({ decision: 'run' });
273
+ expect(decideOf('mall-overview', '随便逛逛')).toMatchObject({ decision: 'run' });
274
+ });
275
+ });
@@ -0,0 +1,259 @@
1
+ /**
2
+ * 品牌导览意图 handler 验收:
3
+ * - exact 整句全等(唯一 pattern「品牌导览」;不入 BM25 词袋);
4
+ * - intent 命中 → port 直调(page=1 / pageSize=200 / 零过滤词)→ shop-list 出卡
5
+ * (schema 通过 + mall scope 透传 + 类目 Tab 聚合);
6
+ * - 固定文案 N 取接口分页字段 total(非本地 items.length——截断/分页下仍为全馆事实数);
7
+ * - 零 LLM 通道不截断:items 超页容量阈值也全量入卡(卡数据直发前端不经模型;投影上限 200 单页顶格);
8
+ * - 失败语义:非整句放行 LLM(continueWith)/ reqCtx 缺失 / scope 缺失 / 空门店 → 无卡文案。
9
+ */
10
+ import { describe, expect, it } from 'vitest';
11
+ import { createInMemoryStateStore, createServerContext } from '@tbox.cn/app-sdk/server';
12
+ import {
13
+ createIntegrationsRegistry,
14
+ createAuthStrategyRegistry,
15
+ createEnvironmentSecretProvider,
16
+ } from '@tbox.cn/app-sdk/server';
17
+ import type { ServerContext } from '@tbox.cn/app-sdk/server';
18
+ import { shopListCardDataSchema } from '@tbox.cn/app-contracts-mall';
19
+ import type { RequestContext } from '@tbox.cn/app-contracts';
20
+ import { createShoppingGuideHandler } from '../src/server/handler';
21
+
22
+ const ctx = (mallId = 'mall-1'): RequestContext => ({
23
+ identity: { userId: 'local_u1', source: 'local' },
24
+ attributes: { mall: { miniAppId: 'app1', mallId } },
25
+ });
26
+
27
+ function shopOf(name: string, category: string) {
28
+ return {
29
+ merchantId: `shop:${name}`,
30
+ name,
31
+ logoUrl: undefined,
32
+ coverUrls: [],
33
+ brand: undefined,
34
+ businessCategories: [category],
35
+ location: { displayText: 'A 栋 2 楼 301', buildingName: 'A 栋', floorName: '2 楼', unitName: '301' },
36
+ status: { code: 'enabled' as const, label: '营业中', observedAt: '2026-08-14T00:00:00Z' },
37
+ description: name,
38
+ contactInfo: [],
39
+ services: [],
40
+ promotionFacts: [],
41
+ };
42
+ }
43
+
44
+ /** 关键差异:total=12(接口事实数)≠ items.length=3(本页/截断数)——断言文案 N 取 total */
45
+ const stubShops = [shopOf('咖啡一号', '餐饮'), shopOf('美妆二号', '零售'), shopOf('健身三号', '运动')];
46
+
47
+ let lastCriteria: Record<string, unknown> | undefined;
48
+
49
+ function stubPorts() {
50
+ return {
51
+ catalog: {},
52
+ shop: {
53
+ async searchShops(criteria: Record<string, unknown>) {
54
+ lastCriteria = criteria;
55
+ return { items: stubShops, page: criteria.page, pageSize: criteria.pageSize, total: 12, hasMore: false };
56
+ },
57
+ },
58
+ category: { async listCategories() { return []; } },
59
+ directory: { async getDirectory() { return null; } },
60
+ } as never;
61
+ }
62
+
63
+
64
+ /** 槽 id → 四键 bag 的单 Port 分发(017 槽位原子化测试基建) */
65
+ const SLOT_KEY_OF: Record<string, string> = {
66
+ 'shopping-guide.catalog': 'catalog',
67
+ 'shopping-guide.shops': 'shop',
68
+ 'shopping-guide.categories': 'category',
69
+ 'shopping-guide.directory': 'directory',
70
+ };
71
+ const portOf = (bag: Record<string, unknown>, service: string): unknown =>
72
+ bag[SLOT_KEY_OF[service] ?? ''] ?? {};
73
+
74
+ const FIXTURE_PROVIDER = 'fixture';
75
+ const FIXTURE_PROVIDER_ID = 'fixture-brand-guide@1';
76
+
77
+ function buildFixtureServer(portsFactory: () => unknown = stubPorts): ServerContext {
78
+ const registry = createIntegrationsRegistry({
79
+ integrations: {
80
+ services: {
81
+ 'shopping-guide.catalog': { provider: FIXTURE_PROVIDER, implementation: FIXTURE_PROVIDER_ID, instances: { 'mall-1': { enabled: true } } },
82
+ 'shopping-guide.shops': { provider: FIXTURE_PROVIDER, implementation: FIXTURE_PROVIDER_ID, instances: { 'mall-1': { enabled: true } } },
83
+ 'shopping-guide.categories': { provider: FIXTURE_PROVIDER, implementation: FIXTURE_PROVIDER_ID, instances: { 'mall-1': { enabled: true } } },
84
+ 'shopping-guide.directory': { provider: FIXTURE_PROVIDER, implementation: FIXTURE_PROVIDER_ID, instances: { 'mall-1': { enabled: true } } },
85
+ },
86
+ },
87
+ catalog: {
88
+ providers: {
89
+ [FIXTURE_PROVIDER]: {
90
+ [FIXTURE_PROVIDER_ID]: { provider: FIXTURE_PROVIDER, implementation: FIXTURE_PROVIDER_ID, credentialType: '-', owner: 'fixture', services: ['shopping-guide.catalog', 'shopping-guide.shops', 'shopping-guide.categories', 'shopping-guide.directory'], local: true },
91
+ },
92
+ },
93
+ services: {},
94
+ credentialTypes: {},
95
+ },
96
+ authStrategies: createAuthStrategyRegistry(),
97
+ secretProvider: createEnvironmentSecretProvider({ source: {}, production: false }),
98
+ instanceIdOf: (scope) => scope.mallId ?? '',
99
+ });
100
+ registry.registerAdapter(FIXTURE_PROVIDER, FIXTURE_PROVIDER_ID, (input) =>
101
+ portOf(portsFactory() as Record<string, unknown>, input.service));
102
+ return createServerContext({ state: createInMemoryStateStore(), integrations: registry });
103
+ }
104
+
105
+ describe('品牌导览意图声明', () => {
106
+ it('exact 整句全等:唯一 pattern「品牌导览」,不入 BM25 词袋', () => {
107
+ expect(createShoppingGuideHandler().intents).toEqual([
108
+ { patterns: ['品牌导览'], match: 'exact' },
109
+ ]);
110
+ });
111
+ });
112
+
113
+ describe('品牌导览 intent 命中(零 LLM 直出全量门店卡)', () => {
114
+ it('port 直调零过滤 + pageSize=200;shop-list schema 通过 + scope 透传 + Tab 聚合', async () => {
115
+ const serverCtx = buildFixtureServer();
116
+ const result = await createShoppingGuideHandler().handle(
117
+ 'intent',
118
+ { content: '品牌导览' },
119
+ serverCtx,
120
+ ctx(),
121
+ );
122
+
123
+ // port 调用形状:无关键词/楼层/类目 + page=1 + 特批 pageSize=200
124
+ expect(lastCriteria).toMatchObject({ keyword: '', keywords: [], page: 1, pageSize: 200 });
125
+ expect(lastCriteria).not.toHaveProperty('businessCategory');
126
+ expect(lastCriteria).not.toHaveProperty('floorCode');
127
+
128
+ // 出卡:shop-list + schema 通过 + mall scope 透传
129
+ expect(result.cards).toHaveLength(1);
130
+ const card = result.cards[0] as { cardType: string; data: unknown };
131
+ expect(card.cardType).toBe('shop-list');
132
+ const data = shopListCardDataSchema.parse(card.data);
133
+ expect(data.mallId).toBe('mall-1');
134
+ expect(data.miniAppId).toBe('app1');
135
+ expect(data.items).toHaveLength(3);
136
+ // 无过滤浏览态:类目 Tab 聚合(与可见门店值域一致)
137
+ expect(data.categoryTabs?.map((t) => t.name)).toEqual(['餐饮', '零售', '运动']);
138
+ });
139
+
140
+ it('固定文案 N 取接口分页字段 total(≠ items.length)', async () => {
141
+ const serverCtx = buildFixtureServer();
142
+ const result = await createShoppingGuideHandler().handle(
143
+ 'intent',
144
+ { content: '品牌导览' },
145
+ serverCtx,
146
+ ctx(),
147
+ );
148
+ // total=12 / items=3:文案必须是 12(接口事实数),不是 3(本页数)
149
+ expect(result.text).toBe('全商场共 12 家在营门店,下滑看看有哪些品牌,也可按类目筛选~');
150
+ });
151
+
152
+ it('零 LLM 通道不截断:items 超页容量阈值也全量入卡(pageSize 回显 200 过契约)', async () => {
153
+ // 25 家门店 > 工具路径单页常见钳制(parsePage 钳 50)/ 投影上限 200 之下的历史截断区:
154
+ // intent 零 LLM 通道卡数据直发前端——必须全量 25 家(客户端浮层/类目 Tab 覆盖全馆)
155
+ const names = Array.from({ length: 25 }, (_, i) => `门店${i + 1}号`);
156
+ const many = names.map(
157
+ (name, i) => shopOf(name, ['餐饮', '零售', '运动', '娱乐', '零售', '零售'][i % 6]),
158
+ );
159
+ const serverCtx = buildFixtureServer(() => ({
160
+ catalog: {},
161
+ shop: {
162
+ async searchShops(criteria: Record<string, unknown>) {
163
+ lastCriteria = criteria;
164
+ return { items: many, page: criteria.page, pageSize: criteria.pageSize, total: 25, hasMore: false };
165
+ },
166
+ },
167
+ category: { async listCategories() { return []; } },
168
+ directory: { async getDirectory() { return null; } },
169
+ } as never));
170
+
171
+ const result = await createShoppingGuideHandler().handle(
172
+ 'intent',
173
+ { content: '品牌导览' },
174
+ serverCtx,
175
+ ctx(),
176
+ );
177
+ const card = result.cards[0] as { cardType: string; data: unknown };
178
+ expect(card.cardType).toBe('shop-list');
179
+ const data = shopListCardDataSchema.parse(card.data);
180
+ expect(data.items).toHaveLength(25);
181
+ expect(data.pageInfo).toMatchObject({ pageSize: 200, total: 25, hasMore: false });
182
+ expect(data.categoryTabs?.map((t) => t.name)).toEqual(['餐饮', '零售', '运动', '娱乐']);
183
+ });
184
+ });
185
+
186
+ describe('品牌导览失败语义(洋葱守卫:未识别放行不吞)', () => {
187
+ it('非整句内容 → continueWith 放行 LLM', async () => {
188
+ const serverCtx = buildFixtureServer();
189
+ const result = await createShoppingGuideHandler().handle(
190
+ 'intent',
191
+ { content: '我想看看品牌导览' },
192
+ serverCtx,
193
+ ctx(),
194
+ );
195
+ expect(result.cards).toEqual([]);
196
+ expect(result.continueWith).toEqual({ content: '我想看看品牌导览' });
197
+ });
198
+
199
+ it('缺 reqCtx / 缺 mall scope → 无卡固定文案', async () => {
200
+ const serverCtx = buildFixtureServer();
201
+ const handler = createShoppingGuideHandler();
202
+ const noReq = await handler.handle('intent', { content: '品牌导览' }, serverCtx, undefined);
203
+ expect(noReq.cards).toEqual([]);
204
+ expect(noReq.text).toContain('缺少运行上下文');
205
+
206
+ const noScope = await handler.handle('intent', { content: '品牌导览' }, serverCtx, {
207
+ identity: { userId: 'u1', source: 'local' },
208
+ attributes: {},
209
+ });
210
+ expect(noScope.cards).toEqual([]);
211
+ expect(noScope.text).toContain('商场上下文缺失');
212
+ });
213
+
214
+ it('空门店结果 → 无卡兜底文案(不出空卡)', async () => {
215
+ const registry = createIntegrationsRegistry({
216
+ integrations: {
217
+ services: {
218
+ 'shopping-guide.catalog': { provider: 'fixture', implementation: 'fixture-empty@1', instances: { 'mall-1': { enabled: true } } },
219
+ 'shopping-guide.shops': { provider: 'fixture', implementation: 'fixture-empty@1', instances: { 'mall-1': { enabled: true } } },
220
+ 'shopping-guide.categories': { provider: 'fixture', implementation: 'fixture-empty@1', instances: { 'mall-1': { enabled: true } } },
221
+ 'shopping-guide.directory': { provider: 'fixture', implementation: 'fixture-empty@1', instances: { 'mall-1': { enabled: true } } },
222
+ },
223
+ },
224
+ catalog: {
225
+ providers: {
226
+ fixture: {
227
+ 'fixture-empty@1': { provider: 'fixture', implementation: 'fixture-empty@1', credentialType: '-', owner: 'fixture', services: ['shopping-guide.catalog', 'shopping-guide.shops', 'shopping-guide.categories', 'shopping-guide.directory'], local: true },
228
+ },
229
+ },
230
+ services: {},
231
+ credentialTypes: {},
232
+ },
233
+ authStrategies: createAuthStrategyRegistry(),
234
+ secretProvider: createEnvironmentSecretProvider({ source: {}, production: false }),
235
+ instanceIdOf: (scope) => scope.mallId ?? '',
236
+ });
237
+ registry.registerAdapter('fixture', 'fixture-empty@1', (input) => {
238
+ const bag = {
239
+ catalog: {},
240
+ shop: { async searchShops() { return { items: [], page: 1, pageSize: 200, total: 0, hasMore: false }; } },
241
+ category: { async listCategories() { return []; } },
242
+ directory: { async getDirectory() { return null; } },
243
+ };
244
+ return portOf(bag, input.service);
245
+ });
246
+ const serverCtx = createServerContext({ state: createInMemoryStateStore(), integrations: registry });
247
+
248
+ const result = await createShoppingGuideHandler().handle('intent', { content: '品牌导览' }, serverCtx, ctx());
249
+ expect(result.cards).toEqual([]);
250
+ expect(result.text).toBe('门店信息暂未就绪,请稍后再试');
251
+ });
252
+
253
+ it('非 intent action(handler 复用面)→ 就绪文案', async () => {
254
+ const serverCtx = buildFixtureServer();
255
+ const result = await createShoppingGuideHandler().handle('other-action', {}, serverCtx, ctx());
256
+ expect(result.cards).toEqual([]);
257
+ expect(result.text).toContain('导购服务已就绪');
258
+ });
259
+ });
@@ -0,0 +1,53 @@
1
+ /**
2
+ * G1:shop-list 卡密度治理——投影不二次截断(MAX_MERCHANT_CARD_ITEMS=200 单页顶格),pageInfo.total 保真。
3
+ * summary 总数事实源不变(tools 层头部句由 pageInfo.total 派生,shopListNoteOf 截断尾句由 items < total 派生)。
4
+ * 2026-09-02 演进 4→20→200(会话实证 6 家门店被截为 4 且浮层不可见丢失;远程 PR #200 定格 200)。
5
+ */
6
+ import { describe, expect, it } from 'vitest';
7
+ import { projectShopList, MAX_MERCHANT_CARD_ITEMS } from '../src/server/projections';
8
+ import { shopListCardDataSchema } from '@tbox.cn/app-contracts-mall';
9
+ import type { ShopRecord, ShoppingPage } from '@tbox.cn/app-contracts-mall';
10
+
11
+ const makeMerchant = (i: number): ShopRecord => ({
12
+ merchantId: `m_${i}`,
13
+ name: `门店${i}`,
14
+ coverUrls: [],
15
+ businessCategories: ['运动'],
16
+ location: { displayText: `L${i} 楼` },
17
+ status: { code: 'enabled', label: '营业中', observedAt: '2026-08-18T00:00:00Z' },
18
+ services: [],
19
+ promotionFacts: [],
20
+ observedAt: '2026-08-18T00:00:00Z',
21
+ });
22
+
23
+ const makePage = (n: number): ShoppingPage<ShopRecord> => ({
24
+ items: Array.from({ length: n }, (_, i) => makeMerchant(i)),
25
+ page: 1,
26
+ pageSize: 10,
27
+ total: n,
28
+ hasMore: false,
29
+ });
30
+
31
+ describe('G1:shop-list 不截断(前端预览 3 条 + 查看全部)', () => {
32
+ it('10 条输入 → 卡 items 不截断,pageInfo.total 保真,schema 通过', () => {
33
+ const data = projectShopList('search', makePage(10));
34
+ expect(MAX_MERCHANT_CARD_ITEMS).toBe(200);
35
+ expect(data.items).toHaveLength(10);
36
+ expect(data.pageInfo?.total).toBe(10);
37
+ expect(data.pageInfo?.pageSize).toBe(10);
38
+ expect(shopListCardDataSchema.safeParse(data).success).toBe(true);
39
+ });
40
+
41
+ it('不足上限(6 条,事故场景回归)→ 不截断,items 全量', () => {
42
+ const data = projectShopList('search', makePage(6));
43
+ expect(data.items).toHaveLength(6);
44
+ expect(data.pageInfo?.total).toBe(6);
45
+ });
46
+
47
+ it('空结果 → items 空、无 pageInfo(空结果不出卡链路上游)', () => {
48
+ const data = projectShopList('search', makePage(0), '未找到匹配的门店。');
49
+ expect(data.items).toHaveLength(0);
50
+ expect(data.pageInfo).toBeUndefined();
51
+ expect(data.summary).toBe('未找到匹配的门店。');
52
+ });
53
+ });