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.2 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import { describeError } from './errorMessage';

const t = (key: string) => `<${key}>`;

describe('describeError', () => {
  it('turns the has-instances refusal code into its sentence', () => {
    expect(describeError(new Error('ENVIRONMENT_HAS_INSTANCES'), t, 'fallback')).toBe(
      '<environments.hasInstances>',
    );
  });

  it.each([
    ['FOLDER_NOT_EMPTY', 'environments.files.folderNotEmpty'],
    ['INSTANCE_READ_ONLY', 'environments.files.readOnly'],
    ['PATH_NOT_FOUND', 'environments.files.notFound'],
    ['DUPLICATE_INSTANCE_DIRECTORY', 'environments.files.folderTaken'],
  ])('turns the drive refusal %s into its sentence rather than the code', (code, key) => {
    expect(describeError(new Error(code), t, 'fallback')).toBe(`<${key}>`);
  });

  it('turns a path the server confined to the instance into the same line', () => {
    expect(describeError(new Error('PATH_OUTSIDE_INSTANCE'), t, 'fallback')).toBe(
      '<environments.files.invalidPath>',
    );
  });

  it.each([
    ['SNAPSHOT_IN_PROGRESS', 'environments.instances.stopSnapshotPending'],
    ['INSTANCE_STARTING', 'environments.instances.stopStarting'],
    ['INSTANCE_BUILDING', 'environments.instances.stopBuilding'],
    ['INSTANCE_BUSY', 'environments.instances.stopBusy'],
    ['INSTANCE_STOP_FAILED', 'environments.instances.stopRefused'],
  ])('turns the stop refusal %s into its sentence', (code, key) => {
    expect(describeError(new Error(code), t, 'fallback')).toBe(`<${key}>`);
  });

  it('turns a path validation issue list into the localized line instead of raw JSON', () => {
    const issues = JSON.stringify([
      {
        code: 'custom',
        message: 'Path must be a relative path inside the workspace',
        path: ['path'],
      },
    ]);
    expect(describeError(new Error(issues), t, 'fallback')).toBe(
      '<environments.files.invalidPath>',
    );
  });

  it('joins other validation issues into one readable message', () => {
    const issues = JSON.stringify([
      {
        code: 'too_big',
        message: 'Too big: expected string to have <=1048576 characters',
        path: ['content'],
      },
    ]);
    expect(describeError(new Error(issues), t, 'fallback')).toBe(
      'Too big: expected string to have <=1048576 characters',
    );
  });

  it('drops a drizzle query dump rather than showing SQL and row values', () => {
    // A missing column surfaced in the create dialog as a screenful of SQL with
    // the instance's own values in it; the caller's line at least says which
    // action failed.
    const drizzle = new Error(
      'Failed query: insert into "environment_instances" ("id", "name") values ' +
        '(default, $1) returning "id", "name" params: 7e3e4ab0,Lobehub Dev',
    );
    expect(describeError(drizzle, t, 'fallback')).toBe('fallback');
  });

  it('drops a message that dragged a stack trace along', () => {
    const stack = new Error('something broke\n    at Object.<anonymous> (/app/server.js:1:1)');
    expect(describeError(stack, t, 'fallback')).toBe('fallback');
  });

  it('keeps an ordinary message and falls back when there is none', () => {
    expect(describeError(new Error('could not delete'), t, 'fallback')).toBe('could not delete');
    expect(describeError({}, t, 'fallback')).toBe('fallback');
    expect(describeError(new Error('[not json'), t, 'fallback')).toBe('[not json');
  });
});

describe('refusals that arrive from the execution plane', () => {
  it("translates the market's in-use code instead of the sentence naming a row id", () => {
    // The market answers `{ error: ENVIRONMENT_IN_USE, error_description: 'Environment
    // "e299f341-…" is currently in use by an active session' }`; forwarding the
    // description put that uuid in a toast.
    expect(describeError({ message: 'ENVIRONMENT_IN_USE' }, t, 'fallback')).toBe(
      '<environments.instances.inUse>',
    );
  });

  it('falls back rather than printing a code it has no sentence for', () => {
    expect(describeError({ message: 'WORKSPACE_NOT_CONFIGURED' }, t, 'fallback')).toBe('fallback');
  });

  it('still shows a refusal written as a sentence', () => {
    expect(describeError({ message: 'The folder is not empty' }, t, 'fallback')).toBe(
      'The folder is not empty',
    );
  });
});