import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { <%= classify(name) %> } from './<%= dasherize(name) %>';

@Component({
  imports: [<%= classify(name) %>],
  template: `
    <<%= selector %>
      label="Código de segurança"
      hint="Use o código do seu aplicativo bancário."
      error="Informe o código de 6 dígitos."
      [(value)]="code"
    />
  `,
})
class HostComponent {
  code = '';
}

describe('<%= classify(name) %>', () => {
  let fixture: ComponentFixture<HostComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({ imports: [HostComponent] }).compileComponents();
    fixture = TestBed.createComponent(HostComponent);
    fixture.detectChanges();
  });

  function input(): HTMLInputElement {
    return fixture.nativeElement.querySelector('input');
  }

  it('wires label, autocomplete, length, hint, and error message', () => {
    const label = fixture.nativeElement.querySelector('label');
    const hint = fixture.nativeElement.querySelector('.otp-field__hint');
    const error = fixture.nativeElement.querySelector('.otp-field__error');

    expect(label.textContent).toContain('Código de segurança');
    expect(label.getAttribute('for')).toBe(input().id);
    expect(input().autocomplete).toBe('one-time-code');
    expect(input().inputMode).toBe('numeric');
    expect(input().getAttribute('maxlength')).toBe('6');
    expect(input().getAttribute('aria-invalid')).toBe('true');
    expect(input().getAttribute('aria-describedby')).toContain(hint.id);
    expect(input().getAttribute('aria-describedby')).toContain(error.id);
    expect(error.getAttribute('role')).toBe('alert');
  });

  it('keeps only numeric characters and updates the bound code', () => {
    input().value = '12a 34567';
    input().dispatchEvent(new Event('input', { bubbles: true }));
    fixture.detectChanges();

    expect(fixture.componentInstance.code).toBe('123456');
  });
});
