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.

5.6 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import { normalizeChatMessageError } from './normalizeChatMessageError';

describe('normalizeChatMessageError', () => {
  it.each([
    { body: { error: { message: 'insufficient quota' } }, type: 'ProviderBizError' },
    { error: { message: 'insufficient quota' }, errorType: 'ProviderBizError' },
    { message: 'insufficient quota' },
    'insufficient quota',
  ])('normalizes equivalent quota errors: %j', (input) => {
    const result = normalizeChatMessageError(input);
    expect(result).toMatchObject({
      attribution: 'user',
      message: 'insufficient quota',
      numericId: 2001,
      retryable: false,
      type: 'InsufficientQuota',
    });
    expect(normalizeChatMessageError(result)).toEqual(result);
  });

  it('preserves a non-enumerable Error message with a known errorType', () => {
    const error = Object.assign(new Error('provider credentials rejected'), {
      errorType: 'InvalidProviderAPIKey',
    });
    expect(normalizeChatMessageError(error)).toMatchObject({
      message: 'provider credentials rejected',
      type: 'InvalidProviderAPIKey',
    });
  });

  it('keeps a known code authoritative over a misleading message', () => {
    expect(
      normalizeChatMessageError({ code: 'PermissionDenied', message: 'insufficient quota' }),
    ).toMatchObject({ type: 'PermissionDenied' });
  });

  it('preserves hetero guide metadata and recovery context', () => {
    const body = {
      agentType: 'claude-code',
      code: 'rate_limit',
      details: { kind: 'usage_limit' },
      message: 'Weekly allowance exhausted',
      rateLimitInfo: { rateLimitType: 'seven_day', resetsAt: 1789826400 },
    };
    expect(normalizeChatMessageError(body)).toMatchObject({ body });
  });

  it('merges upstream status with display context and preserves budget', () => {
    const result = normalizeChatMessageError({
      _responseBody: { error: { message: 'Payment required' } },
      budget: { budgetTypeAtError: 'workspace' },
      error: { status: 402 },
      errorType: 'ProviderBizError',
      provider: 'lobehub',
    });
    expect(result).toMatchObject({
      body: {
        budget: { budgetTypeAtError: 'workspace' },
        error: { message: 'Payment required', status: 402 },
        provider: 'lobehub',
      },
      type: 'InsufficientQuota',
    });
  });

  it('keeps hook display fields when a stream error already carries a provider body', () => {
    const providerBody = { message: 'Provider timed out', provider: 'lobehub' };
    const result = normalizeChatMessageError({
      _responseBody: { traceId: 'trace-1' },
      body: providerBody,
      errorType: 'ProviderBizError',
      message: 'LLM stream error: Provider timed out',
    });

    expect(result.body).toMatchObject({ ...providerBody, traceId: 'trace-1' });
  });

  it('keeps hook display fields on a typed stream error payload', () => {
    const providerBody = { code: 500, message: 'Internal error', provider: 'lobehub' };
    const result = normalizeChatMessageError({
      _responseBody: { traceId: 'trace-1' },
      body: providerBody,
      message: 'Internal error',
      type: 'ProviderBizError',
    });

    expect(result.body).toEqual({ ...providerBody, traceId: 'trace-1' });
    expect(result).not.toHaveProperty('_responseBody');
  });

  it('keeps hook display fields on a plain Error', () => {
    const error = Object.assign(new TypeError('terminated'), {
      _responseBody: { traceId: 'trace-1' },
    });

    expect(normalizeChatMessageError(error)).toMatchObject({
      body: { name: 'TypeError', traceId: 'trace-1' },
      message: 'terminated',
    });
  });

  it('does not infer a provider outage from an unclassified internal 500', () => {
    expect(normalizeChatMessageError(new Error('500 unexpected internal failure'))).toMatchObject({
      type: 500,
    });
  });
});

describe('platform admission 403s', () => {
  const planGate =
    'Model "gemini-4-argon" is available on paid plans only (model tier: pro). Current personal plan: free.';

  it('classifies a plan-tier refusal as a user-side quota state, keeping its type', () => {
    expect(
      normalizeChatMessageError({
        error: { message: 403 },
        errorType: 403,
        message: planGate,
        provider: 'lobehub',
      }),
    ).toMatchObject({
      attribution: 'user',
      category: 'quota',
      countAsFailure: false,
      message: planGate,
      retryable: false,
      type: 403,
    });
  });

  it('classifies a usage-security refusal by its reason, as stored and as thrown', () => {
    const thrown = {
      _responseBody: { provider: 'lobehub', reason: 'not_onboarded' },
      error: { message: 403 },
      errorType: 403,
      message: 'Request blocked.',
      provider: 'lobehub',
    };
    const stored = {
      body: {
        error: { message: 403 },
        message: 'Request blocked.',
        provider: 'lobehub',
        reason: 'not_onboarded',
      },
      message: 'Request blocked.',
      type: 403,
    };

    for (const error of [thrown, stored])
      expect(normalizeChatMessageError(error)).toMatchObject({
        attribution: 'user',
        category: 'auth',
        countAsFailure: false,
        type: 403,
      });
  });

  it('leaves a bare Forbidden that lost its explanation unclassified', () => {
    const normalized = normalizeChatMessageError({
      body: {
        error: { message: 403 },
        message: 'Forbidden',
        model: 'gpt-6-astra',
        provider: 'lobehub',
      },
      message: 'Forbidden',
      type: 403,
    });

    expect(normalized.category).toBeUndefined();
    expect(normalized.attribution).toBeUndefined();
  });
});