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

import { buildSkillDraftPayload, validateSkillBasic } from './skill-draft';

describe('skill draft payload', () => {
  it('only sends editable draft fields and omits the id for create', () => {
    const payload = buildSkillDraftPayload(
      {
        approvalPending: true,
        categoryId: 3,
        description: 'description',
        displayName: 'Display name',
        id: 9,
        name: 'standard-name',
        packageHash: 'must-not-leak',
        resources: [{ contentRef: 'must-not-leak', path: 'secret.md' }],
        skillContent: 'content',
        visibilityScope: 'PRIVATE',
      },
      false,
    );

    expect(payload).toEqual({
      categoryId: 3,
      displayName: 'Display name',
      skillContent: 'content',
      visibilityScope: 'PRIVATE',
    });
  });

  it('includes the id and excludes stale content for update', () => {
    expect(
      buildSkillDraftPayload({ id: 9, skillContent: 'stale content' }, true),
    ).toEqual({
      categoryId: undefined,
      displayName: undefined,
      id: 9,
      visibilityScope: undefined,
    });
  });
});

describe('skill basic validation', () => {
  it('requires canonical standard metadata when creating a Skill', () => {
    expect(
      validateSkillBasic(
        { description: '', displayName: '', name: 'Invalid_Name' },
        true,
      ),
    ).toEqual({
      description: '请输入标准描述',
      displayName: '请输入显示名称',
      name: '仅支持小写字母、数字和连字符',
    });
  });

  it('does not revalidate frontmatter-only fields for an existing Skill', () => {
    expect(
      validateSkillBasic(
        { description: '', displayName: 'Display', name: '' },
        false,
      ),
    ).toEqual({});
  });

  it('uses the same basic-field limits as the backend Skill contract', () => {
    expect(
      validateSkillBasic(
        {
          description: 'd'.repeat(1025),
          displayName: 'x'.repeat(129),
          name: 'a'.repeat(65),
        },
        true,
      ),
    ).toEqual({
      description: '标准描述不能超过 1024 个字符',
      displayName: '显示名称不能超过 128 个字符',
      name: '标准名称不能超过 64 个字符',
    });
    expect(
      validateSkillBasic(
        {
          description: 'd'.repeat(1024),
          displayName: 'x'.repeat(128),
          name: 'a'.repeat(64),
        },
        true,
      ),
    ).toEqual({});
  });
});
