|
1 | 1 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
2 | | -import { expect, it } from "@effect/vitest"; |
3 | | -import { ServerConfigStreamEvent, UsageLimitSourceId } from "@t3tools/contracts"; |
| 2 | +import { describe, expect, it } from "@effect/vitest"; |
| 3 | +import { |
| 4 | + ProviderDriverKind, |
| 5 | + ServerConfigStreamEvent, |
| 6 | + UsageLimitSourceId, |
| 7 | + type ServerProviderUsageWindow, |
| 8 | + type UsageLimitSourceSnapshot, |
| 9 | +} from "@t3tools/contracts"; |
| 10 | +import * as DateTime from "effect/DateTime"; |
4 | 11 | import * as Effect from "effect/Effect"; |
5 | 12 | import * as Layer from "effect/Layer"; |
6 | 13 | import * as Schema from "effect/Schema"; |
@@ -83,3 +90,65 @@ it.effect("keeps malformed source URLs encodable for config subscribers", () => |
83 | 90 | Effect.scoped, |
84 | 91 | ), |
85 | 92 | ); |
| 93 | + |
| 94 | +describe("hubPoolResetAt", () => { |
| 95 | + const now = Date.parse("2026-10-30T00:00:00.000Z"); |
| 96 | + const hours = (count: number) => DateTime.formatIso(DateTime.makeUnsafe(now + count * 3_600_000)); |
| 97 | + const window = (usedPercent: number, resetsAt?: string): ServerProviderUsageWindow => ({ |
| 98 | + id: resetsAt ?? `window-${usedPercent}`, |
| 99 | + kind: "session", |
| 100 | + label: "5h", |
| 101 | + usedPercent, |
| 102 | + ...(resetsAt === undefined ? {} : { resetsAt }), |
| 103 | + }); |
| 104 | + const hub = ( |
| 105 | + ...accounts: ReadonlyArray<readonly [string, ReadonlyArray<ServerProviderUsageWindow>]> |
| 106 | + ): UsageLimitSourceSnapshot => ({ |
| 107 | + id: UsageLimitSourceId.make("hub"), |
| 108 | + kind: "cliproxy", |
| 109 | + label: "hub", |
| 110 | + checkedAt: hours(0), |
| 111 | + accounts: accounts.map(([driver, windows], index) => ({ |
| 112 | + id: `account-${index}`, |
| 113 | + driver: ProviderDriverKind.make(driver), |
| 114 | + usageLimits: { checkedAt: hours(0), windows }, |
| 115 | + })), |
| 116 | + }); |
| 117 | + const codex = ProviderDriverKind.make("codex"); |
| 118 | + |
| 119 | + it.each([ |
| 120 | + { |
| 121 | + name: "the first spent account to recover, after all its spent windows reset", |
| 122 | + sources: [ |
| 123 | + hub( |
| 124 | + ["codex", [window(100, hours(2))]], |
| 125 | + ["codex", [window(100, hours(1)), window(100, hours(3))]], |
| 126 | + ), |
| 127 | + hub(["claudeAgent", [window(100, hours(0.5))]]), |
| 128 | + ], |
| 129 | + expected: hours(2), |
| 130 | + }, |
| 131 | + { |
| 132 | + name: "nothing while an account still has quota", |
| 133 | + sources: [hub(["codex", [window(100, hours(2))]], ["codex", [window(40, hours(1))]])], |
| 134 | + expected: null, |
| 135 | + }, |
| 136 | + { |
| 137 | + name: "nothing once a spent window's reset has passed", |
| 138 | + sources: [hub(["codex", [window(100, hours(2))]], ["codex", [window(100, hours(-1))]])], |
| 139 | + expected: null, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "a recovery time while skipping accounts without readings or reset times", |
| 143 | + sources: [hub(["codex", []], ["codex", [window(100)]], ["codex", [window(100, hours(4))]])], |
| 144 | + expected: hours(4), |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "nothing when the hub pools none of the driver's accounts", |
| 148 | + sources: [hub(["claudeAgent", [window(100, hours(1))]])], |
| 149 | + expected: null, |
| 150 | + }, |
| 151 | + ])("returns $name", ({ sources, expected }) => { |
| 152 | + expect(UsageLimitSources.hubPoolResetAt(sources, codex, now)).toBe(expected); |
| 153 | + }); |
| 154 | +}); |
0 commit comments