Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

7.3 KB
Raw Blame
/**
 * Navigation completeness tests.
 *
 * Validates that every entry in meta.json files maps to a real .mdx file or
 * directory, and that every content file is referenced in its parent meta.json.
 */
import { describe, test, expect } from "bun:test";
import { readdir, readFile, stat } from "fs/promises";
import { join, basename, dirname, relative } from "path";

const CONTENT_DIR = join(import.meta.dir, "../../content/docs");
const LAYOUT_OPTIONS = join(import.meta.dir, "../../lib/layout.shared.tsx");
const GLOBAL_SEARCH = join(import.meta.dir, "../../components/custom-search-dialog.tsx");

/** Separator entries in meta.json start with --- */
function isSeparator(entry: string): boolean {
  return entry.startsWith("---");
}

/** Recursively find all meta.json files under a directory */
async function findMetaFiles(dir: string): Promise<string[]> {
  const results: string[] = [];
  const entries = await readdir(dir, { withFileTypes: true });

  for (const entry of entries) {
    const fullPath = join(dir, entry.name);
    if (entry.isDirectory()) {
      results.push(...(await findMetaFiles(fullPath)));
    } else if (entry.name === "meta.json") {
      results.push(fullPath);
    }
  }
  return results;
}

/** Check if a path exists as a file or directory */
async function exists(path: string): Promise<boolean> {
  try {
    await stat(path);
    return true;
  } catch {
    return false;
  }
}

describe("Navigation - meta.json validity", () => {
  test("root navigation separates current and legacy paths", async () => {
    const metaPath = join(CONTENT_DIR, "meta.json");
    const meta = JSON.parse(await readFile(metaPath, "utf-8"));
    const pages = meta.pages as string[];
    const separators = pages.filter(isSeparator);

    expect(separators).toEqual([
      "---Get Started---",
      "---Core concepts---",
      "---Guides---",
      "---Direct execution (legacy)---",
      "---Migration and security---",
    ]);

    expect(pages.slice(1, pages.indexOf("---Core concepts---"))).toEqual([
      "index",
      "quickstart",
      "providers",
      "agent-plugins",
      "cli",
      "composio-connect",
    ]);

    expect(
      pages.slice(
        pages.indexOf("---Direct execution (legacy)---") + 1,
        pages.indexOf("---Migration and security---")
      )
    ).toEqual([
      "sessions-vs-direct-execution",
      "tools-direct",
      "auth-configuration",
    ]);
  });

  test("Knowledge Base appears between Docs and Examples", async () => {
    const source = await readFile(LAYOUT_OPTIONS, "utf-8");
    const docsIndex = source.indexOf("text: 'Docs'");
    const kbIndex = source.indexOf("text: 'Knowledge Base'");
    const examplesIndex = source.indexOf("text: 'Examples'");

    expect(docsIndex).toBeGreaterThan(-1);
    expect(kbIndex).toBeGreaterThan(docsIndex);
    expect(examplesIndex).toBeGreaterThan(kbIndex);
  });

  test("global search uses canonical knowledge URLs and shared source labels", async () => {
    const source = await readFile(GLOBAL_SEARCH, "utf-8");
    expect(source).toContain("KNOWLEDGE_SOURCE_LABELS");
    expect(source).toContain("canonical_url");
    expect(source).toContain("source_type");
    expect(source).toContain("algoliaHitMetaRef.current.get(href)");
  });

  test("root meta.json entries all resolve to files or directories", async () => {
    const metaPath = join(CONTENT_DIR, "meta.json");
    const meta = JSON.parse(await readFile(metaPath, "utf-8"));
    const missing: string[] = [];

    for (const entry of meta.pages as string[]) {
      if (isSeparator(entry)) continue;
      if (entry === "...") continue;

      const asFile = join(CONTENT_DIR, `${entry}.mdx`);
      const asDir = join(CONTENT_DIR, entry);

      const fileExists = await exists(asFile);
      const dirExists = await exists(asDir);

      if (!fileExists && !dirExists) {
        missing.push(entry);
      }
    }

    expect(missing).toEqual([]);
  });

  test("all nested meta.json entries resolve to files or directories", async () => {
    const metaFiles = await findMetaFiles(CONTENT_DIR);
    const errors: string[] = [];

    for (const metaPath of metaFiles) {
      const dir = dirname(metaPath);
      const meta = JSON.parse(await readFile(metaPath, "utf-8"));
      const relDir = relative(CONTENT_DIR, dir);

      for (const entry of (meta.pages || []) as string[]) {
        if (isSeparator(entry)) continue;

        // Handle "..." (rest) entries which are valid fumadocs syntax
        if (entry === "...") continue;

        const link = entry.match(/^\[[^\]]+\]\((\/[^)]+)\)$/);
        if (link) {
          const target = join(CONTENT_DIR, '..', link[1].slice(1));
          if (!await exists(`${target}.mdx`) && !await exists(join(target, 'index.mdx'))) {
            errors.push(`${relDir}/meta.json → "${entry}" (link target not found)`);
          }
          continue;
        }

        const asFile = join(dir, `${entry}.mdx`);
        const asDir = join(dir, entry);

        const fileExists = await exists(asFile);
        const dirExists = await exists(asDir);

        if (!fileExists && !dirExists) {
          errors.push(`${relDir}/meta.json → "${entry}" (no .mdx file or directory found)`);
        }
      }
    }

    expect(errors).toEqual([]);
  });

  test("no orphan .mdx files missing from meta.json", async () => {
    const metaFiles = await findMetaFiles(CONTENT_DIR);
    const orphans: string[] = [];

    // Check root level
    const rootMetaPath = join(CONTENT_DIR, "meta.json");
    const rootMeta = JSON.parse(await readFile(rootMetaPath, "utf-8"));
    const rootEntries = new Set(
      (rootMeta.pages as string[]).filter((e: string) => !isSeparator(e) && e !== "...")
    );
    // "..." means "include everything else", so skip orphan check for root
    if (rootEntries.has("...")) return;
    const rootFiles = await readdir(CONTENT_DIR, { withFileTypes: true });

    for (const file of rootFiles) {
      if (file.name === "meta.json") continue;
      const name = file.isFile() ? basename(file.name, ".mdx") : file.name;
      if (file.isFile() && !file.name.endsWith(".mdx")) continue;
      if (!rootEntries.has(name)) {
        orphans.push(`docs/${file.name}`);
      }
    }

    // Check each nested directory that has a meta.json
    for (const metaPath of metaFiles) {
      if (metaPath === rootMetaPath) continue;
      const dir = dirname(metaPath);
      const meta = JSON.parse(await readFile(metaPath, "utf-8"));
      const entries = new Set(
        ((meta.pages || []) as string[]).filter((e: string) => !isSeparator(e))
      );
      // "..." means "include everything else", so skip orphan check
      if (entries.has("...")) continue;

      const files = await readdir(dir, { withFileTypes: true });
      const relDir = relative(CONTENT_DIR, dir);

      for (const file of files) {
        if (file.name === "meta.json") continue;
        const name = file.isFile() ? basename(file.name, ".mdx") : file.name;
        if (file.isFile() && !file.name.endsWith(".mdx")) continue;
        if (!entries.has(name)) {
          orphans.push(`${relDir}/${file.name}`);
        }
      }
    }

    if (orphans.length > 0) {
      console.warn(
        `Found ${orphans.length} orphan file(s) not in any meta.json:\n` +
          orphans.map((o) => `  - ${o}`).join("\n")
      );
    }
    // Warn but don't fail — orphans aren't necessarily bugs
    expect(orphans.length).toBeLessThan(20);
  });
});