File size: 9,902 Bytes
097eaf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import { jest } from "@jest/globals";

// ---------------------------------------------------------------------------
// Mocks — must come before imports
// ---------------------------------------------------------------------------

const mockGetValue = jest.fn<(key: string) => Promise<string | null>>();
const mockSetValue =
  jest.fn<(key: string, value: string, ttl: number) => Promise<void>>();
const mockDeleteKey = jest.fn<(key: string) => Promise<void>>();

jest.mock("../../../services/redis", () => ({
  getValue: (key: string) => mockGetValue(key),
  setValue: (key: string, value: string, ttl: number) =>
    mockSetValue(key, value, ttl),
  deleteKey: (key: string) => mockDeleteKey(key),
}));

jest.mock("../../../services/supabase", () => ({
  supabase_service: {},
  isPostgrestNoRowsError: jest.fn(() => true),
}));

jest.mock("../../../lib/logger", () => ({
  logger: {
    info: jest.fn(),
    warn: jest.fn(),
    error: jest.fn(),
    child: jest.fn().mockReturnThis(),
  },
}));

import {
  calculateBrowserSessionCredits,
  BROWSER_CREDITS_PER_HOUR,
  INTERACT_CREDITS_PER_HOUR,
} from "../../../lib/browser-billing";

import {
  markBrowserSessionUsedPrompt,
  didBrowserSessionUsePrompt,
  clearBrowserSessionPromptFlag,
} from "../../../lib/browser-sessions";

// ---------------------------------------------------------------------------
// Setup
// ---------------------------------------------------------------------------

beforeEach(() => {
  jest.clearAllMocks();
  mockGetValue.mockResolvedValue(null);
  mockSetValue.mockResolvedValue(undefined);
  mockDeleteKey.mockResolvedValue(undefined);
});

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

describe("billing constants", () => {
  it("browser rate is 120 credits/hour", () => {
    expect(BROWSER_CREDITS_PER_HOUR).toBe(120);
  });

  it("interact rate is 420 credits/hour (7 credits/min)", () => {
    expect(INTERACT_CREDITS_PER_HOUR).toBe(420);
  });
});

// ---------------------------------------------------------------------------
// calculateBrowserSessionCredits
// ---------------------------------------------------------------------------

describe("calculateBrowserSessionCredits", () => {
  describe("with default browser rate (120/hr)", () => {
    it("returns minimum 2 credits for very short sessions", () => {
      expect(calculateBrowserSessionCredits(0)).toBe(2);
      expect(calculateBrowserSessionCredits(1000)).toBe(2);
      expect(calculateBrowserSessionCredits(10_000)).toBe(2);
    });

    it("calculates correctly for 1 minute", () => {
      expect(calculateBrowserSessionCredits(60_000)).toBe(2);
    });

    it("calculates correctly for 5 minutes", () => {
      expect(calculateBrowserSessionCredits(5 * 60_000)).toBe(10);
    });

    it("calculates correctly for 10 minutes", () => {
      expect(calculateBrowserSessionCredits(10 * 60_000)).toBe(20);
    });

    it("calculates correctly for 1 hour", () => {
      expect(calculateBrowserSessionCredits(3_600_000)).toBe(120);
    });

    it("rounds up to next integer", () => {
      // 61s / 3600s * 120 = 2.033... → ceil = 3
      expect(calculateBrowserSessionCredits(61_000)).toBe(3);
    });
  });

  describe("with interact rate (420/hr)", () => {
    it("returns minimum 2 credits for very short sessions", () => {
      expect(calculateBrowserSessionCredits(0, INTERACT_CREDITS_PER_HOUR)).toBe(
        2,
      );
      expect(
        calculateBrowserSessionCredits(1000, INTERACT_CREDITS_PER_HOUR),
      ).toBe(2);
    });

    it("calculates 7 credits per minute", () => {
      expect(
        calculateBrowserSessionCredits(60_000, INTERACT_CREDITS_PER_HOUR),
      ).toBe(7);
    });

    it("calculates 35 credits for 5 minutes", () => {
      expect(
        calculateBrowserSessionCredits(5 * 60_000, INTERACT_CREDITS_PER_HOUR),
      ).toBe(35);
    });

    it("calculates 70 credits for 10 minutes", () => {
      expect(
        calculateBrowserSessionCredits(10 * 60_000, INTERACT_CREDITS_PER_HOUR),
      ).toBe(70);
    });

    it("calculates 420 credits for 1 hour", () => {
      expect(
        calculateBrowserSessionCredits(3_600_000, INTERACT_CREDITS_PER_HOUR),
      ).toBe(420);
    });

    it("rounds up to next integer", () => {
      // 31s / 3600s * 420 = 3.616... → ceil = 4
      expect(
        calculateBrowserSessionCredits(31_000, INTERACT_CREDITS_PER_HOUR),
      ).toBe(4);
    });
  });

  describe("rate comparison", () => {
    it("interact rate is always >= browser rate for same duration", () => {
      const durations = [0, 1000, 30_000, 60_000, 300_000, 600_000, 3_600_000];
      for (const ms of durations) {
        const browserCredits = calculateBrowserSessionCredits(
          ms,
          BROWSER_CREDITS_PER_HOUR,
        );
        const interactCredits = calculateBrowserSessionCredits(
          ms,
          INTERACT_CREDITS_PER_HOUR,
        );
        expect(interactCredits).toBeGreaterThanOrEqual(browserCredits);
      }
    });

    it("interact rate is 3.5x browser rate for non-trivial durations", () => {
      const browser = calculateBrowserSessionCredits(
        5 * 60_000,
        BROWSER_CREDITS_PER_HOUR,
      );
      const interact = calculateBrowserSessionCredits(
        5 * 60_000,
        INTERACT_CREDITS_PER_HOUR,
      );
      expect(interact / browser).toBe(3.5);
    });
  });
});

// ---------------------------------------------------------------------------
// Prompt flag Redis helpers
// ---------------------------------------------------------------------------

describe("prompt usage tracking", () => {
  describe("markBrowserSessionUsedPrompt", () => {
    it("sets Redis flag with 2-hour TTL", async () => {
      await markBrowserSessionUsedPrompt("session-123");

      expect(mockSetValue).toHaveBeenCalledWith(
        "browser_session:used_prompt:session-123",
        "1",
        7200,
      );
    });

    it("does not throw on Redis failure", async () => {
      mockSetValue.mockRejectedValueOnce(new Error("Redis down"));

      await expect(
        markBrowserSessionUsedPrompt("session-123"),
      ).resolves.not.toThrow();
    });
  });

  describe("didBrowserSessionUsePrompt", () => {
    it("returns true when flag is set", async () => {
      mockGetValue.mockResolvedValueOnce("1");

      const result = await didBrowserSessionUsePrompt("session-123");
      expect(result).toBe(true);
      expect(mockGetValue).toHaveBeenCalledWith(
        "browser_session:used_prompt:session-123",
      );
    });

    it("returns false when flag is not set", async () => {
      mockGetValue.mockResolvedValueOnce(null);

      const result = await didBrowserSessionUsePrompt("session-123");
      expect(result).toBe(false);
    });

    it("returns false on Redis failure (graceful fallback to browser rate)", async () => {
      mockGetValue.mockRejectedValueOnce(new Error("Redis down"));

      const result = await didBrowserSessionUsePrompt("session-123");
      expect(result).toBe(false);
    });
  });

  describe("clearBrowserSessionPromptFlag", () => {
    it("deletes the Redis key", async () => {
      await clearBrowserSessionPromptFlag("session-123");

      expect(mockDeleteKey).toHaveBeenCalledWith(
        "browser_session:used_prompt:session-123",
      );
    });

    it("does not throw on Redis failure", async () => {
      mockDeleteKey.mockRejectedValueOnce(new Error("Redis down"));

      await expect(
        clearBrowserSessionPromptFlag("session-123"),
      ).resolves.not.toThrow();
    });
  });
});

// ---------------------------------------------------------------------------
// Billing rate selection (integration of flag + rate)
// ---------------------------------------------------------------------------

describe("billing rate selection", () => {
  it("uses 420/hr when prompt flag is set", async () => {
    mockGetValue.mockResolvedValueOnce("1");

    const usedPrompt = await didBrowserSessionUsePrompt("session-123");
    const rate = usedPrompt
      ? INTERACT_CREDITS_PER_HOUR
      : BROWSER_CREDITS_PER_HOUR;
    const credits = calculateBrowserSessionCredits(5 * 60_000, rate);

    expect(usedPrompt).toBe(true);
    expect(rate).toBe(420);
    expect(credits).toBe(35);
  });

  it("uses 120/hr when no prompt was used", async () => {
    mockGetValue.mockResolvedValueOnce(null);

    const usedPrompt = await didBrowserSessionUsePrompt("session-123");
    const rate = usedPrompt
      ? INTERACT_CREDITS_PER_HOUR
      : BROWSER_CREDITS_PER_HOUR;
    const credits = calculateBrowserSessionCredits(5 * 60_000, rate);

    expect(usedPrompt).toBe(false);
    expect(rate).toBe(120);
    expect(credits).toBe(10);
  });

  it("falls back to 120/hr when Redis is down", async () => {
    mockGetValue.mockRejectedValueOnce(new Error("Redis down"));

    const usedPrompt = await didBrowserSessionUsePrompt("session-123");
    const rate = usedPrompt
      ? INTERACT_CREDITS_PER_HOUR
      : BROWSER_CREDITS_PER_HOUR;
    const credits = calculateBrowserSessionCredits(5 * 60_000, rate);

    expect(usedPrompt).toBe(false);
    expect(rate).toBe(120);
    expect(credits).toBe(10);
  });

  it("full flow: mark → check → bill → clear", async () => {
    await markBrowserSessionUsedPrompt("session-456");
    expect(mockSetValue).toHaveBeenCalledTimes(1);

    mockGetValue.mockResolvedValueOnce("1");
    const usedPrompt = await didBrowserSessionUsePrompt("session-456");
    expect(usedPrompt).toBe(true);

    const credits = calculateBrowserSessionCredits(
      3 * 60_000,
      INTERACT_CREDITS_PER_HOUR,
    );
    expect(credits).toBe(21); // 3 min * 7 credits/min

    await clearBrowserSessionPromptFlag("session-456");
    expect(mockDeleteKey).toHaveBeenCalledTimes(1);
  });
});