@tbox.cn/app-provider-mock 0.3.0 → 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.
@@ -190,10 +190,11 @@ declare const mockMallInfoFactory: ServiceAdapterFactory;
190
190
  * Mock 入会落地点路由(入会体验闭环 C5):POST /api/m/provider-mock/demo/member-enroll。
191
191
  *
192
192
  * 容器 mock-enrollment 页(dev 脚手架)与 curl 联调共用:
193
- * - body `{userId?}`:缺省 MOCK_SHELL_USER_ID(mock 页无参——与真实 gate 无参协议面一致;
194
- * curl 可定向翻 dev_h5 等浏览器 dev 用户造会员态调试)。
193
+ * - body `{userId?}`:缺省 = 调用方 token 身份(统一登录改造 T11 收口——固定 demo-user
194
+ * 缺省退役,IDOR 写任意用户面收窄为「已鉴权调用方声明 demo 主体」;gate 页经 query
195
+ * 透传 H5 身份,curl 可定向翻指定访客造会员态调试)。
195
196
  * - 幂等(Set.add / 余额保守写);NODE_ENV=production → 503(provider-mock 不进生产装配,双保险)。
196
- * - 经全局 /api 鉴权(AUTH_MODE 缺省 required)——调用方须携 dev-login 令牌;
197
+ * - 经全局 /api 鉴权(AUTH_MODE 缺省 required)——调用方须携登录令牌;
197
198
  * 匿名写显式拒绝(不随 AUTH_MODE 放宽)。
198
199
  * - doctor naked-write-route:/demo 前缀在白名单内(webhook/upload/demo/run)。
199
200
  *
@@ -202,36 +203,6 @@ declare const mockMallInfoFactory: ServiceAdapterFactory;
202
203
 
203
204
  declare function createMockEnrollRouter(): Router;
204
205
 
205
- /**
206
- * Mock Provider 包服务端入口(provider 拆包 F1b)。
207
- *
208
- * 17 槽 local 供给(member×5 / parking×3 / promotion×4 / guide×4 + mall.info;manifest
209
- * contributes.providers.slots,local:true):F2 模块 ports
210
- * slot 一行化后经 createIntegrationsRegistry.resolveService 解析——local 短路免传输/
211
- * 凭据/实例配置。实现自持(R3-1:provider 不得依赖业务模块——workspace-dag 铁律),
212
- * demo ref 恒 `demo:` 前缀,厂商中立。
213
- *
214
- * F1b 阶段形态:adapter 工厂注册经 ctx.integrations(F2 模板装配 createIntegrationsRegistry
215
- * 接线后生效);F1b 前模块仍走探测链,本包装配无行为影响。shopping-guide 槽四键形状
216
- * (catalog 空 + shop/category/directory 纯 mock 数据);assistance/navigation 本地知识库域
217
- * 归模块本地 adapter,不属 provider 供给面。
218
- *
219
- * config 消费:五域工厂透传 input.config → 各 impl 构造期 fail-soft 快照
220
- * (src/server/config.ts;键集/缺省双锁 tests/config-schema.test.ts);零配置 = 零行为变化。
221
- *
222
- * 隐式默认单实例(零拓扑部署兜底):register 期经 ctx.integrations.declareProviderInstances
223
- * 声明 mall-demo——显式 instances/defaultInstanceId 恒赢(融合规则见 contracts-mall
224
- * mallControlPlaneOf);值与 .real-env mock 档演示语义一致(星河 Mock 小镇 / mock-demo-app)。
225
- *
226
- * 入会体验闭环(C5)追加两件:
227
- * - scoped mock 换码器(ctx.authExchanges):固定 demo-user、GUEST/MEMBER 双形态对齐
228
- * 真实厂商换码协议(潜客 token=''+openid ↔ 会员 token+memberId)——mock 档冷登走与
229
- * 真实档同一条换码链,H5 侧代码零分叉。刻意不实现 devExternalCredentials:dev-login
230
- * 走协议 fail-soft 无 claim 签发(login-routes 三层解析能力口缺席即跳过)。
231
- * - mock 入会落地路由(ctx.routes):POST /demo/member-enroll(容器 mock-enrollment 页
232
- * 回调 + curl 联调)。
233
- */
234
-
235
206
  declare const MOCK_PROVIDER = "mock";
236
207
  declare const MOCK_MEMBER_IMPL = "mock-member@1";
237
208
  declare const MOCK_PARKING_IMPL = "mock-parking@1";
@@ -1,4 +1,5 @@
1
1
  // src/server/index.ts
2
+ import { randomBytes } from "crypto";
2
3
  import { readLoginMallId } from "@tbox.cn/app-contracts-mall";
3
4
 
4
5
  // src/server/config.ts
@@ -646,7 +647,7 @@ function resolveMockEnroll(input) {
646
647
  return { status: 401, body: { error: "unauthorized" } };
647
648
  }
648
649
  const body = input.body ?? {};
649
- const userId = typeof body.userId === "string" && body.userId.trim() ? body.userId.trim() : MOCK_SHELL_USER_ID;
650
+ const userId = typeof body.userId === "string" && body.userId.trim() ? body.userId.trim() : input.identity.userId ?? `guest_${Date.now()}`;
650
651
  enrollMockMember(userId);
651
652
  return { status: 200, body: { state: "active", memberRef: `demo:member:${userId}` } };
652
653
  }
@@ -658,7 +659,7 @@ function resolveMockLogout(input) {
658
659
  return { status: 401, body: { error: "unauthorized" } };
659
660
  }
660
661
  const body = input.body ?? {};
661
- const userId = typeof body.userId === "string" && body.userId.trim() ? body.userId.trim() : MOCK_SHELL_USER_ID;
662
+ const userId = typeof body.userId === "string" && body.userId.trim() ? body.userId.trim() : input.identity.userId ?? `guest_${Date.now()}`;
662
663
  logoutMockMember(userId);
663
664
  return { status: 200, body: { state: "logged_out" } };
664
665
  }
@@ -696,20 +697,24 @@ var mallInfoFactory = (input) => mockMallInfoFactory(input);
696
697
  function createMockAlipayLoginExchange(integrations) {
697
698
  return {
698
699
  provider: "alipay",
700
+ /** 裸/demo 域默认开放 guest 登录(browser 轴 auto 档 → guest 签发) */
701
+ allowsGuestLogin() {
702
+ return true;
703
+ },
699
704
  matches(context) {
700
705
  const mallId = readLoginMallId(context);
701
706
  if (!mallId) return true;
702
707
  if (!integrations) return false;
703
708
  return integrations.hasProviderBinding("member.account", { mallId }, MOCK_PROVIDER, MOCK_MEMBER_IMPL);
704
709
  },
705
- async exchangeByCode(_code, _platform, context) {
710
+ async exchangeByCode({ context, previousIdentity }) {
706
711
  if (!readLoginMallId(context)) {
707
712
  throw new Error("AUTH_NOT_CONFIGURED: \u672A\u9009\u62E9\u5546\u573A\uFF08\u767B\u5F55 context \u65E0 mallId\uFF09");
708
713
  }
709
- const userId = MOCK_SHELL_USER_ID;
714
+ const userId = previousIdentity?.source === "guest" ? previousIdentity.userId : `guest_${randomBytes(16).toString("hex")}`;
710
715
  if (isMockMemberEnrolled(userId) && !isMockMemberLoggedOut(userId)) {
711
716
  return {
712
- identity: { userId, source: "external" },
717
+ identity: { userId, source: "guest" },
713
718
  credentials: {
714
719
  token: `demo:token:${userId}`,
715
720
  provider: "mock",
@@ -718,7 +723,7 @@ function createMockAlipayLoginExchange(integrations) {
718
723
  };
719
724
  }
720
725
  return {
721
- identity: { userId, source: "external" },
726
+ identity: { userId, source: "guest" },
722
727
  credentials: {
723
728
  token: "",
724
729
  provider: "mock",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tbox.cn/app-provider-mock",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "description": "商圈 Mock Provider 包(17 槽全 local:true:member×5 / parking×3 / promotion×4 / guide×4 + mall.info):模板缺省开箱演示——免传输/凭据/实例配置,裸跑即完整 mock 演示;全槽 config schema(schemas/×5 域共享)+ 内存 config 消费(改配置重启即变)。",
6
6
  "exports": {
@@ -14,9 +14,9 @@
14
14
  }
15
15
  },
16
16
  "dependencies": {
17
- "@tbox.cn/app-sdk": "0.19.0",
18
- "@tbox.cn/app-contracts": "0.10.0",
19
- "@tbox.cn/app-contracts-mall": "0.10.0"
17
+ "@tbox.cn/app-sdk": "0.20.0",
18
+ "@tbox.cn/app-contracts": "0.11.0",
19
+ "@tbox.cn/app-contracts-mall": "0.11.0"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/express": "4.17.25",
@@ -3,7 +3,7 @@
3
3
  "title": "Mock 商场基础信息实例配置",
4
4
  "type": "object",
5
5
  "properties": {
6
- "name": { "type": "string", "title": "商场名称(空串 = 动态别名「示例商场(mallId)」回退)", "default": "" },
6
+ "name": { "type": "string", "title": "商场名称", "description": "空串 = 动态别名「示例商场(mallId)」回退", "default": "" },
7
7
  "address": { "type": "string", "title": "商场地址", "default": "示例商场地址(demo)" },
8
8
  "latitude": { "type": "number", "minimum": -90, "maximum": 90, "title": "纬度(分式合法)", "default": 30 },
9
9
  "longitude": { "type": "number", "minimum": -180, "maximum": 180, "title": "经度(分式合法)", "default": 120 }
@@ -3,11 +3,11 @@
3
3
  "title": "Mock 会员域实例配置",
4
4
  "type": "object",
5
5
  "properties": {
6
- "memberName": { "type": "string", "title": "会员名称(资料标签展示)", "default": "示例会员" },
7
- "cardName": { "type": "string", "title": "会员卡名称(卡面 + 积分标签)", "default": "示例会员卡" },
8
- "levelName": { "type": "string", "title": "等级名称(空串 = 不展示等级区)", "default": "" },
9
- "initialPoints": { "type": "integer", "minimum": 0, "title": "初始积分(入会后余额;流水金额同源派生)", "default": 300 },
10
- "enrollUrl": { "type": "string", "title": "入会落地页深链(alipays:// 或 H5)", "default": "alipays://platformapi/startapp?appId=demo&page=v/index/index" },
11
- "loginUrl": { "type": "string", "title": "登录页深链(alipays:// 或 H5)", "default": "alipays://platformapi/startapp?appId=demo&page=v/login/index" }
6
+ "memberName": { "type": "string", "title": "会员名称", "description": "资料标签展示", "default": "示例会员" },
7
+ "cardName": { "type": "string", "title": "会员卡名称", "description": "卡面与积分标签", "default": "示例会员卡" },
8
+ "levelName": { "type": "string", "title": "等级名称", "description": "空串 = 不展示等级区", "default": "" },
9
+ "initialPoints": { "type": "integer", "minimum": 0, "title": "初始积分", "description": "入会后余额;流水金额同源派生", "default": 300 },
10
+ "enrollUrl": { "type": "string", "title": "入会落地页深链", "description": "alipays:// 或 H5", "default": "alipays://platformapi/startapp?appId=demo&page=v/index/index" },
11
+ "loginUrl": { "type": "string", "title": "登录页深链", "description": "alipays:// 或 H5", "default": "alipays://platformapi/startapp?appId=demo&page=v/login/index" }
12
12
  }
13
13
  }
@@ -3,15 +3,15 @@
3
3
  "title": "Mock 停车域实例配置",
4
4
  "type": "object",
5
5
  "properties": {
6
- "parkName": { "type": "string", "title": "停车场名称(信息卡标题 + 缴费记录)", "default": "示例停车场" },
6
+ "parkName": { "type": "string", "title": "停车场名称", "description": "信息卡标题 + 缴费记录", "default": "示例停车场" },
7
7
  "feeName": { "type": "string", "title": "费用项名称", "default": "停车费(3 小时)" },
8
8
  "feeCents": { "type": "integer", "minimum": 0, "title": "停车费(分)", "default": 1500 },
9
9
  "remainingSpaces": { "type": "integer", "minimum": 0, "title": "剩余车位", "default": 128 },
10
10
  "firstHourRate": { "type": "string", "title": "首小时费率文案", "default": "免费" },
11
- "memberBenefitEnabled": { "type": "boolean", "title": "会员折扣权益开关(关闭 = 权益列表少一项 + reprice 不变)", "default": true },
11
+ "memberBenefitEnabled": { "type": "boolean", "title": "会员折扣权益开关", "description": "关闭 = 权益列表少一项 + reprice 不变", "default": true },
12
12
  "memberBenefitDiscountCents": { "type": "integer", "minimum": 0, "title": "会员折扣权益预估减免(分)", "default": 75 },
13
- "pointsBenefitEnabled": { "type": "boolean", "title": "积分抵扣权益开关", "default": true },
14
- "invoiceEnabled": { "type": "boolean", "title": "开票入口开关(关闭 = 缴费卡无开票按钮)", "default": true },
13
+ "pointsBenefitEnabled": { "type": "boolean", "title": "积分抵扣权益开关", "description": "关闭 = 权益列表少「积分抵 5 元」项(积分入口)", "default": true },
14
+ "invoiceEnabled": { "type": "boolean", "title": "开票入口开关", "description": "关闭 = 缴费卡无开票按钮", "default": true },
15
15
  "vehicleFloor": { "type": "string", "title": "寻车楼层", "default": "B1" },
16
16
  "vehicleZone": { "type": "string", "title": "寻车区域", "default": "A 区" },
17
17
  "vehicleSpaceNo": { "type": "string", "title": "寻车车位号", "default": "A-128" }
@@ -3,11 +3,11 @@
3
3
  "title": "Mock 营销域实例配置",
4
4
  "type": "object",
5
5
  "properties": {
6
- "couponTitle": { "type": "string", "title": "券标题(列表/详情/资产同源)", "default": "满 100 减 20 元券" },
6
+ "couponTitle": { "type": "string", "title": "券标题", "description": "列表/详情/资产同源", "default": "满 100 减 20 元券" },
7
7
  "couponValueCents": { "type": "integer", "minimum": 0, "title": "券面额(分)", "default": 2000 },
8
8
  "couponThresholdCents": { "type": "integer", "minimum": 0, "title": "券门槛(分)", "default": 10000 },
9
9
  "couponValidityDays": { "type": "integer", "minimum": 1, "maximum": 365, "title": "券有效期(天)", "default": 30 },
10
- "grouponEnabled": { "type": "boolean", "title": "团购演示项开关(关闭 = 优惠列表仅券)", "default": true },
10
+ "grouponEnabled": { "type": "boolean", "title": "团购演示项开关", "description": "关闭 = 优惠列表仅券", "default": true },
11
11
  "activityTitle": { "type": "string", "title": "活动标题", "default": "示例商场周年庆" }
12
12
  }
13
13
  }
@@ -3,7 +3,7 @@
3
3
  "title": "Mock 导购域实例配置",
4
4
  "type": "object",
5
5
  "properties": {
6
- "extraShopName": { "type": "string", "title": "追加门店名(空串 = 不追加)", "default": "" },
6
+ "extraShopName": { "type": "string", "title": "追加门店名", "description": "空串 = 不追加", "default": "" },
7
7
  "extraShopCategory": { "type": "string", "title": "追加门店类目", "default": "餐饮" },
8
8
  "extraShopFloor": { "type": "string", "enum": ["B1", "1 楼", "2 楼", "3 楼", "4 楼"], "title": "追加门店楼层", "default": "3 楼" },
9
9
  "extraShopUnit": { "type": "string", "title": "追加门店铺位号", "default": "304" },
@@ -20,20 +20,21 @@
20
20
  * mallControlPlaneOf);值与 .real-env mock 档演示语义一致(星河 Mock 小镇 / mock-demo-app)。
21
21
  *
22
22
  * 入会体验闭环(C5)追加两件:
23
- * - scoped mock 换码器(ctx.authExchanges):固定 demo-user、GUEST/MEMBER 双形态对齐
24
- * 真实厂商换码协议(潜客 token=''+openid ↔ 会员 token+memberId)——mock 档冷登走与
25
- * 真实档同一条换码链,H5 侧代码零分叉。刻意不实现 devExternalCredentials:dev-login
26
- * 走协议 fail-soft 无 claim 签发(login-routes 三层解析能力口缺席即跳过)。
23
+ * - scoped mock 换码器(ctx.authExchanges):随机访客身份(guest_<32hex>,统一登录改造
24
+ * T17——固定 demo-user 共享身份退役)、GUEST/MEMBER 双形态对齐真实厂商换码协议
25
+ * (潜客 token=''+openid ↔ 会员 token+memberId)——mock 档冷登走与真实档同一条换码链,
26
+ * H5 侧代码零分叉。会话连续性由 previousIdentity 复用轨(previousToken source=guest
27
+ * 同 sub 续签)保证。
27
28
  * - mock 入会落地路由(ctx.routes):POST /demo/member-enroll(容器 mock-enrollment 页
28
29
  * 回调 + curl 联调)。
29
30
  */
31
+ import { randomBytes } from 'node:crypto';
30
32
  import type { ServerContext, ServerModule } from '@tbox.cn/app-sdk/server';
31
33
  import type { ServiceAdapterFactory } from '@tbox.cn/app-sdk/server';
32
34
  import type { ExchangeResult } from '@tbox.cn/app-contracts';
33
35
  import { readLoginMallId } from '@tbox.cn/app-contracts-mall';
34
36
  import {
35
37
  MockMemberServiceImpl,
36
- MOCK_SHELL_USER_ID,
37
38
  isMockMemberEnrolled,
38
39
  isMockMemberLoggedOut,
39
40
  } from './member';
@@ -81,11 +82,20 @@ const mallInfoFactory: ServiceAdapterFactory = (input) => mockMallInfoFactory(in
81
82
  * 为语义锚;mock 档无独立 auth 槽;无条件兜底位归部署装配的自定义换码器,本候选恒
82
83
  * scoped 层 first-match)。authCode 忽略(mock 语义:单 demo 用户/部署,无真实授权码可换)。
83
84
  * 统一范式:context 无 mallId(未选择商场)→ 命中本候选,exchangeByCode 显性抛错,
84
- * 不落内置本地 alipay(缺 env 报错误导部署)。
85
+ * 不落内置本地 alipay(缺 env 报错会误导部署)。
86
+ *
87
+ * 随机访客身份(统一登录改造 T17):mock 档壳内登录恒服务端随机(guest_<32hex>,
88
+ * source 'guest')——固定 demo-user 共享身份退役。会话连续性(enroll 后 MEMBER 形态、
89
+ * 记忆归属)由 previousIdentity 复用轨保证:客户端 previousToken 上送 → previousIdentity
90
+ * source=guest 时同 sub 续签(真实厂商忽略,mock 换码器是唯一复用消费方)。
85
91
  */
86
92
  function createMockAlipayLoginExchange(integrations: ServerContext['integrations']) {
87
93
  return {
88
94
  provider: 'alipay' as const,
95
+ /** 裸/demo 域默认开放 guest 登录(browser 轴 auto 档 → guest 签发) */
96
+ allowsGuestLogin() {
97
+ return true;
98
+ },
89
99
  matches(context?: Readonly<Record<string, unknown>>) {
90
100
  const mallId = readLoginMallId(context);
91
101
  // 未选择商场(context 无 mallId)→ 命中本换码器,由 exchangeByCode 显性抛
@@ -96,22 +106,23 @@ function createMockAlipayLoginExchange(integrations: ServerContext['integrations
96
106
  return integrations.hasProviderBinding('member.account', { mallId }, MOCK_PROVIDER, MOCK_MEMBER_IMPL);
97
107
  },
98
108
  async exchangeByCode(
99
- _code: string,
100
- _platform?: string,
101
- context?: Readonly<Record<string, unknown>>,
109
+ { context, previousIdentity }: { context?: Readonly<Record<string, unknown>>; previousIdentity?: { userId: string; source: string } },
102
110
  ): Promise<ExchangeResult> {
103
111
  // 未选择商场前置校验(与 matches 语义一致:域缺失显性报错,不产出 demo 身份)
104
112
  if (!readLoginMallId(context)) {
105
113
  throw new Error('AUTH_NOT_CONFIGURED: 未选择商场(登录 context 无 mallId)');
106
114
  }
107
- const userId = MOCK_SHELL_USER_ID;
115
+ // 随机访客身份(previousToken 复用轨:guest 同 sub 续签)
116
+ const userId = previousIdentity?.source === 'guest'
117
+ ? previousIdentity.userId
118
+ : `guest_${randomBytes(16).toString('hex')}`;
108
119
  // 会员形态需登录态健康:登出态(loggedOut)降级 GUEST 形态——对齐真实档 silentLogin 信封
109
120
  // 非 200 降级(2026-09-02 真机实测):登出后换码 = 潜客同构结果(token=''),H5 侧
110
121
  // not_enrolled → 入会引导卡 → 半屏 getLoginState 闭环恢复;enroll 清登出态后冷登回 MEMBER。
111
122
  if (isMockMemberEnrolled(userId) && !isMockMemberLoggedOut(userId)) {
112
123
  // MEMBER 形态(对齐真实档会员换码输出):token + memberId 随 attributes 流动
113
124
  return {
114
- identity: { userId, source: 'external' as const },
125
+ identity: { userId, source: 'guest' as const },
115
126
  credentials: {
116
127
  token: `demo:token:${userId}`,
117
128
  provider: 'mock',
@@ -122,7 +133,7 @@ function createMockAlipayLoginExchange(integrations: ServerContext['integrations
122
133
  // GUEST 形态(对齐真实档潜客分支):token='' + openid 身份——「合法登录未入会」;
123
134
  // 登出降级同走此构造(单源)。真实档另携 visitortoken,mock 链路无消费方故极简。
124
135
  return {
125
- identity: { userId, source: 'external' as const },
136
+ identity: { userId, source: 'guest' as const },
126
137
  credentials: {
127
138
  token: '',
128
139
  provider: 'mock',
@@ -2,10 +2,11 @@
2
2
  * Mock 入会落地点路由(入会体验闭环 C5):POST /api/m/provider-mock/demo/member-enroll。
3
3
  *
4
4
  * 容器 mock-enrollment 页(dev 脚手架)与 curl 联调共用:
5
- * - body `{userId?}`:缺省 MOCK_SHELL_USER_ID(mock 页无参——与真实 gate 无参协议面一致;
6
- * curl 可定向翻 dev_h5 等浏览器 dev 用户造会员态调试)。
5
+ * - body `{userId?}`:缺省 = 调用方 token 身份(统一登录改造 T11 收口——固定 demo-user
6
+ * 缺省退役,IDOR 写任意用户面收窄为「已鉴权调用方声明 demo 主体」;gate 页经 query
7
+ * 透传 H5 身份,curl 可定向翻指定访客造会员态调试)。
7
8
  * - 幂等(Set.add / 余额保守写);NODE_ENV=production → 503(provider-mock 不进生产装配,双保险)。
8
- * - 经全局 /api 鉴权(AUTH_MODE 缺省 required)——调用方须携 dev-login 令牌;
9
+ * - 经全局 /api 鉴权(AUTH_MODE 缺省 required)——调用方须携登录令牌;
9
10
  * 匿名写显式拒绝(不随 AUTH_MODE 放宽)。
10
11
  * - doctor naked-write-route:/demo 前缀在白名单内(webhook/upload/demo/run)。
11
12
  *
@@ -14,17 +15,17 @@
14
15
  import { Router } from 'express';
15
16
  import type { Request, Response } from 'express';
16
17
  import { getRequestContext } from '@tbox.cn/app-sdk/server';
17
- import { MOCK_SHELL_USER_ID, enrollMockMember, logoutMockMember } from './member';
18
+ import { enrollMockMember, logoutMockMember } from './member';
18
19
 
19
20
  export type MockEnrollOutcome =
20
21
  | { status: 200; body: { state: 'active'; memberRef: string } }
21
22
  | { status: 401; body: { error: string } }
22
23
  | { status: 503; body: { error: string } };
23
24
 
24
- /** 纯决策:production 拒 503 → 匿名/无身份拒 401 → 翻状态返 200(幂等) */
25
+ /** 纯决策:production 拒 503 → 匿名/无身份拒 401 → 翻状态返 200(幂等;主体缺省 = 调用方) */
25
26
  export function resolveMockEnroll(input: {
26
27
  body?: unknown;
27
- identity?: { source: string } | null;
28
+ identity?: { source: string; userId?: string } | null;
28
29
  nodeEnv?: string;
29
30
  }): MockEnrollOutcome {
30
31
  if ((input.nodeEnv ?? process.env.NODE_ENV) === 'production') {
@@ -36,7 +37,7 @@ export function resolveMockEnroll(input: {
36
37
  const body = (input.body ?? {}) as { userId?: unknown };
37
38
  const userId = typeof body.userId === 'string' && body.userId.trim()
38
39
  ? body.userId.trim()
39
- : MOCK_SHELL_USER_ID;
40
+ : (input.identity.userId ?? `guest_${Date.now()}`);
40
41
  enrollMockMember(userId);
41
42
  return { status: 200, body: { state: 'active', memberRef: `demo:member:${userId}` } };
42
43
  }
@@ -46,10 +47,10 @@ export type MockLogoutOutcome =
46
47
  | { status: 401; body: { error: string } }
47
48
  | { status: 503; body: { error: string } };
48
49
 
49
- /** 纯决策:production 拒 503 → 匿名/无身份拒 401 → 翻登出态返 200(与 enroll 同形) */
50
+ /** 纯决策:production 拒 503 → 匿名/无身份拒 401 → 翻登出态返 200(与 enroll 同形;主体缺省 = 调用方) */
50
51
  export function resolveMockLogout(input: {
51
52
  body?: unknown;
52
- identity?: { source: string } | null;
53
+ identity?: { source: string; userId?: string } | null;
53
54
  nodeEnv?: string;
54
55
  }): MockLogoutOutcome {
55
56
  if ((input.nodeEnv ?? process.env.NODE_ENV) === 'production') {
@@ -61,7 +62,7 @@ export function resolveMockLogout(input: {
61
62
  const body = (input.body ?? {}) as { userId?: unknown };
62
63
  const userId = typeof body.userId === 'string' && body.userId.trim()
63
64
  ? body.userId.trim()
64
- : MOCK_SHELL_USER_ID;
65
+ : (input.identity.userId ?? `guest_${Date.now()}`);
65
66
  logoutMockMember(userId);
66
67
  return { status: 200, body: { state: 'logged_out' } };
67
68
  }
package/tbox.module.json CHANGED
@@ -1,6 +1,8 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
3
  "name": "provider-mock",
4
+ "title": "Mock",
5
+ "description": "全槽本地演示供给:免真实厂商接入的完整商圈演示",
4
6
  "version": "0.1.0",
5
7
  "kind": "business",
6
8
  "risk": {
@@ -6,7 +6,7 @@
6
6
  */
7
7
  import { beforeEach, describe, expect, it } from 'vitest';
8
8
  import { createIntegrationsRegistry, createAuthStrategyRegistry, createEnvironmentSecretProvider, createServerContext } from '@tbox.cn/app-sdk/server';
9
- import { serverModule, MOCK_SHELL_USER_ID, enrollMockMember, logoutMockMember, __resetMockMemberState } from '../src/server/index';
9
+ import { serverModule, enrollMockMember, logoutMockMember, __resetMockMemberState } from '../src/server/index';
10
10
  import { mallControlPlaneOf } from '@tbox.cn/app-contracts-mall/server';
11
11
  import type { IntegrationsConfig } from '@tbox.cn/app-contracts';
12
12
  import type { RequestContext } from '@tbox.cn/app-contracts';
@@ -49,8 +49,8 @@ const catalog = {
49
49
 
50
50
  function requestContext(mallId = 'mall-demo'): RequestContext {
51
51
  return {
52
- identity: { userId: 'u_1', source: 'dev' },
53
- credentials: { provider: 'dev', token: 't' },
52
+ identity: { userId: 'local_u1', source: 'local' },
53
+ credentials: { provider: 'mock', token: 't' },
54
54
  attributes: { mall: { mallId } },
55
55
  };
56
56
  }
@@ -169,53 +169,72 @@ describe('C5:scoped mock 换码器(入会闭环身份供给)', () => {
169
169
  it('未选择商场(无 mallId)→ exchangeByCode 抛 AUTH_NOT_CONFIGURED 未选择商场', async () => {
170
170
  const { ctx } = fixtureCtx();
171
171
  const exchanger = ctx.authExchanges.get('alipay')!;
172
- await expect(exchanger.exchangeByCode('any-authcode', 'alipay')).rejects.toThrow(/未选择商场/);
172
+ await expect(exchanger.exchangeByCode({ code: 'any-authcode' })).rejects.toThrow(/未选择商场/);
173
173
  });
174
174
 
175
- it('GUEST 形态(对齐真实档潜客分支):token="" + openid 身份——合法登录未入会', async () => {
175
+ it('随机访客身份 + GUEST 形态(T17):guest_<32hex> 每次新随机;token="" + openid 键控', async () => {
176
176
  const { ctx } = fixtureCtx();
177
177
  const exchanger = ctx.authExchanges.get('alipay', { mall: { mallId: 'mall-demo' } })!;
178
- const result = await exchanger.exchangeByCode('any-authcode', 'alipay', { mall: { mallId: 'mall-demo' } });
179
- expect(result).toEqual({
180
- identity: { userId: 'demo-user', source: 'external' },
181
- credentials: { token: '', provider: 'mock', attributes: { openid: 'demo:openid:demo-user' } },
182
- });
178
+ const r1 = await exchanger.exchangeByCode({ code: 'c1', context: { mall: { mallId: 'mall-demo' } } });
179
+ expect(r1.identity.source).toBe('guest');
180
+ expect(r1.identity.userId).toMatch(/^guest_[0-9a-f]{32}$/);
181
+ expect(r1.credentials).toEqual({ token: '', provider: 'mock', attributes: { openid: `demo:openid:${r1.identity.userId}` } });
182
+ // 每次冷登新随机(无 previousIdentity)
183
+ const r2 = await exchanger.exchangeByCode({ code: 'c2', context: { mall: { mallId: 'mall-demo' } } });
184
+ expect(r2.identity.userId).not.toBe(r1.identity.userId);
183
185
  });
184
186
 
185
- it('MEMBER 形态(对齐真实档会员换码输出):入会后冷登 → token + memberId', async () => {
186
- enrollMockMember(MOCK_SHELL_USER_ID);
187
+ it('previousIdentity 复用轨:guest 同 sub 续签(enroll 后 MEMBER 形态、会话归属连续)', async () => {
187
188
  const { ctx } = fixtureCtx();
188
189
  const exchanger = ctx.authExchanges.get('alipay', { mall: { mallId: 'mall-demo' } })!;
189
- const result = await exchanger.exchangeByCode('any-authcode', 'alipay', { mall: { mallId: 'mall-demo' } });
190
- expect(result).toEqual({
191
- identity: { userId: 'demo-user', source: 'external' },
192
- credentials: { token: 'demo:token:demo-user', provider: 'mock', attributes: { memberId: 'demo-user' } },
190
+ const first = await exchanger.exchangeByCode({ code: 'c1', context: { mall: { mallId: 'mall-demo' } } });
191
+ enrollMockMember(first.identity.userId);
192
+ const renewed = await exchanger.exchangeByCode({
193
+ code: 'c2',
194
+ context: { mall: { mallId: 'mall-demo' } },
195
+ previousIdentity: { userId: first.identity.userId, source: 'guest' },
196
+ });
197
+ expect(renewed.identity).toEqual({ userId: first.identity.userId, source: 'guest' });
198
+ expect(renewed.credentials).toEqual({
199
+ token: `demo:token:${first.identity.userId}`,
200
+ provider: 'mock',
201
+ attributes: { memberId: first.identity.userId },
193
202
  });
194
203
  });
195
204
 
196
- it('刻意不实现 devExternalCredentials:dev-login 走协议 fail-soft 无 claim 签发', () => {
205
+ it('声明 allowsGuestLogin(browser 轴 auto 档 → guest 签发)', () => {
197
206
  const { ctx } = fixtureCtx();
198
- const exchanger = ctx.authExchanges.get('alipay', { mall: { mallId: 'mall-demo' } });
199
- // 能力口缺席(typeof 守卫语义——login-routes 同款消费形态)
200
- expect(typeof (exchanger as { devExternalCredentials?: unknown })?.devExternalCredentials).toBe('undefined');
207
+ const exchanger = ctx.authExchanges.get('alipay', { mall: { mallId: 'mall-demo' } })!;
208
+ expect(typeof (exchanger as { allowsGuestLogin?: unknown }).allowsGuestLogin).toBe('function');
209
+ expect((exchanger as { allowsGuestLogin: () => boolean }).allowsGuestLogin()).toBe(true);
201
210
  });
202
211
 
203
212
  it('登出态换码降级 GUEST 形态(对齐真实档 silentLogin 非 200 → 访客结果,2026-09-02 真机实测)', async () => {
204
- enrollMockMember(MOCK_SHELL_USER_ID);
205
- logoutMockMember(MOCK_SHELL_USER_ID);
206
213
  const { ctx } = fixtureCtx();
207
214
  const exchanger = ctx.authExchanges.get('alipay', { mall: { mallId: 'mall-demo' } })!;
208
- const degraded = await exchanger.exchangeByCode('any-authcode', 'alipay', { mall: { mallId: 'mall-demo' } });
209
- expect(degraded.identity).toEqual({ userId: MOCK_SHELL_USER_ID, source: 'external' });
215
+ const first = await exchanger.exchangeByCode({ code: 'c1', context: { mall: { mallId: 'mall-demo' } } });
216
+ const userId = first.identity.userId;
217
+ enrollMockMember(userId);
218
+ logoutMockMember(userId);
219
+ const degraded = await exchanger.exchangeByCode({
220
+ code: 'c2',
221
+ context: { mall: { mallId: 'mall-demo' } },
222
+ previousIdentity: { userId, source: 'guest' },
223
+ });
224
+ expect(degraded.identity).toEqual({ userId, source: 'guest' });
210
225
  expect(degraded.credentials).toEqual({
211
226
  token: '',
212
227
  provider: 'mock',
213
- attributes: { openid: 'demo:openid:demo-user' },
228
+ attributes: { openid: `demo:openid:${userId}` },
214
229
  });
215
230
  // 重登录(enroll 清登出态)后恢复 MEMBER 形态——H5 半屏成功登录后冷登即恢复
216
- enrollMockMember(MOCK_SHELL_USER_ID);
217
- const result = await exchanger.exchangeByCode('any-authcode', 'alipay', { mall: { mallId: 'mall-demo' } });
218
- expect(result.credentials?.token).toBe('demo:token:demo-user');
231
+ enrollMockMember(userId);
232
+ const result = await exchanger.exchangeByCode({
233
+ code: 'c3',
234
+ context: { mall: { mallId: 'mall-demo' } },
235
+ previousIdentity: { userId, source: 'guest' },
236
+ });
237
+ expect(result.credentials?.token).toBe(`demo:token:${userId}`);
219
238
  });
220
239
  });
221
240
 
@@ -281,20 +281,20 @@ describe('provider-mock:入会闭环共享 store(C5)', () => {
281
281
 
282
282
  describe('provider-mock:mock 入会落地路由(resolveMockEnroll 纯决策)', () => {
283
283
 
284
- it('有身份 + 无参 → 缺省 demo-user 翻状态返 200', () => {
285
- const outcome = resolveMockEnroll({ body: {}, identity: { source: 'dev' } });
286
- expect(outcome).toEqual({ status: 200, body: { state: 'active', memberRef: 'demo:member:demo-user' } });
287
- expect(isMockMemberEnrolled('demo-user')).toBe(true);
284
+ it('有身份 + 无参 → 缺省调用方身份翻状态返 200(T11 收口:固定 demo-user 缺省退役)', () => {
285
+ const outcome = resolveMockEnroll({ body: {}, identity: { source: 'guest', userId: 'guest_a1' } });
286
+ expect(outcome).toEqual({ status: 200, body: { state: 'active', memberRef: 'demo:member:guest_a1' } });
287
+ expect(isMockMemberEnrolled('guest_a1')).toBe(true);
288
288
  });
289
289
 
290
- it('定向 userId(curl 翻浏览器 dev 用户)', () => {
291
- const outcome = resolveMockEnroll({ body: { userId: 'dev_h5' }, identity: { source: 'dev' } });
292
- expect(outcome).toEqual({ status: 200, body: { state: 'active', memberRef: 'demo:member:dev_h5' } });
293
- expect(isMockMemberEnrolled('dev_h5')).toBe(true);
290
+ it('定向 userId(gate 页经 query 透传 H5 身份 / curl 调试声明主体)', () => {
291
+ const outcome = resolveMockEnroll({ body: { userId: 'guest_h5' }, identity: { source: 'guest', userId: 'guest_caller' } });
292
+ expect(outcome).toEqual({ status: 200, body: { state: 'active', memberRef: 'demo:member:guest_h5' } });
293
+ expect(isMockMemberEnrolled('guest_h5')).toBe(true);
294
294
  });
295
295
 
296
296
  it('幂等:同 userId 两次均 200', () => {
297
- const identity = { source: 'dev' as const };
297
+ const identity = { source: 'local' as const };
298
298
  expect(resolveMockEnroll({ body: {}, identity }).status).toBe(200);
299
299
  expect(resolveMockEnroll({ body: {}, identity }).status).toBe(200);
300
300
  });
@@ -306,29 +306,29 @@ describe('provider-mock:mock 入会落地路由(resolveMockEnroll 纯决策
306
306
  });
307
307
 
308
308
  it('NODE_ENV=production → 503(mock 不进生产装配,双保险)', () => {
309
- expect(resolveMockEnroll({ body: {}, identity: { source: 'dev' }, nodeEnv: 'production' }).status).toBe(503);
309
+ expect(resolveMockEnroll({ body: {}, identity: { source: 'local' }, nodeEnv: 'production' }).status).toBe(503);
310
310
  expect(isMockMemberEnrolled('demo-user')).toBe(false);
311
311
  });
312
312
  });
313
313
 
314
314
  describe('provider-mock:登出态(logout 路由 + 换码降级 GUEST 形态 + getAccount 感知)', () => {
315
- it('resolveMockLogout:有身份 → 200 翻登出态;enroll 清除登出态', () => {
315
+ it('resolveMockLogout:有身份 → 200 翻登出态(缺省调用方);enroll 清除登出态', () => {
316
316
  __resetMockMemberState();
317
- enrollMockMember('demo-user');
318
- expect(isMockMemberLoggedOut('demo-user')).toBe(false);
319
- const outcome = resolveMockLogout({ body: {}, identity: { source: 'dev' } });
317
+ enrollMockMember('guest_a1');
318
+ expect(isMockMemberLoggedOut('guest_a1')).toBe(false);
319
+ const outcome = resolveMockLogout({ body: {}, identity: { source: 'guest', userId: 'guest_a1' } });
320
320
  expect(outcome).toEqual({ status: 200, body: { state: 'logged_out' } });
321
- expect(isMockMemberLoggedOut('demo-user')).toBe(true);
321
+ expect(isMockMemberLoggedOut('guest_a1')).toBe(true);
322
322
  // 重登录(enroll)清除登出态——与真实链同构(gate 页登录成功后厂商侧登录态恢复)
323
- enrollMockMember('demo-user');
324
- expect(isMockMemberLoggedOut('demo-user')).toBe(false);
323
+ enrollMockMember('guest_a1');
324
+ expect(isMockMemberLoggedOut('guest_a1')).toBe(false);
325
325
  });
326
326
 
327
327
  it('resolveMockLogout:匿名/无身份 → 401;production → 503(与 enroll 同形)', () => {
328
328
  __resetMockMemberState();
329
329
  expect(resolveMockLogout({ body: {}, identity: null }).status).toBe(401);
330
330
  expect(resolveMockLogout({ body: {}, identity: { source: 'anonymous' } }).status).toBe(401);
331
- expect(resolveMockLogout({ body: {}, identity: { source: 'dev' }, nodeEnv: 'production' }).status).toBe(503);
331
+ expect(resolveMockLogout({ body: {}, identity: { source: 'local' }, nodeEnv: 'production' }).status).toBe(503);
332
332
  expect(isMockMemberLoggedOut('demo-user')).toBe(false);
333
333
  });
334
334