@tbox.cn/app-toolkit 0.8.5 → 0.8.7

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,363 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { join } from 'node:path';
3
+ import { mkdirSync } from 'node:fs';
4
+ import { createAppToolkit, type AppToolkit } from '../src/factory.js';
5
+ import { invalidateContractsResolver } from '../src/core/contracts-resolver.js';
6
+ import { AppToolkitError } from '../src/core/errors.js';
7
+ import { walkManifests } from '../src/assembly/walk-manifests.js';
8
+ import {
9
+ createTempApp,
10
+ writeFakeContractsPackage,
11
+ linkContracts,
12
+ writeAppFile,
13
+ CASCADE_RESOLVE_BODY,
14
+ SUPPLY_AGGREGATE_BODY,
15
+ type TempApp,
16
+ } from './demo-app.js';
17
+
18
+ /**
19
+ * 任务12 B 面测试:selectionScope(可选候选范围协议)。
20
+ * ① 声明 → manifest parser(zod 接受/拒绝)→ walk vendorMeta(仅自声明通道;槽贡献
21
+ * 不附着;首有效声明胜出)→ loadProvidersView 筛选。
22
+ * ② loadProviders() 无参/缺省 = 完整清单(成员/字段/顺序零变化——T12-05)。
23
+ * ③ scope 三模式矩阵:app=排除受限厂商;module=供给足迹 ∩ owns;service=供给命中
24
+ * (任意 id 可查,未受限恒留——T12-06/07)。
25
+ * ④ 错误矩阵:未知 kind/伴随参数不匹配 400 fields;module 未安装 404(参数错误先于
26
+ * 404——T12-08);additive 旧调用零变化。
27
+ */
28
+
29
+ interface Harness {
30
+ app: TempApp;
31
+ tk: AppToolkit;
32
+ }
33
+
34
+ /**
35
+ * scope 基座:词汇 3 服务(parking.query/parking.payment/member.account)+
36
+ * - provider-limited: 自声明 'limited' + selectionScope: 'supplied-services'(供给 parking.query)
37
+ * - provider-free: 自声明 'free'(无策略——slots 供 parking.query/parking.payment)
38
+ * - provider-allspec: 自声明 'allspec' + 显式 selectionScope: 'all'(供给 parking.vehicle 词汇外——
39
+ * 显式 all 恒完整清单语义)
40
+ * - provider-zero-restricted: 零槽自声明 'zerolim' + supplied-services(「厂商在、供给空」)
41
+ * - provider-foreign: 自声明 'vendora' + supplied-services + slots 全部贡献给外来厂商
42
+ * 'vendorb'(U6:供给照常、meta/策略不附着——vendorb 恒不受 vendora 策略污染)
43
+ * - module-parking(owns query+payment)/ module-member(owns member.account——跨域负例)
44
+ */
45
+ function scopeHarness(): Harness {
46
+ const app = createTempApp('tbox-t12-scope-');
47
+ invalidateContractsResolver();
48
+ const store = join(app.appDir, 'contracts');
49
+ writeFakeContractsPackage(store, {
50
+ version: '0.9.0',
51
+ resolveBody: CASCADE_RESOLVE_BODY,
52
+ supplyBody: SUPPLY_AGGREGATE_BODY,
53
+ slots: ['parking.query', 'parking.payment', 'parking.vehicle', 'member.account'],
54
+ });
55
+ linkContracts(app.appDir, store, { subdir: 'apps/server' });
56
+
57
+ const manifest = (o: Record<string, unknown>): string =>
58
+ JSON.stringify({ schemaVersion: 1, version: '0.1.0', kind: 'business', dependencies: { modules: [] }, env: [], ...o });
59
+
60
+ mkdirSync(join(app.appDir, 'packages', 'provider-limited'), { recursive: true });
61
+ writeAppFile(app.appDir, 'packages/provider-limited/tbox.module.json', manifest({
62
+ id: 'provider-limited',
63
+ declare: {
64
+ providers: {
65
+ id: 'limited',
66
+ selectionScope: 'supplied-services',
67
+ slots: [{ service: 'parking.query', provider: 'limited', implementation: 'limited-parking@1', credentialType: 'limited-c' }],
68
+ },
69
+ },
70
+ }));
71
+ mkdirSync(join(app.appDir, 'packages', 'provider-free'), { recursive: true });
72
+ writeAppFile(app.appDir, 'packages/provider-free/tbox.module.json', manifest({
73
+ id: 'provider-free',
74
+ declare: {
75
+ providers: {
76
+ id: 'free',
77
+ slots: [
78
+ { service: 'parking.query', provider: 'free', implementation: 'free-query@1', credentialType: 'free-c' },
79
+ { service: 'parking.payment', provider: 'free', implementation: 'free-pay@1', credentialType: 'free-c' },
80
+ ],
81
+ },
82
+ },
83
+ }));
84
+ mkdirSync(join(app.appDir, 'packages', 'provider-allspec'), { recursive: true });
85
+ writeAppFile(app.appDir, 'packages/provider-allspec/tbox.module.json', manifest({
86
+ id: 'provider-allspec',
87
+ declare: {
88
+ providers: {
89
+ id: 'allspec',
90
+ selectionScope: 'all',
91
+ slots: [{ service: 'parking.vehicle', provider: 'allspec', implementation: 'allspec-vehicle@1', credentialType: 'allspec-c' }],
92
+ },
93
+ },
94
+ }));
95
+ mkdirSync(join(app.appDir, 'packages', 'provider-zero-restricted'), { recursive: true });
96
+ writeAppFile(app.appDir, 'packages/provider-zero-restricted/tbox.module.json', manifest({
97
+ id: 'provider-zero-restricted',
98
+ declare: { providers: { id: 'zerolim', selectionScope: 'supplied-services', slots: [] } },
99
+ }));
100
+ mkdirSync(join(app.appDir, 'packages', 'provider-foreign'), { recursive: true });
101
+ writeAppFile(app.appDir, 'packages/provider-foreign/tbox.module.json', manifest({
102
+ id: 'provider-foreign',
103
+ declare: {
104
+ providers: {
105
+ id: 'vendora',
106
+ selectionScope: 'supplied-services',
107
+ slots: [{ service: 'member.account', provider: 'vendorb', implementation: 'vendorb-member@1', credentialType: 'vendorb-c' }],
108
+ },
109
+ },
110
+ }));
111
+ mkdirSync(join(app.appDir, 'packages', 'module-parking'), { recursive: true });
112
+ writeAppFile(app.appDir, 'packages/module-parking/tbox.module.json', manifest({
113
+ id: 'module-parking',
114
+ declare: {
115
+ owns: [{ service: 'parking.query' }, { service: 'parking.payment' }],
116
+ consumes: [{ service: 'parking.query', optional: false }],
117
+ },
118
+ }));
119
+ mkdirSync(join(app.appDir, 'packages', 'module-member'), { recursive: true });
120
+ writeAppFile(app.appDir, 'packages/module-member/tbox.module.json', manifest({
121
+ id: 'module-member',
122
+ declare: { owns: [{ service: 'member.account' }] },
123
+ }));
124
+ writeAppFile(app.appDir, 'config/integrations.json', JSON.stringify({ provider: 'mock', services: {} }));
125
+ return { app, tk: createAppToolkit(app.appDir) };
126
+ }
127
+
128
+ describe('任务12 B:selectionScope 声明与 walk 传递', () => {
129
+ it('manifest → vendorMeta:仅自声明厂商受限在场;槽贡献(外来厂商)不附着策略', () => {
130
+ const { app } = scopeHarness();
131
+ const walk = walkManifests(app.appDir);
132
+ expect(walk.catalog.vendorMeta.limited?.selectionScope).toBe('supplied-services');
133
+ expect(walk.catalog.vendorMeta.free?.selectionScope).toBeUndefined();
134
+ expect(walk.catalog.vendorMeta.allspec?.selectionScope).toBeUndefined(); // 显式 all ≡ 缺省——不进 meta
135
+ expect(walk.catalog.vendorMeta.zerolim?.selectionScope).toBe('supplied-services'); // 零槽自声明同路径
136
+ // U6:vendora 的 supplied-services 策略不附着到 vendorb(贡献槽厂商)
137
+ expect(walk.catalog.vendorMeta.vendora?.selectionScope).toBe('supplied-services');
138
+ expect(walk.catalog.vendorMeta.vendorb?.selectionScope).toBeUndefined();
139
+ // vendorb 供给照常贡献(U6——供给/impl/credentialType 不受策略隔离)
140
+ expect(walk.catalog.providers.vendorb?.['vendorb-member@1']?.services).toEqual(['member.account']);
141
+ });
142
+
143
+ it('satisfied zod:selectionScope 缺席/合法值通过;非法值被 parser 拒(非解析期静默剥掉)', () => {
144
+ const { app } = scopeHarness();
145
+ const valid = walkManifests(app.appDir);
146
+ expect(valid.modules.map((m) => m.id)).toContain('provider-limited');
147
+ // 非法值: manifest 解析失败 → 不进 modules+不落 catalog(宽松/legacy 通道不兜底新键)
148
+ mkdirSync(join(app.appDir, 'packages', 'provider-bad'), { recursive: true });
149
+ writeAppFile(app.appDir, 'packages/provider-bad/tbox.module.json', JSON.stringify({
150
+ schemaVersion: 1, id: 'provider-bad', version: '0.1.0', kind: 'business',
151
+ declare: { providers: { id: 'badspec', selectionScope: 'maybe', slots: [] } },
152
+ dependencies: { modules: [] }, env: [],
153
+ }));
154
+ const after = walkManifests(app.appDir);
155
+ expect(after.modules.map((m) => m.id)).not.toContain('provider-bad');
156
+ expect(after.catalog.vendorMeta.badspec?.selectionScope).toBeUndefined();
157
+ });
158
+
159
+ it('范围随首声明一并确定:首包缺省/显式 all 也是有效声明——后包 supplied-services 不得补入(P2②返修)', () => {
160
+ const { app } = scopeHarness();
161
+ // 首包 aa-first 自声明 limited-all(缺省) + slots 供给 parking.query;后包 zz-second 同厂商 restricted
162
+ mkdirSync(join(app.appDir, 'packages', 'aa-first'), { recursive: true });
163
+ writeAppFile(app.appDir, 'packages/aa-first/tbox.module.json', JSON.stringify({
164
+ schemaVersion: 1, id: 'aa-first', version: '0.1.0', kind: 'business',
165
+ declare: { providers: { id: 'latervo', selectionScope: 'all', slots: [{ service: 'parking.query', provider: 'latervo', implementation: 'latervo-q@1', credentialType: 'latervo-c' }] } },
166
+ dependencies: { modules: [] }, env: [],
167
+ }));
168
+ mkdirSync(join(app.appDir, 'packages', 'zz-second'), { recursive: true });
169
+ writeAppFile(app.appDir, 'packages/zz-second/tbox.module.json', JSON.stringify({
170
+ schemaVersion: 1, id: 'zz-second', version: '0.1.0', kind: 'business',
171
+ declare: { providers: { id: 'latervo', selectionScope: 'supplied-services', slots: [] } },
172
+ dependencies: { modules: [] }, env: [],
173
+ }));
174
+ const walk = walkManifests(app.appDir);
175
+ expect(walk.catalog.vendorMeta.latervo?.selectionScope).toBeUndefined(); // 首声明 all 锁定——后声明 restricted 无效
176
+ });
177
+
178
+ it('首声明缺省(未写 selectionScope)同锁:后包 restricted 不得令厂商被隐藏(P2②返修另一向)', async () => {
179
+ const { app } = scopeHarness();
180
+ mkdirSync(join(app.appDir, 'packages', 'aa-first'), { recursive: true });
181
+ writeAppFile(app.appDir, 'packages/aa-first/tbox.module.json', JSON.stringify({
182
+ schemaVersion: 1, id: 'aa-first', version: '0.1.0', kind: 'business',
183
+ declare: { providers: { id: 'silentvo', slots: [{ service: 'parking.query', provider: 'silentvo', implementation: 'silentvo-q@1', credentialType: 'silentvo-c' }] } },
184
+ dependencies: { modules: [] }, env: [],
185
+ }));
186
+ mkdirSync(join(app.appDir, 'packages', 'zz-second'), { recursive: true });
187
+ writeAppFile(app.appDir, 'packages/zz-second/tbox.module.json', JSON.stringify({
188
+ schemaVersion: 1, id: 'zz-second', version: '0.1.0', kind: 'business',
189
+ declare: { providers: { id: 'silentvo', selectionScope: 'supplied-services', slots: [] } },
190
+ dependencies: { modules: [] }, env: [],
191
+ }));
192
+ const walk = walkManifests(app.appDir);
193
+ expect(walk.catalog.vendorMeta.silentvo?.selectionScope).toBeUndefined();
194
+ const view = await createAppToolkit(app.appDir).loadProviders({ scope: { kind: 'app' } });
195
+ expect(view.providers.map((p) => p.provider)).toContain('silentvo'); // 首声明缺省 all——后包不得隐藏
196
+ });
197
+
198
+ it('首有效声明胜出:先自声明 supplied-services 的包建立策略,后包 all 不覆盖', () => {
199
+ const { app } = scopeHarness();
200
+ // packages 发现序 = 目录序;provider-limited(lexically first)已写 supplied-services
201
+ mkdirSync(join(app.appDir, 'packages', 'zz-second'), { recursive: true });
202
+ writeAppFile(app.appDir, 'packages/zz-second/tbox.module.json', JSON.stringify({
203
+ schemaVersion: 1, id: 'zz-second', version: '0.1.0', kind: 'business',
204
+ declare: { providers: { id: 'limited', selectionScope: 'all', slots: [] } },
205
+ dependencies: { modules: [] }, env: [],
206
+ }));
207
+ const walk = walkManifests(app.appDir);
208
+ expect(walk.catalog.vendorMeta.limited?.selectionScope).toBe('supplied-services');
209
+ });
210
+ });
211
+
212
+ describe('任务12 B:loadProviders 筛选矩阵(T12-05/06/07)', () => {
213
+ it('无参/缺省:完整清单——成员、字段、顺序与协议保持(受限厂商仍在完整元数据清单)', async () => {
214
+ const { tk } = scopeHarness();
215
+ const none = await tk.loadProviders();
216
+ const empty = await tk.loadProviders({});
217
+ expect(empty).toEqual(none);
218
+ expect(none.providers.map((p) => p.provider)).toEqual(
219
+ ['allspec', 'free', 'limited', 'vendora', 'vendorb', 'zerolim'].sort((a, b) => a.localeCompare(b)),
220
+ );
221
+ for (const p of none.providers) {
222
+ expect(Object.keys(p).filter((k) => k !== 'name').sort()).toEqual(['local', 'provider']); // 不新增必填字段
223
+ }
224
+ });
225
+
226
+ it('scope=app:受限厂商(limited/zerolim/vendora)排除;无策略 free 与显式 all allspec 保留', async () => {
227
+ const { tk } = scopeHarness();
228
+ const view = await tk.loadProviders({ scope: { kind: 'app' } });
229
+ const names = view.providers.map((p) => p.provider);
230
+ expect(names).toContain('free');
231
+ expect(names).toContain('allspec');
232
+ expect(names).not.toContain('limited');
233
+ expect(names).not.toContain('zerolim');
234
+ expect(names).not.toContain('vendora');
235
+ expect(names).toContain('vendorb'); // 未受限厂商不受外来策略污染——恒可选
236
+ });
237
+
238
+ it('scope=module:模块 owns 服务 ∩ 供给足迹——module-parking 保留 limited;module-member 排除', async () => {
239
+ const { tk } = scopeHarness();
240
+ const parkingScope = await tk.loadProviders({ scope: { kind: 'module', module: 'module-parking' } });
241
+ expect(parkingScope.providers.map((p) => p.provider)).toContain('limited');
242
+ const memberScope = await tk.loadProviders({ scope: { kind: 'module', module: 'module-member' } });
243
+ expect(memberScope.providers.map((p) => p.provider)).not.toContain('limited');
244
+ expect(memberScope.providers.map((p) => p.provider)).toContain('free'); // 无策略恒留
245
+ expect(memberScope.providers.map((p) => p.provider)).not.toContain('zerolim'); // 零槽 restricted 不交集
246
+ });
247
+
248
+ it('scope=service:供给命中保留(limited×parking.query);不供给/未知 id 排除受限、保留未受限', async () => {
249
+ const { tk } = scopeHarness();
250
+ const q = await tk.loadProviders({ scope: { kind: 'service', service: 'parking.query' } });
251
+ expect(q.providers.map((p) => p.provider)).toContain('limited');
252
+ const v = await tk.loadProviders({ scope: { kind: 'service', service: 'parking.vehicle' } });
253
+ expect(v.providers.map((p) => p.provider)).not.toContain('limited');
254
+ expect(v.providers.map((p) => p.provider)).toContain('free'); // 判定过滤——未受限供应商恒保留
255
+ const unknown = await tk.loadProviders({ scope: { kind: 'service', service: 'auth.never-declared' } });
256
+ expect(unknown.providers.map((p) => p.provider)).not.toContain('limited'); // 未知 service 非寻址——受限不供给即排除
257
+ expect(unknown.providers.map((p) => p.provider)).toContain('free');
258
+ });
259
+
260
+ it('排序保持:任意 scope 下结果仍按 provider 字典序(完整清单同构)', async () => {
261
+ const { tk } = scopeHarness();
262
+ const scoped = await tk.loadProviders({ scope: { kind: 'app' } });
263
+ const sorted = [...scoped.providers].sort((a, b) => a.provider.localeCompare(b.provider));
264
+ expect(scoped.providers).toEqual(sorted);
265
+ });
266
+ });
267
+
268
+ describe('任务12 B:错误矩阵(T12-08——参数错误先于 404;additive 旧调用兼容)', () => {
269
+ it('未知/空 kind → 400 fields=[scope];module/service 伴随参数缺席 → 400 fields 对应', async () => {
270
+ const { tk } = scopeHarness();
271
+ await expect(tk.loadProviders({ scope: { kind: 'nope' } as never })).rejects.toMatchObject({
272
+ code: 'VALIDATION_FAILED', httpStatus: 400, fields: ['scope'],
273
+ });
274
+ await expect(tk.loadProviders({ scope: { kind: 'app', module: 'module-parking' } as never })).rejects.toMatchObject({
275
+ code: 'VALIDATION_FAILED', httpStatus: 400,
276
+ });
277
+ await expect(tk.loadProviders({ scope: { kind: 'module' } as never })).rejects.toMatchObject({
278
+ code: 'VALIDATION_FAILED', httpStatus: 400, fields: ['module'],
279
+ });
280
+ await expect(tk.loadProviders({ scope: { kind: 'service' } as never })).rejects.toMatchObject({
281
+ code: 'VALIDATION_FAILED', httpStatus: 400, fields: ['service'],
282
+ });
283
+ });
284
+
285
+ it('scope=module 指向未安装模块 → 404 MODULE_NOT_FOUND(参数合法后)', async () => {
286
+ const { tk } = scopeHarness();
287
+ await expect(tk.loadProviders({ scope: { kind: 'module', module: 'module-not-installed' } })).rejects.toMatchObject({
288
+ code: 'MODULE_NOT_FOUND', httpStatus: 404,
289
+ });
290
+ // 错误种类为 AppToolkitError(复用 toolkit 错误类型)
291
+ await expect(tk.loadProviders({ scope: { kind: 'module', module: 'module-not-installed' } })).rejects.toBeInstanceOf(AppToolkitError);
292
+ });
293
+
294
+ it('404 不依赖受限供应商:全场无策略(all/缺省)时未知模块同样 404 而非 200 完整清单(P2①返修)', async () => {
295
+ // 独立 app:仅 free/allspec(零策略厂商)——受限供应商不在场走不到原 per-provider 校验
296
+ const app = createTempApp('tbox-t12-all404-');
297
+ invalidateContractsResolver();
298
+ const store = join(app.appDir, 'contracts');
299
+ writeFakeContractsPackage(store, {
300
+ version: '0.9.0',
301
+ resolveBody: CASCADE_RESOLVE_BODY,
302
+ supplyBody: SUPPLY_AGGREGATE_BODY,
303
+ slots: ['parking.query'],
304
+ });
305
+ linkContracts(app.appDir, store, { subdir: 'apps/server' });
306
+ const manifest = (o: Record<string, unknown>): string =>
307
+ JSON.stringify({ schemaVersion: 1, version: '0.1.0', kind: 'business', dependencies: { modules: [] }, env: [], ...o });
308
+ mkdirSync(join(app.appDir, 'packages', 'provider-free'), { recursive: true });
309
+ writeAppFile(app.appDir, 'packages/provider-free/tbox.module.json', manifest({
310
+ id: 'provider-free',
311
+ declare: { providers: { id: 'free', slots: [{ service: 'parking.query', provider: 'free', implementation: 'free-query@1', credentialType: 'free-c' }] } },
312
+ }));
313
+ mkdirSync(join(app.appDir, 'packages', 'provider-allspec'), { recursive: true });
314
+ writeAppFile(app.appDir, 'packages/provider-allspec/tbox.module.json', manifest({
315
+ id: 'provider-allspec',
316
+ declare: { providers: { id: 'allspec', selectionScope: 'all', slots: [{ service: 'parking.query', provider: 'allspec', implementation: 'allspec-q@1', credentialType: 'allspec-c' }] } },
317
+ }));
318
+ writeAppFile(app.appDir, 'config/integrations.json', JSON.stringify({ provider: 'mock', services: {} }));
319
+ const tk = createAppToolkit(app.appDir);
320
+ await expect(tk.loadProviders({ scope: { kind: 'module', module: 'module-not-installed' } })).rejects.toMatchObject({
321
+ code: 'MODULE_NOT_FOUND', httpStatus: 404,
322
+ });
323
+ // 已安装模块 + 全 all → 200 完整清单(不受限不筛)
324
+ const okView = await tk.loadProviders({ scope: { kind: 'module', module: 'provider-free' } });
325
+ expect(okView.providers.map((p) => p.provider).sort()).toEqual(['allspec', 'free']);
326
+ });
327
+
328
+ it('空供应商清单时未知模块同样 404(校验先于遍历——不产 200 空清单误导)(P2①返修)', async () => {
329
+ const app = createTempApp('tbox-t12-empty404-');
330
+ invalidateContractsResolver();
331
+ const store = join(app.appDir, 'contracts');
332
+ writeFakeContractsPackage(store, {
333
+ version: '0.9.0',
334
+ resolveBody: CASCADE_RESOLVE_BODY,
335
+ supplyBody: SUPPLY_AGGREGATE_BODY,
336
+ slots: ['parking.query'],
337
+ });
338
+ linkContracts(app.appDir, store, { subdir: 'apps/server' });
339
+ mkdirSync(join(app.appDir, 'packages', 'module-parking'), { recursive: true });
340
+ writeAppFile(app.appDir, 'packages/module-parking/tbox.module.json', JSON.stringify({
341
+ schemaVersion: 1, id: 'module-parking', version: '0.1.0', kind: 'business',
342
+ dependencies: { modules: [] }, env: [],
343
+ declare: { owns: [{ service: 'parking.query' }] },
344
+ }));
345
+ writeAppFile(app.appDir, 'config/integrations.json', JSON.stringify({ provider: 'mock', services: {} }));
346
+ const tk = createAppToolkit(app.appDir);
347
+ await expect(tk.loadProviders({ scope: { kind: 'module', module: 'module-not-installed' } })).rejects.toMatchObject({
348
+ code: 'MODULE_NOT_FOUND', httpStatus: 404,
349
+ });
350
+ const empty = await tk.loadProviders();
351
+ expect(empty.providers).toEqual([]); // 零供应商在场——空清单基线(校验不误伤)
352
+ });
353
+
354
+ it('供给预览端点语义不变:loadProviderSupply 不因缺省受本扩展影响(缺省仍要求显式 scope)', async () => {
355
+ const { tk } = scopeHarness();
356
+ // supply scope 校验单点沿用(签名为必传参——编译级保证;此处断言既有错误矩阵未被触及)
357
+ await expect(tk.loadProviderSupply('limited', { kind: 'module', module: 'module-not-installed' })).rejects.toMatchObject({
358
+ code: 'MODULE_NOT_FOUND', httpStatus: 404,
359
+ });
360
+ const sup = await tk.loadProviderSupply('limited', { kind: 'service', service: 'parking.query' });
361
+ expect(sup.services[0]).toMatchObject({ service: 'parking.query', supplied: true });
362
+ });
363
+ });
@@ -1,5 +1,5 @@
1
1
  import { describe, it, expect } from 'vitest';
2
- import { readdirSync, readFileSync, statSync } from 'node:fs';
2
+ import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
3
3
  import { join } from 'node:path';
4
4
  import { fileURLToPath } from 'node:url';
5
5
 
@@ -149,7 +149,14 @@ describe('G 批 x-tbox-global 标记结构断言(integrations-v6 全局字段
149
149
  expect(globalKeysOf(join(base, 'mall-info.config.json'))).toEqual([]); // 零声明负例
150
150
  });
151
151
 
152
- it('素材对账锁:.real-env 全局层键 ⊆ 对应厂商声明并集(mock/mocktest/wanda/joycity 四档)', () => {
152
+ it('hupo 期望集锁(任务12 商家两字段):merchantId/merchantKey 恒全局标记(配置面表单可见基线)', () => {
153
+ const base = join(fileURLToPath(new URL('../../..', import.meta.url)), 'modules', 'provider-hupo', 'schemas');
154
+ expect(globalKeysOf(join(base, 'activity-play.config.json')).sort()).toEqual(['merchantId', 'merchantKey']);
155
+ // 凭据 schema 已退场(任务12 零 credentialRef——目录若回潮即结构性异常)
156
+ expect(existsSync(join(base, 'credentials'))).toBe(false);
157
+ });
158
+
159
+ it('素材对账锁:.real-env 全局层键 ⊆ 对应厂商声明并集(mock/mocktest/wanda/joycity 四档 + activity-play 素材目录)', () => {
153
160
  const repoRoot = fileURLToPath(new URL('../../..', import.meta.url));
154
161
  const collect = (vendor: string): Set<string> => {
155
162
  const out = new Set<string>();
@@ -183,5 +190,16 @@ describe('G 批 x-tbox-global 标记结构断言(integrations-v6 全局字段
183
190
  expect(declared.has(k), `${vendor} 档全局层键 "${k}" ∉ schema x-tbox-global 声明并集——补标记或改素材(对账锁)`).toBe(true);
184
191
  }
185
192
  }
193
+ // 活动玩法素材目录(任务12 商家两字段——modules.*.config 键 ⊆ hupo 声明并集)
194
+ const hDeclared = collect('hupo');
195
+ const apDir = join(repoRoot, '.real-env', 'config', 'activity-play');
196
+ for (const f of readdirSync(apDir).filter((x) => x.endsWith('.json'))) {
197
+ const ig = JSON.parse(readFileSync(join(apDir, f), 'utf8')) as { modules?: Record<string, { config?: Record<string, unknown> }> };
198
+ for (const [mod, node] of Object.entries(ig.modules ?? {})) {
199
+ for (const k of Object.keys(node.config ?? {})) {
200
+ expect(hDeclared.has(k), `activity-play/${f} ${mod} config 键 "${k}" ∉ hupo x-tbox-global 声明并集——补标记或改素材(对账锁)`).toBe(true);
201
+ }
202
+ }
203
+ }
186
204
  });
187
205
  });
@@ -386,6 +386,25 @@ describe('views/工厂(O 矩阵核心族)', () => {
386
386
  expect(mod.integration).toEqual({ provider: 'wanda' });
387
387
  });
388
388
 
389
+ it('O1b 模块集成位投影含 credentialRef(GET ≡ PUT 三同构——共享连接归属层凭据可经配置面维护)', async () => {
390
+ const appView = await h.tk.loadApp();
391
+ expect(appView.integration?.provider).toBe('mock');
392
+ const mod = await h.tk.loadModule('module-parking');
393
+ // 基座 integrations modules 节仅 provider → 投影仅 provider(无凭据键不合成)
394
+ expect(mod.integration).toEqual({ provider: 'wanda' });
395
+ // 写侧带入 credentialRef 后读对偶对齐(新 toolkit 实例读同 app——与 PUT body 三同构)
396
+ writeIntegrations(h.app.appDir, {
397
+ provider: 'mock',
398
+ instances: [{ id: 'mall-bj-01', name: '北京店' }],
399
+ defaultInstanceId: 'mall-bj-01',
400
+ modules: { 'module-parking': { provider: 'wanda', config: { baseUrl: 'x' }, credentialRef: 'secret://parking-shared' } },
401
+ services: { 'parking.query': { provider: 'wanda', implementation: 'wanda-parking@1' } },
402
+ });
403
+ const tk2 = createAppToolkit(h.app.appDir);
404
+ const mod2 = await tk2.loadModule('module-parking');
405
+ expect(mod2.integration).toEqual({ provider: 'wanda', config: { baseUrl: 'x' }, credentialRef: 'secret://parking-shared' });
406
+ });
407
+
389
408
  it('O2 静态投影零求值键:/app、/modules/:m、/services/:s 结构性无 resolution/issues/contracts', async () => {
390
409
  const appView = await h.tk.loadApp();
391
410
  expect(Object.keys(appView)).toEqual(['services', 'integration', 'binding']);
@@ -367,6 +367,35 @@ describe('写核心管线(FX-1c)', () => {
367
367
  h2.app.dispose();
368
368
  });
369
369
 
370
+ it('v6-G4 模块级 credentialRef 可写(共享连接归属层——DTO/白名单投影三同构):写入/读回/清键', async () => {
371
+ const h2 = harnessWithModule();
372
+ writeAppFile(h2.app.appDir, INTEGRATIONS_FILE, JSON.stringify({
373
+ services: {},
374
+ modules: { 'module-parking': { provider: 'wanda', config: { baseUrl: 'x' } } },
375
+ }, null, 2));
376
+ // 写入(全量替换语义——credentialRef 进节点,provider/config 保留)
377
+ const r = await h2.tk.writeModuleIntegration('module-parking', {
378
+ provider: 'wanda', config: { baseUrl: 'x' }, credentialRef: 'secret://parking-shared',
379
+ });
380
+ expect(r.written).toBe(true);
381
+ const cfg = h2.readConfig();
382
+ expect((cfg.modules as Record<string, unknown>)['module-parking']).toEqual({
383
+ provider: 'wanda', config: { baseUrl: 'x' }, credentialRef: 'secret://parking-shared',
384
+ });
385
+ // 清键:PUT 不带 credentialRef = 该键删除(集成位编辑三键全量替换);resources 保全语义同
386
+ await h2.tk.writeModuleIntegration('module-parking', { provider: 'wanda', config: { baseUrl: 'x' } });
387
+ const cfg2 = h2.readConfig();
388
+ expect((cfg2.modules as Record<string, unknown>)['module-parking']).toEqual({ provider: 'wanda', config: { baseUrl: 'x' } });
389
+ h2.app.dispose();
390
+ });
391
+
392
+ it('v6-G5 implementation 仍是模块节外键(catalog 派生命名空间——不进 DTO/白名单/锁步 zod 键集)', async () => {
393
+ const h2 = harnessWithModule();
394
+ await expect(h2.tk.writeModuleIntegration('module-parking', { provider: 'wanda', implementation: 'wanda-parking@1' } as never))
395
+ .rejects.toMatchObject({ code: 'VALIDATION_FAILED', httpStatus: 400 });
396
+ h2.app.dispose();
397
+ });
398
+
370
399
  it('v6-G3 键集纪律:node 含 provider/config 外键 → 400(fail-visible);零归属模块 → 400', async () => {
371
400
  const h2 = harnessWithModule();
372
401
  await expect(h2.tk.writeModuleIntegration('module-parking', { provider: 'x', resources: { k: {} } } as never))