import path from "node:path";
import { describe, expect, it } from "vitest";
import { discoverProject } from "../src/discovery";
import { findRootShells } from "../src/rules/root-shells";
import { themeHydrationSafeRule } from "../src/rules/theme-hydration-safe";
import { themeProviderMountedInShellRule } from "../src/rules/theme-provider-mounted-in-shell";
import { toastProviderMountedRule } from "../src/rules/toast-provider-mounted";
import { createRuleFixture, runRule } from "./rule-fixture";
const NEXT_THEME_DEPENDENCIES = {
next: "16.2.6",
"next-themes": "0.4.6",
react: "19.2.4",
sonner: "2.0.0",
};
const VITE_DEPENDENCIES = {
react: "19.2.4",
"react-dom": "19.2.4",
sonner: "2.0.0",
vite: "7.0.0",
};
const SAFE_ROOT_LAYOUT = `
import { ThemeProvider } from "next-themes";
import { Toaster } from "sonner";
export default function Layout({ children }) {
return
{children};
}
`;
const BARE_ROOT_LAYOUT =
'export default function Layout({ children }) { return {children}; }';
const getShellPaths = async (rootDir: string): Promise => {
const project = await discoverProject(rootDir);
const shells = await findRootShells(project);
return shells.map((shell) =>
shell.files.map((file) =>
path.relative(rootDir, file.path).split(path.sep).join("/")
)
);
};
describe("root shell discovery", () => {
it("treats a localized layout that renders as the root shell", async () => {
const fixture = await createRuleFixture(NEXT_THEME_DEPENDENCIES);
try {
await fixture.write(
"app/layout.tsx",
"export default function Layout({ children }) { return children; }"
);
await fixture.write("app/[locale]/layout.tsx", SAFE_ROOT_LAYOUT);
await fixture.write(
"app/[locale]/dashboard/layout.tsx",
"export default function Layout({ children }) { return {children}; }"
);
expect(await getShellPaths(fixture.rootDir)).toEqual([
["app/[locale]/layout.tsx"],
]);
expect(
(await runRule(fixture.rootDir, themeProviderMountedInShellRule)).status
).toBe("pass");
expect(
(await runRule(fixture.rootDir, themeHydrationSafeRule)).status
).toBe("pass");
expect(
(await runRule(fixture.rootDir, toastProviderMountedRule)).status
).toBe("pass");
await fixture.write("app/[locale]/layout.tsx", BARE_ROOT_LAYOUT);
const hydration = await runRule(fixture.rootDir, themeHydrationSafeRule);
expect(hydration.status).toBe("fail");
expect(hydration.evidence[0]?.filePath).toBe(
path.join("app", "[locale]", "layout.tsx")
);
} finally {
await fixture.cleanup();
}
});
it("checks every route-group root layout", async () => {
const fixture = await createRuleFixture(NEXT_THEME_DEPENDENCIES);
try {
await fixture.write("app/(marketing)/layout.tsx", SAFE_ROOT_LAYOUT);
await fixture.write("app/(shop)/layout.tsx", SAFE_ROOT_LAYOUT);
expect(await getShellPaths(fixture.rootDir)).toEqual([
["app/(marketing)/layout.tsx"],
["app/(shop)/layout.tsx"],
]);
expect(
(await runRule(fixture.rootDir, themeProviderMountedInShellRule)).status
).toBe("pass");
expect(
(await runRule(fixture.rootDir, themeHydrationSafeRule)).status
).toBe("pass");
await fixture.write("app/(shop)/layout.tsx", BARE_ROOT_LAYOUT);
const theme = await runRule(
fixture.rootDir,
themeProviderMountedInShellRule
);
const hydration = await runRule(fixture.rootDir, themeHydrationSafeRule);
expect(theme.status).toBe("fail");
expect(theme.evidence[0]?.filePath).toBe(
path.join("app", "(shop)", "layout.tsx")
);
expect(hydration.status).toBe("fail");
expect(hydration.evidence[0]?.filePath).toBe(
path.join("app", "(shop)", "layout.tsx")
);
// Toast mounting is satisfied by any root shell.
expect(
(await runRule(fixture.rootDir, toastProviderMountedRule)).status
).toBe("pass");
} finally {
await fixture.cleanup();
}
});
it("keeps the conventional root layout first when nested roots also render ", async () => {
const fixture = await createRuleFixture(NEXT_THEME_DEPENDENCIES);
try {
await fixture.write("app/layout.tsx", SAFE_ROOT_LAYOUT);
await fixture.write("app/(admin)/layout.tsx", SAFE_ROOT_LAYOUT);
expect(await getShellPaths(fixture.rootDir)).toEqual([
["app/layout.tsx"],
["app/(admin)/layout.tsx"],
]);
} finally {
await fixture.cleanup();
}
});
it("uses the Vite entry referenced by index.html for every shell rule", async () => {
const fixture = await createRuleFixture(VITE_DEPENDENCIES);
try {
await fixture.write(
"index.html",
''
);
await fixture.write(
"src/index.tsx",
`
import { createRoot } from "react-dom/client";
import { Toaster } from "sonner";
import { ThemeProvider } from "./theme-provider";
createRoot(document.getElementById("root")).render();
`
);
await fixture.write(
"src/theme-provider.tsx",
"export function ThemeProvider({ children }) { return children; }"
);
expect(await getShellPaths(fixture.rootDir)).toEqual([["src/index.tsx"]]);
expect(
(await runRule(fixture.rootDir, themeProviderMountedInShellRule)).status
).toBe("pass");
expect(
(await runRule(fixture.rootDir, toastProviderMountedRule)).status
).toBe("pass");
} finally {
await fixture.cleanup();
}
});
it("follows a non-conventional index.html entry before fallbacks", async () => {
const fixture = await createRuleFixture(VITE_DEPENDENCIES);
try {
await fixture.write(
"index.html",
''
);
await fixture.write(
"src/bootstrap.tsx",
'import { Toaster } from "sonner"; export const root = ;'
);
await fixture.write(
"src/App.tsx",
"export default function App() { return ; }"
);
expect(await getShellPaths(fixture.rootDir)).toEqual([
["src/bootstrap.tsx", "src/App.tsx"],
]);
expect(
(await runRule(fixture.rootDir, toastProviderMountedRule)).status
).toBe("pass");
} finally {
await fixture.cleanup();
}
});
it("ignores index.html entries outside the project root", async () => {
const fixture = await createRuleFixture(VITE_DEPENDENCIES);
try {
await fixture.write(
"index.html",
''
);
await fixture.write(
"src/main.tsx",
"export default function Main() { return ; }"
);
expect(await getShellPaths(fixture.rootDir)).toEqual([["src/main.tsx"]]);
} finally {
await fixture.cleanup();
}
});
});