/**
 * SET 43d — clade expansion drafts from the empty-draw drop ledger.
 * Drafts are NOT oracle-admitted; mutation-fixture oracle remains the gate.
 */
import { describe, expect, it } from 'vitest';
import {
  FAMILY_BRANCH,
  FORMAT_STRING_CLADE,
  PAGINATION_CLADE,
  plannedCladeFamily,
  RESPONSE_SHAPE_CLADE,
} from '../src/tier1/clades';
import {
  CLADE_DRAFTS_43,
  DRAFT43_FAMILY_IDS,
  draftBodyForFamily,
  matchDraft43Family,
  registerDraft43Clades,
} from '../src/tier1/cladeDrafts43';

describe('SET 43d — draft clade families', () => {
  it('each family has a non-empty draft body with expect()', () => {
    for (const family of DRAFT43_FAMILY_IDS) {
      const draft = CLADE_DRAFTS_43.find(d => d.family === family);
      expect(draft, `missing draft for ${family}`).toBeDefined();
      expect(draft!.body.trim().length).toBeGreaterThan(0);
      expect(draft!.body).toContain('expect(');
      expect(draft!.admission).toBe('draft-pending-mutation-oracle');
      expect(draftBodyForFamily(family)).toBe(draft!.body);
    }
    expect(CLADE_DRAFTS_43).toHaveLength(3);
  });

  it('plannedCladeFamily recognizes representative names', () => {
    expect(plannedCladeFamily({ name: 'HttpStatusCodes' })).toBe(RESPONSE_SHAPE_CLADE);
    expect(plannedCladeFamily({ name: 'ApiErrorBody' })).toBe(RESPONSE_SHAPE_CLADE);
    expect(plannedCladeFamily({ name: 'StructuredLogFormat' })).toBe(FORMAT_STRING_CLADE);
    expect(plannedCladeFamily({ name: 'json log line' })).toBe(FORMAT_STRING_CLADE);
    expect(plannedCladeFamily({ name: 'ListPage' })).toBe(PAGINATION_CLADE);
    expect(plannedCladeFamily({ name: 'CursorOffset' })).toBe(PAGINATION_CLADE);
    expect(plannedCladeFamily({ name: 'LinkHeader' })).toBe(PAGINATION_CLADE);

    expect(matchDraft43Family('HttpStatusCodes')).toBe(RESPONSE_SHAPE_CLADE);
    expect(matchDraft43Family('adds numbers')).toBeNull();
    expect(plannedCladeFamily({ name: 'adds numbers' })).toBeNull();
  });

  it('FAMILY_BRANCH maps each draft family to a branch', () => {
    expect(FAMILY_BRANCH[RESPONSE_SHAPE_CLADE]).toBe('contract');
    expect(FAMILY_BRANCH[FORMAT_STRING_CLADE]).toBe('representation');
    expect(FAMILY_BRANCH[PAGINATION_CLADE]).toBe('state');
  });

  it('registerDraft43Clades exposes matcher + admission note', () => {
    const reg = registerDraft43Clades();
    expect(reg.families).toEqual([...DRAFT43_FAMILY_IDS]);
    expect(reg.drafts).toHaveLength(3);
    expect(reg.match('ApiErrorBody')).toBe(RESPONSE_SHAPE_CLADE);
    expect(reg.admissionNote).toMatch(/mutation-fixture oracle/i);
  });
});
