@tbox.cn/app-module-activity-play 0.3.2 → 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 +59 -80
- package/README.md +10 -2
- 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/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/ports.ts +35 -0
- package/src/server/prizes/query.ts +11 -8
- package/src/server/prizes/routes.ts +139 -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 +15 -8
- 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 +33 -15
- 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
package/dist/chunk-HUBEOW44.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
// src/client/cards/view.ts
|
|
2
|
-
function activityPlayStatusText(status) {
|
|
3
|
-
switch (status) {
|
|
4
|
-
case "active":
|
|
5
|
-
return "\u8FDB\u884C\u4E2D";
|
|
6
|
-
case "not_started":
|
|
7
|
-
return "\u672A\u5F00\u59CB";
|
|
8
|
-
case "ended":
|
|
9
|
-
return "\u5DF2\u7ED3\u675F";
|
|
10
|
-
case "disabled":
|
|
11
|
-
return "\u5DF2\u4E0B\u7EBF";
|
|
12
|
-
default:
|
|
13
|
-
return status;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
function activityPlayStatusTone(status) {
|
|
17
|
-
switch (status) {
|
|
18
|
-
case "active":
|
|
19
|
-
return "success";
|
|
20
|
-
case "not_started":
|
|
21
|
-
return "warning";
|
|
22
|
-
case "disabled":
|
|
23
|
-
return "muted";
|
|
24
|
-
default:
|
|
25
|
-
return "muted";
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
function formatActivityPlayTime(value) {
|
|
29
|
-
if (!value) return "";
|
|
30
|
-
const d = new Date(value);
|
|
31
|
-
if (Number.isNaN(d.getTime())) return value;
|
|
32
|
-
const pad = (n) => String(n).padStart(2, "0");
|
|
33
|
-
return `${d.getFullYear()}.${pad(d.getMonth() + 1)}.${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// src/client/cards/activity-play/ActivityPlayCard.tsx
|
|
37
|
-
import { jsx, jsxs } from "react/jsx-runtime";
|
|
38
|
-
function ActivityRow({ item }) {
|
|
39
|
-
const tone = activityPlayStatusTone(item.status);
|
|
40
|
-
return /* @__PURE__ */ jsx("div", { className: "ap-card__row", children: /* @__PURE__ */ jsxs("div", { className: "ap-card__row-main", children: [
|
|
41
|
-
/* @__PURE__ */ jsxs("div", { className: "ap-card__row-title", children: [
|
|
42
|
-
/* @__PURE__ */ jsx("strong", { children: item.title }),
|
|
43
|
-
/* @__PURE__ */ jsx("span", { className: `ap-badge ap-badge--${tone}`, children: activityPlayStatusText(item.status) })
|
|
44
|
-
] }),
|
|
45
|
-
item.description && /* @__PURE__ */ jsx("p", { className: "ap-card__row-desc", children: item.description })
|
|
46
|
-
] }) });
|
|
47
|
-
}
|
|
48
|
-
function ActivityPlayCard(props) {
|
|
49
|
-
const { data } = props;
|
|
50
|
-
if (data.mode === "detail") {
|
|
51
|
-
const item = data.items[0];
|
|
52
|
-
if (!item) return null;
|
|
53
|
-
const range = [formatActivityPlayTime(item.startsAt), formatActivityPlayTime(item.endsAt)].filter(Boolean).join(" \u2014 ");
|
|
54
|
-
return /* @__PURE__ */ jsxs("section", { className: "ap-card ap-card--detail", children: [
|
|
55
|
-
/* @__PURE__ */ jsx("span", { className: "ap-card__eyebrow", children: "\u6D3B\u52A8\u73A9\u6CD5" }),
|
|
56
|
-
/* @__PURE__ */ jsxs("div", { className: "ap-card__detail-head", children: [
|
|
57
|
-
/* @__PURE__ */ jsx("strong", { children: item.title }),
|
|
58
|
-
/* @__PURE__ */ jsx("span", { className: `ap-badge ap-badge--${activityPlayStatusTone(item.status)}`, children: activityPlayStatusText(item.status) })
|
|
59
|
-
] }),
|
|
60
|
-
item.description && /* @__PURE__ */ jsx("p", { className: "ap-card__detail-desc", children: item.description }),
|
|
61
|
-
item.imageUrl && /* @__PURE__ */ jsx("img", { className: "ap-card__cover", src: item.imageUrl, alt: `${item.title}\u5C01\u9762`, loading: "lazy" }),
|
|
62
|
-
range && /* @__PURE__ */ jsxs("p", { className: "ap-card__meta", children: [
|
|
63
|
-
"\u6D3B\u52A8\u65F6\u95F4\uFF1A",
|
|
64
|
-
range
|
|
65
|
-
] }),
|
|
66
|
-
item.loginRequired && /* @__PURE__ */ jsx("p", { className: "ap-card__meta", children: "\u53C2\u4E0E\u9700\u5148\u767B\u5F55" })
|
|
67
|
-
] });
|
|
68
|
-
}
|
|
69
|
-
if (data.items.length === 0) {
|
|
70
|
-
return /* @__PURE__ */ jsxs("section", { className: "ap-card ap-card--list", children: [
|
|
71
|
-
/* @__PURE__ */ jsx("span", { className: "ap-card__eyebrow", children: "\u6D3B\u52A8\u73A9\u6CD5" }),
|
|
72
|
-
/* @__PURE__ */ jsx("p", { className: "ap-card__empty", children: "\u5F53\u524D\u5546\u573A\u6682\u65E0\u8FDB\u884C\u4E2D\u7684\u6D3B\u52A8\u73A9\u6CD5" })
|
|
73
|
-
] });
|
|
74
|
-
}
|
|
75
|
-
return /* @__PURE__ */ jsxs("section", { className: "ap-card ap-card--list", children: [
|
|
76
|
-
/* @__PURE__ */ jsxs("span", { className: "ap-card__eyebrow", children: [
|
|
77
|
-
"\u6D3B\u52A8\u73A9\u6CD5\uFF08",
|
|
78
|
-
data.items.length,
|
|
79
|
-
"\uFF09"
|
|
80
|
-
] }),
|
|
81
|
-
/* @__PURE__ */ jsx("div", { className: "ap-card__list", children: data.items.map((item) => /* @__PURE__ */ jsx(ActivityRow, { item }, item.activityRef)) })
|
|
82
|
-
] });
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// src/client/index.ts
|
|
86
|
-
var cards = {
|
|
87
|
-
"activity-play": ActivityPlayCard
|
|
88
|
-
};
|
|
89
|
-
var clientModule = {
|
|
90
|
-
cards,
|
|
91
|
-
register(ctx) {
|
|
92
|
-
ctx.cards.registerMap(cards);
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
export {
|
|
97
|
-
clientModule
|
|
98
|
-
};
|
package/tests/entry.test.ts
DELETED
|
@@ -1,458 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 参与入口准备路由行为测试(task-03——六步检查序/错误族/no-store/一致性/登录语义)。
|
|
3
|
-
* 覆盖风险≡验收点:T03-04/05/06/07/08/10 的模块契约面(真实 registry 与生成应用证据
|
|
4
|
-
* 另见 provider e2e 与交付报告)。
|
|
5
|
-
*/
|
|
6
|
-
import { describe, expect, it, vi } 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 { withMallScopeFromQuery, activityPlayEntrySchema, type ActivityPlayActivity, type ActivityPlayEntryPort, type ActivityPlayQueryPort } from '@tbox.cn/app-contracts-mall';
|
|
13
|
-
import { createActivityPlayRouter, activityMatchesForEntry } from '../src/server/routes';
|
|
14
|
-
|
|
15
|
-
const ACTIVITY = {
|
|
16
|
-
activityRef: 'demo:wheel',
|
|
17
|
-
title: '幸运大转盘',
|
|
18
|
-
description: '进入活动页面查看参与规则',
|
|
19
|
-
status: 'active' as const,
|
|
20
|
-
loginRequired: false,
|
|
21
|
-
};
|
|
22
|
-
const TARGET = { kind: 'miniapp-plugin' as const, pluginAlias: 'demoPlay', page: 'entry', params: { activityId: 'demo-wheel' } };
|
|
23
|
-
|
|
24
|
-
const reqCtxOf = (): RequestContext => ({ identity: { userId: 'guest_g', source: 'guest' } });
|
|
25
|
-
const boundCtx = (mallId = 'mall-a'): RequestContext => withMallScopeFromQuery(reqCtxOf(), { mallId });
|
|
26
|
-
|
|
27
|
-
/** 双 stub:query Port 与 entry Port 形态可控 */
|
|
28
|
-
interface Harness {
|
|
29
|
-
app: Express;
|
|
30
|
-
query: ActivityPlayQueryPort;
|
|
31
|
-
entry: ActivityPlayEntryPort;
|
|
32
|
-
logger: { warn: ReturnType<typeof vi.fn> };
|
|
33
|
-
}
|
|
34
|
-
function makeApp(opts: {
|
|
35
|
-
reqCtx?: RequestContext | undefined;
|
|
36
|
-
activity?: ActivityPlayActivity | null;
|
|
37
|
-
entryRecord?: { activity: ActivityPlayActivity; target: typeof TARGET } | null;
|
|
38
|
-
entryPortOverride?: ActivityPlayEntryPort;
|
|
39
|
-
queryError?: unknown;
|
|
40
|
-
entryResolveError?: unknown;
|
|
41
|
-
bindingHash?: string;
|
|
42
|
-
entryBinding?: { provider?: string; implementation?: string; directoryHash?: string };
|
|
43
|
-
registryOverrides?: Record<string, (provider: string, implementation: string) => boolean>;
|
|
44
|
-
omitBinding?: boolean;
|
|
45
|
-
omitBindingLookup?: boolean;
|
|
46
|
-
now?: () => Date;
|
|
47
|
-
}): Harness {
|
|
48
|
-
const activity = 'activity' in opts ? opts.activity : ACTIVITY;
|
|
49
|
-
const baseBinding = {
|
|
50
|
-
provider: 'stubSameSupply',
|
|
51
|
-
implementation: 'stubImpl@1',
|
|
52
|
-
directoryHash: `dir-${opts.bindingHash ?? 'shared'}`,
|
|
53
|
-
};
|
|
54
|
-
const query: ActivityPlayQueryPort = {
|
|
55
|
-
listActivities: async () => ({ items: activity ? [activity] : [] }),
|
|
56
|
-
getActivity: async () => {
|
|
57
|
-
if (opts.queryError) throw opts.queryError;
|
|
58
|
-
return activity ?? null;
|
|
59
|
-
},
|
|
60
|
-
supplyBinding: { ...baseBinding },
|
|
61
|
-
};
|
|
62
|
-
const entry: ActivityPlayEntryPort = {
|
|
63
|
-
getEntry: async () => {
|
|
64
|
-
if (opts.entryResolveError) throw opts.entryResolveError;
|
|
65
|
-
return 'entryRecord' in opts ? opts.entryRecord ?? null : { activity: ACTIVITY, target: TARGET };
|
|
66
|
-
},
|
|
67
|
-
supplyBinding: { ...baseBinding, ...(opts.entryBinding ?? {}) },
|
|
68
|
-
};
|
|
69
|
-
const resolveService = vi.fn(async ({ service }: { service: string }) => {
|
|
70
|
-
if (service === 'activity-play.query') return query;
|
|
71
|
-
if (opts.entryResolveError) throw opts.entryResolveError;
|
|
72
|
-
return opts.entryPortOverride ?? entry;
|
|
73
|
-
});
|
|
74
|
-
// registry 实绑点查(评审返修尾款):缺省「实绑=自报」(点查通过);可按槽 override
|
|
75
|
-
const hasProviderBinding = vi.fn(
|
|
76
|
-
(service: string, _scope: Record<string, string>, provider: string, implementation: string) => {
|
|
77
|
-
const override = opts.registryOverrides?.[service];
|
|
78
|
-
if (override) return override(provider, implementation);
|
|
79
|
-
return provider === 'stubSameSupply' && implementation === 'stubImpl@1';
|
|
80
|
-
},
|
|
81
|
-
);
|
|
82
|
-
if (opts.omitBinding) {
|
|
83
|
-
delete (query as { supplyBinding?: unknown }).supplyBinding;
|
|
84
|
-
delete (entry as { supplyBinding?: unknown }).supplyBinding;
|
|
85
|
-
}
|
|
86
|
-
const ctxIntegrationsBase: Record<string, unknown> = { resolveService };
|
|
87
|
-
if (!opts.omitBindingLookup) ctxIntegrationsBase['hasProviderBinding'] = hasProviderBinding;
|
|
88
|
-
const logger = { warn: vi.fn() };
|
|
89
|
-
const ctxStub = {
|
|
90
|
-
integrations: ctxIntegrationsBase,
|
|
91
|
-
logger,
|
|
92
|
-
} as unknown as ServerContext;
|
|
93
|
-
const app = express();
|
|
94
|
-
app.use(express.json());
|
|
95
|
-
// 缺省注入 guest 身份(无绑定商圈——入口走 query mallId;显式 undefined 走 401 旁路用例)
|
|
96
|
-
app.use((req: Request, _res: Response, next: NextFunction) => {
|
|
97
|
-
(req as unknown as Record<string, unknown>)['tboxAuthContext'] = opts.reqCtx ?? reqCtxOf();
|
|
98
|
-
next();
|
|
99
|
-
});
|
|
100
|
-
app.use(createActivityPlayRouter(ctxStub));
|
|
101
|
-
return { app, query, entry, logger };
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
describe('POST /activities/:activityRef/entry — 六步检查序', () => {
|
|
105
|
-
it('全链成功:body 空对象;data={protocolVersion,activityRef,target} 过公开 schema;no-store', async () => {
|
|
106
|
-
const h = makeApp({});
|
|
107
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
108
|
-
expect(res.status).toBe(200);
|
|
109
|
-
expect(res.headers['cache-control']).toBe('no-store');
|
|
110
|
-
activityPlayEntrySchema.parse(res.body.data);
|
|
111
|
-
expect(res.body.data).toEqual({ protocolVersion: '1', activityRef: 'demo:wheel', target: TARGET });
|
|
112
|
-
// body 省略同样成功
|
|
113
|
-
const res2 = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a');
|
|
114
|
-
expect(res2.status).toBe(200);
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('步骤 1:非空业务体 → 400 INVALID(目标/身份/参数覆盖拒收;context 键平台保留忽略)', async () => {
|
|
118
|
-
const h = makeApp({});
|
|
119
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({ target: { kind: 'url' } });
|
|
120
|
-
expect(res.status).toBe(400);
|
|
121
|
-
expect(res.body.error.code).toBe('INVALID_REQUEST');
|
|
122
|
-
const res2 = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({ userId: 'evil' });
|
|
123
|
-
expect(res2.status).toBe(400);
|
|
124
|
-
const res3 = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({ context: { mallId: 'mall-b' } });
|
|
125
|
-
expect(res3.status).toBe(200); // 平台保留键,模块忽略
|
|
126
|
-
const res4 = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').set('Content-Type', 'application/json').send('null');
|
|
127
|
-
expect(res4.status).toBe(400);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('步骤 1:缺商圈 400 SCOPE_REQUIRED;非法 ref 400;冲突 403(空 body 全步前置)', async () => {
|
|
131
|
-
expect((await request(makeApp({}).app).post('/activities/demo:wheel/entry')).status).toBe(400);
|
|
132
|
-
expect((await request(makeApp({}).app).post('/activities/bad%20ref/entry?mallId=mall-a').send({})).status).toBe(400);
|
|
133
|
-
const bound = makeApp({ reqCtx: boundCtx('mall-b') });
|
|
134
|
-
expect((await request(bound.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({})).status).toBe(403);
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
it('步骤 2:活动不存在 → 404(不读卡片旧状态)', async () => {
|
|
138
|
-
const h = makeApp({ activity: null });
|
|
139
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
140
|
-
expect(res.status).toBe(404);
|
|
141
|
-
expect(res.body.error.code).toBe('ACTIVITY_NOT_FOUND');
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('步骤 3:状态门三态 409(disabled/not_started/ended——只有 active 前进),业务错误 no-store', async () => {
|
|
145
|
-
for (const status of ['disabled', 'not_started', 'ended'] as const) {
|
|
146
|
-
const code = status === 'disabled' ? 'ACTIVITY_DISABLED' : status === 'not_started' ? 'ACTIVITY_NOT_STARTED' : 'ACTIVITY_ENDED';
|
|
147
|
-
const h = makeApp({ activity: { ...ACTIVITY, status } });
|
|
148
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
149
|
-
expect(res.status, status).toBe(409);
|
|
150
|
-
expect(res.body.error.code).toBe(code);
|
|
151
|
-
expect(res.headers['cache-control']).toBe('no-store');
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
it('步骤 4:loginRequired=true——guest/local/external 放行,anonymous 401 LOGIN_REQUIRED;false 时 anonymous 继续', async () => {
|
|
156
|
-
const mk = (source: string, loginRequired: boolean): Express => {
|
|
157
|
-
const binding = { provider: 'x', implementation: 'y@1', directoryHash: 'h' };
|
|
158
|
-
const query: ActivityPlayQueryPort = {
|
|
159
|
-
listActivities: async () => ({ items: [ACTIVITY] }),
|
|
160
|
-
getActivity: async () => ({ ...ACTIVITY, loginRequired }),
|
|
161
|
-
supplyBinding: binding,
|
|
162
|
-
};
|
|
163
|
-
const entry: ActivityPlayEntryPort = {
|
|
164
|
-
getEntry: async () => ({ activity: { ...ACTIVITY, loginRequired }, target: TARGET }),
|
|
165
|
-
supplyBinding: binding,
|
|
166
|
-
};
|
|
167
|
-
const ctxStub = {
|
|
168
|
-
integrations: {
|
|
169
|
-
resolveService: vi.fn(async ({ service }: { service: string }) => (service === 'activity-play.entry' ? entry : query)),
|
|
170
|
-
hasProviderBinding: vi.fn(() => true),
|
|
171
|
-
},
|
|
172
|
-
logger: { warn: vi.fn() },
|
|
173
|
-
} as unknown as ServerContext;
|
|
174
|
-
const app = express();
|
|
175
|
-
app.use(express.json());
|
|
176
|
-
app.use((req: Request, _res: Response, next: NextFunction) => {
|
|
177
|
-
(req as unknown as Record<string, unknown>)['tboxAuthContext'] = withMallScopeFromQuery(
|
|
178
|
-
{ identity: { userId: 'u', source } as never } as RequestContext,
|
|
179
|
-
{ mallId: 'mall-a' },
|
|
180
|
-
);
|
|
181
|
-
next();
|
|
182
|
-
});
|
|
183
|
-
app.use(createActivityPlayRouter(ctxStub));
|
|
184
|
-
return app;
|
|
185
|
-
};
|
|
186
|
-
for (const source of ['local', 'external', 'guest']) {
|
|
187
|
-
const res = await request(mk(source, true)).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
188
|
-
expect(res.status, source).toBe(200);
|
|
189
|
-
}
|
|
190
|
-
const resAnon = await request(mk('anonymous', true)).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
191
|
-
expect(resAnon.status).toBe(401);
|
|
192
|
-
expect(resAnon.body.error.code).toBe('LOGIN_REQUIRED');
|
|
193
|
-
|
|
194
|
-
// loginRequired=false:anonymous 继续(入口可读)
|
|
195
|
-
const resAnonOk = await request(mk('anonymous', false)).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
196
|
-
expect(resAnonOk.status).toBe(200);
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
it('步骤 5:entry 槽未配/实例缺 503;entry 缺席 409 ENTRY_UNAVAILABLE;分叉(同引用不同活动字段)409', async () => {
|
|
200
|
-
const notConfigured = await request(makeApp({ entryResolveError: Object.assign(new Error('x'), { code: 'PROVIDER_SERVICE_NOT_CONFIGURED' }) }).app)
|
|
201
|
-
.post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
202
|
-
expect(notConfigured.status).toBe(503);
|
|
203
|
-
expect(notConfigured.body.error.code).toBe('NOT_CONFIGURED');
|
|
204
|
-
|
|
205
|
-
const absent = await request(makeApp({ entryRecord: null }).app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
206
|
-
expect(absent.status).toBe(409);
|
|
207
|
-
expect(absent.body.error.code).toBe('ENTRY_UNAVAILABLE');
|
|
208
|
-
|
|
209
|
-
// 分叉:entry 供给目录中同 ref 是不同活动(title 差)——不能用 query 侧检查放行 entry 侧目标
|
|
210
|
-
const forked = await request(makeApp({ entryRecord: { activity: { ...ACTIVITY, title: '另一目录的转盘' }, target: { ...TARGET, params: { activityId: 'b-target' } } } }).app)
|
|
211
|
-
.post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
212
|
-
expect(forked.status).toBe(409);
|
|
213
|
-
expect(forked.body.error.code).toBe('ENTRY_UNAVAILABLE');
|
|
214
|
-
|
|
215
|
-
// 状态/登录要求分叉同样不放行(任务核心反例)
|
|
216
|
-
const loginFork = await request(makeApp({ entryRecord: { activity: { ...ACTIVITY, loginRequired: true }, target: TARGET } }).app)
|
|
217
|
-
.post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
218
|
-
expect(loginFork.status).toBe(409);
|
|
219
|
-
const statusFork = await request(makeApp({ entryRecord: { activity: { ...ACTIVITY, status: 'not_started' }, target: TARGET } }).app)
|
|
220
|
-
.post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
221
|
-
expect(statusFork.status).toBe(409);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it('步骤 5/6:供给异常/出站结构非法 → 502,原始细节不透传;非法出站无缓存敞口', async () => {
|
|
225
|
-
const err = await request(makeApp({ entryResolveError: new Error('原始敏感配置') }).app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
226
|
-
expect(err.status).toBe(502);
|
|
227
|
-
expect(err.body.error.code).toBe('PROVIDER_ERROR');
|
|
228
|
-
expect(JSON.stringify(err.body)).not.toContain('敏感');
|
|
229
|
-
|
|
230
|
-
// 出站自证失败:target 破坏(alias URL 形态)
|
|
231
|
-
const badOut = await request(makeApp({ entryRecord: { activity: ACTIVITY, target: { ...TARGET, pluginAlias: 'https://x' } as typeof TARGET } }).app)
|
|
232
|
-
.post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
233
|
-
expect(badOut.status).toBe(502);
|
|
234
|
-
expect(badOut.body.error.code).toBe('PROVIDER_ERROR');
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
it('重试无副作用语义:同参连续三次请求均成功且响应独立(每次重新读活动/入口)', async () => {
|
|
238
|
-
const h = makeApp({});
|
|
239
|
-
for (let i = 0; i < 3; i++) {
|
|
240
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
241
|
-
expect(res.status).toBe(200);
|
|
242
|
-
expect(res.body.data.target).toEqual(TARGET);
|
|
243
|
-
}
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
it('GET 查询面不受 entry 影响:活动对象无 entry/start 等入口字段(零泄漏)', async () => {
|
|
247
|
-
const h = makeApp({});
|
|
248
|
-
const res = await request(h.app).get('/activities/demo:wheel?mallId=mall-a');
|
|
249
|
-
expect(res.status).toBe(200);
|
|
250
|
-
expect(JSON.stringify(res.body)).not.toContain('target');
|
|
251
|
-
expect(JSON.stringify(res.body)).not.toContain('pluginAlias');
|
|
252
|
-
});
|
|
253
|
-
});
|
|
254
|
-
|
|
255
|
-
describe('activityMatchesForEntry(两槽一致性比对单源)', () => {
|
|
256
|
-
it('同对象真;任一字段差(含 status/login/描述/时间)即假', () => {
|
|
257
|
-
expect(activityMatchesForEntry(ACTIVITY, { ...ACTIVITY })).toBe(true);
|
|
258
|
-
for (const patch of [
|
|
259
|
-
{ title: 'x' }, { status: 'ended' as const }, { loginRequired: true }, { description: 'x' },
|
|
260
|
-
{ startsAt: '2026-01-01T00:00:00+08:00' }, { imageUrl: 'https://e.com/a.png' },
|
|
261
|
-
]) {
|
|
262
|
-
expect(activityMatchesForEntry(ACTIVITY, { ...ACTIVITY, ...patch }), JSON.stringify(patch)).toBe(false);
|
|
263
|
-
}
|
|
264
|
-
// description 在两侧同为 undefined(键缺席——归一后等价)
|
|
265
|
-
const noDesc = { ...ACTIVITY, description: undefined };
|
|
266
|
-
expect(activityMatchesForEntry(noDesc, { ...noDesc })).toBe(true);
|
|
267
|
-
expect(activityMatchesForEntry(noDesc, { ...noDesc, description: 'x' })).toBe(false);
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
|
|
271
|
-
describe('评审返修三项(供给签名/非法供给数据/日志安全)', () => {
|
|
272
|
-
it('分叉 P2#1a:同活动字段但目录不同(query 目录 aPlay / entry 目录 otherPlay)→ 409 联检拒绝(不再单 DTO 比对)', async () => {
|
|
273
|
-
const h = makeApp({ entryBinding: { directoryHash: 'dir-different-directory' } });
|
|
274
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
275
|
-
expect(res.status).toBe(409);
|
|
276
|
-
expect(res.body.error.code).toBe('ENTRY_UNAVAILABLE');
|
|
277
|
-
expect(res.body.error.message).toContain('供给不一致');
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
it('分叉 P2#1b:entry 槽绑另一 provider/implementation(活动字段全同)→ 409 联检拒绝', async () => {
|
|
281
|
-
// 场景:entry 侧 port 实现 supplyBinding={provider:'other',impl:'别impl'}——活动目录同质仍不可证同源
|
|
282
|
-
const h = makeApp({ entryBinding: { provider: 'otherVendor', implementation: 'impl2@1', directoryHash: 'dir-shared' } });
|
|
283
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
284
|
-
expect(res.status).toBe(409);
|
|
285
|
-
expect(res.body.error.code).toBe('ENTRY_UNAVAILABLE');
|
|
286
|
-
});
|
|
287
|
-
|
|
288
|
-
it('分叉 P2#1c:任一侧签名缺席(供给未实现 supplyBinding)→ fail-closed 409', async () => {
|
|
289
|
-
const h = makeApp({ omitBinding: true });
|
|
290
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
291
|
-
expect(res.status).toBe(409);
|
|
292
|
-
expect(res.body.error.code).toBe('ENTRY_UNAVAILABLE');
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
it('供给对象非法 P2#2a:query 侧活动 status="paused" → 502(不通过状态门)', async () => {
|
|
296
|
-
const h = makeApp({ activity: { ...ACTIVITY, status: 'paused' as 'active' } });
|
|
297
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
298
|
-
expect(res.status).toBe(502);
|
|
299
|
-
expect(res.body.error.code).toBe('PROVIDER_ERROR');
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
it('供给对象非法 P2#2b:query 侧活动缺 loginRequired → 502(不通过登录步骤)', async () => {
|
|
303
|
-
const h = makeApp({ activity: { ...ACTIVITY, loginRequired: undefined } as unknown as typeof ACTIVITY });
|
|
304
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
305
|
-
expect(res.status).toBe(502);
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
it('供给对象非法 P2#2c:entry 快照 status 非法 → 502;query 侧 activityRef 漂移 → 502', async () => {
|
|
309
|
-
const pausedEntry = makeApp({ entryRecord: { activity: { ...ACTIVITY, status: 'paused' as 'active' }, target: TARGET } });
|
|
310
|
-
expect((await request(pausedEntry.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({})).status).toBe(502);
|
|
311
|
-
const driftedQuery = makeApp({ activity: { ...ACTIVITY, activityRef: 'other:ref' }, entryRecord: { activity: ACTIVITY } as typeof TARGET extends never ? never : { activity: typeof ACTIVITY; target: typeof TARGET } });
|
|
312
|
-
expect((await request(driftedQuery.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({})).status).toBe(502);
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
it('日志安全 P2#3:供给异常响应与 warn 调用体均不含敏感细节——只记 code+ref+mall', async () => {
|
|
316
|
-
const h = makeApp({ entryResolveError: Object.assign(new Error('配置细节 secret-marker target=https://evil/x param credentials: SK-99'), { code: 'PROVIDER_SERVICE_NOT_CONFIGURED' }) });
|
|
317
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
318
|
-
|
|
319
|
-
// 响应零细节
|
|
320
|
-
expect(res.status).toBe(503);
|
|
321
|
-
expect(JSON.stringify(res.body)).not.toContain('secret-marker');
|
|
322
|
-
expect(JSON.stringify(res.body)).not.toContain('SK-99');
|
|
323
|
-
// 日志:warn 必有,但不含敏感 marker——只记安全 code+定位字段
|
|
324
|
-
expect(h.logger.warn).toHaveBeenCalled();
|
|
325
|
-
const logged = JSON.stringify(h.logger.warn.mock.calls);
|
|
326
|
-
expect(logged).not.toContain('secret-marker');
|
|
327
|
-
expect(logged).not.toContain('SK-99');
|
|
328
|
-
expect(logged).not.toContain('err');
|
|
329
|
-
expect(logged).toContain('NOT_CONFIGURED');
|
|
330
|
-
expect(logged).toContain('demo:wheel');
|
|
331
|
-
expect(logged).toContain('mall-a');
|
|
332
|
-
});
|
|
333
|
-
|
|
334
|
-
it('联检缺失但两槽目录确不同(无签名的双 portal 场景)→ 409 从而不会以 200 放行 false 换入 target', async () => {
|
|
335
|
-
const h = makeApp({
|
|
336
|
-
omitBinding: true,
|
|
337
|
-
entryRecord: { activity: ACTIVITY, target: { ...TARGET, params: { activityId: 'other-target' } } },
|
|
338
|
-
});
|
|
339
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
340
|
-
expect(res.status).toBe(409); // fail-closed
|
|
341
|
-
});
|
|
342
|
-
});
|
|
343
|
-
|
|
344
|
-
describe('评审返修尾款:registry 实绑点查(自报签名可谎报——同工厂复用绑别槽)', () => {
|
|
345
|
-
it('entry 槽实际绑定 demo/demo-play@1(复用同一工厂、同目录、自报仍 hupo 常量)→ 409', async () => {
|
|
346
|
-
// 自报一致(stub binding 同 provider/impl/hash)+ registry 点查戳穿:entry 槽实际有效绑定 != 自报
|
|
347
|
-
const h = makeApp({
|
|
348
|
-
registryOverrides: {
|
|
349
|
-
'activity-play.entry': (provider, implementation) => provider === 'demo' && implementation === 'demo-play@1',
|
|
350
|
-
},
|
|
351
|
-
});
|
|
352
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
353
|
-
expect(res.status).toBe(409);
|
|
354
|
-
expect(res.body.error.code).toBe('ENTRY_UNAVAILABLE');
|
|
355
|
-
expect(h.logger.warn.mock.calls.length).toBe(0); // 业务 409 非 5xx 不打日志
|
|
356
|
-
});
|
|
357
|
-
|
|
358
|
-
it('query 槽实绑与自报不符 → 409(双侧点查对称)', async () => {
|
|
359
|
-
const h = makeApp({
|
|
360
|
-
registryOverrides: {
|
|
361
|
-
'activity-play.query': () => false,
|
|
362
|
-
},
|
|
363
|
-
});
|
|
364
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
365
|
-
expect(res.status).toBe(409);
|
|
366
|
-
});
|
|
367
|
-
|
|
368
|
-
it('点查能力缺席(旧 SDK 无 hasProviderBinding)→ fail-closed 409', async () => {
|
|
369
|
-
const h = makeApp({ omitBindingLookup: true });
|
|
370
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
371
|
-
expect(res.status).toBe(409);
|
|
372
|
-
expect(res.body.error.code).toBe('ENTRY_UNAVAILABLE');
|
|
373
|
-
});
|
|
374
|
-
|
|
375
|
-
it('实绑与自报一致(双槽点查皆真)→ 200——点查不误伤合法路径', async () => {
|
|
376
|
-
const h = makeApp({});
|
|
377
|
-
const res = await request(h.app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
378
|
-
expect(res.status).toBe(200);
|
|
379
|
-
});
|
|
380
|
-
});
|
|
381
|
-
|
|
382
|
-
describe('评审返修尾款:真实 registry 回归(createIntegrationsRegistry × 跨 provider 谎报)', () => {
|
|
383
|
-
it('两槽同目录同工厂、entry 绑 demo/demo-play@1 自报 hupo → 点查 409;全 hupo 双槽 → 200', async () => {
|
|
384
|
-
const { createIntegrationsRegistry, createAuthStrategyRegistry, createEnvironmentSecretProvider, createServerContext } = await import('@tbox.cn/app-agent-sdk-server');
|
|
385
|
-
const catalog = {
|
|
386
|
-
providers: {
|
|
387
|
-
hupo: {
|
|
388
|
-
'hupo-activity-play@1': { provider: 'hupo', implementation: 'hupo-activity-play@1', credentialType: '-', owner: '@tbox.cn/app-provider-hupo', local: true, services: ['activity-play.query', 'activity-play.entry'] },
|
|
389
|
-
},
|
|
390
|
-
demo: {
|
|
391
|
-
'demo-play@1': { provider: 'demo', implementation: 'demo-play@1', credentialType: '-', owner: 'demo', local: true, services: ['activity-play.query', 'activity-play.entry'] },
|
|
392
|
-
},
|
|
393
|
-
},
|
|
394
|
-
services: {},
|
|
395
|
-
credentialTypes: {},
|
|
396
|
-
};
|
|
397
|
-
// 复刻评审场景:同一工厂被两组 (provider, impl) 注册——实例按 hupo 常量自报签名(谎报面)
|
|
398
|
-
const sharedDirectory = { activities: [{ activity: { activityRef: 'demo:wheel', title: '幸运大转盘', loginRequired: false }, entry: { kind: 'miniapp-plugin', pluginAlias: 'aPlay', page: 'entry', params: { v: '1' } } }] };
|
|
399
|
-
const makeFatPort = () => ({
|
|
400
|
-
supplyBinding: { provider: 'hupo', implementation: 'hupo-activity-play@1', directoryHash: 'shared-hash' },
|
|
401
|
-
listActivities: async () => ({ items: [] }),
|
|
402
|
-
getActivity: async () => ({ activityRef: 'demo:wheel', title: '幸运大转盘', status: 'active', loginRequired: false }),
|
|
403
|
-
getEntry: async () => ({ activity: { activityRef: 'demo:wheel', title: '幸运大转盘', status: 'active', loginRequired: false }, target: { kind: 'miniapp-plugin' as const, pluginAlias: 'aPlay', page: 'entry', params: {} } }),
|
|
404
|
-
});
|
|
405
|
-
const registry = createIntegrationsRegistry({
|
|
406
|
-
integrations: {
|
|
407
|
-
services: {
|
|
408
|
-
'activity-play.query': { provider: 'hupo', implementation: 'hupo-activity-play@1', instances: { 'mall-a': { config: sharedDirectory } } },
|
|
409
|
-
'activity-play.entry': { provider: 'demo', implementation: 'demo-play@1', instances: { 'mall-a': { config: sharedDirectory } } },
|
|
410
|
-
},
|
|
411
|
-
} as never,
|
|
412
|
-
catalog,
|
|
413
|
-
authStrategies: createAuthStrategyRegistry(),
|
|
414
|
-
secretProvider: createEnvironmentSecretProvider({ source: {}, production: false }),
|
|
415
|
-
instanceIdOf: (scope: Record<string, string>) => scope['mallId'] ?? '',
|
|
416
|
-
});
|
|
417
|
-
(registry as unknown as { registerAdapter: (p: string, i: string, f: () => unknown) => void }).registerAdapter('hupo', 'hupo-activity-play@1', makeFatPort);
|
|
418
|
-
(registry as unknown as { registerAdapter: (p: string, i: string, f: () => unknown) => void }).registerAdapter('demo', 'demo-play@1', makeFatPort);
|
|
419
|
-
const ctx = createServerContext({ integrations: registry });
|
|
420
|
-
const app = express();
|
|
421
|
-
app.use(express.json());
|
|
422
|
-
app.use((req: Request, _res: Response, next: NextFunction) => {
|
|
423
|
-
(req as unknown as Record<string, unknown>).tboxAuthContext = reqCtxOf();
|
|
424
|
-
next();
|
|
425
|
-
});
|
|
426
|
-
app.use(createActivityPlayRouter(ctx));
|
|
427
|
-
// 评审复现场景:自报一致 + 实际 entry 绑 demo → 点查 409
|
|
428
|
-
const liedRes = await request(app).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
429
|
-
expect(liedRes.status).toBe(409);
|
|
430
|
-
expect(liedRes.body.error.code).toBe('ENTRY_UNAVAILABLE');
|
|
431
|
-
|
|
432
|
-
// 对照:两槽都实绑 hupo(同目录同签名)→ 200
|
|
433
|
-
const registry2 = createIntegrationsRegistry({
|
|
434
|
-
integrations: {
|
|
435
|
-
services: {
|
|
436
|
-
'activity-play.query': { provider: 'hupo', implementation: 'hupo-activity-play@1', instances: { 'mall-a': { config: sharedDirectory } } },
|
|
437
|
-
'activity-play.entry': { provider: 'hupo', implementation: 'hupo-activity-play@1', instances: { 'mall-a': { config: sharedDirectory } } },
|
|
438
|
-
},
|
|
439
|
-
} as never,
|
|
440
|
-
catalog,
|
|
441
|
-
authStrategies: createAuthStrategyRegistry(),
|
|
442
|
-
secretProvider: createEnvironmentSecretProvider({ source: {}, production: false }),
|
|
443
|
-
instanceIdOf: (scope: Record<string, string>) => scope['mallId'] ?? '',
|
|
444
|
-
});
|
|
445
|
-
(registry2 as unknown as { registerAdapter: (p: string, i: string, f: () => unknown) => void }).registerAdapter('hupo', 'hupo-activity-play@1', makeFatPort);
|
|
446
|
-
const ctx2 = createServerContext({ integrations: registry2 });
|
|
447
|
-
const app2 = express();
|
|
448
|
-
app2.use(express.json());
|
|
449
|
-
app2.use((req: Request, _res: Response, next: NextFunction) => {
|
|
450
|
-
(req as unknown as Record<string, unknown>).tboxAuthContext = reqCtxOf();
|
|
451
|
-
next();
|
|
452
|
-
});
|
|
453
|
-
app2.use(createActivityPlayRouter(ctx2));
|
|
454
|
-
const okRes = await request(app2).post('/activities/demo:wheel/entry?mallId=mall-a').send({});
|
|
455
|
-
expect(okRes.status).toBe(200);
|
|
456
|
-
expect(okRes.body.data.target.pluginAlias).toBe('aPlay');
|
|
457
|
-
});
|
|
458
|
-
});
|