|
9 | 9 | import { describe, expect, it } from "vite-plus/test"; |
10 | 10 |
|
11 | 11 | import { |
| 12 | + type LimitAccount, |
12 | 13 | isUsageLimitsCommand, |
13 | 14 | collectProviderUsageLimits, |
14 | 15 | sameUsageLimitCommandCoverage, |
@@ -777,12 +778,103 @@ describe("pools", () => { |
777 | 778 | ["weekly", 1], |
778 | 779 | ["monthly", 1], |
779 | 780 | ]); |
780 | | - // Segments read left to right as "who refills next", matching the reset list. |
| 781 | + // Session resets determine the account order for every row. |
781 | 782 | expect(session?.members.map((member) => member.account.key)).toEqual(["hub:a", "hub:b"]); |
782 | 783 | expect(pools[0]?.accounts.map((account) => account.key)).toEqual(["hub:a", "hub:b"]); |
783 | 784 | }); |
784 | 785 | }); |
785 | 786 |
|
| 787 | +describe("pooled account columns", () => { |
| 788 | + const weekly = { |
| 789 | + ...window, |
| 790 | + id: "seven_day", |
| 791 | + kind: "weekly", |
| 792 | + label: "Weekly", |
| 793 | + windowDurationMins: 7 * 24 * 60, |
| 794 | + } as const; |
| 795 | + const account = (key: string, windows: LimitAccount["limits"]["windows"]): LimitAccount => ({ |
| 796 | + key, |
| 797 | + driver: ProviderDriverKind.make("claudeAgent"), |
| 798 | + displayName: key, |
| 799 | + email: undefined, |
| 800 | + plan: undefined, |
| 801 | + accentColor: undefined, |
| 802 | + environments: [], |
| 803 | + sourceLabel: "Hub", |
| 804 | + redeem: null, |
| 805 | + limits: { checkedAt: "2026-09-03T11:00:00.000Z", windows }, |
| 806 | + }); |
| 807 | + const keys = (pool: ReturnType<typeof collectLimitPools>[number]) => |
| 808 | + pool.windows.map((row) => |
| 809 | + row.columns.map((member) => (member.window ? member.account.key : null)), |
| 810 | + ); |
| 811 | + |
| 812 | + it("keeps session columns across rows with opposite reset and usage orders", () => { |
| 813 | + const accounts = [ |
| 814 | + account("a", [ |
| 815 | + { ...weekly, usedPercent: 80, resetsAt: "2026-09-05T12:00:00.000Z" }, |
| 816 | + { ...window, usedPercent: 10, resetsAt: "2026-09-03T15:00:00.000Z" }, |
| 817 | + ]), |
| 818 | + account("b", [ |
| 819 | + { ...weekly, usedPercent: 20, resetsAt: "2026-09-06T12:00:00.000Z" }, |
| 820 | + { ...window, usedPercent: 90, resetsAt: "2026-09-03T13:00:00.000Z" }, |
| 821 | + ]), |
| 822 | + ]; |
| 823 | + const [pool] = collectLimitPools(accounts, now); |
| 824 | + expect(pool!.accounts.map((account) => account.key)).toEqual(["b", "a"]); |
| 825 | + expect(keys(pool!)).toEqual([ |
| 826 | + ["b", "a"], |
| 827 | + ["b", "a"], |
| 828 | + ]); |
| 829 | + expect(pool!.windows[1]!.resets.map((reset) => reset.member.account.key)).toEqual(["a", "b"]); |
| 830 | + expect(pool!.windows[1]!.remainingPercent).toBe(50); |
| 831 | + expect(keys(collectLimitPools(accounts.toReversed(), now)[0]!)).toEqual(keys(pool!)); |
| 832 | + }); |
| 833 | + |
| 834 | + it("preserves gaps without counting missing windows toward pooled quota", () => { |
| 835 | + const [pool] = collectLimitPools( |
| 836 | + [ |
| 837 | + account("a", [window]), |
| 838 | + account("b", [ |
| 839 | + { ...window, resetsAt: "2026-09-03T15:00:00.000Z" }, |
| 840 | + { ...weekly, usedPercent: 80 }, |
| 841 | + ]), |
| 842 | + account("c", [weekly]), |
| 843 | + ], |
| 844 | + now, |
| 845 | + ); |
| 846 | + expect(keys(pool!)).toEqual([ |
| 847 | + ["a", "b", null], |
| 848 | + [null, "b", "c"], |
| 849 | + ]); |
| 850 | + expect(pool!.windows[1]!.members.map((member) => member.account.key)).toEqual(["b", "c"]); |
| 851 | + expect(pool!.windows[1]!.remainingPercent).toBe(40); |
| 852 | + expect(pool!.windows[1]!.resets.map((reset) => reset.restoresPercent)).toEqual([40, 20]); |
| 853 | + }); |
| 854 | + |
| 855 | + it("falls back to weekly resets when no account reports a session", () => { |
| 856 | + const [pool] = collectLimitPools( |
| 857 | + [ |
| 858 | + account("a", [{ ...weekly, resetsAt: "2026-09-06T12:00:00.000Z" }]), |
| 859 | + account("b", [{ ...weekly, resetsAt: "2026-09-05T12:00:00.000Z" }]), |
| 860 | + ], |
| 861 | + now, |
| 862 | + ); |
| 863 | + expect(keys(pool!)).toEqual([["b", "a"]]); |
| 864 | + }); |
| 865 | + |
| 866 | + it("sorts unknown resets last and breaks ties consistently", () => { |
| 867 | + const accounts = [ |
| 868 | + account("z", [{ ...window, resetsAt: undefined }]), |
| 869 | + account("b", [window]), |
| 870 | + account("a", [window]), |
| 871 | + account("y", [{ ...window, resetsAt: "invalid" }]), |
| 872 | + ]; |
| 873 | + expect(keys(collectLimitPools(accounts, now)[0]!)).toEqual([["a", "b", "y", "z"]]); |
| 874 | + expect(keys(collectLimitPools(accounts.toReversed(), now)[0]!)).toEqual([["a", "b", "y", "z"]]); |
| 875 | + }); |
| 876 | +}); |
| 877 | + |
786 | 878 | describe("collectLimitNotices", () => { |
787 | 879 | const checkedAt = "2026-09-03T11:00:00.000Z"; |
788 | 880 | const claude = ProviderDriverKind.make("claudeAgent"); |
|
0 commit comments