import { describe, expect, it } from 'vitest';

import {
  isSkillAccessDeniedError,
  isSkillFileConflictError,
  resolveSkillApiErrorMessage,
} from './skill-api-error';

describe('skill API conflict errors', () => {
  it('recognizes response bodies thrown directly by the request client', () => {
    expect(
      isSkillFileConflictError({
        errorCode: 4091,
        message: '文件已被其他操作更新',
      }),
    ).toBe(true);
  });

  it('also recognizes Axios-shaped conflicts and ignores ordinary errors', () => {
    expect(
      isSkillFileConflictError({ response: { data: {}, status: 409 } }),
    ).toBe(true);
    expect(isSkillFileConflictError({ errorCode: 500 })).toBe(false);
  });

  it('recognizes direct and Axios-shaped access denials', () => {
    expect(isSkillAccessDeniedError({ errorCode: 403 })).toBe(true);
    expect(
      isSkillAccessDeniedError({ response: { data: {}, status: 401 } }),
    ).toBe(true);
    expect(isSkillAccessDeniedError({ errorCode: 500 })).toBe(false);
  });

  it('keeps backend business messages for recoverable category actions', () => {
    expect(
      resolveSkillApiErrorMessage(
        { response: { data: { message: '请先迁移该分类下的 Skill' } } },
        '删除失败',
      ),
    ).toBe('请先迁移该分类下的 Skill');
    expect(resolveSkillApiErrorMessage(undefined, '删除失败')).toBe('删除失败');
  });
});
