Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(provider-share): 复用额度条方向和低余额预警

fmfsaisai6755dc2ea0a8
diff --git a/apps/desktop/scripts/fixtures/provider-share-insights/provider-share-insights-fixture.tsx b/apps/desktop/scripts/fixtures/provider-share-insights/provider-share-insights-fixture.tsx
index 327761fbbd..75b92f01ac 100644
--- a/apps/desktop/scripts/fixtures/provider-share-insights/provider-share-insights-fixture.tsx
+++ b/apps/desktop/scripts/fixtures/provider-share-insights/provider-share-insights-fixture.tsx
@@ -98,6 +98,8 @@ const history = {
void mountPage({
host: (ctx) => {
+ const remainingPercent =
+ typeof ctx.data.remainingPercent === 'number' ? ctx.data.remainingPercent : null;
const quota = ctx.case !== 'closed' && ctx.case !== 'usage-only';
const allowUsage = ctx.case !== 'closed' && ctx.case !== 'quota-only';
const subscriptionAccount: ProviderShareQuotaAccount = {
@@ -109,7 +111,7 @@ void mountPage({
kind: 'session',
label: null,
windowMinutes: 300,
- remainingPercent: 43,
+ remainingPercent: remainingPercent ?? 43,
resetsAt: now + 7_200_000,
},
{
@@ -124,13 +126,13 @@ void mountPage({
const displayedAccount: ProviderShareQuotaAccount =
ctx.case === 'subscription'
? subscriptionAccount
- : ['organization', 'unknown', 'zero'].includes(ctx.case)
+ : remainingPercent !== null || ['organization', 'unknown', 'zero'].includes(ctx.case)
? {
...account,
balance: {
...account.balance,
pools: [],
- remaining: ctx.case === 'zero' ? 0 : 64.8,
+ remaining: remainingPercent ?? (ctx.case === 'zero' ? 0 : 64.8),
total: ctx.case === 'unknown' ? null : 100,
},
}
diff --git a/apps/desktop/scripts/fixtures/provider-share-insights/ui-page.mjs b/apps/desktop/scripts/fixtures/provider-share-insights/ui-page.mjs
index a4e8366455..5ade9f2e96 100644
--- a/apps/desktop/scripts/fixtures/provider-share-insights/ui-page.mjs
+++ b/apps/desktop/scripts/fixtures/provider-share-insights/ui-page.mjs
@@ -26,6 +26,22 @@ export default {
},
subscription: { about: 'Read-only subscription windows with existing remaining bars' },
organization: { about: 'Organization remaining balance and known budget' },
+ warning: {
+ about: '15% remaining uses the existing quota warning',
+ data: { remainingPercent: 15 },
+ },
+ critical: {
+ about: '10% remaining uses the existing quota danger',
+ data: { remainingPercent: 10 },
+ },
+ low: {
+ about: '5% remaining keeps the danger state and true fill',
+ data: { remainingPercent: 5 },
+ },
+ full: {
+ about: '100% remaining fills the track without warning',
+ data: { remainingPercent: 100 },
+ },
unknown: { about: 'Unknown total has no fabricated percentage' },
zero: { about: 'True zero balance has a zero progress bar' },
'quota-only': { about: 'Only remaining allowance shared' },
@@ -79,7 +95,9 @@ export default {
await ui.capture('collapsed-models');
}
const progress = page.getByRole('progressbar');
- if (['default', 'organization', 'zero'].includes(ui.case)) {
+ if (
+ ['default', 'organization', 'zero', 'warning', 'critical', 'low', 'full'].includes(ui.case)
+ ) {
const sizes = await progress.evaluateAll((tracks) =>
tracks.map((track) => {
const row = track.parentElement;
@@ -87,6 +105,8 @@ export default {
const bounds = track.getBoundingClientRect();
return {
track: bounds.width,
+ leftOffset: track.firstElementChild.getBoundingClientRect().left - bounds.left,
+ fill: track.firstElementChild.getBoundingClientRect().width,
content:
row.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight),
belowDetail:
@@ -95,7 +115,16 @@ export default {
}),
);
assert.ok(sizes.length > 0);
- for (const size of sizes) {
+ const remaining =
+ ui.case === 'default'
+ ? [(24.8 / 60) * 100, 100, 100]
+ : [ui.data.remainingPercent ?? (ui.case === 'zero' ? 0 : 64.8)];
+ for (const [index, size] of sizes.entries()) {
+ assert.ok(Math.abs(size.leftOffset) <= 1, 'Remaining fill starts at the left edge');
+ assert.ok(
+ Math.abs(size.fill - (size.track * remaining[index]) / 100) <= 1,
+ 'Fill preserves the actual remaining ratio',
+ );
assert.ok(Math.abs(size.track - size.content) <= 1, 'Balance track spans row content');
assert.ok(size.belowDetail, 'Balance track sits below the amount description');
}
@@ -104,12 +133,13 @@ export default {
assert.equal(await progress.count(), 3);
assert.ok(
Math.abs(
- Number(await progress.first().getAttribute('aria-valuenow')) - (24.8 / 60) * 100,
+ Number(await progress.first().getAttribute('aria-valuenow')) -
+ Math.round((24.8 / 60) * 100),
) < 0.01,
);
}
if (ui.case === 'organization')
- assert.equal(await progress.first().getAttribute('aria-valuenow'), '64.8');
+ assert.equal(await progress.first().getAttribute('aria-valuenow'), '65');
if (ui.case === 'group') assert.equal(await progress.count(), 5);
if (ui.case === 'unknown') assert.equal(await progress.count(), 0);
if (ui.case === 'zero')
diff --git a/apps/desktop/src/renderer/features/billing/CreditPoolRowView.tsx b/apps/desktop/src/renderer/features/billing/CreditPoolRowView.tsx
index b08a1a2c04..bd18bfd7b9 100644
--- a/apps/desktop/src/renderer/features/billing/CreditPoolRowView.tsx
+++ b/apps/desktop/src/renderer/features/billing/CreditPoolRowView.tsx
@@ -1,4 +1,5 @@
import { useTranslation } from 'react-i18next';
+import { QuotaBar } from '@/components/status/QuotaBar';
/** 计费页和分享详情共用的只读余额行;未知总额不显示百分比。 */
export function CreditPoolRowView({
@@ -29,21 +30,31 @@ export function CreditPoolRowView({
>
{detail}
</p>
- <div
- className={`h-1 w-full self-center overflow-hidden rounded-full bg-[var(--surface-chip)] ${progressLayout === 'full-width' ? 'col-span-2' : ''}`}
- role={percent === null ? undefined : 'progressbar'}
- aria-label={t('billing.usage.progressLabel', { label })}
- aria-valuemin={percent === null ? undefined : 0}
- aria-valuemax={percent === null ? undefined : 100}
- aria-valuenow={percent ?? undefined}
- >
- {percent !== null && (
- <div
- className="ml-auto h-full rounded-full bg-[var(--text-primary)]"
- style={{ width: `${percent}%` }}
- />
- )}
- </div>
+ {progressLayout === 'full-width' && percent !== null ? (
+ <QuotaBar
+ usedPercent={100 - percent}
+ showRemaining
+ ariaLabel={t('billing.usage.progressLabel', { label })}
+ aria-valuetext={t('quotaCard.remainingPercent', { percent: Math.round(percent) })}
+ className="col-span-2 self-center"
+ />
+ ) : (
+ <div
+ className={`h-1 w-full self-center overflow-hidden rounded-full bg-[var(--surface-chip)] ${progressLayout === 'full-width' ? 'col-span-2' : ''}`}
+ role={percent === null ? undefined : 'progressbar'}
+ aria-label={t('billing.usage.progressLabel', { label })}
+ aria-valuemin={percent === null ? undefined : 0}
+ aria-valuemax={percent === null ? undefined : 100}
+ aria-valuenow={percent ?? undefined}
+ >
+ {percent !== null && (
+ <div
+ className="ml-auto h-full rounded-full bg-[var(--text-primary)]"
+ style={{ width: `${percent}%` }}
+ />
+ )}
+ </div>
+ )}
</div>
);
}
diff --git a/apps/desktop/src/renderer/features/provider-share/__tests__/ProviderShareReceivedRail.test.tsx b/apps/desktop/src/renderer/features/provider-share/__tests__/ProviderShareReceivedRail.test.tsx
index 4dc1bf0a97..17bab344a1 100644
--- a/apps/desktop/src/renderer/features/provider-share/__tests__/ProviderShareReceivedRail.test.tsx
+++ b/apps/desktop/src/renderer/features/provider-share/__tests__/ProviderShareReceivedRail.test.tsx
@@ -423,17 +423,24 @@ describe('shared read-only insights', () => {
expect(screen.queryByText('Shared Owner')).toBeNull();
setDataOwnerGeneration(null);
});
- // 额度真实 0 与未知总额区分;订阅复用同一剩余比例展示。tier: unit。
+ // 分享余额曾把剩余比例当普通余额条,缺少低余额预警。经真实分享组件验证剩余/已用转换;
+ // 最小输入15%剩余应warn,未知总额不得预警。沿用QuotaBar阈值测试,不复制阈值算法。tier: unit。
it.each([
- { remaining: 25, total: 100, expected: '25' },
- { remaining: 0, total: 0, expected: '0' },
- { remaining: 25, total: null, expected: null },
- ])('renders known balance progress without inventing unknown totals: %j', async ({ remaining, total, expected }) => {
+ { remaining: 100, total: 100, expected: '100', severity: 'normal' },
+ { remaining: 64.8, total: 100, expected: '65', severity: 'normal' },
+ { remaining: 15, total: 100, expected: '15', severity: 'warn' },
+ { remaining: 10, total: 100, expected: '10', severity: 'crit' },
+ { remaining: 5, total: 100, expected: '5', severity: 'crit' },
+ { remaining: 0, total: 0, expected: '0', severity: 'crit' },
+ { remaining: 25, total: null, expected: null, severity: null },
+ ])('renders known balance progress without inventing unknown totals: %j', async ({ remaining, total, expected, severity }) => {
const data = insights(true, false);
data.quota!.accounts[0].balance = { remaining, total, currency: 'USD', resetsAt: null, pools: [] };
command.mockResolvedValue(data);
await act(async () => { render(<ProviderShareInsightsSection share={{ ...share, status: 'active' }} />); });
- expect(screen.queryByRole('progressbar')?.getAttribute('aria-valuenow') ?? null).toBe(expected);
+ const bar = screen.queryByRole('progressbar');
+ expect(bar?.getAttribute('aria-valuenow') ?? null).toBe(expected);
+ expect(bar?.getAttribute('data-severity') ?? null).toBe(severity);
});
it('uses remaining subscription progress without reset or management actions', async () => {
const data = insights(true, false);