// #region HealthPageIntegrationTest:Module [TYPE Function]
// @SEMANTICS: health-page, integration-test, slug-link, delete-flow
// @PURPOSE: Lock dashboard health page contract for slug navigation and report deletion.
// @LAYER UI (Tests)
// @RELATION DEPENDS_ON -> [HealthCenterPage]

import { describe, it, expect } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';

const PAGE_PATH = path.resolve(
  process.cwd(),
  'src/routes/dashboards/health/+page.svelte',
);

describe('Dashboard health page contract', () => {
  it('renders slug-first dashboard link bound to environment route context', () => {
    const source = fs.readFileSync(PAGE_PATH, 'utf-8');

    expect(source).toContain("{$t.health?.validation_details || 'Dashboards'}");
    expect(source).toContain("item.dashboard_slug || item.dashboard_id");
    expect(source).toContain("href={`/dashboards/${encodeURIComponent(String(item.dashboard_slug || item.dashboard_id))}?env_id=${encodeURIComponent(item.environment_id)}`}");
  });

  it('keeps explicit delete report flow with confirm and DELETE request', () => {
    const source = fs.readFileSync(PAGE_PATH, 'utf-8');

    expect(source).toContain('async function handleDeleteReport(item: HealthItem)');
    expect(source).toContain("requestApi(`/health/summary/${item.record_id}`, 'DELETE')");
    expect(source).toContain(".replace('{slug}', slug)");
    expect(source).toContain("$t.common?.delete");
  });

  it('reuses global health store refresh for all-environment summary loads', () => {
    const source = fs.readFileSync(PAGE_PATH, 'utf-8');

    expect(source).toContain("import { healthStore } from '$lib/stores/health.svelte.js';");
    expect(source).toContain('? getHealthSummary(selectedEnvId)');
    expect(source).toContain(': healthStore.refresh();');
  });
});
// #endregion HealthPageIntegrationTest:Module
