@tbox.cn/app-module-activity-play 0.3.1 → 0.4.0
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 +57 -66
- package/README.md +13 -5
- package/dist/chunk-GDHTMID6.js +185 -0
- package/dist/chunk-XIDGKP5D.js +1154 -0
- package/dist/client/index.d.ts +2 -0
- package/dist/client/index.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +38 -10
- package/dist/server/index.d.ts +593 -85
- package/dist/server/index.js +37 -9
- package/package.json +5 -5
- package/skills/activity-play/SKILL.md +41 -15
- package/src/client/cards/activity-play/ActivityPlayCard.tsx +3 -3
- package/src/client/cards/activity-play-prize/ActivityPlayPrizeDetailCard.tsx +44 -0
- package/src/client/cards/activity-play-prize/ActivityPlayPrizeListCard.tsx +62 -0
- package/src/client/cards/activity-play-prize/index.ts +8 -0
- package/src/client/cards/view.ts +6 -3
- package/src/client/index.ts +6 -0
- package/src/server/cards/activity-play/meta.ts +5 -4
- package/src/server/cards/activity-play/samples.ts +43 -18
- 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 +5 -1
- package/src/server/errors.ts +9 -28
- package/src/server/index.ts +48 -11
- package/src/server/ports.ts +4 -25
- package/src/server/prizes/cards.ts +79 -0
- package/src/server/prizes/errors.ts +88 -0
- package/src/server/prizes/ports.ts +35 -0
- package/src/server/prizes/query.ts +104 -0
- package/src/server/prizes/routes.ts +139 -0
- package/src/server/prizes/subject.ts +29 -0
- package/src/server/prizes/tools.ts +180 -0
- package/src/server/query.ts +47 -28
- package/src/server/routes.ts +44 -230
- package/src/server/tools.ts +36 -31
- package/tbox.module.json +16 -6
- package/tests/cards-capture.test.ts +3 -4
- package/tests/ports.test.ts +57 -0
- package/tests/prizes-routes.test.ts +230 -0
- package/tests/prizes-tools.test.ts +216 -0
- package/tests/prizes.test.ts +375 -0
- package/tests/routes.test.ts +89 -16
- package/tests/samples.test.ts +2 -2
- package/tests/smoke.test.ts +8 -4
- package/tests/tools.test.ts +50 -31
- package/dist/chunk-CM4OEPC3.js +0 -689
- package/dist/chunk-HUBEOW44.js +0 -98
- package/tests/entry.test.ts +0 -458
|
@@ -0,0 +1,375 @@
|
|
|
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 type { ServerContext } from '@tbox.cn/app-agent-sdk-server';
|
|
19
|
+
import { PROVIDER_ERROR_CODES } from '@tbox.cn/app-contracts';
|
|
20
|
+
import type { ActivityPlayPrizeQueryPort } from '@tbox.cn/app-contracts-mall';
|
|
21
|
+
import {
|
|
22
|
+
activityPlayPrizeDetailCardDataSchema,
|
|
23
|
+
activityPlayPrizeListCardDataSchema,
|
|
24
|
+
} from '@tbox.cn/app-contracts-mall';
|
|
25
|
+
import { requirePrizeSubjectId } from '../src/server/prizes/subject';
|
|
26
|
+
import {
|
|
27
|
+
normalizePrizeRecordRef,
|
|
28
|
+
queryMyPrizeDetail,
|
|
29
|
+
queryMyPrizeList,
|
|
30
|
+
requirePrizeQueryContext,
|
|
31
|
+
} from '../src/server/prizes/query';
|
|
32
|
+
import {
|
|
33
|
+
ActivityPlayPrizeHttpError,
|
|
34
|
+
toActivityPlayPrizeError,
|
|
35
|
+
} from '../src/server/prizes/errors';
|
|
36
|
+
import {
|
|
37
|
+
PRIZE_NO_RESULTS_TEXT,
|
|
38
|
+
toPrizeDetailCardData,
|
|
39
|
+
toPrizeListCardData,
|
|
40
|
+
} from '../src/server/prizes/cards';
|
|
41
|
+
import prizeListMeta from '../src/server/cards/activity-play-prize-list/meta';
|
|
42
|
+
import prizeDetailMeta from '../src/server/cards/activity-play-prize-detail/meta';
|
|
43
|
+
|
|
44
|
+
// ===== fixtures =====
|
|
45
|
+
|
|
46
|
+
type Identity = NonNullable<RequestContext['identity']>;
|
|
47
|
+
|
|
48
|
+
const reqCtxOf = (identity: Identity, mallId?: string): RequestContext => ({
|
|
49
|
+
identity,
|
|
50
|
+
...(mallId ? { attributes: { mall: { mallId } } } : {}),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
/** 无可信身份形态(上下文在场但 identity 缺席——装配旁路防御分支) */
|
|
54
|
+
const noIdentityCtx = (mallId?: string): RequestContext =>
|
|
55
|
+
({ ...(mallId ? { attributes: { mall: { mallId } } } : {}) }) as RequestContext;
|
|
56
|
+
|
|
57
|
+
const localCtx = (mallId = 'mall-a', userId = '2088000000000001'): RequestContext =>
|
|
58
|
+
reqCtxOf({ userId, source: 'local' }, mallId);
|
|
59
|
+
|
|
60
|
+
const RECORD = {
|
|
61
|
+
prizeRecordRef: 'prize-demo-001',
|
|
62
|
+
title: '咖啡兑换券',
|
|
63
|
+
prizeType: 'COUPON',
|
|
64
|
+
claimStatus: 'CLAIMED',
|
|
65
|
+
useStatus: 'UNUSED',
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const DETAIL = {
|
|
69
|
+
...RECORD,
|
|
70
|
+
redemptionMethod: '门店柜台',
|
|
71
|
+
redemptionInstruction: '请按活动页面的领取及使用指引操作',
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/** 替身 Port(记录最近一次调用的 q/ref——主体与标识断言锚点) */
|
|
75
|
+
function makePort(opts: Partial<{
|
|
76
|
+
listResult: unknown;
|
|
77
|
+
detailResult: unknown;
|
|
78
|
+
listImpl: (q: unknown) => Promise<unknown>;
|
|
79
|
+
}> = {}): ActivityPlayPrizeQueryPort & {
|
|
80
|
+
listMyPrizes: ReturnType<typeof vi.fn>;
|
|
81
|
+
getMyPrizeDetail: ReturnType<typeof vi.fn>;
|
|
82
|
+
} {
|
|
83
|
+
return {
|
|
84
|
+
listMyPrizes: vi.fn(async (q: unknown) =>
|
|
85
|
+
'listImpl' in opts && opts.listImpl
|
|
86
|
+
? opts.listImpl(q)
|
|
87
|
+
: ('listResult' in opts ? opts.listResult : { total: 1, items: [RECORD] })),
|
|
88
|
+
getMyPrizeDetail: vi.fn(async (_q: unknown, _ref: string) =>
|
|
89
|
+
('detailResult' in opts ? opts.detailResult : DETAIL)),
|
|
90
|
+
} as never;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** 供给解析替身 ctx:resolveService 恒返回注入的 Port(记录调用——零解析断言锚点) */
|
|
94
|
+
function ctxOf(port: unknown): { ctx: ServerContext; resolveService: ReturnType<typeof vi.fn> } {
|
|
95
|
+
const resolveService = vi.fn(async () => port);
|
|
96
|
+
return { ctx: { integrations: { resolveService } } as unknown as ServerContext, resolveService };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ===== 主体纪律(任务06 约定) =====
|
|
100
|
+
|
|
101
|
+
describe('requirePrizeSubjectId(严格主体检查)', () => {
|
|
102
|
+
it('local/external/guest 均返回 userId 原值(不数字化/不加减前缀)', () => {
|
|
103
|
+
for (const source of ['local', 'external', 'guest'] as const) {
|
|
104
|
+
const v = requirePrizeSubjectId(reqCtxOf({ userId: '2088112233445566', source }));
|
|
105
|
+
expect(v).toBe('2088112233445566');
|
|
106
|
+
}
|
|
107
|
+
expect(requirePrizeSubjectId(reqCtxOf({ userId: '001', source: 'local' }))).toBe('001');
|
|
108
|
+
expect(requirePrizeSubjectId(reqCtxOf({ userId: '1.20', source: 'guest' }))).toBe('1.20');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('anonymous/缺席/空值 → LOGIN_REQUIRED(401)', () => {
|
|
112
|
+
for (const bad of [
|
|
113
|
+
reqCtxOf({ userId: 'u1', source: 'anonymous' }),
|
|
114
|
+
noIdentityCtx(),
|
|
115
|
+
reqCtxOf({ userId: '', source: 'local' }),
|
|
116
|
+
reqCtxOf({ userId: ' ', source: 'local' }),
|
|
117
|
+
]) {
|
|
118
|
+
expect(() => requirePrizeSubjectId(bad)).toThrow(ActivityPlayPrizeHttpError);
|
|
119
|
+
try {
|
|
120
|
+
requirePrizeSubjectId(bad);
|
|
121
|
+
} catch (e) {
|
|
122
|
+
expect((e as ActivityPlayPrizeHttpError).code).toBe('LOGIN_REQUIRED');
|
|
123
|
+
expect((e as ActivityPlayPrizeHttpError).httpStatus).toBe(401);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// ===== 查询核心(检查序/供给自证/标识原样) =====
|
|
130
|
+
|
|
131
|
+
describe('queryMyPrizeList / queryMyPrizeDetail', () => {
|
|
132
|
+
it('缺商圈 → SCOPE_REQUIRED(400)', async () => {
|
|
133
|
+
const port = makePort();
|
|
134
|
+
const noMallCtx = reqCtxOf({ userId: '2088000000000001', source: 'local' });
|
|
135
|
+
await expect(queryMyPrizeList(noMallCtx, ctxOf(port).ctx)).rejects.toMatchObject({
|
|
136
|
+
code: 'SCOPE_REQUIRED',
|
|
137
|
+
httpStatus: 400,
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('身份检查先于供给呼叫:缺用户/anonymous → LOGIN_REQUIRED 且零奖品 API 调用', async () => {
|
|
142
|
+
for (const bad of [
|
|
143
|
+
noIdentityCtx('mall-a'),
|
|
144
|
+
reqCtxOf({ userId: 'u1', source: 'anonymous' }, 'mall-a'),
|
|
145
|
+
]) {
|
|
146
|
+
const listPort = makePort();
|
|
147
|
+
const listResolution = ctxOf(listPort);
|
|
148
|
+
await expect(queryMyPrizeList(bad, listResolution.ctx)).rejects.toMatchObject({
|
|
149
|
+
code: 'LOGIN_REQUIRED',
|
|
150
|
+
httpStatus: 401,
|
|
151
|
+
});
|
|
152
|
+
expect(listPort.listMyPrizes).not.toHaveBeenCalled();
|
|
153
|
+
expect(listResolution.resolveService).not.toHaveBeenCalled(); // 零供给解析接触(先于 registry)
|
|
154
|
+
|
|
155
|
+
const detailPort = makePort();
|
|
156
|
+
const detailResolution = ctxOf(detailPort);
|
|
157
|
+
await expect(queryMyPrizeDetail(bad, detailResolution.ctx, 'prize-demo-001')).rejects.toMatchObject({
|
|
158
|
+
code: 'LOGIN_REQUIRED',
|
|
159
|
+
});
|
|
160
|
+
expect(detailPort.getMyPrizeDetail).not.toHaveBeenCalled();
|
|
161
|
+
expect(detailResolution.resolveService).not.toHaveBeenCalled();
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('连续与并发 A/B 用户不串主体:Port 收到的 subjectId 逐次对应', async () => {
|
|
166
|
+
const port = makePort({ listResult: { total: 0, items: [] } });
|
|
167
|
+
await queryMyPrizeList(localCtx('mall-a', 'user-A'), ctxOf(port).ctx);
|
|
168
|
+
await queryMyPrizeList(localCtx('mall-a', 'user-B'), ctxOf(port).ctx);
|
|
169
|
+
await Promise.all([
|
|
170
|
+
queryMyPrizeList(localCtx('mall-a', 'user-A'), ctxOf(port).ctx),
|
|
171
|
+
queryMyPrizeList(localCtx('mall-a', 'user-B'), ctxOf(port).ctx),
|
|
172
|
+
]);
|
|
173
|
+
const subjects = port.listMyPrizes.mock.calls.map(([q]) => (q as { subjectId: string }).subjectId);
|
|
174
|
+
expect(subjects).toEqual(['user-A', 'user-B', 'user-A', 'user-B']);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('组合子与严格读取同值自证(subjectId == identity.userId 原字符串)', () => {
|
|
178
|
+
const { q, subjectId } = requirePrizeQueryContext(localCtx('mall-a', 'user-X'));
|
|
179
|
+
expect(q.subjectId).toBe('user-X');
|
|
180
|
+
expect(subjectId).toBe('user-X');
|
|
181
|
+
expect(q.scope.mallId).toBe('mall-a');
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('prizeRecordRef 原样定位:001 / 1.20 / 1e3 不数字化过 Port;首尾空白是标识原值不 trim 改写', async () => {
|
|
185
|
+
for (const ref of ['001', '1.20', '1e3', ' record-001 ']) {
|
|
186
|
+
const port = makePort({ detailResult: { ...DETAIL, prizeRecordRef: ref } });
|
|
187
|
+
const detail = await queryMyPrizeDetail(localCtx(), ctxOf(port).ctx, ref);
|
|
188
|
+
expect(detail.prizeRecordRef).toBe(ref);
|
|
189
|
+
expect(port.getMyPrizeDetail).toHaveBeenCalledWith(expect.anything(), ref);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('『.』『..』整值原样达 Port(拒绝防线归 provider 纵深——模块不改判 400/404 混淆语义)', async () => {
|
|
194
|
+
const port = makePort({ detailResult: { ...DETAIL, prizeRecordRef: '.' } });
|
|
195
|
+
const detail = await queryMyPrizeDetail(localCtx(), ctxOf(port).ctx, '.');
|
|
196
|
+
expect(detail.prizeRecordRef).toBe('.');
|
|
197
|
+
expect(port.getMyPrizeDetail).toHaveBeenCalledWith(expect.anything(), '.');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('ref 非法(全空白/超长)→ INVALID_REQUEST 且零供给调用;非全空白原值直传(trim 只判空白不改写)', async () => {
|
|
201
|
+
for (const bad of ['', ' ', '\t', 'x'.repeat(129)]) {
|
|
202
|
+
const port = makePort();
|
|
203
|
+
await expect(queryMyPrizeDetail(localCtx(), ctxOf(port).ctx, bad)).rejects.toMatchObject({
|
|
204
|
+
code: 'INVALID_REQUEST',
|
|
205
|
+
httpStatus: 400,
|
|
206
|
+
});
|
|
207
|
+
expect(port.getMyPrizeDetail).not.toHaveBeenCalled();
|
|
208
|
+
}
|
|
209
|
+
expect(normalizePrizeRecordRef(' 1.20 ')).toBe(' 1.20 ');
|
|
210
|
+
expect(normalizePrizeRecordRef(' record-001 ')).toBe(' record-001 ');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('供给列表输出非法(必缺/负 total/未知键)→ PROVIDER_ERROR(不丢坏记录冒称未中奖)', async () => {
|
|
214
|
+
for (const bad of [
|
|
215
|
+
{ total: 1, items: [{ ...RECORD, title: '' }] },
|
|
216
|
+
{ total: -1, items: [] },
|
|
217
|
+
{ items: [] },
|
|
218
|
+
{ total: 1, items: [RECORD], rawJson: 'x' },
|
|
219
|
+
{ total: 1, items: [{ ...RECORD, prizeType: 7 }] },
|
|
220
|
+
]) {
|
|
221
|
+
const port: unknown = makePort({ listResult: bad });
|
|
222
|
+
await expect(queryMyPrizeList(localCtx(), ctxOf(port).ctx)).rejects.toMatchObject({
|
|
223
|
+
code: 'PROVIDER_ERROR',
|
|
224
|
+
httpStatus: 502,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('详情 null → PRIZE_NOT_FOUND(404)(不存在与不可见同文案)', async () => {
|
|
230
|
+
const port = makePort({ detailResult: null });
|
|
231
|
+
await expect(queryMyPrizeDetail(localCtx(), ctxOf(port).ctx, 'prize-demo-001')).rejects.toMatchObject({
|
|
232
|
+
code: 'PRIZE_NOT_FOUND',
|
|
233
|
+
httpStatus: 404,
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('供给返回另一记录标识 → PROVIDER_ERROR(不得以请求标识包装输出)', async () => {
|
|
238
|
+
const port = makePort({ detailResult: { ...DETAIL, prizeRecordRef: 'prize-other-002' } });
|
|
239
|
+
await expect(queryMyPrizeDetail(localCtx(), ctxOf(port).ctx, 'prize-demo-001')).rejects.toMatchObject({
|
|
240
|
+
code: 'PROVIDER_ERROR',
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('列表→详情按条目原值标识定位:带空白 ref 原样回查、不改写不误判 502', async () => {
|
|
245
|
+
// 评审返修回归:供应商可返回 ' record-001 ' 形标识——列表条目 ref 必须原值喂给详情查询
|
|
246
|
+
const spacedRef = ' record-001 ';
|
|
247
|
+
const port = {
|
|
248
|
+
listMyPrizes: vi.fn(async () => ({ total: 1, items: [{ ...RECORD, prizeRecordRef: spacedRef }] })),
|
|
249
|
+
getMyPrizeDetail: vi.fn(async (_q: unknown, ref: string) => ({ ...DETAIL, prizeRecordRef: ref })),
|
|
250
|
+
};
|
|
251
|
+
const list = await queryMyPrizeList(localCtx(), ctxOf(port).ctx);
|
|
252
|
+
const card = toPrizeListCardData({ mallId: 'mall-a', result: list });
|
|
253
|
+
const refFromCard = card.items[0]!.prizeRecordRef;
|
|
254
|
+
expect(refFromCard).toBe(spacedRef);
|
|
255
|
+
|
|
256
|
+
const detail = await queryMyPrizeDetail(localCtx(), ctxOf(port).ctx, refFromCard);
|
|
257
|
+
expect(detail.prizeRecordRef).toBe(spacedRef);
|
|
258
|
+
expect(port.getMyPrizeDetail).toHaveBeenCalledWith(expect.anything(), spacedRef);
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
it('供给结构非法(详情缺必填/未知键)→ PROVIDER_ERROR', async () => {
|
|
262
|
+
for (const bad of [{ ...DETAIL, claimStatus: '' }, { ...DETAIL, extra: 1 }]) {
|
|
263
|
+
const port = makePort({ detailResult: bad });
|
|
264
|
+
await expect(queryMyPrizeDetail(localCtx(), ctxOf(port).ctx, 'prize-demo-001')).rejects.toMatchObject({
|
|
265
|
+
code: 'PROVIDER_ERROR',
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
// ===== 错误分类(认证不降格——任务06/07 约定) =====
|
|
272
|
+
|
|
273
|
+
describe('toActivityPlayPrizeError(映射守门)', () => {
|
|
274
|
+
it('本域认证错误原样通过(LOGIN_REQUIRED 不被降格为 502 上游失败)', () => {
|
|
275
|
+
const err = toActivityPlayPrizeError(ActivityPlayPrizeHttpError.loginRequired('缺少当前用户'));
|
|
276
|
+
expect(err.code).toBe('LOGIN_REQUIRED');
|
|
277
|
+
expect(err.httpStatus).toBe(401);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
it('供给配置缺口(未配置/实例缺席/未实现/绑定未解析)→ NOT_CONFIGURED(503)', () => {
|
|
281
|
+
for (const code of [
|
|
282
|
+
PROVIDER_ERROR_CODES.SERVICE_NOT_CONFIGURED,
|
|
283
|
+
PROVIDER_ERROR_CODES.INSTANCE_NOT_FOUND,
|
|
284
|
+
PROVIDER_ERROR_CODES.INSTANCE_DISABLED,
|
|
285
|
+
PROVIDER_ERROR_CODES.PROVIDER_NOT_FOUND,
|
|
286
|
+
PROVIDER_ERROR_CODES.PROVIDER_NOT_IMPLEMENTED,
|
|
287
|
+
PROVIDER_ERROR_CODES.BINDING_UNRESOLVED,
|
|
288
|
+
]) {
|
|
289
|
+
const err = toActivityPlayPrizeError(Object.assign(new Error('raw'), { code }));
|
|
290
|
+
expect(err.code).toBe('NOT_CONFIGURED');
|
|
291
|
+
expect(err.httpStatus).toBe(503);
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
it('未知异常 → PROVIDER_ERROR(502),原始 message 不透出', () => {
|
|
296
|
+
const err = toActivityPlayPrizeError(new Error('凭证 sk-xxx 泄漏面'));
|
|
297
|
+
expect(err.code).toBe('PROVIDER_ERROR');
|
|
298
|
+
expect(err.httpStatus).toBe(502);
|
|
299
|
+
expect(err.message).not.toContain('sk-xxx');
|
|
300
|
+
});
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// ===== 出卡适配(卡片 data 面) =====
|
|
304
|
+
|
|
305
|
+
describe('prizes/cards 适配', () => {
|
|
306
|
+
it('列表多条目:条目恒附 send 标准动作(value 含记录标识、visibleValue 可读名)', () => {
|
|
307
|
+
const data = toPrizeListCardData({
|
|
308
|
+
mallId: 'mall-a',
|
|
309
|
+
result: { total: 2, items: [RECORD, { ...RECORD, prizeRecordRef: 'prize-demo-002', title: '周年庆礼盒' }] },
|
|
310
|
+
});
|
|
311
|
+
activityPlayPrizeListCardDataSchema.parse(data);
|
|
312
|
+
expect(data.items).toHaveLength(2);
|
|
313
|
+
for (const item of data.items) {
|
|
314
|
+
expect(item.itemActions).toHaveLength(1);
|
|
315
|
+
const [action] = item.itemActions!;
|
|
316
|
+
if (action.kind !== 'send') throw new Error('奖品列表动作须为 send');
|
|
317
|
+
expect(action.value).toContain(item.prizeRecordRef);
|
|
318
|
+
expect(action.visibleValue).toContain(item.title);
|
|
319
|
+
expect(action.label).toBe('查看详情');
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('空态:total=0 → noResultsReason;total>0 空 items 保留事实不加空态文案', () => {
|
|
324
|
+
const empty = toPrizeListCardData({ mallId: 'mall-a', result: { total: 0, items: [] } });
|
|
325
|
+
activityPlayPrizeListCardDataSchema.parse(empty);
|
|
326
|
+
expect(empty.noResultsReason).toBe(PRIZE_NO_RESULTS_TEXT);
|
|
327
|
+
|
|
328
|
+
const pagedEmpty = toPrizeListCardData({ mallId: 'mall-a', result: { total: 5, items: [] } });
|
|
329
|
+
activityPlayPrizeListCardDataSchema.parse(pagedEmpty);
|
|
330
|
+
expect(pagedEmpty.noResultsReason).toBeUndefined();
|
|
331
|
+
expect(pagedEmpty.total).toBe(5);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it('超过展示上限(50)截断:卡过自身契约、total 保留真实值 + truncated;恰好 50 无标记', () => {
|
|
335
|
+
const many = Array.from({ length: 51 }, (_, i) => ({
|
|
336
|
+
...RECORD,
|
|
337
|
+
prizeRecordRef: `prize-demo-${String(i).padStart(3, '0')}`,
|
|
338
|
+
}));
|
|
339
|
+
const overflowed = toPrizeListCardData({ mallId: 'mall-a', result: { total: 51, items: many } });
|
|
340
|
+
// 评审返修回归:51 条记录不得生成违反卡 schema 的卡片(items.max(50))
|
|
341
|
+
activityPlayPrizeListCardDataSchema.parse(overflowed);
|
|
342
|
+
expect(overflowed.items).toHaveLength(50);
|
|
343
|
+
expect(overflowed.total).toBe(51);
|
|
344
|
+
expect(overflowed.truncated).toBe(true);
|
|
345
|
+
// 截断面保序,末条目动作与其自身标识对应(先收窄 send 变体再读 value——联合收窄)
|
|
346
|
+
const last = overflowed.items[49]!;
|
|
347
|
+
const lastAction = last.itemActions![0]!;
|
|
348
|
+
if (lastAction.kind !== 'send') throw new Error('奖品列表动作须为 send');
|
|
349
|
+
expect(lastAction.value).toContain('prize-demo-049');
|
|
350
|
+
|
|
351
|
+
const exact = toPrizeListCardData({ mallId: 'mall-a', result: { total: 50, items: many.slice(0, 50) } });
|
|
352
|
+
activityPlayPrizeListCardDataSchema.parse(exact);
|
|
353
|
+
expect(exact.truncated).toBeUndefined();
|
|
354
|
+
expect(exact.total).toBe(50);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it('详情平铺:{mallId, ...detail} 过卡契约(无 mode/items/prize 包装键)', () => {
|
|
358
|
+
const data = toPrizeDetailCardData({ mallId: 'mall-a', detail: DETAIL });
|
|
359
|
+
activityPlayPrizeDetailCardDataSchema.parse(data);
|
|
360
|
+
expect('mode' in data).toBe(false);
|
|
361
|
+
expect('items' in data).toBe(false);
|
|
362
|
+
expect('prize' in data).toBe(false);
|
|
363
|
+
expect(data.mallId).toBe('mall-a');
|
|
364
|
+
expect(data.prizeRecordRef).toBe('prize-demo-001');
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it('meta/samples:卡型常量与 schema 对齐;全样本与 sampleData 过卡契约', () => {
|
|
368
|
+
expect(prizeListMeta.cardType).toBe('activity-play-prize-list');
|
|
369
|
+
expect(prizeDetailMeta.cardType).toBe('activity-play-prize-detail');
|
|
370
|
+
activityPlayPrizeListCardDataSchema.parse(prizeListMeta.sampleData);
|
|
371
|
+
activityPlayPrizeDetailCardDataSchema.parse(prizeDetailMeta.sampleData);
|
|
372
|
+
for (const s of prizeListMeta.samples ?? []) activityPlayPrizeListCardDataSchema.parse(s.data);
|
|
373
|
+
for (const s of prizeDetailMeta.samples ?? []) activityPlayPrizeDetailCardDataSchema.parse(s.data);
|
|
374
|
+
});
|
|
375
|
+
});
|
package/tests/routes.test.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
type ActivityPlayQueryPort,
|
|
17
17
|
} from '@tbox.cn/app-contracts-mall';
|
|
18
18
|
import { createActivityPlayRouter } from '../src/server/routes';
|
|
19
|
+
import { normalizeActivityId } from '../src/server/query';
|
|
19
20
|
|
|
20
21
|
/** 会话基础上下文(登录态 guest——等同浏览器轴 guest 登录产物) */
|
|
21
22
|
const baseReqCtx = (): RequestContext => ({
|
|
@@ -25,22 +26,26 @@ const baseReqCtx = (): RequestContext => ({
|
|
|
25
26
|
interface PortCall {
|
|
26
27
|
q: ActivityPlayQueryContext;
|
|
27
28
|
input?: { keyword?: string; limit?: number };
|
|
28
|
-
|
|
29
|
+
activityId?: string;
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
/** 可观测 Port 替身(记录调用 +
|
|
32
|
+
/** 可观测 Port 替身(记录调用 + 可配置返回/抛错)——自定义 handler 经包装后调用面
|
|
33
|
+
* 仍被捕获(「Port 收到什么入参」是原字符串语义回归的断言主体,不能因自定义而失明) */
|
|
32
34
|
function makePort(overrides: Partial<ActivityPlayQueryPort> = {}) {
|
|
33
35
|
const calls: PortCall[] = [];
|
|
36
|
+
const defaults: ActivityPlayQueryPort = {
|
|
37
|
+
async listActivities() { return { items: [] }; },
|
|
38
|
+
async getActivity() { return null; },
|
|
39
|
+
};
|
|
34
40
|
const port: ActivityPlayQueryPort = {
|
|
35
41
|
async listActivities(q, input) {
|
|
36
42
|
calls.push({ q, input });
|
|
37
|
-
return
|
|
43
|
+
return (overrides.listActivities ?? defaults.listActivities)(q, input);
|
|
38
44
|
},
|
|
39
|
-
async getActivity(q,
|
|
40
|
-
calls.push({ q,
|
|
41
|
-
return
|
|
45
|
+
async getActivity(q, activityId) {
|
|
46
|
+
calls.push({ q, activityId });
|
|
47
|
+
return (overrides.getActivity ?? defaults.getActivity)(q, activityId);
|
|
42
48
|
},
|
|
43
|
-
...overrides,
|
|
44
49
|
};
|
|
45
50
|
return { port, calls };
|
|
46
51
|
}
|
|
@@ -52,7 +57,7 @@ interface Harness {
|
|
|
52
57
|
}
|
|
53
58
|
|
|
54
59
|
/** 装配被测 App:auth 形中间件写入 reqCtx → 挂模块 Router(stub ctx)。
|
|
55
|
-
* 缺省 port = 可观测替身(空目录);自定义 port
|
|
60
|
+
* 缺省 port = 可观测替身(空目录);自定义 port 的调用面同样被 makePort 包装捕获。 */
|
|
56
61
|
function makeApp(opts: {
|
|
57
62
|
reqCtx: RequestContext | undefined;
|
|
58
63
|
port?: ActivityPlayQueryPort;
|
|
@@ -77,10 +82,9 @@ function makeApp(opts: {
|
|
|
77
82
|
}
|
|
78
83
|
|
|
79
84
|
const ITEM = {
|
|
80
|
-
|
|
85
|
+
activityId: 'demo:wheel',
|
|
81
86
|
title: '幸运转盘',
|
|
82
87
|
status: 'active',
|
|
83
|
-
loginRequired: false,
|
|
84
88
|
} as const;
|
|
85
89
|
|
|
86
90
|
describe('GET /api/m/activity-play/activities(列表)', () => {
|
|
@@ -216,19 +220,51 @@ describe('GET /api/m/activity-play/activities(列表)', () => {
|
|
|
216
220
|
});
|
|
217
221
|
});
|
|
218
222
|
|
|
219
|
-
describe('GET /api/m/activity-play/activities/:
|
|
223
|
+
describe('GET /api/m/activity-play/activities/:activityId(详情)', () => {
|
|
220
224
|
it('评审返修回归:数字/布尔形态合法字面量引用 200(原始字串——HTTP/工具同参同果)', async () => {
|
|
221
225
|
const refs = ['1.2', 'true'];
|
|
222
|
-
const { port } = makePort({ async getActivity(_q, ref) { return { ...ITEM,
|
|
226
|
+
const { port } = makePort({ async getActivity(_q, ref) { return { ...ITEM, activityId: ref }; } });
|
|
223
227
|
const h = makeApp({ reqCtx: baseReqCtx(), port });
|
|
224
228
|
for (const ref of refs) {
|
|
225
229
|
const res = await request(h.app).get(`/activities/${ref}?mallId=mall-a`);
|
|
226
230
|
expect(res.status, ref).toBe(200);
|
|
227
|
-
expect(res.body.data.
|
|
231
|
+
expect(res.body.data.activityId).toBe(ref);
|
|
228
232
|
}
|
|
229
233
|
});
|
|
230
234
|
|
|
231
|
-
it('
|
|
235
|
+
it('P2 返修回归:标识原值直传(列表返回 " record-001 " → 详情原样命中——trim 改写=404 串答)', async () => {
|
|
236
|
+
const SPACED = { activityId: ' record-001 ', title: '原值活动', status: 'active' } as const;
|
|
237
|
+
const { port, calls } = makePort({
|
|
238
|
+
async listActivities() { return { items: [SPACED] }; },
|
|
239
|
+
async getActivity(_q, id) { return id === ' record-001 ' ? SPACED : null; },
|
|
240
|
+
});
|
|
241
|
+
const h = makeApp({ reqCtx: baseReqCtx(), port });
|
|
242
|
+
const list = await request(h.app).get('/activities?mallId=mall-a');
|
|
243
|
+
expect(list.status).toBe(200);
|
|
244
|
+
expect(list.body.data.items[0].activityId).toBe(' record-001 ');
|
|
245
|
+
// %20 原样编码进路径;Port 收到原值(非 trim 后 'record-001')
|
|
246
|
+
const res = await request(h.app).get('/activities/%20record-001%20?mallId=mall-a');
|
|
247
|
+
expect(res.status).toBe(200);
|
|
248
|
+
expect(res.body.data.activityId).toBe(' record-001 ');
|
|
249
|
+
expect(calls.at(-1)?.activityId).toBe(' record-001 ');
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('P2 返修回归:『.』『..』路径标识防线(contract 拒 + handler 单元拒;HTTP 客户端边界如实记录)', async () => {
|
|
253
|
+
// dot 段防线三面:①契约 activityPlayIdSchema 拒(contracts-mall 域测试);②模块
|
|
254
|
+
// normalizeActivityId 直拒(handler 入口——若有形态抵达即 400);③provider adapter
|
|
255
|
+
// 构造请求前纵深拒(provider-hupo 测试:transport 零 dot 请求)。
|
|
256
|
+
// HTTP 用例侧如实:supertest/superagent 在客户端就把点段规范化(%2E 与 . 同样
|
|
257
|
+
// 落列表路由 200)——客户端边界不等价「dot 可送达 handler」;此处断言单元入口
|
|
258
|
+
// 行为(原字符串语义的回归主体在 handler/Port 层)。
|
|
259
|
+
for (const dot of ['.', '..']) {
|
|
260
|
+
expect(() => normalizeActivityId(dot), dot).toThrow();
|
|
261
|
+
}
|
|
262
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
263
|
+
const res = await request(h.app).get('/activities/%2E?mallId=mall-a');
|
|
264
|
+
expect([200, 400]).toContain(res.status); // 客户端规范化或契约拒——不允许 5xx/404 串答
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('命中:返回 {"data":活动对象};activityId 原样透传(含冒号)', async () => {
|
|
232
268
|
const { port } = makePort({ async getActivity() { return { ...ITEM }; } });
|
|
233
269
|
const h = makeApp({ reqCtx: baseReqCtx(), port });
|
|
234
270
|
const res = await request(h.app).get('/activities/demo:wheel?mallId=mall-a');
|
|
@@ -243,9 +279,10 @@ describe('GET /api/m/activity-play/activities/:activityRef(详情)', () => {
|
|
|
243
279
|
expect(res.body.error.code).toBe('ACTIVITY_NOT_FOUND');
|
|
244
280
|
});
|
|
245
281
|
|
|
246
|
-
it('畸形
|
|
282
|
+
it('畸形 activityId → 400 INVALID_REQUEST;商圈规则与列表一致', async () => {
|
|
247
283
|
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
248
|
-
|
|
284
|
+
// %20 单空格标识:按原字符串语义合法(空间非路径段破坏符)——原值透传,上游不存在 → 404
|
|
285
|
+
expect((await request(h.app).get('/activities/%20?mallId=mall-a')).status).toBe(404);
|
|
249
286
|
expect((await request(h.app).get('/activities/demo:wheel')).status).toBe(400);
|
|
250
287
|
expect((await request(h.app).get('/activities/demo:wheel?mallId=')).status).toBe(400);
|
|
251
288
|
});
|
|
@@ -253,6 +290,42 @@ describe('GET /api/m/activity-play/activities/:activityRef(详情)', () => {
|
|
|
253
290
|
beforeEach(() => { vi.clearAllMocks(); });
|
|
254
291
|
});
|
|
255
292
|
|
|
293
|
+
describe('远端活动改造回归(no-store × entry 退场)', () => {
|
|
294
|
+
it('成功响应恒 no-store(列表与详情——远端事实不落缓存)', async () => {
|
|
295
|
+
const { port } = makePort({ async listActivities() { return { items: [ITEM] }; }, async getActivity() { return ITEM; } });
|
|
296
|
+
const h = makeApp({ reqCtx: baseReqCtx(), port });
|
|
297
|
+
const list = await request(h.app).get('/activities?mallId=mall-a');
|
|
298
|
+
expect(list.status).toBe(200);
|
|
299
|
+
expect(list.headers['cache-control']).toBe('no-store');
|
|
300
|
+
const detail = await request(h.app).get('/activities/demo:wheel?mallId=mall-a');
|
|
301
|
+
expect(detail.status).toBe(200);
|
|
302
|
+
expect(detail.headers['cache-control']).toBe('no-store');
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it('业务错误响应恒 no-store(404/502/400 抽查)', async () => {
|
|
306
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
307
|
+
expect((await request(h.app).get('/activities/demo:none?mallId=mall-a')).headers['cache-control']).toBe('no-store');
|
|
308
|
+
expect((await request(h.app).get('/activities?mallId=mall-a&limit=0')).headers['cache-control']).toBe('no-store');
|
|
309
|
+
const h2 = makeApp({ reqCtx: baseReqCtx(), resolveError: new Error('boom') });
|
|
310
|
+
expect((await request(h2.app).get('/activities?mallId=mall-a')).headers['cache-control']).toBe('no-store');
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it('供给对象带旧字段残留(loginRequired/activityRef)→ 502 PROVIDER_ERROR(出站契约同门拦截)', async () => {
|
|
314
|
+
const badItem = { ...ITEM, loginRequired: false };
|
|
315
|
+
const { port } = makePort({ async listActivities() { return { items: [badItem] }; } });
|
|
316
|
+
const h = makeApp({ reqCtx: baseReqCtx(), port });
|
|
317
|
+
const res = await request(h.app).get('/activities?mallId=mall-a');
|
|
318
|
+
expect(res.status).toBe(502);
|
|
319
|
+
expect(res.body.error.code).toBe('PROVIDER_ERROR');
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it('POST entry 路由已退场(R3):参与前检查接口不存在——路由 miss 显性 404', async () => {
|
|
323
|
+
const h = makeApp({ reqCtx: baseReqCtx() });
|
|
324
|
+
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
325
|
+
expect(res.status).toBe(404); // express 路由表无该 POST 路径(非业务信封)
|
|
326
|
+
});
|
|
327
|
+
});
|
|
328
|
+
|
|
256
329
|
describe('错误信封与调用面', () => {
|
|
257
330
|
it('统一错误信封 {"error":{"code","message"}}(三面抽查)', async () => {
|
|
258
331
|
const h = makeApp({ reqCtx: baseReqCtx() });
|
package/tests/samples.test.ts
CHANGED
|
@@ -23,7 +23,7 @@ describe('activity-play 样本即契约(T02-05)', () => {
|
|
|
23
23
|
}
|
|
24
24
|
});
|
|
25
25
|
|
|
26
|
-
it('覆盖维度在场:list(含空态)+ detail
|
|
26
|
+
it('覆盖维度在场:list(含空态)+ detail;四种已知状态与 unknown 全覆盖(R-02)', () => {
|
|
27
27
|
expect(sampledModes.has('list')).toBe(true);
|
|
28
28
|
expect(sampledModes.has('detail')).toBe(true);
|
|
29
29
|
const listEmpty = (meta.samples ?? []).some(
|
|
@@ -31,7 +31,7 @@ describe('activity-play 样本即契约(T02-05)', () => {
|
|
|
31
31
|
&& (s.data as { items: unknown[] }).items.length === 0,
|
|
32
32
|
);
|
|
33
33
|
expect(listEmpty).toBe(true);
|
|
34
|
-
for (const status of ['not_started', 'active', 'ended', 'disabled']) {
|
|
34
|
+
for (const status of ['not_started', 'active', 'ended', 'disabled', 'unknown']) {
|
|
35
35
|
expect(sampledStatuses.has(status), `样本缺状态 ${status}`).toBe(true);
|
|
36
36
|
}
|
|
37
37
|
});
|
package/tests/smoke.test.ts
CHANGED
|
@@ -6,8 +6,8 @@ describe('module-activity-play 模块形态(铁律冒烟)', () => {
|
|
|
6
6
|
it('双端描述对象在场且自洽(satisfies ServerModule/ClientModule 编译期锁)', () => {
|
|
7
7
|
expect(serverModule).toBeTruthy();
|
|
8
8
|
expect(clientModule).toBeTruthy();
|
|
9
|
-
// 2026-11-16 起含授权占位 renderer
|
|
10
|
-
expect(Object.keys(clientModule.cards)).toEqual(['activity-play']);
|
|
9
|
+
// 2026-11-16 起含授权占位 renderer;2026-11-18 起含奖品两卡占位(与 server 侧对齐——铁律 cards key = meta.cardType)
|
|
10
|
+
expect(Object.keys(clientModule.cards)).toEqual(['activity-play', 'activity-play-prize-list', 'activity-play-prize-detail']);
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
it('cards key 约定 = meta.cardType(key 恒等于 meta.cardType——铁律位)', () => {
|
|
@@ -16,8 +16,12 @@ describe('module-activity-play 模块形态(铁律冒烟)', () => {
|
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
-
it('
|
|
20
|
-
expect(Object.keys(serverModule.cards)).toEqual([
|
|
19
|
+
it('卡型面 = 活动单卡型 + 奖品两张独立卡型(activity-play mode 承载列表/详情;奖品按 api.md 独立卡型)', () => {
|
|
20
|
+
expect(Object.keys(serverModule.cards)).toEqual([
|
|
21
|
+
'activity-play',
|
|
22
|
+
'activity-play-prize-list',
|
|
23
|
+
'activity-play-prize-detail',
|
|
24
|
+
]);
|
|
21
25
|
});
|
|
22
26
|
|
|
23
27
|
it('本地技能声明接线(skills map 键 = skill 名,前置 name=activity-play)', () => {
|