@tbox.cn/app-provider-mock 0.6.7 → 0.7.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.
- package/dist/server/index.js +15 -8
- package/package.json +5 -5
- package/src/server/index.ts +28 -13
- package/tests/integrations-e2e.test.ts +64 -2
- package/tests/mock-impl.test.ts +1 -1
package/dist/server/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/server/index.ts
|
|
2
2
|
import { randomBytes } from "crypto";
|
|
3
|
-
import { readLoginMallId } from "@tbox.cn/app-contracts-mall";
|
|
3
|
+
import { readLoginMallId, readScopeDefaultMallId } from "@tbox.cn/app-contracts-mall";
|
|
4
4
|
|
|
5
5
|
// src/server/member.ts
|
|
6
6
|
var CFG = {
|
|
@@ -693,7 +693,7 @@ var shoppingFactory = (input) => {
|
|
|
693
693
|
return new MockShoppingServiceImpl(void 0);
|
|
694
694
|
};
|
|
695
695
|
var mallInfoFactory = (input) => mockMallInfoFactory(input);
|
|
696
|
-
function createMockAlipayLoginExchange(
|
|
696
|
+
function createMockAlipayLoginExchange(deps) {
|
|
697
697
|
return {
|
|
698
698
|
provider: "alipay",
|
|
699
699
|
/** 裸/demo 域默认开放 guest 登录(browser 轴 auto 档 → guest 签发) */
|
|
@@ -701,14 +701,17 @@ function createMockAlipayLoginExchange(integrations) {
|
|
|
701
701
|
return true;
|
|
702
702
|
},
|
|
703
703
|
matches(context) {
|
|
704
|
-
const mallId = readLoginMallId(context);
|
|
704
|
+
const mallId = readLoginMallId(context) ?? deps.defaultMallId();
|
|
705
705
|
if (!mallId) return true;
|
|
706
|
-
if (!integrations) return false;
|
|
707
|
-
return integrations.hasProviderBinding("member.account", { mallId }, MOCK_PROVIDER, MOCK_MEMBER_IMPL);
|
|
706
|
+
if (!deps.integrations) return false;
|
|
707
|
+
return deps.integrations.hasProviderBinding("member.account", { mallId }, MOCK_PROVIDER, MOCK_MEMBER_IMPL);
|
|
708
708
|
},
|
|
709
709
|
async exchangeByCode({ context, previousIdentity }) {
|
|
710
|
-
|
|
711
|
-
|
|
710
|
+
const mallId = readLoginMallId(context) ?? deps.defaultMallId();
|
|
711
|
+
if (!mallId) {
|
|
712
|
+
throw new Error(
|
|
713
|
+
"AUTH_SCOPE_REQUIRED: \u672A\u9009\u62E9\u5546\u573A\uFF08\u767B\u5F55 context \u65E0 mallId \u4E14\u65E0\u9ED8\u8BA4\u5546\u5708\u515C\u5E95\u2014\u2014\u5165\u53E3 URL \u5E26 ?mallId=\uFF0C\u6216 integrations.json \u914D\u7F6E defaultInstanceId\uFF09"
|
|
714
|
+
);
|
|
712
715
|
}
|
|
713
716
|
const userId = previousIdentity?.source === "guest" ? previousIdentity.userId : `guest_${randomBytes(16).toString("hex")}`;
|
|
714
717
|
if (isMockMemberEnrolled(userId) && !isMockMemberLoggedOut(userId)) {
|
|
@@ -747,7 +750,11 @@ var serverModule = {
|
|
|
747
750
|
ctx.integrations?.registerAdapter(MOCK_PROVIDER, MOCK_PROMOTION_IMPL, promotionFactory);
|
|
748
751
|
ctx.integrations?.registerAdapter(MOCK_PROVIDER, MOCK_SHOPPING_IMPL, shoppingFactory);
|
|
749
752
|
ctx.integrations?.registerAdapter(MOCK_MALL_INFO_PROVIDER, MOCK_MALL_INFO_IMPL, mallInfoFactory);
|
|
750
|
-
ctx.authExchanges.register(createMockAlipayLoginExchange(
|
|
753
|
+
ctx.authExchanges.register(createMockAlipayLoginExchange({
|
|
754
|
+
integrations: ctx.integrations,
|
|
755
|
+
// 登录期求值(域装配 register 晚于本包仍生效;零拓扑 mock 档经隐式声明实例派生 mall-demo)
|
|
756
|
+
defaultMallId: () => readScopeDefaultMallId(ctx)
|
|
757
|
+
}));
|
|
751
758
|
ctx.routes.register("provider-mock", createMockEnrollRouter());
|
|
752
759
|
}
|
|
753
760
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbox.cn/app-provider-mock",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "商圈 Mock Provider 包(极简零配置):18 槽 local 供给开箱即用(含 auth.alipay-login 标签供给——登录换码路由仍走 member.account 绑定);免传输/凭据/实例配置/集成配置;config 消费与配置验证面见 @tbox.cn/app-provider-mocktest。",
|
|
6
6
|
"exports": {
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@tbox.cn/app-contracts": "0.
|
|
18
|
-
"@tbox.cn/app-contracts-mall": "0.
|
|
19
|
-
"@tbox.cn/app-agent-sdk-client": "0.
|
|
20
|
-
"@tbox.cn/app-agent-sdk-server": "0.
|
|
17
|
+
"@tbox.cn/app-contracts": "0.13.1",
|
|
18
|
+
"@tbox.cn/app-contracts-mall": "0.13.1",
|
|
19
|
+
"@tbox.cn/app-agent-sdk-client": "0.2.0",
|
|
20
|
+
"@tbox.cn/app-agent-sdk-server": "0.3.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/express": "4.17.25",
|
package/src/server/index.ts
CHANGED
|
@@ -32,7 +32,7 @@ import { randomBytes } from 'node:crypto';
|
|
|
32
32
|
import type { ServerContext, ServerModule } from '@tbox.cn/app-agent-sdk-server';
|
|
33
33
|
import type { ServiceAdapterFactory } from '@tbox.cn/app-agent-sdk-server';
|
|
34
34
|
import type { ExchangeResult } from '@tbox.cn/app-contracts';
|
|
35
|
-
import { readLoginMallId } from '@tbox.cn/app-contracts-mall';
|
|
35
|
+
import { readLoginMallId, readScopeDefaultMallId } from '@tbox.cn/app-contracts-mall';
|
|
36
36
|
import {
|
|
37
37
|
MockMemberServiceImpl,
|
|
38
38
|
isMockMemberEnrolled,
|
|
@@ -81,15 +81,21 @@ const mallInfoFactory: ServiceAdapterFactory = (input) => mockMallInfoFactory(in
|
|
|
81
81
|
* matches 借 member.account 绑定路由(017 槽位原子化:换码器产出登录态身份 → account 槽
|
|
82
82
|
* 为语义锚;mock 档无独立 auth 槽;无条件兜底位归部署装配的自定义换码器,本候选恒
|
|
83
83
|
* scoped 层 first-match)。authCode 忽略(mock 语义:单 demo 用户/部署,无真实授权码可换)。
|
|
84
|
-
*
|
|
85
|
-
*
|
|
84
|
+
* 统一范式(2026-10-12 修订):context 无 mallId **且无默认商圈兜底**时命中本候选,
|
|
85
|
+
* exchangeByCode 显性抛 AUTH_SCOPE_REQUIRED(503),不落内置本地 alipay(缺 env 报错会
|
|
86
|
+
* 误导部署);有默认商圈(零拓扑 mock 档经隐式声明实例派生 mall-demo,或显式
|
|
87
|
+
* defaultInstanceId)时按默认商圈换发——与 WS 轴 defaultContext 兜底同源同值,三轴归一。
|
|
86
88
|
*
|
|
87
89
|
* 随机访客身份(统一登录改造 T17):mock 档壳内登录恒服务端随机(guest_<32hex>,
|
|
88
90
|
* source 'guest')——固定 demo-user 共享身份退役。会话连续性(enroll 后 MEMBER 形态、
|
|
89
91
|
* 记忆归属)由 previousIdentity 复用轨保证:客户端 previousToken 上送 → previousIdentity
|
|
90
92
|
* source=guest 时同 sub 续签(真实厂商忽略,mock 换码器是唯一复用消费方)。
|
|
91
93
|
*/
|
|
92
|
-
function createMockAlipayLoginExchange(
|
|
94
|
+
function createMockAlipayLoginExchange(deps: {
|
|
95
|
+
integrations: ServerContext['integrations'];
|
|
96
|
+
/** 默认商圈兜底(登录期求值——scopeDefaults 读取视图经域装配;无域装配/显式 null → undefined) */
|
|
97
|
+
defaultMallId: () => string | undefined;
|
|
98
|
+
}) {
|
|
93
99
|
return {
|
|
94
100
|
provider: 'alipay' as const,
|
|
95
101
|
/** 裸/demo 域默认开放 guest 登录(browser 轴 auto 档 → guest 签发) */
|
|
@@ -97,20 +103,25 @@ function createMockAlipayLoginExchange(integrations: ServerContext['integrations
|
|
|
97
103
|
return true;
|
|
98
104
|
},
|
|
99
105
|
matches(context?: Readonly<Record<string, unknown>>) {
|
|
100
|
-
const mallId = readLoginMallId(context);
|
|
101
|
-
//
|
|
102
|
-
//
|
|
106
|
+
const mallId = readLoginMallId(context) ?? deps.defaultMallId();
|
|
107
|
+
// 域缺失(context 无 mallId 且无默认商圈兜底)→ 命中本换码器,由 exchangeByCode 显性抛
|
|
108
|
+
// AUTH_SCOPE_REQUIRED(503)——不落内置本地 alipay(缺 env 报错误导部署)。
|
|
103
109
|
// 仅当 mallId 在场但不在 mock 绑定时才 return false(继续 fallback,多厂商并存语义)。
|
|
104
110
|
if (!mallId) return true;
|
|
105
|
-
if (!integrations) return false;
|
|
106
|
-
return integrations.hasProviderBinding('member.account', { mallId }, MOCK_PROVIDER, MOCK_MEMBER_IMPL);
|
|
111
|
+
if (!deps.integrations) return false;
|
|
112
|
+
return deps.integrations.hasProviderBinding('member.account', { mallId }, MOCK_PROVIDER, MOCK_MEMBER_IMPL);
|
|
107
113
|
},
|
|
108
114
|
async exchangeByCode(
|
|
109
115
|
{ context, previousIdentity }: { context?: Readonly<Record<string, unknown>>; previousIdentity?: { userId: string; source: string } },
|
|
110
116
|
): Promise<ExchangeResult> {
|
|
111
|
-
//
|
|
112
|
-
|
|
113
|
-
|
|
117
|
+
// 域缺失前置校验(与 matches 语义一致:域缺失显性报错,不产出 demo 身份;
|
|
118
|
+
// 错误码族 2026-10-12:域缺失独立于配置缺失——修复动作 = 带 mallId/配 defaultInstanceId)
|
|
119
|
+
const mallId = readLoginMallId(context) ?? deps.defaultMallId();
|
|
120
|
+
if (!mallId) {
|
|
121
|
+
throw new Error(
|
|
122
|
+
'AUTH_SCOPE_REQUIRED: 未选择商场(登录 context 无 mallId 且无默认商圈兜底'
|
|
123
|
+
+ '——入口 URL 带 ?mallId=,或 integrations.json 配置 defaultInstanceId)',
|
|
124
|
+
);
|
|
114
125
|
}
|
|
115
126
|
// 随机访客身份(previousToken 复用轨:guest 同 sub 续签)
|
|
116
127
|
const userId = previousIdentity?.source === 'guest'
|
|
@@ -164,7 +175,11 @@ export const serverModule = {
|
|
|
164
175
|
ctx.integrations?.registerAdapter(MOCK_PROVIDER, MOCK_SHOPPING_IMPL, shoppingFactory);
|
|
165
176
|
ctx.integrations?.registerAdapter(MOCK_MALL_INFO_PROVIDER, MOCK_MALL_INFO_IMPL, mallInfoFactory);
|
|
166
177
|
// scoped mock 换码器(member.account 绑 mock 的部署命中;其他部署不路由本包)
|
|
167
|
-
ctx.authExchanges.register(createMockAlipayLoginExchange(
|
|
178
|
+
ctx.authExchanges.register(createMockAlipayLoginExchange({
|
|
179
|
+
integrations: ctx.integrations,
|
|
180
|
+
// 登录期求值(域装配 register 晚于本包仍生效;零拓扑 mock 档经隐式声明实例派生 mall-demo)
|
|
181
|
+
defaultMallId: () => readScopeDefaultMallId(ctx),
|
|
182
|
+
}));
|
|
168
183
|
// mock 入会落地路由(POST /api/m/provider-mock/demo/member-enroll)
|
|
169
184
|
ctx.routes.register('provider-mock', createMockEnrollRouter());
|
|
170
185
|
},
|
|
@@ -69,6 +69,26 @@ function fixtureCtx(integrationsOverride?: IntegrationsConfig, ownerOf?: Record<
|
|
|
69
69
|
return { ctx, registry };
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/** Fix A 兜底用 fixture:scopeDefaults 静态注入(无域装配时 ctx.scopeDefaults 读取视图的静态回落面) */
|
|
73
|
+
function fixtureCtxWithDefaults(scopeDefaults: Record<string, unknown>, integrationsOverride?: IntegrationsConfig) {
|
|
74
|
+
const registry = createIntegrationsRegistry({
|
|
75
|
+
integrations: integrationsOverride ?? integrations,
|
|
76
|
+
catalog,
|
|
77
|
+
authStrategies: createAuthStrategyRegistry(),
|
|
78
|
+
secretProvider: createEnvironmentSecretProvider({ source: {}, production: false }),
|
|
79
|
+
instanceIdOf: (scope) => scope.mallId ?? '',
|
|
80
|
+
});
|
|
81
|
+
const ctx = createServerContext({ integrations: registry, scopeDefaults });
|
|
82
|
+
serverModule.register(ctx);
|
|
83
|
+
return { ctx, registry };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 零拓扑形态(CLI ensureMockBindings 生成物对齐:root.provider 继承 + 空槽对象 = 通配激活) */
|
|
87
|
+
const zeroTopology = {
|
|
88
|
+
provider: 'mock',
|
|
89
|
+
services: Object.fromEntries(Object.keys(integrations.services).map((k) => [k, {}])),
|
|
90
|
+
} as IntegrationsConfig;
|
|
91
|
+
|
|
72
92
|
describe('F2 端到端:provider-mock 经槽位轴解析', () => {
|
|
73
93
|
it('register(经 ctx.integrations)→ resolveService 三槽产出 mock 实现', async () => {
|
|
74
94
|
const { ctx, registry } = fixtureCtx();
|
|
@@ -167,10 +187,10 @@ describe('C5:scoped mock 换码器(入会闭环身份供给)', () => {
|
|
|
167
187
|
expect(ctx.authExchanges.get('alipay', { mall: { mallId: 'mall-other' } })).toBeUndefined();
|
|
168
188
|
});
|
|
169
189
|
|
|
170
|
-
it('
|
|
190
|
+
it('域缺失(无 mallId 无默认商圈)→ exchangeByCode 抛 AUTH_SCOPE_REQUIRED 未选择商场', async () => {
|
|
171
191
|
const { ctx } = fixtureCtx();
|
|
172
192
|
const exchanger = ctx.authExchanges.get('alipay')!;
|
|
173
|
-
await expect(exchanger.exchangeByCode({ code: 'any-authcode' })).rejects.toThrow(
|
|
193
|
+
await expect(exchanger.exchangeByCode({ code: 'any-authcode' })).rejects.toThrow(/AUTH_SCOPE_REQUIRED: 未选择商场/);
|
|
174
194
|
});
|
|
175
195
|
|
|
176
196
|
it('随机访客身份 + GUEST 形态(T17):guest_<32hex> 每次新随机;token="" + openid 键控', async () => {
|
|
@@ -239,6 +259,48 @@ describe('C5:scoped mock 换码器(入会闭环身份供给)', () => {
|
|
|
239
259
|
});
|
|
240
260
|
});
|
|
241
261
|
|
|
262
|
+
// ── 默认商圈兜底(Fix A,2026-10-12):mock 换码器对齐 scoped 厂商换码器既有 defaultMallId 兜底形态 ──
|
|
263
|
+
|
|
264
|
+
describe('C5b:默认商圈兜底(Fix A)', () => {
|
|
265
|
+
beforeEach(() => __resetMockMemberState());
|
|
266
|
+
|
|
267
|
+
it('无 context + 默认商圈 mall-demo → matches 命中 + GUEST 形态(显式实例档)', async () => {
|
|
268
|
+
const { ctx } = fixtureCtxWithDefaults({ defaultMallId: 'mall-demo' });
|
|
269
|
+
const exchanger = ctx.authExchanges.get('alipay');
|
|
270
|
+
expect(exchanger?.provider).toBe('alipay');
|
|
271
|
+
const r = await exchanger!.exchangeByCode({ code: 'c1' });
|
|
272
|
+
expect(r.identity.source).toBe('guest');
|
|
273
|
+
expect(r.identity.userId).toMatch(/^guest_[0-9a-f]{32}$/);
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
it('零拓扑变体(CLI 生成物形态):root.provider 继承 + 空槽对象 → 无 context 换码 GUEST', async () => {
|
|
277
|
+
const { ctx } = fixtureCtxWithDefaults({ defaultMallId: 'mall-demo' }, zeroTopology);
|
|
278
|
+
const exchanger = ctx.authExchanges.get('alipay');
|
|
279
|
+
expect(exchanger?.provider).toBe('alipay'); // matches 经通配激活命中 mock 绑定
|
|
280
|
+
const r = await exchanger!.exchangeByCode({ code: 'c1' });
|
|
281
|
+
expect(r.identity.source).toBe('guest');
|
|
282
|
+
expect(r.identity.userId).toMatch(/^guest_[0-9a-f]{32}$/);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it('默认商圈绑他牌(member.account 非 mock)→ matches false 落穿后续候选', () => {
|
|
286
|
+
const { ctx } = fixtureCtxWithDefaults({ defaultMallId: 'mall-other' });
|
|
287
|
+
// mall-other 无 member.account 绑定(fixture 仅 mall-demo)→ scoped 不命中
|
|
288
|
+
expect(ctx.authExchanges.get('alipay')).toBeUndefined();
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
it('无默认(scopeDefaults 空)→ matches 命中 + exchangeByCode 抛 AUTH_SCOPE_REQUIRED(显性报错保留)', async () => {
|
|
292
|
+
const { ctx } = fixtureCtxWithDefaults({});
|
|
293
|
+
const exchanger = ctx.authExchanges.get('alipay')!;
|
|
294
|
+
expect(exchanger.provider).toBe('alipay');
|
|
295
|
+
await expect(exchanger.exchangeByCode({ code: 'c1' })).rejects.toThrow(/AUTH_SCOPE_REQUIRED: 未选择商场/);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('兜底不吞显式参数:context mallId 不在 mock 绑定 → scoped 落穿(多厂商并存语义不回归)', () => {
|
|
299
|
+
const { ctx } = fixtureCtxWithDefaults({ defaultMallId: 'mall-demo' });
|
|
300
|
+
expect(ctx.authExchanges.get('alipay', { mall: { mallId: 'mall-other' } })).toBeUndefined();
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
|
|
242
304
|
|
|
243
305
|
// ── 隐式默认单实例声明(零拓扑兜底——provider-mock declareProviderInstances) ──
|
|
244
306
|
|
package/tests/mock-impl.test.ts
CHANGED
|
@@ -143,7 +143,7 @@ describe('provider-mock:mock 实现 Port 面与厂商中立', () => {
|
|
|
143
143
|
});
|
|
144
144
|
|
|
145
145
|
describe('provider-mock:serverModule 形态(无卡模块 + adapter 注册)', () => {
|
|
146
|
-
it('cards 空 map + register
|
|
146
|
+
it('cards 空 map + register 注册六 adapter/换码器/路由(真实 ServerContext 面)', () => {
|
|
147
147
|
expect(Object.keys(serverModule.cards)).toHaveLength(0);
|
|
148
148
|
expect(serverModule.declaration.moduleId).toBe('provider-mock');
|
|
149
149
|
const registered: Array<[string, string, unknown]> = [];
|