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

/**
 * R8UXB-008 — Dual hub source-of-truth drift guard.
 *
 * ContentHub.tsx and RecoverHub.tsx both render Content cells. This test
 * asserts that both files import from content-hub-cells.ts (the shared
 * module) so there is exactly ONE authoritative cell definition.
 *
 * Future drift: if a dev adds a Content cell directly in ContentHub.tsx or
 * RecoverHub.tsx instead of updating content-hub-cells.ts, this test fails CI.
 *
 * Also asserts that the ContentHub route now redirects to the Recover hub
 * server-side (R8UXB-008 preferred resolution).
 */
describe('ContentHub drift guard — R8UXB-008', () => {
  it('ContentHub imports from content-hub-cells.ts (shared source of truth)', async () => {
    // The preferred resolution: ContentHub.tsx still exists for the PHP contract
    // tests but ContentHubController now redirects server-side. ContentHub.tsx
    // must import CONTENT_CELLS so it stays in sync if ever rendered directly.
    const contentHubSrc = await import('./ContentHub?raw');
    expect(contentHubSrc.default).toContain("from './content-hub-cells'");
  });

  it('RecoverHub sources Content cells from content-hub-cells.ts', async () => {
    const recoverHubSrc = await import('./RecoverHub?raw');
    // RecoverHub must either import CONTENT_CELLS or use contentCell() helper.
    const usesSharedCells =
      recoverHubSrc.default.includes("from './content-hub-cells'") ||
      recoverHubSrc.default.includes('contentCell(');
    expect(usesSharedCells).toBe(true);
  });

  it('neither ContentHub nor RecoverHub defines a standalone CONTENT_CELLS array', async () => {
    const contentHubSrc = await import('./ContentHub?raw');
    const recoverHubSrc = await import('./RecoverHub?raw');

    // Neither file should define its own `const CONTENT_CELLS = [...]` —
    // that must live exclusively in content-hub-cells.ts.
    const contentHubHasStandaloneArray = /const CONTENT_CELLS\s*=\s*\[/.test(contentHubSrc.default);
    const recoverHubHasStandaloneArray = /const CONTENT_CELLS\s*=\s*\[/.test(recoverHubSrc.default);

    expect(contentHubHasStandaloneArray).toBe(false);
    expect(recoverHubHasStandaloneArray).toBe(false);
  });
});
