Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

2.3 KB
Raw Blame
// No LLM call and no database: `chat.models` reads only `ctx.config.ai` and the
// static whitelist.

import { expect, test } from 'bun:test';
import { testCoreConfig } from '../../../test/config-fixture';
import { stubHttpCtx, TEST_SESSION } from '../../../test/rpc-fixtures';
import { makeTrpcContext } from '../../rpc/base';
import type { CookieOptions } from '../../shared/cookie';
import { chatRouter } from './assistant.rpc';

const COOKIE_OPTIONS: CookieOptions = {
  domain: '.openpanel.dev',
  secure: true,
  sameSite: 'lax',
  httpOnly: true,
  path: '/',
};

// `TrpcContext.session` is never literally `null`, only its `userId` is.
const EMPTY_SESSION = { session: null, user: null, userId: null };

/** OPENAI_API_KEY / ANTHROPIC_API_KEY arrive as `ctx.config.ai`. */
function configWithKeys(keys: { openai?: string; anthropic?: string }) {
  const base = testCoreConfig();
  return testCoreConfig({
    ai: {
      openai: { ...base.ai.openai, apiKey: keys.openai },
      anthropic: { ...base.ai.anthropic, apiKey: keys.anthropic },
    },
  });
}

async function callerWith(
  session: unknown,
  keys: { openai?: string; anthropic?: string } = {}
) {
  const { ctx } = stubHttpCtx({ config: configWithKeys(keys) }, session);
  const trpcCtx = await makeTrpcContext(ctx, new Headers(), {
    cookieOptions: COOKIE_OPTIONS,
  });
  return chatRouter.createCaller(trpcCtx);
}

test('models rejects an unauthenticated caller before reading provider keys', async () => {
  const caller = await callerWith(EMPTY_SESSION);
  await expect(caller.models()).rejects.toMatchObject({
    code: 'UNAUTHORIZED',
  });
});

test('models returns no models and a null default with no provider keys set', async () => {
  const caller = await callerWith(TEST_SESSION);
  await expect(caller.models()).resolves.toEqual({
    providers: { openai: false, anthropic: false },
    models: [],
    defaultModelId: null,
    canChooseModel: false,
  });
});

test('models filters to the configured provider and prefers the recorded default', async () => {
  const caller = await callerWith(TEST_SESSION, { openai: 'sk-test' });
  const result = await caller.models();
  expect(result.providers).toEqual({ openai: true, anthropic: false });
  expect(result.models.every((m) => m.provider === 'openai')).toBe(true);
  expect(result.defaultModelId).toBe('gpt-6-1-sol');
});