Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
test(desktop,proxy): 修复 CN 构建与 Windows 下 9 个环境相关单测失败
Lizi79d1397ba7f0
diff --git a/apps/desktop/src/main/usage/__tests__/modelPricing.test.ts b/apps/desktop/src/main/usage/__tests__/modelPricing.test.tsindex 62bc53e6d1..9a3eff629b 100644--- a/apps/desktop/src/main/usage/__tests__/modelPricing.test.ts+++ b/apps/desktop/src/main/usage/__tests__/modelPricing.test.ts@@ -55,7 +55,6 @@ vi.mock('../../secrets/providerSecretStore', () => ({import { CURRENT_CINDY_REGION } from '../../../shared/brandRegion';import { providerReferencePriceQuote } from '../../../shared/modelPriceQuote';-import { DEFAULT_USAGE_CURRENCY } from '../../../shared/regionalMoney';import { getActiveCatalog } from '../../maker-host/active-catalog';import {applyModelPriceOverrides,@@ -63,7 +62,11 @@ import {readModelPriceOverridesSnapshot,setModelPriceOverride,} from '../modelPriceOverrideStore';-import { __resetActiveLedgerCurrencyForTesting, currentLedgerCurrency } from '../ledgerCurrency';+import {+ __resetActiveLedgerCurrencyForTesting,+ currentLedgerCurrency,+ LEDGER_CURRENCY_FALLBACK,+} from '../ledgerCurrency';import {__resetModelPricingCacheForTesting,clearGatewayModelPricing,@@ -331,10 +334,12 @@ describe('pricing cache lifecycle', () => {});});- // 模拟重启:清掉内存缓存与账本币种,只留磁盘缓存+ // 模拟重启:清掉内存缓存与账本币种,只留磁盘缓存。重置后回退链落在最后一档+ // USD(不按构建区域猜,见 usage/ledgerCurrency),下面从磁盘恢复出 USD 才是+ // 「快照真的把币种带回来了」的证据。__resetModelPricingCacheForTesting();__resetActiveLedgerCurrencyForTesting();- expect(currentLedgerCurrency()).toBe(DEFAULT_USAGE_CURRENCY);+ expect(currentLedgerCurrency()).toBe(LEDGER_CURRENCY_FALLBACK);const hydrated = await getGatewayModelPricing();expect(hydrated).toEqual({});diff --git a/apps/desktop/src/main/usage/__tests__/turnCostCalculator.test.ts b/apps/desktop/src/main/usage/__tests__/turnCostCalculator.test.tsindex 52c9af9299..b08801ea0a 100644--- a/apps/desktop/src/main/usage/__tests__/turnCostCalculator.test.ts+++ b/apps/desktop/src/main/usage/__tests__/turnCostCalculator.test.ts@@ -19,7 +19,6 @@ import {type TurnPricingContext,} from '../turnCostCalculator';import {- DEFAULT_USAGE_CURRENCY,type ModelPriceQuote,type ModelPricingCatalog,type RegionalMoney,@@ -299,11 +298,11 @@ describe('resolveTurnCost', () => {expect(claude.money?.amount).toBe(5);});- it('lets the build default ledger currency decide the SDK fallback when the Gateway quote is missing', () => {- // 没有活动账本币种(冷启动、目录还没同步下来)时由构建默认币种定夺,断言因此- // 跟着构建区域走:Global(USD 账本)直接按 SDK 的 USD 兜底记账;中国大陆版- // (CNY 账本)按 resolveTurnCost 里 sdk-fallback 的币种守卫这一轮不记。- // 不能写死 USD —— 那样只有 Global 构建下能过,等于把构建默认值当成了常量。+ it('falls back to USD for the SDK amount when the ledger currency is unknown — never the build region', () => {+ // 没有活动账本币种(冷启动、目录还没同步下来)时按 ledgerCurrency.ts 的回退链+ // 落到 USD,绝不按构建区域猜(#1302:按区域回落会让同一账号的账本币种反复翻转,+ // 覆盖当天累计)。SDK 的 costDelta 本来就是 USD 口径,与兜底币种同口径,因此+ // 无论 cn / global 构建,这一轮都按 USD 原值兜底记账,断言两种构建下同形。__resetActiveLedgerCurrencyForTesting();const result = resolveTurnCost({rawModel: 'unknown-model',@@ -318,16 +317,12 @@ describe('resolveTurnCost', () => {context: XD_GATEWAY,});expect(result.source).toBe('sdk-fallback');- expect(result.money).toEqual(- DEFAULT_USAGE_CURRENCY === 'USD'- ? {- amount: 1.23,- currency: 'USD',- approximate: false,- kind: 'actual-cost',- }- : null,- );+ expect(result.money).toEqual({+ amount: 1.23,+ currency: 'USD',+ approximate: false,+ kind: 'actual-cost',+ });});it('falls back to the SDK USD amount for a USD-settled account on a CN build', () => {diff --git a/apps/desktop/src/main/usage/__tests__/usageHistory.test.ts b/apps/desktop/src/main/usage/__tests__/usageHistory.test.tsindex aac20a6304..9d4919f32a 100644--- a/apps/desktop/src/main/usage/__tests__/usageHistory.test.ts+++ b/apps/desktop/src/main/usage/__tests__/usageHistory.test.ts@@ -210,9 +210,15 @@ beforeEach(async () => {);currentDbClient.userId = 'user-a';__resetUsageHistoryCacheForTesting();- // 账本币种是跨用例的模块级状态,逐例重置回未知,让默认路径的用例始终从- // 构建默认币种起算,不受前一例显式设定的账号币种影响。+ // 账本币种是跨用例的模块级状态,逐例重置,不受前一例显式设定的账号币种影响。+ // 重置后必须再显式落一次账号币种:生产里由 modelPricing(报价目录同步/磁盘快照+ // 恢复)写入,而本文件把它整体 mock 掉了;不落这一笔,回退链会落到与构建区域+ // 无关的 USD(见 usage/ledgerCurrency),CN 构建上用 actual() 构造的 CNY 行会被+ // 当成异币种整批归零。落成构建默认币种,使各用例在 cn / global 构建下分别验证+ // CNY / USD 账本口径,断言两种构建下同形。需要异币种账号的用例(如 USD 结算+ // 账号)在用例内自行覆写。__resetActiveLedgerCurrencyForTesting();+ setActiveLedgerCurrency(DEFAULT_USAGE_CURRENCY);vi.mocked(getAllSpendDays).mockResolvedValue([]);vi.mocked(getModelUsageSince).mockResolvedValue([]);vi.mocked(getGatewayModelPricing).mockResolvedValue(null);@@ -392,7 +398,7 @@ describe('readUsageHistoryWith', () => {expect(result.totals.last30DaysEstimatedValue.amount).toBeCloseTo(regionalUsdAmount(5));});- it('keeps current-region subscription estimates when history uses another currency', async () => {+ it('keeps active-ledger subscription estimates when history uses another currency', async () => {const historicalCurrency = DEFAULT_USAGE_CURRENCY === 'CNY' ? 'USD' : 'CNY';const estimateAmount = regionalUsdAmount(2);const result = await readUsageHistoryWith(diff --git a/packages/anthropic-compat-proxy/src/server-outbound-proxy.test.ts b/packages/anthropic-compat-proxy/src/server-outbound-proxy.test.tsindex d31879dd69..beb7665faa 100644--- a/packages/anthropic-compat-proxy/src/server-outbound-proxy.test.ts+++ b/packages/anthropic-compat-proxy/src/server-outbound-proxy.test.ts@@ -1,3 +1,4 @@+import dns from 'node:dns';import { createServer as createHttpServer } from 'node:http';import { afterEach, describe, expect, it, vi } from 'vitest';@@ -166,6 +167,25 @@ describe('anthropic-compat-proxy outbound proxy wiring', () => {});it('falls back to direct connection when the resolver throws or returns unsupported urls', async () => {+ // 本用例是文件里唯一真的对 .invalid 假域做**直连**(前面的用例域名都原样交给+ // 代理桩,不走本地解析)。macOS 上 NXDOMAIN 即时返回,但 Windows 的解析器会+ // 带着 DNS 搜索后缀逐个重查,轻松拖过测试超时。桩掉 .invalid 的 lookup 让它+ // 立即 ENOTFOUND —— 仍走真实 net.connect 失败路径,只是把结果变成确定性的;+ // 其余域名(本文件只有 IP 字面量,压根不进 lookup)原样放行。+ const realLookup = dns.lookup;+ vi.spyOn(dns, 'lookup').mockImplementation(((hostname: string, options: unknown, callback?: unknown) => {+ const cb = (typeof options === 'function' ? options : callback) as (err: NodeJS.ErrnoException | null) => void;+ if (!hostname.endsWith('.invalid')) {+ return (realLookup as (...args: unknown[]) => unknown)(hostname, options, callback);+ }+ const err: NodeJS.ErrnoException = Object.assign(+ new Error(`getaddrinfo ENOTFOUND ${hostname}`),+ { code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname },+ );+ queueMicrotask(() => cb(err));+ }) as typeof dns.lookup);+ cleanups.push(() => { vi.restoreAllMocks(); });+const warns: string[] = [];proxy = await createAnthropicCompatProxy({upstream: 'http://upstream.invalid:8080',