Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
1.5 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest'
import type { AuthClient } from '../authClient.js'
import { RpcTokenManager } from '../rpcTokenManager.js'
import type { RpcTokenResult } from '../types.js'

function deferred<T>() {
  let resolve!: (value: T) => void
  const promise = new Promise<T>(done => {
    resolve = done
  })
  return { promise, resolve }
}

const issuedToken = (token: string): RpcTokenResult => ({
  token,
  accessScope: 'team',
  teamId: 'team-a',
  scopes: ['host:activity:read'],
  hostRefs: ['chatllm'],
  expiresInSeconds: 300,
})

describe('RpcTokenManager', () => {
  it('does not cache a token whose request completed after the cache was cleared', async () => {
    const pending = deferred<RpcTokenResult>()
    const issueRpcToken = vi
      .fn()
      .mockReturnValueOnce(pending.promise)
      .mockResolvedValueOnce(issuedToken('test-token-new'))
    const manager = new RpcTokenManager({ issueRpcToken } as unknown as AuthClient)

    const oldSessionRequest = manager.getOrIssue('old-session', ['host:activity:read'], ['chatllm'])
    manager.clear()
    pending.resolve(issuedToken('test-token-old'))
    await expect(oldSessionRequest).resolves.toMatchObject({ token: 'test-token-old' })

    expect(manager.getMetadata()).toEqual({ expiresAtMs: null, scopes: [], hostRefs: [] })
    await expect(
      manager.getOrIssue('new-session', ['host:activity:read'], ['chatllm'])
    ).resolves.toMatchObject({ token: 'test-token-new' })
    expect(issueRpcToken).toHaveBeenCalledTimes(2)
  })
})