Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

4.8 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import type { ProviderRateLimits } from './rate-limit-types'
import type { CodexManagedAccountSummary } from './managed-account-types'
import { buildCodexResetCreditExpectedScope } from './codex-reset-credit-scope'

const account: CodexManagedAccountSummary = {
  id: 'account-host',
  email: '[email protected]',
  managedHomeRuntime: 'host',
  wslDistro: null,
  createdAt: 10,
  updatedAt: 20,
  lastAuthenticatedAt: 20
}

const limits: ProviderRateLimits = {
  provider: 'codex',
  session: {
    usedPercent: 100,
    windowMinutes: 300,
    resetsAt: 1_000,
    resetDescription: 'soon'
  },
  weekly: null,
  rateLimitResetCredits: {
    availableCount: 1,
    totalEarnedCount: 2,
    nextExpiresAt: 2_000,
    credits: [{ status: 'available', expiresAt: 2_000, grantedAt: 500 }]
  },
  updatedAt: 30,
  error: null,
  status: 'ok'
}

describe('buildCodexResetCreditExpectedScope', () => {
  it('builds a deterministic exact host scope', () => {
    const first = buildCodexResetCreditExpectedScope({
      target: { runtime: 'host', wslDistro: null },
      account,
      limits
    })
    const second = buildCodexResetCreditExpectedScope({
      target: { runtime: 'host', wslDistro: null },
      account,
      limits: {
        ...limits,
        rateLimitResetCredits: {
          ...limits.rateLimitResetCredits!,
          credits: [
            { status: 'available', expiresAt: 3_000, grantedAt: 600 },
            ...limits.rateLimitResetCredits!.credits!
          ]
        }
      }
    })

    const firstWithBothRows = buildCodexResetCreditExpectedScope({
      target: { runtime: 'host', wslDistro: null },
      account,
      limits: {
        ...limits,
        rateLimitResetCredits: {
          ...limits.rateLimitResetCredits!,
          credits: [
            ...limits.rateLimitResetCredits!.credits!,
            { status: 'available', expiresAt: 3_000, grantedAt: 600 }
          ]
        }
      }
    })

    expect(firstWithBothRows).toEqual(second)
    expect(first).toMatchObject({
      target: { runtime: 'host', wslDistro: null },
      accountId: account.id,
      accountRevision: account.updatedAt,
      offerRevision: expect.stringMatching(/^v1:/)
    })
  })

  it('changes the offer revision when fetched credit or window data changes', () => {
    const original = buildCodexResetCreditExpectedScope({
      target: { runtime: 'host', wslDistro: null },
      account,
      limits
    })
    const refreshed = buildCodexResetCreditExpectedScope({
      target: { runtime: 'host', wslDistro: null },
      account,
      limits: { ...limits, updatedAt: limits.updatedAt + 1 }
    })

    expect(refreshed?.offerRevision).not.toBe(original?.offerRevision)
    const changedCredits = buildCodexResetCreditExpectedScope({
      target: { runtime: 'host', wslDistro: null },
      account,
      limits: {
        ...limits,
        rateLimitResetCredits: {
          availableCount: 1,
          totalEarnedCount: 2,
          nextExpiresAt: 2_000,
          credits: [{ status: 'available', expiresAt: 3_000, grantedAt: 500 }]
        }
      }
    })
    expect(changedCredits?.offerRevision).not.toBe(original?.offerRevision)
    const changedWindow = buildCodexResetCreditExpectedScope({
      target: { runtime: 'host', wslDistro: null },
      account,
      limits: {
        ...limits,
        session: {
          usedPercent: 50,
          windowMinutes: 300,
          resetsAt: 1_000,
          resetDescription: 'soon'
        }
      }
    })
    expect(changedWindow?.offerRevision).not.toBe(original?.offerRevision)
  })

  it.each([
    {
      name: 'system default account',
      target: { runtime: 'host', wslDistro: null } as const,
      candidate: null,
      candidateLimits: limits
    },
    {
      name: 'no available credit',
      target: { runtime: 'host', wslDistro: null } as const,
      candidate: account,
      candidateLimits: {
        ...limits,
        rateLimitResetCredits: { ...limits.rateLimitResetCredits!, availableCount: 0 }
      }
    },
    {
      name: 'unknown WSL distro',
      target: { runtime: 'wsl', wslDistro: null } as const,
      candidate: { ...account, managedHomeRuntime: 'wsl' as const, wslDistro: 'Ubuntu' },
      candidateLimits: limits
    },
    {
      name: 'runtime mismatch',
      target: { runtime: 'wsl', wslDistro: 'Ubuntu' } as const,
      candidate: account,
      candidateLimits: limits
    },
    {
      name: 'WSL distro mismatch',
      target: { runtime: 'wsl', wslDistro: 'Ubuntu' } as const,
      candidate: { ...account, managedHomeRuntime: 'wsl' as const, wslDistro: 'Debian' },
      candidateLimits: limits
    }
  ])('fails closed for $name', ({ target, candidate, candidateLimits }) => {
    expect(
      buildCodexResetCreditExpectedScope({
        target,
        account: candidate,
        limits: candidateLimits
      })
    ).toBeNull()
  })
})