@tbox.cn/app-module-activity-play 0.0.0-stage → 0.3.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.
- package/INTEGRATION.md +94 -0
- package/LICENSE +21 -0
- package/README.md +30 -2
- package/dist/chunk-CM4OEPC3.js +689 -0
- package/dist/chunk-HUBEOW44.js +98 -0
- package/dist/client/index.css +107 -0
- package/dist/client/index.d.ts +11 -0
- package/dist/client/index.js +6 -0
- package/dist/index.css +107 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +46 -0
- package/dist/server/index.d.ts +309 -0
- package/dist/server/index.js +42 -0
- package/package.json +67 -3
- package/skills/activity-play/SKILL.md +55 -0
- package/src/client/cards/activity-play/ActivityPlayCard.tsx +77 -0
- package/src/client/cards/activity-play/index.ts +1 -0
- package/src/client/cards/view.ts +42 -0
- package/src/client/index.ts +22 -0
- package/src/client/styles/activity-play.css +136 -0
- package/src/index.ts +6 -0
- package/src/server/cards/activity-play/meta.ts +26 -0
- package/src/server/cards/activity-play/samples.ts +116 -0
- package/src/server/cards/activity-play-prize-detail/meta.ts +29 -0
- package/src/server/cards/activity-play-prize-detail/samples.ts +54 -0
- package/src/server/cards/activity-play-prize-list/meta.ts +41 -0
- package/src/server/cards/activity-play-prize-list/samples.ts +78 -0
- package/src/server/cards/index.ts +7 -0
- package/src/server/errors.ts +101 -0
- package/src/server/index.ts +72 -0
- package/src/server/ports.ts +50 -0
- package/src/server/prizes/cards.ts +79 -0
- package/src/server/prizes/errors.ts +88 -0
- package/src/server/prizes/query.ts +101 -0
- package/src/server/prizes/subject.ts +29 -0
- package/src/server/query.ts +98 -0
- package/src/server/routes.ts +371 -0
- package/src/server/tools.ts +187 -0
- package/tbox.module.json +65 -0
- package/tests/cards-capture.test.ts +110 -0
- package/tests/entry.test.ts +458 -0
- package/tests/ports.test.ts +83 -0
- package/tests/prizes.test.ts +357 -0
- package/tests/routes.test.ts +271 -0
- package/tests/samples.test.ts +53 -0
- package/tests/smoke.test.ts +27 -0
- package/tests/tools.test.ts +251 -0
- package/tsconfig.json +8 -0
- package/tsup.config.ts +27 -0
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本人奖品链行为测试(任务07 步骤 1——替身 Port 注入,验证查询核心/主体/出卡适配;
|
|
3
|
+
* 生产接线(registry 解析 + HTTP/工具出口)在步骤 2 落地后补集成面)。
|
|
4
|
+
*
|
|
5
|
+
* 覆盖风险 ≡ 验收点:
|
|
6
|
+
* - 主体纪律:身份检查先于供给呼叫(缺用户/anonymous → 零奖品 API 调用);
|
|
7
|
+
* userid 原值传递(不数字化/不重派生);连续与并发 A/B 不串主体;
|
|
8
|
+
* - 标识纪律:prizeRecordRef 原样定位('001'/'1.20'/'1e3');供给返回另一标识
|
|
9
|
+
* 不得以请求标识包装输出;
|
|
10
|
+
* - 供给自证:必缺/结构非法/未知键 → PROVIDER_ERROR(不丢坏记录冒称未中奖);
|
|
11
|
+
* - 错误分类:认证错误不降格 502(任务06/07 约定);配置缺口 → 503;不存在与
|
|
12
|
+
* 不可见同文案 PRIZE_NOT_FOUND;
|
|
13
|
+
* - 出卡契约:列表条目恒附 send 标准动作(value 含记录标识);空态 noResultsReason;
|
|
14
|
+
* 失败不出「未中奖」卡;详情平铺。
|
|
15
|
+
*/
|
|
16
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
17
|
+
import type { RequestContext } from '@tbox.cn/app-contracts';
|
|
18
|
+
import { PROVIDER_ERROR_CODES } from '@tbox.cn/app-contracts';
|
|
19
|
+
import type { ActivityPlayPrizeQueryPort } from '@tbox.cn/app-contracts-mall';
|
|
20
|
+
import {
|
|
21
|
+
activityPlayPrizeDetailCardDataSchema,
|
|
22
|
+
activityPlayPrizeListCardDataSchema,
|
|
23
|
+
} from '@tbox.cn/app-contracts-mall';
|
|
24
|
+
import { requirePrizeSubjectId } from '../src/server/prizes/subject';
|
|
25
|
+
import {
|
|
26
|
+
normalizePrizeRecordRef,
|
|
27
|
+
queryMyPrizeDetail,
|
|
28
|
+
queryMyPrizeList,
|
|
29
|
+
requirePrizeQueryContext,
|
|
30
|
+
} from '../src/server/prizes/query';
|
|
31
|
+
import {
|
|
32
|
+
ActivityPlayPrizeHttpError,
|
|
33
|
+
toActivityPlayPrizeError,
|
|
34
|
+
} from '../src/server/prizes/errors';
|
|
35
|
+
import {
|
|
36
|
+
PRIZE_NO_RESULTS_TEXT,
|
|
37
|
+
toPrizeDetailCardData,
|
|
38
|
+
toPrizeListCardData,
|
|
39
|
+
} from '../src/server/prizes/cards';
|
|
40
|
+
import prizeListMeta from '../src/server/cards/activity-play-prize-list/meta';
|
|
41
|
+
import prizeDetailMeta from '../src/server/cards/activity-play-prize-detail/meta';
|
|
42
|
+
|
|
43
|
+
// ===== fixtures =====
|
|
44
|
+
|
|
45
|
+
type Identity = NonNullable<RequestContext['identity']>;
|
|
46
|
+
|
|
47
|
+
const reqCtxOf = (identity: Identity, mallId?: string): RequestContext => ({
|
|
48
|
+
identity,
|
|
49
|
+
...(mallId ? { attributes: { mall: { mallId } } } : {}),
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
/** 无可信身份形态(上下文在场但 identity 缺席——装配旁路防御分支) */
|
|
53
|
+
const noIdentityCtx = (mallId?: string): RequestContext =>
|
|
54
|
+
({ ...(mallId ? { attributes: { mall: { mallId } } } : {}) }) as RequestContext;
|
|
55
|
+
|
|
56
|
+
const localCtx = (mallId = 'mall-a', userId = '2088000000000001'): RequestContext =>
|
|
57
|
+
reqCtxOf({ userId, source: 'local' }, mallId);
|
|
58
|
+
|
|
59
|
+
const RECORD = {
|
|
60
|
+
prizeRecordRef: 'prize-demo-001',
|
|
61
|
+
title: '咖啡兑换券',
|
|
62
|
+
prizeType: 'COUPON',
|
|
63
|
+
claimStatus: 'CLAIMED',
|
|
64
|
+
useStatus: 'UNUSED',
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const DETAIL = {
|
|
68
|
+
...RECORD,
|
|
69
|
+
redemptionMethod: '门店柜台',
|
|
70
|
+
redemptionInstruction: '请按活动页面的领取及使用指引操作',
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/** 替身 Port(记录最近一次调用的 q/ref——主体与标识断言锚点) */
|
|
74
|
+
function makePort(opts: Partial<{
|
|
75
|
+
listResult: unknown;
|
|
76
|
+
detailResult: unknown;
|
|
77
|
+
listImpl: (q: unknown) => Promise<unknown>;
|
|
78
|
+
}> = {}): ActivityPlayPrizeQueryPort & {
|
|
79
|
+
listMyPrizes: ReturnType<typeof vi.fn>;
|
|
80
|
+
getMyPrizeDetail: ReturnType<typeof vi.fn>;
|
|
81
|
+
} {
|
|
82
|
+
return {
|
|
83
|
+
listMyPrizes: vi.fn(async (q: unknown) =>
|
|
84
|
+
'listImpl' in opts && opts.listImpl
|
|
85
|
+
? opts.listImpl(q)
|
|
86
|
+
: ('listResult' in opts ? opts.listResult : { total: 1, items: [RECORD] })),
|
|
87
|
+
getMyPrizeDetail: vi.fn(async (_q: unknown, _ref: string) =>
|
|
88
|
+
('detailResult' in opts ? opts.detailResult : DETAIL)),
|
|
89
|
+
} as never;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ===== 主体纪律(任务06 约定) =====
|
|
93
|
+
|
|
94
|
+
describe('requirePrizeSubjectId(严格主体检查)', () => {
|
|
95
|
+
it('local/external/guest 均返回 userId 原值(不数字化/不加减前缀)', () => {
|
|
96
|
+
for (const source of ['local', 'external', 'guest'] as const) {
|
|
97
|
+
const v = requirePrizeSubjectId(reqCtxOf({ userId: '2088112233445566', source }));
|
|
98
|
+
expect(v).toBe('2088112233445566');
|
|
99
|
+
}
|
|
100
|
+
expect(requirePrizeSubjectId(reqCtxOf({ userId: '001', source: 'local' }))).toBe('001');
|
|
101
|
+
expect(requirePrizeSubjectId(reqCtxOf({ userId: '1.20', source: 'guest' }))).toBe('1.20');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('anonymous/缺席/空值 → LOGIN_REQUIRED(401)', () => {
|
|
105
|
+
for (const bad of [
|
|
106
|
+
reqCtxOf({ userId: 'u1', source: 'anonymous' }),
|
|
107
|
+
noIdentityCtx(),
|
|
108
|
+
reqCtxOf({ userId: '', source: 'local' }),
|
|
109
|
+
reqCtxOf({ userId: ' ', source: 'local' }),
|
|
110
|
+
]) {
|
|
111
|
+
expect(() => requirePrizeSubjectId(bad)).toThrow(ActivityPlayPrizeHttpError);
|
|
112
|
+
try {
|
|
113
|
+
requirePrizeSubjectId(bad);
|
|
114
|
+
} catch (e) {
|
|
115
|
+
expect((e as ActivityPlayPrizeHttpError).code).toBe('LOGIN_REQUIRED');
|
|
116
|
+
expect((e as ActivityPlayPrizeHttpError).httpStatus).toBe(401);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// ===== 查询核心(检查序/供给自证/标识原样) =====
|
|
123
|
+
|
|
124
|
+
describe('queryMyPrizeList / queryMyPrizeDetail', () => {
|
|
125
|
+
it('缺商圈 → SCOPE_REQUIRED(400)', async () => {
|
|
126
|
+
const port = makePort();
|
|
127
|
+
const noMallCtx = reqCtxOf({ userId: '2088000000000001', source: 'local' });
|
|
128
|
+
await expect(queryMyPrizeList(noMallCtx, port)).rejects.toMatchObject({
|
|
129
|
+
code: 'SCOPE_REQUIRED',
|
|
130
|
+
httpStatus: 400,
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('身份检查先于供给呼叫:缺用户/anonymous → LOGIN_REQUIRED 且零奖品 API 调用', async () => {
|
|
135
|
+
for (const bad of [
|
|
136
|
+
noIdentityCtx('mall-a'),
|
|
137
|
+
reqCtxOf({ userId: 'u1', source: 'anonymous' }, 'mall-a'),
|
|
138
|
+
]) {
|
|
139
|
+
const listPort = makePort();
|
|
140
|
+
await expect(queryMyPrizeList(bad, listPort)).rejects.toMatchObject({
|
|
141
|
+
code: 'LOGIN_REQUIRED',
|
|
142
|
+
httpStatus: 401,
|
|
143
|
+
});
|
|
144
|
+
expect(listPort.listMyPrizes).not.toHaveBeenCalled();
|
|
145
|
+
|
|
146
|
+
const detailPort = makePort();
|
|
147
|
+
await expect(queryMyPrizeDetail(bad, detailPort, 'prize-demo-001')).rejects.toMatchObject({
|
|
148
|
+
code: 'LOGIN_REQUIRED',
|
|
149
|
+
});
|
|
150
|
+
expect(detailPort.getMyPrizeDetail).not.toHaveBeenCalled();
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('连续与并发 A/B 用户不串主体:Port 收到的 subjectId 逐次对应', async () => {
|
|
155
|
+
const port = makePort({ listResult: { total: 0, items: [] } });
|
|
156
|
+
await queryMyPrizeList(localCtx('mall-a', 'user-A'), port);
|
|
157
|
+
await queryMyPrizeList(localCtx('mall-a', 'user-B'), port);
|
|
158
|
+
await Promise.all([
|
|
159
|
+
queryMyPrizeList(localCtx('mall-a', 'user-A'), port),
|
|
160
|
+
queryMyPrizeList(localCtx('mall-a', 'user-B'), port),
|
|
161
|
+
]);
|
|
162
|
+
const subjects = port.listMyPrizes.mock.calls.map(([q]) => (q as { subjectId: string }).subjectId);
|
|
163
|
+
expect(subjects).toEqual(['user-A', 'user-B', 'user-A', 'user-B']);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('组合子与严格读取同值自证(subjectId == identity.userId 原字符串)', () => {
|
|
167
|
+
const { q, subjectId } = requirePrizeQueryContext(localCtx('mall-a', 'user-X'));
|
|
168
|
+
expect(q.subjectId).toBe('user-X');
|
|
169
|
+
expect(subjectId).toBe('user-X');
|
|
170
|
+
expect(q.scope.mallId).toBe('mall-a');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('prizeRecordRef 原样定位:001 / 1.20 / 1e3 不数字化过 Port;首尾空白是标识原值不 trim 改写', async () => {
|
|
174
|
+
for (const ref of ['001', '1.20', '1e3', ' record-001 ']) {
|
|
175
|
+
const port = makePort({ detailResult: { ...DETAIL, prizeRecordRef: ref } });
|
|
176
|
+
const detail = await queryMyPrizeDetail(localCtx(), port, ref);
|
|
177
|
+
expect(detail.prizeRecordRef).toBe(ref);
|
|
178
|
+
expect(port.getMyPrizeDetail).toHaveBeenCalledWith(expect.anything(), ref);
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('ref 非法(全空白/超长)→ INVALID_REQUEST 且零供给调用;非全空白原值直传(trim 只判空白不改写)', async () => {
|
|
183
|
+
for (const bad of ['', ' ', '\t', 'x'.repeat(129)]) {
|
|
184
|
+
const port = makePort();
|
|
185
|
+
await expect(queryMyPrizeDetail(localCtx(), port, bad)).rejects.toMatchObject({
|
|
186
|
+
code: 'INVALID_REQUEST',
|
|
187
|
+
httpStatus: 400,
|
|
188
|
+
});
|
|
189
|
+
expect(port.getMyPrizeDetail).not.toHaveBeenCalled();
|
|
190
|
+
}
|
|
191
|
+
expect(normalizePrizeRecordRef(' 1.20 ')).toBe(' 1.20 ');
|
|
192
|
+
expect(normalizePrizeRecordRef(' record-001 ')).toBe(' record-001 ');
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('供给列表输出非法(必缺/负 total/未知键)→ PROVIDER_ERROR(不丢坏记录冒称未中奖)', async () => {
|
|
196
|
+
for (const bad of [
|
|
197
|
+
{ total: 1, items: [{ ...RECORD, title: '' }] },
|
|
198
|
+
{ total: -1, items: [] },
|
|
199
|
+
{ items: [] },
|
|
200
|
+
{ total: 1, items: [RECORD], rawJson: 'x' },
|
|
201
|
+
{ total: 1, items: [{ ...RECORD, prizeType: 7 }] },
|
|
202
|
+
]) {
|
|
203
|
+
const port: unknown = makePort({ listResult: bad });
|
|
204
|
+
await expect(queryMyPrizeList(localCtx(), port as ActivityPlayPrizeQueryPort)).rejects.toMatchObject({
|
|
205
|
+
code: 'PROVIDER_ERROR',
|
|
206
|
+
httpStatus: 502,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('详情 null → PRIZE_NOT_FOUND(404)(不存在与不可见同文案)', async () => {
|
|
212
|
+
const port = makePort({ detailResult: null });
|
|
213
|
+
await expect(queryMyPrizeDetail(localCtx(), port, 'prize-demo-001')).rejects.toMatchObject({
|
|
214
|
+
code: 'PRIZE_NOT_FOUND',
|
|
215
|
+
httpStatus: 404,
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('供给返回另一记录标识 → PROVIDER_ERROR(不得以请求标识包装输出)', async () => {
|
|
220
|
+
const port = makePort({ detailResult: { ...DETAIL, prizeRecordRef: 'prize-other-002' } });
|
|
221
|
+
await expect(queryMyPrizeDetail(localCtx(), port, 'prize-demo-001')).rejects.toMatchObject({
|
|
222
|
+
code: 'PROVIDER_ERROR',
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
it('列表→详情按条目原值标识定位:带空白 ref 原样回查、不改写不误判 502', async () => {
|
|
227
|
+
// 评审返修回归:供应商可返回 ' record-001 ' 形标识——列表条目 ref 必须原值喂给详情查询
|
|
228
|
+
const spacedRef = ' record-001 ';
|
|
229
|
+
const port = {
|
|
230
|
+
listMyPrizes: vi.fn(async () => ({ total: 1, items: [{ ...RECORD, prizeRecordRef: spacedRef }] })),
|
|
231
|
+
getMyPrizeDetail: vi.fn(async (_q: unknown, ref: string) => ({ ...DETAIL, prizeRecordRef: ref })),
|
|
232
|
+
};
|
|
233
|
+
const list = await queryMyPrizeList(localCtx(), port);
|
|
234
|
+
const card = toPrizeListCardData({ mallId: 'mall-a', result: list });
|
|
235
|
+
const refFromCard = card.items[0]!.prizeRecordRef;
|
|
236
|
+
expect(refFromCard).toBe(spacedRef);
|
|
237
|
+
|
|
238
|
+
const detail = await queryMyPrizeDetail(localCtx(), port, refFromCard);
|
|
239
|
+
expect(detail.prizeRecordRef).toBe(spacedRef);
|
|
240
|
+
expect(port.getMyPrizeDetail).toHaveBeenCalledWith(expect.anything(), spacedRef);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('供给结构非法(详情缺必填/未知键)→ PROVIDER_ERROR', async () => {
|
|
244
|
+
for (const bad of [{ ...DETAIL, claimStatus: '' }, { ...DETAIL, extra: 1 }]) {
|
|
245
|
+
const port = makePort({ detailResult: bad });
|
|
246
|
+
await expect(queryMyPrizeDetail(localCtx(), port, 'prize-demo-001')).rejects.toMatchObject({
|
|
247
|
+
code: 'PROVIDER_ERROR',
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
// ===== 错误分类(认证不降格——任务06/07 约定) =====
|
|
254
|
+
|
|
255
|
+
describe('toActivityPlayPrizeError(映射守门)', () => {
|
|
256
|
+
it('本域认证错误原样通过(LOGIN_REQUIRED 不被降格为 502 上游失败)', () => {
|
|
257
|
+
const err = toActivityPlayPrizeError(ActivityPlayPrizeHttpError.loginRequired('缺少当前用户'));
|
|
258
|
+
expect(err.code).toBe('LOGIN_REQUIRED');
|
|
259
|
+
expect(err.httpStatus).toBe(401);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('供给配置缺口(未配置/实例缺席/未实现/绑定未解析)→ NOT_CONFIGURED(503)', () => {
|
|
263
|
+
for (const code of [
|
|
264
|
+
PROVIDER_ERROR_CODES.SERVICE_NOT_CONFIGURED,
|
|
265
|
+
PROVIDER_ERROR_CODES.INSTANCE_NOT_FOUND,
|
|
266
|
+
PROVIDER_ERROR_CODES.INSTANCE_DISABLED,
|
|
267
|
+
PROVIDER_ERROR_CODES.PROVIDER_NOT_FOUND,
|
|
268
|
+
PROVIDER_ERROR_CODES.PROVIDER_NOT_IMPLEMENTED,
|
|
269
|
+
PROVIDER_ERROR_CODES.BINDING_UNRESOLVED,
|
|
270
|
+
]) {
|
|
271
|
+
const err = toActivityPlayPrizeError(Object.assign(new Error('raw'), { code }));
|
|
272
|
+
expect(err.code).toBe('NOT_CONFIGURED');
|
|
273
|
+
expect(err.httpStatus).toBe(503);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('未知异常 → PROVIDER_ERROR(502),原始 message 不透出', () => {
|
|
278
|
+
const err = toActivityPlayPrizeError(new Error('凭证 sk-xxx 泄漏面'));
|
|
279
|
+
expect(err.code).toBe('PROVIDER_ERROR');
|
|
280
|
+
expect(err.httpStatus).toBe(502);
|
|
281
|
+
expect(err.message).not.toContain('sk-xxx');
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// ===== 出卡适配(卡片 data 面) =====
|
|
286
|
+
|
|
287
|
+
describe('prizes/cards 适配', () => {
|
|
288
|
+
it('列表多条目:条目恒附 send 标准动作(value 含记录标识、visibleValue 可读名)', () => {
|
|
289
|
+
const data = toPrizeListCardData({
|
|
290
|
+
mallId: 'mall-a',
|
|
291
|
+
result: { total: 2, items: [RECORD, { ...RECORD, prizeRecordRef: 'prize-demo-002', title: '周年庆礼盒' }] },
|
|
292
|
+
});
|
|
293
|
+
activityPlayPrizeListCardDataSchema.parse(data);
|
|
294
|
+
expect(data.items).toHaveLength(2);
|
|
295
|
+
for (const item of data.items) {
|
|
296
|
+
expect(item.itemActions).toHaveLength(1);
|
|
297
|
+
const [action] = item.itemActions!;
|
|
298
|
+
if (action.kind !== 'send') throw new Error('奖品列表动作须为 send');
|
|
299
|
+
expect(action.value).toContain(item.prizeRecordRef);
|
|
300
|
+
expect(action.visibleValue).toContain(item.title);
|
|
301
|
+
expect(action.label).toBe('查看详情');
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('空态:total=0 → noResultsReason;total>0 空 items 保留事实不加空态文案', () => {
|
|
306
|
+
const empty = toPrizeListCardData({ mallId: 'mall-a', result: { total: 0, items: [] } });
|
|
307
|
+
activityPlayPrizeListCardDataSchema.parse(empty);
|
|
308
|
+
expect(empty.noResultsReason).toBe(PRIZE_NO_RESULTS_TEXT);
|
|
309
|
+
|
|
310
|
+
const pagedEmpty = toPrizeListCardData({ mallId: 'mall-a', result: { total: 5, items: [] } });
|
|
311
|
+
activityPlayPrizeListCardDataSchema.parse(pagedEmpty);
|
|
312
|
+
expect(pagedEmpty.noResultsReason).toBeUndefined();
|
|
313
|
+
expect(pagedEmpty.total).toBe(5);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it('超过展示上限(50)截断:卡过自身契约、total 保留真实值 + truncated;恰好 50 无标记', () => {
|
|
317
|
+
const many = Array.from({ length: 51 }, (_, i) => ({
|
|
318
|
+
...RECORD,
|
|
319
|
+
prizeRecordRef: `prize-demo-${String(i).padStart(3, '0')}`,
|
|
320
|
+
}));
|
|
321
|
+
const overflowed = toPrizeListCardData({ mallId: 'mall-a', result: { total: 51, items: many } });
|
|
322
|
+
// 评审返修回归:51 条记录不得生成违反卡 schema 的卡片(items.max(50))
|
|
323
|
+
activityPlayPrizeListCardDataSchema.parse(overflowed);
|
|
324
|
+
expect(overflowed.items).toHaveLength(50);
|
|
325
|
+
expect(overflowed.total).toBe(51);
|
|
326
|
+
expect(overflowed.truncated).toBe(true);
|
|
327
|
+
// 截断面保序,末条目动作与其自身标识对应(先收窄 send 变体再读 value——联合收窄)
|
|
328
|
+
const last = overflowed.items[49]!;
|
|
329
|
+
const lastAction = last.itemActions![0]!;
|
|
330
|
+
if (lastAction.kind !== 'send') throw new Error('奖品列表动作须为 send');
|
|
331
|
+
expect(lastAction.value).toContain('prize-demo-049');
|
|
332
|
+
|
|
333
|
+
const exact = toPrizeListCardData({ mallId: 'mall-a', result: { total: 50, items: many.slice(0, 50) } });
|
|
334
|
+
activityPlayPrizeListCardDataSchema.parse(exact);
|
|
335
|
+
expect(exact.truncated).toBeUndefined();
|
|
336
|
+
expect(exact.total).toBe(50);
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it('详情平铺:{mallId, ...detail} 过卡契约(无 mode/items/prize 包装键)', () => {
|
|
340
|
+
const data = toPrizeDetailCardData({ mallId: 'mall-a', detail: DETAIL });
|
|
341
|
+
activityPlayPrizeDetailCardDataSchema.parse(data);
|
|
342
|
+
expect('mode' in data).toBe(false);
|
|
343
|
+
expect('items' in data).toBe(false);
|
|
344
|
+
expect('prize' in data).toBe(false);
|
|
345
|
+
expect(data.mallId).toBe('mall-a');
|
|
346
|
+
expect(data.prizeRecordRef).toBe('prize-demo-001');
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('meta/samples:卡型常量与 schema 对齐;全样本与 sampleData 过卡契约', () => {
|
|
350
|
+
expect(prizeListMeta.cardType).toBe('activity-play-prize-list');
|
|
351
|
+
expect(prizeDetailMeta.cardType).toBe('activity-play-prize-detail');
|
|
352
|
+
activityPlayPrizeListCardDataSchema.parse(prizeListMeta.sampleData);
|
|
353
|
+
activityPlayPrizeDetailCardDataSchema.parse(prizeDetailMeta.sampleData);
|
|
354
|
+
for (const s of prizeListMeta.samples ?? []) activityPlayPrizeListCardDataSchema.parse(s.data);
|
|
355
|
+
for (const s of prizeDetailMeta.samples ?? []) activityPlayPrizeDetailCardDataSchema.parse(s.data);
|
|
356
|
+
});
|
|
357
|
+
});
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 活动玩法 HTTP 接口行为测试(supertest 装配真实 Router + 会话上下文中间件)。
|
|
3
|
+
* 覆盖风险≡验收点:scope 冲突/补齐、参数合法性、错误域区分(503/502/404)、
|
|
4
|
+
* 空目录非错误、错误信封形状、客户端身份字段不可覆盖查询主体。
|
|
5
|
+
*/
|
|
6
|
+
import { describe, expect, it, vi, beforeEach } from 'vitest';
|
|
7
|
+
import express from 'express';
|
|
8
|
+
import type { Express, Request, Response, NextFunction } from 'express';
|
|
9
|
+
import request from 'supertest';
|
|
10
|
+
import type { ServerContext } from '@tbox.cn/app-agent-sdk-server';
|
|
11
|
+
import type { RequestContext } from '@tbox.cn/app-contracts';
|
|
12
|
+
import {
|
|
13
|
+
ACTIVITY_PLAY_QUERY_SERVICE,
|
|
14
|
+
withMallScopeFromQuery,
|
|
15
|
+
type ActivityPlayQueryContext,
|
|
16
|
+
type ActivityPlayQueryPort,
|
|
17
|
+
} from '@tbox.cn/app-contracts-mall';
|
|
18
|
+
import { createActivityPlayRouter } from '../src/server/routes';
|
|
19
|
+
|
|
20
|
+
/** 会话基础上下文(登录态 guest——等同浏览器轴 guest 登录产物) */
|
|
21
|
+
const baseReqCtx = (): RequestContext => ({
|
|
22
|
+
identity: { userId: 'guest_abc', source: 'guest' },
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
interface PortCall {
|
|
26
|
+
q: ActivityPlayQueryContext;
|
|
27
|
+
input?: { keyword?: string; limit?: number };
|
|
28
|
+
activityRef?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** 可观测 Port 替身(记录调用 + 可配置返回/抛错) */
|
|
32
|
+
function makePort(overrides: Partial<ActivityPlayQueryPort> = {}) {
|
|
33
|
+
const calls: PortCall[] = [];
|
|
34
|
+
const port: ActivityPlayQueryPort = {
|
|
35
|
+
async listActivities(q, input) {
|
|
36
|
+
calls.push({ q, input });
|
|
37
|
+
return { items: [] };
|
|
38
|
+
},
|
|
39
|
+
async getActivity(q, activityRef) {
|
|
40
|
+
calls.push({ q, activityRef });
|
|
41
|
+
return null;
|
|
42
|
+
},
|
|
43
|
+
...overrides,
|
|
44
|
+
};
|
|
45
|
+
return { port, calls };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface Harness {
|
|
49
|
+
app: Express;
|
|
50
|
+
calls: PortCall[];
|
|
51
|
+
resolveService: ReturnType<typeof vi.fn>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 装配被测 App:auth 形中间件写入 reqCtx → 挂模块 Router(stub ctx)。
|
|
55
|
+
* 缺省 port = 可观测替身(空目录);自定义 port 时 calls 不记录(相应用例不校验调用面)。 */
|
|
56
|
+
function makeApp(opts: {
|
|
57
|
+
reqCtx: RequestContext | undefined;
|
|
58
|
+
port?: ActivityPlayQueryPort;
|
|
59
|
+
resolveError?: unknown;
|
|
60
|
+
}): Harness {
|
|
61
|
+
const { port: defaultPort, calls } = makePort();
|
|
62
|
+
const resolveService = vi.fn(async () => {
|
|
63
|
+
if (opts.resolveError) throw opts.resolveError;
|
|
64
|
+
return opts.port ?? defaultPort;
|
|
65
|
+
});
|
|
66
|
+
const ctxStub = {
|
|
67
|
+
integrations: { resolveService },
|
|
68
|
+
logger: { warn: vi.fn() },
|
|
69
|
+
} as unknown as ServerContext;
|
|
70
|
+
const app = express();
|
|
71
|
+
app.use((req: Request, _res: Response, next: NextFunction) => {
|
|
72
|
+
if (opts.reqCtx) (req as unknown as Record<string, unknown>)['tboxAuthContext'] = opts.reqCtx;
|
|
73
|
+
next();
|
|
74
|
+
});
|
|
75
|
+
app.use(createActivityPlayRouter(ctxStub));
|
|
76
|
+
return { app, calls, resolveService };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const ITEM = {
|
|
80
|
+
activityRef: 'demo:wheel',
|
|
81
|
+
title: '幸运转盘',
|
|
82
|
+
status: 'active',
|
|
83
|
+
loginRequired: false,
|
|
84
|
+
} as const;
|
|
85
|
+
|
|
86
|
+
describe('GET /api/m/activity-play/activities(列表)', () => {
|
|
87
|
+
it('正常查询:query mallId 注入,keyword 透传,limit 缺省 20;信封 {"data":{"items":[]}}', async () => {
|
|
88
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
89
|
+
const res = await request(h.app).get('/activities?mallId=mall-a');
|
|
90
|
+
expect(res.status).toBe(200);
|
|
91
|
+
expect(res.body).toEqual({ data: { items: [] } });
|
|
92
|
+
expect(h.calls).toHaveLength(1);
|
|
93
|
+
expect(h.calls[0]?.input).toEqual({ limit: 20 });
|
|
94
|
+
expect(h.calls[0]?.q.scope.mallId).toBe('mall-a');
|
|
95
|
+
expect(h.resolveService).toHaveBeenCalledWith(
|
|
96
|
+
expect.objectContaining({ service: ACTIVITY_PLAY_QUERY_SERVICE, scope: { mallId: 'mall-a' } }),
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('空目录(配置完整零活动)= 200 空 items,绝不当错误或回退演示数据', async () => {
|
|
101
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
102
|
+
const res = await request(h.app).get('/activities?mallId=mall-a');
|
|
103
|
+
expect(res.status).toBe(200);
|
|
104
|
+
expect(res.body.data.items).toEqual([]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('验收返修:普通字符串字面量是合法输入(keyword/mallId 值 = "invalid" 不得误判参数错误)', async () => {
|
|
108
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
109
|
+
// keyword=invalid:只是匹配不到任何活动(大小写归 provider,路由不掺和)
|
|
110
|
+
expect((await request(h.app).get('/activities?mallId=mall-a&keyword=invalid')).status).toBe(200);
|
|
111
|
+
expect(h.calls.at(-1)?.input?.keyword).toBe('invalid');
|
|
112
|
+
expect(h.calls.at(-1)?.q.scope.mallId).toBe('mall-a');
|
|
113
|
+
expect((await request(h.app).get('/activities?mallId=mall-a&keyword=INVALID')).status).toBe(200);
|
|
114
|
+
// mallId=invalid:形状合法,路由放行(实例白名单裁决归 registry;stub 恒出品)
|
|
115
|
+
expect((await request(h.app).get('/activities?mallId=invalid')).status).toBe(200);
|
|
116
|
+
expect(h.calls.at(-1)?.q.scope.mallId).toBe('invalid');
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it('keyword/mallId 数组多值仍显式 400 INVALID_REQUEST(非哨兵语义变化不影响)', async () => {
|
|
120
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
121
|
+
expect((await request(h.app).get('/activities?mallId=mall-a&keyword=a&keyword=b')).status).toBe(400);
|
|
122
|
+
expect((await request(h.app).get('/activities?mallId=a&mallId=b')).status).toBe(400);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('keyword/limit 契约:keyword 命中透传;limit=1 与 50 合法、0/51/非数字 → 400 INVALID_REQUEST', async () => {
|
|
126
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
127
|
+
expect((await request(h.app).get('/activities?mallId=mall-a&keyword=%E8%BD%AC%E7%9B%98&limit=3')).status).toBe(200);
|
|
128
|
+
expect(h.calls[0]?.input).toEqual({ keyword: '转盘', limit: 3 });
|
|
129
|
+
|
|
130
|
+
expect((await request(h.app).get('/activities?mallId=mall-a&limit=1')).status).toBe(200);
|
|
131
|
+
expect((await request(h.app).get('/activities?mallId=mall-a&limit=50')).status).toBe(200);
|
|
132
|
+
// 评审返修:limit 空串/纯空白 = 非法值 400(不静默当缺省——任务 01 语义回归)
|
|
133
|
+
expect((await request(h.app).get('/activities?mallId=mall-a&limit=')).status).toBe(400);
|
|
134
|
+
expect((await request(h.app).get('/activities?mallId=mall-a&limit=%20')).status).toBe(400);
|
|
135
|
+
for (const bad of ['0', '51', 'abc', '-1', '20.5', '20.0', '2e1']) {
|
|
136
|
+
const res = await request(h.app).get(`/activities?mallId=mall-a&limit=${bad}`);
|
|
137
|
+
expect(res.status, `limit=${bad}`).toBe(400);
|
|
138
|
+
expect(res.body.error.code).toBe('INVALID_REQUEST');
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('商圈缺口:无 query mallId 且上下文无绑定 → 400 SCOPE_REQUIRED;畸形 mallId → 400', async () => {
|
|
143
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
144
|
+
const res = await request(h.app).get('/activities');
|
|
145
|
+
expect(res.status).toBe(400);
|
|
146
|
+
expect(res.body.error.code).toBe('SCOPE_REQUIRED');
|
|
147
|
+
|
|
148
|
+
const res2 = await request(h.app).get('/activities?mallId=bad%20mall%24');
|
|
149
|
+
expect(res2.status).toBe(400);
|
|
150
|
+
expect(res2.body.error.code).toBe('INVALID_REQUEST');
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('商圈冲突:上下文已绑定 mall-a,query mall-b → 403 SCOPE_MISMATCH;一致则放行', async () => {
|
|
154
|
+
const bound = withMallScopeFromQuery(baseReqCtx(), { mallId: 'mall-a' });
|
|
155
|
+
const h = makeApp({ reqCtx: bound });
|
|
156
|
+
const res = await request(h.app).get('/activities?mallId=mall-b');
|
|
157
|
+
expect(res.status).toBe(403);
|
|
158
|
+
expect(res.body.error.code).toBe('SCOPE_MISMATCH');
|
|
159
|
+
|
|
160
|
+
const ok = await request(h.app).get('/activities?mallId=mall-a');
|
|
161
|
+
expect(ok.status).toBe(200);
|
|
162
|
+
expect(h.calls[0]?.q.scope.mallId).toBe('mall-a');
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('仅有绑定(无 query)→ 用绑定商圈解析;不要求显式 mallId 透传', async () => {
|
|
166
|
+
const bound = withMallScopeFromQuery(baseReqCtx(), { mallId: 'mall-a' });
|
|
167
|
+
const h = makeApp({ reqCtx: bound });
|
|
168
|
+
const res = await request(h.app).get('/activities');
|
|
169
|
+
expect(res.status).toBe(200);
|
|
170
|
+
expect(h.calls[0]?.q.scope.mallId).toBe('mall-a');
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('客户端身份字段不可覆盖查询主体:userId query 只是被忽略的未知参数', async () => {
|
|
174
|
+
const bound = withMallScopeFromQuery(baseReqCtx(), { mallId: 'mall-a' });
|
|
175
|
+
const h = makeApp({ reqCtx: bound });
|
|
176
|
+
await request(h.app).get('/activities?mallId=mall-a&userId=attacker&openid=x&memberId=1');
|
|
177
|
+
expect(h.calls[0]?.q.subjectId).toBe('guest_abc');
|
|
178
|
+
expect(h.calls[0]?.q.requestContext.identity.userId).toBe('guest_abc');
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('匿名身份:subjectId 缺席(目录查询不因匿名拒绝——登录要求归入口任务)', async () => {
|
|
182
|
+
const anonymous: RequestContext = { identity: { userId: 'anon_x', source: 'anonymous' } };
|
|
183
|
+
const h = makeApp({ reqCtx: anonymous });
|
|
184
|
+
const res = await request(h.app).get('/activities?mallId=mall-a');
|
|
185
|
+
expect(res.status).toBe(200);
|
|
186
|
+
expect(h.calls[0]?.q.subjectId).toBeUndefined();
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('供给配置缺席族 → 503 NOT_CONFIGURED(三个预期缺席码逐一)', async () => {
|
|
190
|
+
for (const code of ['PROVIDER_SERVICE_NOT_CONFIGURED', 'PROVIDER_INSTANCE_NOT_FOUND', 'PROVIDER_INSTANCE_DISABLED']) {
|
|
191
|
+
const h = makeApp({ reqCtx: baseReqCtx(), resolveError: Object.assign(new Error('boom'), { code }) });
|
|
192
|
+
const res = await request(h.app).get('/activities?mallId=mall-a');
|
|
193
|
+
expect(res.status, code).toBe(503);
|
|
194
|
+
expect(res.body.error.code).toBe('NOT_CONFIGURED');
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('供给绑定解析/未实施族 → 503 NOT_CONFIGURED;装配缺口显性可区分', async () => {
|
|
199
|
+
for (const code of ['PROVIDER_BINDING_UNRESOLVED', 'PROVIDER_NOT_IMPLEMENTED', 'PROVIDER_NOT_FOUND']) {
|
|
200
|
+
const h = makeApp({ reqCtx: baseReqCtx(), resolveError: Object.assign(new Error('boom'), { code }) });
|
|
201
|
+
const res = await request(h.app).get('/activities?mallId=mall-a');
|
|
202
|
+
expect(res.status, code).toBe(503);
|
|
203
|
+
expect(res.body.error.code).toBe('NOT_CONFIGURED');
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('供给配置非法/执行异常 → 502 PROVIDER_ERROR,且不透传原始 message', async () => {
|
|
208
|
+
const h = makeApp({
|
|
209
|
+
reqCtx: baseReqCtx(),
|
|
210
|
+
resolveError: new Error('原始敏感细节: activities[2].title missing'),
|
|
211
|
+
});
|
|
212
|
+
const res = await request(h.app).get('/activities?mallId=mall-a');
|
|
213
|
+
expect(res.status).toBe(502);
|
|
214
|
+
expect(res.body.error.code).toBe('PROVIDER_ERROR');
|
|
215
|
+
expect(JSON.stringify(res.body)).not.toContain('敏感细节');
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe('GET /api/m/activity-play/activities/:activityRef(详情)', () => {
|
|
220
|
+
it('评审返修回归:数字/布尔形态合法字面量引用 200(原始字串——HTTP/工具同参同果)', async () => {
|
|
221
|
+
const refs = ['1.2', 'true'];
|
|
222
|
+
const { port } = makePort({ async getActivity(_q, ref) { return { ...ITEM, activityRef: ref }; } });
|
|
223
|
+
const h = makeApp({ reqCtx: baseReqCtx(), port });
|
|
224
|
+
for (const ref of refs) {
|
|
225
|
+
const res = await request(h.app).get(`/activities/${ref}?mallId=mall-a`);
|
|
226
|
+
expect(res.status, ref).toBe(200);
|
|
227
|
+
expect(res.body.data.activityRef).toBe(ref);
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('命中:返回 {"data":活动对象};activityRef 原样透传(含冒号)', async () => {
|
|
232
|
+
const { port } = makePort({ async getActivity() { return { ...ITEM }; } });
|
|
233
|
+
const h = makeApp({ reqCtx: baseReqCtx(), port });
|
|
234
|
+
const res = await request(h.app).get('/activities/demo:wheel?mallId=mall-a');
|
|
235
|
+
expect(res.status).toBe(200);
|
|
236
|
+
expect(res.body).toEqual({ data: ITEM });
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('未知引用 → 404 ACTIVITY_NOT_FOUND(独立于供给缺席的 503)', async () => {
|
|
240
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
241
|
+
const res = await request(h.app).get('/activities/demo:none?mallId=mall-a');
|
|
242
|
+
expect(res.status).toBe(404);
|
|
243
|
+
expect(res.body.error.code).toBe('ACTIVITY_NOT_FOUND');
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it('畸形 activityRef → 400 INVALID_REQUEST;商圈规则与列表一致', async () => {
|
|
247
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
248
|
+
expect((await request(h.app).get('/activities/%20?mallId=mall-a')).status).toBe(400);
|
|
249
|
+
expect((await request(h.app).get('/activities/demo:wheel')).status).toBe(400);
|
|
250
|
+
expect((await request(h.app).get('/activities/demo:wheel?mallId=')).status).toBe(400);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
beforeEach(() => { vi.clearAllMocks(); });
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
describe('错误信封与调用面', () => {
|
|
257
|
+
it('统一错误信封 {"error":{"code","message"}}(三面抽查)', async () => {
|
|
258
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
259
|
+
const bad = await request(h.app).get('/activities');
|
|
260
|
+
expect(Object.keys(bad.body)).toEqual(['error']);
|
|
261
|
+
expect(Object.keys(bad.body.error).sort()).toEqual(['code', 'message']);
|
|
262
|
+
expect(typeof bad.body.error.message).toBe('string');
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('无鉴权装配旁路(无可信上下文)→ 401 LOGIN_REQUIRED 显性拒绝', async () => {
|
|
266
|
+
const h = makeApp({ reqCtx: undefined });
|
|
267
|
+
const res = await request(h.app).get('/activities?mallId=mall-a');
|
|
268
|
+
expect(res.status).toBe(401);
|
|
269
|
+
expect(res.body.error.code).toBe('LOGIN_REQUIRED');
|
|
270
|
+
});
|
|
271
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* activity-play 卡样本双断言(样本即契约,对齐 promotion/member 模式):
|
|
3
|
+
* 全样本(含 sampleData 初始态)dataSchema.safeParse 通过 + findSampleShapeViolations
|
|
4
|
+
* 无越界键;覆盖维度(列表/详情/空态/四种状态)结构性在场——防钟样例集体漂移成
|
|
5
|
+
* "都是列表"之类(T02-05 验收面的模块侧自锁)。
|
|
6
|
+
*/
|
|
7
|
+
import { describe, expect, it } from 'vitest';
|
|
8
|
+
import { findSampleShapeViolations } from '@tbox.cn/app-agent-sdk-server';
|
|
9
|
+
import { activityPlayCardDataSchema } from '@tbox.cn/app-contracts-mall';
|
|
10
|
+
import meta from '../src/server/cards/activity-play/meta';
|
|
11
|
+
|
|
12
|
+
const sampledModes = new Set(((meta.samples ?? []).map((s) => (s.data as { mode: string }).mode)));
|
|
13
|
+
const sampledStatuses = new Set(
|
|
14
|
+
(meta.samples ?? []).flatMap((s) => (s.data as { items: { status: string }[] }).items.map((i) => i.status)),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
describe('activity-play 样本即契约(T02-05)', () => {
|
|
18
|
+
it('sampleData 与全附加样本过 dataSchema + shape 无越界键', () => {
|
|
19
|
+
expect(activityPlayCardDataSchema.safeParse(meta.sampleData).success).toBe(true);
|
|
20
|
+
expect(findSampleShapeViolations(meta).violations).toEqual([]);
|
|
21
|
+
for (const s of meta.samples ?? []) {
|
|
22
|
+
expect(activityPlayCardDataSchema.safeParse(s.data).success, `${meta.cardType}/${s.label}`).toBe(true);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('覆盖维度在场:list(含空态)+ detail;四种状态全覆盖', () => {
|
|
27
|
+
expect(sampledModes.has('list')).toBe(true);
|
|
28
|
+
expect(sampledModes.has('detail')).toBe(true);
|
|
29
|
+
const listEmpty = (meta.samples ?? []).some(
|
|
30
|
+
(s) => (s.data as { mode: string; items: unknown[] }).mode === 'list'
|
|
31
|
+
&& (s.data as { items: unknown[] }).items.length === 0,
|
|
32
|
+
);
|
|
33
|
+
expect(listEmpty).toBe(true);
|
|
34
|
+
for (const status of ['not_started', 'active', 'ended', 'disabled']) {
|
|
35
|
+
expect(sampledStatuses.has(status), `样本缺状态 ${status}`).toBe(true);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('样本不含 B 端 enabled 配置字段与未知键(卡 data 面 = 契约 strict 面)', () => {
|
|
40
|
+
for (const s of [ { label: 'sampleData', data: meta.sampleData }, ...(meta.samples ?? []) ]) {
|
|
41
|
+
const items = (s.data as { items: Record<string, unknown>[] }).items;
|
|
42
|
+
for (const item of items) {
|
|
43
|
+
expect(item['enabled'], `${s.label}`).toBeUndefined();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('样本 mallId 恒 demo 商圈(卡片 mallId 语义 = 执行上下文投影位)', () => {
|
|
49
|
+
for (const s of meta.samples ?? []) {
|
|
50
|
+
expect((s.data as { mallId: string }).mallId).toBe('mall-demo');
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|