@tbox.cn/app-contracts-mall 0.14.0 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,232 @@
1
+ /**
2
+ * 活动玩法查询契约行为测试:状态计算的临界/缺省语义 + 运行时 schema 形状。
3
+ * 风险≡验收点:时间边界(开始包含/结束不包含)、单侧缺省、停用优先、劣质供给 fail-visible。
4
+ */
5
+ import { describe, expect, it } from 'vitest';
6
+ import {
7
+ activityPlayActivitySchema,
8
+ activityPlayDirectoryEntrySchema,
9
+ activityPlayStatusOf,
10
+ activityPlayTargetSchema,
11
+ activityPlayEntrySchema,
12
+ ACTIVITY_PLAY_PARAMS_RESERVED_KEYS,
13
+ } from '../src/domain/activity-play';
14
+ import { activityPlayCardDataSchema } from '../src/activity-play/cards';
15
+ import { ACTIVITY_PLAY_QUERY_SERVICE } from '../src/services/activity-play';
16
+ import slotsJson from '../src/service-slots.json';
17
+
18
+ const T0 = '2026-06-01T10:00:00+08:00';
19
+ const now = (iso: string) => new Date(iso);
20
+
21
+ describe('activityPlayStatusOf 状态计算(单源助手)', () => {
22
+ it('双边界:开始包含、结束不包含', () => {
23
+ const input = { enabled: true, startsAt: T0, endsAt: '2026-06-02T10:00:00+08:00' };
24
+ expect(activityPlayStatusOf({ ...input, now: now('2026-06-01T09:59:59+08:00') })).toBe('not_started');
25
+ expect(activityPlayStatusOf({ ...input, now: now(T0) })).toBe('active');
26
+ expect(activityPlayStatusOf({ ...input, now: new Date('2026-06-01T10:00:00.001+08:00') })).toBe('active');
27
+ expect(activityPlayStatusOf({ ...input, now: now('2026-06-02T09:59:59+08:00') })).toBe('active');
28
+ expect(activityPlayStatusOf({ ...input, now: now('2026-06-02T10:00:00+08:00') })).toBe('ended');
29
+ });
30
+
31
+ it('时区参与判定:同一时刻跨偏移写法等价', () => {
32
+ // 2026-06-01T02:00:00Z === 10:00+08(同一时刻)→ 视为已到达开始界
33
+ const input = { enabled: true, startsAt: '2026-06-01T02:00:00Z' };
34
+ expect(activityPlayStatusOf({ ...input, now: new Date('2026-06-01T01:59:59Z') })).toBe('not_started');
35
+ expect(activityPlayStatusOf({ ...input, now: new Date('2026-06-01T02:00:00Z') })).toBe('active');
36
+ });
37
+
38
+ it('单侧缺省:缺开始恒不落 not_started;缺结束恒不落 ended', () => {
39
+ expect(activityPlayStatusOf({ enabled: true, endsAt: T0, now: now('2026-05-01T00:00:00+08:00') })).toBe('active');
40
+ expect(activityPlayStatusOf({ enabled: true, endsAt: T0, now: now('2026-07-01T00:00:00+08:00') })).toBe('ended');
41
+ expect(activityPlayStatusOf({ enabled: true, startsAt: T0, now: now('2027-01-01T00:00:00+08:00') })).toBe('active');
42
+ });
43
+
44
+ it('停用(enabled=false)优先于时间;双侧缺省 = active', () => {
45
+ expect(activityPlayStatusOf({ enabled: false, startsAt: T0, endsAt: '2026-06-02T10:00:00+08:00', now: now(T0) })).toBe('disabled');
46
+ expect(activityPlayStatusOf({ enabled: true, now: now(T0) })).toBe('active');
47
+ expect(activityPlayStatusOf({ enabled: false, now: now(T0) })).toBe('disabled');
48
+ });
49
+
50
+ it('不可解析时间 fail-visible(不静默当无界处理)', () => {
51
+ expect(() => activityPlayStatusOf({ enabled: true, startsAt: 'not-a-date', now: new Date() })).toThrow(/startsAt/);
52
+ expect(() => activityPlayStatusOf({ enabled: true, endsAt: 'not-a-date', now: new Date() })).toThrow(/endsAt/);
53
+ });
54
+ });
55
+
56
+ describe('activityPlayActivitySchema 运行时校验', () => {
57
+ const base = {
58
+ activityRef: 'demo:wheel',
59
+ title: '幸运转盘',
60
+ status: 'active',
61
+ loginRequired: false,
62
+ };
63
+
64
+ it('最小对象合法;可选字段缺省为 undefined(不合成默认值)', () => {
65
+ const parsed = activityPlayActivitySchema.safeParse(base);
66
+ expect(parsed.success).toBe(true);
67
+ if (parsed.success) {
68
+ expect(parsed.data.loginRequired).toBe(false);
69
+ expect(parsed.data.description).toBeUndefined();
70
+ }
71
+ });
72
+
73
+ it('非合法 URL / 非法 status / 空标题被拒(供给数据劣质可识别)', () => {
74
+ expect(activityPlayActivitySchema.safeParse({ ...base, imageUrl: 'http://x/a.png' }).success).toBe(false);
75
+ // 残缺 URL(正则形状可过、URL 语义不过——评审实测形态)
76
+ expect(activityPlayActivitySchema.safeParse({ ...base, imageUrl: 'https://?' }).success).toBe(false);
77
+ expect(activityPlayActivitySchema.safeParse({ ...base, status: 'running' }).success).toBe(false);
78
+ expect(activityPlayActivitySchema.safeParse({ ...base, title: '' }).success).toBe(false);
79
+ expect(activityPlayActivitySchema.safeParse({ ...base, activityRef: '' }).success).toBe(false);
80
+ });
81
+
82
+ it('未知字段被拒(strict——防 B 端键名 typo 静默入列,后续 entry 字段也须显式扩 schema)', () => {
83
+ expect(activityPlayActivitySchema.safeParse({ ...base, entryUrl: 'x' }).success).toBe(false);
84
+ });
85
+
86
+ it('时间字段要求含时区偏移的 ISO 形态(naive 形态拒绝)', () => {
87
+ expect(activityPlayActivitySchema.safeParse({ ...base, startsAt: '2026-06-01T10:00:00' }).success).toBe(false);
88
+ expect(activityPlayActivitySchema.safeParse({ ...base, startsAt: '2026-06-01T10:00:00+08:00' }).success).toBe(true);
89
+ expect(activityPlayActivitySchema.safeParse({ ...base, endsAt: '2026-06-02T02:00:00Z' }).success).toBe(true);
90
+ });
91
+
92
+ it('时间是真实日历时刻(Julian 滚日值拒绝——Date.parse 会滚动,zod 不认)', () => {
93
+ expect(activityPlayActivitySchema.safeParse({ ...base, startsAt: '2027-02-30T00:00:00Z' }).success).toBe(false);
94
+ expect(activityPlayActivitySchema.safeParse({ ...base, startsAt: '2027-02-29T00:00:00Z' }).success).toBe(false); // 非闰年
95
+ expect(activityPlayActivitySchema.safeParse({ ...base, startsAt: '2028-02-29T00:00:00Z' }).success).toBe(true); // 闰年合法
96
+ });
97
+ });
98
+
99
+ describe('activity-play.query 槽词汇接线', () => {
100
+ it('服务常量与词汇真源一致', () => {
101
+ expect(ACTIVITY_PLAY_QUERY_SERVICE).toBe('activity-play.query');
102
+ expect(slotsJson.slots).toContain(ACTIVITY_PLAY_QUERY_SERVICE);
103
+ });
104
+ });
105
+
106
+ describe('activityPlayDirectoryEntrySchema(供给方配置条目单源)', () => {
107
+ it('enabled/loginRequired 缺省 true/false(与表单 schema default 同源)', () => {
108
+ const parsed = activityPlayDirectoryEntrySchema.safeParse({ activityRef: 'demo:wheel', title: '转盘' });
109
+ expect(parsed.success).toBe(true);
110
+ if (parsed.success) {
111
+ expect(parsed.data.enabled).toBe(true);
112
+ expect(parsed.data.loginRequired).toBe(false);
113
+ }
114
+ });
115
+
116
+ it('显式 null 按类型错误拒绝(缺省缺席合法——区分"没配"与"配错")', () => {
117
+ expect(activityPlayDirectoryEntrySchema.safeParse({ activityRef: 'a', title: 't', enabled: null }).success).toBe(false);
118
+ expect(activityPlayDirectoryEntrySchema.safeParse({ activityRef: 'a', title: 't', loginRequired: null }).success).toBe(false);
119
+ expect(activityPlayDirectoryEntrySchema.safeParse({ activityRef: null, title: 't' }).success).toBe(false);
120
+ expect(activityPlayDirectoryEntrySchema.safeParse({ activityRef: 'a', title: null }).success).toBe(false);
121
+ expect(activityPlayDirectoryEntrySchema.safeParse({ activityRef: 'a', title: 't', description: null }).success).toBe(false);
122
+ });
123
+
124
+ it('劣质字段可识别(返修实证负例直构)', () => {
125
+ expect(activityPlayDirectoryEntrySchema.safeParse({ activityRef: 'bad ref!', title: 't' }).success).toBe(false);
126
+ expect(activityPlayDirectoryEntrySchema.safeParse({ activityRef: 'a', title: 't', startsAt: '2027-02-30T00:00:00Z' }).success).toBe(false);
127
+ expect(activityPlayDirectoryEntrySchema.safeParse({ activityRef: 'a', title: 't', imageUrl: 'https://?' }).success).toBe(false);
128
+ expect(activityPlayDirectoryEntrySchema.safeParse({ activityRef: 'a', title: 't', entryUrl: 'x' }).success).toBe(false); // 提前入场的入口字段
129
+ });
130
+ });
131
+
132
+ describe('activityPlayCardDataSchema(出卡契约:list/detail 传输形态)', () => {
133
+ const item = { activityRef: 'demo:wheel', title: '幸运转盘', status: 'active', loginRequired: false } as const;
134
+
135
+ it('list:条目直接复用 activityPlayActivitySchema;空 items 合法(前端空态/示例验证)', () => {
136
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'list', items: [item] }).success).toBe(true);
137
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'list', items: [] }).success).toBe(true);
138
+ });
139
+
140
+ it('detail:恰好 1 项合法(可携 disabled);空/多项拒绝;非法 mode 拒绝', () => {
141
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'detail', items: [item] }).success).toBe(true);
142
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'detail', items: [{ ...item, status: 'disabled' }] }).success).toBe(true);
143
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'detail', items: [] }).success).toBe(false);
144
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'detail', items: [item, item] }).success).toBe(false);
145
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'mixed', items: [item] }).success).toBe(false);
146
+ });
147
+
148
+ it('list 至多 50 项(limit 协议与卡片容量同源)', () => {
149
+ const many = Array.from({ length: 51 }, () => item);
150
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'list', items: many }).success).toBe(false);
151
+ const fifty = Array.from({ length: 50 }, (_, i) => ({ ...item, activityRef: `demo:${i}` }));
152
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'list', items: fifty }).success).toBe(true);
153
+ });
154
+
155
+ it('卡 data 不携带 enabled 配置字段(strict 继承)——B 端字段不出卡', () => {
156
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'detail', items: [{ ...item, enabled: false }] }).success).toBe(false);
157
+ });
158
+
159
+ it('条目劣质 DTO 传导拒绝(复用条目单源校验)', () => {
160
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'list', items: [{ ...item, imageUrl: 'https://?' }] }).success).toBe(false);
161
+ expect(activityPlayCardDataSchema.safeParse({ mallId: 'mall-demo', mode: 'list', items: [{ ...item, status: 'running' }] }).success).toBe(false);
162
+ expect(activityPlayCardDataSchema.safeParse({ mallId: '', mode: 'list', items: [item] }).success).toBe(false);
163
+ });
164
+ });
165
+
166
+ describe('activityPlayTargetSchema(结构化插件目标——task-03)', () => {
167
+ const ok = { kind: 'miniapp-plugin', pluginAlias: 'demoPlay', page: 'entry', params: { activityId: 'demo-wheel' } } as const;
168
+
169
+ it('基准目标合法;params 缺席归一空对象', () => {
170
+ const r = activityPlayTargetSchema.safeParse(ok);
171
+ expect(r.success).toBe(true);
172
+ if (r.success) expect(r.data.params).toEqual({ activityId: 'demo-wheel' });
173
+ const noParams = activityPlayTargetSchema.safeParse({ kind: 'miniapp-plugin', pluginAlias: 'demoPlay', page: 'entry' });
174
+ expect(noParams.success).toBe(true);
175
+ if (noParams.success) expect(noParams.data.params).toEqual({});
176
+ });
177
+
178
+ it('kind 仅 miniapp-plugin;未知字段拒(strict)', () => {
179
+ expect(activityPlayTargetSchema.safeParse({ ...ok, kind: 'url' }).success).toBe(false);
180
+ expect(activityPlayTargetSchema.safeParse({ ...ok, extra: 1 }).success).toBe(false);
181
+ });
182
+
183
+ it('别名单段约束:拒整条 URL/协议头/路径分隔/穿越/控制字符/空', () => {
184
+ for (const badAlias of ['https://host/x', 'a/b', 'plugin://x', '..', './x', '../x', 'a b', 'a\tb', 'a?b', '', 'x'.repeat(65)]) {
185
+ expect(activityPlayTargetSchema.safeParse({ ...ok, pluginAlias: badAlias }).success, JSON.stringify(badAlias)).toBe(false);
186
+ expect(activityPlayTargetSchema.safeParse({ ...ok, page: badAlias }).success, JSON.stringify(badAlias)).toBe(false);
187
+ }
188
+ });
189
+
190
+ it('params:字符串映射(非字符串值拒);≤32 项;键/值长度与总长上限', () => {
191
+ expect(activityPlayTargetSchema.safeParse({ ...ok, params: { a: 1 } }).success).toBe(false);
192
+ expect(activityPlayTargetSchema.safeParse({ ...ok, params: { a: null } }).success).toBe(false);
193
+ expect(activityPlayTargetSchema.safeParse({ ...ok, params: { '': 'x' } }).success).toBe(false);
194
+ const tooMany = Object.fromEntries(Array.from({ length: 33 }, (_, i) => [`k${i}`, 'v']));
195
+ expect(activityPlayTargetSchema.safeParse({ ...ok, params: tooMany }).success).toBe(false);
196
+ const longValue = { k: 'x'.repeat(257) };
197
+ expect(activityPlayTargetSchema.safeParse({ ...ok, params: longValue }).success).toBe(false);
198
+ const longTotal = { k: 'x'.repeat(3000), k2: 'y'.repeat(2000) };
199
+ expect(activityPlayTargetSchema.safeParse({ ...ok, params: longTotal }).success).toBe(false);
200
+ });
201
+
202
+ it('params 保留键黑名单:身份/凭证键及其大小写/连字符/下划线变体一致拒绝', () => {
203
+ const variants = ['userId', 'USERID', 'user_id', 'user-id', 'openId', 'OPEN-ID', 'memberID', 'token', 'authCode', 'AUTH_CODE', 'access_token', 'refresh-token', 'jwt', 'ticket'];
204
+ for (const key of variants) {
205
+ expect(
206
+ activityPlayTargetSchema.safeParse({ ...ok, params: { [key]: 'x' } }).success,
207
+ key,
208
+ ).toBe(false);
209
+ }
210
+ // 非保留键不受影响
211
+ expect(activityPlayTargetSchema.safeParse({ ...ok, params: { activity: 'a', channel: 'c' } }).success).toBe(true);
212
+ // 黑名单常量本体可考据
213
+ expect((ACTIVITY_PLAY_PARAMS_RESERVED_KEYS as readonly string[]).length).toBeGreaterThan(5);
214
+ });
215
+
216
+ it('params 控制字符拒收(键与值)', () => {
217
+ expect(activityPlayTargetSchema.safeParse({ ...ok, params: { 'a\u0001b': 'v' } }).success).toBe(false);
218
+ expect(activityPlayTargetSchema.safeParse({ ...ok, params: { k: 'v\u0007' } }).success).toBe(false);
219
+ });
220
+ });
221
+
222
+ describe('activityPlayEntrySchema(入口准备结果)', () => {
223
+ it('协议版本 + 活动引用 + 目标;protocolVersion 仅 \'1\',引用与目标契约单源', () => {
224
+ const ok = { protocolVersion: '1', activityRef: 'demo:wheel', target: { kind: 'miniapp-plugin', pluginAlias: 'demoPlay', page: 'entry', params: {} } };
225
+ expect(activityPlayEntrySchema.safeParse(ok).success).toBe(true);
226
+ expect(activityPlayEntrySchema.safeParse({ ...ok, protocolVersion: '2' }).success).toBe(false);
227
+ expect(activityPlayEntrySchema.safeParse({ ...ok, protocolVersion: 1 }).success).toBe(false);
228
+ expect(activityPlayEntrySchema.safeParse({ ...ok, activityRef: '' }).success).toBe(false);
229
+ // 未知字段拒——响应面只有三键(零身份/凭据字段)
230
+ expect(activityPlayEntrySchema.safeParse({ ...ok, userId: 'u' }).success).toBe(false);
231
+ });
232
+ });
@@ -30,6 +30,7 @@ import {
30
30
  SHOPPING_DIRECTORY_SERVICE,
31
31
  } from '../src/services/shopping-guide';
32
32
  import { MALL_INFO_SERVICE } from '../src/services/mall-info';
33
+ import { ACTIVITY_PLAY_ENTRY_SERVICE, ACTIVITY_PLAY_QUERY_SERVICE } from '../src/services/activity-play';
33
34
  import { AUTH_LOGIN_SERVICE } from '../src/assembly';
34
35
 
35
36
  const constants = [
@@ -51,25 +52,27 @@ const constants = [
51
52
  SHOPPING_DIRECTORY_SERVICE,
52
53
  AUTH_LOGIN_SERVICE,
53
54
  MALL_INFO_SERVICE,
55
+ ACTIVITY_PLAY_QUERY_SERVICE,
56
+ ACTIVITY_PLAY_ENTRY_SERVICE,
54
57
  ];
55
58
 
56
59
  describe('service slot 词汇交叉锁(JSON 真源 ↔ TS 共址常量)', () => {
57
- it('计数 = 18(auth.alipay-login 与 mall.info 族含在内;聚合快照槽已退词汇——运行时 ctx.services 键不受影响)', () => {
58
- expect(slotsJson.slots).toHaveLength(18);
59
- expect(constants).toHaveLength(18);
60
+ it('计数 = 20(auth.alipay-login 与 mall.info 族含在内;聚合快照槽已退词汇;activity-play.entry 入词汇——task-03)', () => {
61
+ expect(slotsJson.slots).toHaveLength(20);
62
+ expect(constants).toHaveLength(20);
60
63
  });
61
64
 
62
65
  it('常量并集 ≡ JSON slots(互为子集;无重复无缺漏)', () => {
63
66
  const jsonSet = new Set(slotsJson.slots);
64
67
  const constSet = new Set(constants);
65
- expect(jsonSet.size).toBe(18);
66
- expect(constSet.size).toBe(18);
68
+ expect(jsonSet.size).toBe(20);
69
+ expect(constSet.size).toBe(20);
67
70
  for (const id of jsonSet) expect(constSet.has(id), `常量缺 ${id}`).toBe(true);
68
71
  for (const id of constSet) expect(jsonSet.has(id), `JSON 缺 ${id}`).toBe(true);
69
72
  });
70
73
 
71
- it('词汇形状:domain.capability 且 domain ∈ 五域 + auth', () => {
72
- const domains = new Set(['member', 'parking', 'promotion', 'shopping-guide', 'auth', 'mall']);
74
+ it('词汇形状:domain.capability 且 domain ∈ 六域 + activity-play', () => {
75
+ const domains = new Set(['member', 'parking', 'promotion', 'shopping-guide', 'auth', 'mall', 'activity-play']);
73
76
  for (const id of slotsJson.slots) {
74
77
  expect(id).toMatch(/^[a-z-]+\.[a-z-]+$/);
75
78
  expect(domains.has(id.split('.')[0]), `${id} 域前缀未登记`).toBe(true);