@@ -4,27 +4,29 @@ import { tmpdir } from "node:os";
44import { join } from "node:path" ;
55import { encodeMessage , encodeString , encodeVarintField , iterFields } from "../../src/adapters/devin/cloud-direct/wire" ;
66import { saveCredential } from "../../src/oauth/store" ;
7- import { clearProviderQuotaCache , fetchProviderQuotaReports } from "../../src/providers/quota" ;
7+ import { clearProviderQuotaCache , fetchProviderQuotaReports , QUOTA_RESPONSE_MAX_BYTES } from "../../src/providers/quota" ;
88import { decodeDevinUserStatus , devinQuotaFromStatus , fetchDevinQuota } from "../../src/providers/quota/devin" ;
99import { AUTHORITATIVE_EMPTY_QUOTA , TERMINAL_QUOTA_FAILURE } from "../../src/providers/quota/report-cache" ;
1010import type { OcxConfig } from "../../src/types" ;
1111import { removeTreeWithRetry } from "../helpers/remove-tree" ;
1212
1313const KEY = "devin-session-token$synthetic.fixture.key" ;
14- const PLAN_END = 1_792_000_000 ;
15- const DAILY_RESET = 1_790_500_000 ;
16- const WEEKLY_RESET = 1_791_000_000 ;
14+ // Far enough ahead that the fetch-path tests, which use the real clock, see live windows.
15+ const PLAN_END = 4_000_000_000 ;
16+ const DAILY_RESET = 3_990_000_000 ;
17+ const WEEKLY_RESET = 3_995_000_000 ;
1718
1819/** Signed ints ride the wire as 64-bit two's complement, exactly like the server sends -1. */
1920const int = ( num : number , value : number ) => encodeVarintField ( num , BigInt . asUintN ( 64 , BigInt ( value ) ) ) ;
2021
21- function planInfo ( p : { tier : number ; name : string ; billing : number ; hideDaily ?: boolean ; monthlyFlow ?: number } ) : Buffer {
22+ function planInfo ( p : { tier : number ; name : string ; billing : number ; hideDaily ?: boolean ; hideWeekly ?: boolean ; monthlyFlow ?: number } ) : Buffer {
2223 return Buffer . concat ( [
2324 int ( 1 , p . tier ) ,
2425 encodeString ( 2 , p . name ) ,
2526 ...( p . monthlyFlow ? [ int ( 13 , p . monthlyFlow ) ] : [ ] ) ,
2627 int ( 35 , p . billing ) ,
2728 ...( p . hideDaily ? [ int ( 36 , 1 ) ] : [ ] ) ,
29+ ...( p . hideWeekly ? [ int ( 37 , 1 ) ] : [ ] ) ,
2830 ] ) ;
2931}
3032
@@ -52,6 +54,12 @@ const creditPlan = (status: Record<number, number>) => userStatusResponse(
5254 false ,
5355) ;
5456
57+ const creditPlanWithFlow = ( monthlyFlow : number , status : Record < number , number > ) => userStatusResponse (
58+ planInfo ( { tier : 16 , name : "Pro" , billing : 1 , monthlyFlow } ) ,
59+ status ,
60+ false ,
61+ ) ;
62+
5563describe ( "Devin GetUserStatus decode and mapping" , ( ) => {
5664 test ( "quota-billed plan publishes the dated weekly window and nothing hidden or unlimited" , ( ) => {
5765 const status = decodeDevinUserStatus ( quotaPlan ( ) ) ;
@@ -85,6 +93,48 @@ describe("Devin GetUserStatus decode and mapping", () => {
8593 expect ( spent . monthlyPercent ) . toBe ( 100 ) ;
8694 } ) ;
8795
96+ test ( "a quota-billed plan with a zero credit balance is not credit-exhausted" , ( ) => {
97+ const buf = userStatusResponse ( planInfo ( { tier : 17 , name : "Max" , billing : 2 } ) , { 6 : 10 , 8 : 0 , 14 : 100 , 15 : 90 } ) ;
98+ const quota = devinQuotaFromStatus ( decodeDevinUserStatus ( buf ) ! , 1 ) ;
99+ expect ( quota . weeklyPercent ) . toBe ( 10 ) ;
100+ expect ( quota . monthlyPercent ) . toBeUndefined ( ) ;
101+ } ) ;
102+
103+ test ( "an unknown billing strategy uses credits only when no window is dated" , ( ) => {
104+ const undated = userStatusResponse ( planInfo ( { tier : 16 , name : "Pro" , billing : 0 } ) , { 6 : 50 , 8 : 50 } , false ) ;
105+ expect ( devinQuotaFromStatus ( decodeDevinUserStatus ( undated ) ! , 1 ) . monthlyPercent ) . toBe ( 50 ) ;
106+ const dated = userStatusResponse ( planInfo ( { tier : 16 , name : "Pro" , billing : 0 } ) , { 6 : 50 , 8 : 50 , 14 : 100 , 15 : 100 } ) ;
107+ expect ( devinQuotaFromStatus ( decodeDevinUserStatus ( dated ) ! , 1 ) . monthlyPercent ) . toBeUndefined ( ) ;
108+ } ) ;
109+
110+ test ( "a window whose reset has already passed is dropped rather than read as spent" , ( ) => {
111+ const buf = userStatusResponse ( planInfo ( { tier : 17 , name : "Max" , billing : 2 } ) , { 8 : - 1 , 14 : 0 , 15 : 0 } ) ;
112+ const status = decodeDevinUserStatus ( buf ) ! ;
113+ expect ( devinQuotaFromStatus ( status , WEEKLY_RESET * 1000 + 1 ) ) . toEqual ( { updatedAt : WEEKLY_RESET * 1000 + 1 } ) ;
114+ expect ( devinQuotaFromStatus ( status , DAILY_RESET * 1000 + 1 ) ) . toEqual ( {
115+ updatedAt : DAILY_RESET * 1000 + 1 ,
116+ weeklyPercent : 100 ,
117+ weeklyResetAt : WEEKLY_RESET * 1000 ,
118+ } ) ;
119+ } ) ;
120+
121+ test ( "hide_weekly_quota (#37) suppresses the weekly window" , ( ) => {
122+ const buf = userStatusResponse ( planInfo ( { tier : 17 , name : "Max" , billing : 2 , hideWeekly : true } ) , { 8 : - 1 , 14 : 70 , 15 : 10 } ) ;
123+ expect ( devinQuotaFromStatus ( decodeDevinUserStatus ( buf ) ! , 1 ) ) . toEqual ( {
124+ updatedAt : 1 ,
125+ customWindows : [ { label : "Daily" , percent : 30 , resetAt : DAILY_RESET * 1000 } ] ,
126+ } ) ;
127+ } ) ;
128+
129+ test ( "flow credits read used #5 against available #9 only when the plan grants them (#13)" , ( ) => {
130+ const granted = creditPlanWithFlow ( 100 , { 5 : 30 , 9 : 70 } ) ;
131+ expect ( devinQuotaFromStatus ( decodeDevinUserStatus ( granted ) ! , 1 ) . customWindows ) . toEqual ( [
132+ { label : "Flow credits" , percent : 30 , resetAt : PLAN_END * 1000 } ,
133+ ] ) ;
134+ const ungranted = creditPlanWithFlow ( 0 , { 5 : 30 , 9 : 70 } ) ;
135+ expect ( devinQuotaFromStatus ( decodeDevinUserStatus ( ungranted ) ! , 1 ) . customWindows ) . toBeUndefined ( ) ;
136+ } ) ;
137+
88138 test ( "a response without PlanStatus is not a usable status" , ( ) => {
89139 expect ( decodeDevinUserStatus ( encodeMessage ( 1 , encodeString ( 3 , "Fixture User" ) ) ) ) . toBeNull ( ) ;
90140 expect ( decodeDevinUserStatus ( Buffer . alloc ( 0 ) ) ) . toBeNull ( ) ;
@@ -112,15 +162,27 @@ describe("fetchDevinQuota transport", () => {
112162 expect ( ( [ ...iterFields ( metadata ) ] . find ( f => f . num === 3 ) ?. value as Buffer ) . toString ( ) ) . toBe ( KEY ) ;
113163 } ) ;
114164
115- test ( "401 is terminal, 5xx and network faults keep the last-good row" , async ( ) => {
116- globalThis . fetch = ( async ( ) => new Response ( "unauthenticated: " + KEY , { status : 401 } ) ) as unknown as typeof fetch ;
117- expect ( await fetchDevinQuota ( "devin" , KEY , undefined ) ) . toBe ( TERMINAL_QUOTA_FAILURE ) ;
118- globalThis . fetch = ( async ( ) => new Response ( "" , { status : 503 } ) ) as unknown as typeof fetch ;
119- expect ( await fetchDevinQuota ( "devin" , KEY , undefined ) ) . toBeNull ( ) ;
165+ test ( "401/403/404 are terminal; 408/409/429/499, 5xx and network faults keep the last-good row" , async ( ) => {
166+ for ( const status of [ 401 , 403 , 404 ] ) {
167+ globalThis . fetch = ( async ( ) => new Response ( "unauthenticated: " + KEY , { status } ) ) as unknown as typeof fetch ;
168+ expect ( await fetchDevinQuota ( "devin" , KEY , undefined ) ) . toBe ( TERMINAL_QUOTA_FAILURE ) ;
169+ }
170+ for ( const status of [ 408 , 409 , 429 , 499 , 503 ] ) {
171+ globalThis . fetch = ( async ( ) => new Response ( "" , { status } ) ) as unknown as typeof fetch ;
172+ expect ( await fetchDevinQuota ( "devin" , KEY , undefined ) ) . toBeNull ( ) ;
173+ }
120174 globalThis . fetch = ( async ( ) => { throw new TypeError ( "network down" ) ; } ) as unknown as typeof fetch ;
121175 expect ( await fetchDevinQuota ( "devin" , KEY , undefined ) ) . toBeNull ( ) ;
122176 } ) ;
123177
178+ test ( "a truncated or oversized body keeps the last-good row instead of throwing" , async ( ) => {
179+ // Tag for field 1 varint, then a continuation byte with nothing after it.
180+ globalThis . fetch = ( async ( ) => new Response ( new Uint8Array ( [ 0x08 , 0x80 ] ) , { status : 200 } ) ) as unknown as typeof fetch ;
181+ expect ( await fetchDevinQuota ( "devin" , KEY , undefined ) ) . toBeNull ( ) ;
182+ globalThis . fetch = ( async ( ) => new Response ( new Uint8Array ( QUOTA_RESPONSE_MAX_BYTES + 1 ) , { status : 200 } ) ) as unknown as typeof fetch ;
183+ expect ( await fetchDevinQuota ( "devin" , KEY , undefined ) ) . toBeNull ( ) ;
184+ } ) ;
185+
124186 test ( "a decoded status with nothing measurable is authoritative-empty" , async ( ) => {
125187 const unlimited = userStatusResponse ( planInfo ( { tier : 17 , name : "Max" , billing : 2 } ) , { 8 : - 1 } , false ) ;
126188 globalThis . fetch = ( async ( ) => new Response ( new Uint8Array ( unlimited ) , { status : 200 } ) ) as unknown as typeof fetch ;
0 commit comments