Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.3 KB
Raw Blame
import {
  AUTH_FAILURE_HEADER,
  AUTH_REQUIRED_HEADER,
  MARKET_AUTH_REQUIRED_MESSAGE,
  TRPC_ERROR_CODE_UNAUTHORIZED,
} from '@lobechat/desktop-bridge';
import { TRPCError } from '@trpc/server';
import { describe, expect, it } from 'vitest';

import { createResponseMeta } from './responseMeta';

describe('createResponseMeta', () => {
  it('should return undefined headers when no errors and no resHeaders', () => {
    const result = createResponseMeta({ ctx: undefined, errors: [] });
    expect(result.headers).toBeUndefined();
  });

  it('should forward resHeaders from context', () => {
    const resHeaders = new Headers({ 'X-Custom': 'value' });
    const result = createResponseMeta({
      ctx: { resHeaders },
      errors: [],
    });

    expect(result.headers).toBeInstanceOf(Headers);
    expect(result.headers?.get('X-Custom')).toBe('value');
  });

  it('should set AUTH_REQUIRED_HEADER header for UNAUTHORIZED error', () => {
    const error = new TRPCError({ code: TRPC_ERROR_CODE_UNAUTHORIZED });
    const result = createResponseMeta({
      ctx: undefined,
      errors: [error],
    });

    expect(result.headers).toBeInstanceOf(Headers);
    expect(result.headers?.get(AUTH_REQUIRED_HEADER)).toBe('true');
  });

  it('should set AUTH_REQUIRED_HEADER and preserve resHeaders for UNAUTHORIZED error', () => {
    const resHeaders = new Headers({ 'X-Custom': 'value' });
    const error = new TRPCError({ code: TRPC_ERROR_CODE_UNAUTHORIZED });
    const result = createResponseMeta({
      ctx: { resHeaders },
      errors: [error],
    });

    expect(result.headers).toBeInstanceOf(Headers);
    expect(result.headers?.get(AUTH_REQUIRED_HEADER)).toBe('true');
    expect(result.headers?.get('X-Custom')).toBe('value');
  });

  it('should NOT set AUTH_REQUIRED_HEADER for non-UNAUTHORIZED errors', () => {
    const error = new TRPCError({ code: 'BAD_REQUEST' });
    const result = createResponseMeta({
      ctx: undefined,
      errors: [error],
    });

    expect(result.headers).toBeUndefined();
  });

  it('should handle context without resHeaders property', () => {
    const error = new TRPCError({ code: TRPC_ERROR_CODE_UNAUTHORIZED });
    const result = createResponseMeta({
      ctx: { userId: 'test-user' },
      errors: [error],
    });

    expect(result.headers).toBeInstanceOf(Headers);
    expect(result.headers?.get(AUTH_REQUIRED_HEADER)).toBe('true');
  });

  it('should NOT set AUTH_REQUIRED_HEADER for Market OAuth UNAUTHORIZED errors', () => {
    const error = new TRPCError({
      code: TRPC_ERROR_CODE_UNAUTHORIZED,
      message: MARKET_AUTH_REQUIRED_MESSAGE,
    });
    const result = createResponseMeta({
      ctx: undefined,
      errors: [error],
    });

    expect(result.headers).toBeUndefined();
  });

  it('should NOT set AUTH_REQUIRED_HEADER for runtime provider auth errors', () => {
    const error = new TRPCError({
      cause: { errorType: 'InvalidProviderAPIKey' },
      code: TRPC_ERROR_CODE_UNAUTHORIZED,
      message: 'InvalidProviderAPIKey',
    });

    const result = createResponseMeta({
      ctx: undefined,
      errors: [error],
    });

    expect(result.headers).toBeUndefined();
  });

  it('should handle multiple errors where one is UNAUTHORIZED', () => {
    const errors = [
      new TRPCError({ code: 'BAD_REQUEST' }),
      new TRPCError({ code: TRPC_ERROR_CODE_UNAUTHORIZED }),
    ];
    const result = createResponseMeta({
      ctx: undefined,
      errors,
    });

    expect(result.headers).toBeInstanceOf(Headers);
    expect(result.headers?.get(AUTH_REQUIRED_HEADER)).toBe('true');
  });

  it('keeps AUTH_FAILURE_HEADER next to an UNAUTHORIZED error', () => {
    const resHeaders = new Headers({ [AUTH_FAILURE_HEADER]: 'jwt_expired' });
    const result = createResponseMeta({
      ctx: { resHeaders },
      errors: [new TRPCError({ code: TRPC_ERROR_CODE_UNAUTHORIZED })],
    });

    expect(result.headers?.get(AUTH_REQUIRED_HEADER)).toBe('true');
    expect(result.headers?.get(AUTH_FAILURE_HEADER)).toBe('jwt_expired');
  });

  it('drops AUTH_FAILURE_HEADER when the response is not an UNAUTHORIZED error', () => {
    const resHeaders = new Headers({ [AUTH_FAILURE_HEADER]: 'no_token', 'X-Custom': 'v' });
    const result = createResponseMeta({ ctx: { resHeaders }, errors: [] });

    expect(result.headers?.get(AUTH_FAILURE_HEADER)).toBeNull();
    expect(result.headers?.get('X-Custom')).toBe('v');
  });
});