import { RouterUsageAdapter } from './router-usage.adapter'; const stats = {totalRequests:0,totalPromptTokens:0,totalCompletionTokens:0,totalCachedTokens:0,byModel:{},byProvider:{},byAccount:{}}; describe('bounded read-only adapter', () => { afterEach(() => jest.restoreAllMocks()); it('uses only authenticated GET stats with a validated period and never follows redirects', async () => { const fetcher = jest.spyOn(global, 'fetch').mockResolvedValue(new Response(JSON.stringify(stats))); const adapter = new RouterUsageAdapter(); process.env.ROUTER_USAGE_BASE_URL = 'https://nr.parsaaghayi.ir'; process.env.ROUTER_USAGE_CLI_TOKEN = 'server-only-secret'; const result = await adapter.summary('24h'); expect(result.status).toBe('available'); expect(result.attribution).toEqual({scope:'9router_total',hermes:null,additive:false}); expect(fetcher).toHaveBeenCalledTimes(1); expect(fetcher).toHaveBeenCalledWith('https://nr.parsaaghayi.ir/api/usage/stats?period=24h', expect.objectContaining({method:'GET',redirect:'error',headers:{'x-9r-cli-token':'server-only-secret',Accept:'application/json'}})); expect(JSON.stringify(result)).not.toContain('server-only-secret'); }); it('rejects invalid time filters before IO', async () => { const fetcher = jest.spyOn(global,'fetch'); await expect(new RouterUsageAdapter().summary('../providers')).rejects.toThrow(); expect(fetcher).not.toHaveBeenCalled(); }); it('fails closed when credential is missing', async () => { delete process.env.ROUTER_USAGE_CLI_TOKEN; const fetcher = jest.spyOn(global,'fetch'); const result = await new RouterUsageAdapter().summary('7d'); expect(result).toMatchObject({status:'unavailable',data:null,reason:'not_configured'}); expect(fetcher).not.toHaveBeenCalled(); }); it.each([401,403,429,500])('returns unknown data without upstream error text for %s', async status => { process.env.ROUTER_USAGE_CLI_TOKEN='secret'; jest.spyOn(global,'fetch').mockResolvedValue(new Response('credential-secret',{status})); const result = await new RouterUsageAdapter().summary('7d'); expect(result).toMatchObject({status:'unavailable',data:null,reason:'upstream_unavailable'}); expect(JSON.stringify(result)).not.toContain('credential-secret'); }); it('rejects oversized bodies even without content length', async () => { process.env.ROUTER_USAGE_CLI_TOKEN='secret'; jest.spyOn(global,'fetch').mockResolvedValue(new Response('x'.repeat(1048577))); expect(await new RouterUsageAdapter().summary('7d')).toMatchObject({status:'unavailable',data:null}); }); it('does not return stale last-good totals after a failure', async () => { process.env.ROUTER_USAGE_CLI_TOKEN='secret'; jest.spyOn(global,'fetch').mockResolvedValueOnce(new Response(JSON.stringify(stats))).mockRejectedValueOnce(new Error('token-secret')); const adapter=new RouterUsageAdapter(); expect((await adapter.summary('7d')).status).toBe('available'); expect(await adapter.summary('7d')).toMatchObject({status:'unavailable',data:null}); }); }); describe('transport boundaries', () => { afterEach(()=>jest.restoreAllMocks()); it('never sends credentials to a configured alternate origin', async()=>{ process.env.ROUTER_USAGE_CLI_TOKEN='secret'; process.env.ROUTER_USAGE_BASE_URL='https://attacker.invalid'; const fetcher=jest.spyOn(global,'fetch'); expect(await new RouterUsageAdapter().summary()).toMatchObject({status:'unavailable',reason:'invalid_configuration'}); expect(fetcher).not.toHaveBeenCalled(); delete process.env.ROUTER_USAGE_BASE_URL; }); it('bounds simultaneous upstream calls and exposes no stale totals', async()=>{ process.env.ROUTER_USAGE_CLI_TOKEN='secret'; let resolve!: (value: Response) => void; const pending=new Promise(r=>{resolve=r;}); jest.spyOn(global,'fetch').mockReturnValue(pending); const adapter=new RouterUsageAdapter(); const calls=Array.from({length:4},()=>adapter.summary()); expect(await adapter.summary()).toMatchObject({status:'unavailable',reason:'busy',data:null}); resolve(new Response('',{status:503})); await Promise.all(calls); }); });