import { describe, expect, test } from "bun:test"; import { decideApiRecovery } from "../../bin/claude-pty"; /** * 自愈预算计的是连续断流,不是整段任务。 * * 2026-09-23 一个 100 分钟的 iOS 修复:三次 Connection lost 彼此隔了几十分钟, * 中间模型都在正常干活,终身配额 2 仍在 git push 前把会话杀掉。间隔够长就该 * 从 1 重新计;只有短时间里连着失败才算把配额烧光。 */ const RESET = 10 * 60 * 1000; describe("API 错误自愈连续计数", () => { test("第一次断流自愈,计数从 1 开始", () => { const d = decideApiRecovery({ count: 0, lastAt: 0 }, 1_000); expect(d).toEqual({ action: "recover", count: 1, lastAt: 1_000, reset: false }); }); test("10 分钟内连续失败往上累加", () => { const first = decideApiRecovery({ count: 0, lastAt: 0 }, 0); const second = decideApiRecovery({ count: first.count, lastAt: first.lastAt }, RESET - 1); expect(second).toEqual({ action: "recover", count: 2, lastAt: RESET - 1, reset: false }); }); test("连续第 4 次仍自愈,第 5 次放弃", () => { const fourth = decideApiRecovery({ count: 3, lastAt: 1_000 }, 1_000 + RESET - 1); expect(fourth).toEqual({ action: "recover", count: 4, lastAt: 1_000 + RESET - 1, reset: false }); const fifth = decideApiRecovery( { count: fourth.count, lastAt: fourth.lastAt }, fourth.lastAt + RESET - 1, ); expect(fifth.action).toBe("exhaust"); expect(fifth.count).toBe(4); expect(fifth.reset).toBe(false); }); test("距上次自愈已满 10 分钟,即使已经连续 4 次也从 1 重新计", () => { const d = decideApiRecovery({ count: 4, lastAt: 1_000 }, 1_000 + RESET); expect(d).toEqual({ action: "recover", count: 1, lastAt: 1_000 + RESET, reset: true }); }); });