import test from "node:test";
import assert from "node:assert/strict";
import { readFile } from "node:fs/promises";

const read = (relative) => readFile(new URL(relative, import.meta.url), "utf8");

test("the app registers secondary screens without guarding the Chats root", async () => {
  const app = await read("../App.jsx");
  const provider = await read("./BackNavigationProvider.jsx");

  assert.match(app, /<BackNavigationProvider>/);
  assert.match(app, /useBackLayer\(screenLayerActive/);
  assert.match(app, /onBack=\{closeScreen\}/);
  assert.doesNotMatch(provider, /confirmExit/);
  assert.doesNotMatch(provider, /onRootBack/);
  assert.doesNotMatch(provider, /Exit Code Remote Manager\?/);
  assert.doesNotMatch(provider, /ensureRootGuard/);
  assert.doesNotMatch(provider, /exitApp/);
  assert.match(provider, /CapacitorApp\.addListener\("backButton"/);
  assert.match(provider, /if \(!navigation\.back\(\)\) void CapacitorApp\.minimizeApp\(\)/);
});

test("transient mobile surfaces participate in back navigation", async () => {
  const files = await Promise.all([
    read("../features/context/ContextBar.jsx"),
    read("../features/notifications/NotificationCenter.jsx"),
    read("../features/chats/ChatsPanel.jsx"),
    read("../features/changes/ChangeDiffSheet.jsx"),
    read("../features/conversation/ComposerModelMenu.jsx"),
    read("../features/conversation/ComposerPermissionMenu.jsx"),
    read("../features/conversation/ComposerToolsMenu.jsx"),
    read("../features/conversation/QueuedMessageList.jsx"),
    read("../features/conversation/ConversationTurn.jsx")
  ]);

  for (const source of files) assert.match(source, /useBackLayer|useDetailsBackLayer/);
  assert.match(files[4], /useDetailsBackLayer\(menuRef, "model-menu"\)/);
});
