import { provideHttpClient } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { provideRouter } from '@angular/router';
import { AUTH_BFF_BASE_URL } from 'feature-auth';
import { App } from './app';

const BFF_BASE = 'http://bff.test/api';

describe('App', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [App],
      providers: [
        provideRouter([]),
        provideHttpClient(),
        provideHttpClientTesting(),
        { provide: AUTH_BFF_BASE_URL, useValue: BFF_BASE },
      ],
    }).compileComponents();
  });

  it('renders the layout shell — skip link, header, sidebar, main, footer', async () => {
    const fixture = TestBed.createComponent(App);
    await fixture.whenStable();
    const root = fixture.nativeElement as HTMLElement;
    expect(root.querySelector('a.skip-link')).not.toBeNull();
    expect(root.querySelector('app-header')).not.toBeNull();
    expect(root.querySelector('app-sidebar')).not.toBeNull();
    expect(root.querySelector('main#main-content')).not.toBeNull();
    expect(root.querySelector('app-footer')).not.toBeNull();
    // Drain the bootstrap /me request fired by the Header's AuthService.
    const httpCtrl = TestBed.inject(HttpTestingController);
    httpCtrl
      .expectOne(`${BFF_BASE}/auth/me`)
      .flush({}, { status: 401, statusText: 'Unauthorized' });
  });
});
