-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredicate.test.ts
More file actions
181 lines (167 loc) · 5.17 KB
/
Copy pathpredicate.test.ts
File metadata and controls
181 lines (167 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { describe, expect, test } from "bun:test";
import {
EXHAUSTED_MIN_FRACTION,
MAX_PLAUSIBLE_REMAINING_MS,
evaluateBlock,
type UsageReportLike,
} from "./predicate";
const NOW = 1_700_000_000_000;
function report(
limits: Partial<UsageReportLike["limits"][number]>[],
provider = "anthropic",
): UsageReportLike {
return {
provider,
limits: limits.map(l => ({
windowId: "5h",
usedFraction: undefined,
resetsAt: undefined,
tiered: false,
...l,
})),
};
}
describe("evaluateBlock", () => {
test("ignores non-anthropic active provider", () => {
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "openai",
reports: [report([{ windowId: "5h", usedFraction: 1, resetsAt: NOW + 60_000 }])],
handledEpisodes: new Set(),
});
expect(decision).toEqual({ act: false, reason: "wrong-provider" });
});
test("ignores when no window is exhausted", () => {
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "anthropic",
reports: [report([{ windowId: "5h", usedFraction: 0.5, resetsAt: NOW + 60_000 }])],
handledEpisodes: new Set(),
});
expect(decision).toEqual({ act: false, reason: "not-exhausted" });
});
test("ignores when no report is available", () => {
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "anthropic",
reports: undefined,
handledEpisodes: new Set(),
});
expect(decision).toEqual({ act: false, reason: "not-exhausted" });
});
test("ignores an exhausted window with no reset time", () => {
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "anthropic",
reports: [report([{ windowId: "5h", usedFraction: 1, resetsAt: undefined }])],
handledEpisodes: new Set(),
});
expect(decision).toEqual({ act: false, reason: "no-reset-time" });
});
test("ignores a reset time already in the past", () => {
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "anthropic",
reports: [report([{ windowId: "5h", usedFraction: 1, resetsAt: NOW - 1_000 }])],
handledEpisodes: new Set(),
});
expect(decision).toEqual({ act: false, reason: "reset-in-past" });
});
test("ignores an implausibly distant reset time", () => {
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "anthropic",
reports: [
report([
{ windowId: "5h", usedFraction: 1, resetsAt: NOW + MAX_PLAUSIBLE_REMAINING_MS + 1 },
]),
],
handledEpisodes: new Set(),
});
expect(decision).toEqual({ act: false, reason: "reset-implausible" });
});
test("ignores an episode already handled", () => {
const resetsAt = NOW + 60_000;
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "anthropic",
reports: [report([{ windowId: "5h", usedFraction: 1, resetsAt }])],
handledEpisodes: new Set([`5h:${resetsAt}`]),
});
expect(decision).toEqual({ act: false, reason: "already-handled" });
});
test("acts on a confirmed single-window block", () => {
const resetsAt = NOW + 90 * 60_000;
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "anthropic",
reports: [report([{ windowId: "5h", usedFraction: EXHAUSTED_MIN_FRACTION, resetsAt }])],
handledEpisodes: new Set(),
});
expect(decision).toEqual({ act: true, windowId: "5h", resetsAt, episodeKey: `5h:${resetsAt}` });
});
test("when both windows are exhausted, binds to the later reset", () => {
const fiveHourReset = NOW + 60_000;
const sevenDayReset = NOW + 3 * 24 * 3_600_000;
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "anthropic",
reports: [
report([
{ windowId: "5h", usedFraction: 1, resetsAt: fiveHourReset },
{ windowId: "7d", usedFraction: 1, resetsAt: sevenDayReset },
]),
],
handledEpisodes: new Set(),
});
expect(decision).toEqual({
act: true,
windowId: "7d",
resetsAt: sevenDayReset,
episodeKey: `7d:${sevenDayReset}`,
});
});
test("refuses to act when one exhausted window has no reset time even if another does", () => {
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "anthropic",
reports: [
report([
{ windowId: "5h", usedFraction: 1, resetsAt: NOW + 60_000 },
{ windowId: "7d", usedFraction: 1, resetsAt: undefined },
]),
],
handledEpisodes: new Set(),
});
expect(decision).toEqual({ act: false, reason: "no-reset-time" });
});
test("prefers an untiered limit over a tiered one for the same window", () => {
const untieredReset = NOW + 60_000;
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "anthropic",
reports: [
report([
{ windowId: "5h", usedFraction: 1, resetsAt: NOW + 120_000, tiered: true },
{ windowId: "5h", usedFraction: 1, resetsAt: untieredReset, tiered: false },
]),
],
handledEpisodes: new Set(),
});
expect(decision).toEqual({
act: true,
windowId: "5h",
resetsAt: untieredReset,
episodeKey: `5h:${untieredReset}`,
});
});
test("ignores reports from a different provider", () => {
const decision = evaluateBlock({
nowMs: NOW,
activeProvider: "anthropic",
reports: [report([{ windowId: "5h", usedFraction: 1, resetsAt: NOW + 60_000 }], "openai")],
handledEpisodes: new Set(),
});
expect(decision).toEqual({ act: false, reason: "not-exhausted" });
});
});