dsh-desktop-statusbar 3.0.6 → 3.0.8
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/docs/official-models.json +24 -2
- package/docs/official-models.md +5 -3
- package/lib/balance-api.js +175 -3
- package/lib/client.js +278 -40
- package/lib/index.js +153 -14
- package/package.json +1 -1
- package/tests/verify-clock.js +3 -2
- package/tests/verify-context.js +44 -11
- package/tests/verify-copy.js +1 -1
- package/tests/verify-fixes.js +58 -0
- package/tests/verify-generic-relay.js +25 -15
- package/tests/verify-magpie-quota.mjs +328 -1
- package/tests/verify-prices.js +1 -1
- package/tests/verify-provider-fees.js +66 -1
- package/tests/verify-relay-settings.js +8 -8
- package/tests/verify-render.js +14 -0
- package/tests/verify-tips.js +87 -0
- package/tests/verify.js +94 -16
|
@@ -16,13 +16,14 @@ import { dirname, join } from 'node:path'
|
|
|
16
16
|
import { fileURLToPath } from 'node:url'
|
|
17
17
|
import {
|
|
18
18
|
adaptersFor,
|
|
19
|
-
magpieModelProvider, magpieModelPrefix, magpieBalanceOf,
|
|
19
|
+
magpieModelProvider, magpieModelPrefix, magpieModelQuotaProvider, magpieBalanceOf,
|
|
20
20
|
magpieAccountFromRoute, magpieParseMoney, magpieQuotaEntries,
|
|
21
21
|
} from '../lib/balance-api.js'
|
|
22
22
|
import {
|
|
23
23
|
queryBalanceForTest,
|
|
24
24
|
providerConfigFromSettings,
|
|
25
25
|
parseGenericRelayBalance,
|
|
26
|
+
parseGenericRelayBalanceAny,
|
|
26
27
|
queryGenericRelayBalance,
|
|
27
28
|
} from '../lib/index.js'
|
|
28
29
|
|
|
@@ -160,6 +161,166 @@ check('models 的 owned_by 与模型前缀兜底', () => {
|
|
|
160
161
|
assert.equal(magpieModelPrefix('nope'), null)
|
|
161
162
|
})
|
|
162
163
|
|
|
164
|
+
/* 订阅型配额:used/remaining 是百分比窗口(没有 unit=credits),必须按 percent 解析,不能当积分。 */
|
|
165
|
+
check('work/codex 订阅百分比窗口(5h/7d)解析为 percent,不是 credits', () => {
|
|
166
|
+
const quotas = {
|
|
167
|
+
data: [{
|
|
168
|
+
provider: 'work/codex', name: 'codex', kind: 'subscription', user: 'u1',
|
|
169
|
+
windows: [
|
|
170
|
+
{ name: '5 hours', used: 19, remaining: 81 },
|
|
171
|
+
{ name: '7 days', used: 45, remaining: 55 },
|
|
172
|
+
],
|
|
173
|
+
}],
|
|
174
|
+
}
|
|
175
|
+
const value = magpieBalanceOf(quotas, { provider: 'work/codex' })
|
|
176
|
+
assert.equal(value.ok, true)
|
|
177
|
+
assert.equal(value.mode, 'percent')
|
|
178
|
+
assert.equal(value.isPercent, true)
|
|
179
|
+
assert.equal(value.isCredits, undefined, 'percent 不能标成积分')
|
|
180
|
+
/* primary 优先「五小时」窗口:total = 5 hours 的已用 19% */
|
|
181
|
+
assert.equal(value.total, '19.00%')
|
|
182
|
+
assert.equal(value.currency, '', '百分比不带货币')
|
|
183
|
+
/* 完整 windows 透传(供客户端画五小时/七天进度条) */
|
|
184
|
+
assert.ok(Array.isArray(value.windows), 'percent 应带 windows 数组')
|
|
185
|
+
assert.equal(value.windows.length, 2)
|
|
186
|
+
assert.equal(value.windows[0].name, '5 hours')
|
|
187
|
+
assert.equal(value.windows[0].used, 19)
|
|
188
|
+
assert.equal(value.windows[0].remaining, 81)
|
|
189
|
+
assert.equal(value.windows[1].name, '7 days')
|
|
190
|
+
assert.equal(value.windows[1].used, 45)
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
check('percent 无五小时窗口时 primary = 合并后 used 最大的一类', () => {
|
|
194
|
+
const quotas = {
|
|
195
|
+
data: [{
|
|
196
|
+
provider: 'p3', kind: 'subscription', user: 'u',
|
|
197
|
+
windows: [
|
|
198
|
+
{ name: 'monthly', used: 10, remaining: 90 },
|
|
199
|
+
{ name: 'weekly', used: 45, remaining: 55 },
|
|
200
|
+
],
|
|
201
|
+
}],
|
|
202
|
+
}
|
|
203
|
+
const value = magpieBalanceOf(quotas, { provider: 'p3' })
|
|
204
|
+
assert.equal(value.mode, 'percent')
|
|
205
|
+
assert.equal(value.total, '45.00%', '无五小时 → 合并后 used 最大的一类(weekly 45%)')
|
|
206
|
+
assert.equal(value.windows.length, 2)
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
check('percent 多账号:5h/7d 各账号合并为综合百分比(不取单账号)', () => {
|
|
210
|
+
const quotas = {
|
|
211
|
+
data: [
|
|
212
|
+
{ provider: 'p5', kind: 'subscription', user: 'a',
|
|
213
|
+
windows: [{ name: '5 hours', used: 20, remaining: 80 }, { name: '7 days', used: 30, remaining: 70 }] },
|
|
214
|
+
{ provider: 'p5', kind: 'subscription', user: 'b',
|
|
215
|
+
windows: [{ name: '5 hours', used: 60, remaining: 40 }, { name: '7 days', used: 50, remaining: 50 }] },
|
|
216
|
+
],
|
|
217
|
+
}
|
|
218
|
+
const value = magpieBalanceOf(quotas, { provider: 'p5' })
|
|
219
|
+
assert.equal(value.mode, 'percent')
|
|
220
|
+
assert.equal(value.windows.length, 2)
|
|
221
|
+
/* 无 limit ⇒ 按账号数等权平均:5h remaining (80+40)/2=60%,used (20+60)/2=40%;
|
|
222
|
+
7d remaining (70+50)/2=60%,used (30+50)/2=40%。total = fiveHour 合并 used = 40%。 */
|
|
223
|
+
assert.equal(value.windows[0].name, '5 hours')
|
|
224
|
+
assert.equal(value.windows[0].remaining, 60, '5h 综合剩余 60%(不是单取任一账号 80/40)')
|
|
225
|
+
assert.equal(value.windows[0].used, 40)
|
|
226
|
+
assert.equal(value.windows[1].name, '7 days')
|
|
227
|
+
assert.equal(value.windows[1].remaining, 60, '7d 综合剩余 60%')
|
|
228
|
+
assert.equal(value.total, '40.00%', 'total 取 fiveHour 合并后的 used 40%')
|
|
229
|
+
/* 每个合并窗口带 items(逐账号明细):5h 两条,分别是 a 剩余 80、b 剩余 40 */
|
|
230
|
+
const fiveItems = value.windows[0].items
|
|
231
|
+
assert.ok(Array.isArray(fiveItems) && fiveItems.length === 2, '5h 窗口带 2 条账号明细')
|
|
232
|
+
assert.deepEqual(fiveItems.map((one) => one.name), ['a', 'b'], '5h 明细账号名')
|
|
233
|
+
assert.equal(fiveItems[0].remaining, 80, 'a 的 5h 剩余 80')
|
|
234
|
+
assert.equal(fiveItems[1].remaining, 40, 'b 的 5h 剩余 40')
|
|
235
|
+
/* 合并窗口带通用 key(建议修-3):fiveHour / sevenDay,供客户端本地化优先使用。 */
|
|
236
|
+
assert.equal(value.windows[0].key, 'fiveHour', '5h 合并窗口 key=fiveHour')
|
|
237
|
+
assert.equal(value.windows[1].key, 'sevenDay', '7d 合并窗口 key=sevenDay')
|
|
238
|
+
const sevenItems = value.windows[1].items
|
|
239
|
+
assert.ok(Array.isArray(sevenItems) && sevenItems.length === 2, '7d 窗口带 2 条账号明细')
|
|
240
|
+
assert.equal(sevenItems[1].remaining, 50, 'b 的 7d 剩余 50')
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
check('percent 多账号带 limit:按 limit 加权平均', () => {
|
|
244
|
+
const quotas = {
|
|
245
|
+
data: [
|
|
246
|
+
{ provider: 'p6', kind: 'subscription', user: 'a',
|
|
247
|
+
windows: [{ name: '5 hours', used: 0, remaining: 100, limit: 100 }] },
|
|
248
|
+
{ provider: 'p6', kind: 'subscription', user: 'b',
|
|
249
|
+
windows: [{ name: '5 hours', used: 50, remaining: 50, limit: 300 }] },
|
|
250
|
+
],
|
|
251
|
+
}
|
|
252
|
+
const value = magpieBalanceOf(quotas, { provider: 'p6' })
|
|
253
|
+
/* limit 加权:remaining = (100*100 + 50*300)/(100+300) = (10000+15000)/400 = 62.5 */
|
|
254
|
+
const win = value.windows[0]
|
|
255
|
+
assert.ok(Math.abs(win.remaining - 62.5) < 1e-9, 'remaining 按 limit 加权 = 62.5,实际 ' + win.remaining)
|
|
256
|
+
assert.ok(Math.abs(win.used - 37.5) < 1e-9, 'used 按 limit 加权 = 37.5')
|
|
257
|
+
assert.equal(value.total, '37.50%')
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
check('percent 指定 account 也只汇总全部账号(与 WorkBuddy 统一口径)', () => {
|
|
261
|
+
const quotas = {
|
|
262
|
+
data: [
|
|
263
|
+
{ provider: 'p7', kind: 'subscription', user: 'a',
|
|
264
|
+
windows: [{ name: '5 hours', used: 20, remaining: 80 }] },
|
|
265
|
+
{ provider: 'p7', kind: 'subscription', user: 'b',
|
|
266
|
+
windows: [{ name: '5 hours', used: 60, remaining: 40 }] },
|
|
267
|
+
],
|
|
268
|
+
}
|
|
269
|
+
/* options.account='a'(模拟 Magpie route 指定账号):percent 仍须汇总 a+b。 */
|
|
270
|
+
const value = magpieBalanceOf(quotas, { provider: 'p7', account: 'a' })
|
|
271
|
+
assert.equal(value.mode, 'percent')
|
|
272
|
+
assert.equal(value.windows[0].remaining, 60, '5h 综合剩余 (80+40)/2=60%,不受 account=a 影响')
|
|
273
|
+
assert.equal(value.total, '40.00%', 'total 取合并 used 40%')
|
|
274
|
+
assert.equal(value.windows[0].items.length, 2, 'items 仍包含 a 和 b 两个账号')
|
|
275
|
+
assert.deepEqual(value.windows[0].items.map((one) => one.name), ['a', 'b'])
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
check('percent 窗口:忽略 image-only 与文本占位窗口', () => {
|
|
279
|
+
const quotas = {
|
|
280
|
+
data: [{
|
|
281
|
+
provider: 'p1', kind: 'subscription', user: 'u',
|
|
282
|
+
windows: [
|
|
283
|
+
{ name: 'image gen', used: 80, remaining: 20 },
|
|
284
|
+
{ name: '5 hours', used: 12, remaining: 88 },
|
|
285
|
+
{ name: 'bad', used: 'x', remaining: null },
|
|
286
|
+
],
|
|
287
|
+
}],
|
|
288
|
+
}
|
|
289
|
+
const value = magpieBalanceOf(quotas, { provider: 'p1' })
|
|
290
|
+
assert.equal(value.mode, 'percent')
|
|
291
|
+
assert.equal(value.total, '12.00%', 'image-only 与坏数据被忽略,取真正的时间窗口')
|
|
292
|
+
})
|
|
293
|
+
|
|
294
|
+
check('credits 窗口优先于 percent 窗口(有 unit=credits 时不按百分比)', () => {
|
|
295
|
+
const quotas = {
|
|
296
|
+
data: [{
|
|
297
|
+
provider: 'p2', kind: 'subscription', user: 'u',
|
|
298
|
+
windows: [
|
|
299
|
+
{ name: 'Credits', used: 5, remaining: 95, limit: 100, amount: 5, unit: 'credits' },
|
|
300
|
+
{ name: '5 hours', used: 12, remaining: 88 },
|
|
301
|
+
],
|
|
302
|
+
}],
|
|
303
|
+
}
|
|
304
|
+
const value = magpieBalanceOf(quotas, { provider: 'p2' })
|
|
305
|
+
assert.equal(value.mode, 'credits')
|
|
306
|
+
assert.equal(value.isCredits, true)
|
|
307
|
+
assert.equal(value.isPercent, undefined)
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
check('模型 work/codex/gpt-6-luna → quotas provider work/codex(通用最长前缀匹配)', () => {
|
|
311
|
+
const quotas = { data: [
|
|
312
|
+
{ provider: 'work/codex', kind: 'subscription', windows: [{ name: '5 hours', used: 19, remaining: 81 }] },
|
|
313
|
+
{ provider: 'work', kind: 'subscription', windows: [] },
|
|
314
|
+
{ provider: 'other', kind: 'subscription', windows: [] },
|
|
315
|
+
] }
|
|
316
|
+
/* owned_by 是 work,前缀只取第一段也是 work,都不是 quotas 里的完整 provider;
|
|
317
|
+
最长前缀匹配应命中 work/codex。 */
|
|
318
|
+
assert.equal(magpieModelProvider({ data: [{ id: 'work/codex/gpt-6-luna', owned_by: 'work' }] }, 'work/codex/gpt-6-luna'), 'work')
|
|
319
|
+
assert.equal(magpieModelQuotaProvider(quotas, 'work/codex/gpt-6-luna'), 'work/codex')
|
|
320
|
+
assert.equal(magpieModelQuotaProvider(quotas, 'nope/x'), null)
|
|
321
|
+
assert.equal(magpieModelQuotaProvider(quotas, 'work/codex/gpt-6-luna/extra'), 'work/codex')
|
|
322
|
+
})
|
|
323
|
+
|
|
163
324
|
check('orbi 别名:magpieQuotaEntries 能命中 quotas 里的 orbis', () => {
|
|
164
325
|
const quotas = { data: [{ provider: 'orbis', name: 'orbis', kind: 'balance', windows: [], balance: '$1.97' }] }
|
|
165
326
|
const entries = magpieQuotaEntries(quotas, 'orbi')
|
|
@@ -380,6 +541,67 @@ check('queryGenericRelayBalance:dashboard 与 /v1/credits 两条兜底端点',
|
|
|
380
541
|
assert.equal(genericCredits.total, '5.00')
|
|
381
542
|
})
|
|
382
543
|
|
|
544
|
+
check('parseGenericRelayBalanceAny:{balance,unit} 无路径也能通用解析', () => {
|
|
545
|
+
const parsed = parseGenericRelayBalanceAny({ balance: 34.5, unit: 'USD' })
|
|
546
|
+
assert.equal(parsed.currency, 'USD')
|
|
547
|
+
assert.equal(parsed.total, '34.50')
|
|
548
|
+
assert.equal(parseGenericRelayBalanceAny({ nope: true }), null)
|
|
549
|
+
})
|
|
550
|
+
|
|
551
|
+
/* 配置了 endpoint、但 balancePath 解析不出来(默认 data.quota)→ 按同一响应通用回退为 34.50。
|
|
552
|
+
走 queryBalanceForTest,用一个临时 relay-balance.json 平台档案 + credentials。 */
|
|
553
|
+
let relayFallback = null
|
|
554
|
+
{
|
|
555
|
+
const relayFile = join(TEMP_HOME, 'dsh-status-bar', 'relay-balance.json')
|
|
556
|
+
const previous = readFileSync(relayFile, 'utf8')
|
|
557
|
+
writeFileSync(relayFile, JSON.stringify({
|
|
558
|
+
version: 2,
|
|
559
|
+
channels: [{
|
|
560
|
+
channelKey: 'compat-relay',
|
|
561
|
+
endpoint: 'https://compat.example/v1/balance',
|
|
562
|
+
tokenHeader: 'authorization', userIdHeader: 'x-user-id', tokenPrefix: 'Bearer ',
|
|
563
|
+
balancePath: 'data.quota', currency: 'CNY', feeMultiplier: 1, balanceDivisor: 1, headerNames: [],
|
|
564
|
+
}],
|
|
565
|
+
}), 'utf8')
|
|
566
|
+
const creds = { resolve: async () => ({ value: JSON.stringify({ token: 'tk', userId: 'u', headers: {} }), source: 'test' }), set: async () => {}, unset: async () => {} }
|
|
567
|
+
const relayCtx = { logger: { info: () => {} }, credentials: creds, get: (n) => (n === 'credentials' ? creds : null) }
|
|
568
|
+
const originalFetch = globalThis.fetch
|
|
569
|
+
globalThis.fetch = async (url) => {
|
|
570
|
+
const text = String(url)
|
|
571
|
+
if (text.indexOf('compat.example') >= 0) {
|
|
572
|
+
/* queryRelayBalance 走 readLimitedRelayJson:需要 headers.get(content-length) 与 reader.releaseLock。 */
|
|
573
|
+
const buf = Buffer.from(JSON.stringify({ balance: 34.5, unit: 'USD' }), 'utf8')
|
|
574
|
+
return {
|
|
575
|
+
ok: true, status: 200,
|
|
576
|
+
headers: { get: (name) => (String(name).toLowerCase() === 'content-length' ? String(buf.byteLength) : null) },
|
|
577
|
+
body: {
|
|
578
|
+
getReader() {
|
|
579
|
+
let done = false
|
|
580
|
+
return {
|
|
581
|
+
async read() { if (done) return { done: true }; done = true; return { done: false, value: new Uint8Array(buf) } },
|
|
582
|
+
async cancel() {},
|
|
583
|
+
releaseLock() {},
|
|
584
|
+
}
|
|
585
|
+
},
|
|
586
|
+
},
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
throw new Error('unexpected url ' + text)
|
|
590
|
+
}
|
|
591
|
+
try {
|
|
592
|
+
relayFallback = await queryBalanceForTest(relayCtx, false, 'compat-relay', 'm', 'compat-relay', '')
|
|
593
|
+
} finally {
|
|
594
|
+
globalThis.fetch = originalFetch
|
|
595
|
+
writeFileSync(relayFile, previous, 'utf8')
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
check('queryRelayBalance:balancePath=data.quota 解析失败 → 通用形状回退为 34.50', () => {
|
|
599
|
+
assert.equal(relayFallback.ok, true)
|
|
600
|
+
assert.equal(relayFallback.source, 'relay')
|
|
601
|
+
assert.equal(relayFallback.total, '34.50')
|
|
602
|
+
assert.equal(relayFallback.currency, 'USD')
|
|
603
|
+
})
|
|
604
|
+
|
|
383
605
|
/* queryBalance 接线:没有专用适配器的 provider 先走 settings + credentials + 通用探测,命中即返回。 */
|
|
384
606
|
let genericWired = null
|
|
385
607
|
{
|
|
@@ -421,6 +643,111 @@ check('queryBalance:通用中转站命中时直接返回(不依赖 Magpie)
|
|
|
421
643
|
assert.equal(genericWired.total, '7.00')
|
|
422
644
|
})
|
|
423
645
|
|
|
646
|
+
/* provider 配置缺失(settings 里没有该 provider)时,用 cordis.patch.yml 的 baseURL + 引用名 key 兜底探测。
|
|
647
|
+
patch 文件在 DSH_HOME/dsh-status-bar/... 由 providerBaseUrls() 读取;这里写一份临时 patch 验证取到 baseURL。 */
|
|
648
|
+
let genericPatchFallback = null
|
|
649
|
+
{
|
|
650
|
+
/* providerBaseUrls() 只在存在当前 profile 目录时读它的 cordis.patch.yml:
|
|
651
|
+
用 DSH_PROFILE 指定一个临时 profile,把 patch 写进 <DSH_HOME>/profiles/<name>/。 */
|
|
652
|
+
const profileName = 'testprofile'
|
|
653
|
+
const patchDir = join(TEMP_HOME, 'profiles', profileName)
|
|
654
|
+
mkdirSync(patchDir, { recursive: true })
|
|
655
|
+
process.env.DSH_PROFILE = profileName
|
|
656
|
+
writeFileSync(join(patchDir, 'cordis.patch.yml'), [
|
|
657
|
+
'- id: llm-pi-ai',
|
|
658
|
+
' config:',
|
|
659
|
+
' providers:',
|
|
660
|
+
' patchrelay:',
|
|
661
|
+
' baseURL: https://patchrelay.example/v1',
|
|
662
|
+
'',
|
|
663
|
+
].join('\n'), 'utf8')
|
|
664
|
+
const creds = {
|
|
665
|
+
resolve: async (ref) => (ref === 'PATCHRELAY_API_KEY' ? { value: 'pk', source: 'test' } : undefined),
|
|
666
|
+
set: async () => {}, unset: async () => {},
|
|
667
|
+
}
|
|
668
|
+
const patchCtx = { logger: { info: () => {} }, credentials: creds, get: (n) => (n === 'credentials' ? creds : null) }
|
|
669
|
+
const originalFetch = globalThis.fetch
|
|
670
|
+
const urls = []
|
|
671
|
+
globalThis.fetch = async (url) => {
|
|
672
|
+
urls.push(String(url))
|
|
673
|
+
if (String(url).indexOf('patchrelay.example') >= 0) return reply({ balance: 12, unit: 'USD' })
|
|
674
|
+
throw new Error('unexpected url ' + String(url))
|
|
675
|
+
}
|
|
676
|
+
try {
|
|
677
|
+
genericPatchFallback = await queryBalanceForTest(patchCtx, false, 'patchrelay', 'm', null, '')
|
|
678
|
+
} finally {
|
|
679
|
+
globalThis.fetch = originalFetch
|
|
680
|
+
delete process.env.DSH_PROFILE
|
|
681
|
+
}
|
|
682
|
+
assert.equal(urls[0], 'https://patchrelay.example/v1/usage', '走 patch baseURL 的通用探测')
|
|
683
|
+
}
|
|
684
|
+
check('queryBalance:provider 配置缺失时用 baseURL/key 兜底(不回归)', () => {
|
|
685
|
+
assert.equal(genericPatchFallback.ok, true)
|
|
686
|
+
assert.equal(genericPatchFallback.source, 'generic-relay')
|
|
687
|
+
assert.equal(genericPatchFallback.total, '12.00')
|
|
688
|
+
})
|
|
689
|
+
|
|
690
|
+
/* Magpie 透传:直连失败、但 quotas 有该 provider(按 providerHint 命中),返回 magpie-quotas 余额。 */
|
|
691
|
+
let relayPassthrough = null
|
|
692
|
+
let relayNoMagpie = null
|
|
693
|
+
{
|
|
694
|
+
const relayFile = join(TEMP_HOME, 'dsh-status-bar', 'relay-balance.json')
|
|
695
|
+
const previous = readFileSync(relayFile, 'utf8')
|
|
696
|
+
writeFileSync(relayFile, JSON.stringify({
|
|
697
|
+
version: 2,
|
|
698
|
+
channels: [{
|
|
699
|
+
channelKey: 'relay-up', endpoint: 'https://rm.example/v1/balance',
|
|
700
|
+
tokenHeader: 'authorization', userIdHeader: 'x-user-id', tokenPrefix: 'Bearer ',
|
|
701
|
+
balancePath: 'data.quota', currency: 'CNY', feeMultiplier: 1, balanceDivisor: 1, headerNames: [],
|
|
702
|
+
}],
|
|
703
|
+
}), 'utf8')
|
|
704
|
+
const creds = { resolve: async () => ({ value: JSON.stringify({ token: 'tk', userId: 'u', headers: {} }), source: 'test' }), set: async () => {}, unset: async () => {} }
|
|
705
|
+
const rctx = { logger: { info: () => {} }, credentials: creds, get: (n) => (n === 'credentials' ? creds : null) }
|
|
706
|
+
const QUOTA_PCT = { data: [{ provider: 'relay-up', kind: 'subscription', user: 'u', windows: [{ name: '5 hours', used: 30, remaining: 70 }, { name: '7 days', used: 60, remaining: 40 }] }] }
|
|
707
|
+
const directFail = (url) => {
|
|
708
|
+
const text = String(url)
|
|
709
|
+
if (text.indexOf('rm.example') >= 0) return { ok: true, status: 200, headers: { get: () => null }, body: { getReader() { let d = false; const buf = Buffer.from(JSON.stringify({ nope: 1 })); return { async read() { if (d) return { done: true }; d = true; return { done: false, value: new Uint8Array(buf) } }, async cancel() {}, releaseLock() {} } } } }
|
|
710
|
+
return null
|
|
711
|
+
}
|
|
712
|
+
const originalFetch = globalThis.fetch
|
|
713
|
+
/* 1) 直连失败 + Magpie 有该 provider(providerHint=relay-magpie 命中 quotas) */
|
|
714
|
+
globalThis.fetch = async (url) => {
|
|
715
|
+
const text = String(url)
|
|
716
|
+
if (text.indexOf('/v1/magpie/quotas') >= 0) return reply(QUOTA_PCT)
|
|
717
|
+
if (text.indexOf('/v1/magpie/route') >= 0) return reply({ route: null })
|
|
718
|
+
if (text.indexOf('/v1/models') >= 0) return reply({ data: [] })
|
|
719
|
+
return directFail(url)
|
|
720
|
+
}
|
|
721
|
+
try {
|
|
722
|
+
relayPassthrough = await queryBalanceForTest(rctx, false, 'relay-up', 'm', 'relay-up', '')
|
|
723
|
+
} finally {
|
|
724
|
+
/* 2) 直连失败 + Magpie 没有该 provider → 原 relay 失败 */
|
|
725
|
+
globalThis.fetch = async (url) => {
|
|
726
|
+
const text = String(url)
|
|
727
|
+
if (text.indexOf('/v1/magpie/quotas') >= 0) return reply({ data: [] })
|
|
728
|
+
if (text.indexOf('/v1/magpie/route') >= 0) return reply({ route: null })
|
|
729
|
+
if (text.indexOf('/v1/models') >= 0) return reply({ data: [] })
|
|
730
|
+
return directFail(url)
|
|
731
|
+
}
|
|
732
|
+
try {
|
|
733
|
+
relayNoMagpie = await queryBalanceForTest(rctx, false, 'relay-up', 'm', 'relay-up', '')
|
|
734
|
+
} finally {
|
|
735
|
+
globalThis.fetch = originalFetch
|
|
736
|
+
writeFileSync(relayFile, previous, 'utf8')
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
check('queryBalance:直连失败但 Magpie 有余额 → 透传 magpie-quotas(viaMagpie)', () => {
|
|
741
|
+
assert.equal(relayPassthrough.ok, true)
|
|
742
|
+
assert.equal(relayPassthrough.source, 'magpie-quotas')
|
|
743
|
+
assert.equal(relayPassthrough.upstream, 'relay-up')
|
|
744
|
+
assert.equal(relayPassthrough.isPercent, true)
|
|
745
|
+
assert.equal(relayPassthrough.viaMagpie, true, '透传结果打 viaMagpie:true(source 仍 magpie-quotas)')
|
|
746
|
+
})
|
|
747
|
+
check('queryBalance:直连失败且 Magpie 无该 provider → 原 relay 失败(不伪造)', () => {
|
|
748
|
+
assert.equal(relayNoMagpie.ok, false)
|
|
749
|
+
})
|
|
750
|
+
|
|
424
751
|
/* --------------------------------------- ② queryMagpieBalance 编排(替换 fetch) */
|
|
425
752
|
|
|
426
753
|
/* 只测编排:把 globalThis.fetch 临时换掉,按 URL 返回,跑完恢复原 fetch。 */
|
package/tests/verify-prices.js
CHANGED
|
@@ -161,7 +161,7 @@ const V7_FABLE = {
|
|
|
161
161
|
};
|
|
162
162
|
const v7 = boot({ version: 6, priceVersion: 7, currency: 'CNY', models: { 'claude-fable-5.1': V7_FABLE } });
|
|
163
163
|
const v7Cfg = v7.loadConfig();
|
|
164
|
-
assert.strictEqual(v7Cfg.priceVersion, api.PRICE_VERSION, '2d) priceVersion 7
|
|
164
|
+
assert.strictEqual(v7Cfg.priceVersion, api.PRICE_VERSION, '2d) priceVersion 7 升到当前版本(10)');
|
|
165
165
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(v7Cfg.models['claude-fable-5.2'])), {
|
|
166
166
|
/* v8 起官方 USD 源表入价格库时:显示值按实时汇率折成 CNY,基准价保留官网 USD 原值 */
|
|
167
167
|
tiered: false, currency: 'CNY',
|
|
@@ -590,7 +590,9 @@ assert.equal(fetched.length, magpieMark + 2, 'Magpie 走 quotas + models 两个
|
|
|
590
590
|
const costAfterDebt = await dayCostNow();
|
|
591
591
|
assert.equal(costAfterDebt - costWithDebt, 4, 'settled debt adds the multiplied amount to day cost');
|
|
592
592
|
|
|
593
|
-
/* Tolerant model-name matching: provider prefix + separator variants hit the same price entry.
|
|
593
|
+
/* Tolerant model-name matching: provider prefix + separator variants hit the same price entry.
|
|
594
|
+
remote-magpie/ 前缀归 Magpie,先清空通道配置让倍率=1,避免残留配置干扰命中价断言。 */
|
|
595
|
+
await removeMagpie();
|
|
594
596
|
const tolerantBefore = await dayCostNow();
|
|
595
597
|
priceBook = {
|
|
596
598
|
...priceBook,
|
|
@@ -902,6 +904,69 @@ assert.equal(fetched.length, magpieMark + 2, 'Magpie 走 quotas + models 两个
|
|
|
902
904
|
assert.equal(magpieRes.total.cost, 6, '恢复显式 Magpie 倍率 3 后准确乘算 2 × 3 = 6');
|
|
903
905
|
await postMagpie(1.5);
|
|
904
906
|
|
|
907
|
+
/* ---- Magpie 主会话(非子代理):走通用 tokenCost 也必须按「缺倍率默认 1」计价 ---- */
|
|
908
|
+
/* 用户报的缺陷:主会话里 provider=magpie / 模型 remote-magpie/ 时,providerAffiliation 判为 relay,
|
|
909
|
+
tokenCost 走「缺 feeConfigured 就返回 0 / 记欠账」,于是走 Magpie 的调用有时不被计费。
|
|
910
|
+
修复后:Magpie 归属缺倍率默认 1;配置了通道倍率 / 条目倍率仍照乘。 */
|
|
911
|
+
priceBook = Object.assign({}, priceBook, {
|
|
912
|
+
'magpie-main-model': plainTierPrice(2, 0),
|
|
913
|
+
'magpie-main-entry': Object.assign(tierPrice(1, 0), { feeMultiplier: 2 }),
|
|
914
|
+
});
|
|
915
|
+
await postPrices(priceBook);
|
|
916
|
+
const magpieDayCost = async () => {
|
|
917
|
+
const rep = await daily();
|
|
918
|
+
const d = rep.body.days.find((entry) => entry.date === DAY);
|
|
919
|
+
return d === undefined ? 0 : d.cost;
|
|
920
|
+
};
|
|
921
|
+
/* a) 主会话 provider=magpie,无任何倍率配置 ⇒ 按模型价 2 计(非零,且 ≠ 欠账) */
|
|
922
|
+
await removeMagpie();
|
|
923
|
+
let before = await magpieDayCost();
|
|
924
|
+
writeSession('main-magpie-nocfg', null, [assistantMessage(1, OFF_PEAK_AT, 'magpie', 'magpie-main-model', M)]);
|
|
925
|
+
let after = await magpieDayCost();
|
|
926
|
+
assert.equal(after - before, 2, '主会话 Magpie 无配置:按模型价 2 计(修复前为 0)');
|
|
927
|
+
{ const diskDay = JSON.parse(fs.readFileSync(LEDGER_FILE, 'utf8')).days[DAY] || {};
|
|
928
|
+
assert.ok((diskDay.unpricedByProvider || []).every((one) => one.model !== 'magpie-main-model'),
|
|
929
|
+
'主会话 Magpie 无配置不再挂欠账'); }
|
|
930
|
+
|
|
931
|
+
/* b) remote-magpie/ 是局域网 Magpie 的模型命名空间:即使 provider 为空,模型前缀本身
|
|
932
|
+
也归 Magpie,按模型价计(非零)、不挂欠账。不再降级成通用 relay。 */
|
|
933
|
+
await removeMagpie();
|
|
934
|
+
before = await magpieDayCost();
|
|
935
|
+
writeSession('main-prefix-magpie', null, [assistantMessage(1, OFF_PEAK_AT, '', 'remote-magpie/group/magpie-main-model', M)]);
|
|
936
|
+
after = await magpieDayCost();
|
|
937
|
+
assert.equal(after - before, 2, 'provider 为空 + remote-magpie/ 前缀:按 Magpie 模型价 2 计(非零)');
|
|
938
|
+
/* 欠账读台账文件(/api/daily-usage 的响应体不暴露这类欠账,与其它审计用例一致从盘上读)。 */
|
|
939
|
+
const prefixDiskDay = JSON.parse(fs.readFileSync(LEDGER_FILE, 'utf8')).days[DAY] || {};
|
|
940
|
+
assert.ok((prefixDiskDay.unpricedByProvider || []).every((one) => one.model !== 'remote-magpie/group/magpie-main-model'),
|
|
941
|
+
'provider 为空 + remote-magpie/ 前缀不再挂欠账');
|
|
942
|
+
|
|
943
|
+
/* c) 配置 Magpie 通道倍率 2 ⇒ 2 × 2 = 4(配置必须生效) */
|
|
944
|
+
await removeMagpie();
|
|
945
|
+
await postMagpie(2);
|
|
946
|
+
before = await magpieDayCost();
|
|
947
|
+
writeSession('main-magpie-chan2', null, [assistantMessage(1, OFF_PEAK_AT, 'magpie', 'magpie-main-model', M)]);
|
|
948
|
+
after = await magpieDayCost();
|
|
949
|
+
assert.equal(after - before, 4, '主会话 Magpie 通道倍率 2 ⇒ 2 × 2 = 4');
|
|
950
|
+
|
|
951
|
+
/* d) 通道倍率 1.5 × 价格条目 feeMultiplier 2 × 模型价 1 = 3(与子代理口径一致) */
|
|
952
|
+
await removeMagpie();
|
|
953
|
+
await postMagpie(1.5);
|
|
954
|
+
before = await magpieDayCost();
|
|
955
|
+
writeSession('main-magpie-entry', null, [assistantMessage(1, OFF_PEAK_AT, 'magpie', 'magpie-main-entry', M)]);
|
|
956
|
+
after = await magpieDayCost();
|
|
957
|
+
assert.equal(after - before, 3, '主会话 Magpie 通道 1.5 × 条目 2 × 模型价 1 = 3');
|
|
958
|
+
|
|
959
|
+
/* e) 反证:普通自建 relay(plain-provider)主会话缺 feeConfigured 仍记 0 / 欠账(行为不许改) */
|
|
960
|
+
await removeMagpie();
|
|
961
|
+
before = await magpieDayCost();
|
|
962
|
+
writeSession('main-plain-relay', null, [assistantMessage(1, OFF_PEAK_AT, 'plain-provider', 'plain-unpriced-model', M)]);
|
|
963
|
+
after = await magpieDayCost();
|
|
964
|
+
assert.equal(after - before, 0, '普通 relay 主会话缺倍率仍为 0(不按官方价误计)');
|
|
965
|
+
{ const diskDay = JSON.parse(fs.readFileSync(LEDGER_FILE, 'utf8')).days[DAY] || {};
|
|
966
|
+
assert.ok((diskDay.unpricedByProvider || []).some((one) => one.provider === 'plain-provider' && one.model === 'plain-unpriced-model'),
|
|
967
|
+
'普通 relay 主会话缺倍率仍记欠账(行为不变)'); }
|
|
968
|
+
|
|
969
|
+
console.log('Magpie 主会话: 无配置按模型价非零 / remote-magpie 前缀同上 / 通道与条目倍率照乘 / 普通 relay 缺倍率仍 0 且记欠账。');
|
|
905
970
|
console.log('Magpie subagent: 默认模型价非零 / 无配置默认 1 倍 / 显式通道与条目倍率准确 / 普通 relay 缺倍率仍 0。');
|
|
906
971
|
console.log('PROVIDER FEE CHECKS PASSED');
|
|
907
972
|
console.log('Projection: message/attempt/summary providers, missing-provider defaults, provider-only calls (no event multiplier), replay version.');
|
|
@@ -305,7 +305,7 @@ console.log('\n===== ② 官方目录:默认全部已加入(价格卡),
|
|
|
305
305
|
const models = api.snapshot().models;
|
|
306
306
|
|
|
307
307
|
/* 播种:官方模型全部进价格库;已有条目一律不动 */
|
|
308
|
-
assert.strictEqual(api.snapshot().priceVersion,
|
|
308
|
+
assert.strictEqual(api.snapshot().priceVersion, 10, '迁移后价格库版本应为 10');
|
|
309
309
|
CATALOG_NAMES.forEach((name) => {
|
|
310
310
|
assert.ok(models[name] !== undefined, '官方模型应默认加入价格库:' + name);
|
|
311
311
|
});
|
|
@@ -355,7 +355,7 @@ console.log('\n===== ② 官方目录:默认全部已加入(价格卡),
|
|
|
355
355
|
const migrated = loadClient(undefined, [[CONFIG_KEY, RETIRED_CONFIG]]);
|
|
356
356
|
const migratedApi = migrated.api;
|
|
357
357
|
const migratedModels = migratedApi.snapshot().models;
|
|
358
|
-
assert.strictEqual(migratedApi.snapshot().priceVersion,
|
|
358
|
+
assert.strictEqual(migratedApi.snapshot().priceVersion, 10, '旧配置迁移后价格库版本应为 10');
|
|
359
359
|
assert.strictEqual(migratedModels['GLM-5.2'], undefined, '已下架官方模型迁移时应被清理');
|
|
360
360
|
assert.strictEqual(migratedModels['muse-spark-1.1'], undefined, '小写 muse 键同样按确切名字清理');
|
|
361
361
|
assert.ok(migratedModels['my-model'] !== undefined, '真正自建条目必须保留');
|
|
@@ -374,7 +374,7 @@ console.log('\n===== ② 官方目录:默认全部已加入(价格卡),
|
|
|
374
374
|
assert.ok(JSON.stringify(savedModels(migrated)).length > 0 && migratedBefore.length > 0, '配置非空');
|
|
375
375
|
|
|
376
376
|
|
|
377
|
-
/* v6/v8 补播:老用户的 priceVersion=5 价格库(官方目录
|
|
377
|
+
/* v6/v8/v10 补播:老用户的 priceVersion=5 价格库(官方目录 29 条里缺 kimi-k3)→ 自动补上 kimi-k3;
|
|
378
378
|
已有条目一律不动;已下架模型不会被补回来。 */
|
|
379
379
|
const catalogModelOf = (name) => {
|
|
380
380
|
for (let i = 0; i < CATALOG_VENDORS.length; i += 1) {
|
|
@@ -408,7 +408,7 @@ console.log('\n===== ② 官方目录:默认全部已加入(价格卡),
|
|
|
408
408
|
});
|
|
409
409
|
const backfilled = loadClient(undefined, [[CONFIG_KEY, V5_CONFIG]]);
|
|
410
410
|
const backfilledModels = backfilled.api.snapshot().models;
|
|
411
|
-
assert.strictEqual(backfilled.api.snapshot().priceVersion,
|
|
411
|
+
assert.strictEqual(backfilled.api.snapshot().priceVersion, 10, 'v5 → v10 迁移后版本号升到 10');
|
|
412
412
|
assert.deepStrictEqual(plain(backfilledModels['kimi-k3']), {
|
|
413
413
|
tiered: false, currency: 'CNY',
|
|
414
414
|
peak: { input: 20, cacheRead: 2, cacheWrite: 20, output: 100 },
|
|
@@ -430,13 +430,13 @@ console.log('\n===== ② 官方目录:默认全部已加入(价格卡),
|
|
|
430
430
|
}, 'v6 补播不动自建条目');
|
|
431
431
|
assert.strictEqual(backfilledModels['GLM-5.2'], undefined, 'v6 补播不会把已下架的官方模型加回来');
|
|
432
432
|
assert.deepStrictEqual(Object.keys(backfilledModels).sort(), CATALOG_NAMES.concat(['my-model']).sort(),
|
|
433
|
-
'v6/v8 补播只补当前官方目录缺失项:
|
|
433
|
+
'v6/v8/v10 补播只补当前官方目录缺失项:29 条官方 + 1 条自建');
|
|
434
434
|
/* 二次加载:版本已是 6,不再补播、也不改动 */
|
|
435
435
|
const reloaded = loadClient(undefined, [[CONFIG_KEY, backfilled.store.get(CONFIG_KEY)]]);
|
|
436
|
-
assert.strictEqual(reloaded.api.snapshot().priceVersion,
|
|
436
|
+
assert.strictEqual(reloaded.api.snapshot().priceVersion, 10, '已迁移配置再次加载不回退版本');
|
|
437
437
|
assert.deepStrictEqual(Object.keys(reloaded.api.snapshot().models).sort(), CATALOG_NAMES.concat(['my-model']).sort(),
|
|
438
438
|
'二次加载模型集合不变(不会重复补播)');
|
|
439
|
-
console.log(' v6/v8 补播:v5 价格库自动补上 kimi-k3(
|
|
439
|
+
console.log(' v6/v8/v10 补播:v5 价格库自动补上 kimi-k3(29 条),用户条目不动、已下架模型不回流');
|
|
440
440
|
|
|
441
441
|
|
|
442
442
|
/* v7 迁移:缺 base 的旧条目补「当前值 + 当前币种」,数值一字不动 */
|
|
@@ -450,7 +450,7 @@ console.log('\n===== ② 官方目录:默认全部已加入(价格卡),
|
|
|
450
450
|
});
|
|
451
451
|
const noBase = loadClient(undefined, [[CONFIG_KEY, NO_BASE_CONFIG]]);
|
|
452
452
|
const noBaseModels = noBase.api.snapshot().models;
|
|
453
|
-
assert.strictEqual(noBase.api.snapshot().priceVersion,
|
|
453
|
+
assert.strictEqual(noBase.api.snapshot().priceVersion, 10, 'v6 → v10 迁移后版本号升到 10');
|
|
454
454
|
assert.deepStrictEqual(plain(noBaseModels['my-usd']), {
|
|
455
455
|
/* 3.0.4 起:USD 显示值折成 CNY(4 × 6.742424 = 26.97 …),原始 USD 值进 base。 */
|
|
456
456
|
tiered: false, currency: 'CNY',
|
package/tests/verify-render.js
CHANGED
|
@@ -370,6 +370,20 @@ function renderTree() {
|
|
|
370
370
|
assert.ok(reduced[0].includes('.dsb-ring-team') && reduced[0].includes('.dsb-ring-deep'),
|
|
371
371
|
'reduced-motion 那条必须同时包含蓝档与紫档(与绿色同待遇,不能只停一个):' + (reduced === null ? '' : reduced[0]));
|
|
372
372
|
|
|
373
|
+
/* ⑧-4b 环心占用数字的 CSS 必须存在:字号 / 颜色 / pointer-events / 居中都不可少(HTML 覆盖层方案) */
|
|
374
|
+
assert.ok(/\.dsb-ring-percent\{[^}]*pointer-events:none/.test(cssSource),
|
|
375
|
+
'环心数字 .dsb-ring-percent 必须 pointer-events:none(否则挡住圆环按钮的点击/悬停)');
|
|
376
|
+
assert.ok(/\.dsb-ring-percent\{[^}]*font-size:12px/.test(cssSource), '环心数字必须显式给字号 12px(宿主最小字号放大后靠 transform scale(0.5) 视觉仍是 6px)');
|
|
377
|
+
assert.ok(/\.dsb-ring-percent\{[^}]*color:/.test(cssSource), '环心数字必须有显式颜色(跟随主题文字色)');
|
|
378
|
+
assert.ok(/\.dsb-ring-percent\{[^}]*transform:scale\(0?\.5\)/.test(cssSource), '环心数字 transform 必须是 scale(.5)(视觉缩到 6px,不再带 translateY 补偿)');
|
|
379
|
+
assert.strictEqual(/translateY/.test(cssSource.match(/\.dsb-ring-percent\{[^}]*\}/)[0]), false, '环心数字不再含 translateY(之前偏上,已去掉负的向下补偿)');
|
|
380
|
+
assert.ok(/\.dsb-ring-percent\{[^}]*transform-origin:center center/.test(cssSource), '环心数字 transform-origin 必须是 center center');
|
|
381
|
+
assert.ok(/\.dsb-ring-percent\{[^}]*display:flex[^}]*align-items:center[^}]*justify-content:center/.test(cssSource),
|
|
382
|
+
'环心数字必须用 flex 强制水平+垂直居中(不依赖 SVG 基线)');
|
|
383
|
+
assert.ok(/\.dsb-ring-wrap\{[^}]*position:relative/.test(cssSource), '外层 .dsb-ring-wrap 必须是相对定位,作为数字覆盖层的定位基准');
|
|
384
|
+
assert.ok(/\.dsb-ring-wrap\{[^}]*width:14px/.test(cssSource), '外层 .dsb-ring-wrap 尺寸固定 14px');
|
|
385
|
+
assert.ok(/\.dsb-ring\{[^}]*position:absolute[^}]*inset:0/.test(cssSource), '内层 .dsb-ring SVG 绝对铺满外层');
|
|
386
|
+
|
|
373
387
|
/* ⑧-5 弧长:teamRunning 用成员级最大(55),不是 Lead 自己的占用 */
|
|
374
388
|
const circumference = api.METER_CIRCUMFERENCE;
|
|
375
389
|
const percentToOffset = (pc) => circumference * (1 - pc / 100);
|
package/tests/verify-tips.js
CHANGED
|
@@ -70,6 +70,8 @@ const TEMPLATES = {
|
|
|
70
70
|
segTps: '输出速度',
|
|
71
71
|
segSessionTime: '运行用时',
|
|
72
72
|
segBalance: '余额',
|
|
73
|
+
quotaFiveHour: '5 小时额度',
|
|
74
|
+
quotaSevenDay: '7 天额度',
|
|
73
75
|
tipModelTime: '模型用时',
|
|
74
76
|
tipToolTime: '工具调用',
|
|
75
77
|
tipWaitTime: '等待用时',
|
|
@@ -570,6 +572,91 @@ assert.strictEqual(quota.segments[1].width, 62.5, '已用那段宽 = 已用百
|
|
|
570
572
|
assert.strictEqual(quota.segments[0].width + quota.segments[1].width, 100, '两段加起来正好 100%');
|
|
571
573
|
console.log('余额气泡:', values(gifted.rows).join(' / '));
|
|
572
574
|
|
|
575
|
+
/* --------------------------------------------- 订阅配额窗口(五小时/七天)→ quota 气泡
|
|
576
|
+
* 用户要求:底栏默认显示五小时额度;点击底栏出现五小时和七天额度进度条。
|
|
577
|
+
* 数据来自 host 的 balance.windows(percent 模式携带完整窗口)。这里断言:
|
|
578
|
+
* ① 底栏文字仍按 total(已用→剩余)显示,不受窗口影响;
|
|
579
|
+
* ② tip.kind === 'quota',面板两个窗口各一行(本地化名称 + 剩余百分比);
|
|
580
|
+
* ③ 复用 creditsPanel 渲染出两行两根进度条(灰=剩余、透明=已用)。 */
|
|
581
|
+
const quotaWinView = api.segmentView('balance', Object.assign({}, src, {
|
|
582
|
+
balance: {
|
|
583
|
+
ok: true, isPercent: true, total: '19.00%', currency: '',
|
|
584
|
+
windows: [
|
|
585
|
+
{ key: 'fiveHour', name: '5 hours', used: 19, remaining: 81, items: [
|
|
586
|
+
{ name: 'acct-a', used: 10, remaining: 90 }, { name: 'acct-b', used: 28, remaining: 72 }
|
|
587
|
+
] },
|
|
588
|
+
{ key: 'sevenDay', name: '7 days', used: 45, remaining: 55, items: [
|
|
589
|
+
{ name: 'acct-a', used: 40, remaining: 60 }, { name: 'acct-b', used: 50, remaining: 50 }
|
|
590
|
+
] }
|
|
591
|
+
]
|
|
592
|
+
}
|
|
593
|
+
}), t);
|
|
594
|
+
assert.strictEqual(quotaWinView.text, '余额 81.00%', '底栏文字仍按 total 剩余显示(100-19=81%)');
|
|
595
|
+
assert.strictEqual(quotaWinView.tip.kind, 'quota', '有 windows 的配额 → tip.kind=quota');
|
|
596
|
+
const qp = quotaWinView.tip.panel;
|
|
597
|
+
assert.strictEqual(qp.rewards.length, 2, '两个窗口两行');
|
|
598
|
+
assert.deepStrictEqual(Array.from(qp.rewards, (one) => one.label), ['5 小时额度', '7 天额度'], '五小时/七天窗口名本地化');
|
|
599
|
+
assert.strictEqual(qp.rewards[0].valueText, '81.00%', '五小时剩余 81%');
|
|
600
|
+
/* 每个 reward 带 name=本地化 label(下钻子视图标题用它);totalText 填第一行剩余(不再空串)。 */
|
|
601
|
+
assert.strictEqual(qp.rewards[0].name, '5 小时额度', 'reward.name = 本地化 label(下钻标题用)');
|
|
602
|
+
assert.strictEqual(qp.rewards[1].name, '7 天额度', 'reward.name = 本地化 label(7 天)');
|
|
603
|
+
assert.strictEqual(qp.totalText, '81.00%', 'totalText 填第一行剩余(此前空串)');
|
|
604
|
+
assert.strictEqual(qp.rewards[1].valueText, '55.00%', '七天剩余 55%');
|
|
605
|
+
assert.strictEqual(qp.rewards[1].percent, 45, '七天已用 45%(进度条宽度口径)');
|
|
606
|
+
/* 建议修-3:优先按 key 本地化 —— 即使 name 不含 5/hour/7/day,只要 key=fiveHour/sevenDay 也能本地化。 */
|
|
607
|
+
const keyOnly = api.segmentView('balance', Object.assign({}, src, {
|
|
608
|
+
balance: { ok: true, isPercent: true, total: '10.00%', currency: '', windows: [
|
|
609
|
+
{ key: 'fiveHour', name: 'rolling', used: 10, remaining: 90 },
|
|
610
|
+
{ key: 'sevenDay', name: 'weekly', used: 20, remaining: 80 }
|
|
611
|
+
] }
|
|
612
|
+
}), t).tip.panel;
|
|
613
|
+
assert.strictEqual(keyOnly.rewards[0].label, '5 小时额度', 'key=fiveHour 优先本地化(name=rolling 也认)');
|
|
614
|
+
assert.strictEqual(keyOnly.rewards[1].label, '7 天额度', 'key=sevenDay 优先本地化(name=weekly 也认)');
|
|
615
|
+
assert.strictEqual(keyOnly.totalText, '90.00%', 'key 路径下 totalText = 第一行剩余 90%');
|
|
616
|
+
/* 每个 reward 带 count/items:多账号下钻用。5h 两条账号明细。 */
|
|
617
|
+
assert.strictEqual(qp.rewards[0].count, 2, '5h 聚合行 count=2(可下钻)');
|
|
618
|
+
assert.strictEqual(qp.rewards[1].count, 2, '7d 聚合行 count=2');
|
|
619
|
+
assert.strictEqual(qp.rewards[0].items.length, 2, '5h 带 2 条账号明细');
|
|
620
|
+
assert.deepStrictEqual(Array.from(qp.rewards[0].items, (one) => one.label), ['acct-a', 'acct-b'], '5h 明细账号名');
|
|
621
|
+
assert.strictEqual(qp.rewards[0].items[0].valueText, '90.00%', 'acct-a 5h 剩余 90%');
|
|
622
|
+
assert.strictEqual(qp.rewards[0].items[1].valueText, '72.00%', 'acct-b 5h 剩余 72%');
|
|
623
|
+
assert.strictEqual(qp.rewards[1].items[1].valueText, '50.00%', 'acct-b 7d 剩余 50%');
|
|
624
|
+
/* 渲染路径:tipPanelInner 的 quota 分支直接调 creditsPanel —— 这里同样直接渲染,验两行两根 bar。 */
|
|
625
|
+
const quotaPanelNode = api.creditsPanel(qp, '余额', { left: 0, top: 0 }, () => ({}), { id: null, open() {}, back() {} });
|
|
626
|
+
const qbars = [];
|
|
627
|
+
const qrows = [];
|
|
628
|
+
(function walk(node) {
|
|
629
|
+
if (node === null || node === undefined || node === false) return;
|
|
630
|
+
if (Array.isArray(node)) { node.forEach(walk); return }
|
|
631
|
+
if (typeof node !== 'object') return;
|
|
632
|
+
if (node.props !== undefined && typeof node.props.className === 'string' && node.props.className.split(' ').indexOf('dsb-bar') >= 0) qbars.push(node);
|
|
633
|
+
if (node.props !== undefined && typeof node.props.className === 'string' && node.props.className.split(' ').indexOf('dsb-meter-row') >= 0) qrows.push(node);
|
|
634
|
+
(node.children || []).forEach(walk);
|
|
635
|
+
})(quotaPanelNode);
|
|
636
|
+
assert.strictEqual(qrows.length, 2, '渲染出两行(五小时/七天),实际 ' + qrows.length);
|
|
637
|
+
assert.strictEqual(qbars.length, 2, '渲染出两根进度条,实际 ' + qbars.length);
|
|
638
|
+
/* 第一根(五小时):灰剩余 81% 在左、透明已用 19% 在右 */
|
|
639
|
+
const q0 = (qbars[0].children || []).filter((n) => n !== null && n !== undefined && n !== false);
|
|
640
|
+
assert.strictEqual(q0[0].props.style.backgroundColor, 'var(--dsw-static-neutral-bluish-400)', '五小时条第一段灰(剩余)');
|
|
641
|
+
assert.strictEqual(q0[0].props.style.width, '81%', '五小时灰段宽 81%');
|
|
642
|
+
assert.strictEqual(q0[1].props.style.backgroundColor, 'transparent', '五小时条第二段透明(已用)');
|
|
643
|
+
assert.strictEqual(q0[1].props.style.width, '19%', '五小时透明段宽 19%');
|
|
644
|
+
/* 下钻:creditsPanel 传 sub.id=0(点开第一个聚合行)→ 子视图只列该窗口的 items(2 条账号行)。 */
|
|
645
|
+
const quotaDown = api.creditsPanel(qp, '余额', { left: 0, top: 0 }, () => ({}), { id: 0, open() {}, back() {} });
|
|
646
|
+
const dRows = [];
|
|
647
|
+
const dBars = [];
|
|
648
|
+
(function walk(node) {
|
|
649
|
+
if (node === null || node === undefined || node === false) return;
|
|
650
|
+
if (Array.isArray(node)) { node.forEach(walk); return }
|
|
651
|
+
if (typeof node !== 'object') return;
|
|
652
|
+
if (node.props !== undefined && typeof node.props.className === 'string' && node.props.className.split(' ').indexOf('dsb-meter-row') >= 0) dRows.push(node);
|
|
653
|
+
if (node.props !== undefined && typeof node.props.className === 'string' && node.props.className.split(' ').indexOf('dsb-bar') >= 0) dBars.push(node);
|
|
654
|
+
(node.children || []).forEach(walk);
|
|
655
|
+
})(quotaDown);
|
|
656
|
+
assert.strictEqual(dRows.length, 2, '下钻子视图列出该窗口 2 条账号行,实际 ' + dRows.length);
|
|
657
|
+
assert.strictEqual(dBars.length, 2, '下钻子视图 2 根账号进度条,实际 ' + dBars.length);
|
|
658
|
+
console.log('订阅配额气泡: 5 小时 81% / 7 天 55%(两行两根条,下钻逐账号 2 行)');
|
|
659
|
+
|
|
573
660
|
/* --------------------------------------------- 积分套餐条:灰=剩余、从**左**起
|
|
574
661
|
* 用户拍板原话「灰条从左边起」(来回改过一次,这条断言把最终方向钉死)。
|
|
575
662
|
* 这里直接渲染 creditsPanel,按**段序**断言:第一段必须是灰(topUp)、宽度 = 100 - used;
|